foc.c 3.1 KB

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