foc.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. #include <string.h>
  2. #include "libs/task.h"
  3. #include "bsp/bsp.h"
  4. #include "foc/foc.h"
  5. #include "foc/park_clark.h"
  6. #include "foc/svpwm.h"
  7. #include "foc/foc_task.h"
  8. #include "foc/phase_current.h"
  9. #include "foc/hall_sensor.h"
  10. #include "foc/vbus_sensor.h"
  11. #include "foc/ntc_sensor.h"
  12. static u32 foc_stm_task_handler(void);
  13. static u32 foc_measure_task_handler(void);
  14. static void foc_defulat_value(void);
  15. static motor_foc_t m_foc;
  16. void foc_init(void) {
  17. foc_defulat_value();
  18. hall_sensor_init();
  19. HAL_ADC1_Enable();
  20. /* init pwm hardware timer */
  21. PWM_TimerEnable();
  22. task_start(foc_measure_task_handler, 0);
  23. task_start(foc_stm_task_handler, 0);
  24. printf("test\n");
  25. foc_ramp_speed(400.0f, 10.0f);
  26. //foc_hall_detect(3.0f, m_foc.hall_table);
  27. }
  28. static void foc_defulat_value(void){
  29. memset(&m_foc, 0, sizeof(m_foc));
  30. m_foc.state = IDLE;
  31. m_foc.gate_output = false;
  32. m_foc.vbus = 12.0f;
  33. phase_current_init(&m_foc.current_samp);
  34. }
  35. void foc_ramp_speed(float rpm, u32 duration_ms) {
  36. float current = 8.0f;
  37. m_foc.dq_ref.q = 0;
  38. m_foc.dq_ref.d = 0;
  39. //m_foc.override_p.is_override_theta = true;
  40. foc_pwm_start(true);
  41. for (int i = 0;i < 100;i++) {
  42. m_foc.dq_ref.d = (float)i * current / 1000.0f;
  43. task_udelay(10);
  44. }
  45. m_foc.dq_ref.d = 0.0f;
  46. m_foc.dq_ref.q = 4.0f;
  47. while(1) {
  48. for (int i = 0; i < 360; i++) {
  49. m_foc.override_p.theta = i;
  50. task_udelay(100);
  51. int theta = hall_sensor_get_theta();
  52. if (i % 10 == 0) {
  53. printf("$%d;", theta);
  54. }
  55. }
  56. }
  57. }
  58. int foc_hall_detect(float current, u16 *hall_table){
  59. foc_pwm_start(true);
  60. m_foc.override_p.is_override_theta = true;
  61. m_foc.override_p.theta = 0.0f;
  62. m_foc.override_p.is_override_v_dq = true;
  63. m_foc.override_p.v_dq.d = .0f;
  64. m_foc.override_p.v_dq.q = .0f;
  65. for (int i = 0;i < 1000;i++) {
  66. m_foc.override_p.v_dq.d = (float)i * current / 1000.0f;
  67. task_udelay(1000);
  68. }
  69. float sin_hall[8];
  70. float cos_hall[8];
  71. int hall_iterations[8];
  72. memset(sin_hall, 0, sizeof(sin_hall));
  73. memset(cos_hall, 0, sizeof(cos_hall));
  74. memset(hall_iterations, 0, sizeof(hall_iterations));
  75. // Forwards
  76. for (int i = 0;i < 3;i++) {
  77. for (int j = 0;j < 360;j++) {
  78. m_foc.override_p.theta = j;
  79. task_udelay(10 * 1000);
  80. int hall = get_hall_stat(7);
  81. float s, c;
  82. normal_sincosf(degree_2_pi(j), &s, &c);
  83. sin_hall[hall] += s;
  84. cos_hall[hall] += c;
  85. hall_iterations[hall]++;
  86. }
  87. }
  88. // Reverse
  89. for (int i = 0;i < 3;i++) {
  90. for (int j = 360;j >= 0;j--) {
  91. m_foc.override_p.theta = j;
  92. task_udelay(10 * 1000);
  93. int hall = get_hall_stat(7);
  94. float s, c;
  95. normal_sincosf(degree_2_pi(j), &s, &c);
  96. sin_hall[hall] += s;
  97. cos_hall[hall] += c;
  98. hall_iterations[hall]++;
  99. }
  100. }
  101. foc_pwm_start(false);
  102. m_foc.override_p.is_override_theta = false;
  103. m_foc.override_p.is_override_v_dq = false;
  104. int fails = 0;
  105. for(int i = 0;i < 8;i++) {
  106. if (hall_iterations[i] > 30) {
  107. float ang = pi_2_degree(atan2f(sin_hall[i], cos_hall[i]));
  108. fast_norm_angle(&ang);
  109. hall_table[i] = (u16)ang;
  110. } else {
  111. hall_table[i] = 0xFFFF;
  112. fails++;
  113. }
  114. }
  115. return fails == 2;
  116. }
  117. void foc_clear(void) {
  118. foc_defulat_value();
  119. PWM_Stop();
  120. m_foc.gate_output = false;
  121. }
  122. static u32 foc_measure_task_handler(void){
  123. vbus_sample_voltage();
  124. ntc_sensor_sample();
  125. return 1;
  126. }
  127. static u32 foc_stm_task_handler(void) {
  128. switch (m_foc.state) {
  129. case START:
  130. break;
  131. case CHARGER_BOOT_CAP:
  132. break;
  133. case READY_TO_RUN:
  134. break;
  135. default:
  136. break;
  137. }
  138. return 10;
  139. }
  140. void foc_brake_handler(void) {
  141. }
  142. void foc_pwm_up_handler(void){
  143. phase_current_adc_triger(&m_foc.current_samp);
  144. }
  145. void current_sample_handler(void) {
  146. foc_task(&m_foc);
  147. }
  148. void foc_pwm_start(bool start) {
  149. if (start == m_foc.gate_output) {
  150. return;
  151. }
  152. if (start) {
  153. PWM_Start();
  154. }else {
  155. PWM_Stop();
  156. }
  157. m_foc.gate_output = start;
  158. }