samples.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 u32 sample_task(void *);
  18. static samples_t _vbus;
  19. #ifdef THROTTLE_CHAN
  20. static samples_t _throttle;
  21. #endif
  22. #ifdef U_VOL_ADC_CHAN
  23. static samples_t _uvw_phase[3];
  24. #endif
  25. void samples_init(void){
  26. _vbus.filted_value = (MAX_vDC);
  27. _vbus.value = (MAX_vDC);
  28. _vbus.lowpass = (0.001f);
  29. sample_vbus();
  30. #ifdef THROTTLE_CHAN
  31. _throttle.filted_value = (0);
  32. _throttle.value = (0);
  33. _throttle.lowpass = (0.01f);
  34. sample_throttle();
  35. #endif
  36. #ifdef U_VOL_ADC_CHAN
  37. _uvw_phase[0].value = _uvw_phase[0].filted_value = 0;
  38. _uvw_phase[0].lowpass = 0.01f;
  39. _uvw_phase[1].value = _uvw_phase[1].filted_value = 0;
  40. _uvw_phase[1].lowpass = 0.01f;
  41. _uvw_phase[2].value = _uvw_phase[2].filted_value = 0;
  42. _uvw_phase[2].lowpass = 0.01f;
  43. sample_uvw_phase();
  44. #endif
  45. shark_task_create(sample_task, NULL);
  46. }
  47. void get_phase_vols(float *uvw) {
  48. uvw[0] = _uvw_phase[0].filted_value;
  49. uvw[1] = _uvw_phase[1].filted_value;
  50. uvw[2] = _uvw_phase[2].filted_value;
  51. }
  52. float get_vbus_float(void) {
  53. return (_vbus.filted_value);
  54. }
  55. int get_vbus_int(void){
  56. return _vbus.filted_int;
  57. }
  58. float get_throttle_float(void) {
  59. #ifdef THROTTLE_CHAN
  60. return _throttle.filted_value;
  61. #else
  62. return 0.0f;
  63. #endif
  64. }
  65. static u32 sample_task(void *param) {
  66. sample_vbus();
  67. sample_throttle();
  68. sample_uvw_phase();
  69. return 0;
  70. }
  71. static void sample_vbus(void){
  72. u32 ticks = task_ticks_abs();
  73. s16 vadc = adc_get_vbus();
  74. _vbus.value = (float)vadc * VBUS_VOL_CEOF;
  75. LowPass_Filter(_vbus.filted_value, _vbus.value, _vbus.lowpass);
  76. _vbus.filted_int = (int)_vbus.filted_value;
  77. }
  78. static void sample_throttle(void){
  79. #ifdef THROTTLE_CHAN
  80. s16 vadc = adc_get_throttle();
  81. _throttle.value = (float)vadc * THROTTLE_VOL_CEOF;
  82. LowPass_Filter(_throttle.filted_value, _throttle.value, _throttle.lowpass);
  83. #endif
  84. }
  85. void sample_uvw_phase(void) {
  86. #ifdef U_VOL_ADC_CHAN
  87. u16 uvw[3];
  88. adc_get_uvw_phaseV(uvw);
  89. _uvw_phase[0].value = (float)uvw[0] * UVW_VOL_CEOF;
  90. LowPass_Filter(_uvw_phase[0].filted_value, _uvw_phase[0].value, _uvw_phase[0].lowpass);
  91. _uvw_phase[1].value = (float)uvw[1] * UVW_VOL_CEOF;
  92. LowPass_Filter(_uvw_phase[1].filted_value, _uvw_phase[1].value, _uvw_phase[1].lowpass);
  93. _uvw_phase[2].value = (float)uvw[2] * UVW_VOL_CEOF;
  94. LowPass_Filter(_uvw_phase[2].filted_value, _uvw_phase[2].value, _uvw_phase[2].lowpass);
  95. #endif
  96. }