weight_read.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #ifndef __WEIGHT_READ_H
  2. #define __WEIGHT_READ_H
  3. #include "gpio.h"
  4. #include "tim.h"
  5. #include "stdbool.h"
  6. #include "stdlib.h"
  7. #define ROUND_OFF 0.5f
  8. #define ALLOW_VALUE 15.0f
  9. #define VALUE_MID 2
  10. #define N 9
  11. #define MAX_ZERO 10.0f
  12. #define MIN_ZERO -5.0f
  13. #define SENSOR_NUM 3
  14. #define SAMPLE_NUM 5
  15. #define HALF_HOUR 1800000
  16. #define CREEP_VALUE 1.2f
  17. #define BASE_VALUE (uint32_t)0x800000
  18. #define OFF_VALUE (uint32_t)0x3000
  19. typedef union
  20. {
  21. float f;
  22. uint8_t i[4];
  23. } F32;
  24. typedef enum
  25. {
  26. higher = 0,
  27. equal = 1,
  28. lower = 2,
  29. } WARN_INIT;
  30. struct SENSOR
  31. {
  32. GPIO_TypeDef *SCK_GPIO_Port;
  33. GPIO_TypeDef *DOUT_GPIO_Port;
  34. uint16_t SCK_Pin;
  35. uint16_t DOUT_Pin;
  36. uint32_t GrossWeight;
  37. uint32_t Raw_Value;
  38. float Real_Variation;
  39. float Real_Weight;
  40. F32 K;
  41. float base_k;
  42. float Scale;
  43. uint32_t raw_init_value;
  44. WARN_INIT init_flag;
  45. bool licence_flag;
  46. bool err_flag;
  47. uint8_t Num;
  48. };
  49. // typedef struct __WEIGHING_DEVICE WEIGHING_DEVICE;
  50. typedef struct
  51. {
  52. float (*get_weight)(void);
  53. void (*get_allgrossweight)(void);
  54. void (*processing_data)(void);
  55. void (*filter_init)(void);
  56. } WEIGHING_OPS;
  57. typedef struct
  58. {
  59. struct SENSOR *sensor[SENSOR_NUM];
  60. // uint32_t creep_time;
  61. // float creep_snap;
  62. float Weight_last;
  63. float Weight_current;
  64. uint16_t rate;
  65. // uint8_t creep_count;
  66. bool check_self_flag;
  67. uint8_t sensor_num_mask;
  68. float correct_k;
  69. WEIGHING_OPS *_ops;
  70. } WEIGHING_DEVICE;
  71. WEIGHING_DEVICE *Get_Device_Handle(void);
  72. void Filter_Value(uint32_t* data);
  73. void Convert_Into_Weight(void);
  74. uint32_t Read_Value(struct SENSOR *sensor);
  75. static inline float constrain_float(float x, float low, float high)
  76. {
  77. if (x < low)
  78. return low;
  79. else if (x > high)
  80. return high;
  81. else
  82. return x;
  83. }
  84. #define Min(a, b) \
  85. ({ \
  86. typeof(a) _min1 = a; \
  87. typeof(b) _min2 = b; \
  88. (void)(&_min1 == &_min2); \
  89. _min1 < _min2 ? _min1 : _min2; \
  90. })
  91. #define Max(a, b) \
  92. ({ \
  93. typeof(a) _max1 = a; \
  94. typeof(b) _max2 = b; \
  95. (void)(&_max1 == &_max2); \
  96. _max1 > _max2 ? _max1 : _max2; \
  97. })
  98. #endif