PageRenderTime 34ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/mituav-udb3_quadrotor_control-265f56d90e10/UDB3.X/AHRS/AHRS.c

https://bitbucket.org/mituav/udb3_quadrotor_control
C | 330 lines | 183 code | 66 blank | 81 comment | 19 complexity | d5fb4ae21eaaf487ae126b7a39795474 MD5 | raw file
  1. #include "AHRS.h"
  2. /* globals */
  3. extern volatile tLoopFlags loop;
  4. extern volatile tSensorCal SensorCal;
  5. extern volatile tSensorData SensorData;
  6. extern volatile tAHRSdata AHRSdata;
  7. extern _Q16 num0p998, num0p0001, num0p05, num0p1, num0p2, num0p5, num0p8, num0p9, num0p95, num1p0, num1p1;
  8. // Initialize
  9. void AHRS_init(void){
  10. // Setup calibration struct
  11. SensorCal.biasCountGyro = 0;
  12. SensorCal.biasTotalGyro = 2000;
  13. SensorCal.blankReadsGyro = 200;
  14. SensorCal.biasCountAcc = 0;
  15. SensorCal.biasTotalAcc = 200;
  16. SensorCal.blankReadsAcc = 50;
  17. SensorCal.pBias = _Q16ftoi(0.0);
  18. SensorCal.qBias = _Q16ftoi(0.0);
  19. SensorCal.rBias = _Q16ftoi(0.0);
  20. // Initialize attitude
  21. AHRSdata.q_est.o = _Q16ftoi(1.0);
  22. AHRSdata.q_est.x = _Q16ftoi(0.0);
  23. AHRSdata.q_est.y = _Q16ftoi(0.0);
  24. AHRSdata.q_est.z = _Q16ftoi(0.0);
  25. AHRSdata.q_meas.o = _Q16ftoi(1.0);
  26. AHRSdata.q_meas.x = _Q16ftoi(0.0);
  27. AHRSdata.q_meas.y = _Q16ftoi(0.0);
  28. AHRSdata.q_meas.z = _Q16ftoi(0.0);
  29. AHRSdata.q_zero.o = _Q16ftoi(1.0);
  30. AHRSdata.q_zero.x = _Q16ftoi(0.0);
  31. AHRSdata.q_zero.y = _Q16ftoi(0.0);
  32. AHRSdata.q_zero.z = _Q16ftoi(0.0);
  33. // Parameters
  34. SensorCal.gyroRawBias = 512; // Mid-range value for 10-bit A2D
  35. SensorCal.accelRawBias= 500; // Mid-range value for 10-bit A2D (adjusted manually)
  36. SensorCal.gyroScale = _Q16ftoi(PI/180.0 / 1024.0 * 3.3 / 0.0033 * 1.5 ); // Based on 10-bit A2D and gyro sensitivity of 3.3mV / deg/s with fudge factor
  37. SensorCal.gyroPropDT = _Q16ftoi(0.001 / 2.0);
  38. SensorCal.accelScale = _Q16ftoi( 3.3 / 1024.0 / 0.200 ); // at 6g resolution, sensitivity is 200mV/g
  39. SensorCal.acc_window_min = _Q16ftoi(0.5);
  40. SensorCal.acc_window_max = _Q16ftoi(1.5);
  41. SensorCal.axBias = _Q16ftoi(0.0);
  42. SensorCal.ayBias = _Q16ftoi(0.0);
  43. SensorCal.azBias = _Q16ftoi(0.0);
  44. SensorCal.accFiltA = _Q16ftoi( 0.9 );
  45. SensorCal.accFiltB = _Q16ftoi( 0.1 );
  46. SensorCal.correctSatLow = _Q16ftoi( -0.025 );
  47. SensorCal.correctSatHigh = _Q16ftoi( 0.025 );
  48. //SensorCal.K_AttFilter = _Q16ftoi(0.025); // Default value, may be overwritten by SensorPacket
  49. SensorCal.K_AttFilter = _Q16ftoi(0.01); //Debug: No Acc correction
  50. }
  51. void AHRS_GyroProp(void){
  52. // $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
  53. // Read raw gyro values from A2D registers, A2D automatically scans inputs at 5KHz
  54. // $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
  55. SensorData.gyroX = xgyro; SensorData.gyroX -= SensorCal.gyroRawBias;
  56. SensorData.gyroY = ygyro; SensorData.gyroY -= SensorCal.gyroRawBias;
  57. SensorData.gyroZ = zgyro; SensorData.gyroZ -= SensorCal.gyroRawBias;
  58. // $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
  59. // Gyro scaling, to rad/sec
  60. // $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
  61. int16toQ16(&AHRSdata.p, &SensorData.gyroX);
  62. AHRSdata.p = -mult( AHRSdata.p, SensorCal.gyroScale);
  63. int16toQ16(&AHRSdata.q, &SensorData.gyroY);
  64. AHRSdata.q = mult( AHRSdata.q, SensorCal.gyroScale );
  65. int16toQ16(&AHRSdata.r, &SensorData.gyroZ);
  66. AHRSdata.r = mult( AHRSdata.r, SensorCal.gyroScale );
  67. // $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
  68. // Initial gyro bias calculation
  69. // $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
  70. if( SensorCal.biasCountGyro < SensorCal.biasTotalGyro){
  71. // Do some blank reads to clear any garbage in the initial transient
  72. if(--SensorCal.blankReadsGyro > 0)
  73. return;
  74. SensorCal.pBias += AHRSdata.p;
  75. SensorCal.qBias += AHRSdata.q;
  76. SensorCal.rBias += AHRSdata.r;
  77. led_on(LED_RED);
  78. if( ++SensorCal.biasCountGyro == SensorCal.biasTotalGyro ){
  79. _Q16 tmp = _Q16ftoi(1.0 / ((float)SensorCal.biasTotalGyro ));
  80. //SensorCal.pBias = mult( SensorCal.pBias, tmp);
  81. //SensorCal.qBias = mult( SensorCal.qBias, tmp);
  82. //SensorCal.rBias = mult( SensorCal.rBias, tmp);
  83. tmp = _Q16ftoi((float)SensorCal.biasTotalGyro );
  84. SensorCal.pBias = _IQ16div( SensorCal.pBias, tmp );
  85. SensorCal.qBias = _IQ16div( SensorCal.qBias, tmp );
  86. SensorCal.rBias = _IQ16div( SensorCal.rBias, tmp );
  87. led_off(LED_RED);
  88. led_off(LED_GREEN);
  89. }
  90. // TODO: Initialize q_est to q_meas
  91. return;
  92. }
  93. // $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
  94. // Gyro bias correction
  95. // $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
  96. AHRSdata.p -= SensorCal.pBias;
  97. AHRSdata.q -= SensorCal.qBias;
  98. AHRSdata.r -= SensorCal.rBias;
  99. // $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
  100. // Gyro propagation
  101. // $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
  102. AHRSdata.q_est.o -= mult( mult(AHRSdata.q_est.x,AHRSdata.p) + mult(AHRSdata.q_est.y,AHRSdata.q) + mult(AHRSdata.q_est.z,AHRSdata.r) , SensorCal.gyroPropDT);
  103. AHRSdata.q_est.x += mult( mult(AHRSdata.q_est.o,AHRSdata.p) - mult(AHRSdata.q_est.z,AHRSdata.q) + mult(AHRSdata.q_est.y,AHRSdata.r) , SensorCal.gyroPropDT);
  104. AHRSdata.q_est.y += mult( mult(AHRSdata.q_est.z,AHRSdata.p) + mult(AHRSdata.q_est.o,AHRSdata.q) - mult(AHRSdata.q_est.x,AHRSdata.r) , SensorCal.gyroPropDT);
  105. AHRSdata.q_est.z += mult( mult(AHRSdata.q_est.x,AHRSdata.q) - mult(AHRSdata.q_est.y,AHRSdata.p) + mult(AHRSdata.q_est.o,AHRSdata.r) , SensorCal.gyroPropDT);
  106. // Run the attitude control after propogating gyros
  107. loop.AttCtl = 1;
  108. }
  109. void AHRS_AccMagCorrect(void)
  110. {
  111. // $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
  112. // Read raw accel values from A2D registers, A2D automatically scans inputs at 5KHz
  113. // $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
  114. SensorData.accX = xaccel; SensorData.accX -= SensorCal.accelRawBias;
  115. SensorData.accY = yaccel; SensorData.accY -= SensorCal.accelRawBias;
  116. SensorData.accZ = zaccel; SensorData.accZ -= SensorCal.accelRawBias;
  117. // $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
  118. // Accel scaling, to g
  119. // $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
  120. int16toQ16(&AHRSdata.ax, &SensorData.accX);
  121. AHRSdata.ax = -mult( AHRSdata.ax, SensorCal.accelScale);
  122. int16toQ16(&AHRSdata.ay, &SensorData.accY);
  123. AHRSdata.ay = mult( AHRSdata.ay, SensorCal.accelScale );
  124. int16toQ16(&AHRSdata.az, &SensorData.accZ);
  125. AHRSdata.az = -mult( AHRSdata.az, SensorCal.accelScale );
  126. // $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
  127. // Initial acc bias calculation
  128. // $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
  129. if( SensorCal.biasCountAcc < SensorCal.biasTotalAcc){
  130. // Do some blank reads to clear any garbage in the initial transient
  131. if(--SensorCal.blankReadsAcc > 0)
  132. return;
  133. SensorCal.axBias += AHRSdata.ax;
  134. SensorCal.ayBias += AHRSdata.ay;
  135. SensorCal.azBias += AHRSdata.az;
  136. led_on(LED_RED);
  137. if( ++SensorCal.biasCountAcc == SensorCal.biasTotalAcc ){
  138. _Q16 tmp = _Q16ftoi(1.0 / ((float)SensorCal.biasTotalAcc ));
  139. SensorCal.axBias = mult( SensorCal.axBias, tmp);
  140. SensorCal.ayBias = mult( SensorCal.ayBias, tmp);
  141. //Z is different, and hast to be biased by 1g
  142. SensorCal.azBias = mult( SensorCal.azBias, tmp) + _Q16ftoi(1.0);
  143. led_off(LED_RED);
  144. led_off(LED_GREEN);
  145. }
  146. return;
  147. }
  148. // $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
  149. // Acc bias correction
  150. // $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
  151. AHRSdata.ax = AHRSdata.ax - SensorCal.axBias;
  152. AHRSdata.axavg = mult(AHRSdata.ax, SensorCal.accFiltA) + mult(AHRSdata.axavg, SensorCal.accFiltB);
  153. AHRSdata.ay = AHRSdata.ay - SensorCal.ayBias;
  154. AHRSdata.ayavg = mult(AHRSdata.ay, SensorCal.accFiltA) + mult(AHRSdata.ayavg, SensorCal.accFiltB);
  155. AHRSdata.az = AHRSdata.az - SensorCal.azBias;
  156. AHRSdata.azavg = mult(AHRSdata.az, SensorCal.accFiltA) + mult(AHRSdata.azavg, SensorCal.accFiltB);
  157. _Q16 ax = AHRSdata.axavg;
  158. _Q16 ay = AHRSdata.ayavg;
  159. _Q16 az = AHRSdata.azavg;
  160. // $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
  161. // Maneuver detector, do not use accels during fast movement
  162. // $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
  163. // TODO: Check angular rates
  164. // Roll and pitch calculation, assumes accelerometer units are 10000*g
  165. // Normalize the acceleration vector to length 1
  166. _Q16 root = _Q16sqrt( mult(ax,ax) + mult(ay,ay) + mult(az,az));
  167. // TODO: Make sure we're around 1g
  168. if( root < SensorCal.acc_window_min || root > SensorCal.acc_window_max){
  169. return;
  170. }
  171. // Normalize
  172. ax = _IQ16div(ax, root);
  173. ay = _IQ16div(ay, root);
  174. az = _IQ16div(az, root);
  175. // Too close to singularity (due to numerical precision limits)
  176. if( ax > num0p998 || -ax > num0p998 )
  177. return;
  178. root = _Q16sqrt( mult(ay,ay) + mult(az,az));
  179. if(root < num0p0001 )
  180. root = num0p0001;
  181. // Calculate sin/cos of roll and pitch
  182. _Q16 sinR = - _IQ16div(ay,root);
  183. _Q16 cosR = - _IQ16div(az,root);
  184. _Q16 sinP = ax;
  185. _Q16 cosP = -( mult(ay,sinR) + mult(az,cosR) );
  186. // Calculate half-angles
  187. _Q16 cosR2 = _Q16sqrt( mult( num1p0 + cosR , num0p5 ));
  188. if(cosR2 < num0p0001 )
  189. cosR2 = num0p0001;
  190. _Q16 sinR2 = mult(_IQ16div( sinR , cosR2) , num0p5 ); // WARNING: This step is numerically ill-behaved!
  191. _Q16 cosP2 = _Q16sqrt( mult( num1p0 + cosP , num0p5 ));
  192. if(cosP2 < num0p0001 )
  193. cosP2 = num0p0001;
  194. _Q16 sinP2 = mult(_IQ16div( sinP , cosP2) , num0p5 ); // WARNING: This step is numerically ill-behaved!
  195. // Too close to singularity (due to numerical precision limits)
  196. if( mult(cosR2,cosR2) + mult(sinR2,sinR2) > num1p1 || mult(cosP2,cosP2) + mult(sinP2,sinP2) > num1p1 )
  197. return;
  198. // Yaw calculation
  199. // Normalize the magnetometer vector to length 1
  200. /* magx = (float)AHRSdata.magY;
  201. magy = -(float)AHRSdata.magX;
  202. magz = (float)AHRSdata.magZ;
  203. // Todo: magx*magx can be done in fixed pt
  204. root = sqrt( magx*magx + magy*magy + magz*magz );
  205. magx /= root;
  206. magy /= root;
  207. magz /= root;
  208. yaw = atan2(-cosR*magy - sinR*magz , cosP*magx+sinP*sinR*magy-sinP*cosR*magz);
  209. yaw += PI;
  210. if(yaw > PI){
  211. yaw -= 2*PI;
  212. }
  213. sinY2 = sin(yaw/2.0);
  214. cosY2 = cos(yaw/2.0);
  215. */
  216. _Q16 cosY2 = _Q16ftoi(1.0);
  217. _Q16 sinY2 = 0;
  218. // Finally get quaternion
  219. tQuaternion qroll,qpitch,qyaw;
  220. qyaw.o = cosY2; qyaw.x = 0; qyaw.y = 0; qyaw.z = sinY2;
  221. qpitch.o = cosP2; qpitch.x = 0; qpitch.y = sinP2; qpitch.z = 0;
  222. qroll.o = cosR2; qroll.x = sinR2; qroll.y = 0; qroll.z = 0;
  223. AHRSdata.q_meas = qprod(qyaw,qpitch);
  224. AHRSdata.q_meas = qprod(AHRSdata.q_meas, qroll);
  225. // Check if flipped from last measurement
  226. if( mult(AHRSdata.q_meas.x,AHRSdata.q_est.x) + mult(AHRSdata.q_meas.y,AHRSdata.q_est.y) + mult(AHRSdata.q_meas.z,AHRSdata.q_est.z) + mult(AHRSdata.q_meas.o,AHRSdata.q_est.o) < 0 )
  227. {
  228. AHRSdata.q_meas.o = - AHRSdata.q_meas.o;
  229. AHRSdata.q_meas.x = - AHRSdata.q_meas.x;
  230. AHRSdata.q_meas.y = - AHRSdata.q_meas.y;
  231. AHRSdata.q_meas.z = - AHRSdata.q_meas.z;
  232. }
  233. // Gyro bias estimation
  234. // Make the correction
  235. AHRSdata.q_est.o -= mult(AHRSdata.q_est.o-AHRSdata.q_meas.o, SensorCal.K_AttFilter);
  236. AHRSdata.q_est.x -= mult(AHRSdata.q_est.x-AHRSdata.q_meas.x, SensorCal.K_AttFilter);
  237. AHRSdata.q_est.y -= mult(AHRSdata.q_est.y-AHRSdata.q_meas.y, SensorCal.K_AttFilter);
  238. AHRSdata.q_est.z -= mult(AHRSdata.q_est.z-AHRSdata.q_meas.z, SensorCal.K_AttFilter);
  239. }
  240. volatile tQuaternion qerr;
  241. void AHRS_ZeroCorrect( void )
  242. {
  243. // Check if flipped from last measurement
  244. if( mult(AHRSdata.q_zero.x,AHRSdata.q_est.x) + mult(AHRSdata.q_zero.y,AHRSdata.q_est.y) + mult(AHRSdata.q_zero.z,AHRSdata.q_est.z) + mult(AHRSdata.q_zero.o,AHRSdata.q_est.o) < 0 )
  245. {
  246. AHRSdata.q_zero.o = - AHRSdata.q_zero.o;
  247. AHRSdata.q_zero.x = - AHRSdata.q_zero.x;
  248. AHRSdata.q_zero.y = - AHRSdata.q_zero.y;
  249. AHRSdata.q_zero.z = - AHRSdata.q_zero.z;
  250. }
  251. qerr.o = AHRSdata.q_est.o-AHRSdata.q_zero.o;
  252. qerr.x = AHRSdata.q_est.x-AHRSdata.q_zero.x;
  253. qerr.y = AHRSdata.q_est.y-AHRSdata.q_zero.y;
  254. qerr.z = AHRSdata.q_est.z-AHRSdata.q_zero.z;
  255. //_Q16sat(&qerr.o, SensorCal.correctSatHigh, SensorCal.correctSatLow);
  256. //_Q16sat(&qerr.x, SensorCal.correctSatHigh, SensorCal.correctSatLow);
  257. //_Q16sat(&qerr.y, SensorCal.correctSatHigh, SensorCal.correctSatLow);
  258. //_Q16sat(&qerr.z, SensorCal.correctSatHigh, SensorCal.correctSatLow);
  259. // Make the correction
  260. AHRSdata.q_est.o -= mult(qerr.o , SensorCal.K_AttFilter);
  261. AHRSdata.q_est.x -= mult(qerr.x, SensorCal.K_AttFilter);
  262. AHRSdata.q_est.y -= mult(qerr.y, SensorCal.K_AttFilter);
  263. AHRSdata.q_est.z -= mult(qerr.z, SensorCal.K_AttFilter);
  264. }