commands.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #include "os/os_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_core.h"
  10. #include "foc/motor/motor.h"
  11. #include "foc/commands.h"
  12. #include "prot/can_foc_msg.h"
  13. #include "app/nv_storage.h"
  14. static u32 foc_command_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. shark_task_create(foc_command_task, NULL);
  20. }
  21. bool foc_send_command(foc_cmd_body_t *command) {
  22. return queue_put(_cmd_queue, command);
  23. }
  24. static u32 foc_command_task(void *args) {
  25. foc_cmd_body_t command;
  26. if (queue_get(_cmd_queue, &command)) {
  27. process_foc_command(&command);
  28. if (command.data) {
  29. os_free(command.data);
  30. }
  31. }
  32. return 0;
  33. }
  34. static void do_hall_calibrate(u8 can_addr, float vd) {
  35. sys_debug("cali hall phase, %f\n", vd);
  36. PMSM_FOC_HallCalibrate(true, vd);
  37. can_send_ack(can_addr, CMD_2_CAN_KEY(Foc_Cali_Hall_Phase), 0);
  38. }
  39. static void process_foc_command(foc_cmd_body_t *command) {
  40. sys_debug("command %d\n", command->cmd);
  41. switch (command->cmd) {
  42. case Foc_Start_Motor:
  43. {
  44. u8 erroCode = 0;
  45. bool success;
  46. foc_start_cmd_t *scmd = (foc_start_cmd_t *)command->data;
  47. if (scmd->start_stop == Foc_Start) {
  48. success = mc_start(TRQ_MODE);
  49. }else if (scmd->start_stop == Foc_Stop) {
  50. success = mc_stop();
  51. }
  52. if (!success) {
  53. erroCode = PMSM_FOC_GetErrCode();
  54. }
  55. sys_debug("start motor %d\n", erroCode);
  56. can_send_ack(command->can_src, CMD_2_CAN_KEY(Foc_Start_Motor), (u8)erroCode);
  57. break;
  58. }
  59. case Foc_Set_Cruise_Mode:
  60. {
  61. u8 enable = decode_u8(command->data);
  62. PMSM_FOC_EnableCruise(enable);
  63. break;
  64. }
  65. case Foc_Cali_Hall_Phase:
  66. {
  67. do_hall_calibrate(command->can_src, decode_s32((u8 *)command->data));
  68. break;
  69. }
  70. case Foc_Set_Open_Dq_Vol:
  71. {
  72. s32 vq = decode_s32(((u8 *)command->data) + 4);
  73. sys_debug("set v_q %d\n", vq);
  74. PMSM_FOC_SetOpenVdq(0, vq);
  75. can_send_ack(command->can_src, CMD_2_CAN_KEY(Foc_Set_Open_Dq_Vol), 1);
  76. break;
  77. }
  78. default:
  79. break;
  80. }
  81. }