app_self_checking.c 14 KB

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