app_self_checking.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /*Includes ----------------------------------------------*/
  2. #include "system.h"
  3. #include "exception.h"
  4. #include "ble_comm.h"
  5. #include "app_charge.h"
  6. #include "app_flash.h"
  7. #include "app_self_checking.h"
  8. #include "hal_led.h"
  9. #include "hal_monitor.h"
  10. #include "hal_ble_uart0.h"
  11. #include "hal_ble_client.h"
  12. #include "hal_ser_imu_mode_manage.h"
  13. /*Private macro ------------------------------------------------*/
  14. #define SELF_CHECK_WHEEL_EMPTY_TIMES_MAX MONITOR_SUSPEND_MODE_OVERFLOW_ERR_SUM_MAX //允许最大轮空次数
  15. #define SELF_CHECK_ITEMS 6 //自检项数
  16. #define SELF_CHECK_ITEM_SUCCESS 0 //自检项目成功
  17. #define SELF_CHECK_ITEM_FAIL 1 //自检项目失败
  18. #define SELF_CHECK_ITEM_FRONT_SENSOR_INDEX 0 //自检项目 - 前脚传感器
  19. #define SELF_CHECK_ITEM_BACK_SENSOR_INDEX 1 //自检项目 - 后脚传感器
  20. #define SELF_CHECK_ITEM_BATTERY_INDEX 2 //自检项目 - 电量
  21. #define SELF_CHECK_ITEM_CHARGE_INDEX 3 //自检项目 - 充电电压
  22. #define SELF_CHECK_ITEM_ANT_INDEX 4 //自检项目 - 天线
  23. #define SELF_CHECK_ITEM_SUSPEND_MODE_INDEX 5 //自检项目 - 异常挂起
  24. #define SELF_CHECK_PROCESS_CYCLE 10 //自检线程周期 - ms为单位
  25. #define SELF_CHECK_LED_DISPLAY_TIME (3000/SELF_CHECK_PROCESS_CYCLE) //自检灯显示时长 - ms为单位
  26. #define SELF_CHECK_LED_TOGGLE_TIME (200/SELF_CHECK_PROCESS_CYCLE) //自检灯翻转时长 - ms为单位
  27. #define SELF_CHECK_SCAN_TRIGGER_TIMEOUT (10000/SELF_CHECK_PROCESS_CYCLE) //扫描触发自检超时时间 - ms为单位
  28. #define SELF_CHECK_SCAN_TARGET_ADV_NAME "SH_SELF_CHECK" //扫描触发的目标广播名字
  29. /*STRUCTION ----------------------------------------------------*/
  30. typedef enum {
  31. SELF_CHECK_STAGE_READY, //准备阶段
  32. SELF_CHECK_STAGE_CHECK, //自检阶段
  33. } SELF_CHECK_STAGE_e;
  34. typedef enum{
  35. SELF_CHECK_LED_ON, //自检灯 - 开
  36. SELF_CHECK_LED_OFF, //自检灯 - 关
  37. } SELF_CHECK_LED_SWITCH_e;
  38. typedef struct self_check
  39. {
  40. /*private member*/
  41. SELF_CHECK_STAGE_e stage; //阶段字段
  42. int wheel_empty_times; //轮空次数
  43. uint8_t result[SELF_CHECK_ITEMS]; //自检结果
  44. uint8_t item_fail_num; //自检项目失败数量
  45. SELF_CHECK_LED_SWITCH_e led_switch; //自检灯开关
  46. uint16_t led_display_time; //自检灯显示时长
  47. uint16_t led_toggle_time; //自检灯翻转时长
  48. uint32_t scan_trigger_timeout; //扫描触发自检超时时间
  49. bool is_scan_success; //是否找到广播名字
  50. } Self_Check_t;
  51. /*Local Variable -----------------------------------------------*/
  52. static Self_Check_t ob_self_check;
  53. /*Local Functions ----------------------------------------------*/
  54. static void clear_all_except(void)
  55. {
  56. Except_ClearExceptype(EXCEPT_DATA_BACK_MAG);
  57. Except_ClearExceptype(EXCEPT_DATA_FRONT_ACC);
  58. Except_ClearExceptype(EXCEPT_DATA_FRONT_GRY);
  59. Except_ClearExceptype(EXCEPT_DATA_FRONT_MAG);
  60. Except_ClearExceptype(EXCEPT_DATA_CHARGE);
  61. Except_ClearExceptype(EXCEPT_DATA_BATTERY);
  62. Except_ClearExceptype(EXCEPT_MODE_SUSPEND_OVERFLOW);
  63. Except_ClearExceptype(EXCEPT_ANT_DAMAGE);
  64. }
  65. static void scan_report(unsigned char *adv_data, unsigned short adv_data_len,signed char rssi)
  66. {
  67. //查找目标广播名字
  68. if(advdata_name_find(adv_data, adv_data_len,SELF_CHECK_SCAN_TARGET_ADV_NAME))
  69. {
  70. ob_self_check.is_scan_success = true;
  71. }
  72. }
  73. static void app_self_checking_Process(void)
  74. {
  75. switch(app_charge_Getstate())
  76. {
  77. case BLE_Client_T_CHARGE_PULLOUT: //没充电
  78. ob_self_check.is_scan_success = false; //重置扫描成功标志位
  79. ob_self_check.scan_trigger_timeout = SELF_CHECK_SCAN_TRIGGER_TIMEOUT; //重置扫描触发超时时间
  80. advdata_report_Evt_Regist(NULL); //禁止扫描触发
  81. break;
  82. case BLE_Client_T_CHARGE_DONE: //充电
  83. case BLE_Client_T_CHARGE_INSERT:
  84. if(ob_self_check.scan_trigger_timeout != 0) //扫描监测
  85. {
  86. advdata_report_Evt_Regist(scan_report);
  87. ob_self_check.scan_trigger_timeout--;
  88. }
  89. if(ob_self_check.is_scan_success == true)
  90. {
  91. ob_self_check.scan_trigger_timeout = 0; //清空扫描触发超时时间
  92. clear_all_except(); //清除所有异常
  93. memset(ob_self_check.result, SELF_CHECK_ITEM_SUCCESS, SELF_CHECK_ITEMS);
  94. if(hal_ser_imu_mode_manage_get_ready(HAL_SER_IMU_MODE_MANAGE_SELF_CHECK) == -1){
  95. hal_ser_imu_mode_manage_set_required(HAL_SER_IMU_MODE_MANAGE_SELF_CHECK, HAL_SER_IMU_MODE_MANAGE_IMU_MODE_ON);//扫描监测到目标广播名,开启自检模式
  96. }
  97. ob_self_check.stage = SELF_CHECK_STAGE_CHECK; //进入自检阶段
  98. }
  99. if(ob_self_check.scan_trigger_timeout == 0 && ob_self_check.is_scan_success == false) //超时且没扫描到广播名
  100. {
  101. ob_self_check.stage = SELF_CHECK_STAGE_READY; //设置阶段为准备阶段
  102. }
  103. break;
  104. }
  105. switch(ob_self_check.stage)
  106. {
  107. case SELF_CHECK_STAGE_READY: //准备阶段
  108. ob_self_check.wheel_empty_times = 0; //清空轮空次数
  109. LED_Stop(LED_SELF_CHECK);
  110. ob_self_check.led_switch = SELF_CHECK_LED_OFF; //设置自检灯为关
  111. ob_self_check.led_display_time = 0; //清空自检灯显示时长
  112. ob_self_check.led_toggle_time = 0; //清空自检灯翻转时长
  113. if(hal_ser_imu_mode_manage_get_ready(HAL_SER_IMU_MODE_MANAGE_SELF_CHECK) != -1)
  114. {
  115. hal_ser_imu_mode_manage_set_required(HAL_SER_IMU_MODE_MANAGE_SELF_CHECK, HAL_SER_IMU_MODE_MANAGE_IMU_MODE_OFF);//当前是自检模式,退出。
  116. }
  117. break;
  118. case SELF_CHECK_STAGE_CHECK: //自检阶段
  119. ob_self_check.wheel_empty_times++;
  120. if(ob_self_check.wheel_empty_times >= SELF_CHECK_WHEEL_EMPTY_TIMES_MAX){
  121. ob_self_check.wheel_empty_times = SELF_CHECK_WHEEL_EMPTY_TIMES_MAX;
  122. //灯显示结果,逻辑应该是先显示完灯后在去重新检测
  123. if(ob_self_check.led_display_time != 0)
  124. {
  125. if(ob_self_check.item_fail_num != 0)
  126. {
  127. if(ob_self_check.led_toggle_time != 0)
  128. {
  129. if(ob_self_check.led_switch == SELF_CHECK_LED_ON)LED_Start(LED_SELF_CHECK,COLOR_RED);
  130. if(ob_self_check.led_switch == SELF_CHECK_LED_OFF)LED_Start(LED_SELF_CHECK,COLOR_BLACK);
  131. ob_self_check.led_toggle_time--;
  132. if(ob_self_check.led_toggle_time == 0)
  133. {
  134. if(ob_self_check.led_switch == SELF_CHECK_LED_ON)ob_self_check.led_switch = SELF_CHECK_LED_OFF;
  135. else{
  136. ob_self_check.led_switch = SELF_CHECK_LED_ON;
  137. ob_self_check.item_fail_num--;
  138. }
  139. ob_self_check.led_toggle_time = SELF_CHECK_LED_TOGGLE_TIME;
  140. }
  141. }
  142. }
  143. else
  144. {
  145. LED_Start(LED_SELF_CHECK,COLOR_BLACK);
  146. }
  147. ob_self_check.led_display_time--;
  148. return;
  149. }
  150. /*获取自检结果前---------------------------------------------------------------------*/
  151. ob_self_check.result[SELF_CHECK_ITEM_FRONT_SENSOR_INDEX] = SELF_CHECK_ITEM_SUCCESS;
  152. ob_self_check.result[SELF_CHECK_ITEM_BACK_SENSOR_INDEX] = SELF_CHECK_ITEM_SUCCESS;
  153. ob_self_check.result[SELF_CHECK_ITEM_BATTERY_INDEX] = SELF_CHECK_ITEM_SUCCESS;
  154. ob_self_check.result[SELF_CHECK_ITEM_CHARGE_INDEX] = SELF_CHECK_ITEM_SUCCESS;
  155. ob_self_check.result[SELF_CHECK_ITEM_ANT_INDEX] = SELF_CHECK_ITEM_SUCCESS;
  156. ob_self_check.result[SELF_CHECK_ITEM_SUSPEND_MODE_INDEX] = SELF_CHECK_ITEM_SUCCESS;
  157. ob_self_check.item_fail_num = 0;
  158. /*获取自检结果中---------------------------------------------------------------------*/
  159. //若进入不了自检模式
  160. if(hal_ser_imu_mode_manage_get_ready(HAL_SER_IMU_MODE_MANAGE_SELF_CHECK) == -1){
  161. Except_SetExceptype(EXCEPT_DATA_FRONT_ACC);
  162. Except_SetExceptype(EXCEPT_DATA_FRONT_GRY);
  163. Except_SetExceptype(EXCEPT_DATA_FRONT_MAG);
  164. Except_SetExceptype(EXCEPT_MODE_SUSPEND_OVERFLOW);
  165. }
  166. //自检结果 - 异常挂起
  167. if(Except_IsError(EXCEPT_MODE_SUSPEND_OVERFLOW)){ob_self_check.result[SELF_CHECK_ITEM_SUSPEND_MODE_INDEX] = SELF_CHECK_ITEM_FAIL;ob_self_check.item_fail_num = SELF_CHECK_ITEM_SUSPEND_MODE_INDEX + 1;}
  168. //自检结果 - 前脚传感器
  169. if(Except_IsError(EXCEPT_DATA_FRONT_ACC)){ob_self_check.result[SELF_CHECK_ITEM_FRONT_SENSOR_INDEX] = SELF_CHECK_ITEM_FAIL;}
  170. if(Except_IsError(EXCEPT_DATA_FRONT_GRY)){ob_self_check.result[SELF_CHECK_ITEM_FRONT_SENSOR_INDEX] = SELF_CHECK_ITEM_FAIL;}
  171. if(Except_IsError(EXCEPT_DATA_FRONT_MAG)){ob_self_check.result[SELF_CHECK_ITEM_FRONT_SENSOR_INDEX] = SELF_CHECK_ITEM_FAIL;}
  172. if(Except_IsError(EXCEPT_DATA_FRONT_ACC) || Except_IsError(EXCEPT_DATA_FRONT_GRY) || Except_IsError(EXCEPT_DATA_FRONT_MAG)){ob_self_check.item_fail_num = SELF_CHECK_ITEM_FRONT_SENSOR_INDEX + 1;}
  173. //自检结果 - 后脚传感器
  174. if(Except_IsError(EXCEPT_DATA_BACK_MAG)){ob_self_check.result[SELF_CHECK_ITEM_BACK_SENSOR_INDEX] = SELF_CHECK_ITEM_FAIL;ob_self_check.item_fail_num = SELF_CHECK_ITEM_BACK_SENSOR_INDEX + 1;}
  175. //自检结果 - 电量
  176. if(Except_IsError(EXCEPT_DATA_BATTERY)){ob_self_check.result[SELF_CHECK_ITEM_BATTERY_INDEX] = SELF_CHECK_ITEM_FAIL;ob_self_check.item_fail_num = SELF_CHECK_ITEM_BATTERY_INDEX + 1;}
  177. //自检结果 - 充电电压
  178. if(Except_IsError(EXCEPT_DATA_CHARGE)){ob_self_check.result[SELF_CHECK_ITEM_CHARGE_INDEX] = SELF_CHECK_ITEM_FAIL;ob_self_check.item_fail_num = SELF_CHECK_ITEM_CHARGE_INDEX + 1;}
  179. //自检结果 - 天线
  180. if(Except_IsError(EXCEPT_ANT_DAMAGE)){ob_self_check.result[SELF_CHECK_ITEM_ANT_INDEX] = SELF_CHECK_ITEM_FAIL;ob_self_check.item_fail_num = SELF_CHECK_ITEM_ANT_INDEX + 1;}
  181. /*获取自检结果后---------------------------------------------------------------------*/
  182. //设置自检灯显示时长和翻转时长
  183. if(ob_self_check.item_fail_num != 0){
  184. ob_self_check.led_switch = SELF_CHECK_LED_ON;
  185. ob_self_check.led_display_time = SELF_CHECK_LED_DISPLAY_TIME;
  186. ob_self_check.led_toggle_time = SELF_CHECK_LED_TOGGLE_TIME;
  187. }else{
  188. ob_self_check.led_switch = SELF_CHECK_LED_ON;
  189. if(mFlash.mClient.isConfig == 'C')
  190. {
  191. LED_Start(LED_SELF_CHECK,COLOR_BLUE);
  192. }
  193. else
  194. {
  195. LED_Start(LED_SELF_CHECK,COLOR_CYAN);
  196. }
  197. }
  198. }
  199. break;
  200. }
  201. }
  202. static void app_self_checking_printf_process(void)
  203. {
  204. SEGGER_RTT_printf(0,"self_check: FRONT_SENSOR:%d BACK_SENSOR:%d BATTERY:%d CHARGE:%d ANT:%d suspend_mode:%d scan_trigger_timeout:%d\n", \
  205. Except_IsError(EXCEPT_DATA_FRONT_ACC) || Except_IsError(EXCEPT_DATA_FRONT_GRY) || Except_IsError(EXCEPT_DATA_FRONT_MAG), \
  206. Except_IsError(EXCEPT_DATA_BACK_MAG), \
  207. Except_IsError(EXCEPT_DATA_BATTERY), \
  208. Except_IsError(EXCEPT_DATA_CHARGE), \
  209. Except_IsError(EXCEPT_ANT_DAMAGE), \
  210. Except_IsError(EXCEPT_MODE_SUSPEND_OVERFLOW),ob_self_check.scan_trigger_timeout);
  211. }
  212. /*API ----------------------------------------------------------*/
  213. void app_self_checking_Init(void)
  214. {
  215. ob_self_check.stage = SELF_CHECK_STAGE_READY;
  216. ob_self_check.wheel_empty_times = 0;
  217. ob_self_check.item_fail_num = 0;
  218. ob_self_check.led_switch = SELF_CHECK_LED_OFF;
  219. ob_self_check.led_display_time = 0;
  220. ob_self_check.led_toggle_time = 0;
  221. ob_self_check.is_scan_success = false;
  222. ob_self_check.scan_trigger_timeout = SELF_CHECK_SCAN_TRIGGER_TIMEOUT;
  223. Process_Start(SELF_CHECK_PROCESS_CYCLE,"app_self_checking_Process",app_self_checking_Process);
  224. Process_Start(1000,"app_self_checking_printf_process",app_self_checking_printf_process);
  225. }
  226. uint32_t app_self_checking_get_scan_trigger_timeout(void)
  227. {
  228. return ob_self_check.scan_trigger_timeout;
  229. }