samples.c 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #include "foc/samples.h"
  2. #include "bsp/bsp.h"
  3. #include "bsp/adc.h"
  4. #include "math/fast_math.h"
  5. #include "os/os_task.h"
  6. #include "math/fix_math.h"
  7. typedef struct {
  8. sfix6_t value;
  9. sfix6_t filted_value;
  10. sfix10_t lowpass;
  11. }samples_t;
  12. static void sample_vbus(void);
  13. static void sample_timer_handler(shark_timer_t *);
  14. static samples_t _vbus;
  15. static shark_timer_t sample_timer = TIMER_INIT(sample_timer, sample_timer_handler);
  16. void samples_init(void){
  17. _vbus.filted_value = MAX_VBUS_VOLTAGE;
  18. _vbus.value = MAX_VBUS_VOLTAGE;
  19. _vbus.lowpass = _F2sFix10(0.2f);
  20. sample_vbus();
  21. shark_timer_post(&sample_timer, 1);
  22. }
  23. s16 get_vbus_sfix6(void) {
  24. return _vbus.filted_value;
  25. }
  26. float get_vbus_float(void) {
  27. return sfix6toF(_vbus.filted_value);
  28. }
  29. static void sample_timer_handler(shark_timer_t *timer) {
  30. sample_vbus();
  31. shark_timer_post(&sample_timer, 1);
  32. }
  33. static void sample_vbus(void){
  34. s32 vadc = adc_sample_regular_channel(VBUS_V_CHAN, 16);
  35. s32 fix_vbus = (vadc * ADC_REFERENCE_VOLTAGE * 45 / 4096);//1:44
  36. s32 fix_filter = _vbus.filted_value;
  37. LowPass_Filter(fix_filter, fix_vbus, _vbus.lowpass); //sfix6 * sfix10
  38. _vbus.filted_value = fix_filter >> 10; // to sfix6
  39. }