imu_mouse.c 11 KB

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