hal_monitor.c 8.8 KB

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