samples.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #include "foc/samples.h"
  2. #include "bsp/bsp.h"
  3. #include "bsp/adc.h"
  4. #include "math/fast_math.h"
  5. #include "math/fix_math.h"
  6. #include "os/os_task.h"
  7. #include "foc/foc_config.h"
  8. #include "bsp/delay.h"
  9. typedef struct {
  10. float value;
  11. float filted_value;
  12. int filted_int;
  13. float lowpass;
  14. }samples_t;
  15. static void sample_vbus(void);
  16. static void sample_throttle(void);
  17. static void sample_uvw_phase(void);
  18. static u32 sample_task(void *);
  19. static samples_t _vbus;
  20. #ifdef THROTTLE_CHAN
  21. static samples_t _throttle;
  22. #endif
  23. #ifdef U_VOL_ADC_CHAN
  24. static samples_t _uvw_phase[3];
  25. #endif
  26. void samples_init(void){
  27. _vbus.filted_value = (MAX_vDC);
  28. _vbus.value = (MAX_vDC);
  29. _vbus.lowpass = (0.001f);
  30. sample_vbus();
  31. #ifdef THROTTLE_CHAN
  32. _throttle.filted_value = (0);
  33. _throttle.value = (0);
  34. _throttle.lowpass = (0.01f);
  35. sample_throttle();
  36. #endif
  37. #ifdef U_VOL_ADC_CHAN
  38. _uvw_phase[0].value = _uvw_phase[0].filted_value = 0;
  39. _uvw_phase[0].lowpass = 0.01f;
  40. _uvw_phase[1].value = _uvw_phase[1].filted_value = 0;
  41. _uvw_phase[1].lowpass = 0.01f;
  42. _uvw_phase[2].value = _uvw_phase[2].filted_value = 0;
  43. _uvw_phase[2].lowpass = 0.01f;
  44. sample_uvw_phase();
  45. #endif
  46. shark_task_create(sample_task, NULL);
  47. }
  48. void get_phase_vols(float *uvw) {
  49. uvw[0] = _uvw_phase[0].filted_value;
  50. uvw[1] = _uvw_phase[1].filted_value;
  51. uvw[2] = _uvw_phase[2].filted_value;
  52. }
  53. float get_vbus_float(void) {
  54. return (_vbus.filted_value);
  55. }
  56. int get_vbus_int(void){
  57. return _vbus.filted_int;
  58. }
  59. float get_throttle_float(void) {
  60. #ifdef THROTTLE_CHAN
  61. return _throttle.filted_value;
  62. #else
  63. return 0.0f;
  64. #endif
  65. }
  66. static u32 sample_task(void *param) {
  67. sample_vbus();
  68. sample_throttle();
  69. sample_uvw_phase();
  70. return 0;
  71. }
  72. static void sample_vbus(void){
  73. u32 ticks = task_ticks_abs();
  74. s16 vadc = adc_get_vbus();
  75. _vbus.value = (float)vadc * VBUS_VOL_CEOF;
  76. LowPass_Filter(_vbus.filted_value, _vbus.value, _vbus.lowpass);
  77. _vbus.filted_int = (int)_vbus.filted_value;
  78. }
  79. static void sample_throttle(void){
  80. #ifdef THROTTLE_CHAN
  81. s16 vadc = adc_get_throttle();
  82. _throttle.value = (float)vadc * THROTTLE_VOL_CEOF;
  83. LowPass_Filter(_throttle.filted_value, _throttle.value, _throttle.lowpass);
  84. #endif
  85. }
  86. static void sample_uvw_phase(void) {
  87. #ifdef U_VOL_ADC_CHAN
  88. u16 uvw[3];
  89. adc_get_uvw_phaseV(uvw);
  90. _uvw_phase[0].value = (float)uvw[0] * UVW_VOL_CEOF;
  91. LowPass_Filter(_uvw_phase[0].filted_value, _uvw_phase[0].value, _uvw_phase[0].lowpass);
  92. _uvw_phase[1].value = (float)uvw[1] * UVW_VOL_CEOF;
  93. LowPass_Filter(_uvw_phase[1].filted_value, _uvw_phase[1].value, _uvw_phase[1].lowpass);
  94. _uvw_phase[2].value = (float)uvw[2] * UVW_VOL_CEOF;
  95. LowPass_Filter(_uvw_phase[2].filted_value, _uvw_phase[2].value, _uvw_phase[2].lowpass);
  96. #endif
  97. }