task.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. #include "task.h"
  2. #include "soft_can.h"
  3. #include "tim.h"
  4. #include "string.h"
  5. #include "math.h"
  6. #include "warn.h"
  7. #include "soft_flow.h"
  8. static void Task_80_hz(WEIGHING_DEVICE *device);
  9. static void Task_10_hz(void);
  10. static void Task_5hz(void);
  11. static void Task_1hz(WEIGHING_DEVICE *device);
  12. void Task_Polling(void)
  13. {
  14. WEIGHING_DEVICE *device = Get_Device_Handle();
  15. //解析称重消息
  16. Can_Rx_Decode();
  17. if (true == Start_80_hz) // tim4定时器0.2s进入一次中断,更新此FLAG
  18. {
  19. Start_80_hz = false;
  20. }
  21. if (true == Start_10_hz)
  22. {
  23. Task_80_hz(device);
  24. Task_10_hz();
  25. Start_10_hz = false;
  26. }
  27. if (true == Start_5_hz)
  28. {
  29. // Task_5hz();
  30. can2Pmu_3hz_info();
  31. Start_5_hz = false;
  32. //发送流量计数据
  33. Flow_Function();
  34. if(factory_calibration == true)
  35. Set_Ack_Status(ack_e1);
  36. }
  37. if (true == Start_1_hz)
  38. {
  39. Task_1hz(device);
  40. Start_1_hz = false;
  41. }
  42. }
  43. static void Get_Diff(WEIGHING_DEVICE *_device)
  44. {
  45. WEIGHING_DEVICE *device = _device;
  46. volatile uint8_t licence_count[SENSOR_NUM] = {0};
  47. for (uint8_t sensor_num_c = 0; sensor_num_c < SENSOR_NUM; sensor_num_c++)
  48. {
  49. for (uint8_t sensor_num_t = 0; sensor_num_t < SENSOR_NUM; sensor_num_t++)
  50. {
  51. if (sensor_num_c == device->sensor_num_mask)
  52. {
  53. continue;
  54. }
  55. int diff = fabsf(device->sensor[sensor_num_c]->Real_Variation - device->sensor[sensor_num_t]->Real_Variation);
  56. if (diff > (int)12000 && !device->sensor[sensor_num_t]->err_flag)
  57. {
  58. licence_count[sensor_num_c]++;
  59. }
  60. if ((device->sensor_num_mask == 4) && (licence_count[sensor_num_c] >= 3))
  61. {
  62. device->sensor[sensor_num_c]->licence_flag = true;
  63. }
  64. else if ((device->sensor_num_mask != 4) && (licence_count[sensor_num_c] >= 2))
  65. {
  66. device->sensor[sensor_num_c]->licence_flag = true;
  67. }
  68. else
  69. {
  70. device->sensor[sensor_num_c]->licence_flag = false;
  71. }
  72. }
  73. }
  74. }
  75. static bool Auxiliary_Judgment(struct SENSOR *sensor)
  76. {
  77. struct SENSOR *Sensor = sensor;
  78. if (Sensor->raw_init_value <= Sensor->Raw_Value)
  79. {
  80. if (Sensor->Raw_Value - Sensor->raw_init_value >= 2500)
  81. {
  82. sensor->err_flag = true;
  83. return true;
  84. }
  85. }
  86. return false;
  87. }
  88. static void Judge_Higher(struct SENSOR *sensor, uint32_t *err_time)
  89. {
  90. struct SENSOR *Sensor = sensor;
  91. uint32_t Error_Time = *err_time;
  92. if (Sensor->Raw_Value >= BASE_VALUE)
  93. {
  94. if (((Sensor->Raw_Value <= 0x803000) && (Sensor->licence_flag)) && (HAL_GetTick() - Error_Time > 2000))
  95. {
  96. Sensor->err_flag = true;
  97. }
  98. if (((Sensor->Raw_Value > 0x801000) && (Sensor->Raw_Value <= 0x803000)) && (Sensor->licence_flag == false))
  99. {
  100. Sensor->err_flag = false;
  101. *err_time = HAL_GetTick();
  102. }
  103. else if (Sensor->Raw_Value > 0x803000)
  104. {
  105. Sensor->err_flag = false;
  106. *err_time = HAL_GetTick();
  107. }
  108. }
  109. else
  110. {
  111. if ((((Sensor->Raw_Value > 0x7FB000) && (Sensor->licence_flag))) && (HAL_GetTick() - Error_Time > 2000))
  112. {
  113. Sensor->err_flag = true;
  114. }
  115. if ((Sensor->Raw_Value <= 0x7FF000 && Sensor->Raw_Value > 0x7FB000) && (Sensor->licence_flag == false))
  116. {
  117. Sensor->err_flag = false;
  118. *err_time = HAL_GetTick();
  119. }
  120. else if (Sensor->Raw_Value <= 0x7FB000)
  121. {
  122. Sensor->err_flag = false;
  123. *err_time = HAL_GetTick();
  124. }
  125. }
  126. }
  127. static void Judge_Lower(struct SENSOR *sensor, uint32_t *err_time)
  128. {
  129. struct SENSOR *Sensor = sensor;
  130. uint32_t Error_Time = *err_time;
  131. if (Sensor->Raw_Value >= BASE_VALUE)
  132. {
  133. if (HAL_GetTick() - Error_Time > 2000)
  134. {
  135. Sensor->err_flag = true;
  136. }
  137. }
  138. else
  139. {
  140. if ((Sensor->Raw_Value > 0x7FB000) && (HAL_GetTick() - Error_Time > 2000))
  141. {
  142. Sensor->err_flag = true;
  143. }
  144. if (((Sensor->Raw_Value <= 0x7FF000) && (Sensor->Raw_Value > 0x7FB000)) && (Sensor->licence_flag == false))
  145. {
  146. Sensor->err_flag = false;
  147. *err_time = HAL_GetTick();
  148. }
  149. else if (Sensor->Raw_Value <= 0x7FB000)
  150. {
  151. Sensor->err_flag = false;
  152. *err_time = HAL_GetTick();
  153. }
  154. }
  155. }
  156. static void Judge_Equal(struct SENSOR *sensor, uint32_t *err_time)
  157. {
  158. struct SENSOR *Sensor = sensor;
  159. uint32_t Error_Time = *err_time;
  160. if (Sensor->Raw_Value >= BASE_VALUE)
  161. {
  162. if (((Sensor->Raw_Value <= 0x803000 && Sensor->licence_flag)) && (HAL_GetTick() - Error_Time > 2000))
  163. {
  164. Sensor->err_flag = true;
  165. }
  166. if (((Sensor->Raw_Value > 0x801000) && (Sensor->Raw_Value <= 0x803000)) && (Sensor->licence_flag == false))
  167. {
  168. Sensor->err_flag = false;
  169. *err_time = HAL_GetTick();
  170. }
  171. else if (Sensor->Raw_Value > 0x803000)
  172. {
  173. Sensor->err_flag = true;
  174. }
  175. }
  176. else
  177. {
  178. if ((Sensor->Raw_Value >= 0x7FB000 && (Sensor->licence_flag)) && (HAL_GetTick() - Error_Time > 2000))
  179. {
  180. Sensor->err_flag = true;
  181. }
  182. if (((Sensor->Raw_Value < 0x7FF000) && (Sensor->Raw_Value >= 0x7FB000)) && (Sensor->licence_flag == false))
  183. {
  184. Sensor->err_flag = false;
  185. *err_time = HAL_GetTick();
  186. }
  187. else if (Sensor->Raw_Value < 0x7FB000)
  188. {
  189. Sensor->err_flag = false;
  190. *err_time = HAL_GetTick();
  191. }
  192. }
  193. }
  194. void Find_Err_Sensor(WEIGHING_DEVICE *_device)
  195. {
  196. WEIGHING_DEVICE *device = _device;
  197. uint8_t sensor_num_c;
  198. static uint32_t error_time[SENSOR_NUM] = {0};
  199. Get_Diff(device);
  200. for (sensor_num_c = 0; sensor_num_c < SENSOR_NUM; sensor_num_c++)
  201. {
  202. if (sensor_num_c == device->sensor_num_mask)
  203. {
  204. continue;
  205. }
  206. struct SENSOR *sensor = device->sensor[sensor_num_c];
  207. switch (sensor->init_flag)
  208. {
  209. case higher:
  210. if (!Auxiliary_Judgment(sensor))
  211. {
  212. Judge_Higher(sensor, &error_time[sensor_num_c]);
  213. }
  214. break;
  215. case lower:
  216. if (!Auxiliary_Judgment(sensor))
  217. {
  218. Judge_Lower(sensor, &error_time[sensor_num_c]);
  219. }
  220. break;
  221. case equal:
  222. if (!Auxiliary_Judgment(sensor))
  223. {
  224. Judge_Equal(sensor, &error_time[sensor_num_c]);
  225. }
  226. break;
  227. default:
  228. break;
  229. }
  230. }
  231. }
  232. #define RATE_DT 60
  233. #define ONE_KG 1000
  234. static void Compute_Rate(WEIGHING_DEVICE *device)
  235. {
  236. static float Last_Rate = 0.f;
  237. static float Rate_buf = 0.f;
  238. Last_Rate = Rate_buf;
  239. Rate_buf = device->Weight_current;
  240. if (Last_Rate > 1e-6f)
  241. {
  242. device->rate = (uint16_t)((fabsf((Last_Rate - Rate_buf) / Last_Rate)) * RATE_DT);
  243. }
  244. }
  245. static void Task_80_hz(WEIGHING_DEVICE *device)
  246. {
  247. device->_ops->processing_data();
  248. }
  249. static void Task_10_hz(void)
  250. {
  251. Get_Sensor_Status();
  252. CAN2PMU_Send();
  253. }
  254. static void Task_5hz(void)
  255. {
  256. // Find_Err_Sensor(hdevice);
  257. }
  258. static void Task_1hz(WEIGHING_DEVICE *device)
  259. {
  260. Compute_Rate(device);
  261. }