foc.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. #include <string.h>
  2. #include "libs/task.h"
  3. #include "hal/pwm.h"
  4. #include "hal/hal.h"
  5. #include "foc/foc.h"
  6. #include "foc/park_clark.h"
  7. #include "foc/svpwm.h"
  8. #include "foc/foc_task.h"
  9. #include "foc/phase_current.h"
  10. static void charge_cap_timer_handler(timer_t *t);
  11. static u32 foc_main_task_handler(void);
  12. static timer_t charge_cap_timer = {.handler = charge_cap_timer_handler};
  13. static motor_foc_t m_foc;
  14. u16 _hall_table[] = {0xFFFF, 292, 47, 1, 180, 227, 113, 0xFFFF};
  15. void foc_init(void) {
  16. /* init pwm hardware timer */
  17. m_foc.state = IDLE;
  18. m_foc.gate_output = false;
  19. memcpy(m_foc.hall_table, _hall_table, sizeof(_hall_table));
  20. PWM_TimerInit();
  21. task_start(foc_main_task_handler, 0);
  22. foc_hall_detect(6.0f, (u16 *)m_foc.hall_table);
  23. }
  24. void foc_motor_spin(dq_t *dq_v, float angle) {
  25. alpha_beta_t alphabeta;
  26. phase_time_t phase_time;
  27. u8 sector = 0xFF;
  28. Rev_Park(dq_v, degree_2_pi(angle), &alphabeta);
  29. svpwm(&alphabeta, MAX_VBUS, FOC_PWM_period/2, &phase_time, &sector);
  30. PWM_UpdateDuty(phase_time.A, phase_time.B, phase_time.C, FOC_PWM_period/2);
  31. }
  32. int foc_hall_detect(float current, u16 *hall_table){
  33. dq_t dq_v = {.d = 0.0f, .q = 0.0f};
  34. foc_start_pwm(true);
  35. for (int i = 0;i < 1000;i++) {
  36. dq_v.d = (float)i * current / 1000.0f;
  37. foc_motor_spin(&dq_v, 0);
  38. task_udelay(1000);
  39. }
  40. float sin_hall[8];
  41. float cos_hall[8];
  42. int hall_iterations[8];
  43. memset(sin_hall, 0, sizeof(sin_hall));
  44. memset(cos_hall, 0, sizeof(cos_hall));
  45. memset(hall_iterations, 0, sizeof(hall_iterations));
  46. // Forwards
  47. for (int i = 0;i < 3;i++) {
  48. for (int j = 0;j < 360;j++) {
  49. foc_motor_spin(&dq_v, j);
  50. task_udelay(10 * 1000);
  51. int hall = HALL_Read(7);
  52. float s, c;
  53. normal_sincosf(degree_2_pi(j), &s, &c);
  54. sin_hall[hall] += s;
  55. cos_hall[hall] += c;
  56. hall_iterations[hall]++;
  57. }
  58. }
  59. // Reverse
  60. for (int i = 0;i < 3;i++) {
  61. for (int j = 360;j >= 0;j--) {
  62. foc_motor_spin(&dq_v, j);
  63. task_udelay(10 * 1000);
  64. int hall = HALL_Read(7);
  65. float s, c;
  66. normal_sincosf(degree_2_pi(j), &s, &c);
  67. sin_hall[hall] += s;
  68. cos_hall[hall] += c;
  69. hall_iterations[hall]++;
  70. }
  71. }
  72. int fails = 0;
  73. for(int i = 0;i < 8;i++) {
  74. if (hall_iterations[i] > 30) {
  75. float ang = pi_2_degree(atan2f(sin_hall[i], cos_hall[i]));
  76. fast_norm_angle(&ang);
  77. hall_table[i] = (u16)ang;
  78. } else {
  79. hall_table[i] = 0xFFFF;
  80. fails++;
  81. }
  82. }
  83. foc_start_pwm(false);
  84. return fails == 2;
  85. }
  86. static void charge_cap_timer_handler(timer_t *t) {
  87. if (m_foc.state == CHARGER_BOOT_CAP) {
  88. m_foc.state = READY_TO_RUN;
  89. }
  90. }
  91. static u32 foc_main_task_handler(void) {
  92. switch (m_foc.state) {
  93. case START:
  94. m_foc.state = CHARGER_BOOT_CAP;
  95. timer_post(&charge_cap_timer, 10);
  96. PWM_TurnOnLowSides();
  97. break;
  98. case CHARGER_BOOT_CAP:
  99. break;
  100. case READY_TO_RUN:
  101. break;
  102. default:
  103. break;
  104. }
  105. return 1;
  106. }
  107. void foc_brake_handler(void) {
  108. if (m_foc.state == CHARGER_BOOT_CAP) {
  109. timer_post(&charge_cap_timer, 10);
  110. PWM_TurnOnLowSides();
  111. }
  112. }
  113. void foc_pwm_up_handler(void){
  114. phase_current_adc_triger(&m_foc.current_samp);
  115. }
  116. void hall_detect_handler(void) {
  117. }
  118. void current_sample_handler(void) {
  119. foc_task(&m_foc);
  120. }
  121. void foc_start_pwm(bool start) {
  122. if (start != m_foc.gate_output) {
  123. if (start) {
  124. PWM_Start();
  125. }else {
  126. PWM_Stop();
  127. }
  128. m_foc.gate_output = start;
  129. }
  130. }