app_self_checking.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*Includes ----------------------------------------------*/
  2. #include "system.h"
  3. #include "exception.h"
  4. #include "ble_comm.h"
  5. #include "app_charge.h"
  6. #include "app_self_checking.h"
  7. #include "hal_monitor.h"
  8. #include "hal_ble_uart0.h"
  9. #include "hal_ble_client.h"
  10. #include "hal_mode_manage.h"
  11. /*Private macro ------------------------------------------------*/
  12. #define SELF_CHECK_WHEEL_EMPTY_TIMES_MAX 20 //允许最大轮空次数
  13. #define SELF_CHECK_ITEMS 4 //自检项数
  14. #define SELF_CHECK_ITEM_SUCCESS 0 //自检项目成功
  15. #define SELF_CHECK_ITEM_FAIL 1 //自检项目失败
  16. #define SELF_CHECK_ITEM_FRONT_SENSOR_INDEX 0 //自检项目 - 前脚传感器
  17. #define SELF_CHECK_ITEM_BACK_SENSOR_INDEX 1 //自检项目 - 后脚传感器
  18. #define SELF_CHECK_ITEM_BATTERY_INDEX 2 //自检项目 - 电量
  19. #define SELF_CHECK_ITEM_CHARGE_INDEX 3 //自检项目 - 充电电压
  20. /*STRUCTION ----------------------------------------------------*/
  21. typedef enum {
  22. SELF_CHECK_STAGE_READY, //准备阶段
  23. SELF_CHECK_STAGE_CHECK, //自检阶段
  24. } SELF_CHECK_STAGE_e;
  25. typedef struct self_check
  26. {
  27. /*private member*/
  28. SELF_CHECK_STAGE_e stage; //阶段字段
  29. int wheel_empty_times; //轮空次数
  30. uint8_t result[SELF_CHECK_ITEMS]; //自检结果
  31. } Self_Check_t;
  32. /*Local Variable -----------------------------------------------*/
  33. static Self_Check_t ob_self_check;
  34. /*Local Functions ----------------------------------------------*/
  35. static void app_self_checking_Process(void)
  36. {
  37. static char adv_name[TARFET_LEN_MAX];
  38. int adv_name_len;
  39. uint32_t txd,rxd;
  40. switch(app_charge_Getstate())
  41. {
  42. case BLE_Client_T_CHARGE_PULLOUT: //没充电
  43. ob_self_check.stage = SELF_CHECK_STAGE_READY; //设置阶段为准备阶段
  44. ob_self_check.wheel_empty_times = 0; //清空轮空次数
  45. break;
  46. case BLE_Client_T_CHARGE_INSERT: //充电
  47. case BLE_Client_T_CHARGE_DONE:
  48. switch(ob_self_check.stage)
  49. {
  50. case SELF_CHECK_STAGE_READY: //准备阶段
  51. slave_get_advname_len(&adv_name_len);
  52. if(adv_name_len != 0){
  53. slave_get_advname(adv_name, adv_name_len);
  54. UART0_GetPinConfig(&txd, &rxd);
  55. UART0_Initialize(PIN_TXD_BLE,UART0_INVALID_PIN,UART_HZ);
  56. UART0_Tx_Send(0,UART0_T_SELF_CHECK_RDY,(uint8_t*)adv_name,adv_name_len); //将广播名发送给触发设备
  57. UART0_Initialize(txd,rxd,UART_HZ);
  58. }
  59. break;
  60. case SELF_CHECK_STAGE_CHECK: //自检阶段
  61. ob_self_check.wheel_empty_times++;
  62. if(ob_self_check.wheel_empty_times >= SELF_CHECK_WHEEL_EMPTY_TIMES_MAX){
  63. if(hal_mode_get() != HAL_MODE_SELF_CHECK){
  64. Except_SaveExceptype(EXCEPT_DATA_FRONT_ACC);
  65. Except_SaveExceptype(EXCEPT_DATA_FRONT_GRY);
  66. Except_SaveExceptype(EXCEPT_DATA_FRONT_MAG);
  67. }
  68. memset(ob_self_check.result, SELF_CHECK_ITEM_SUCCESS, SELF_CHECK_ITEMS);
  69. //前脚传感器
  70. if(Except_IsError(EXCEPT_DATA_FRONT_ACC)){ob_self_check.result[SELF_CHECK_ITEM_FRONT_SENSOR_INDEX] = SELF_CHECK_ITEM_FAIL;}
  71. if(Except_IsError(EXCEPT_DATA_FRONT_GRY)){ob_self_check.result[SELF_CHECK_ITEM_FRONT_SENSOR_INDEX] = SELF_CHECK_ITEM_FAIL;}
  72. if(Except_IsError(EXCEPT_DATA_FRONT_MAG)){ob_self_check.result[SELF_CHECK_ITEM_FRONT_SENSOR_INDEX] = SELF_CHECK_ITEM_FAIL;}
  73. //后脚传感器
  74. if(Except_IsError(EXCEPT_DATA_BACK_MAG)){ob_self_check.result[SELF_CHECK_ITEM_BACK_SENSOR_INDEX] = SELF_CHECK_ITEM_FAIL;}
  75. //电量
  76. if(Except_IsError(EXCEPT_DATA_BATTERY)){ob_self_check.result[SELF_CHECK_ITEM_BATTERY_INDEX] = SELF_CHECK_ITEM_FAIL;}
  77. //充电电压
  78. if(Except_IsError(EXCEPT_DATA_CHARGE)){ob_self_check.result[SELF_CHECK_ITEM_CHARGE_INDEX] = SELF_CHECK_ITEM_FAIL;}
  79. //灯显示结果,逻辑应该是先显示完灯后在去重新检测
  80. ob_self_check.wheel_empty_times = SELF_CHECK_WHEEL_EMPTY_TIMES_MAX;
  81. }
  82. break;
  83. }
  84. break;
  85. }
  86. }
  87. static void cb_UART0_R_SELF_CHECK_ASK(void* handle)
  88. {
  89. if(ob_self_check.stage == SELF_CHECK_STAGE_READY)
  90. {
  91. hal_mode_set(HAL_MODE_SELF_CHECK); //串口接收自检指令,开启自检模式
  92. ob_self_check.stage = SELF_CHECK_STAGE_CHECK; //进入自检阶段
  93. }
  94. }
  95. /*API ----------------------------------------------------------*/
  96. void app_self_checking_Init(void)
  97. {
  98. ob_self_check.stage = SELF_CHECK_STAGE_READY;
  99. ob_self_check.wheel_empty_times = 0;
  100. UART0_Rx_Regist(UART0_R_SELF_CHECK_ASK,cb_UART0_R_SELF_CHECK_ASK);
  101. Process_Start(10,"app_self_checking_Process",app_self_checking_Process);
  102. }