hal_monitor.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /*Includes ------------------------------------------------------*/
  2. #include "exception.h"
  3. #include "system.h"
  4. #include "hal_monitor.h"
  5. #include "hal_imu.h"
  6. #include "hal_mode_manage.h"
  7. #include "hal_battery.h"
  8. #include "bsp_time.h"
  9. /*Private macro ------------------------------------------------*/
  10. #define MONITOR_DATA_ERR_SUM_MAX 5 //数据监测错误累计最大值
  11. #define BATTERY_VOL_THRESHOLD_MIN 2500 //2.5V
  12. #define BATTERY_VOL_THRESHOLD_MAX 4000 //4V
  13. #define CHARGE_VOL_THRESHOLD 80 //充电前和充电期间的充电电压变化值,单位mv
  14. /*STRUCTION -----------------------------------------------------*/
  15. typedef struct hal_monitor
  16. {
  17. /*private member*/
  18. int16_t last_f_acc[3]; //上一次的前脚加速度值
  19. int16_t last_f_gry[3]; //上一次的前脚陀螺仪值
  20. int16_t last_f_mag[3]; //上一次的前脚地磁计值
  21. int16_t last_b_mag[3]; //上一次的后脚地磁计值
  22. int16_t before_charge_vol; //充电前的电压值
  23. int16_t last_f_acc_err_sum; //上一次的前脚加速度值错误累计
  24. int16_t last_f_gry_err_sum; //上一次的前脚陀螺仪值错误累计
  25. int16_t last_f_mag_err_sum; //上一次的前脚地磁计值错误累计
  26. int16_t last_b_mag_err_sum; //上一次的后脚地磁计值错误累计
  27. uint32_t last_tim; //上一次的时间
  28. } Hal_Monitor_t;
  29. /*Local Variable ----------------------------------------------*/
  30. static Hal_Monitor_t ob_monitor;
  31. /*Local Functions ----------------------------------------------*/
  32. static void monitor_sensor_data(Hal_Monitor_t *p_monitor, int16_t *f_acc, int16_t *f_gry, int16_t *f_mag, int16_t *b_mag)
  33. {
  34. if(p_monitor == NULL)return;
  35. /*前脚加速度*/
  36. if(f_acc != NULL)
  37. {
  38. if(
  39. p_monitor->last_f_acc[0] == f_acc[0] && \
  40. p_monitor->last_f_acc[1] == f_acc[1] && \
  41. p_monitor->last_f_acc[2] == f_acc[2]
  42. )
  43. {
  44. p_monitor->last_f_acc_err_sum++;
  45. if(p_monitor->last_f_acc_err_sum >= MONITOR_DATA_ERR_SUM_MAX)
  46. Except_SaveExceptype(EXCEPT_DATA_FRONT_ACC);
  47. }else{
  48. p_monitor->last_f_acc_err_sum = 0;
  49. }
  50. p_monitor->last_f_acc[0] = f_acc[0];
  51. p_monitor->last_f_acc[1] = f_acc[1];
  52. p_monitor->last_f_acc[2] = f_acc[2];
  53. }
  54. /*前脚陀螺仪*/
  55. if(f_gry != NULL)
  56. {
  57. if(
  58. p_monitor->last_f_gry[0] == f_gry[0] && \
  59. p_monitor->last_f_gry[1] == f_gry[1] && \
  60. p_monitor->last_f_gry[2] == f_gry[2]
  61. )
  62. {
  63. p_monitor->last_f_gry_err_sum++;
  64. if(p_monitor->last_f_gry_err_sum >= MONITOR_DATA_ERR_SUM_MAX)
  65. Except_SaveExceptype(EXCEPT_DATA_FRONT_GRY);
  66. }else{
  67. p_monitor->last_f_gry_err_sum = 0;
  68. }
  69. p_monitor->last_f_gry[0] = f_gry[0];
  70. p_monitor->last_f_gry[1] = f_gry[1];
  71. p_monitor->last_f_gry[2] = f_gry[2];
  72. }
  73. /*前脚地磁计*/
  74. if(f_mag != NULL)
  75. {
  76. if(
  77. p_monitor->last_f_mag[0] == f_mag[0] && \
  78. p_monitor->last_f_mag[1] == f_mag[1] && \
  79. p_monitor->last_f_mag[2] == f_mag[2]
  80. )
  81. {
  82. p_monitor->last_f_mag_err_sum++;
  83. if(p_monitor->last_f_mag_err_sum >= MONITOR_DATA_ERR_SUM_MAX)
  84. Except_SaveExceptype(EXCEPT_DATA_FRONT_MAG);
  85. }else{
  86. p_monitor->last_f_mag_err_sum = 0;
  87. }
  88. p_monitor->last_f_mag[0] = f_mag[0];
  89. p_monitor->last_f_mag[1] = f_mag[1];
  90. p_monitor->last_f_mag[2] = f_mag[2];
  91. }
  92. /*后脚地磁计*/
  93. if(b_mag != NULL)
  94. {
  95. if(
  96. p_monitor->last_b_mag[0] == b_mag[0] && \
  97. p_monitor->last_b_mag[1] == b_mag[1] && \
  98. p_monitor->last_b_mag[2] == b_mag[2]
  99. )
  100. {
  101. p_monitor->last_b_mag_err_sum++;
  102. if(p_monitor->last_b_mag_err_sum >= MONITOR_DATA_ERR_SUM_MAX)
  103. Except_SaveExceptype(EXCEPT_DATA_BACK_MAG);
  104. }else{
  105. p_monitor->last_b_mag_err_sum = 0;
  106. }
  107. p_monitor->last_b_mag[0] = b_mag[0];
  108. p_monitor->last_b_mag[1] = b_mag[1];
  109. p_monitor->last_b_mag[2] = b_mag[2];
  110. }
  111. }
  112. static void monitor_battery_charge_data(Hal_Monitor_t *p_monitor)
  113. {
  114. int16_t charge_vol;
  115. int16_t bat_vol;
  116. int16_t charge_threshold;
  117. uint32_t ch = nrf_gpio_pin_read(PIN_CHARGING);
  118. if(!ch){ //没充电
  119. charge_vol = ADC_RESULT_IN_MILLI_VOLTS(ADC_GetValue(PIN_ADC_CHARGMEASURE_CHANNEL));
  120. p_monitor->before_charge_vol = p_monitor->before_charge_vol > charge_vol ? charge_vol : p_monitor->before_charge_vol;
  121. }else{ //充电
  122. charge_vol = ADC_RESULT_IN_MILLI_VOLTS(ADC_GetValue(PIN_ADC_CHARGMEASURE_CHANNEL));
  123. bat_vol = ADC_RESULT_IN_MILLI_VOLTS(ADC_GetValue(PIN_ADC_BAT_CHANNEL))*5/3;
  124. //当电池电量满了,不充电(经测试,哪怕电量满了,充电电压也有10+mv)
  125. //当电池电量没满,充电(经测试,电量没满的充电电压跟电池电压有关,最小充电电压100+mv)
  126. charge_threshold = charge_vol - p_monitor->before_charge_vol; //充电前和充电期间的充电电压变化值
  127. if(charge_threshold < CHARGE_VOL_THRESHOLD)
  128. {
  129. Except_SaveExceptype(EXCEPT_DATA_CHARGE);
  130. }
  131. //电池小于2.5V或充电电压变化小于阈值且电池电压大于4V
  132. if(bat_vol <= BATTERY_VOL_THRESHOLD_MIN || (charge_threshold < CHARGE_VOL_THRESHOLD && bat_vol > BATTERY_VOL_THRESHOLD_MAX))
  133. {
  134. Except_SaveExceptype(EXCEPT_DATA_BATTERY);
  135. }
  136. }
  137. }
  138. static void hal_monitor_process(void)
  139. {
  140. int i = 0;
  141. int16_t group_num = 0;
  142. int16_t f_acc[3];
  143. int16_t f_gry[3];
  144. int16_t f_mag[3];
  145. int16_t b_mag[3];
  146. //监测传感器数据,配置期间不处理
  147. if(IMU_IsNoSignal())
  148. {
  149. switch(hal_mode_get())
  150. {
  151. case HAL_MODE_SELF_CHECK:
  152. case HAL_MODE_GAME:
  153. case HAL_MODE_REALSTEP:
  154. if(TIME_GetTicks()-ob_monitor.last_tim>=FullPower_Interval) //监测前脚传感器数据(acc + gry + mag)+ 监测后脚传感器数据(mag)
  155. {
  156. ob_monitor.last_tim = TIME_GetTicks();
  157. group_num = IMU_Get_Front_Data_Num();
  158. for(i=0;i<group_num;i++)
  159. {
  160. IMU_Get_Front_Data(i, f_gry, f_acc, f_mag, NULL);
  161. monitor_sensor_data(&ob_monitor, f_acc, f_gry, f_mag, NULL);
  162. }
  163. IMU_Get_Back_Data(b_mag);
  164. monitor_sensor_data(&ob_monitor, NULL, NULL, NULL, b_mag);
  165. }
  166. break;
  167. case HAL_MODE_STANDBY:
  168. if(TIME_GetTicks()-ob_monitor.last_tim>=StandByPower_Interval) //监测前脚传感器数据(acc)
  169. {
  170. ob_monitor.last_tim = TIME_GetTicks();
  171. group_num = IMU_Get_Front_Data_Num();
  172. for(i=0;i<group_num;i++)
  173. {
  174. IMU_Get_Front_Data(i, NULL, f_acc, NULL, NULL);
  175. monitor_sensor_data(&ob_monitor, f_acc, NULL, NULL, NULL);
  176. }
  177. }
  178. break;
  179. case HAL_MODE_NORMAL:
  180. if(TIME_GetTicks()-ob_monitor.last_tim>=LowPower_Interval) //监测前脚传感器数据(acc + mag)
  181. {
  182. ob_monitor.last_tim = TIME_GetTicks();
  183. group_num = IMU_Get_Front_Data_Num();
  184. for(i=0;i<group_num;i++)
  185. {
  186. IMU_Get_Front_Data(i, NULL, f_acc, f_mag, NULL);
  187. monitor_sensor_data(&ob_monitor, f_acc, NULL, f_mag, NULL);
  188. }
  189. }
  190. break;
  191. default:
  192. break;
  193. }
  194. }
  195. //监测电池和充电数据
  196. monitor_battery_charge_data(&ob_monitor);
  197. }
  198. /*API ---------------------------------------------------------*/
  199. void hal_monitor_Init(void)
  200. {
  201. ob_monitor.before_charge_vol = 5;
  202. Process_Start(0,"hal_monitor_process",hal_monitor_process);
  203. }