commands.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. if (!queue_put(_cmd_queue, command)) {
  22. if (command->data) {
  23. os_free(command->data);
  24. }
  25. return false;
  26. }
  27. return true;
  28. }
  29. static u32 foc_command_task(void *args) {
  30. foc_cmd_body_t command;
  31. if (queue_get(_cmd_queue, &command)) {
  32. process_foc_command(&command);
  33. if (command.data) {
  34. os_free(command.data);
  35. }
  36. }
  37. return 0;
  38. }
  39. static void conf_foc_pid(u8 id, pid_conf_t *pid) {
  40. nv_set_pid(id, pid);
  41. }
  42. static void process_foc_command(foc_cmd_body_t *command) {
  43. u8 erroCode = 0;
  44. sys_debug("command %d\n", command->cmd);
  45. switch (command->cmd) {
  46. case Foc_Start_Motor:
  47. {
  48. bool success;
  49. foc_start_cmd_t *scmd = (foc_start_cmd_t *)command->data;
  50. sys_debug("start cmd %d\n", scmd->start_stop);
  51. if (scmd->start_stop == Foc_Start) {
  52. success = mc_start(CTRL_MODE_TRQ);
  53. }else if (scmd->start_stop == Foc_Stop) {
  54. success = mc_stop();
  55. }
  56. if (!success) {
  57. erroCode = PMSM_FOC_GetErrCode();
  58. }
  59. sys_debug("start motor %d\n", erroCode);
  60. break;
  61. }
  62. case Foc_Set_Cruise_Mode:
  63. {
  64. u8 enable = decode_u8(command->data);
  65. if (!PMSM_FOC_EnableCruise(enable)) {
  66. erroCode = PMSM_FOC_GetErrCode();
  67. }
  68. break;
  69. }
  70. case Foc_Set_Speed_Limit:
  71. {
  72. s16 speed = decode_s16(((u8 *)command->data));
  73. PMSM_FOC_SpeedLimit(speed);
  74. }
  75. case Foc_Set_iDC_Limit:
  76. {
  77. s16 current = decode_s16(((u8 *)command->data));
  78. PMSM_FOC_iBusLimit(current);
  79. }
  80. case Foc_Set_Phase_Current:
  81. {
  82. s16 curr = decode_s16(((u8 *)command->data));
  83. PMSM_FOC_PhaseCurrLim((float)curr);
  84. }
  85. case Foc_Cali_Hall_Phase:
  86. {
  87. s16 vd = decode_s16((u8 *)command->data);
  88. mc_encoder_off_calibrate((vd));
  89. break;
  90. }
  91. case Foc_Set_Open_Dq_Vol:
  92. {
  93. s16 vd = decode_s16(((u8 *)command->data));
  94. s16 vq = decode_s16(((u8 *)command->data) + 2);
  95. sys_debug("set v_q %d\n", vq);
  96. PMSM_FOC_SetOpenVdq(vd, (vq));
  97. break;
  98. }
  99. case Foc_Conf_Pid:
  100. {
  101. pid_conf_t pid;
  102. u8 id = decode_u8((u8 *)command->data);
  103. memcpy((char *)&pid, (char *)command->data + 1, sizeof(pid_conf_t));
  104. conf_foc_pid(id, &pid);
  105. }
  106. case Foc_Start_EPM_Move:
  107. {
  108. bool move = decode_u8((u8 *)command->data) == 0?false:true;
  109. EPM_Dir_t dir = (EPM_Dir_t)decode_u8((u8 *)command->data + 1);
  110. if(!PMSM_FOC_Start_epmMove(move, dir)) {
  111. erroCode = PMSM_FOC_GetErrCode();
  112. }
  113. break;
  114. }
  115. default:
  116. {
  117. erroCode = FOC_Unknow_Cmd;
  118. break;
  119. }
  120. }
  121. //can_send_ack(command->can_src, CMD_2_CAN_KEY(command->cmd), (u8)erroCode);
  122. }