app_self_checking.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. /*Includes ----------------------------------------------*/
  2. #include "system.h"
  3. #include "exception.h"
  4. #include "app_charge.h"
  5. #include "app_flash.h"
  6. #include "app_self_checking.h"
  7. #include "hal_led.h"
  8. #include "hal_mt.h"
  9. #include "hal_ble_uart0.h"
  10. #include "hal_ble_client.h"
  11. #include "app_flash.h"
  12. #include "app_pair_chargerpin.h"
  13. #include "bsp_time.h"
  14. #include "hal_scan_manage.h"
  15. #include "ble_comm.h"
  16. #include "bll_imu.h"
  17. /*Private macro ------------------------------------------------*/
  18. #define SELF_CHECK_WHEEL_EMPTY_TIMES_MAX MONITOR_SUSPEND_MODE_OVERFLOW_ERR_SUM_MAX //允许最大轮空次数
  19. #define SELF_CHECK_ITEM_SUCCESS 0 //自检项目成功
  20. #define SELF_CHECK_ITEM_FAIL 1
  21. //自检项目失败
  22. enum{
  23. SELF_CHECK_ITEM_FRONT_SENSOR=0, //自检项目 - 前脚传感器
  24. SELF_CHECK_ITEM_BACK_SENSOR, //自检项目 - 后脚传感器
  25. SELF_CHECK_ITEM_BATTERY, //自检项目 - 电量
  26. SELF_CHECK_ITEM_CHARGE, //自检项目 - 充电电压
  27. SELF_CHECK_ITEM_ANT, //自检项目 - 天线
  28. SELF_CHECK_ITEM_SUSPEND_MODE, //自检项目 - 异常挂起
  29. SELF_CHECK_ITEMS //自检项数
  30. };
  31. #define SELF_CHECK_LED_DISPLAY_TIME 3000 //自检灯显示时长 - ms为单位
  32. #define SELF_CHECK_LED_TOGGLE_TIME 200 //自检灯翻转时长 - ms为单位
  33. #define SELF_CHECK_SCAN_TRIGGER_TIMEOUT 10000 //扫描触发自检超时时间 - ms为单位
  34. #define SELF_CHECK_SET_IMU_TIMEOUT 20000 //等待IMU初始化成功的时间
  35. #define SELF_CHECK_SCAN_TARGET_ADV_NAME "SH_SELF_CHECK" //扫描触发的目标广播名字
  36. /*STRUCTION --------------------------------------------------
  37. --*/
  38. typedef enum {
  39. SELF_CHECK_STAGE_READY, //准备阶段
  40. SELF_CHECK_STAGE_CHECK, //自检阶段
  41. } SELF_CHECK_STAGE_e;
  42. typedef enum{
  43. SELF_CHECK_LED_ON, //自检灯 - 开
  44. SELF_CHECK_LED_OFF, //自检灯 - 关
  45. } SELF_CHECK_LED_SWITCH_e;
  46. typedef struct self_check
  47. {
  48. /*private member*/
  49. SELF_CHECK_STAGE_e stage; //阶段字段
  50. uint8_t result[SELF_CHECK_ITEMS]; //自检结果
  51. uint8_t item_fail_num; //自检项目失败数量
  52. SELF_CHECK_LED_SWITCH_e led_switch; //自检灯开关
  53. uint32_t led_display_time; //自检灯显示时长
  54. uint32_t led_toggle_time; //自检灯翻转时长
  55. uint32_t scan_trigger_timeout; //扫描触发自检超时时间
  56. bool is_scan_success; //是否找到广播名字
  57. } Self_Check_t;
  58. /*Local Variable -----------------------------------------------*/
  59. static Self_Check_t ob_self_check;
  60. /*Local Functions ----------------------------------------------*/
  61. static void clear_all_except(void)
  62. {
  63. Except_ClearExceptype(EXCEPT_DATA_BACK_MAG);
  64. Except_ClearExceptype(EXCEPT_DATA_FRONT_ACC);
  65. Except_ClearExceptype(EXCEPT_DATA_FRONT_GRY);
  66. Except_ClearExceptype(EXCEPT_DATA_FRONT_MAG);
  67. Except_ClearExceptype(EXCEPT_DATA_CHARGE);
  68. Except_ClearExceptype(EXCEPT_DATA_BATTERY);
  69. Except_ClearExceptype(EXCEPT_IMU_SUSPEND_OVERFLOW);
  70. Except_ClearExceptype(EXCEPT_ANT_DAMAGE);
  71. }
  72. static void selft_scan_cb(void)
  73. {
  74. ob_self_check.is_scan_success = true;
  75. }
  76. static scan_reslt_t scan_reslt_handle ={
  77. .handle = selft_scan_cb,
  78. .scanname =SELF_CHECK_SCAN_TARGET_ADV_NAME,
  79. };
  80. //static pair_reslt_t UartPair_result ={
  81. // .flag = 0,
  82. //};
  83. static void app_self_check_getreslut(){
  84. uint8_t i=0;
  85. /*获取自检结果前---------------------------------------------------------------------*/
  86. for(i=0;i<SELF_CHECK_ITEMS;i++)ob_self_check.result[i] = SELF_CHECK_ITEM_SUCCESS;
  87. ob_self_check.item_fail_num = 0;
  88. /*获取自检结果中---------------------------------------------------------------------*/
  89. //自检结果 - 异常挂起
  90. if(Except_IsError(EXCEPT_IMU_SUSPEND_OVERFLOW)){ob_self_check.result[SELF_CHECK_ITEM_SUSPEND_MODE] = SELF_CHECK_ITEM_FAIL;ob_self_check.item_fail_num = SELF_CHECK_ITEM_SUSPEND_MODE + 1;}
  91. //自检结果 - 前脚传感器
  92. if(Except_IsError(EXCEPT_DATA_FRONT_ACC)){ob_self_check.result[SELF_CHECK_ITEM_FRONT_SENSOR] = SELF_CHECK_ITEM_FAIL;}
  93. if(Except_IsError(EXCEPT_DATA_FRONT_GRY)){ob_self_check.result[SELF_CHECK_ITEM_FRONT_SENSOR] = SELF_CHECK_ITEM_FAIL;}
  94. if(Except_IsError(EXCEPT_DATA_FRONT_MAG)){ob_self_check.result[SELF_CHECK_ITEM_FRONT_SENSOR] = SELF_CHECK_ITEM_FAIL;}
  95. 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 + 1;}
  96. //自检结果 - 后脚传感器
  97. if(Except_IsError(EXCEPT_DATA_BACK_MAG)){ob_self_check.result[SELF_CHECK_ITEM_BACK_SENSOR] = SELF_CHECK_ITEM_FAIL;ob_self_check.item_fail_num = SELF_CHECK_ITEM_BACK_SENSOR + 1;}
  98. //自检结果 - 电量
  99. if(Except_IsError(EXCEPT_DATA_BATTERY)){ob_self_check.result[SELF_CHECK_ITEM_BATTERY] = SELF_CHECK_ITEM_FAIL;ob_self_check.item_fail_num = SELF_CHECK_ITEM_BATTERY + 1;}
  100. //自检结果 - 充电电压
  101. if(Except_IsError(EXCEPT_DATA_CHARGE)){ob_self_check.result[SELF_CHECK_ITEM_CHARGE] = SELF_CHECK_ITEM_FAIL;ob_self_check.item_fail_num = SELF_CHECK_ITEM_CHARGE+ 1;}
  102. //自检结果 - 天线
  103. if(Except_IsError(EXCEPT_ANT_DAMAGE)){ob_self_check.result[SELF_CHECK_ITEM_ANT] = SELF_CHECK_ITEM_FAIL;ob_self_check.item_fail_num = SELF_CHECK_ITEM_ANT + 1;}
  104. //设置自检灯显示时长和翻转时长
  105. if(ob_self_check.item_fail_num != 0){
  106. ob_self_check.led_switch = SELF_CHECK_LED_ON;
  107. ob_self_check.led_display_time = TIME_GetTicks();
  108. ob_self_check.led_toggle_time = TIME_GetTicks();
  109. }else{
  110. ob_self_check.led_switch = SELF_CHECK_LED_ON;
  111. // if(UartPair_result.flag){
  112. // LED_Start(LED_SELF_CHECK,COLOR_BLUE);
  113. // }
  114. // else{
  115. // LED_Start(LED_SELF_CHECK,COLOR_CYAN);
  116. // }
  117. }
  118. }
  119. //自检模式
  120. static const bll_imu_one_way_param_t selfcheck_front_param={
  121. .acc_power_mode = FML_IMU_ACC_POWER_MODE_NORMAL, //前脚 - 加速度正常模式
  122. .gry_power_mode = FML_IMU_GRY_POWER_MODE_NORMAL, //前脚 - 陀螺仪正常模式
  123. .timestamp_resolution = FML_IMU_TIMESTAMP_25US, //前脚 - 时间戳25US精度
  124. .timestamp_switch = FML_IMU_TIMESTAMP_ON, //前脚 - 时间戳开启
  125. .acc_fs = FML_IMU_ACC_FS_16G, //前脚 - 加速度量程 - 16G
  126. .gry_fs = FML_IMU_GRY_FS_2000DPS, //前脚 - 陀螺仪量程 - 2000DPS
  127. .mag_fs = FML_IMU_MAG_FS_30GS, //前脚 - 地磁计量程 - 30GS
  128. .acc_odr = FML_IMU_ACC_ODR_104HZ, //前脚 - 加速度采样频率 - 104HZ
  129. .gry_odr = FML_IMU_GRY_ODR_104HZ, //前脚 - 陀螺仪采样频率 - 104HZ
  130. .mag_odr = FML_IMU_MAG_ODR_200HZ, //前脚 - 地磁计采样频率 - 200HZ
  131. .fifo_odr = FML_IMU_FIFO_ODR_104HZ,
  132. };
  133. static const bll_imu_one_way_param_t selfcheck_back_param={
  134. .acc_power_mode = FML_IMU_ACC_POWER_MODE_NORMAL, //后脚 - 加速度正常模式
  135. .gry_power_mode = FML_IMU_GRY_POWER_MODE_NORMAL, //后脚 - 陀螺仪正常模式
  136. .timestamp_resolution = FML_IMU_TIMESTAMP_25US, //后脚 - 时间戳25US精度
  137. .timestamp_switch = FML_IMU_TIMESTAMP_OFF, //后脚 - 时间戳关闭
  138. .acc_fs = FML_IMU_ACC_FS_16G, //后脚 - 加速度量程 - 16G
  139. .gry_fs = FML_IMU_GRY_FS_2000DPS, //后脚 - 陀螺仪量程 - 2000DPS
  140. .mag_fs = FML_IMU_MAG_FS_30GS, //后脚 - 地磁计量程 - 30GS
  141. .acc_odr = FML_IMU_ACC_ODR_OFF, //后脚 - 加速度采样频率 - 关闭
  142. .gry_odr = FML_IMU_GRY_ODR_OFF, //后脚 - 陀螺仪采样频率 - 关闭
  143. .mag_odr = FML_IMU_MAG_ODR_200HZ, //后脚 - 地磁计采样频率 - 200HZ
  144. .fifo_odr = FML_IMU_FIFO_ODR_OFF,
  145. };
  146. static const bll_imu_param_t selfcheck_bll_imu_param_t={
  147. .config_param[FML_IMU_DIR_FRONT] = &selfcheck_front_param,
  148. .config_param[FML_IMU_DIR_BACK] = &selfcheck_back_param,
  149. };
  150. static void ReadyIntoSelfMode(void){
  151. //设置为自检模式,且跳转到下一个流程
  152. hal_ble_scan_result_Regist(&scan_reslt_handle);
  153. ob_self_check.is_scan_success = false;
  154. ob_self_check.scan_trigger_timeout = TIME_GetTicks();
  155. ob_self_check.stage = SELF_CHECK_STAGE_READY;
  156. clear_all_except(); //清除所有异常
  157. LED_Start(LED_SELF_CHECK,COLOR_BLACK);
  158. LED_Stop(LED_SELF_CHECK);
  159. bll_imu_Resume_config_param(&selfcheck_bll_imu_param_t);
  160. memset(ob_self_check.result, SELF_CHECK_ITEM_SUCCESS, SELF_CHECK_ITEMS);
  161. // UartPair_result.flag =0;
  162. // app_UartPair_Regist(&UartPair_result);
  163. DEBUG_LOG("app_self_checking_Process clear_all_except\r\n");
  164. }
  165. static void app_self_checking_Process(void)
  166. {
  167. static uint8_t state =0;
  168. static uint8_t state_selft =0;
  169. static uint8_t displaycnt =0;
  170. uint8_t front_CS =0,back_CS =0;
  171. uint8_t imu_configcnt =0;
  172. switch(state){
  173. case 0:
  174. if(BLE_CHARGE_DONE == app_charge_Getstate() || \
  175. BLE_CHARGE_INSERT == app_charge_Getstate()
  176. ){
  177. if(Except_IsErrorExist()) //如果存在错误
  178. {
  179. front_CS = bll_imu_query_config_param_is_ready(BLL_IMU_DIR_FRONT,&selfcheck_bll_imu_param_t);
  180. back_CS = bll_imu_query_config_param_is_ready(BLL_IMU_DIR_BACK,&selfcheck_bll_imu_param_t);
  181. if(BLL_IMU_CONFIG_FINISH == front_CS && BLL_IMU_CONFIG_FINISH == back_CS){ //如果当前是自检配置
  182. bll_imu_Resume_unregister_config_param(&selfcheck_bll_imu_param_t);//关掉自检配置
  183. }
  184. else{ //如果当前不是自检配置{
  185. ST_scan_start();
  186. ReadyIntoSelfMode();
  187. state =1;
  188. state_selft =0;
  189. }
  190. }
  191. else{
  192. ST_scan_start();//设置为自检模式,且跳转到下一个流程
  193. ReadyIntoSelfMode();
  194. state =1;
  195. state_selft =0;
  196. }
  197. }
  198. break;
  199. case 1:
  200. if(ob_self_check.is_scan_success == true)
  201. {
  202. ob_self_check.stage = SELF_CHECK_STAGE_CHECK; //进入自检阶段
  203. state =2;
  204. Process_SetHoldOn(app_self_checking_Process,1);
  205. DEBUG_LOG("ob_self_check.is_scan_success\r\n");
  206. }else{
  207. if((TIME_GetTicks() - ob_self_check.scan_trigger_timeout) > SELF_CHECK_SCAN_TRIGGER_TIMEOUT) //超时且没扫描到广播名
  208. {
  209. state = 2;
  210. ob_self_check.is_scan_success = false; //重置扫描成功标志位
  211. state_selft =3;
  212. hal_ble_scan_result_Clear(&scan_reslt_handle); //禁止扫描触发
  213. ST_scan_stop();
  214. DEBUG_LOG("ob_self_check.scan_trigger_timeout\r\n");
  215. }
  216. }
  217. break;
  218. case 2:
  219. if(BLE_CHARGE_PULLOUT == app_charge_Getstate()){
  220. state = 0;
  221. // app_UartPair_Clear(&UartPair_result);
  222. DEBUG_LOG("ob_self_check.scan BLE_Client_T_CHARGE_PULLOUT\r\n");
  223. }
  224. break;
  225. default:
  226. break;
  227. }
  228. switch(state_selft)
  229. {
  230. case 0:
  231. if(SELF_CHECK_STAGE_CHECK == ob_self_check.stage){//扫描成功
  232. MT_Run(500);
  233. bll_imu_Resume_config_param(&selfcheck_bll_imu_param_t);//开启自检模式
  234. state_selft = 1;
  235. imu_configcnt =0;
  236. }
  237. break;
  238. case 1:
  239. if(BLE_CHARGE_PULLOUT == app_charge_Getstate()){
  240. state_selft = 3;
  241. }
  242. else {
  243. front_CS = bll_imu_query_config_param_is_ready(BLL_IMU_DIR_FRONT,&selfcheck_bll_imu_param_t);
  244. back_CS = bll_imu_query_config_param_is_ready(BLL_IMU_DIR_BACK,&selfcheck_bll_imu_param_t);
  245. if(BLL_IMU_CONFIG_FINISH == front_CS && BLL_IMU_CONFIG_FINISH == back_CS){
  246. state_selft = 2;
  247. displaycnt =0;
  248. app_self_check_getreslut();
  249. }
  250. else if(BLL_IMU_CONFIG_DOING != front_CS || BLL_IMU_CONFIG_DOING != back_CS){
  251. bll_imu_Resume_config_param(&selfcheck_bll_imu_param_t);
  252. imu_configcnt++;
  253. DEBUG_LOG(">>>>>>222 out sleep mode,%d %d\n",front_CS,back_CS);
  254. if(imu_configcnt > 20){imu_configcnt =0;
  255. Except_SetExceptype(EXCEPT_DATA_FRONT_ACC);
  256. Except_SetExceptype(EXCEPT_DATA_FRONT_GRY);
  257. Except_SetExceptype(EXCEPT_DATA_FRONT_MAG);
  258. Except_SetExceptype(EXCEPT_IMU_SUSPEND_OVERFLOW);
  259. state_selft = 2;
  260. displaycnt =0;
  261. }
  262. }
  263. }
  264. break;
  265. case 2:
  266. if(displaycnt < ob_self_check.item_fail_num){
  267. if(TIME_GetTicks() - ob_self_check.led_toggle_time >= SELF_CHECK_LED_TOGGLE_TIME){ob_self_check.led_toggle_time = TIME_GetTicks();
  268. if(ob_self_check.led_switch == SELF_CHECK_LED_ON){
  269. LED_Start(LED_SELF_CHECK,COLOR_LIGHRED);
  270. ob_self_check.led_switch = SELF_CHECK_LED_OFF;
  271. }
  272. else{
  273. LED_Start(LED_SELF_CHECK,COLOR_BLACK);
  274. ob_self_check.led_switch = SELF_CHECK_LED_ON;
  275. displaycnt++;
  276. }
  277. }
  278. }
  279. else if(TIME_GetTicks() - ob_self_check.led_display_time >= SELF_CHECK_LED_DISPLAY_TIME){//显示时间到了,重现读取自检结果,并显示
  280. ob_self_check.led_display_time = TIME_GetTicks();
  281. displaycnt =0;
  282. app_self_check_getreslut();
  283. }
  284. break;
  285. case 3:
  286. state_selft = 0;
  287. ob_self_check.stage = SELF_CHECK_STAGE_READY;
  288. LED_Start(LED_SELF_CHECK,COLOR_BLACK);
  289. LED_Stop(LED_SELF_CHECK);
  290. ob_self_check.led_switch = SELF_CHECK_LED_OFF; //设置自检灯为关
  291. bll_imu_Resume_unregister_config_param(&selfcheck_bll_imu_param_t);//关掉自检配置
  292. break;
  293. default:
  294. break;
  295. }
  296. }
  297. //static void app_self_checking_printf_process(void)
  298. //{
  299. // DEBUG_LOG("self_check: FRONT_SENSOR:%d BACK_SENSOR:%d BATTERY:%d CHARGE:%d ANT:%d suspend_mode:%d scan_trigger_timeout:%d\n", \
  300. // Except_IsError(EXCEPT_DATA_FRONT_ACC) || Except_IsError(EXCEPT_DATA_FRONT_GRY) || Except_IsError(EXCEPT_DATA_FRONT_MAG), \
  301. // Except_IsError(EXCEPT_DATA_BACK_MAG), \
  302. // Except_IsError(EXCEPT_DATA_BATTERY), \
  303. // Except_IsError(EXCEPT_DATA_CHARGE), \
  304. // Except_IsError(EXCEPT_ANT_DAMAGE), \
  305. // Except_IsError(EXCEPT_IMU_SUSPEND_OVERFLOW),ob_self_check.scan_trigger_timeout);
  306. //}
  307. /*API ----------------------------------------------------------*/
  308. void app_self_checking_Init(void)
  309. {
  310. ob_self_check.stage = SELF_CHECK_STAGE_READY;
  311. ob_self_check.item_fail_num = 0;
  312. ob_self_check.led_switch = SELF_CHECK_LED_OFF;
  313. ob_self_check.led_display_time = 0;
  314. ob_self_check.led_toggle_time = 0;
  315. ob_self_check.is_scan_success = false;
  316. ob_self_check.scan_trigger_timeout = SELF_CHECK_SCAN_TRIGGER_TIMEOUT;
  317. Process_Start(10,"app_self_checking_Process",app_self_checking_Process);
  318. // Process_Start(1000,"app_self_checking_printf_process",app_self_checking_printf_process);
  319. }
  320. uint32_t app_self_checking_get_scan_trigger_timeout(void)
  321. {
  322. return ob_self_check.scan_trigger_timeout;
  323. }