commands.c 2.7 KB

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