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. float lowpass;
  13. }samples_t;
  14. static void sample_vbus(void);
  15. static void sample_throttle(void);
  16. static void sample_uvw_phase(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. s16 get_vbus_sfix5(void){
  56. return _vbus.filted_value;
  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. u32 sapmple_delta;
  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. sapmple_delta = task_ticks_rel(ticks);
  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. }