selfcheck.c 24 KB

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