foc_cmd.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #include "os/co_task.h"
  2. #include "os/queue.h"
  3. #include "libs/logger.h"
  4. #include "libs/utils.h"
  5. #include "prot/can_message.h"
  6. #include "bsp/bsp.h"
  7. #include "bsp/pwm.h"
  8. #include "bsp/adc.h"
  9. #include "foc/foc_api.h"
  10. #include "foc/foc_core.h"
  11. #include "foc/hall_sensor.h"
  12. #include "foc/foc_cmd.h"
  13. extern motor_foc_t g_foc;
  14. static void foc_cmd_task(void *args);
  15. static void process_foc_command(foc_cmd_body_t *command);
  16. static co_queue_t _cmd_queue;
  17. void foc_command_init(void) {
  18. _cmd_queue = queue_create(16, sizeof(foc_cmd_body_t));
  19. co_task_create(foc_cmd_task, NULL, 512);
  20. }
  21. bool foc_send_command(foc_cmd_body_t *command) {
  22. return queue_put(_cmd_queue, command);
  23. }
  24. static void foc_cmd_task(void *args) {
  25. foc_cmd_body_t command;
  26. while(1) {
  27. if (queue_get(_cmd_queue, &command)) {
  28. process_foc_command(&command);
  29. if (command.data) {
  30. co_free(command.data);
  31. }
  32. }
  33. co_task_yield();
  34. }
  35. }
  36. static void do_hall_calibrate(u8 can_addr, float vq) {
  37. sys_debug("cali hall phase, %d\n", g_foc.state);
  38. if (g_foc.state == IDLE) {
  39. can_send_ack(can_addr, CMD_2_CAN_KEY(Foc_Cali_Hall_Phase), 1);
  40. pwm_turn_on_low_side();
  41. delay_ms(10);
  42. foc_pwm_start(true);
  43. pwm_enable_channel();
  44. adc_start_insert_convert();
  45. int result = hall_sensor_calibrate(vq, NULL);
  46. sys_debug("hall phase cali %d\n", result);
  47. can_send_ack(can_addr, CMD_2_CAN_KEY(Foc_Hall_Phase_Cali_Result), result);
  48. }else {
  49. can_send_ack(can_addr, CMD_2_CAN_KEY(Foc_Cali_Hall_Phase), 0);
  50. }
  51. }
  52. static void process_foc_command(foc_cmd_body_t *command) {
  53. sys_debug("command %d\n", command->cmd);
  54. switch (command->cmd) {
  55. case Foc_Start_Motor:
  56. {
  57. foc_fault_t f;
  58. foc_start_stop_t start = (foc_start_stop_t)decode_u8((u8 *)command->data);
  59. if (start == Foc_Start) {
  60. f = foc_start_motor();
  61. }else if (start == Foc_Stop) {
  62. f = foc_stop_motor();
  63. }
  64. can_send_ack(command->can_src, CMD_2_CAN_KEY(Foc_Start_Motor), (u8)f);
  65. break;
  66. }
  67. case Foc_Cali_Hall_Phase:
  68. do_hall_calibrate(command->can_src, decode_u32((u8 *)command->data));
  69. break;
  70. case Foc_Cali_Hall_Offset:
  71. {
  72. int offset = 0;
  73. u8 inc = decode_u8((u8 *)command->data);
  74. if (inc) {
  75. offset = hall_offset_increase(1);
  76. }else {
  77. offset = hall_offset_increase(-1);
  78. }
  79. sys_debug("cali hall offset %d\n", offset);
  80. can_send_ack(command->can_src, CMD_2_CAN_KEY(Foc_Cali_Hall_Offset), 1);
  81. break;
  82. }
  83. case Foc_Set_Open_Dq_Vol:
  84. {
  85. s32 v_q = decode_s32(((u8 *)command->data) + 4);
  86. sys_debug("set v_q %d\n", v_q);
  87. foc_set_voltage_ramp(v_q);
  88. break;
  89. }
  90. default:
  91. break;
  92. }
  93. }