motor.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #include "foc/motor/motor.h"
  2. #include "foc/motor/hall.h"
  3. #include "math/fast_math.h"
  4. #include "bsp/bsp.h"
  5. #include "bsp/adc.h"
  6. #include "bsp/pwm.h"
  7. const motor_param_t motor_params = {
  8. .poles = 5,
  9. .ld = 0.578477f,
  10. .lq = 5.78477f,
  11. .rs = 1.088f,
  12. .inertia = 3.319367f,
  13. .b_emf = 4.332566f,
  14. };
  15. static bool _motor_started = false;
  16. void motor_start() {
  17. if (_motor_started) {
  18. return;
  19. }
  20. pwm_start();
  21. adc_start_convert();
  22. _motor_started = true;
  23. }
  24. void motor_stop() {
  25. if (!_motor_started) {
  26. return;
  27. }
  28. adc_stop_convert();
  29. pwm_stop();
  30. _motor_started = false;
  31. }
  32. void motor_drvier_low_side(bool on){
  33. if (on) {
  34. pwm_turn_on_low_side();
  35. }
  36. }
  37. u32 motor_get_raw_speed(void) {
  38. return (u32)hall_sensor_get_speed();
  39. }
  40. u32 motor_get_speed(void) {
  41. static u32 _speed = 0;
  42. float raw_speed = hall_sensor_get_speed();
  43. if (raw_speed == 0.0f) {
  44. _speed = 0;
  45. }
  46. _speed = LowPass_Filter(_speed, raw_speed, 0.8f);
  47. return _speed;
  48. }
  49. void motor_update_duty(s32 A, s32 B, s32 C, s32 next_a, s32 next_b, s32 next_c) {
  50. #if SHUNT_NUM==THREE_SHUNTS_SAMPLE
  51. pwm_update_duty(A, B, C);
  52. #else
  53. pwm_wait_and_clear_updata();
  54. pwm_update_duty_dma(A, B, C, next_a, next_b, next_c);
  55. #endif
  56. }
  57. void motor_update_sample(u32 samp1, u32 samp2, u8 sector) {
  58. pwm_update_2smaples(samp1, samp2);
  59. #ifdef ENABLE_AUX_TIMER
  60. if (samp2 < FOC_PWM_Half_Period) {
  61. adc_update_ext_trigger(ADC_TRIGGER_PHASE);
  62. }else {
  63. adc_update_ext_trigger(ADC_TRIGGER_PHASE2);
  64. }
  65. #endif
  66. adc_current_sample_config(sector);
  67. }