selfcheck.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087
  1. /*Includes ----------------------------------------------*/
  2. #include "tool.h"
  3. #include "hal_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_lsm6ds3tr_c.h"
  14. #include "drv_qmc6310_v2.h"
  15. #include "drv_qma7981.h"
  16. #include "hal_led.h"
  17. #include "hal_battery.h"
  18. #include "hal_charge.h"
  19. #include "hal_ble_common.h"
  20. #include "bll_imu.h"
  21. #include "app_flash.h"
  22. #include "system.h"
  23. #include "hal_mahonyAHRS.h"
  24. #include "app_detectIsHost.h"
  25. #include "drv_trigger.h"
  26. /*Private macro ------------------------------------------------------------------------------------------------------------------------------------*/
  27. #define SELFCHECK_SCAN_DEVICE_RSSI_MIN_THRESHOLD_3 -18 //需要扫描的设备的RSSI最小阈值,-76
  28. #define SELFCHECK_SCAN_DEVICE_RSSI_MIN_THRESHOLD_2 -60 //需要扫描的设备的RSSI最小阈值,-76
  29. #define SELFCHECK_CHARGE_CHIP_PIN_ADC_MIN_THRESHOLD 80 //充电芯片引脚的最小ADC阈值
  30. #define SELFCHECK_BATTERY_PIN_ADC_MIN_THRESHOLD 2200 //电池分压电阻的最小ADC阈值
  31. #define SELFCHECK_BATTERY_PIN_ADC_MAX_THRESHOLD 4300 //电池分压电阻的最大ADC阈值,拆掉电阻后,原始值为3721,换算为5.49V
  32. #define SELFCHECK_MIDDLE_ACC_PROS_ROLL_MIN_THRESHOLD 0 //中间加速度正向ROLL值最小阈值
  33. #define SELFCHECK_MIDDLE_ACC_PROS_ROLL_MAX_THRESHOLD 35 //中间加速度正向ROLL值最大阈值
  34. #define SELFCHECK_MIDDLE_ACC_CONS_ROLL_MIN_THRESHOLD 160 //中间加速度反向ROLL值最小阈值
  35. #define SELFCHECK_MIDDLE_ACC_CONS_ROLL_MAX_THRESHOLD 180 //中间加速度反向ROLL值最大阈值
  36. #define SELFCHECK_SENSOR_MAG_NO_WELDING_CAPACITOR_MIN_THRESHOLD 200 //地磁传感器没焊接电容的最小阈值
  37. #define SELFCHECK_LED_DISPLAY_CYCLE_THRESHOLD 300 //自检灯显示周期时间,单位ms
  38. #define SELFCHECK_MIDDLE_ACC_CHECK_MT_MIN_THRESHOLD_2 3000 //中间加速度检测电机最小差值,不震动的差值是129,有个能震动的差值是1800+
  39. #define SELFCHECK_MIDDLE_ACC_CHECK_MT_MIN_THRESHOLD_3 1000 //中间加速度检测电机最小差值,不震动的差值是129,有个能震动的差值是1800+
  40. #define SELFCHECK_IMU_MONITOR_DATA_ERR_SUM_MAX 416 //数据监测错误累计最大值
  41. #define SELFCHECK_CHARGE_CHIP_PIN_ADC_COUNT 5 //充电芯片引脚检测次数
  42. /*STRUCTION ------------------------------------------------------------------------------------------------------------------------------------*/
  43. typedef enum {
  44. SELFCHECK_RESULT_SUCCESS = 0, //自检成功
  45. SELFCHECK_RESULT_ERR_FRONT_SENSOR_CONFIG_SIX_AXIS_ID, //自检失败——前脚传感器配置六轴ID
  46. SELFCHECK_RESULT_ERR_FRONT_SENSOR_CONFIG_MAG_ID, //自检失败——前脚传感器配置地磁ID
  47. SELFCHECK_RESULT_ERR_BACK_SENSOR_CONFIG_ID, //自检失败——后脚传感器配置地磁ID
  48. SELFCHECK_RESULT_ERR_FRONT_IMU_REVERSE, //自检失败——前脚传感器装反
  49. SELFCHECK_RESULT_ERR_FRONT_MAG_DATA_TO_SMALL, //自检失败——前脚传感器地磁数据过小
  50. SELFCHECK_RESULT_ERR_BACK_DATA_TO_SMALL, //自检失败——后脚传感器地磁数据过小
  51. SELFCHECK_RESULT_ERR_FRONT_SENSOR_CONFIG_SIX_AXIS, //自检失败——前脚传感器配置六轴
  52. SELFCHECK_RESULT_ERR_FRONT_SENSOR_CONFIG_MAG, //自检失败——前脚传感器配置地磁
  53. SELFCHECK_RESULT_ERR_FRONT_SIX_AXIS_NO_DATA, //自检失败——前脚传感器六轴没数据
  54. SELFCHECK_RESULT_ERR_FRONT_SIX_AXIS_EXCP_DATA, //自检失败——前脚传感器六轴数据异常(数据持续相等)
  55. SELFCHECK_RESULT_ERR_FRONT_MAG_NO_DATA, //自检失败——前脚传感器地磁没数据
  56. SELFCHECK_RESULT_ERR_FRONT_MAG_EXCP_DATA, //自检失败——前脚传感器地磁数据异常(数据持续相等)
  57. SELFCHECK_RESULT_ERR_BACK_SENSOR_CONFIG, //自检失败——后脚传感器配置
  58. SELFCHECK_RESULT_ERR_BACK_NO_DATA, //自检失败——后脚传感器没数据
  59. SELFCHECK_RESULT_ERR_BACK_EXCP_DATA, //自检失败——后脚传感器数据异常(数据持续相等)
  60. SELFCHECK_RESULT_ERR_MIDDLE_SENSOR_CONFIG, //自检失败——中间传感器配置
  61. SELFCHECK_RESULT_ERR_MIDDLE_NO_DATA_OR_EXCP_DATA, //自检失败——中间传感器没数据或数据异常
  62. SELFCHECK_RESULT_ERR_CHARGE_CHIP_PIN_ADC, //自检失败——充电芯片引脚ADC
  63. SELFCHECK_RESULT_ERR_BATTERY_PIN_ADC, //自检失败——电池分压电阻ADC
  64. SELFCHECK_RESULT_ERR_RSSI, //自检失败——RSSI
  65. SELFCHECK_RESULT_ERR_MT, //自检失败——震动电机
  66. } SELFCHECK_RESULT_e;
  67. typedef struct _selfcheck
  68. {
  69. uint32_t selfcheck_result; //自检结果
  70. uint32_t selfcheck_result_led_color; //自检结果的led灯颜色
  71. uint32_t selfcheck_result_flash_num; //自检结果闪烁次数
  72. int16_t max_rssi; //最大的RSSI值
  73. fml_imu_data_t f_data; //前脚传感器数据
  74. fml_imu_data_t b_data; //后脚传感器数据
  75. qma_data_t m_data; //中间传感器数据
  76. bool f_is_read_data; //前脚传感器是否读到数据标志位
  77. bool b_is_read_data; //后脚传感器是否读到数据标志位
  78. char order; //指令
  79. uint32_t charge_chip_adc_max; //充电芯片阈值满足数
  80. int16_t battery_adc_max; //电量最大值
  81. } SelfCheck_t;
  82. /*Local Variable ------------------------------------------------------------------------------------------------------------------------------------*/
  83. static SelfCheck_t ob_selfcheck;
  84. static const bll_imu_one_way_param_t game_front_param={
  85. .acc_power_mode = FML_IMU_ACC_POWER_MODE_NORMAL, //前脚 - 加速度正常模式
  86. .gry_power_mode = FML_IMU_GRY_POWER_MODE_NORMAL, //前脚 - 陀螺仪正常模式
  87. .timestamp_resolution = FML_IMU_TIMESTAMP_25US, //前脚 - 时间戳25US精度
  88. .timestamp_switch = FML_IMU_TIMESTAMP_ON, //前脚 - 时间戳开启
  89. .acc_fs = FML_IMU_ACC_FS_16G, //前脚 - 加速度量程 - 16G
  90. .gry_fs = FML_IMU_GRY_FS_2000DPS, //前脚 - 陀螺仪量程 - 2000DPS
  91. .mag_fs = FML_IMU_MAG_FS_30GS, //前脚 - 地磁计量程 - 30GS
  92. .acc_odr = FML_IMU_ACC_ODR_416HZ, //前脚 - 加速度采样频率 - 104HZ
  93. .gry_odr = FML_IMU_GRY_ODR_416HZ, //前脚 - 陀螺仪采样频率 - 104HZ
  94. .mag_odr = FML_IMU_MAG_ODR_200HZ, //前脚 - 地磁计采样频率 - 200HZ
  95. .fifo_odr = FML_IMU_FIFO_ODR_416HZ,
  96. };
  97. static const bll_imu_one_way_param_t game_back_param={
  98. .acc_power_mode = FML_IMU_ACC_POWER_MODE_NORMAL, //后脚 - 加速度正常模式
  99. .gry_power_mode = FML_IMU_GRY_POWER_MODE_NORMAL, //后脚 - 陀螺仪正常模式
  100. .timestamp_resolution = FML_IMU_TIMESTAMP_25US, //后脚 - 时间戳25US精度
  101. .timestamp_switch = FML_IMU_TIMESTAMP_OFF, //后脚 - 时间戳关闭
  102. .acc_fs = FML_IMU_ACC_FS_16G, //后脚 - 加速度量程 - 16G
  103. .gry_fs = FML_IMU_GRY_FS_2000DPS, //后脚 - 陀螺仪量程 - 2000DPS
  104. .mag_fs = FML_IMU_MAG_FS_30GS, //后脚 - 地磁计量程 - 30GS
  105. .acc_odr = FML_IMU_ACC_ODR_OFF, //后脚 - 加速度采样频率 - 关闭
  106. .gry_odr = FML_IMU_GRY_ODR_OFF, //后脚 - 陀螺仪采样频率 - 关闭
  107. .mag_odr = FML_IMU_MAG_ODR_200HZ, //后脚 - 地磁计采样频率 - 200HZ
  108. .fifo_odr = FML_IMU_FIFO_ODR_OFF,
  109. };
  110. static const bll_imu_param_t game_bll_imu_param_t={
  111. .config_param[FML_IMU_DIR_FRONT] = &game_front_param,
  112. .config_param[FML_IMU_DIR_BACK] = &game_back_param,
  113. };
  114. static MahonyAHRS_t Self_Mind_Mahony={0};
  115. static MahonyAHRS_t Self_Front_Mahony={0};
  116. /*Local Functions ------------------------------------------------------------------------------------------------------------------------------------*/
  117. static void monitor_sensor_data(int16_t *f_acc, int16_t *f_gry, int16_t *f_mag, int16_t *b_mag)
  118. {
  119. static int16_t last_f_acc[3]; //上一次的前脚加速度值
  120. static int16_t last_f_gry[3]; //上一次的前脚陀螺仪值
  121. static int16_t last_f_mag[3]; //上一次的前脚地磁计值
  122. static int16_t last_b_mag[3]; //上一次的后脚地磁计值
  123. static int16_t last_f_acc_err_sum; //上一次的前脚加速度值错误累计
  124. static int16_t last_f_gry_err_sum; //上一次的前脚陀螺仪值错误累计
  125. static int16_t last_f_mag_err_sum; //上一次的前脚地磁计值错误累计
  126. static int16_t last_b_mag_err_sum; //上一次的后脚地磁计值错误累计
  127. /*前脚加速度*/
  128. if(f_acc != NULL)
  129. {
  130. if(
  131. last_f_acc[0] == f_acc[0] && \
  132. last_f_acc[1] == f_acc[1] && \
  133. last_f_acc[2] == f_acc[2]
  134. )
  135. {
  136. last_f_acc_err_sum++;
  137. if(last_f_acc_err_sum >= SELFCHECK_IMU_MONITOR_DATA_ERR_SUM_MAX){
  138. ob_selfcheck.selfcheck_result |= (1 << SELFCHECK_RESULT_ERR_FRONT_SIX_AXIS_EXCP_DATA);
  139. }
  140. }else{
  141. last_f_acc_err_sum = 0;
  142. }
  143. last_f_acc[0] = f_acc[0];
  144. last_f_acc[1] = f_acc[1];
  145. last_f_acc[2] = f_acc[2];
  146. }
  147. /*前脚陀螺仪*/
  148. if(f_gry != NULL)
  149. {
  150. if(
  151. last_f_gry[0] == f_gry[0] && \
  152. last_f_gry[1] == f_gry[1] && \
  153. last_f_gry[2] == f_gry[2]
  154. )
  155. {
  156. last_f_gry_err_sum++;
  157. if(last_f_gry_err_sum >= SELFCHECK_IMU_MONITOR_DATA_ERR_SUM_MAX){
  158. ob_selfcheck.selfcheck_result |= (1 << SELFCHECK_RESULT_ERR_FRONT_SIX_AXIS_EXCP_DATA);
  159. }
  160. }else{
  161. last_f_gry_err_sum = 0;
  162. }
  163. last_f_gry[0] = f_gry[0];
  164. last_f_gry[1] = f_gry[1];
  165. last_f_gry[2] = f_gry[2];
  166. }
  167. /*前脚地磁计*/
  168. if(f_mag != NULL)
  169. {
  170. if(
  171. last_f_mag[0] == f_mag[0] && \
  172. last_f_mag[1] == f_mag[1] && \
  173. last_f_mag[2] == f_mag[2]
  174. )
  175. {
  176. last_f_mag_err_sum++;
  177. if(last_f_mag_err_sum >= SELFCHECK_IMU_MONITOR_DATA_ERR_SUM_MAX){
  178. ob_selfcheck.selfcheck_result |= (1 << SELFCHECK_RESULT_ERR_FRONT_MAG_EXCP_DATA);
  179. }
  180. }else{
  181. last_f_mag_err_sum = 0;
  182. }
  183. last_f_mag[0] = f_mag[0];
  184. last_f_mag[1] = f_mag[1];
  185. last_f_mag[2] = f_mag[2];
  186. }
  187. /*后脚地磁计*/
  188. if(b_mag != NULL)
  189. {
  190. if(
  191. last_b_mag[0] == b_mag[0] && \
  192. last_b_mag[1] == b_mag[1] && \
  193. last_b_mag[2] == b_mag[2]
  194. )
  195. {
  196. last_b_mag_err_sum++;
  197. if(last_b_mag_err_sum >= (SELFCHECK_IMU_MONITOR_DATA_ERR_SUM_MAX/2)){
  198. ob_selfcheck.selfcheck_result |= (1 << SELFCHECK_RESULT_ERR_BACK_EXCP_DATA);
  199. }
  200. }else{
  201. last_b_mag_err_sum = 0;
  202. }
  203. last_b_mag[0] = b_mag[0];
  204. last_b_mag[1] = b_mag[1];
  205. last_b_mag[2] = b_mag[2];
  206. }
  207. }
  208. static void fb_data_notify_cb(uint32_t dir_bit)
  209. {
  210. int data_len;
  211. if((dir_bit >> BLL_IMU_DIR_FRONT) & 0x01)
  212. {
  213. memset(&ob_selfcheck.f_data,0,sizeof(ob_selfcheck.f_data));
  214. data_len = bll_imu_get_data_num(BLL_IMU_DIR_FRONT);
  215. for(int i=0; i<data_len;i++)
  216. {
  217. bll_imu_get_data(BLL_IMU_DIR_FRONT, i, &ob_selfcheck.f_data);
  218. }
  219. monitor_sensor_data(ob_selfcheck.f_data.acc, ob_selfcheck.f_data.gry, ob_selfcheck.f_data.mag, NULL);
  220. Mahony_update(&Self_Front_Mahony,0,0,0,ob_selfcheck.f_data.acc[0],ob_selfcheck.f_data.acc[1],ob_selfcheck.f_data.acc[2],0,0,0);
  221. }
  222. if((dir_bit >> BLL_IMU_DIR_BACK) & 0x01)
  223. {
  224. memset(&ob_selfcheck.b_data,0,sizeof(ob_selfcheck.b_data));
  225. data_len = bll_imu_get_data_num(BLL_IMU_DIR_BACK);
  226. for(int i=0; i<data_len;i++)
  227. {
  228. bll_imu_get_data(BLL_IMU_DIR_BACK, i, &ob_selfcheck.b_data);
  229. }
  230. monitor_sensor_data(NULL, NULL, NULL, ob_selfcheck.b_data.mag);
  231. }
  232. if(((dir_bit >> BLL_IMU_DIR_FRONT) & 0x01))
  233. {
  234. ob_selfcheck.f_is_read_data = true;
  235. }
  236. if((dir_bit >> BLL_IMU_DIR_BACK))
  237. {
  238. ob_selfcheck.b_is_read_data = true;
  239. }
  240. }
  241. static void scan_report_cb(ble_gap_evt_adv_report_t const * p_adv_report)
  242. {
  243. ob_selfcheck.max_rssi = (ob_selfcheck.max_rssi > p_adv_report->rssi)?ob_selfcheck.max_rssi:p_adv_report->rssi;
  244. }
  245. static void selfcheck_get_result_led(void);
  246. static void selfcheck_result_display_process(void)
  247. {
  248. static uint32_t led_display_count = 0;
  249. //根据自检结果显示结果:
  250. //前脚传感器——红色(前脚六轴配置问题闪烁1下,前脚地磁配置问题闪烁2下,前脚六轴数据读取失败或数据异常闪烁3下,前脚地磁数据读取失败或数据异常闪烁4下)
  251. //后脚传感器——红色(后脚地磁配置问题闪烁5下,后脚地磁数据读取失败或数据异常闪烁6下)
  252. //中间传感器——红色(中间加速度配置问题闪烁7下,加速度roll值不在范围内闪烁8下)
  253. //充电芯片和电池分压电阻和蓝牙天线rssi——红色(充电芯片问题闪烁9下,电池分压电阻闪烁10下,蓝牙天线rssi问题闪烁11下)
  254. //中间加速度检测震动电机(待定)
  255. //上述检测通过,蓝色(1秒周期,500ms亮,500ms灭(断电源线)),若检测到鞋垫,则绿色(1秒周期,500ms亮,500ms灭(断电源线))。
  256. //LED电源引脚亮灯拉高,灭灯拉低。(4秒周期,40ms亮,160ms灭(断电源线)一组)
  257. //喂狗
  258. feed_watchdog();
  259. //天线如果没有检测到可以持续检测
  260. if(ob_selfcheck.selfcheck_result_flash_num == SELFCHECK_RESULT_ERR_RSSI && (ob_selfcheck.selfcheck_result & (1 << SELFCHECK_RESULT_ERR_RSSI)) == 0)
  261. {
  262. //检测震动电机是否通过
  263. if((ob_selfcheck.selfcheck_result & (1 << SELFCHECK_RESULT_ERR_MT)) == 0)
  264. {
  265. //重新根据自检结果设置灯
  266. ob_selfcheck.selfcheck_result_led_color = COLOR_GREEN;
  267. ob_selfcheck.selfcheck_result_flash_num = 1;
  268. led_display_count = 0;
  269. WS2812_DisplayDot(COLOR_BLACK);WS2812_Pwm_Play();
  270. return;
  271. }
  272. else
  273. {
  274. //重新根据自检结果设置灯
  275. ob_selfcheck.selfcheck_result_led_color = COLOR_RED;
  276. ob_selfcheck.selfcheck_result_flash_num = SELFCHECK_RESULT_ERR_MT;
  277. led_display_count = 0;
  278. WS2812_DisplayDot(COLOR_BLACK);WS2812_Pwm_Play();
  279. return;
  280. }
  281. }
  282. Process_UpdatePeroid(selfcheck_result_display_process,SELFCHECK_LED_DISPLAY_CYCLE_THRESHOLD);
  283. led_display_count++;
  284. if(led_display_count % 2 == 0)
  285. {
  286. WS2812_DisplayDot(COLOR_BLACK);WS2812_Pwm_Play();
  287. }
  288. else
  289. {
  290. WS2812_DisplayDot(ob_selfcheck.selfcheck_result_led_color);WS2812_Pwm_Play();
  291. }
  292. if(led_display_count >= ob_selfcheck.selfcheck_result_flash_num * 2)
  293. {
  294. led_display_count = 0;
  295. WS2812_DisplayDot(COLOR_BLACK);WS2812_Pwm_Play();
  296. if(ob_selfcheck.selfcheck_result_led_color != COLOR_GREEN)Process_UpdatePeroid(selfcheck_result_display_process,SELFCHECK_LED_DISPLAY_CYCLE_THRESHOLD * 3);
  297. }
  298. }
  299. static void selfcheck_led_process(void)
  300. {
  301. //喂狗
  302. feed_watchdog();
  303. static uint8_t flow = 0;
  304. switch(flow)
  305. {
  306. case 0:
  307. WS2812_DisplayDot(COLOR_RED);WS2812_Pwm_Play();
  308. flow = 1;
  309. break;
  310. case 1:
  311. WS2812_DisplayDot(COLOR_GREEN);WS2812_Pwm_Play();
  312. flow = 2;
  313. break;
  314. case 2:
  315. WS2812_DisplayDot(COLOR_BLUE);WS2812_Pwm_Play();
  316. flow = 0;
  317. break;
  318. }
  319. }
  320. static void selfcheck_mt_process(void)
  321. {
  322. static int16_t max_acc_z = -32767;
  323. static int16_t min_acc_z = 32767;
  324. //喂狗
  325. feed_watchdog();
  326. //检测震动是否达标
  327. //读取中间传感器数据,计算加速度roll值
  328. if(ob_selfcheck.order == 0x02 || ob_selfcheck.order == 0x03)
  329. {
  330. memset(&ob_selfcheck.m_data,0,sizeof(ob_selfcheck.m_data));
  331. if(drv_qma_get_acc_data(&ob_selfcheck.m_data) == 0)
  332. {
  333. if(ob_selfcheck.m_data.acc[0]==0 && ob_selfcheck.m_data.acc[1]==0 && ob_selfcheck.m_data.acc[2]==0)return;
  334. if(ob_selfcheck.m_data.acc[0]==-1 && ob_selfcheck.m_data.acc[1]==-1 && ob_selfcheck.m_data.acc[2]==-1)return;
  335. max_acc_z = (max_acc_z > ob_selfcheck.m_data.acc[2])?max_acc_z:ob_selfcheck.m_data.acc[2];
  336. min_acc_z = (min_acc_z > ob_selfcheck.m_data.acc[2])?ob_selfcheck.m_data.acc[2]:min_acc_z;
  337. if(max_acc_z > min_acc_z)
  338. {
  339. if(ob_selfcheck.order == 0x02)
  340. {
  341. if((max_acc_z - min_acc_z) < SELFCHECK_MIDDLE_ACC_CHECK_MT_MIN_THRESHOLD_2)
  342. {
  343. ob_selfcheck.selfcheck_result |= (1 << SELFCHECK_RESULT_ERR_MT);
  344. }
  345. else
  346. {
  347. ob_selfcheck.selfcheck_result &= ~(1 << SELFCHECK_RESULT_ERR_MT);
  348. }
  349. }
  350. if(ob_selfcheck.order == 0x03)
  351. {
  352. if((max_acc_z - min_acc_z) < SELFCHECK_MIDDLE_ACC_CHECK_MT_MIN_THRESHOLD_3)
  353. {
  354. ob_selfcheck.selfcheck_result |= (1 << SELFCHECK_RESULT_ERR_MT);
  355. }
  356. else
  357. {
  358. ob_selfcheck.selfcheck_result &= ~(1 << SELFCHECK_RESULT_ERR_MT);
  359. }
  360. }
  361. }
  362. else
  363. {
  364. ob_selfcheck.selfcheck_result |= (1 << SELFCHECK_RESULT_ERR_MT);
  365. }
  366. // SEGGER_RTT_printf(0,"max_acc_z-min_acc_z:%d\r\n",max_acc_z-min_acc_z);
  367. }
  368. }
  369. }
  370. static void selfcheck_continue_mt_process(void)
  371. {
  372. static uint8_t flow = 0;
  373. static uint32_t shake_ms = 0;
  374. static uint32_t shake2_ms = 0;
  375. if((ob_selfcheck.selfcheck_result & (1 << SELFCHECK_RESULT_ERR_MT)) == 0)
  376. {
  377. shake_ms = 300;
  378. shake2_ms = 700;
  379. }
  380. else
  381. {
  382. shake_ms = 1000;
  383. shake2_ms = 1000;
  384. }
  385. //喂狗
  386. feed_watchdog();
  387. switch(flow)
  388. {
  389. case 0:
  390. nrf_gpio_pin_write(PIN_MT_EN,1);
  391. flow = 1;
  392. Process_SetHoldOn(selfcheck_continue_mt_process,1);
  393. Process_UpdatePeroid(selfcheck_continue_mt_process,shake_ms);
  394. break;
  395. case 1:
  396. nrf_gpio_pin_write(PIN_MT_EN,0);
  397. flow = 0;
  398. Process_SetHoldOn(selfcheck_continue_mt_process,0);
  399. Process_UpdatePeroid(selfcheck_continue_mt_process,shake2_ms);
  400. break;
  401. }
  402. }
  403. #define N 10 //N > 3
  404. int data[N];
  405. int middleFilter(int in_data)
  406. {
  407. int sum = 0;
  408. int temp[N];
  409. int change;
  410. int i,j;
  411. //记录数据
  412. for(i=0; i<(N-1); i++)
  413. {
  414. data[i]=data[i+1];
  415. }
  416. data[N-1] = in_data;
  417. //复制数据
  418. for(i=0; i<N; i++)
  419. temp[i] = data[i];
  420. //冒泡法排序
  421. for(i=1; i<N; i++)
  422. for(j=0; j<N-i; j++)
  423. {
  424. if(temp[i] > temp[i+1])
  425. {
  426. change = temp[i];
  427. temp[i] = temp[i+1];
  428. temp[i+1] = change;
  429. }
  430. }
  431. //求和
  432. for(i=1; i<(N-1); i++)
  433. sum = sum + temp[i];
  434. //返回平均值
  435. return(sum/((N-2)));
  436. }
  437. static void selfcheck_get_result_led(void)
  438. {
  439. //根据自检结果设置灯
  440. if(ob_selfcheck.selfcheck_result == 0)
  441. {
  442. ob_selfcheck.selfcheck_result_led_color = COLOR_GREEN;
  443. ob_selfcheck.selfcheck_result_flash_num = 1;
  444. }
  445. else
  446. {
  447. ob_selfcheck.selfcheck_result_led_color = COLOR_RED;
  448. for(int i=0;i<sizeof(ob_selfcheck.selfcheck_result)*8;i++)
  449. {
  450. if((ob_selfcheck.selfcheck_result & (1 << i)) != 0)
  451. {
  452. ob_selfcheck.selfcheck_result_flash_num = i;
  453. break;
  454. }
  455. }
  456. }
  457. }
  458. static void selfcheck_process(void)
  459. {
  460. BLL_IMU_CONFIG_RESULT f_config_result,b_config_result;
  461. uint16_t front_mag_norm;
  462. uint16_t back_mag_norm;
  463. int16_t roll;
  464. uint8_t buf[256];
  465. uint8_t L=0;
  466. char mac_buf[16];
  467. static uint32_t t_count = 0;
  468. static uint32_t battery_adc_t_count = 0;
  469. static uint32_t roll_t_count = 0;
  470. static uint32_t sensor_t_count = 0;
  471. static uint32_t last_tim = 0;
  472. static uint32_t continue_trigger = 0;
  473. t_count++;
  474. //喂狗
  475. feed_watchdog();
  476. //读取中间传感器数据,计算加速度roll值
  477. memset(&ob_selfcheck.m_data,0,sizeof(ob_selfcheck.m_data));
  478. if(drv_qma_get_acc_data(&ob_selfcheck.m_data) != 0)
  479. {
  480. ob_selfcheck.selfcheck_result |= (1 << SELFCHECK_RESULT_ERR_MIDDLE_NO_DATA_OR_EXCP_DATA);
  481. //重新配置
  482. drv_qma_set_acc_odr(QMA_ACC_ODR_104HZ);
  483. }
  484. Mahony_update(&Self_Mind_Mahony,0,0,0,ob_selfcheck.m_data.acc[0],ob_selfcheck.m_data.acc[1],ob_selfcheck.m_data.acc[2],0,0,0);
  485. //第3秒检测加速度roll值,设置自检结果。
  486. if(t_count - roll_t_count == (3000/100))
  487. {
  488. roll = (int16_t)(Self_Mind_Mahony.roll*100);
  489. if(roll < 0)roll *= -1;
  490. roll = (roll > 0 && roll < 100)?1:roll/100;
  491. if(!( \
  492. (SELFCHECK_MIDDLE_ACC_PROS_ROLL_MIN_THRESHOLD < roll && roll < SELFCHECK_MIDDLE_ACC_PROS_ROLL_MAX_THRESHOLD) || \
  493. (SELFCHECK_MIDDLE_ACC_CONS_ROLL_MIN_THRESHOLD < roll && roll < SELFCHECK_MIDDLE_ACC_CONS_ROLL_MAX_THRESHOLD) \
  494. ))ob_selfcheck.selfcheck_result |= (1 << SELFCHECK_RESULT_ERR_MIDDLE_NO_DATA_OR_EXCP_DATA);
  495. else
  496. ob_selfcheck.selfcheck_result &= ~(1 << SELFCHECK_RESULT_ERR_MIDDLE_NO_DATA_OR_EXCP_DATA);
  497. //更新计时
  498. roll_t_count = t_count;
  499. // SEGGER_RTT_printf(0,"middle roll:%d\r\n",roll);
  500. }
  501. //每1秒查看前后传感器是否配置且读取成功(至少要1秒才有结果)且值本身是否正常(如地磁没有焊接电容),设置自检结果。
  502. if(t_count - sensor_t_count >= (1000/100))
  503. {
  504. //前脚传感器判断
  505. f_config_result = bll_imu_query_config_param_is_ready(BLL_IMU_DIR_FRONT,&game_bll_imu_param_t);
  506. if(f_config_result != BLL_IMU_CONFIG_FINISH)
  507. {
  508. if(f_config_result == BLL_IMU_CONFIG_FAIL_FRONT_SIX_AXIS_GET_ID)ob_selfcheck.selfcheck_result |= (1 << SELFCHECK_RESULT_ERR_FRONT_SENSOR_CONFIG_SIX_AXIS_ID);
  509. if(f_config_result == BLL_IMU_CONFIG_FAIL_FRONT_MAG_GET_ID)ob_selfcheck.selfcheck_result |= (1 << SELFCHECK_RESULT_ERR_FRONT_SENSOR_CONFIG_MAG_ID);
  510. if(f_config_result == BLL_IMU_CONFIG_FAIL_FRONT_MAG)ob_selfcheck.selfcheck_result |= (1 << SELFCHECK_RESULT_ERR_FRONT_SENSOR_CONFIG_MAG);
  511. if(f_config_result == BLL_IMU_CONFIG_FAIL_FRONT_SIX_AXIS)ob_selfcheck.selfcheck_result |= (1 << SELFCHECK_RESULT_ERR_FRONT_SENSOR_CONFIG_SIX_AXIS);
  512. }
  513. else
  514. {
  515. //配置没问题
  516. ob_selfcheck.selfcheck_result &= ~(1 << SELFCHECK_RESULT_ERR_FRONT_SENSOR_CONFIG_MAG);
  517. ob_selfcheck.selfcheck_result &= ~(1 << SELFCHECK_RESULT_ERR_FRONT_SENSOR_CONFIG_SIX_AXIS);
  518. if(ob_selfcheck.f_is_read_data != true)ob_selfcheck.selfcheck_result |= (1 << SELFCHECK_RESULT_ERR_FRONT_SIX_AXIS_NO_DATA);
  519. else
  520. {
  521. //数据没问题
  522. ob_selfcheck.selfcheck_result &= ~(1 << SELFCHECK_RESULT_ERR_FRONT_SIX_AXIS_NO_DATA);
  523. ob_selfcheck.f_is_read_data = false;
  524. 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]));
  525. //前脚传感器地磁没焊接电容
  526. if(front_mag_norm < SELFCHECK_SENSOR_MAG_NO_WELDING_CAPACITOR_MIN_THRESHOLD)ob_selfcheck.selfcheck_result |= (1 << SELFCHECK_RESULT_ERR_FRONT_MAG_DATA_TO_SMALL);
  527. else ob_selfcheck.selfcheck_result &= ~(1 << SELFCHECK_RESULT_ERR_FRONT_MAG_DATA_TO_SMALL);
  528. }
  529. }
  530. //后脚传感器判断
  531. b_config_result = bll_imu_query_config_param_is_ready(BLL_IMU_DIR_BACK,&game_bll_imu_param_t);
  532. if(b_config_result != BLL_IMU_CONFIG_FINISH)
  533. {
  534. if(b_config_result == BLL_IMU_CONFIG_FAIL_BACK_MAG_GET_ID)ob_selfcheck.selfcheck_result |= (1 << SELFCHECK_RESULT_ERR_BACK_SENSOR_CONFIG_ID);
  535. else ob_selfcheck.selfcheck_result |= (1 << SELFCHECK_RESULT_ERR_BACK_SENSOR_CONFIG);
  536. }
  537. else
  538. {
  539. //配置没问题
  540. ob_selfcheck.selfcheck_result &= ~(1 << SELFCHECK_RESULT_ERR_BACK_SENSOR_CONFIG);
  541. if(ob_selfcheck.b_is_read_data != true)ob_selfcheck.selfcheck_result |= (1 << SELFCHECK_RESULT_ERR_BACK_NO_DATA);
  542. else
  543. {
  544. //数据没问题
  545. ob_selfcheck.selfcheck_result &= ~(1 << SELFCHECK_RESULT_ERR_BACK_NO_DATA);
  546. ob_selfcheck.b_is_read_data = false;
  547. 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]));
  548. //后脚传感器地磁没焊接电容
  549. if(back_mag_norm < SELFCHECK_SENSOR_MAG_NO_WELDING_CAPACITOR_MIN_THRESHOLD)ob_selfcheck.selfcheck_result |= (1 << SELFCHECK_RESULT_ERR_BACK_DATA_TO_SMALL);
  550. else ob_selfcheck.selfcheck_result &= ~(1 << SELFCHECK_RESULT_ERR_BACK_DATA_TO_SMALL);
  551. }
  552. }
  553. //任意传感器配置失败,重新配置
  554. 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)
  555. {
  556. bll_imu_Resume_config_param(&game_bll_imu_param_t);
  557. }
  558. //更新计时-
  559. sensor_t_count = t_count;
  560. }
  561. //第3秒获取前脚是否装反
  562. if(t_count == (3000/100) && ob_selfcheck.order == 0x03)
  563. {
  564. roll = (int16_t)(Self_Front_Mahony.roll*100);
  565. if(roll < 0)roll *= -1;
  566. roll = (roll > 0 && roll < 100)?1:roll/100;
  567. if(!(roll >=SELFCHECK_MIDDLE_ACC_CONS_ROLL_MIN_THRESHOLD && roll <= SELFCHECK_MIDDLE_ACC_CONS_ROLL_MAX_THRESHOLD))
  568. {
  569. ob_selfcheck.selfcheck_result |= (1 << SELFCHECK_RESULT_ERR_FRONT_IMU_REVERSE);
  570. }
  571. // SEGGER_RTT_printf(0,"front roll:%d\r\n",roll);
  572. }
  573. //第3秒开启震动
  574. if(t_count == (3000/100))
  575. {
  576. nrf_gpio_pin_write(PIN_MT_EN,1);
  577. //初始化震动电机自检线程
  578. Process_Start(30,"selfcheck_mt_process",selfcheck_mt_process);
  579. Process_SetHoldOn(selfcheck_mt_process,1);
  580. }
  581. //查看能否读取到任意广播名字,且最小的RSSI是否满足条件,设置自检结果。
  582. if(ob_selfcheck.order == 0x03)
  583. {
  584. if(SELFCHECK_SCAN_DEVICE_RSSI_MIN_THRESHOLD_3 >= ob_selfcheck.max_rssi)ob_selfcheck.selfcheck_result |= (1 << SELFCHECK_RESULT_ERR_RSSI);
  585. else ob_selfcheck.selfcheck_result &= ~(1 << SELFCHECK_RESULT_ERR_RSSI);
  586. // SEGGER_RTT_printf(0,"SELFCHECK_SCAN_DEVICE_RSSI_MIN_THRESHOLD\r\n");
  587. }
  588. else
  589. {
  590. if(SELFCHECK_SCAN_DEVICE_RSSI_MIN_THRESHOLD_2 >= ob_selfcheck.max_rssi)ob_selfcheck.selfcheck_result |= (1 << SELFCHECK_RESULT_ERR_RSSI);
  591. else ob_selfcheck.selfcheck_result &= ~(1 << SELFCHECK_RESULT_ERR_RSSI);
  592. // SEGGER_RTT_printf(0,"SELFCHECK_SCAN_DEVICE_RSSI_MIN_THRESHOLD_2\r\n");
  593. }
  594. //每4秒读取ADC,以5V电压测试电池分压电阻是否焊接,不考虑阻值,设置自检结果。
  595. if(t_count - battery_adc_t_count >= (4000/100))
  596. {
  597. if(!(SELFCHECK_BATTERY_PIN_ADC_MIN_THRESHOLD < ob_selfcheck.battery_adc_max && ob_selfcheck.battery_adc_max < SELFCHECK_BATTERY_PIN_ADC_MAX_THRESHOLD))ob_selfcheck.selfcheck_result |= (1 << SELFCHECK_RESULT_ERR_BATTERY_PIN_ADC);
  598. else ob_selfcheck.selfcheck_result &= ~(1 << SELFCHECK_RESULT_ERR_BATTERY_PIN_ADC);
  599. //更新计时
  600. battery_adc_t_count = t_count;
  601. // SEGGER_RTT_printf(0,"ob_selfcheck.battery_adc_max:%d\r\n",ob_selfcheck.battery_adc_max);
  602. // SEGGER_RTT_printf(0,"ob_selfcheck.charge_chip_adc_max:%d\r\n",ob_selfcheck.charge_chip_adc_max);
  603. }
  604. //第4秒关闭100毫秒震动电机自检线程
  605. if(t_count == (4000/100))
  606. {
  607. Process_Stop(selfcheck_mt_process);
  608. Process_Stop(selfcheck_led_process);
  609. WS2812_DisplayDot(COLOR_BLACK);WS2812_Pwm_Play();
  610. Process_Start(100,"selfcheck_continue_mt_process",selfcheck_continue_mt_process);
  611. Process_SetHoldOn(selfcheck_continue_mt_process,1);
  612. }
  613. //第4+SELFCHECK_LED_DISPLAY_CYCLE_THRESHOLD秒初始化自检显示结果线程,设置holdon,全功率运行。
  614. if(t_count == ((4000 + SELFCHECK_LED_DISPLAY_CYCLE_THRESHOLD)/100))
  615. {
  616. //根据自检结果设置灯
  617. selfcheck_get_result_led();
  618. Process_Start(SELFCHECK_LED_DISPLAY_CYCLE_THRESHOLD,"selfcheck_result_display_process",selfcheck_result_display_process);
  619. Process_SetHoldOn(selfcheck_result_display_process,1);
  620. }
  621. //上报中间传感器的加速度值和roll值
  622. L=0;
  623. int16_t rol = (int16_t)(Mahony_M_GetRoll()*100);
  624. int16_t pitch = (int16_t)(Mahony_M_GetPitch()*100);
  625. int16_t yaw = (int16_t)(Mahony_M_GetYaw()*100);
  626. buf[L++] = (uint8_t)(rol>>8);
  627. buf[L++] = (uint8_t)(rol>>0);
  628. buf[L++] = (uint8_t)(pitch>>8);
  629. buf[L++] = (uint8_t)(pitch>>0);
  630. buf[L++] = (uint8_t)(yaw>>8);
  631. buf[L++] = (uint8_t)(yaw>>0);
  632. buf[L++] = 0;
  633. buf[L++] = 0;
  634. buf[L++] = 0;
  635. buf[L++] = 0;
  636. buf[L++] = 0;
  637. buf[L++] = 0;
  638. buf[L++] = 0;
  639. Mahony_send_ANO(0x01,buf,L);
  640. L=0;
  641. int32_t middle_acc_x = ob_selfcheck.m_data.acc[2];
  642. int32_t middle_acc_y = ob_selfcheck.m_data.acc[1];
  643. int16_t middle_acc_z = ob_selfcheck.m_data.acc[0];
  644. middle_acc_x *= 100;
  645. middle_acc_y *= 100;
  646. middle_acc_z *= 10;
  647. buf[L++] = (uint8_t)(middle_acc_x>>24);
  648. buf[L++] = (uint8_t)(middle_acc_x>>16);
  649. buf[L++] = (uint8_t)(middle_acc_x>>8);
  650. buf[L++] = (uint8_t)(middle_acc_x>>0);
  651. buf[L++] = (uint8_t)(middle_acc_y>>24);
  652. buf[L++] = (uint8_t)(middle_acc_y>>16);
  653. buf[L++] = (uint8_t)(middle_acc_y>>8);
  654. buf[L++] = (uint8_t)(middle_acc_y>>0);
  655. buf[L++] = (uint8_t)(middle_acc_z>>8);
  656. buf[L++] = (uint8_t)(middle_acc_z>>0);
  657. Mahony_send_ANO(0x07,buf,L);
  658. //上报前脚传感器的加速度数据、陀螺仪数据、地磁计开平方和数据
  659. //上报后脚传感器的地磁计开平方和数据
  660. //上报扫描到的任意广播的rssi值
  661. if(TIME_GetTicks()-last_tim >= 1000)
  662. {
  663. last_tim = TIME_GetTicks();
  664. if(ob_selfcheck.max_rssi == -120)
  665. {
  666. nrf_ble_scan_stop();
  667. host_disconnect();
  668. if(host_isconnect() == 0)
  669. {
  670. host_set_scan_name((char *)"***********",sizeof("***********"));
  671. advdata_report_Evt_Regist(scan_report_cb);
  672. Continuous_scan_start();
  673. }
  674. }
  675. ob_selfcheck.max_rssi = -120;
  676. }
  677. L=0;
  678. buf[L++] = (uint8_t)(ob_selfcheck.f_data.acc[0]>>8);
  679. buf[L++] = (uint8_t)(ob_selfcheck.f_data.acc[0]>>0);
  680. buf[L++] = (uint8_t)(ob_selfcheck.f_data.acc[1]>>8);
  681. buf[L++] = (uint8_t)(ob_selfcheck.f_data.acc[1]>>0);
  682. buf[L++] = (uint8_t)(ob_selfcheck.f_data.acc[2]>>8);
  683. buf[L++] = (uint8_t)(ob_selfcheck.f_data.acc[2]>>0);
  684. buf[L++] = (uint8_t)(ob_selfcheck.f_data.gry[0]>>8);
  685. buf[L++] = (uint8_t)(ob_selfcheck.f_data.gry[0]>>0);
  686. buf[L++] = (uint8_t)(ob_selfcheck.f_data.gry[1]>>8);
  687. buf[L++] = (uint8_t)(ob_selfcheck.f_data.gry[1]>>0);
  688. buf[L++] = (uint8_t)(ob_selfcheck.f_data.gry[2]>>8);
  689. buf[L++] = (uint8_t)(ob_selfcheck.f_data.gry[2]>>0);
  690. 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]));
  691. 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]));
  692. buf[L++] = (uint8_t)(front_mag_norm>>8);
  693. buf[L++] = (uint8_t)(front_mag_norm>>0);
  694. buf[L++] = (uint8_t)(back_mag_norm>>8);
  695. buf[L++] = (uint8_t)(back_mag_norm>>0);
  696. buf[L++] = (uint8_t)(ob_selfcheck.max_rssi>> 8);
  697. buf[L++] = (uint8_t)(ob_selfcheck.max_rssi>>0);
  698. Mahony_send_ANO(0x02,buf,L);
  699. //退出开关
  700. uint32_t ch = drv_GetChargeTrig();
  701. if(ch > 0)
  702. {
  703. if(continue_trigger <= 10)continue_trigger = 10;
  704. }
  705. else
  706. {
  707. if(continue_trigger != 0)continue_trigger--;
  708. }
  709. // SEGGER_RTT_printf(0,"ch:%d\r\n",ch);
  710. // SEGGER_RTT_printf(0,"continue_trigger:%d,%d,%d\r\n",continue_trigger,adc_value,ch);
  711. // SEGGER_RTT_printf(0,"front mag:%d,%d,%d,%d\r\n",ob_selfcheck.f_data.mag[0],ob_selfcheck.f_data.mag[1],ob_selfcheck.f_data.mag[2],front_mag_norm);
  712. // SEGGER_RTT_printf(0,"back mag:%d,%d,%d,%d\r\n",ob_selfcheck.b_data.mag[0],ob_selfcheck.b_data.mag[1],ob_selfcheck.b_data.mag[2],back_mag_norm);
  713. //第6秒重启
  714. // if(t_count >= (5000/100) && !slave_isconnect() && continue_trigger==0)
  715. if(t_count >= (6000/100) && continue_trigger==0)
  716. {
  717. memset(mac_buf, 0, sizeof(mac_buf));
  718. 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]);
  719. ST_scan_stop();
  720. host_set_scan_name(mac_buf, strlen(mac_buf));
  721. Flash_DeleteAllInfor();
  722. Flash_DeleteAllStep();
  723. Flash_DeleteAllBackup();
  724. //恢复出厂信息
  725. memset(&mFlash,0xFF,sizeof(mFlash));
  726. memset(&mBackup,0xFF,sizeof(mBackup));
  727. //保存数据到flash
  728. if(Flash_SaveStep() != ZONE_OP_SUCCESS)Except_TxError(EXCEPT_Power,"save step fail");
  729. extern battercb_t battery_record;
  730. // extern void printbatter_cb(battercb_t *c,unsigned char *buff);
  731. memcpy(&mFlash.mbattercb_t,&battery_record,sizeof(battercb_t));
  732. mBackup.RestartCnt =0;
  733. if(Flash_SaveBackup() != ZONE_OP_SUCCESS)Except_TxError(EXCEPT_Power,"pwr save backup fail");
  734. if(Flash_SaveInfomation() != ZONE_OP_SUCCESS)Except_TxError(EXCEPT_Power,"pwr save information fail");
  735. NVIC_SystemReset();
  736. }
  737. }
  738. static void adc_callback(uint32_t sample_point, Fml_Adc_All_Channel_Adc_Value_t all_adc_value)
  739. {
  740. int16_t battery_adc_value = 0;
  741. int16_t charge_adc_value = 0;
  742. //筛选最大的电池分压后的电压
  743. fml_adc_get_value(PIN_ADC_BAT_CHANNEL,&battery_adc_value);
  744. battery_adc_value = ADC_RESULT_IN_MILLI_VOLTS(battery_adc_value) * 5 / 3;
  745. ob_selfcheck.battery_adc_max = middleFilter(battery_adc_value);
  746. //充电芯片检测
  747. fml_adc_get_value(PIN_ADC_CHARGMEASURE_CHANNEL,&charge_adc_value);
  748. //充电电流超过500ma就异常
  749. if(charge_adc_value >= 1560)
  750. {
  751. ob_selfcheck.selfcheck_result |= (1 << SELFCHECK_RESULT_ERR_CHARGE_CHIP_PIN_ADC);
  752. }
  753. else //充电电流正常的情况
  754. {
  755. //电压大于4V,不检测充电芯片,因为充电芯片断闸。
  756. if(battery_adc_value >= 4000)
  757. {
  758. ob_selfcheck.charge_chip_adc_max = SELFCHECK_CHARGE_CHIP_PIN_ADC_COUNT;
  759. }
  760. if(SELFCHECK_CHARGE_CHIP_PIN_ADC_MIN_THRESHOLD < charge_adc_value)ob_selfcheck.charge_chip_adc_max++;
  761. if(ob_selfcheck.charge_chip_adc_max >= SELFCHECK_CHARGE_CHIP_PIN_ADC_COUNT)ob_selfcheck.selfcheck_result &= ~(1 << SELFCHECK_RESULT_ERR_CHARGE_CHIP_PIN_ADC);
  762. else ob_selfcheck.selfcheck_result |= (1 << SELFCHECK_RESULT_ERR_CHARGE_CHIP_PIN_ADC);
  763. }
  764. // SEGGER_RTT_printf(0,"charge_chip_adc_max:%d,%d\r\n",ob_selfcheck.charge_chip_adc_max,charge_adc_value);
  765. }
  766. static void selfcheck_trigger_Init_Porcess(void)
  767. {
  768. if(ob_selfcheck.order != 0)
  769. {
  770. selfcheck_trigger_callback(ob_selfcheck.order); //会关掉所有线程
  771. }
  772. }
  773. /*API ------------------------------------------------------------------------------------------------------------------------------------*/
  774. /**
  775. @brief 自检触发初始化
  776. @param 无
  777. @return 无
  778. */
  779. void selfcheck_trigger_Init(void)
  780. {
  781. memset(&ob_selfcheck, 0, sizeof(ob_selfcheck.order));
  782. Process_Start(0,"selfcheck_trigger_Init_Porcess",selfcheck_trigger_Init_Porcess);
  783. }
  784. /**
  785. @brief 设置自检触发指令
  786. @param order - [in] 指令类型
  787. @return 无
  788. */
  789. void selfcheck_trigger_set_order(char order)
  790. {
  791. // SEGGER_RTT_printf(0,"order:0x%x,0x%x\r\n",order,ob_selfcheck.order);
  792. if(ob_selfcheck.order == 0)ob_selfcheck.order = order;
  793. }
  794. /**
  795. @brief 自检触发回调
  796. @param order - [in] 回调触发的指令
  797. @return 无
  798. */
  799. void selfcheck_trigger_callback(char order)
  800. {
  801. int error;
  802. uint32_t last_tim = TIME_GetTicks();
  803. //喂狗
  804. feed_watchdog();
  805. //初始化结构体,自检结果为成功
  806. memset(&ob_selfcheck,0,sizeof(ob_selfcheck));
  807. ob_selfcheck.max_rssi = -120;
  808. ob_selfcheck.selfcheck_result = SELFCHECK_RESULT_SUCCESS;
  809. ob_selfcheck.selfcheck_result_led_color = COLOR_BLACK;
  810. ob_selfcheck.f_is_read_data = false;
  811. ob_selfcheck.b_is_read_data = false;
  812. ob_selfcheck.order = order;
  813. // SEGGER_RTT_printf(0,"order:0x%x\r\n",order);
  814. //关闭所有线程,初始化0毫秒自检线程,设置holdon,全功率运行。
  815. Process_All_Stop();
  816. Process_Start(100,"selfcheck_process",selfcheck_process);
  817. Process_SetHoldOn(selfcheck_process,1);
  818. //关闭前后脚传感器电源
  819. drv_qmc6310_power_off();
  820. drv_lsm_power_off();
  821. nrf_delay_ms(200);
  822. feed_watchdog();
  823. //关闭扫描,设置任意设备扫描回调,开启扫描
  824. while(TIME_GetTicks() - last_tim <= 1000)
  825. {
  826. nrf_ble_scan_stop();
  827. host_disconnect();
  828. if(host_isconnect() == 0)break;
  829. }
  830. host_set_scan_name((char *)"***********",sizeof("***********"));
  831. advdata_report_Evt_Regist(scan_report_cb);
  832. Continuous_scan_start();
  833. //配置LED电源引脚为输出SOD1
  834. nrf_gpio_cfg(
  835. PIN_LED_ENABLE,
  836. NRF_GPIO_PIN_DIR_OUTPUT,
  837. NRF_GPIO_PIN_INPUT_DISCONNECT,
  838. NRF_GPIO_PIN_NOPULL,
  839. NRF_GPIO_PIN_S0D1,
  840. NRF_GPIO_PIN_NOSENSE);
  841. //配置震动电机引脚为输出SOS1
  842. nrf_gpio_cfg_output(PIN_MT_EN);
  843. nrf_gpio_pin_write(PIN_MT_EN,0);
  844. //配置led灯电源引脚为输出
  845. nrf_gpio_pin_write(PIN_LED_ENABLE,LED_ENABLE);
  846. //重新初始化ADC,配置所有通道。
  847. ADC_SetPinChannel(PIN_ADC_CHARGMEASURE, PIN_ADC_CHARGMEASURE_CHANNEL,NRF_GPIO_PIN_NOPULL);
  848. ADC_SetPinChannel(PIN_ADC_BAT_IN, PIN_ADC_BAT_CHANNEL,NRF_GPIO_PIN_NOPULL);
  849. fml_adc_sample_update_notify_register(adc_callback);
  850. //插电IO口初始化
  851. nrf_gpio_cfg_input(PIN_CHARGING, NRF_GPIO_PIN_NOPULL);
  852. //配置前脚传感器、中间传感器、后脚传感器
  853. bll_imu_Init();
  854. bll_imu_register_data_notify_callback(BLL_IMU_DATA_NOTIFY_CB_PRIORITY_1, fb_data_notify_cb);
  855. bll_imu_Resume_config_param(&game_bll_imu_param_t);
  856. error = drv_qma_Init();
  857. if(error == -1)ob_selfcheck.selfcheck_result |= (1 << SELFCHECK_RESULT_ERR_MIDDLE_SENSOR_CONFIG);
  858. nrf_delay_ms(20);
  859. error = drv_qma_set_acc_odr(QMA_ACC_ODR_104HZ);
  860. if(error == -1)ob_selfcheck.selfcheck_result |= (1 << SELFCHECK_RESULT_ERR_MIDDLE_SENSOR_CONFIG);
  861. //前脚地磁自检
  862. if(drv_lsm_selfcheck_mag() != 0)
  863. {
  864. ob_selfcheck.selfcheck_result |= (1 << SELFCHECK_RESULT_ERR_FRONT_MAG_DATA_TO_SMALL);
  865. // SEGGER_RTT_printf(0,"drv_lsm_selfcheck_mag() != 0!!!!!!!!!!!!!!\r\n");
  866. }
  867. //后脚地磁自检
  868. if(drv_qmc6310_selfcheck_mag() != 0)
  869. {
  870. ob_selfcheck.selfcheck_result |= (1 << SELFCHECK_RESULT_ERR_BACK_DATA_TO_SMALL);
  871. // SEGGER_RTT_printf(0,"drv_qmc6310_selfcheck_mag() != 0!!!!!!!!!!!!!!\r\n");
  872. }
  873. //初始化LED自检线程
  874. Process_Start(300,"selfcheck_led_process",selfcheck_led_process);
  875. Process_SetHoldOn(selfcheck_led_process,1);
  876. //初始化计算roll值算法
  877. Mahony_Init(&Self_Mind_Mahony,104);
  878. Mahony_Init(&Self_Front_Mahony,416);
  879. //喂狗
  880. feed_watchdog();
  881. }