imu_mouse.c 12 KB

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