hal_attitude.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /********************** Í·Îļþ *************************/
  2. #include "usr.h"
  3. #include "hal_imu.h"
  4. #include "hal_step.h"
  5. #include "system.h"
  6. #include "bsp_time.h"
  7. #include "hal_ble_client.h"
  8. #include "hal_flash.h"
  9. #include "hal_attitude.h"
  10. #include "drv_iic_01.h"
  11. #include "drv_mpu9250.h"
  12. #include "bmi160_support.h"
  13. static uint8_t Attmode =0;
  14. static uint8_t QeqAttMode =0;
  15. static uint8_t ReadTemp =0;
  16. typedef struct _hal_att{
  17. int16_t acc[3];
  18. int16_t gry[3];
  19. int16_t temperature;
  20. }hal_att_t;
  21. static hal_att_t mIhal_att_t ={0};
  22. int16_t hal_att_GetAcc_Z(void)
  23. {
  24. return mIhal_att_t.acc[2];
  25. }
  26. int16_t* hal_att_GetAcc(void)
  27. {
  28. return mIhal_att_t.acc;
  29. }
  30. int16_t* hal_att_GetGry(void)
  31. {
  32. return mIhal_att_t.gry;
  33. }
  34. int16_t hal_att_Get_temperature(void)
  35. {
  36. return mIhal_att_t.temperature;
  37. }
  38. void hal_att_read_acc(void){
  39. #if MPU9250_USE_ENABLE && MPU9250_READ_ENABLE
  40. MPU9250_Read_Acc((int16_t*)acc);
  41. #endif
  42. #if BMI160_USE_ENABLE && BMI160_READ_ENABLE
  43. struct bmi160_accel_t accelxyz;
  44. bmi160_read_accel_xyz(&accelxyz);
  45. mIhal_att_t.acc[0] = accelxyz.x;
  46. mIhal_att_t.acc[1] = accelxyz.y;
  47. mIhal_att_t.acc[2] = accelxyz.z;
  48. #endif
  49. }
  50. void hal_att_read_Acc_Gry_temp(void){
  51. #if MPU9250_USE_ENABLE && MPU9250_READ_ENABLE
  52. if(!MPU9250_Read((int16_t*)mIhal_att_t.acc,(int16_t*)mIhal_att_t.gry,(int16_t*)mIhal_att_t.mag,(int16_t*)&mIhal_att_t.temperature)){
  53. SEGGER_RTT_printf(0,"===============================>MPU9250_Read fail\n");
  54. }
  55. #endif
  56. #if BMI160_USE_ENABLE && BMI160_READ_ENABLE
  57. uint32_t err = 0;
  58. struct bmi160_accel_t accelxyz;
  59. struct bmi160_gyro_t gyroxyz;
  60. err = bmi160_read_gyro_xyz(&gyroxyz);
  61. err += bmi160_read_accel_xyz(&accelxyz);
  62. // s16 temp;
  63. // err += bmi160_get_temp(&temp);
  64. if(err){
  65. SEGGER_RTT_printf(0,"===============================>hal_attitude fail\n");
  66. }else{
  67. mIhal_att_t.acc[0] = accelxyz.x;
  68. mIhal_att_t.acc[1] = accelxyz.y;
  69. mIhal_att_t.acc[2] = accelxyz.z;
  70. mIhal_att_t.gry[0] = gyroxyz.x;
  71. mIhal_att_t.gry[1] = gyroxyz.y;
  72. mIhal_att_t.gry[2] = gyroxyz.z;
  73. // mIhal_att_t.temperature = temp;
  74. }
  75. #endif
  76. }
  77. static void hal_att_Initialize(void){
  78. #if MPU9250_USE_ENABLE
  79. IIC_01_Init();
  80. for(int i=0;i<5;i++){
  81. nrf_delay_ms(500);
  82. if(MPU9250_Reset()) break;
  83. app_err_Set(ERR_NUM_IMU_MPU9250,1);
  84. SEGGER_RTT_printf(0,"MPU9250_Reset fail!!\n");
  85. }
  86. for(int i=0;i<20;i++){
  87. nrf_delay_ms(200);
  88. if(MPU9250_Acc_Init()) break;
  89. app_err_Set(ERR_NUM_IMU_MPU9250,1);
  90. SEGGER_RTT_printf(0,"MPU9250_Acc_Init fail!!\n");
  91. }
  92. for(int i=0;i<20;i++){
  93. nrf_delay_ms(500);
  94. if(MPU9250_MageInit()) break;
  95. app_err_Set(ERR_NUM_IMU_MPU9250,1);
  96. SEGGER_RTT_printf(0,"MPU9250_MageInit fail!!\n");
  97. }
  98. #endif
  99. #if BMI160_USE_ENABLE
  100. u8 v_accel_power_mode_stat_u8 =0;
  101. for(uint8_t i=0;i<20;i++){
  102. nrf_delay_ms(200);
  103. bmi160_initialize_sensor(ACCEL_PEDOMETER);
  104. bmi160_get_accel_power_mode_stat(&v_accel_power_mode_stat_u8);
  105. if(2 == v_accel_power_mode_stat_u8) {
  106. Attmode = Att_Lowpower;
  107. QeqAttMode = Att_Lowpower;
  108. break;//µÍ¹¦ºÄacc
  109. }
  110. app_err_Set(ERR_NUM_IMU_BMI160,1);
  111. SEGGER_RTT_printf(0,"bmi160_initialize_sensor(ACCEL_PEDOMETER) fail!!\n");
  112. }
  113. #endif
  114. }
  115. static void hal_attitude_process(void){
  116. #if MPU9250_USE_ENABLE
  117. if(QeqAttMode != Attmode && ReadTemp <=21){
  118. if(QeqAttMode == Att_GameMode){
  119. if(MPU9250_Init())Attmode = QeqAttMode;
  120. else {
  121. ReadTemp++;
  122. SEGGER_RTT_printf(0,"MPU9250 into game fail\n");
  123. }
  124. }else{
  125. if(MPU9250_Acc_Init())Attmode = QeqAttMode;
  126. else {
  127. ReadTemp++;
  128. SEGGER_RTT_printf(0,"MPU9250 into lower fail\n");
  129. }
  130. }
  131. }
  132. #endif
  133. #if BMI160_USE_ENABLE
  134. uint8_t mode =0;
  135. if(QeqAttMode != Attmode && ReadTemp <=21){
  136. if(QeqAttMode == Att_GameMode){
  137. mode = STANDARD_UI_IMU;
  138. }else{
  139. mode = ACCEL_PEDOMETER_FIFO;
  140. }
  141. if(0 == bmi160_initialize_sensor(mode)){
  142. Attmode = QeqAttMode;
  143. }else if(ReadTemp++ >=20){
  144. SEGGER_RTT_printf(0,"bmi160_initialize_sensor,mode: fail!!\n");
  145. }
  146. }
  147. #endif
  148. }
  149. uint8_t hal_attitude_GetMode(uint8_t mode){
  150. if(Attmode == mode)return 1;
  151. else return 0;
  152. }
  153. void hal_attitude_setMode(uint8_t mode){
  154. #if MPU9250_USE_ENABLE
  155. if(QeqAttMode != mode)MPU9250_Reset();
  156. #endif
  157. QeqAttMode = mode;
  158. ReadTemp =0;
  159. }
  160. void hal_attitude_init(void)
  161. {
  162. SPI_Init();
  163. hal_att_Initialize();
  164. Process_Start(200,"hal_attitude_process",hal_attitude_process);
  165. }