foc_cmd.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. #include "prot/can_foc_msg.h"
  14. extern motor_foc_t g_foc;
  15. static void foc_cmd_task(void *args);
  16. static void process_foc_command(foc_cmd_body_t *command);
  17. static co_queue_t _cmd_queue;
  18. void foc_command_init(void) {
  19. _cmd_queue = queue_create(16, sizeof(foc_cmd_body_t));
  20. co_task_create(foc_cmd_task, NULL, 512);
  21. }
  22. bool foc_send_command(foc_cmd_body_t *command) {
  23. return queue_put(_cmd_queue, command);
  24. }
  25. static void foc_cmd_task(void *args) {
  26. foc_cmd_body_t command;
  27. while(1) {
  28. if (queue_get(_cmd_queue, &command)) {
  29. process_foc_command(&command);
  30. if (command.data) {
  31. co_free(command.data);
  32. }
  33. }
  34. co_task_yield();
  35. }
  36. }
  37. static void do_hall_calibrate(u8 can_addr, float vd) {
  38. sys_debug("cali hall phase, %d, %f\n", g_foc.state, vd);
  39. if (g_foc.state == IDLE) {
  40. can_send_ack(can_addr, CMD_2_CAN_KEY(Foc_Cali_Hall_Phase), 1);
  41. pwm_turn_on_low_side();
  42. delay_ms(10);
  43. foc_pwm_start(true);
  44. adc_start_insert_convert();
  45. int result = hall_sensor_calibrate(vd);
  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. sys_debug("start motor %d\n", f);
  65. can_send_ack(command->can_src, CMD_2_CAN_KEY(Foc_Start_Motor), (u8)f);
  66. break;
  67. }
  68. case Foc_Cali_Hall_Phase:
  69. do_hall_calibrate(command->can_src, decode_s32((u8 *)command->data));
  70. break;
  71. case Foc_Cali_Hall_Offset:
  72. {
  73. int offset = 0;
  74. u8 inc = decode_u8((u8 *)command->data);
  75. if (inc) {
  76. offset = hall_offset_increase(1);
  77. }else {
  78. offset = hall_offset_increase(-1);
  79. }
  80. sys_debug("cali hall offset %d\n", offset);
  81. can_response_hall_offset(command->can_src, offset);
  82. break;
  83. }
  84. case Foc_Set_Open_Dq_Vol:
  85. {
  86. s32 v_q = decode_s32(((u8 *)command->data) + 4);
  87. sys_debug("set v_q %d\n", v_q);
  88. foc_set_voltage_ramp(v_q);
  89. can_send_ack(command->can_src, CMD_2_CAN_KEY(Foc_Set_Open_Dq_Vol), 1);
  90. break;
  91. }
  92. default:
  93. break;
  94. }
  95. }