samples.c 2.6 KB

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