selfcheck.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705
  1. /*Includes ----------------------------------------------*/
  2. #include "tool.h"
  3. #include "MahonyAHRS.h"
  4. #include "ble_comm.h"
  5. #include "nrf_delay.h"
  6. #include "bsp_pwm.h"
  7. #include "bsp_time.h"
  8. #include "fml_adc.h"
  9. #include "bsp_wdt.h"
  10. #include "exception.h"
  11. #include "selfcheck.h"
  12. #include "system.h"
  13. #include "drv_qma7981.h"
  14. #include "hal_led.h"
  15. #include "hal_battery.h"
  16. #include "bll_imu.h"
  17. #include "app_flash.h"
  18. #include "system.h"
  19. /*Private macro ------------------------------------------------------------------------------------------------------------------------------------*/
  20. #define SELFCHECK_SCAN_DEVICE_RSSI_MIN_THRESHOLD -60 //需要扫描的设备的RSSI最小阈值,-76
  21. #define SELFCHECK_CHARGE_CHIP_PIN_ADC_MIN_THRESHOLD 80 //充电芯片引脚的最小ADC阈值
  22. #define SELFCHECK_BATTERY_PIN_ADC_MIN_THRESHOLD 3300 //电池分压电阻的最小ADC阈值
  23. #define SELFCHECK_BATTERY_PIN_ADC_MAX_THRESHOLD 4300 //电池分压电阻的最大ADC阈值,拆掉电阻后,原始值为3721,换算为5.49V
  24. #define SELFCHECK_MIDDLE_ACC_PROS_ROLL_MIN_THRESHOLD 0 //中间加速度正向ROLL值最小阈值
  25. #define SELFCHECK_MIDDLE_ACC_PROS_ROLL_MAX_THRESHOLD 35 //中间加速度正向ROLL值最大阈值
  26. #define SELFCHECK_MIDDLE_ACC_CONS_ROLL_MIN_THRESHOLD 160 //中间加速度反向ROLL值最小阈值
  27. #define SELFCHECK_MIDDLE_ACC_CONS_ROLL_MAX_THRESHOLD 180 //中间加速度反向ROLL值最大阈值
  28. #define SELFCHECK_WEAR_INSOLE_MAG_NORM_MIN_THRESHOLD 5000 //穿鞋垫的地磁norm值最小阈值
  29. #define SELFCHECK_SENSOR_MAG_SHAKE_TRIGGER_THRESHOLD 18000 //地磁抖动触发检测
  30. #define SELFCHECK_SENSOR_MAG_NO_WELDING_CAPACITOR_MIN_THRESHOLD 200 //地磁传感器没焊接电容的最小阈值
  31. #define SELFCHECK_SENSOR_MAG_SHAKE_THRESHOLD 200 //地磁抖动阈值
  32. /*STRUCTION ------------------------------------------------------------------------------------------------------------------------------------*/
  33. typedef enum {
  34. SELFCHECK_RESULT_SUCCESS = 0, //自检成功
  35. SELFCHECK_RESULT_ERR_FRONT_SENSOR_CONFIG_SIX_AXIS, //自检失败——前脚传感器配置六轴
  36. SELFCHECK_RESULT_ERR_FRONT_SENSOR_CONFIG_MAG, //自检失败——前脚传感器配置地磁
  37. SELFCHECK_RESULT_ERR_FRONT_SIX_AXIS_NO_DATA_OR_EXCP_DATA, //自检失败——前脚传感器六轴没数据或数据异常
  38. SELFCHECK_RESULT_ERR_FRONT_MAG_NO_DATA_OR_EXCP_DATA, //自检失败——前脚传感器地磁没数据或数据异常
  39. SELFCHECK_RESULT_ERR_BACK_SENSOR_CONFIG, //自检失败——后脚传感器配置
  40. SELFCHECK_RESULT_ERR_BACK_NO_DATA_OR_EXCP_DATA, //自检失败——后脚传感器没数据或数据异常
  41. SELFCHECK_RESULT_ERR_MIDDLE_SENSOR_CONFIG, //自检失败——中间传感器配置
  42. SELFCHECK_RESULT_ERR_MIDDLE_NO_DATA_OR_EXCP_DATA, //自检失败——中间传感器没数据或数据异常
  43. SELFCHECK_RESULT_ERR_CHARGE_CHIP_PIN_ADC, //自检失败——充电芯片引脚ADC
  44. SELFCHECK_RESULT_ERR_BATTERY_PIN_ADC, //自检失败——电池分压电阻ADC
  45. SELFCHECK_RESULT_ERR_RSSI, //自检失败——RSSI
  46. } SELFCHECK_RESULT_e;
  47. typedef struct _selfcheck
  48. {
  49. uint32_t selfcheck_result; //自检结果
  50. uint32_t selfcheck_result_led_color; //自检结果的led灯颜色
  51. uint32_t selfcheck_result_flash_num; //自检结果闪烁次数
  52. bool selfcheck_is_led_display; //自检结果led灯是否显示中
  53. int16_t max_rssi; //最大的RSSI值
  54. fml_imu_data_t f_data; //前脚传感器数据
  55. fml_imu_data_t b_data; //后脚传感器数据
  56. qma_data_t m_data; //中间传感器数据
  57. bool f_is_read_data; //前脚传感器是否读到数据标志位
  58. bool b_is_read_data; //后脚传感器是否读到数据标志位
  59. } SelfCheck_t;
  60. /*Local Variable ------------------------------------------------------------------------------------------------------------------------------------*/
  61. static SelfCheck_t ob_selfcheck;
  62. static const bll_imu_one_way_param_t game_front_param={
  63. .acc_power_mode = FML_IMU_ACC_POWER_MODE_NORMAL, //前脚 - 加速度正常模式
  64. .gry_power_mode = FML_IMU_GRY_POWER_MODE_NORMAL, //前脚 - 陀螺仪正常模式
  65. .timestamp_resolution = FML_IMU_TIMESTAMP_25US, //前脚 - 时间戳25US精度
  66. .timestamp_switch = FML_IMU_TIMESTAMP_ON, //前脚 - 时间戳开启
  67. .acc_fs = FML_IMU_ACC_FS_16G, //前脚 - 加速度量程 - 16G
  68. .gry_fs = FML_IMU_GRY_FS_2000DPS, //前脚 - 陀螺仪量程 - 2000DPS
  69. .mag_fs = FML_IMU_MAG_FS_30GS, //前脚 - 地磁计量程 - 30GS
  70. .acc_odr = FML_IMU_ACC_ODR_416HZ, //前脚 - 加速度采样频率 - 104HZ
  71. .gry_odr = FML_IMU_GRY_ODR_416HZ, //前脚 - 陀螺仪采样频率 - 104HZ
  72. .mag_odr = FML_IMU_MAG_ODR_200HZ, //前脚 - 地磁计采样频率 - 200HZ
  73. .fifo_odr = FML_IMU_FIFO_ODR_416HZ,
  74. };
  75. static const bll_imu_one_way_param_t game_back_param={
  76. .acc_power_mode = FML_IMU_ACC_POWER_MODE_NORMAL, //后脚 - 加速度正常模式
  77. .gry_power_mode = FML_IMU_GRY_POWER_MODE_NORMAL, //后脚 - 陀螺仪正常模式
  78. .timestamp_resolution = FML_IMU_TIMESTAMP_25US, //后脚 - 时间戳25US精度
  79. .timestamp_switch = FML_IMU_TIMESTAMP_OFF, //后脚 - 时间戳关闭
  80. .acc_fs = FML_IMU_ACC_FS_16G, //后脚 - 加速度量程 - 16G
  81. .gry_fs = FML_IMU_GRY_FS_2000DPS, //后脚 - 陀螺仪量程 - 2000DPS
  82. .mag_fs = FML_IMU_MAG_FS_30GS, //后脚 - 地磁计量程 - 30GS
  83. .acc_odr = FML_IMU_ACC_ODR_OFF, //后脚 - 加速度采样频率 - 关闭
  84. .gry_odr = FML_IMU_GRY_ODR_OFF, //后脚 - 陀螺仪采样频率 - 关闭
  85. .mag_odr = FML_IMU_MAG_ODR_200HZ, //后脚 - 地磁计采样频率 - 200HZ
  86. .fifo_odr = FML_IMU_FIFO_ODR_OFF,
  87. };
  88. static const bll_imu_param_t game_bll_imu_param_t={
  89. .config_param[FML_IMU_DIR_FRONT] = &game_front_param,
  90. .config_param[FML_IMU_DIR_BACK] = &game_back_param,
  91. };
  92. /*Local Functions ------------------------------------------------------------------------------------------------------------------------------------*/
  93. static void fb_data_notify_cb(uint32_t dir_bit)
  94. {
  95. int data_len;
  96. if((dir_bit >> BLL_IMU_DIR_FRONT) & 0x01)
  97. {
  98. memset(&ob_selfcheck.f_data,0,sizeof(ob_selfcheck.f_data));
  99. data_len = bll_imu_get_data_num(BLL_IMU_DIR_FRONT);
  100. for(int i=0; i<data_len;i++)
  101. {
  102. bll_imu_get_data(BLL_IMU_DIR_FRONT, i, &ob_selfcheck.f_data);
  103. }
  104. }
  105. if((dir_bit >> BLL_IMU_DIR_BACK) & 0x01)
  106. {
  107. memset(&ob_selfcheck.b_data,0,sizeof(ob_selfcheck.b_data));
  108. data_len = bll_imu_get_data_num(BLL_IMU_DIR_BACK);
  109. for(int i=0; i<data_len;i++)
  110. {
  111. bll_imu_get_data(BLL_IMU_DIR_BACK, i, &ob_selfcheck.b_data);
  112. }
  113. }
  114. if(((dir_bit >> BLL_IMU_DIR_FRONT) & 0x01))
  115. {
  116. ob_selfcheck.f_is_read_data = true;
  117. }
  118. if((dir_bit >> BLL_IMU_DIR_BACK))
  119. {
  120. ob_selfcheck.b_is_read_data = true;
  121. }
  122. }
  123. static void scan_report_cb(uint8_t *adv_data, uint16_t adv_data_len,int8_t rssi)
  124. {
  125. ob_selfcheck.max_rssi = (ob_selfcheck.max_rssi > rssi)?ob_selfcheck.max_rssi:rssi;
  126. }
  127. static void selfcheck_led_display_process(void)
  128. {
  129. static uint32_t cur_flash_num = 0;
  130. static uint8_t level = LED_ENABLE;
  131. static uint8_t red_off_led_count = 0;
  132. static uint32_t tim = 0;
  133. //喂狗
  134. feed_watchdog();
  135. //led display
  136. if(ob_selfcheck.selfcheck_is_led_display == true)
  137. {
  138. if(cur_flash_num == 0)tim = TIME_GetTicks();
  139. //判断是否结束
  140. if(level == LED_ENABLE)cur_flash_num++;
  141. if(cur_flash_num > ob_selfcheck.selfcheck_result_flash_num)
  142. {
  143. nrf_gpio_pin_write(PIN_LED_ENABLE,LED_DISABLE);
  144. if(ob_selfcheck.selfcheck_result_led_color == COLOR_RED && (TIME_GetTicks() - tim < 5000))return;
  145. cur_flash_num = 0;
  146. level = LED_ENABLE;
  147. red_off_led_count = 0;
  148. ob_selfcheck.selfcheck_is_led_display = false;
  149. Process_Stop(selfcheck_led_display_process);
  150. return;
  151. }
  152. if(ob_selfcheck.selfcheck_result_led_color == COLOR_RED)
  153. {
  154. Process_Start(50,"selfcheck_led_display_process",selfcheck_led_display_process);
  155. nrf_gpio_pin_write(PIN_LED_ENABLE,level);nrf_delay_ms(5);
  156. if(level == LED_ENABLE){WS2812_DisplayDot(ob_selfcheck.selfcheck_result_led_color);WS2812_Pwm_Play();}
  157. }
  158. else
  159. {
  160. Process_Start(500,"selfcheck_led_display_process",selfcheck_led_display_process);
  161. nrf_gpio_pin_write(PIN_LED_ENABLE,level);nrf_delay_ms(5);
  162. if(level == LED_ENABLE){WS2812_DisplayDot(ob_selfcheck.selfcheck_result_led_color);WS2812_Pwm_Play();}
  163. }
  164. //周期翻转
  165. if(ob_selfcheck.selfcheck_result_led_color == COLOR_RED)
  166. {
  167. if(level == LED_ENABLE){level = LED_DISABLE;red_off_led_count = 0;}
  168. if(level == LED_DISABLE){red_off_led_count++;if(red_off_led_count >= 6)level = LED_ENABLE;}
  169. }
  170. else
  171. {
  172. level = (level == LED_ENABLE)?LED_DISABLE:LED_ENABLE;
  173. }
  174. }
  175. }
  176. static void selfcheck_result_display_process(void)
  177. {
  178. uint16_t front_mag_norm;
  179. //喂狗
  180. feed_watchdog();
  181. if(ob_selfcheck.selfcheck_is_led_display == false)
  182. {
  183. //根据自检结果显示结果:
  184. //前脚传感器——红色(前脚六轴配置问题闪烁1下,前脚地磁配置问题闪烁2下,前脚六轴数据读取失败或数据异常闪烁3下,前脚地磁数据读取失败或数据异常闪烁4下)
  185. //后脚传感器——红色(后脚地磁配置问题闪烁5下,后脚地磁数据读取失败或数据异常闪烁6下)
  186. //中间传感器——红色(中间加速度配置问题闪烁7下,加速度roll值不在范围内闪烁8下)
  187. //充电芯片和电池分压电阻和蓝牙天线rssi——红色(充电芯片问题闪烁9下,电池分压电阻闪烁10下,蓝牙天线rssi问题闪烁11下)
  188. //中间加速度检测震动电机(待定)
  189. //上述检测通过,蓝色(1秒周期,500ms亮,500ms灭(断电源线)),若检测到鞋垫,则绿色(1秒周期,500ms亮,500ms灭(断电源线))。
  190. //LED电源引脚亮灯拉高,灭灯拉低。(4秒周期,40ms亮,160ms灭(断电源线)一组)
  191. //获取颜色+闪烁次数+周期
  192. if(ob_selfcheck.selfcheck_result == 0)
  193. {
  194. front_mag_norm = sqrt((double)(ob_selfcheck.f_data.mag[0]*ob_selfcheck.f_data.mag[0]) + (double)(ob_selfcheck.f_data.mag[1]*ob_selfcheck.f_data.mag[1]) + (double)(ob_selfcheck.f_data.mag[2]*ob_selfcheck.f_data.mag[2]));
  195. if(SELFCHECK_WEAR_INSOLE_MAG_NORM_MIN_THRESHOLD <= front_mag_norm)
  196. {
  197. ob_selfcheck.selfcheck_result_led_color = COLOR_GREEN;
  198. ob_selfcheck.selfcheck_result_flash_num = 1;
  199. }
  200. else
  201. {
  202. ob_selfcheck.selfcheck_result_led_color = COLOR_BLUE;
  203. ob_selfcheck.selfcheck_result_flash_num = 1;
  204. }
  205. }
  206. else
  207. {
  208. ob_selfcheck.selfcheck_result_led_color = COLOR_RED;
  209. for(int i=0;i<sizeof(ob_selfcheck.selfcheck_result)*8;i++)
  210. {
  211. if((ob_selfcheck.selfcheck_result & (1 << i)) != 0)
  212. {
  213. ob_selfcheck.selfcheck_result_flash_num = i;
  214. break;
  215. }
  216. }
  217. }
  218. Process_Start(0,"selfcheck_led_display_process",selfcheck_led_display_process);
  219. ob_selfcheck.selfcheck_is_led_display = true;
  220. }
  221. }
  222. static void selfcheck_mt_process(void)
  223. {
  224. //喂狗
  225. feed_watchdog();
  226. nrf_gpio_pin_toggle(PIN_MT_EN);
  227. }
  228. static void selfcheck_process(void)
  229. {
  230. BLL_IMU_CONFIG_RESULT f_config_result,b_config_result;
  231. int16_t adc_value = 0;
  232. uint16_t front_mag_norm;
  233. uint16_t back_mag_norm;
  234. int16_t roll;
  235. uint8_t buf[256];
  236. uint8_t L=0;
  237. char mac_buf[16];
  238. static uint32_t charge_chip_adc_max = 0;
  239. static int16_t battery_adc_max = 0;
  240. static uint32_t t_count = 0;
  241. static uint32_t battery_adc_t_count = 0;
  242. static uint32_t roll_t_count = 0;
  243. static uint32_t sensor_t_count = 0;
  244. static uint32_t last_tim = 0;
  245. static uint32_t continue_trigger = 0;
  246. static uint32_t front_mag_norm_max = 0;
  247. static uint32_t back_mag_norm_max = 0;
  248. static uint32_t front_mag_norm_min = 0xffffffff;
  249. static uint32_t back_mag_norm_min = 0xffffffff;
  250. static bool front_mag_shake_trigger = false;
  251. static bool back_mag_shake_trigger = false;
  252. t_count++;
  253. //喂狗
  254. feed_watchdog();
  255. //读取中间传感器数据,计算加速度roll值
  256. memset(&ob_selfcheck.m_data,0,sizeof(ob_selfcheck.m_data));
  257. if(drv_qma_get_acc_data(&ob_selfcheck.m_data) != 0)
  258. {
  259. ob_selfcheck.selfcheck_result |= (1 << SELFCHECK_RESULT_ERR_MIDDLE_NO_DATA_OR_EXCP_DATA);
  260. //重新配置
  261. drv_qma_set_acc_odr(QMA_ACC_ODR_104HZ);
  262. }
  263. Mahony_process(0,0,0,ob_selfcheck.m_data.acc[0],ob_selfcheck.m_data.acc[1],ob_selfcheck.m_data.acc[2],0,0,0);
  264. //筛选最大的电池分压后的电压
  265. fml_adc_get_value(PIN_ADC_BAT_CHANNEL,&adc_value);
  266. adc_value = ADC_RESULT_IN_MILLI_VOLTS(adc_value) * 5 / 3;
  267. battery_adc_max = adc_value;
  268. //每3秒读取ADC,以5V电压测试电池分压电阻是否焊接,不考虑阻值,设置自检结果。
  269. if(t_count - battery_adc_t_count > (3000/100))
  270. {
  271. if(!(SELFCHECK_BATTERY_PIN_ADC_MIN_THRESHOLD < battery_adc_max && battery_adc_max < SELFCHECK_BATTERY_PIN_ADC_MAX_THRESHOLD))ob_selfcheck.selfcheck_result |= (1 << SELFCHECK_RESULT_ERR_BATTERY_PIN_ADC);
  272. else ob_selfcheck.selfcheck_result &= ~(1 << SELFCHECK_RESULT_ERR_BATTERY_PIN_ADC);
  273. //更新计时
  274. battery_adc_t_count = t_count;
  275. }
  276. //充电芯片检测
  277. fml_adc_get_value(PIN_ADC_CHARGMEASURE_CHANNEL,&adc_value);
  278. if(SELFCHECK_CHARGE_CHIP_PIN_ADC_MIN_THRESHOLD < adc_value)
  279. {
  280. charge_chip_adc_max++;
  281. }
  282. if(charge_chip_adc_max > 10)
  283. {
  284. ob_selfcheck.selfcheck_result &= ~(1 << SELFCHECK_RESULT_ERR_CHARGE_CHIP_PIN_ADC);
  285. }
  286. else
  287. {
  288. ob_selfcheck.selfcheck_result |= (1 << SELFCHECK_RESULT_ERR_CHARGE_CHIP_PIN_ADC);
  289. }
  290. //每3秒检测加速度roll值,设置自检结果。
  291. if(t_count - roll_t_count > (3000/100))
  292. {
  293. roll = (int16_t)(getRoll()*100);
  294. if(roll < 0)roll *= -1;
  295. roll = (roll > 0 && roll < 100)?1:roll/100;
  296. if(!( \
  297. (SELFCHECK_MIDDLE_ACC_PROS_ROLL_MIN_THRESHOLD < roll && roll < SELFCHECK_MIDDLE_ACC_PROS_ROLL_MAX_THRESHOLD) || \
  298. (SELFCHECK_MIDDLE_ACC_CONS_ROLL_MIN_THRESHOLD < roll && roll < SELFCHECK_MIDDLE_ACC_CONS_ROLL_MAX_THRESHOLD) \
  299. ))ob_selfcheck.selfcheck_result |= (1 << SELFCHECK_RESULT_ERR_MIDDLE_NO_DATA_OR_EXCP_DATA);
  300. else
  301. ob_selfcheck.selfcheck_result &= ~(1 << SELFCHECK_RESULT_ERR_MIDDLE_NO_DATA_OR_EXCP_DATA);
  302. //更新计时
  303. roll_t_count = t_count;
  304. }
  305. //每1秒查看前后传感器是否配置且读取成功(至少要1秒才有结果)且值本身是否正常(如地磁没有焊接电容),设置自检结果。
  306. if(t_count - sensor_t_count > (1000/100))
  307. {
  308. //前脚传感器判断
  309. f_config_result = bll_imu_query_config_param_is_ready(BLL_IMU_DIR_FRONT,&game_bll_imu_param_t);
  310. if(f_config_result != BLL_IMU_CONFIG_FINISH)
  311. {
  312. if(f_config_result == BLL_IMU_CONFIG_FAIL_FRONT_MAG)ob_selfcheck.selfcheck_result |= (1 << SELFCHECK_RESULT_ERR_FRONT_SENSOR_CONFIG_MAG);
  313. if(f_config_result == BLL_IMU_CONFIG_FAIL_FRONT_SIX_AXIS)ob_selfcheck.selfcheck_result |= (1 << SELFCHECK_RESULT_ERR_FRONT_SENSOR_CONFIG_SIX_AXIS);
  314. }
  315. else
  316. {
  317. //配置没问题
  318. ob_selfcheck.selfcheck_result &= ~(1 << SELFCHECK_RESULT_ERR_FRONT_SENSOR_CONFIG_MAG);
  319. ob_selfcheck.selfcheck_result &= ~(1 << SELFCHECK_RESULT_ERR_FRONT_SENSOR_CONFIG_SIX_AXIS);
  320. if(ob_selfcheck.f_is_read_data != true)ob_selfcheck.selfcheck_result |= (1 << SELFCHECK_RESULT_ERR_FRONT_SIX_AXIS_NO_DATA_OR_EXCP_DATA);
  321. else
  322. {
  323. //数据没问题
  324. ob_selfcheck.selfcheck_result &= ~(1 << SELFCHECK_RESULT_ERR_FRONT_SIX_AXIS_NO_DATA_OR_EXCP_DATA);
  325. ob_selfcheck.f_is_read_data = false;
  326. front_mag_norm = sqrt((double)(ob_selfcheck.f_data.mag[0]*ob_selfcheck.f_data.mag[0]) + (double)(ob_selfcheck.f_data.mag[1]*ob_selfcheck.f_data.mag[1]) + (double)(ob_selfcheck.f_data.mag[2]*ob_selfcheck.f_data.mag[2]));
  327. //前脚传感器地磁没焊接电容
  328. if(front_mag_norm < SELFCHECK_SENSOR_MAG_NO_WELDING_CAPACITOR_MIN_THRESHOLD)ob_selfcheck.selfcheck_result |= (1 << SELFCHECK_RESULT_ERR_FRONT_MAG_NO_DATA_OR_EXCP_DATA);
  329. else ob_selfcheck.selfcheck_result &= ~(1 << SELFCHECK_RESULT_ERR_FRONT_MAG_NO_DATA_OR_EXCP_DATA);
  330. }
  331. }
  332. //后脚传感器判断
  333. b_config_result = bll_imu_query_config_param_is_ready(BLL_IMU_DIR_BACK,&game_bll_imu_param_t);
  334. if(b_config_result != BLL_IMU_CONFIG_FINISH)ob_selfcheck.selfcheck_result |= (1 << SELFCHECK_RESULT_ERR_BACK_SENSOR_CONFIG);
  335. else
  336. {
  337. //配置没问题
  338. ob_selfcheck.selfcheck_result &= ~(1 << SELFCHECK_RESULT_ERR_BACK_SENSOR_CONFIG);
  339. if(ob_selfcheck.b_is_read_data != true)ob_selfcheck.selfcheck_result |= (1 << SELFCHECK_RESULT_ERR_BACK_NO_DATA_OR_EXCP_DATA);
  340. else
  341. {
  342. //数据没问题
  343. ob_selfcheck.selfcheck_result &= ~(1 << SELFCHECK_RESULT_ERR_BACK_NO_DATA_OR_EXCP_DATA);
  344. ob_selfcheck.b_is_read_data = false;
  345. back_mag_norm = sqrt((double)(ob_selfcheck.b_data.mag[0]*ob_selfcheck.b_data.mag[0]) + (double)(ob_selfcheck.b_data.mag[1]*ob_selfcheck.b_data.mag[1]) + (double)(ob_selfcheck.b_data.mag[2]*ob_selfcheck.b_data.mag[2]));
  346. //后脚传感器地磁没焊接电容
  347. if(back_mag_norm < SELFCHECK_SENSOR_MAG_NO_WELDING_CAPACITOR_MIN_THRESHOLD)ob_selfcheck.selfcheck_result |= (1 << SELFCHECK_RESULT_ERR_BACK_NO_DATA_OR_EXCP_DATA);
  348. else ob_selfcheck.selfcheck_result &= ~(1 << SELFCHECK_RESULT_ERR_BACK_NO_DATA_OR_EXCP_DATA);
  349. }
  350. }
  351. //任意传感器配置失败,重新配置
  352. if(f_config_result != BLL_IMU_CONFIG_FINISH || b_config_result != BLL_IMU_CONFIG_FINISH || ob_selfcheck.b_is_read_data != true || ob_selfcheck.f_is_read_data != true)
  353. {
  354. bll_imu_Resume_config_param(&game_bll_imu_param_t);
  355. }
  356. //更新计时
  357. sensor_t_count = t_count;
  358. }
  359. //fornt back mag data check shake
  360. if(t_count > (2000/100))
  361. {
  362. front_mag_norm = sqrt((double)(ob_selfcheck.f_data.mag[0]*ob_selfcheck.f_data.mag[0]) + (double)(ob_selfcheck.f_data.mag[1]*ob_selfcheck.f_data.mag[1]) + (double)(ob_selfcheck.f_data.mag[2]*ob_selfcheck.f_data.mag[2]));
  363. back_mag_norm = sqrt((double)(ob_selfcheck.b_data.mag[0]*ob_selfcheck.b_data.mag[0]) + (double)(ob_selfcheck.b_data.mag[1]*ob_selfcheck.b_data.mag[1]) + (double)(ob_selfcheck.b_data.mag[2]*ob_selfcheck.b_data.mag[2]));
  364. if(front_mag_norm >= SELFCHECK_SENSOR_MAG_SHAKE_TRIGGER_THRESHOLD)
  365. {
  366. //get max
  367. front_mag_norm_max = (front_mag_norm_max > front_mag_norm)?front_mag_norm_max:front_mag_norm;
  368. //get min
  369. front_mag_norm_min = (front_mag_norm_min > front_mag_norm)?front_mag_norm:front_mag_norm_min;
  370. front_mag_shake_trigger = true;
  371. }
  372. if(back_mag_norm >= SELFCHECK_SENSOR_MAG_SHAKE_TRIGGER_THRESHOLD)
  373. {
  374. //get max
  375. back_mag_norm_max = (back_mag_norm_max > back_mag_norm)?back_mag_norm_max:back_mag_norm;
  376. //get min
  377. back_mag_norm_min = (back_mag_norm_min > back_mag_norm)?back_mag_norm:back_mag_norm_min;
  378. back_mag_shake_trigger = true;
  379. }
  380. DEBUG_LOG("front_mag_norm_max:%d,%d,%d,%d,%d,%d,%d,%d,%d,%d\r\n",front_mag_norm,back_mag_norm,front_mag_norm_max,back_mag_norm_max, \
  381. front_mag_norm_min,back_mag_norm_min,front_mag_norm_max - front_mag_norm_min, \
  382. back_mag_norm_max - back_mag_norm_min,front_mag_shake_trigger,back_mag_shake_trigger);
  383. if(t_count >= (5000/100))
  384. {
  385. if(front_mag_norm_max - front_mag_norm_min >= SELFCHECK_SENSOR_MAG_SHAKE_THRESHOLD && front_mag_shake_trigger)
  386. {
  387. ob_selfcheck.selfcheck_result |= (1 << SELFCHECK_RESULT_ERR_FRONT_MAG_NO_DATA_OR_EXCP_DATA);
  388. }
  389. if(back_mag_norm_max - back_mag_norm_min >= SELFCHECK_SENSOR_MAG_SHAKE_THRESHOLD && back_mag_shake_trigger)
  390. {
  391. ob_selfcheck.selfcheck_result |= (1 << SELFCHECK_RESULT_ERR_BACK_NO_DATA_OR_EXCP_DATA);
  392. }
  393. }
  394. }
  395. //第5秒查看能否读取到任意广播名字,且最小的RSSI是否满足条件,设置自检结果。
  396. if(t_count == (5000/100))
  397. {
  398. if(SELFCHECK_SCAN_DEVICE_RSSI_MIN_THRESHOLD >= ob_selfcheck.max_rssi)ob_selfcheck.selfcheck_result |= (1 << SELFCHECK_RESULT_ERR_RSSI);
  399. }
  400. //第5秒关闭100毫秒震动电机自检线程,初始化40毫秒自检显示结果线程,设置holdon,全功率运行。
  401. if(t_count == (5000/100))
  402. {
  403. nrf_gpio_pin_write(PIN_MT_EN,0);
  404. Process_Stop(selfcheck_mt_process);
  405. Process_Start(0,"selfcheck_result_display_process",selfcheck_result_display_process);
  406. Process_SetHoldOn(selfcheck_result_display_process,1);
  407. }
  408. //上报中间传感器的加速度值和roll值
  409. L=0;
  410. int16_t rol = (int16_t)(getRoll()*100);
  411. int16_t pitch = (int16_t)(getPitch()*100);
  412. int16_t yaw = (int16_t)(getYaw()*100);
  413. buf[L++] = (uint8_t)(rol>>8);
  414. buf[L++] = (uint8_t)(rol>>0);
  415. buf[L++] = (uint8_t)(pitch>>8);
  416. buf[L++] = (uint8_t)(pitch>>0);
  417. buf[L++] = (uint8_t)(yaw>>8);
  418. buf[L++] = (uint8_t)(yaw>>0);
  419. buf[L++] = 0;
  420. buf[L++] = 0;
  421. buf[L++] = 0;
  422. buf[L++] = 0;
  423. buf[L++] = 0;
  424. buf[L++] = 0;
  425. buf[L++] = 0;
  426. Mahony_send_ANO(0x01,buf,L);
  427. L=0;
  428. int32_t middle_acc_x = ob_selfcheck.m_data.acc[0];
  429. int32_t middle_acc_y = ob_selfcheck.m_data.acc[1];
  430. int16_t middle_acc_z = ob_selfcheck.m_data.acc[2];
  431. middle_acc_x *= 100;
  432. middle_acc_y *= 100;
  433. middle_acc_z *= 10;
  434. buf[L++] = (uint8_t)(middle_acc_x>>24);
  435. buf[L++] = (uint8_t)(middle_acc_x>>16);
  436. buf[L++] = (uint8_t)(middle_acc_x>>8);
  437. buf[L++] = (uint8_t)(middle_acc_x>>0);
  438. buf[L++] = (uint8_t)(middle_acc_y>>24);
  439. buf[L++] = (uint8_t)(middle_acc_y>>16);
  440. buf[L++] = (uint8_t)(middle_acc_y>>8);
  441. buf[L++] = (uint8_t)(middle_acc_y>>0);
  442. buf[L++] = (uint8_t)(middle_acc_z>>8);
  443. buf[L++] = (uint8_t)(middle_acc_z>>0);
  444. Mahony_send_ANO(0x07,buf,L);
  445. //上报前脚传感器的加速度数据、陀螺仪数据、地磁计开平方和数据
  446. //上报后脚传感器的地磁计开平方和数据
  447. //上报扫描到的任意广播的rssi值
  448. if(TIME_GetTicks()-last_tim >= 1000)
  449. {
  450. last_tim = TIME_GetTicks();
  451. ob_selfcheck.max_rssi = -120;
  452. }
  453. L=0;
  454. buf[L++] = (uint8_t)(ob_selfcheck.f_data.acc[0]>>8);
  455. buf[L++] = (uint8_t)(ob_selfcheck.f_data.acc[0]>>0);
  456. buf[L++] = (uint8_t)(ob_selfcheck.f_data.acc[1]>>8);
  457. buf[L++] = (uint8_t)(ob_selfcheck.f_data.acc[1]>>0);
  458. buf[L++] = (uint8_t)(ob_selfcheck.f_data.acc[2]>>8);
  459. buf[L++] = (uint8_t)(ob_selfcheck.f_data.acc[2]>>0);
  460. buf[L++] = (uint8_t)(ob_selfcheck.f_data.gry[0]>>8);
  461. buf[L++] = (uint8_t)(ob_selfcheck.f_data.gry[0]>>0);
  462. buf[L++] = (uint8_t)(ob_selfcheck.f_data.gry[1]>>8);
  463. buf[L++] = (uint8_t)(ob_selfcheck.f_data.gry[1]>>0);
  464. buf[L++] = (uint8_t)(ob_selfcheck.f_data.gry[2]>>8);
  465. buf[L++] = (uint8_t)(ob_selfcheck.f_data.gry[2]>>0);
  466. front_mag_norm = sqrt((double)(ob_selfcheck.f_data.mag[0]*ob_selfcheck.f_data.mag[0]) + (double)(ob_selfcheck.f_data.mag[1]*ob_selfcheck.f_data.mag[1]) + (double)(ob_selfcheck.f_data.mag[2]*ob_selfcheck.f_data.mag[2]));
  467. back_mag_norm = sqrt((double)(ob_selfcheck.b_data.mag[0]*ob_selfcheck.b_data.mag[0]) + (double)(ob_selfcheck.b_data.mag[1]*ob_selfcheck.b_data.mag[1]) + (double)(ob_selfcheck.b_data.mag[2]*ob_selfcheck.b_data.mag[2]));
  468. buf[L++] = (uint8_t)(front_mag_norm>>8);
  469. buf[L++] = (uint8_t)(front_mag_norm>>0);
  470. buf[L++] = (uint8_t)(back_mag_norm>>8);
  471. buf[L++] = (uint8_t)(back_mag_norm>>0);
  472. buf[L++] = (uint8_t)(ob_selfcheck.max_rssi>> 8);
  473. buf[L++] = (uint8_t)(ob_selfcheck.max_rssi>>0);
  474. Mahony_send_ANO(0x02,buf,L);
  475. fml_adc_get_value(PIN_CHARGING_CHANNEL,&adc_value);
  476. if(adc_value >= 2000)
  477. {
  478. if(continue_trigger <= 50)continue_trigger++;
  479. }
  480. else
  481. {
  482. if(continue_trigger != 0)continue_trigger--;
  483. }
  484. //第30秒重启
  485. if(t_count >= (30000/100) && !slave_isconnect() && continue_trigger==0)
  486. {
  487. memset(mac_buf, 0, sizeof(mac_buf));
  488. sprintf(mac_buf, "%02X%02X%02X%02X%02X%02X", mFlash.macHost[0], mFlash.macHost[1], mFlash.macHost[2], mFlash.mClient.macAddr[3], mFlash.mClient.macAddr[4], mFlash.mClient.macAddr[5]);
  489. ST_scan_stop();
  490. host_set_scan_name(mac_buf, strlen(mac_buf));
  491. //保存数据到flash
  492. if(Flash_SaveStep() != ZONE_OP_SUCCESS)Except_TxError(EXCEPT_Power,"save step fail");
  493. extern battercb_t battery_record;
  494. // extern void printbatter_cb(battercb_t *c,unsigned char *buff);
  495. memcpy(&mFlash.mbattercb_t,&battery_record,sizeof(battercb_t));
  496. mBackup.RestartCnt =0;
  497. if(Flash_SaveBackup() != ZONE_OP_SUCCESS)Except_TxError(EXCEPT_Power,"pwr save backup fail");
  498. if(Flash_SaveInfomation() != ZONE_OP_SUCCESS)Except_TxError(EXCEPT_Power,"pwr save information fail");
  499. NVIC_SystemReset();
  500. }
  501. }
  502. static void adc_callback(uint32_t sample_point, Fml_Adc_All_Channel_Adc_Value_t all_adc_value)
  503. {
  504. //纯粹做覆盖
  505. }
  506. /*API ------------------------------------------------------------------------------------------------------------------------------------*/
  507. /**
  508. @brief 自检触发回调
  509. @param order - [in] 回调触发的指令
  510. @return 无
  511. */
  512. void selfcheck_trigger_callback(char order)
  513. {
  514. int error;
  515. uint32_t last_tim = TIME_GetTicks();
  516. //喂狗
  517. feed_watchdog();
  518. //初始化结构体,自检结果为成功
  519. memset(&ob_selfcheck,0,sizeof(ob_selfcheck));
  520. ob_selfcheck.max_rssi = -120;
  521. ob_selfcheck.selfcheck_result = SELFCHECK_RESULT_SUCCESS;
  522. ob_selfcheck.selfcheck_result_led_color = COLOR_BLACK;
  523. ob_selfcheck.f_is_read_data = false;
  524. ob_selfcheck.b_is_read_data = false;
  525. ob_selfcheck.selfcheck_is_led_display = false;
  526. //关闭所有线程,初始化0毫秒自检线程,设置holdon,全功率运行。
  527. Process_All_Stop();
  528. Process_Start(100,"selfcheck_process",selfcheck_process);
  529. Process_SetHoldOn(selfcheck_process,1);
  530. //关闭扫描,设置任意设备扫描回调,开启扫描
  531. while(TIME_GetTicks() - last_tim <= 1000)
  532. {
  533. host_disconnect();
  534. if(host_isconnect() == 0)break;
  535. }
  536. ST_scan_stop();
  537. host_set_scan_name((char *)"***********",sizeof("***********"));
  538. advdata_report_Evt_Regist(scan_report_cb);
  539. error = ST_scan_start();
  540. if(error != APP_SUCCESS)ob_selfcheck.selfcheck_result |= (1 << SELFCHECK_RESULT_ERR_RSSI);
  541. //配置LED电源引脚为输出SOD1
  542. nrf_gpio_cfg(
  543. PIN_LED_ENABLE,
  544. NRF_GPIO_PIN_DIR_OUTPUT,
  545. NRF_GPIO_PIN_INPUT_DISCONNECT,
  546. NRF_GPIO_PIN_NOPULL,
  547. NRF_GPIO_PIN_S0D1,
  548. NRF_GPIO_PIN_NOSENSE);
  549. //配置震动电机引脚为输出SOS1
  550. nrf_gpio_cfg_output(PIN_MT_EN);
  551. nrf_gpio_pin_write(PIN_MT_EN,0);
  552. //重新初始化ADC,配置所有通道。
  553. ADC_SetPinChannel(PIN_ADC_CHARGMEASURE, PIN_ADC_CHARGMEASURE_CHANNEL,NRF_GPIO_PIN_NOPULL);
  554. ADC_SetPinChannel(PIN_ADC_BAT_IN, PIN_ADC_BAT_CHANNEL,NRF_GPIO_PIN_NOPULL);
  555. ADC_SetPinChannel(PIN_CHARGING, PIN_CHARGING_CHANNEL,NRF_GPIO_PIN_NOPULL);
  556. fml_adc_sample_update_notify_register(adc_callback);
  557. //配置前脚传感器、中间传感器、后脚传感器
  558. bll_imu_Init();
  559. bll_imu_register_data_notify_callback(BLL_IMU_DATA_NOTIFY_CB_PRIORITY_1, fb_data_notify_cb);
  560. bll_imu_Resume_config_param(&game_bll_imu_param_t);
  561. error = drv_qma_Init();
  562. if(error == -1)ob_selfcheck.selfcheck_result |= (1 << SELFCHECK_RESULT_ERR_MIDDLE_SENSOR_CONFIG);
  563. nrf_delay_ms(20);
  564. error = drv_qma_set_acc_odr(QMA_ACC_ODR_104HZ);
  565. if(error == -1)ob_selfcheck.selfcheck_result |= (1 << SELFCHECK_RESULT_ERR_MIDDLE_SENSOR_CONFIG);
  566. //初始化500ms震动电机自检线程,拉高震动电机引脚500ms然后拉低500ms(周期)
  567. Process_Start(500,"selfcheck_mt_process",selfcheck_mt_process);
  568. Process_SetHoldOn(selfcheck_mt_process,1);
  569. //初始化计算roll值算法
  570. Mahony_Init(10);
  571. //喂狗
  572. feed_watchdog();
  573. }