app_self_checking.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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_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. hal_mode_set(HAL_MODE_SELF_CHECK); //扫描监测到目标广播名,开启自检模式
  95. ob_self_check.stage = SELF_CHECK_STAGE_CHECK; //进入自检阶段
  96. }
  97. if(ob_self_check.scan_trigger_timeout == 0 && ob_self_check.is_scan_success == false) //超时且没扫描到广播名
  98. {
  99. ob_self_check.stage = SELF_CHECK_STAGE_READY; //设置阶段为准备阶段
  100. }
  101. break;
  102. }
  103. switch(ob_self_check.stage)
  104. {
  105. case SELF_CHECK_STAGE_READY: //准备阶段
  106. ob_self_check.wheel_empty_times = 0; //清空轮空次数
  107. LED_Stop(LED_SELF_CHECK);
  108. ob_self_check.led_switch = SELF_CHECK_LED_OFF; //设置自检灯为关
  109. ob_self_check.led_display_time = 0; //清空自检灯显示时长
  110. ob_self_check.led_toggle_time = 0; //清空自检灯翻转时长
  111. if(hal_mode_get() == HAL_MODE_SELF_CHECK)hal_mode_set(HAL_MODE_NORMAL); //当前是自检模式,退出。
  112. break;
  113. case SELF_CHECK_STAGE_CHECK: //自检阶段
  114. ob_self_check.wheel_empty_times++;
  115. if(ob_self_check.wheel_empty_times >= SELF_CHECK_WHEEL_EMPTY_TIMES_MAX){
  116. ob_self_check.wheel_empty_times = SELF_CHECK_WHEEL_EMPTY_TIMES_MAX;
  117. //灯显示结果,逻辑应该是先显示完灯后在去重新检测
  118. if(ob_self_check.led_display_time != 0)
  119. {
  120. if(ob_self_check.item_fail_num != 0)
  121. {
  122. if(ob_self_check.led_toggle_time != 0)
  123. {
  124. if(ob_self_check.led_switch == SELF_CHECK_LED_ON)LED_Start(LED_SELF_CHECK,COLOR_RED);
  125. if(ob_self_check.led_switch == SELF_CHECK_LED_OFF)LED_Start(LED_SELF_CHECK,COLOR_BLACK);
  126. ob_self_check.led_toggle_time--;
  127. if(ob_self_check.led_toggle_time == 0)
  128. {
  129. if(ob_self_check.led_switch == SELF_CHECK_LED_ON)ob_self_check.led_switch = SELF_CHECK_LED_OFF;
  130. else{
  131. ob_self_check.led_switch = SELF_CHECK_LED_ON;
  132. ob_self_check.item_fail_num--;
  133. }
  134. ob_self_check.led_toggle_time = SELF_CHECK_LED_TOGGLE_TIME;
  135. }
  136. }
  137. }
  138. else
  139. {
  140. LED_Start(LED_SELF_CHECK,COLOR_BLACK);
  141. }
  142. ob_self_check.led_display_time--;
  143. return;
  144. }
  145. /*获取自检结果前---------------------------------------------------------------------*/
  146. ob_self_check.result[SELF_CHECK_ITEM_FRONT_SENSOR_INDEX] = SELF_CHECK_ITEM_SUCCESS;
  147. ob_self_check.result[SELF_CHECK_ITEM_BACK_SENSOR_INDEX] = SELF_CHECK_ITEM_SUCCESS;
  148. ob_self_check.result[SELF_CHECK_ITEM_BATTERY_INDEX] = SELF_CHECK_ITEM_SUCCESS;
  149. ob_self_check.result[SELF_CHECK_ITEM_CHARGE_INDEX] = SELF_CHECK_ITEM_SUCCESS;
  150. ob_self_check.result[SELF_CHECK_ITEM_ANT_INDEX] = SELF_CHECK_ITEM_SUCCESS;
  151. ob_self_check.result[SELF_CHECK_ITEM_SUSPEND_MODE_INDEX] = SELF_CHECK_ITEM_SUCCESS;
  152. ob_self_check.item_fail_num = 0;
  153. /*获取自检结果中---------------------------------------------------------------------*/
  154. //若进入不了自检模式
  155. if(hal_mode_get() != HAL_MODE_SELF_CHECK){
  156. Except_SetExceptype(EXCEPT_DATA_FRONT_ACC);
  157. Except_SetExceptype(EXCEPT_DATA_FRONT_GRY);
  158. Except_SetExceptype(EXCEPT_DATA_FRONT_MAG);
  159. Except_SetExceptype(EXCEPT_MODE_SUSPEND_OVERFLOW);
  160. }
  161. //自检结果 - 异常挂起
  162. 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;}
  163. //自检结果 - 前脚传感器
  164. if(Except_IsError(EXCEPT_DATA_FRONT_ACC)){ob_self_check.result[SELF_CHECK_ITEM_FRONT_SENSOR_INDEX] = SELF_CHECK_ITEM_FAIL;}
  165. if(Except_IsError(EXCEPT_DATA_FRONT_GRY)){ob_self_check.result[SELF_CHECK_ITEM_FRONT_SENSOR_INDEX] = SELF_CHECK_ITEM_FAIL;}
  166. if(Except_IsError(EXCEPT_DATA_FRONT_MAG)){ob_self_check.result[SELF_CHECK_ITEM_FRONT_SENSOR_INDEX] = SELF_CHECK_ITEM_FAIL;}
  167. 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;}
  168. //自检结果 - 后脚传感器
  169. 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;}
  170. //自检结果 - 电量
  171. 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;}
  172. //自检结果 - 充电电压
  173. 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;}
  174. //自检结果 - 天线
  175. 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;}
  176. /*获取自检结果后---------------------------------------------------------------------*/
  177. //设置自检灯显示时长和翻转时长
  178. if(ob_self_check.item_fail_num != 0){
  179. ob_self_check.led_switch = SELF_CHECK_LED_ON;
  180. ob_self_check.led_display_time = SELF_CHECK_LED_DISPLAY_TIME;
  181. ob_self_check.led_toggle_time = SELF_CHECK_LED_TOGGLE_TIME;
  182. }else{
  183. ob_self_check.led_switch = SELF_CHECK_LED_ON;
  184. if(mFlash.mClient.isConfig == 'C')
  185. {
  186. LED_Start(LED_SELF_CHECK,COLOR_BLUE);
  187. }
  188. else
  189. {
  190. LED_Start(LED_SELF_CHECK,COLOR_CYAN);
  191. }
  192. }
  193. }
  194. break;
  195. }
  196. }
  197. static void app_self_checking_printf_process(void)
  198. {
  199. 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", \
  200. Except_IsError(EXCEPT_DATA_FRONT_ACC) || Except_IsError(EXCEPT_DATA_FRONT_GRY) || Except_IsError(EXCEPT_DATA_FRONT_MAG), \
  201. Except_IsError(EXCEPT_DATA_BACK_MAG), \
  202. Except_IsError(EXCEPT_DATA_BATTERY), \
  203. Except_IsError(EXCEPT_DATA_CHARGE), \
  204. Except_IsError(EXCEPT_ANT_DAMAGE), \
  205. Except_IsError(EXCEPT_MODE_SUSPEND_OVERFLOW),ob_self_check.scan_trigger_timeout);
  206. }
  207. /*API ----------------------------------------------------------*/
  208. void app_self_checking_Init(void)
  209. {
  210. ob_self_check.stage = SELF_CHECK_STAGE_READY;
  211. ob_self_check.wheel_empty_times = 0;
  212. ob_self_check.item_fail_num = 0;
  213. ob_self_check.led_switch = SELF_CHECK_LED_OFF;
  214. ob_self_check.led_display_time = 0;
  215. ob_self_check.led_toggle_time = 0;
  216. ob_self_check.is_scan_success = false;
  217. ob_self_check.scan_trigger_timeout = SELF_CHECK_SCAN_TRIGGER_TIMEOUT;
  218. Process_Start(SELF_CHECK_PROCESS_CYCLE,"app_self_checking_Process",app_self_checking_Process);
  219. Process_Start(1000,"app_self_checking_printf_process",app_self_checking_printf_process);
  220. }
  221. uint32_t app_self_checking_get_scan_trigger_timeout(void)
  222. {
  223. return ob_self_check.scan_trigger_timeout;
  224. }