warn.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #include "task.h"
  2. #include "soft_can.h"
  3. #include "string.h"
  4. #include "math.h"
  5. #include "warn.h"
  6. #include "my_math.h"
  7. static void Error_Led(FORMAT_CAN2PMU *msg)
  8. {
  9. if (msg->WorkStatus == GOWRONG)
  10. {
  11. HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET);
  12. }
  13. else
  14. {
  15. HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_SET);
  16. }
  17. }
  18. static bool Check_Err(FORMAT_CAN2PMU *msg)
  19. {
  20. WEIGHING_DEVICE *device = Get_Device_Handle();
  21. for (uint8_t sensor_num_c = 0; sensor_num_c < SENSOR_NUM; sensor_num_c++)
  22. {
  23. if (sensor_num_c == device->sensor_num_mask)
  24. {
  25. continue;
  26. }
  27. if (device->sensor[sensor_num_c]->err_flag)
  28. {
  29. msg->WorkStatus = GOWRONG;
  30. msg->AlarmStatus = ANOMALY;
  31. return true;
  32. }
  33. }
  34. msg->WorkStatus = NORMAL;
  35. msg->AlarmStatus = NO_ERR;
  36. return false;
  37. }
  38. static void Warn_Check(WEIGHING_DEVICE *device, FORMAT_CAN2PMU *msg)
  39. {
  40. Check_Err(msg);
  41. device->Weight_current = device->_ops->get_weight();
  42. if (device->Weight_last > 1e-6f && msg->AlarmStatus != ANOMALY)
  43. {
  44. /*rate*/
  45. if (device->Weight_current > RUNNING_THRESHOLD_VALUE)
  46. {
  47. memcpy(&msg->WeightRate, &device->rate, sizeof(device->rate));
  48. }
  49. /*小于一定值不计算rate*/
  50. if (device->Weight_last < RATE_THRESHOLD_VALUE)
  51. {
  52. msg->WeightRate = 0;
  53. }
  54. }
  55. Error_Led(msg); // 出现异常亮灯
  56. // for(uint8_t sensor_num_c = 0; sensor_num_c < SENSOR_NUM; sensor_num_c++)
  57. // {
  58. // if(sensor_num_c == device->sensor_num_mask)
  59. // {
  60. // continue;
  61. // }
  62. // if(device->sensor[sensor_num_c]->err_flag)
  63. // {
  64. // Weight_curt -= device->sensor[sensor_num_c]->Real_Weight;
  65. // }
  66. // }
  67. }
  68. void Get_Sensor_Status(void)
  69. {
  70. WEIGHING_DEVICE *device = Get_Device_Handle();
  71. FORMAT_CAN2PMU *msg = Get_Tx_Msg();
  72. if (device->check_self_flag == false)
  73. {
  74. msg->WorkStatus = HAVENOTCHECKED;
  75. }
  76. if (msg->AlarmStatus >= ANOMALY)
  77. {
  78. msg->WorkStatus = GOWRONG;
  79. }
  80. Warn_Check(device, msg);
  81. }
  82. /*截止频率(hz),周期时间(s)*/
  83. float apply(float sample, float sample_last, float cutoff_freq, float dt)
  84. {
  85. float out_put = 0.0f;
  86. float rc = 0.0f;
  87. float alpha = 0.0f;
  88. if (cutoff_freq <= 0.0f || dt <= 0.0f)
  89. {
  90. out_put = sample;
  91. return out_put;
  92. }
  93. rc = 1.0f / (M_2PI_F * cutoff_freq); /*计算rc常数*/
  94. alpha = constrain_float(dt / (dt + rc), 0.0f, 1.0f);
  95. out_put = sample_last + (sample - sample_last) * alpha;
  96. return out_put;
  97. }