imu.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. #include <furi.h>
  2. #include "imu.h"
  3. #include "ICM42688P/ICM42688P.h"
  4. #define TAG "IMU"
  5. #define ACCEL_GYRO_RATE DataRate100Hz
  6. #define FILTER_SAMPLE_FREQ 100.f
  7. #define FILTER_BETA 0.08f
  8. #define SAMPLE_RATE_DIV 5
  9. #define SENSITIVITY_K 30.f
  10. #define EXP_RATE 1.1f
  11. #define IMU_CALI_AVG 64
  12. typedef enum {
  13. ImuStop = (1 << 0),
  14. ImuNewData = (1 << 1),
  15. } ImuThreadFlags;
  16. #define FLAGS_ALL (ImuStop | ImuNewData)
  17. typedef struct {
  18. float q0;
  19. float q1;
  20. float q2;
  21. float q3;
  22. float roll;
  23. float pitch;
  24. float yaw;
  25. } ImuProcessedData;
  26. typedef struct {
  27. FuriThread* thread;
  28. ICM42688P* icm42688p;
  29. ImuProcessedData processed_data;
  30. } ImuThread;
  31. static void imu_madgwick_filter(
  32. ImuProcessedData* out,
  33. ICM42688PScaledData* accel,
  34. ICM42688PScaledData* gyro);
  35. static void imu_irq_callback(void* context) {
  36. furi_assert(context);
  37. ImuThread* imu = context;
  38. furi_thread_flags_set(furi_thread_get_id(imu->thread), ImuNewData);
  39. }
  40. static void imu_process_data(ImuThread* imu, ICM42688PFifoPacket* in_data) {
  41. ICM42688PScaledData accel_data;
  42. ICM42688PScaledData gyro_data;
  43. // Get accel and gyro data in g and degrees/s
  44. icm42688p_apply_scale_fifo(imu->icm42688p, in_data, &accel_data, &gyro_data);
  45. // Gyro: degrees/s to rads/s
  46. gyro_data.x = gyro_data.x / 180.f * M_PI;
  47. gyro_data.y = gyro_data.y / 180.f * M_PI;
  48. gyro_data.z = gyro_data.z / 180.f * M_PI;
  49. // Sensor Fusion algorithm
  50. ImuProcessedData* out = &imu->processed_data;
  51. imu_madgwick_filter(out, &accel_data, &gyro_data);
  52. // Quaternion to euler angles
  53. float roll = atan2f(
  54. out->q0 * out->q1 + out->q2 * out->q3, 0.5f - out->q1 * out->q1 - out->q2 * out->q2);
  55. float pitch = asinf(-2.0f * (out->q1 * out->q3 - out->q0 * out->q2));
  56. float yaw = atan2f(
  57. out->q1 * out->q2 + out->q0 * out->q3, 0.5f - out->q2 * out->q2 - out->q3 * out->q3);
  58. // Euler angles: rads to degrees
  59. out->roll = roll / M_PI * 180.f;
  60. out->pitch = pitch / M_PI * 180.f;
  61. out->yaw = yaw / M_PI * 180.f;
  62. }
  63. static void calibrate_gyro(ImuThread* imu) {
  64. ICM42688PRawData data;
  65. ICM42688PScaledData offset_scaled = {.x = 0.f, .y = 0.f, .z = 0.f};
  66. icm42688p_write_gyro_offset(imu->icm42688p, &offset_scaled);
  67. furi_delay_ms(10);
  68. int32_t avg_x = 0;
  69. int32_t avg_y = 0;
  70. int32_t avg_z = 0;
  71. for(uint8_t i = 0; i < IMU_CALI_AVG; i++) {
  72. icm42688p_read_gyro_raw(imu->icm42688p, &data);
  73. avg_x += data.x;
  74. avg_y += data.y;
  75. avg_z += data.z;
  76. furi_delay_ms(2);
  77. }
  78. data.x = avg_x / IMU_CALI_AVG;
  79. data.y = avg_y / IMU_CALI_AVG;
  80. data.z = avg_z / IMU_CALI_AVG;
  81. icm42688p_apply_scale(&data, icm42688p_gyro_get_full_scale(imu->icm42688p), &offset_scaled);
  82. FURI_LOG_I(
  83. TAG,
  84. "Offsets: x %f, y %f, z %f",
  85. (double)offset_scaled.x,
  86. (double)offset_scaled.y,
  87. (double)offset_scaled.z);
  88. icm42688p_write_gyro_offset(imu->icm42688p, &offset_scaled);
  89. }
  90. // static float imu_angle_diff(float a, float b) {
  91. // float diff = a - b;
  92. // if(diff > 180.f)
  93. // diff -= 360.f;
  94. // else if(diff < -180.f)
  95. // diff += 360.f;
  96. // return diff;
  97. // }
  98. static int32_t imu_thread(void* context) {
  99. furi_assert(context);
  100. ImuThread* imu = context;
  101. // float yaw_last = 0.f;
  102. // float pitch_last = 0.f;
  103. // float diff_x = 0.f;
  104. // float diff_y = 0.f;
  105. calibrate_gyro(imu);
  106. icm42688p_accel_config(imu->icm42688p, AccelFullScale16G, ACCEL_GYRO_RATE);
  107. icm42688p_gyro_config(imu->icm42688p, GyroFullScale2000DPS, ACCEL_GYRO_RATE);
  108. imu->processed_data.q0 = 1.f;
  109. imu->processed_data.q1 = 0.f;
  110. imu->processed_data.q2 = 0.f;
  111. imu->processed_data.q3 = 0.f;
  112. icm42688_fifo_enable(imu->icm42688p, imu_irq_callback, imu);
  113. while(1) {
  114. uint32_t events = furi_thread_flags_wait(FLAGS_ALL, FuriFlagWaitAny, FuriWaitForever);
  115. if(events & ImuStop) {
  116. break;
  117. }
  118. if(events & ImuNewData) {
  119. uint16_t data_pending = icm42688_fifo_get_count(imu->icm42688p);
  120. ICM42688PFifoPacket data;
  121. while(data_pending--) {
  122. icm42688_fifo_read(imu->icm42688p, &data);
  123. imu_process_data(imu, &data);
  124. }
  125. }
  126. }
  127. icm42688_fifo_disable(imu->icm42688p);
  128. return 0;
  129. }
  130. ImuThread* imu_start(ICM42688P* icm42688p) {
  131. ImuThread* imu = malloc(sizeof(ImuThread));
  132. imu->icm42688p = icm42688p;
  133. imu->thread = furi_thread_alloc_ex("ImuThread", 4096, imu_thread, imu);
  134. furi_thread_start(imu->thread);
  135. return imu;
  136. }
  137. void imu_stop(ImuThread* imu) {
  138. furi_assert(imu);
  139. furi_thread_flags_set(furi_thread_get_id(imu->thread), ImuStop);
  140. furi_thread_join(imu->thread);
  141. furi_thread_free(imu->thread);
  142. free(imu);
  143. }
  144. static float imu_inv_sqrt(float number) {
  145. union {
  146. float f;
  147. uint32_t i;
  148. } conv = {.f = number};
  149. conv.i = 0x5F3759Df - (conv.i >> 1);
  150. conv.f *= 1.5f - (number * 0.5f * conv.f * conv.f);
  151. return conv.f;
  152. }
  153. /* Simple madgwik filter, based on: https://github.com/arduino-libraries/MadgwickAHRS/ */
  154. static void imu_madgwick_filter(
  155. ImuProcessedData* out,
  156. ICM42688PScaledData* accel,
  157. ICM42688PScaledData* gyro) {
  158. float recipNorm;
  159. float s0, s1, s2, s3;
  160. float qDot1, qDot2, qDot3, qDot4;
  161. float _2q0, _2q1, _2q2, _2q3, _4q0, _4q1, _4q2, _8q1, _8q2, q0q0, q1q1, q2q2, q3q3;
  162. // Rate of change of quaternion from gyroscope
  163. qDot1 = 0.5f * (-out->q1 * gyro->x - out->q2 * gyro->y - out->q3 * gyro->z);
  164. qDot2 = 0.5f * (out->q0 * gyro->x + out->q2 * gyro->z - out->q3 * gyro->y);
  165. qDot3 = 0.5f * (out->q0 * gyro->y - out->q1 * gyro->z + out->q3 * gyro->x);
  166. qDot4 = 0.5f * (out->q0 * gyro->z + out->q1 * gyro->y - out->q2 * gyro->x);
  167. // Compute feedback only if accelerometer measurement valid (avoids NaN in accelerometer normalisation)
  168. if(!((accel->x == 0.0f) && (accel->y == 0.0f) && (accel->z == 0.0f))) {
  169. // Normalise accelerometer measurement
  170. recipNorm = imu_inv_sqrt(accel->x * accel->x + accel->y * accel->y + accel->z * accel->z);
  171. accel->x *= recipNorm;
  172. accel->y *= recipNorm;
  173. accel->z *= recipNorm;
  174. // Auxiliary variables to avoid repeated arithmetic
  175. _2q0 = 2.0f * out->q0;
  176. _2q1 = 2.0f * out->q1;
  177. _2q2 = 2.0f * out->q2;
  178. _2q3 = 2.0f * out->q3;
  179. _4q0 = 4.0f * out->q0;
  180. _4q1 = 4.0f * out->q1;
  181. _4q2 = 4.0f * out->q2;
  182. _8q1 = 8.0f * out->q1;
  183. _8q2 = 8.0f * out->q2;
  184. q0q0 = out->q0 * out->q0;
  185. q1q1 = out->q1 * out->q1;
  186. q2q2 = out->q2 * out->q2;
  187. q3q3 = out->q3 * out->q3;
  188. // Gradient decent algorithm corrective step
  189. s0 = _4q0 * q2q2 + _2q2 * accel->x + _4q0 * q1q1 - _2q1 * accel->y;
  190. s1 = _4q1 * q3q3 - _2q3 * accel->x + 4.0f * q0q0 * out->q1 - _2q0 * accel->y - _4q1 +
  191. _8q1 * q1q1 + _8q1 * q2q2 + _4q1 * accel->z;
  192. s2 = 4.0f * q0q0 * out->q2 + _2q0 * accel->x + _4q2 * q3q3 - _2q3 * accel->y - _4q2 +
  193. _8q2 * q1q1 + _8q2 * q2q2 + _4q2 * accel->z;
  194. s3 = 4.0f * q1q1 * out->q3 - _2q1 * accel->x + 4.0f * q2q2 * out->q3 - _2q2 * accel->y;
  195. recipNorm =
  196. imu_inv_sqrt(s0 * s0 + s1 * s1 + s2 * s2 + s3 * s3); // normalise step magnitude
  197. s0 *= recipNorm;
  198. s1 *= recipNorm;
  199. s2 *= recipNorm;
  200. s3 *= recipNorm;
  201. // Apply feedback step
  202. qDot1 -= FILTER_BETA * s0;
  203. qDot2 -= FILTER_BETA * s1;
  204. qDot3 -= FILTER_BETA * s2;
  205. qDot4 -= FILTER_BETA * s3;
  206. }
  207. // Integrate rate of change of quaternion to yield quaternion
  208. out->q0 += qDot1 * (1.0f / FILTER_SAMPLE_FREQ);
  209. out->q1 += qDot2 * (1.0f / FILTER_SAMPLE_FREQ);
  210. out->q2 += qDot3 * (1.0f / FILTER_SAMPLE_FREQ);
  211. out->q3 += qDot4 * (1.0f / FILTER_SAMPLE_FREQ);
  212. // Normalise quaternion
  213. recipNorm = imu_inv_sqrt(
  214. out->q0 * out->q0 + out->q1 * out->q1 + out->q2 * out->q2 + out->q3 * out->q3);
  215. out->q0 *= recipNorm;
  216. out->q1 *= recipNorm;
  217. out->q2 *= recipNorm;
  218. out->q3 *= recipNorm;
  219. }
  220. /* IMU API */
  221. struct Imu {
  222. FuriHalSpiBusHandle* icm42688p_device;
  223. ICM42688P* icm42688p;
  224. ImuThread* thread;
  225. bool present;
  226. };
  227. Imu* imu_alloc(void) {
  228. Imu* imu = malloc(sizeof(Imu));
  229. imu->icm42688p_device = malloc(sizeof(FuriHalSpiBusHandle));
  230. memcpy(imu->icm42688p_device, &furi_hal_spi_bus_handle_external, sizeof(FuriHalSpiBusHandle));
  231. imu->icm42688p_device->cs = &gpio_ext_pc3;
  232. imu->icm42688p = icm42688p_alloc(imu->icm42688p_device, &gpio_ext_pb2);
  233. imu->present = icm42688p_init(imu->icm42688p);
  234. if(imu->present) {
  235. imu->thread = imu_start(imu->icm42688p);
  236. }
  237. return imu;
  238. }
  239. void imu_free(Imu* imu) {
  240. if(imu->present) {
  241. imu_stop(imu->thread);
  242. }
  243. icm42688p_deinit(imu->icm42688p);
  244. icm42688p_free(imu->icm42688p);
  245. free(imu->icm42688p_device);
  246. free(imu);
  247. }
  248. bool imu_present(Imu* imu) {
  249. return imu->present;
  250. }
  251. float imu_pitch_get(Imu* imu) {
  252. // we pretend that reading a float is an atomic operation
  253. return imu->thread->processed_data.pitch;
  254. }
  255. float imu_roll_get(Imu* imu) {
  256. // we pretend that reading a float is an atomic operation
  257. return imu->thread->processed_data.roll;
  258. }
  259. float imu_yaw_get(Imu* imu) {
  260. // we pretend that reading a float is an atomic operation
  261. return imu->thread->processed_data.yaw;
  262. }