MPU6050.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710
  1. /*
  2. * MPU6050.c
  3. *
  4. * Created on: Oct 26, 2021
  5. * Author: Administrator
  6. */
  7. #include "MPU6050.h"
  8. #include "main.h"
  9. #include <stdio.h>
  10. extern int t;
  11. extern int a;
  12. extern int b;
  13. extern int c;
  14. //void addr_check_point(u8 addr){
  15. // if (addr != 0x69)
  16. // {
  17. // printf("checkpoint error addr");
  18. // }
  19. //}
  20. //void common_check_point(u8 value){
  21. //printf("<checkpoint> value => %x\n", value);
  22. //}
  23. u8 IIC_Write_Byte(u8 reg, u8 value)
  24. {
  25. return IIC_Write_Len(a, reg, 1, &value);
  26. }
  27. u8 IIC_Read_Byte(u8 reg)
  28. {
  29. u8 value = 0;
  30. IIC_Read_Len(a, reg, 1, &value);
  31. return value;
  32. }
  33. u8 IIC_Write_Len1(u8 addr, u8 reg, u8 len, u8 *buf)
  34. {
  35. addr=a;
  36. addr = (addr << 1) | 0;
  37. u8 data[10] = {0};
  38. data[0] = reg;
  39. for (int i = 0; i < len; i++)
  40. {
  41. data[1 + i] = buf[i];
  42. }
  43. if (HAL_I2C_Master_Transmit(&hi2c1, (uint16_t)addr, (uint8_t *)data, len + 1, 10000) != HAL_OK
  44. ||HAL_I2C_Master_Transmit(&hi2c2, (uint16_t)addr, (uint8_t *)data, len + 1, 10000) != HAL_OK
  45. ||HAL_I2C_Master_Transmit(&hi2c3, (uint16_t)addr, (uint8_t *)data, len + 1, 10000) != HAL_OK)
  46. {
  47. printf("++write len byte fail\r\n");
  48. return 1;
  49. }
  50. return 0;
  51. }
  52. // IIC连续写
  53. u8 IIC_Write_Len(u8 addr, u8 reg, u8 len, u8 *buf)
  54. {
  55. addr=a;
  56. if (t == 1)
  57. {
  58. addr = (addr << 1) | 0;
  59. u8 data[10] = {0};
  60. data[0] = reg;
  61. for (int i = 0; i < len; i++)
  62. {
  63. data[1 + i] = buf[i];
  64. }
  65. if (HAL_I2C_Master_Transmit(&hi2c1, (uint16_t)addr, (uint8_t *)data, len + 1, 10000) != HAL_OK)
  66. {
  67. printf("++write len byte fail\r\n");
  68. return 1;
  69. }
  70. }
  71. else if (t == 2)
  72. {
  73. addr = (addr << 1) | 0;
  74. u8 data[10] = {0};
  75. data[0] = reg;
  76. for (int i = 0; i < len; i++)
  77. {
  78. data[1 + i] = buf[i];
  79. }
  80. if (HAL_I2C_Master_Transmit(&hi2c2, (uint16_t)addr, (uint8_t *)data, len + 1, 10000) != HAL_OK)
  81. {
  82. printf("++write len byte fail\r\n");
  83. return 1;
  84. }
  85. }
  86. else if (t == 3)
  87. {
  88. addr = (addr << 1) | 0;
  89. u8 data[10] = {0};
  90. data[0] = reg;
  91. for (int i = 0; i < len; i++)
  92. {
  93. data[1 + i] = buf[i];
  94. }
  95. if (HAL_I2C_Master_Transmit(&hi2c3, (uint16_t)addr, (uint8_t *)data, len + 1, 10000) != HAL_OK)
  96. {
  97. printf("++write len byte fail\r\n");
  98. return 1;
  99. }
  100. }
  101. return 0;
  102. }
  103. u8 IIC_Read_Len1(u8 addr, u8 reg, u8 len, u8 *buf)
  104. {addr=a;
  105. u8 tmp = (addr << 1) | 0;
  106. if (HAL_I2C_Master_Transmit(&hi2c1, (uint16_t)tmp, (uint8_t *)&reg, 1, 10000) != HAL_OK
  107. ||HAL_I2C_Master_Transmit(&hi2c2, (uint16_t)tmp, (uint8_t *)&reg, 1, 10000) != HAL_OK
  108. ||HAL_I2C_Master_Transmit(&hi2c3, (uint16_t)tmp, (uint8_t *)&reg, 1, 10000) != HAL_OK)
  109. {
  110. printf("++write byte fail\r\n");
  111. return 1;
  112. // return addr_test;
  113. }
  114. tmp = (addr << 1) | 1;
  115. if (HAL_I2C_Master_Receive(&hi2c1, (uint16_t)tmp, (uint8_t *)buf, len, 10000) != HAL_OK
  116. ||HAL_I2C_Master_Receive(&hi2c2, (uint16_t)tmp, (uint8_t *)buf, len, 10000) != HAL_OK
  117. ||HAL_I2C_Master_Receive(&hi2c3, (uint16_t)tmp, (uint8_t *)buf, len, 10000) != HAL_OK)
  118. {
  119. printf("--read byte fail\r\n");
  120. }
  121. }
  122. // IIC连续读
  123. u8 IIC_Read_Len(u8 addr, u8 reg, u8 len, u8 *buf)
  124. {
  125. addr=a;
  126. if (t == 1)
  127. {
  128. u8 tmp = (addr << 1) | 0;
  129. if (HAL_I2C_Master_Transmit(&hi2c1, (uint16_t)tmp, (uint8_t *)&reg, 1, 10000) != HAL_OK)
  130. {
  131. printf("++write byte fail\r\n");
  132. return 1;
  133. // return addr_test;
  134. }
  135. tmp = (addr << 1) | 1;
  136. if (HAL_I2C_Master_Receive(&hi2c1, (uint16_t)tmp, (uint8_t *)buf, len, 10000) != HAL_OK)
  137. {
  138. printf("--read byte fail\r\n");
  139. }
  140. }
  141. else if (t == 2)
  142. {
  143. u8 tmp = (addr << 1) | 0;
  144. if (HAL_I2C_Master_Transmit(&hi2c2, (uint16_t)tmp, (uint8_t *)&reg, 1, 10000) != HAL_OK)
  145. {
  146. printf("++write byte fail\r\n");
  147. return 1;
  148. // return addr_test;
  149. }
  150. tmp = (addr << 1) | 1;
  151. if (HAL_I2C_Master_Receive(&hi2c2, (uint16_t)tmp, (uint8_t *)buf, len, 10000) != HAL_OK)
  152. {
  153. printf("--read byte fail\r\n");
  154. }
  155. }
  156. else if (t == 3)
  157. {
  158. u8 tmp = (addr << 1) | 0;
  159. if (HAL_I2C_Master_Transmit(&hi2c3, (uint16_t)tmp, (uint8_t *)&reg, 1, 10000) != HAL_OK)
  160. {
  161. printf("++write byte fail\r\n");
  162. return 1;
  163. // return addr_test;
  164. }
  165. tmp = (addr << 1) | 1;
  166. if (HAL_I2C_Master_Receive(&hi2c3, (uint16_t)tmp, (uint8_t *)buf, len, 10000) != HAL_OK)
  167. {
  168. printf("--read byte fail\r\n");
  169. }
  170. }
  171. // return -1;
  172. }
  173. // 初始化MPU6050
  174. // 返回值: 0,成功
  175. // 其他,错误代码
  176. u8 MPU_Init(void)
  177. {
  178. u8 res = 0;
  179. IIC_Write_Byte(MPU_PWR_MGMT1_REG, 0X80); // 复位MPU6050
  180. HAL_Delay(100);
  181. IIC_Write_Byte(MPU_PWR_MGMT1_REG, 0X00); // 唤醒MPU6050
  182. MPU_Set_Gyro_Fsr(3); // 陀螺仪传感器,±2000dps
  183. MPU_Set_Accel_Fsr(0); // 加速度传感器 ±2g
  184. MPU_Set_Rate(50); // 设置采样率50HZ
  185. IIC_Write_Byte(MPU_INT_EN_REG, 0X00); // 关闭所有中断
  186. IIC_Write_Byte(MPU_USER_CTRL_REG, 0X00); // I2C主模式关闭
  187. IIC_Write_Byte(MPU_FIFO_EN_REG, 0X00); // 关闭FIFO
  188. IIC_Write_Byte(MPU_INTBP_CFG_REG, 0X80); // INT引脚低电平有效
  189. res = IIC_Read_Byte(MPU_DEVICE_ID_REG);
  190. printf("the addr is:%d\r\n", res);
  191. // if(res==a)//器件ID正确
  192. // {
  193. IIC_Write_Byte(MPU_PWR_MGMT1_REG, 0X01); // 设置CLKSEL,PLL X 轴为参考
  194. IIC_Write_Byte(MPU_PWR_MGMT2_REG, 0X00); // 加速度陀螺仪都工作
  195. MPU_Set_Rate(50); // 设置采样率为50HZ
  196. // }
  197. // else
  198. // {
  199. // return 1;
  200. // }
  201. return 0;
  202. }
  203. // 设置MPU6050陀螺仪传感器满量程范围
  204. // fsr:0,±250dps;1,±500dps;2,±1000dps;3,±2000dps
  205. // 返回值:0,设置成功
  206. // 其他,设置失败
  207. u8 MPU_Set_Gyro_Fsr(u8 fsr)
  208. {
  209. return IIC_Write_Byte(MPU_GYRO_CFG_REG, fsr << 3); // 设置陀螺仪满量程范围
  210. }
  211. // 设置MPU6050加速度传感器满量程范围
  212. // fsr:0,±2g;1,±4g;2,±8g;3,±16g
  213. // 返回值:0,设置成功
  214. // 其他,设置失败
  215. u8 MPU_Set_Accel_Fsr(u8 fsr)
  216. {
  217. return IIC_Write_Byte(MPU_ACCEL_CFG_REG, fsr << 3); // 设置加速度传感器满量程范围
  218. }
  219. // 设置MPU6050的数字低通滤波器
  220. // lpf:数字低通滤波频率(Hz)
  221. // 返回值:0,设置成功
  222. // 其他,设置失败
  223. u8 MPU_Set_LPF(u16 lpf)
  224. {
  225. u8 data = 0;
  226. if (lpf >= 188)
  227. data = 1;
  228. else if (lpf >= 98)
  229. data = 2;
  230. else if (lpf >= 42)
  231. data = 2;
  232. else if (lpf >= 42)
  233. data = 3;
  234. else if (lpf >= 20)
  235. data = 4;
  236. else if (lpf >= 10)
  237. data = 5;
  238. else
  239. data = 6;
  240. return IIC_Write_Byte(MPU_CFG_REG, data); // 设置数字低通滤波器
  241. }
  242. // 设置MPU6050的采样率(假定Fs=1KHz)
  243. // rate:4~1000(Hz)
  244. // 返回值:0,设置成功
  245. // 其他,设置失败
  246. u8 MPU_Set_Rate(u16 rate)
  247. {
  248. u8 data;
  249. if (rate > 1000)
  250. rate = 1000;
  251. if (rate < 4)
  252. rate = 4;
  253. data = 1000 / rate - 1;
  254. data = IIC_Write_Byte(MPU_SAMPLE_RATE_REG, data); // 设置数字低通滤波器
  255. return MPU_Set_LPF(rate / 2); // 自动设置LPF为采样率的一半
  256. }
  257. // 得到温度值
  258. // 返回值:温度值(扩大了100倍)
  259. short MPU_Get_Temperature(void)
  260. {
  261. u8 buf[2];
  262. short raw;
  263. float temp;
  264. IIC_Read_Len(a, MPU_TEMP_OUTH_REG, 2, buf);
  265. raw = ((u16)buf[0] << 8) | buf[1];
  266. temp = 36.53 + ((double)raw) / 340;
  267. return temp * 100;
  268. ;
  269. }
  270. // 得到陀螺仪值(原始值)
  271. // gx,gy,gz:陀螺仪x,y,z轴的原始读数(带符号)
  272. // 返回值:0,成功
  273. // 其他,错误代码
  274. u8 MPU_Get_Gyroscope(short *gx, short *gy, short *gz)
  275. {
  276. u8 buf[6], res;
  277. res = IIC_Read_Len(a, MPU_GYRO_XOUTH_REG, 6, buf);
  278. if (res == 0)
  279. {
  280. *gx = ((u16)buf[0] << 8) | buf[1];
  281. *gy = ((u16)buf[2] << 8) | buf[3];
  282. *gz = ((u16)buf[4] << 8) | buf[5];
  283. }
  284. return res;
  285. }
  286. // 得到加速度值(原始值)
  287. // ax,ay,az:陀螺仪x,y,z轴的原始读数(带符号)
  288. // 返回值:0,成功
  289. // 其他,错误代码
  290. u8 MPU_Get_Accelerometer(short *ax, short *ay, short *az)
  291. {
  292. u8 buf[6], res;
  293. res = IIC_Read_Len(a, MPU_ACCEL_XOUTH_REG, 6, buf);
  294. if (res == 0)
  295. {
  296. *ax = ((u16)buf[0] << 8) | buf[1];
  297. *ay = ((u16)buf[2] << 8) | buf[3];
  298. *az = ((u16)buf[4] << 8) | buf[5];
  299. }
  300. return res;
  301. ;
  302. }
  303. /*--------------------以下为使用板载固件DMP的相关代码,这个融合得到的姿态角效果很好*/
  304. /**
  305. * @brief 获取当前毫秒值
  306. * @param 存储最新毫秒值的变量
  307. * @retval 无
  308. */
  309. int get_tick_count(unsigned long *count)
  310. {
  311. count[0] = HAL_GetTick();
  312. return 0;
  313. }
  314. #include "inv_mpu.h"
  315. #include "inv_mpu_dmp_motion_driver.h"
  316. /* Data requested by client. */
  317. #define PRINT_ACCEL (0x01)
  318. #define PRINT_GYRO (0x02)
  319. #define PRINT_QUAT (0x04)
  320. #define ACCEL_ON (0x01)
  321. #define GYRO_ON (0x02)
  322. #define MOTION (0)
  323. #define NO_MOTION (1)
  324. /* Starting sampling rate. */ // 100
  325. #define DEFAULT_MPU_HZ (50)
  326. struct rx_s
  327. {
  328. unsigned char header[3];
  329. unsigned char cmd;
  330. };
  331. struct hal_s
  332. {
  333. unsigned char sensors;
  334. unsigned char dmp_on;
  335. unsigned char wait_for_tap;
  336. volatile unsigned char new_gyro;
  337. unsigned short report;
  338. unsigned short dmp_features;
  339. unsigned char motion_int_mode;
  340. struct rx_s rx;
  341. };
  342. static struct hal_s hal = {0};
  343. /* USB RX binary semaphore. Actually, it's just a flag. Not included in struct
  344. * because it's declared extern elsewhere.
  345. */
  346. volatile unsigned char rx_new;
  347. /* The sensors can be mounted onto the board in any orientation. The mounting
  348. * matrix seen below tells the MPL how to rotate the raw data from thei
  349. * driver(s).
  350. * TODO: The following matrices refer to the configuration on an internal test
  351. * board at Invensense. If needed, please modify the matrices to match the
  352. * chip-to-body matrix for your particular set up.
  353. */
  354. static signed char gyro_orientation[9] = {-1, 0, 0,
  355. 0, -1, 0,
  356. 0, 0, 1};
  357. /* Every time new gyro data is available, this function is called in an
  358. * ISR context. In this example, it sets a flag protecting the FIFO read
  359. * function.
  360. */
  361. static void gyro_data_ready_cb(void)
  362. {
  363. hal.new_gyro = 1;
  364. }
  365. enum packet_type_e
  366. {
  367. PACKET_TYPE_ACCEL,
  368. PACKET_TYPE_GYRO,
  369. PACKET_TYPE_QUAT,
  370. PACKET_TYPE_TAP,
  371. PACKET_TYPE_ANDROID_ORIENT,
  372. PACKET_TYPE_PEDO,
  373. PACKET_TYPE_MISC
  374. };
  375. #define BYTE u_char
  376. /* Send data to the Python client application.
  377. * Data is formatted as follows:
  378. * packet[0] = $
  379. * packet[1] = packet type (see packet_type_e)
  380. * packet[2+] = data
  381. */
  382. void send_packet(char packet_type, void *data)
  383. {
  384. #define MAX_BUF_LENGTH (18)
  385. char buf[MAX_BUF_LENGTH], length;
  386. memset(buf, 0, MAX_BUF_LENGTH);
  387. buf[0] = '$';
  388. buf[1] = packet_type;
  389. if (packet_type == PACKET_TYPE_ACCEL || packet_type == PACKET_TYPE_GYRO)
  390. {
  391. short *sdata = (short *)data;
  392. buf[2] = (char)(sdata[0] >> 8);
  393. buf[3] = (char)sdata[0];
  394. buf[4] = (char)(sdata[1] >> 8);
  395. buf[5] = (char)sdata[1];
  396. buf[6] = (char)(sdata[2] >> 8);
  397. buf[7] = (char)sdata[2];
  398. length = 8;
  399. }
  400. else if (packet_type == PACKET_TYPE_QUAT)
  401. {
  402. long *ldata = (long *)data;
  403. buf[2] = (char)(ldata[0] >> 24);
  404. buf[3] = (char)(ldata[0] >> 16);
  405. buf[4] = (char)(ldata[0] >> 8);
  406. buf[5] = (char)ldata[0];
  407. buf[6] = (char)(ldata[1] >> 24);
  408. buf[7] = (char)(ldata[1] >> 16);
  409. buf[8] = (char)(ldata[1] >> 8);
  410. buf[9] = (char)ldata[1];
  411. buf[10] = (char)(ldata[2] >> 24);
  412. buf[11] = (char)(ldata[2] >> 16);
  413. buf[12] = (char)(ldata[2] >> 8);
  414. buf[13] = (char)ldata[2];
  415. buf[14] = (char)(ldata[3] >> 24);
  416. buf[15] = (char)(ldata[3] >> 16);
  417. buf[16] = (char)(ldata[3] >> 8);
  418. buf[17] = (char)ldata[3];
  419. length = 18;
  420. }
  421. else if (packet_type == PACKET_TYPE_TAP)
  422. {
  423. buf[2] = ((char *)data)[0];
  424. buf[3] = ((char *)data)[1];
  425. length = 4;
  426. }
  427. else if (packet_type == PACKET_TYPE_ANDROID_ORIENT)
  428. {
  429. buf[2] = ((char *)data)[0];
  430. length = 3;
  431. }
  432. else if (packet_type == PACKET_TYPE_PEDO)
  433. {
  434. long *ldata = (long *)data;
  435. buf[2] = (char)(ldata[0] >> 24);
  436. buf[3] = (char)(ldata[0] >> 16);
  437. buf[4] = (char)(ldata[0] >> 8);
  438. buf[5] = (char)ldata[0];
  439. buf[6] = (char)(ldata[1] >> 24);
  440. buf[7] = (char)(ldata[1] >> 16);
  441. buf[8] = (char)(ldata[1] >> 8);
  442. buf[9] = (char)ldata[1];
  443. length = 10;
  444. }
  445. else if (packet_type == PACKET_TYPE_MISC)
  446. {
  447. buf[2] = ((char *)data)[0];
  448. buf[3] = ((char *)data)[1];
  449. buf[4] = ((char *)data)[2];
  450. buf[5] = ((char *)data)[3];
  451. length = 6;
  452. }
  453. // cdcSendDataWaitTilDone((BYTE*)buf, length, CDC0_INTFNUM, 100);
  454. }
  455. /* These next two functions converts the orientation matrix (see
  456. * gyro_orientation) to a scalar representation for use by the DMP.
  457. * NOTE: These functions are borrowed from Invensense's MPL.
  458. */
  459. static inline unsigned short inv_row_2_scale(const signed char *row)
  460. {
  461. unsigned short b;
  462. if (row[0] > 0)
  463. b = 0;
  464. else if (row[0] < 0)
  465. b = 4;
  466. else if (row[1] > 0)
  467. b = 1;
  468. else if (row[1] < 0)
  469. b = 5;
  470. else if (row[2] > 0)
  471. b = 2;
  472. else if (row[2] < 0)
  473. b = 6;
  474. else
  475. b = 7; // error
  476. return b;
  477. }
  478. static inline unsigned short inv_orientation_matrix_to_scalar(
  479. const signed char *mtx)
  480. {
  481. unsigned short scalar;
  482. /*
  483. XYZ 010_001_000 Identity Matrix
  484. XZY 001_010_000
  485. YXZ 010_000_001
  486. YZX 000_010_001
  487. ZXY 001_000_010
  488. ZYX 000_001_010
  489. */
  490. scalar = inv_row_2_scale(mtx);
  491. scalar |= inv_row_2_scale(mtx + 3) << 3;
  492. scalar |= inv_row_2_scale(mtx + 6) << 6;
  493. return scalar;
  494. }
  495. static inline void run_self_test(void)
  496. {
  497. int result;
  498. char test_packet[4] = {0};
  499. long gyro[3], accel[3];
  500. result = mpu_run_self_test(gyro, accel);
  501. if (result == 0x7)
  502. {
  503. /* Test passed. We can trust the gyro data here, so let's push it down
  504. * to the DMP.
  505. */
  506. float sens;
  507. unsigned short accel_sens;
  508. mpu_get_gyro_sens(&sens);
  509. gyro[0] = (long)(gyro[0] * sens);
  510. gyro[1] = (long)(gyro[1] * sens);
  511. gyro[2] = (long)(gyro[2] * sens);
  512. dmp_set_gyro_bias(gyro);
  513. mpu_get_accel_sens(&accel_sens);
  514. accel[0] *= accel_sens;
  515. accel[1] *= accel_sens;
  516. accel[2] *= accel_sens;
  517. dmp_set_accel_bias(accel);
  518. }
  519. /* Report results. */
  520. test_packet[0] = 't';
  521. test_packet[1] = result;
  522. send_packet(PACKET_TYPE_MISC, test_packet);
  523. }
  524. #define INT_EXIT_LPM0 12
  525. void DMP_Init(void)
  526. {
  527. printf("checkpoint Func DMP_Init\n");
  528. struct int_param_s int_param;
  529. /* Set up gyro.
  530. * Every function preceded by mpu_ is a driver function and can be found
  531. * in inv_mpu.h.
  532. */
  533. int_param.cb = gyro_data_ready_cb;
  534. int_param.pin = 16;
  535. int_param.lp_exit = INT_EXIT_LPM0;
  536. int_param.active_low = 1;
  537. int result = mpu_init(&int_param);
  538. printf("mpu_init, %d\r\n", result);
  539. if (result == 0) // mpu初始化
  540. {
  541. if (!mpu_set_sensors(INV_XYZ_GYRO | INV_XYZ_ACCEL)) // 设置需要的传感器
  542. printf("mpu_set_sensor complete ......\r\n");
  543. if (!mpu_configure_fifo(INV_XYZ_GYRO | INV_XYZ_ACCEL)) // 设置fifo
  544. printf("mpu_configure_fifo complete ......\r\n");
  545. if (!mpu_set_sample_rate(DEFAULT_MPU_HZ)) // 设置采集样率
  546. printf("mpu_set_sample_rate complete ......\r\n");
  547. unsigned short gyro_rate = 0, gyro_fsr = 0;
  548. unsigned char accel_fsr = 0;
  549. mpu_get_sample_rate(&gyro_rate);
  550. mpu_get_gyro_fsr(&gyro_fsr);
  551. mpu_get_accel_fsr(&accel_fsr);
  552. printf("gyro_rate %d, gyro_fsr %d, accel_fsr %d\r\n", gyro_rate, gyro_fsr, accel_fsr);
  553. if (!dmp_load_motion_driver_firmware()) // 加载dmp固件
  554. printf("dmp_load_motion_driver_firmware complete ......\r\n");
  555. if (!dmp_set_orientation(inv_orientation_matrix_to_scalar(gyro_orientation)))
  556. printf("dmp_set_orientation complete ......\r\n"); // 设置陀螺仪方向
  557. if (!dmp_enable_feature(DMP_FEATURE_6X_LP_QUAT | DMP_FEATURE_TAP |
  558. DMP_FEATURE_ANDROID_ORIENT | DMP_FEATURE_SEND_RAW_ACCEL | DMP_FEATURE_SEND_CAL_GYRO |
  559. DMP_FEATURE_GYRO_CAL))
  560. printf("dmp_enable_feature complete ......\r\n");
  561. if (!dmp_set_fifo_rate(DEFAULT_MPU_HZ)) // 设置速率
  562. printf("dmp_set_fifo_rate complete ......\r\n");
  563. run_self_test(); // 自检(非必要)
  564. if (!mpu_set_dmp_state(1)) // 使能
  565. printf("mpu_set_dmp_state complete ......\r\n");
  566. }
  567. }
  568. #define q30 1073741824.0
  569. uint8_t Read_DMP(float *Pitch, float *Roll, float *Yaw)
  570. {
  571. short gyro[3], accel[3], sensors;
  572. float q0 = 1.0f, q1 = 0.0f, q2 = 0.0f, q3 = 0.0f;
  573. unsigned long sensor_timestamp;
  574. unsigned char more;
  575. long quat[4];
  576. int ret = dmp_read_fifo(gyro, accel, quat, &sensor_timestamp, &sensors, &more);
  577. // printf("dmp_read_fifo, %d\r\n", ret);
  578. if (ret)
  579. {
  580. return 1;
  581. }
  582. if (sensors & INV_WXYZ_QUAT)
  583. {
  584. q0 = quat[0] / q30;
  585. q1 = quat[1] / q30;
  586. q2 = quat[2] / q30;
  587. q3 = quat[3] / q30;
  588. *Pitch = (float)asin(-2 * q1 * q3 + 2 * q0 * q2) * 57.3;
  589. *Roll = (float)atan2(2 * q2 * q3 + 2 * q0 * q1, -2 * q1 * q1 - 2 * q2 * q2 + 1) * 57.3; // roll
  590. *Yaw = (float)atan2(2 * (q1 * q2 + q0 * q3), q0 * q0 + q1 * q1 - q2 * q2 - q3 * q3) * 57.3;
  591. return 0;
  592. }
  593. else
  594. {
  595. return 2;
  596. }
  597. }