imu_mouse.c 12 KB

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