app_self_checking.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. #include "app_self_checking.h"
  2. #include "usr_config.h"
  3. #include "bsp_time.h"
  4. #include "system.h"
  5. #include "hal_ble_client.h"
  6. #include "hal_ble_host.h"
  7. #include "hal_mt.h"
  8. #include "nrf_delay.h"
  9. #include "app_charge.h"
  10. #include "hal_imu.h"
  11. #include "nrf_gpio.h"
  12. #include "hal_battery.h"
  13. #include "hal_ble_uart0.h"
  14. #include "hal_led.h"
  15. #include "ble_comm.h"
  16. #include "tool.h"
  17. #include "hal_mode_manage.h"
  18. #include "app_flash.h"
  19. #include "exception.h"
  20. #define TEST_SUCCESS 0 //测试成功
  21. #define TEST_FAIL 1 //测试失败
  22. #define TEST_UNKNOWN 2 //测试未知
  23. #define TEST_ITEMS 4 //非人工测试项数(前脚传感器、后脚传感器、电量、充电电流)
  24. #define ENTER_SELF_CHECK 1 //进入自检标志位
  25. #define QUITE_SELF_CHECK 0 //退出自检标志位
  26. #define TEST_RESULT_LED_SUCCESS_GREEN 0
  27. #define TEST_RESULT_LED_ERR_RED (TEST_RESULT_LED_SUCCESS_GREEN+0x01)
  28. #define TEST_RESULT_LED_ERR_BLUE (TEST_RESULT_LED_SUCCESS_GREEN+0x02)
  29. #define TEST_RESULT_LED_ERR_PURPLE (TEST_RESULT_LED_SUCCESS_GREEN+0x03)
  30. #define TEST_RESULT_LED_ERR_YELLOW (TEST_RESULT_LED_SUCCESS_GREEN+0x04)
  31. #define TEST_RESULT_LED_ERR_WHITE (TEST_RESULT_LED_SUCCESS_GREEN+0x05)
  32. #define APP_SELF_CHECKING_PROCESS_CYCLE 10 //线程周期,单位ms
  33. #define APP_SELF_CHECKING_READY_PROCESS_CYCLE 50 //线程周期,单位ms
  34. #define DISPLAY_TEST_RESULT_LED_TIME 1000 //用灯显示测试结果的时间,单位ms
  35. #define DISPLAY_TEST_RESULT_CONTINUE_TIME 3000 //显示测试结果的总时间,单位ms
  36. #define SELF_CHECK_RECIVE_ORDER_TIMES 1 //允许接受到的自检指令最大次数
  37. #define BATTERY_TEST_VALUE 2500 //电池必须大于2.5V,否则算异常
  38. #define BATTERY_CHARGE_CHANGE_VALUE 80 //自检前和自检期间的充电电压变化值,单位mv
  39. #define TRIGGER_MONITOR_ACC_Z 1500 //触发数据监测的加速度Z轴
  40. #define ENTER_MONITOR_TIME (10000/StandByPower_Interval) //进入数据监测所需时间(10S)
  41. #define MONITOR_DURATION_TIME (60000/FullPower_Interval) //数据监测至少需要维持的时间
  42. static uint8_t self_check_state = QUITE_SELF_CHECK;
  43. static uint8_t self_check_result_buff[TEST_ITEMS];
  44. static uint8_t self_check_recive_order_times = 0; //允许接受到的自检指令最大次数
  45. static int16_t before_check_charge_vol = 0; //自检前的充电电压
  46. static uint32_t enter_monitor_times = ENTER_MONITOR_TIME; //进入数据监测所需时间
  47. static uint8_t enter_monitor_flag = 0; //进入数据监测标志位
  48. static uint32_t monitor_duration = MONITOR_DURATION_TIME; //数据监测至少需要维持的时间
  49. /*-----------------以下属于数据监测线程的相关函数----------------------------*/
  50. //监测前脚传感器数据(acc + gry + mag)
  51. static void monitor_except_front_acc_gry_mag(int16_t *monitor_acc, int16_t *monitor_gry, int16_t *monitor_mag)
  52. {
  53. #define MONITOR_FRONT_ACC_SUM 5
  54. #define MONITOR_FRONT_GRY_SUM 5
  55. #define MONITOR_FRONT_MAG_SUM 5
  56. static uint8_t gry_err_sum, acc_err_sum, mag_err_sum;
  57. static int16_t last_gry[3], last_acc[3], last_mag[3];
  58. // SEGGER_RTT_printf(0,"front last_gry[0]=%d, last_gry[1]=%d, last_gry[2]=%d\r\n",last_gry[0],last_gry[1],last_gry[2]);
  59. // SEGGER_RTT_printf(0,"front last_acc[0]=%d, last_acc[1]=%d, last_acc[2]=%d\r\n",last_acc[0],last_acc[1],last_acc[2]);
  60. // SEGGER_RTT_printf(0,"front last_mag[0]=%d, last_mag[1]=%d, last_mag[2]=%d\r\n",last_mag[0],last_mag[1],last_mag[2]);
  61. if(monitor_gry != NULL){
  62. if(last_gry[0] == monitor_gry[0] && last_gry[1] == monitor_gry[1] && last_gry[2] == monitor_gry[2]){
  63. gry_err_sum++;
  64. SEGGER_RTT_printf(0,"==>front gry_err_sum=%d\r\n",gry_err_sum);
  65. if(gry_err_sum >= MONITOR_FRONT_GRY_SUM){
  66. Except_SaveExceptype(EXCEPT_DATA_FRONT_GRY);
  67. }
  68. }else{
  69. gry_err_sum = 0;
  70. }
  71. last_gry[0] = monitor_gry[0];
  72. last_gry[1] = monitor_gry[1];
  73. last_gry[2] = monitor_gry[2];
  74. }
  75. if(monitor_acc != NULL){
  76. if(last_acc[0] == monitor_acc[0] && last_acc[1] == monitor_acc[1] && last_acc[2] == monitor_acc[2]){
  77. acc_err_sum++;
  78. SEGGER_RTT_printf(0,"==>front acc_err_sum=%d\r\n",acc_err_sum);
  79. if(acc_err_sum >= MONITOR_FRONT_ACC_SUM){
  80. Except_SaveExceptype(EXCEPT_DATA_FRONT_ACC);
  81. }
  82. }else{
  83. acc_err_sum = 0;
  84. }
  85. last_acc[0] = monitor_acc[0];
  86. last_acc[1] = monitor_acc[1];
  87. last_acc[2] = monitor_acc[2];
  88. }
  89. if(monitor_mag != NULL){
  90. if(last_mag[0] == monitor_mag[0] && last_mag[1] == monitor_mag[1] && last_mag[2] == monitor_mag[2]){
  91. mag_err_sum++;
  92. SEGGER_RTT_printf(0,"==>front mag_err_sum=%d\r\n",mag_err_sum);
  93. if(mag_err_sum >= MONITOR_FRONT_MAG_SUM){
  94. Except_SaveExceptype(EXCEPT_DATA_FRONT_MAG);
  95. }
  96. }else{
  97. mag_err_sum = 0;
  98. }
  99. last_mag[0] = monitor_mag[0];
  100. last_mag[1] = monitor_mag[1];
  101. last_mag[2] = monitor_mag[2];
  102. acValBuffer.Sine6 = mag_err_sum;
  103. }
  104. }
  105. //监测后脚传感器数据(mag)
  106. static void monitor_except_back_mag(int16_t *monitor_mag)
  107. {
  108. #define MONITOR_BACK_MAG_SUM 5
  109. static uint8_t mag_err_sum;
  110. static int16_t last_mag[3];
  111. // SEGGER_RTT_printf(0,"back last_mag[0]=%d, last_mag[1]=%d, last_mag[2]=%d\r\n",last_mag[0],last_mag[1],last_mag[2]);
  112. if(monitor_mag != NULL){
  113. if(last_mag[0] == monitor_mag[0] && last_mag[1] == monitor_mag[1] && last_mag[2] == monitor_mag[2]){
  114. mag_err_sum++;
  115. SEGGER_RTT_printf(0,"==>back mag_err_sum=%d\r\n",mag_err_sum);
  116. if(mag_err_sum >= MONITOR_BACK_MAG_SUM){
  117. Except_SaveExceptype(EXCEPT_DATA_BACK_MAG);
  118. }
  119. }else{
  120. mag_err_sum = 0;
  121. }
  122. last_mag[0] = monitor_mag[0];
  123. last_mag[1] = monitor_mag[1];
  124. last_mag[2] = monitor_mag[2];
  125. acValBuffer.Sine3 = mag_err_sum;
  126. }
  127. }
  128. void app_self_checking_monitor_Process(void)
  129. {
  130. int16_t group_num = 0;
  131. int16_t front_acc[3];
  132. int16_t front_gry[3];
  133. int16_t front_mag[3];
  134. int16_t back_mag[3];
  135. static uint32_t tim =0;
  136. if(!IMU_IsNoSignal())return; //配置期间不处理
  137. switch(hal_mode_get())
  138. {
  139. case HAL_MODE_SELF_CHECK:
  140. if(monitor_duration > 0 && enter_monitor_flag)monitor_duration--;
  141. case HAL_MODE_GAME:
  142. case HAL_MODE_REALSTEP: // 游戏模式 + 实时计步模式 + 自检模式
  143. if(TIME_GetTicks()-tim>=FullPower_Interval){tim = TIME_GetTicks();
  144. group_num = IMU_Get_Front_Data_Num();
  145. for(int i=0;i<group_num;i++){
  146. IMU_Get_Front_Data(i, front_gry, front_acc, front_mag, NULL);
  147. monitor_except_front_acc_gry_mag(front_acc, front_gry, front_mag);//监测前脚传感器数据(acc + gry + mag)
  148. }
  149. static uint32_t tim2 = 0;
  150. acValBuffer.Sine2 = TIME_GetTicks()-tim2;
  151. tim2 = TIME_GetTicks();
  152. IMU_Get_Back_Data(back_mag);
  153. monitor_except_back_mag(back_mag); //监测后脚传感器数据(mag)
  154. acValBuffer.Sine4 = (int32_t)(sqrt((float) (back_mag[0] * back_mag[0] + back_mag[1] * back_mag[1] + back_mag[2] * back_mag[2])));
  155. }
  156. break;
  157. case HAL_MODE_STANDBY: // 待机模式
  158. if(TIME_GetTicks()-tim>=StandByPower_Interval){tim = TIME_GetTicks();
  159. group_num = IMU_Get_Front_Data_Num();
  160. for(int i=0;i<group_num;i++){
  161. IMU_Get_Front_Data(i, NULL, front_acc, NULL, NULL);
  162. monitor_except_front_acc_gry_mag(front_acc, NULL, NULL); //监测前脚传感器数据(acc)
  163. }
  164. }
  165. break;
  166. case HAL_MODE_NORMAL: // 正常模式
  167. if(TIME_GetTicks()-tim>=LowPower_Interval){tim = TIME_GetTicks();
  168. group_num = IMU_Get_Front_Data_Num();
  169. for(int i=0;i<group_num;i++){
  170. IMU_Get_Front_Data(i, NULL, front_acc, front_mag, NULL);
  171. monitor_except_front_acc_gry_mag(front_acc, NULL, front_mag); //监测前脚传感器数据(acc + mag)
  172. }
  173. }
  174. break;
  175. default:
  176. break;
  177. }
  178. }
  179. /*-----------------以下属于自检流程线程的相关函数----------------------------*/
  180. //人工测试(电机、指示灯)
  181. static void app_self_checking_artificial(void)
  182. {
  183. uint32_t temp_result = 0;
  184. static uint8_t state = 0;
  185. static uint32_t display_times = 0;
  186. switch(state){
  187. case 0:
  188. Process_SetHoldOn(app_self_checking_artificial,1);
  189. //测试电机是否正常
  190. MT_Run(100);
  191. //显示其余的非人工测试结果
  192. for(int i=0;i<TEST_ITEMS;i++)temp_result +=self_check_result_buff[i];
  193. // SEGGER_RTT_printf(0,"temp_result=%d\n",temp_result);
  194. if(temp_result == TEST_SUCCESS){ //如果全部测试通过
  195. LED_Start(LED_SELF_CHECK,COLOR_GREEN);
  196. nrf_gpio_pin_write(PIN_LED_RUN,LED_SMALL_ENABLE);
  197. Process_UpdatePeroid(app_self_checking_artificial,DISPLAY_TEST_RESULT_LED_TIME);
  198. state = 1;
  199. break;
  200. }else{ //没有全部通过
  201. static uint8_t loop = 0;
  202. if(self_check_result_buff[loop++] != TEST_SUCCESS){
  203. switch(loop)
  204. {
  205. case TEST_RESULT_LED_ERR_RED:
  206. if(self_check_result_buff[loop-1] != TEST_UNKNOWN)LED_Start(LED_SELF_CHECK,COLOR_RED);
  207. else LED_Start(LED_SELF_CHECK,COLOR_WHITE);
  208. break;
  209. case TEST_RESULT_LED_ERR_BLUE:
  210. if(self_check_result_buff[loop-1] != TEST_UNKNOWN)LED_Start(LED_SELF_CHECK,COLOR_BLUE);
  211. else LED_Start(LED_SELF_CHECK,COLOR_WHITE);
  212. break;
  213. case TEST_RESULT_LED_ERR_PURPLE:
  214. if(self_check_result_buff[loop-1] != TEST_UNKNOWN)LED_Start(LED_SELF_CHECK,COLOR_PURPLE);
  215. else LED_Start(LED_SELF_CHECK,COLOR_WHITE);
  216. break;
  217. case TEST_RESULT_LED_ERR_YELLOW:
  218. if(self_check_result_buff[loop-1] != TEST_UNKNOWN)LED_Start(LED_SELF_CHECK,COLOR_YELLOW);
  219. else LED_Start(LED_SELF_CHECK,COLOR_WHITE);
  220. break;
  221. }
  222. Process_UpdatePeroid(app_self_checking_artificial,DISPLAY_TEST_RESULT_LED_TIME);
  223. }else{
  224. Process_UpdatePeroid(app_self_checking_artificial,0); //通过的某项就跳过等待时间。
  225. }
  226. if(loop == TEST_ITEMS){
  227. loop = 0;
  228. state = 1;
  229. }
  230. }
  231. break;
  232. case 1:
  233. for(int i=0;i<TEST_ITEMS;i++)temp_result +=self_check_result_buff[i];
  234. temp_result = (temp_result == 0) ? DISPLAY_TEST_RESULT_LED_TIME : (temp_result * DISPLAY_TEST_RESULT_LED_TIME);//跑完一次case0的时间
  235. if((temp_result * ++display_times) <= DISPLAY_TEST_RESULT_CONTINUE_TIME){
  236. state = 0;
  237. break;
  238. }
  239. display_times = 0;
  240. Process_UpdatePeroid(app_self_checking_artificial,0);
  241. LED_Stop(LED_SELF_CHECK);
  242. nrf_gpio_pin_write(PIN_LED_RUN,LED_SMALL_DISABLE);
  243. state = 0;
  244. Process_SetHoldOn(app_self_checking_artificial,0);
  245. Process_Stop(app_self_checking_artificial);
  246. break;
  247. default:state=0;Process_UpdatePeroid(app_self_checking_artificial,0);break;
  248. }
  249. }
  250. //测试前脚IMU和地磁是否正常
  251. static void app_checking_front_sensor(uint8_t *check_result)
  252. {
  253. int16_t acc_front[3];
  254. int16_t gry_front[3];
  255. int16_t mag6310_front[3];
  256. int32_t timestamp_front;
  257. for(int i=0; i < IMU_Get_Front_Data_Num(); i++)
  258. {
  259. IMU_Get_Front_Data(i, gry_front, acc_front, mag6310_front, &timestamp_front);
  260. if( (gry_front[0] == 0 && gry_front[1] == 0 && gry_front[2] == 0) || \
  261. (acc_front[0] == 0 && acc_front[1] == 0 && acc_front[2] == 0) || \
  262. (mag6310_front[0] == 0 && mag6310_front[1] == 0 && mag6310_front[2] == 0) ||
  263. (timestamp_front == 0))
  264. {
  265. *check_result = TEST_FAIL;
  266. }
  267. else if(mFlash.exception.except_data_front_acc || mFlash.exception.except_data_front_gry || mFlash.exception.except_data_front_mag){
  268. *check_result = TEST_FAIL;
  269. }
  270. else if(monitor_duration)*check_result = TEST_UNKNOWN;
  271. }
  272. }
  273. //测试后脚地磁是否正常
  274. static void app_checking_back_sensor(uint8_t *check_result)
  275. {
  276. static int16_t mag6310_back[3];
  277. IMU_Get_Back_Data(mag6310_back);
  278. if(mag6310_back[0] == 0 && mag6310_back[1] == 0 && mag6310_back[2] == 0)*check_result = TEST_FAIL;
  279. else if(mFlash.exception.except_data_back_mag)*check_result = TEST_FAIL;
  280. else if(monitor_duration)*check_result = TEST_UNKNOWN;
  281. }
  282. //测试电量检测是否正常
  283. static void app_checking_bat(uint8_t *check_result)
  284. {
  285. int16_t cur_vol;
  286. int16_t bat;
  287. cur_vol = ADC_RESULT_IN_MILLI_VOLTS(ADC_GetValue(PIN_ADC_CHARGMEASURE_CHANNEL));
  288. bat = ADC_RESULT_IN_MILLI_VOLTS(ADC_GetValue(PIN_ADC_BAT_CHANNEL))*5/3;
  289. SEGGER_RTT_printf(0,"app_checking_bat cur_vol:%d before_check_charge_vol:%d cur_vol-before_check_charge_vol:%d\r\n",cur_vol,before_check_charge_vol,cur_vol-before_check_charge_vol);
  290. //电池小于2.5V或充电电压变化小于阈值且电池电压大于4V
  291. if(bat <= BATTERY_TEST_VALUE || ((cur_vol-before_check_charge_vol)<BATTERY_CHARGE_CHANGE_VALUE && bat > 4000))*check_result = TEST_FAIL;
  292. else if(monitor_duration)*check_result = TEST_UNKNOWN;
  293. }
  294. //测试充电电流是否正常
  295. static void app_checking_charge(uint8_t *check_result)
  296. {
  297. int16_t cur_vol;
  298. cur_vol = ADC_GetValue(PIN_ADC_CHARGMEASURE_CHANNEL);
  299. cur_vol = ADC_RESULT_IN_MILLI_VOLTS(cur_vol);
  300. SEGGER_RTT_printf(0,"app_checking_charge cur_vol:%d before_check_charge_vol:%d cur_vol-before_check_charge_vol:%d\r\n",cur_vol,before_check_charge_vol,cur_vol-before_check_charge_vol);
  301. //当电池电量满了,不充电(经测试,哪怕电量满了,充电电压也有10+mv)
  302. //当电池电量没满,充电(经测试,电量没满的充电电压跟电池电压有关,最小充电电压100+mv)
  303. if((cur_vol-before_check_charge_vol)<BATTERY_CHARGE_CHANGE_VALUE)*check_result = TEST_FAIL;
  304. else if(monitor_duration)*check_result = TEST_UNKNOWN;
  305. }
  306. //发送非人工检测结果,只有在插上充电时,才配置串口
  307. static void app_self_check_send_result(uint8_t *buf, uint8_t datalen)
  308. {
  309. uint32_t txd,rxd;
  310. UART0_GetPinConfig(&txd, &rxd);
  311. UART0_Initialize(PIN_TXD_BLE,PIN_RXD_BLE,UART_HZ);
  312. for(int i=0;i<10;i++)
  313. {
  314. UART0_Tx_Send(0,UART0_T_SELF_CHECK_ACK,buf,datalen);
  315. }
  316. UART0_Initialize(txd,rxd,UART_HZ);
  317. }
  318. //触发自检配置用于监测数据变化,不需要通过串口发送结果。
  319. static void app_self_checking_monitor_trigger(void)
  320. {
  321. static uint32_t tim =0;
  322. if(TIME_GetTicks()-tim>=StandByPower_Interval){tim = TIME_GetTicks();
  323. int16_t front_acc[3];
  324. if(IMU_Get_Front_Data_Num() >= 1)IMU_Get_Front_Data(IMU_Get_Front_Data_Num()-1, NULL, front_acc, NULL, NULL);
  325. // SEGGER_RTT_printf(0,"front_acc[0]=%d\r,front_acc[1]=%d\r,front_acc[2]=%d\r\n",front_acc[0],front_acc[1],front_acc[2]);
  326. if(front_acc[2] >= TRIGGER_MONITOR_ACC_Z){
  327. if(enter_monitor_times == 0){
  328. if(hal_mode_get() != HAL_MODE_SELF_CHECK){
  329. hal_mode_set(HAL_MODE_SELF_CHECK); //维持一段时间,开启自检模式
  330. enter_monitor_flag = 1;
  331. }
  332. enter_monitor_times = ENTER_MONITOR_TIME;
  333. }else{
  334. enter_monitor_times--;
  335. }
  336. }else{
  337. enter_monitor_times = ENTER_MONITOR_TIME;
  338. if(enter_monitor_flag && self_check_state != ENTER_SELF_CHECK){
  339. enter_monitor_flag = 0;
  340. if(hal_mode_get() != HAL_MODE_NORMAL)hal_mode_set(HAL_MODE_NORMAL); //关闭自检模式
  341. }
  342. }
  343. }
  344. }
  345. void app_self_checking_Process(void)
  346. {
  347. static uint8_t wait_times = 20 + 1; // 预防IMU初始化失败,重复几次才初始化成功的情况,所以等待时间为初始化失败上限+1
  348. //触发自检配置用于监测数据变化,不需要通过串口发送结果。
  349. app_self_checking_monitor_trigger();
  350. //触发自检配置用于自检流程,需要通过串口发送结果。
  351. if(self_check_state == ENTER_SELF_CHECK){
  352. if(wait_times-- != 0)return;
  353. wait_times = 20 + 1;
  354. memset(self_check_result_buff,TEST_SUCCESS,TEST_ITEMS);
  355. if(app_charge_Getstate()!=BLE_Client_T_CHARGE_PULLOUT){
  356. //如果插上充电,测试充电电流是否正常
  357. app_checking_charge(&self_check_result_buff[3]);
  358. }
  359. if(hal_mode_get() == HAL_MODE_SELF_CHECK){
  360. //测试前脚IMU和地磁是否正常
  361. app_checking_front_sensor(&self_check_result_buff[0]);
  362. }else{
  363. //前脚传感器损坏
  364. self_check_result_buff[0] = TEST_FAIL;
  365. }
  366. //测试后脚地磁是否正常
  367. app_checking_back_sensor(&self_check_result_buff[1]);
  368. //测试电量检测是否正常
  369. app_checking_bat(&self_check_result_buff[2]);
  370. //发送非人工检测结果,只有在插上充电时,才配置串口
  371. if(app_charge_Getstate()!=BLE_Client_T_CHARGE_PULLOUT){
  372. app_self_check_send_result(self_check_result_buff, TEST_ITEMS);
  373. }
  374. //关闭自检模式
  375. hal_mode_set(HAL_MODE_NORMAL);
  376. self_check_state = QUITE_SELF_CHECK;
  377. // self_check_result_buff[0] = TEST_FAIL;
  378. // self_check_result_buff[1] = TEST_FAIL;
  379. // self_check_result_buff[2] = TEST_FAIL;
  380. // self_check_result_buff[3] = TEST_FAIL;
  381. // self_check_result_buff[0] = TEST_SUCCESS;
  382. // self_check_result_buff[1] = TEST_SUCCESS;
  383. // self_check_result_buff[2] = TEST_SUCCESS;
  384. // self_check_result_buff[3] = TEST_SUCCESS;
  385. //开启需要人工去测试的线程
  386. Process_Start(0,"app_self_checking_artificial",app_self_checking_artificial);
  387. }else if(app_charge_Getstate()==BLE_Client_T_CHARGE_PULLOUT){
  388. //更新可接受的自检指令最大次数
  389. self_check_recive_order_times = SELF_CHECK_RECIVE_ORDER_TIMES;
  390. //用于判断充电芯片和电池是否断线
  391. int16_t temp_vol = ADC_RESULT_IN_MILLI_VOLTS(ADC_GetValue(PIN_ADC_CHARGMEASURE_CHANNEL));
  392. before_check_charge_vol = before_check_charge_vol > temp_vol ? temp_vol : before_check_charge_vol;
  393. }
  394. // //地磁触发自检
  395. // else if((IMU_GetCurrentMode() == STATE_LOW_POWER_MODE) && app_charge_Getstate()!=BLE_Client_T_CHARGE_PULLOUT){
  396. // int16_t mag[3];
  397. // IMU_Get_Index_Front_Low_Power_Data(NULL, mag, NULL, IMU_Get_Front_Update_Data_GroupNum());
  398. // int32_t front_mag_norm;
  399. // front_mag_norm = (int32_t)(sqrt((float) (mag[0] * mag[0] + mag[1] * mag[1] + mag[2] * mag[2])));
  400. // if(front_mag_norm>=20000){
  401. // IMU_SetSelfCheckMode(1); //开机,开启自检模式
  402. // self_check_state = ENTER_SELF_CHECK;
  403. // }
  404. // }
  405. // //用于测量充电电压、电池电压、电量百分比
  406. // int16_t bat;
  407. // int16_t vol;
  408. // if(app_charge_Getstate()!=BLE_Client_T_CHARGE_PULLOUT){
  409. // bat = ADC_GetValue(PIN_ADC_BAT_CHANNEL);
  410. // bat = ADC_RESULT_IN_MILLI_VOLTS(bat)*5/3;
  411. // SEGGER_RTT_printf(0,"charge!!! %d %d %d\r\n",before_check_charge_vol,bat,GetBatteryPersent());
  412. // }else{
  413. // bat = ADC_GetValue(PIN_ADC_BAT_CHANNEL);
  414. // bat = ADC_RESULT_IN_MILLI_VOLTS(bat)*5/3;
  415. // SEGGER_RTT_printf(0,"no charge!!! %d %d %d\r\n",before_check_charge_vol,bat,GetBatteryPersent());
  416. // }
  417. }
  418. void app_self_checking_ready_Process(void)
  419. {
  420. char advname_buf[100];
  421. int adv_len;
  422. if(app_charge_Getstate()!=BLE_Client_T_CHARGE_PULLOUT && self_check_recive_order_times){
  423. slave_get_advname_len(&adv_len);
  424. slave_get_advname(advname_buf, adv_len);
  425. uint32_t txd,rxd;
  426. UART0_GetPinConfig(&txd, &rxd);
  427. UART0_Initialize(PIN_TXD_BLE,UART0_INVALID_PIN,UART_HZ);
  428. UART0_Tx_Send(0,UART0_T_SELF_CHECK_RDY,(uint8_t*)advname_buf,adv_len);
  429. // SEGGER_RTT_printf(0,"advname_buf:%s len:%d\n",advname_buf,adv_len);
  430. UART0_Initialize(txd,rxd,UART_HZ);
  431. }
  432. }
  433. void cb_UART0_R_SELF_CHECK_ASK(void* handle)
  434. {
  435. if(self_check_recive_order_times){
  436. hal_mode_set(HAL_MODE_SELF_CHECK); //串口接收自检指令,开启自检模式
  437. self_check_state = ENTER_SELF_CHECK;
  438. self_check_recive_order_times--;
  439. }
  440. }
  441. void app_self_checking_Init(void)
  442. {
  443. UART0_Rx_Regist(UART0_R_SELF_CHECK_ASK,cb_UART0_R_SELF_CHECK_ASK);
  444. Process_Start(0,"app_self_checking_monitor_Process",app_self_checking_monitor_Process);
  445. Process_Start(APP_SELF_CHECKING_PROCESS_CYCLE,"app_self_checking_Process",app_self_checking_Process);
  446. Process_Start(APP_SELF_CHECKING_READY_PROCESS_CYCLE,"app_self_checking_ready_Process",app_self_checking_ready_Process);
  447. }