samples.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. typedef struct {
  8. float value;
  9. float filted_value;
  10. float lowpass;
  11. s16 fix_value;
  12. }samples_t;
  13. static void sample_vbus(void);
  14. static void sample_throttle(void);
  15. static u32 sample_task(void *);
  16. static samples_t _vbus;
  17. static samples_t _throttle;
  18. void samples_init(void){
  19. _vbus.filted_value = (MAX_VBUS_VOLTAGE);
  20. _vbus.value = (MAX_VBUS_VOLTAGE);
  21. _vbus.lowpass = (0.2f);
  22. _throttle.filted_value = (0);
  23. _throttle.value = (0);
  24. _throttle.lowpass = (0.2f);
  25. sample_throttle();
  26. sample_vbus();
  27. shark_task_create(sample_task, NULL);
  28. }
  29. float get_vbus_float(void) {
  30. return (float)((s32)(_vbus.filted_value * 100.0f)/100.0f);
  31. }
  32. s16 get_vbus_sfix5(void){
  33. return _vbus.fix_value;
  34. }
  35. float get_throttle_float(void) {
  36. return 0.0f;//(float)((s32)(_throttle.filted_value * 100.0f)/100.0f);
  37. }
  38. static u32 sample_task(void *param) {
  39. sample_vbus();
  40. sample_throttle();
  41. return 0;
  42. }
  43. static void sample_vbus(void){
  44. s32 vadc = adc_sample_regular_channel(VBUS_V_CHAN, 16);
  45. float fix_vbus = ((float)vadc * ADC_REFERENCE_VOLTAGE * 45.0F / 4096.0F);//1:44
  46. LowPass_Filter(_vbus.filted_value, (fix_vbus), _vbus.lowpass);
  47. _vbus.fix_value = S16Q5toF(_vbus.filted_value);
  48. }
  49. static void sample_throttle(void){
  50. s32 vadc = adc_sample_regular_channel(THROTTLE_CHAN, 16);
  51. float fix_V = ((float)vadc * ADC_REFERENCE_VOLTAGE * 45.0F / 4096.0F);//1:44
  52. LowPass_Filter(_throttle.filted_value, (fix_V), _throttle.lowpass);
  53. _throttle.fix_value = S16Q5toF(_throttle.filted_value);
  54. }