commands.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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/motor/motor.h"
  10. #include "foc/commands.h"
  11. #include "prot/can_foc_msg.h"
  12. #include "app/nv_storage.h"
  13. static u32 foc_command_task(void *args);
  14. static void process_foc_command(foc_cmd_body_t *command);
  15. static co_queue_t _cmd_queue;
  16. void foc_command_init(void) {
  17. _cmd_queue = queue_create(16, sizeof(foc_cmd_body_t));
  18. shark_task_create(foc_command_task, NULL);
  19. }
  20. bool foc_send_command(foc_cmd_body_t *command) {
  21. return queue_put(_cmd_queue, command);
  22. }
  23. static u32 foc_command_task(void *args) {
  24. foc_cmd_body_t command;
  25. if (queue_get(_cmd_queue, &command)) {
  26. process_foc_command(&command);
  27. if (command.data) {
  28. os_free(command.data);
  29. }
  30. }
  31. return 0;
  32. }
  33. static void conf_foc_pid(pid_conf_t *pid) {
  34. }
  35. static void process_foc_command(foc_cmd_body_t *command) {
  36. u8 erroCode = 0;
  37. sys_debug("command %d\n", command->cmd);
  38. switch (command->cmd) {
  39. case Foc_Start_Motor:
  40. {
  41. bool success;
  42. foc_start_cmd_t *scmd = (foc_start_cmd_t *)command->data;
  43. if (scmd->start_stop == Foc_Start) {
  44. success = mc_start(CTRL_MODE_OPEN);
  45. }else if (scmd->start_stop == Foc_Stop) {
  46. success = mc_stop();
  47. }
  48. if (!success) {
  49. erroCode = PMSM_FOC_GetErrCode();
  50. }
  51. sys_debug("start motor %d\n", erroCode);
  52. break;
  53. }
  54. case Foc_Set_Cruise_Mode:
  55. {
  56. u8 enable = decode_u8(command->data);
  57. if (!PMSM_FOC_EnableCruise(enable)) {
  58. erroCode = PMSM_FOC_GetErrCode();
  59. }
  60. break;
  61. }
  62. case Foc_Set_Speed_Limit:
  63. {
  64. s16 speed = decode_s16(((u8 *)command->data));
  65. PMSM_FOC_SpeedLimit(speed);
  66. }
  67. case Foc_Set_Current_Limit:
  68. {
  69. s16 current = decode_s16(((u8 *)command->data));
  70. PMSM_FOC_iBusLimit(current);
  71. }
  72. case Foc_Cali_Hall_Phase:
  73. {
  74. s16 vd = decode_s16((u8 *)command->data);
  75. mc_hall_calibrate((vd));
  76. break;
  77. }
  78. case Foc_Set_Open_Dq_Vol:
  79. {
  80. s16 vq = decode_s16(((u8 *)command->data) + 2);
  81. sys_debug("set v_q %d\n", vq);
  82. PMSM_FOC_SetOpenVdq((0), (vq));
  83. break;
  84. }
  85. case Foc_Conf_Pid:
  86. {
  87. pid_conf_t pid[5];
  88. memcpy((char *)pid, (char *)command->data, 5 * sizeof(pid_conf_t));
  89. conf_foc_pid(pid);
  90. }
  91. default:
  92. {
  93. erroCode = FOC_Unknow_Cmd;
  94. break;
  95. }
  96. }
  97. can_send_ack(command->can_src, CMD_2_CAN_KEY(command->cmd), (u8)erroCode);
  98. }