exception.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. #include "exception.h"
  2. #include "app_flash.h"
  3. #include "bsp_time.h"
  4. #include "system.h"
  5. #include "hal_imu.h"
  6. #include "hal_mode_manage.h"
  7. #include "hal_led.h"
  8. #define TRIGGER_EXCEPT_CONS_DISPLAY_ACC_Z 1500 //触发异常显示的加速度Z轴
  9. #define TRIGGER_EXCEPT_PROS_DISPLAY_ACC_Z -1500 //复位触发异常显示的加速度Z轴
  10. #define DISPLAY_EXCEPT_LED_TIME 200 //用灯显示异常结果的时间,单位ms
  11. #define DISPLAY_EXCEPT_CONTINUE_TIME 10000 //显示异常结果的总时间,单位ms
  12. ExcepSign Global_Ex;
  13. static uint8_t except_result_led_onoff = 0;
  14. static uint32_t display_times = 0;
  15. static void Except_Result_LED(void)
  16. {
  17. static uint8_t state = 0;
  18. static uint8_t toggle = 1;
  19. switch(state){
  20. case 0:
  21. Process_SetHoldOn(Except_Result_LED,1);
  22. if(toggle){
  23. LED_Start(LED_EXCEPT,COLOR_ORANGE);
  24. toggle = 0;
  25. }else{
  26. LED_Start(LED_EXCEPT,COLOR_BLACK);
  27. toggle = 1;
  28. }
  29. state = 1;
  30. Process_UpdatePeroid(Except_Result_LED,DISPLAY_EXCEPT_LED_TIME);
  31. break;
  32. case 1:
  33. if((DISPLAY_EXCEPT_LED_TIME * ++display_times) <= DISPLAY_EXCEPT_CONTINUE_TIME){
  34. Process_UpdatePeroid(Except_Result_LED,0);//跳过判断时间。
  35. state = 0;
  36. break;
  37. }
  38. display_times = 0;
  39. Process_UpdatePeroid(Except_Result_LED,0);
  40. LED_Stop(LED_EXCEPT);
  41. state = 0;
  42. toggle = 1;
  43. Process_SetHoldOn(Except_Result_LED,0);
  44. Process_Stop(Except_Result_LED);
  45. break;
  46. default:state=0;Process_UpdatePeroid(Except_Result_LED,0);break;
  47. }
  48. }
  49. static void Exception_Process(void)
  50. {
  51. int16_t front_acc[3];
  52. if(IMU_Get_Front_Data_Num() >= 1)IMU_Get_Front_Data(IMU_Get_Front_Data_Num()-1, NULL, front_acc, NULL, NULL);
  53. if(front_acc[2] >= TRIGGER_EXCEPT_CONS_DISPLAY_ACC_Z && except_result_led_onoff == 0){
  54. if(mFlash.exception.except_number_of_exist > 0){
  55. Process_Start(0,"Except_Result_LED",Except_Result_LED); //开灯
  56. display_times = 1;
  57. except_result_led_onoff = 1;
  58. }
  59. }else if(front_acc[2] <= TRIGGER_EXCEPT_PROS_DISPLAY_ACC_Z && except_result_led_onoff == 1 && display_times == 0){
  60. except_result_led_onoff = 0;
  61. }
  62. }
  63. void Except_SaveExceptype(ExcepType_t excep_type)
  64. {
  65. switch(excep_type)
  66. {
  67. case EXCEPT_DATA_BACK_MAG:
  68. if(mFlash.exception.except_data_back_mag != 1){
  69. mFlash.exception.except_data_back_mag = 1;
  70. mFlash.exception.except_number_of_exist++;
  71. Flash_SaveInfomation();
  72. }
  73. break;
  74. case EXCEPT_DATA_FRONT_ACC:
  75. if(mFlash.exception.except_data_front_acc != 1){
  76. mFlash.exception.except_data_front_acc = 1;
  77. mFlash.exception.except_number_of_exist++;
  78. Flash_SaveInfomation();
  79. }
  80. break;
  81. case EXCEPT_DATA_FRONT_GRY:
  82. if(mFlash.exception.except_data_front_gry != 1){
  83. mFlash.exception.except_data_front_gry = 1;
  84. mFlash.exception.except_number_of_exist++;
  85. Flash_SaveInfomation();
  86. }
  87. break;
  88. case EXCEPT_DATA_FRONT_MAG:
  89. if(mFlash.exception.except_data_front_mag != 1){
  90. mFlash.exception.except_data_front_mag = 1;
  91. mFlash.exception.except_number_of_exist++;
  92. Flash_SaveInfomation();
  93. }
  94. break;
  95. case EXCEPT_DATA_CHARGE:
  96. if(mFlash.exception.except_data_charge != 1){
  97. mFlash.exception.except_data_charge = 1;
  98. mFlash.exception.except_number_of_exist++;
  99. Flash_SaveInfomation();
  100. }
  101. break;
  102. case EXCEPT_DATA_BATTERY:
  103. if(mFlash.exception.except_data_battery != 1){
  104. mFlash.exception.except_data_battery = 1;
  105. mFlash.exception.except_number_of_exist++;
  106. Flash_SaveInfomation();
  107. }
  108. break;
  109. case EXCEPT_MODE_SUSPEND_OVERFLOW:
  110. if(mFlash.exception.except_mode_suspend_overflow != 1){
  111. mFlash.exception.except_mode_suspend_overflow = 1;
  112. mFlash.exception.except_number_of_exist++;
  113. Flash_SaveInfomation();
  114. }
  115. break;
  116. default:
  117. break;
  118. }
  119. }
  120. bool Except_IsError(ExcepType_t excep_type)
  121. {
  122. switch(excep_type)
  123. {
  124. case EXCEPT_DATA_BACK_MAG:
  125. if(mFlash.exception.except_data_back_mag == 1){
  126. return true;
  127. }
  128. break;
  129. case EXCEPT_DATA_FRONT_ACC:
  130. if(mFlash.exception.except_data_front_acc == 1){
  131. return true;
  132. }
  133. break;
  134. case EXCEPT_DATA_FRONT_GRY:
  135. if(mFlash.exception.except_data_front_gry == 1){
  136. return true;
  137. }
  138. break;
  139. case EXCEPT_DATA_FRONT_MAG:
  140. if(mFlash.exception.except_data_front_mag == 1){
  141. return true;
  142. }
  143. break;
  144. case EXCEPT_DATA_CHARGE:
  145. if(mFlash.exception.except_data_charge == 1){
  146. return true;
  147. }
  148. break;
  149. case EXCEPT_DATA_BATTERY:
  150. if(mFlash.exception.except_data_battery == 1){
  151. return true;
  152. }
  153. break;
  154. case EXCEPT_MODE_SUSPEND_OVERFLOW:
  155. if(mFlash.exception.except_mode_suspend_overflow == 1){
  156. return true;
  157. }
  158. break;
  159. default:
  160. break;
  161. }
  162. return false;
  163. }
  164. void Exception_Init(void)
  165. {
  166. Process_Start(0,"Exception_Process",Exception_Process);
  167. }