commands.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 process_foc_command(foc_cmd_body_t *command) {
  35. u8 erroCode = 0;
  36. sys_debug("command %d\n", command->cmd);
  37. switch (command->cmd) {
  38. case Foc_Start_Motor:
  39. {
  40. bool success;
  41. foc_start_cmd_t *scmd = (foc_start_cmd_t *)command->data;
  42. if (scmd->start_stop == Foc_Start) {
  43. success = mc_start(TRQ_MODE);
  44. }else if (scmd->start_stop == Foc_Stop) {
  45. success = mc_stop();
  46. }
  47. if (!success) {
  48. erroCode = PMSM_FOC_GetErrCode();
  49. }
  50. sys_debug("start motor %d\n", erroCode);
  51. break;
  52. }
  53. case Foc_Set_Cruise_Mode:
  54. {
  55. u8 enable = decode_u8(command->data);
  56. if (!PMSM_FOC_EnableCruise(enable)) {
  57. erroCode = PMSM_FOC_GetErrCode();
  58. }
  59. break;
  60. }
  61. case Foc_Set_Speed_Limit:
  62. {
  63. s16 speed = decode_s16(((u8 *)command->data));
  64. PMSM_FOC_SpeedLimit(speed);
  65. }
  66. case Foc_Set_Current_Limit:
  67. {
  68. s16 current = decode_s16(((u8 *)command->data));
  69. PMSM_FOC_iBusLimit(current);
  70. }
  71. case Foc_Cali_Hall_Phase:
  72. {
  73. PMSM_FOC_HallCalibrate(true, decode_s32((u8 *)command->data));
  74. break;
  75. }
  76. case Foc_Set_Open_Dq_Vol:
  77. {
  78. s32 vq = decode_s32(((u8 *)command->data) + 4);
  79. sys_debug("set v_q %d\n", vq);
  80. PMSM_FOC_SetOpenVdq(0, vq);
  81. break;
  82. }
  83. default:
  84. {
  85. erroCode = FOC_Unknow_Cmd;
  86. break;
  87. }
  88. }
  89. can_send_ack(command->can_src, CMD_2_CAN_KEY(command->cmd), (u8)erroCode);
  90. }