hal_monitor.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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 charge_vol_max; //充电期间最大的电压值
  24. int16_t last_f_acc_err_sum; //上一次的前脚加速度值错误累计
  25. int16_t last_f_gry_err_sum; //上一次的前脚陀螺仪值错误累计
  26. int16_t last_f_mag_err_sum; //上一次的前脚地磁计值错误累计
  27. int16_t last_b_mag_err_sum; //上一次的后脚地磁计值错误累计
  28. int16_t suspend_mode_err_sum; //异常挂起模式持续错误累计
  29. uint32_t last_tim; //上一次的时间
  30. } Hal_Monitor_t;
  31. /*Local Variable ----------------------------------------------*/
  32. static Hal_Monitor_t ob_monitor;
  33. /*Local Functions ----------------------------------------------*/
  34. static void monitor_sensor_data(int16_t *f_acc, int16_t *f_gry, int16_t *f_mag, int16_t *b_mag)
  35. {
  36. /*前脚加速度*/
  37. if(f_acc != NULL)
  38. {
  39. if(
  40. ob_monitor.last_f_acc[0] == f_acc[0] && \
  41. ob_monitor.last_f_acc[1] == f_acc[1] && \
  42. ob_monitor.last_f_acc[2] == f_acc[2]
  43. )
  44. {
  45. ob_monitor.last_f_acc_err_sum++;
  46. if(ob_monitor.last_f_acc_err_sum >= MONITOR_DATA_ERR_SUM_MAX){
  47. Except_SetExceptype(EXCEPT_DATA_FRONT_ACC);
  48. }
  49. }else{
  50. ob_monitor.last_f_acc_err_sum = 0;
  51. }
  52. ob_monitor.last_f_acc[0] = f_acc[0];
  53. ob_monitor.last_f_acc[1] = f_acc[1];
  54. ob_monitor.last_f_acc[2] = f_acc[2];
  55. }
  56. /*前脚陀螺仪*/
  57. if(f_gry != NULL)
  58. {
  59. if(
  60. ob_monitor.last_f_gry[0] == f_gry[0] && \
  61. ob_monitor.last_f_gry[1] == f_gry[1] && \
  62. ob_monitor.last_f_gry[2] == f_gry[2]
  63. )
  64. {
  65. ob_monitor.last_f_gry_err_sum++;
  66. if(ob_monitor.last_f_gry_err_sum >= MONITOR_DATA_ERR_SUM_MAX){
  67. Except_SetExceptype(EXCEPT_DATA_FRONT_GRY);
  68. }
  69. }else{
  70. ob_monitor.last_f_gry_err_sum = 0;
  71. }
  72. ob_monitor.last_f_gry[0] = f_gry[0];
  73. ob_monitor.last_f_gry[1] = f_gry[1];
  74. ob_monitor.last_f_gry[2] = f_gry[2];
  75. }
  76. /*前脚地磁计*/
  77. if(f_mag != NULL)
  78. {
  79. if(
  80. ob_monitor.last_f_mag[0] == f_mag[0] && \
  81. ob_monitor.last_f_mag[1] == f_mag[1] && \
  82. ob_monitor.last_f_mag[2] == f_mag[2]
  83. )
  84. {
  85. ob_monitor.last_f_mag_err_sum++;
  86. if(ob_monitor.last_f_mag_err_sum >= MONITOR_DATA_ERR_SUM_MAX){
  87. Except_SetExceptype(EXCEPT_DATA_FRONT_MAG);
  88. }
  89. }else{
  90. ob_monitor.last_f_mag_err_sum = 0;
  91. }
  92. ob_monitor.last_f_mag[0] = f_mag[0];
  93. ob_monitor.last_f_mag[1] = f_mag[1];
  94. ob_monitor.last_f_mag[2] = f_mag[2];
  95. }
  96. /*后脚地磁计*/
  97. if(b_mag != NULL)
  98. {
  99. if(
  100. ob_monitor.last_b_mag[0] == b_mag[0] && \
  101. ob_monitor.last_b_mag[1] == b_mag[1] && \
  102. ob_monitor.last_b_mag[2] == b_mag[2]
  103. )
  104. {
  105. ob_monitor.last_b_mag_err_sum++;
  106. if(ob_monitor.last_b_mag_err_sum >= MONITOR_DATA_ERR_SUM_MAX){
  107. Except_SetExceptype(EXCEPT_DATA_BACK_MAG);
  108. }
  109. }else{
  110. ob_monitor.last_b_mag_err_sum = 0;
  111. }
  112. ob_monitor.last_b_mag[0] = b_mag[0];
  113. ob_monitor.last_b_mag[1] = b_mag[1];
  114. ob_monitor.last_b_mag[2] = b_mag[2];
  115. }
  116. }
  117. static void monitor_battery_charge_data(void)
  118. {
  119. int16_t bat_vol;
  120. int16_t charge_vol;
  121. int16_t charge_threshold;
  122. uint32_t ch = nrf_gpio_pin_read(PIN_CHARGING);
  123. static uint32_t charge_cycle = 50;
  124. if(!ch)//没充电
  125. {
  126. charge_vol = ADC_RESULT_IN_MILLI_VOLTS(ADC_GetValue(PIN_ADC_CHARGMEASURE_CHANNEL));
  127. ob_monitor.before_charge_vol = ob_monitor.before_charge_vol > charge_vol ? charge_vol : ob_monitor.before_charge_vol;
  128. ob_monitor.charge_vol_max = 0;
  129. charge_cycle = 50;
  130. }else //充电
  131. {
  132. /* 过筛50轮 */
  133. if(charge_cycle != 0){
  134. charge_vol = ADC_RESULT_IN_MILLI_VOLTS(ADC_GetValue(PIN_ADC_CHARGMEASURE_CHANNEL));
  135. ob_monitor.charge_vol_max = ob_monitor.charge_vol_max < charge_vol ? charge_vol : ob_monitor.charge_vol_max;
  136. charge_cycle--;
  137. return;
  138. }
  139. charge_vol = ADC_RESULT_IN_MILLI_VOLTS(ADC_GetValue(PIN_ADC_CHARGMEASURE_CHANNEL));
  140. ob_monitor.charge_vol_max = ob_monitor.charge_vol_max < charge_vol ? charge_vol : ob_monitor.charge_vol_max;
  141. bat_vol = ADC_RESULT_IN_MILLI_VOLTS(ADC_GetValue(PIN_ADC_BAT_CHANNEL))*5/3;
  142. //当电池电量没满,充电(经测试,电量没满的充电电压跟电池电压有关,最小充电电压100+mv)
  143. charge_threshold = ob_monitor.charge_vol_max - ob_monitor.before_charge_vol; //充电前和充电期间的充电电压变化值
  144. if(charge_threshold < CHARGE_VOL_THRESHOLD)
  145. {
  146. Except_SetExceptype(EXCEPT_DATA_CHARGE);
  147. }
  148. //电池小于2.5V或充电电压变化小于阈值且电池电压大于4V
  149. if(bat_vol <= BATTERY_VOL_THRESHOLD_MIN || (charge_threshold < CHARGE_VOL_THRESHOLD && bat_vol > BATTERY_VOL_THRESHOLD_MAX))
  150. {
  151. Except_SetExceptype(EXCEPT_DATA_BATTERY);
  152. }
  153. }
  154. }
  155. static void monitor_suspend_mode_overflow(void)
  156. {
  157. if(hal_mode_get() == HAL_MODE_EXCEPT_SUSPEND)
  158. {
  159. ob_monitor.suspend_mode_err_sum++;
  160. if(ob_monitor.suspend_mode_err_sum >= MONITOR_SUSPEND_MODE_OVERFLOW_ERR_SUM_MAX){
  161. Except_SetExceptype(EXCEPT_MODE_SUSPEND_OVERFLOW);
  162. }
  163. }
  164. else
  165. {
  166. ob_monitor.suspend_mode_err_sum = 0;
  167. }
  168. }
  169. static void hal_monitor_process(void)
  170. {
  171. int i = 0;
  172. int16_t group_num = 0;
  173. int16_t f_acc[3];
  174. int16_t f_gry[3];
  175. int16_t f_mag[3];
  176. int16_t b_mag[3];
  177. //监测传感器数据,配置期间不处理
  178. if(IMU_IsNoSignal())
  179. {
  180. switch(hal_mode_get())
  181. {
  182. case HAL_MODE_SELF_CHECK:
  183. case HAL_MODE_GAME:
  184. case HAL_MODE_REALSTEP:
  185. if(TIME_GetTicks()-ob_monitor.last_tim>=FullPower_Interval) //监测前脚传感器数据(acc + gry + mag)+ 监测后脚传感器数据(mag)
  186. {
  187. ob_monitor.last_tim = TIME_GetTicks();
  188. group_num = IMU_Get_Front_Data_Num();
  189. for(i=0;i<group_num;i++)
  190. {
  191. IMU_Get_Front_Data(i, f_gry, f_acc, f_mag, NULL);
  192. monitor_sensor_data(f_acc, f_gry, f_mag, NULL);
  193. }
  194. IMU_Get_Back_Data(b_mag);
  195. monitor_sensor_data(NULL, NULL, NULL, b_mag);
  196. }
  197. break;
  198. case HAL_MODE_STANDBY:
  199. if(TIME_GetTicks()-ob_monitor.last_tim>=StandByPower_Interval) //监测前脚传感器数据(acc)
  200. {
  201. ob_monitor.last_tim = TIME_GetTicks();
  202. group_num = IMU_Get_Front_Data_Num();
  203. for(i=0;i<group_num;i++)
  204. {
  205. IMU_Get_Front_Data(i, NULL, f_acc, NULL, NULL);
  206. monitor_sensor_data(f_acc, NULL, NULL, NULL);
  207. }
  208. }
  209. break;
  210. case HAL_MODE_NORMAL:
  211. if(TIME_GetTicks()-ob_monitor.last_tim>=LowPower_Interval) //监测前脚传感器数据(acc + mag)
  212. {
  213. ob_monitor.last_tim = TIME_GetTicks();
  214. group_num = IMU_Get_Front_Data_Num();
  215. for(i=0;i<group_num;i++)
  216. {
  217. IMU_Get_Front_Data(i, NULL, f_acc, f_mag, NULL);
  218. monitor_sensor_data(f_acc, NULL, f_mag, NULL);
  219. }
  220. }
  221. break;
  222. default:
  223. break;
  224. }
  225. }
  226. //监测电池和充电数据
  227. monitor_battery_charge_data();
  228. //监测异常挂起模式是否持续时间超过临界点
  229. monitor_suspend_mode_overflow();
  230. }
  231. /*API ---------------------------------------------------------*/
  232. void hal_monitor_Init(void)
  233. {
  234. ob_monitor.before_charge_vol = 5;
  235. ob_monitor.charge_vol_max = 0;
  236. Process_Start(0,"hal_monitor_process",hal_monitor_process);
  237. }