commands.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 conf_foc_pid(pid_conf_t *pid) {
  35. PMSM_FOC_SetIDPid((float)pid->kp/pid->gain, (float)pid->ki/pid->gain, (float)pid->kb/pid->gain);
  36. pid += 1;
  37. PMSM_FOC_SetIQPid((float)pid->kp/pid->gain, (float)pid->ki/pid->gain, (float)pid->kb/pid->gain);
  38. pid += 1;
  39. PMSM_FOC_SetSpdPid((float)pid->kp/pid->gain, (float)pid->ki/pid->gain, (float)pid->kb/pid->gain);
  40. pid += 1;
  41. PMSM_FOC_SetTrqPid((float)pid->kp/pid->gain, (float)pid->ki/pid->gain, (float)pid->kb/pid->gain);
  42. }
  43. static void process_foc_command(foc_cmd_body_t *command) {
  44. u8 erroCode = 0;
  45. sys_debug("command %d\n", command->cmd);
  46. switch (command->cmd) {
  47. case Foc_Start_Motor:
  48. {
  49. bool success;
  50. foc_start_cmd_t *scmd = (foc_start_cmd_t *)command->data;
  51. if (scmd->start_stop == Foc_Start) {
  52. success = mc_start(TRQ_MODE);
  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_Current_Limit:
  76. {
  77. s16 current = decode_s16(((u8 *)command->data));
  78. PMSM_FOC_iBusLimit(current);
  79. }
  80. case Foc_Cali_Hall_Phase:
  81. {
  82. PMSM_FOC_HallCalibrate(true, decode_s32((u8 *)command->data));
  83. break;
  84. }
  85. case Foc_Set_Open_Dq_Vol:
  86. {
  87. s32 vq = decode_s32(((u8 *)command->data) + 4);
  88. sys_debug("set v_q %d\n", vq);
  89. PMSM_FOC_SetOpenVdq(0, vq);
  90. break;
  91. }
  92. case Foc_Conf_Pid:
  93. {
  94. pid_conf_t pid[4];
  95. memcpy((char *)pid, (char *)command->data, 4 * sizeof(pid_conf_t));
  96. conf_foc_pid(pid);
  97. }
  98. default:
  99. {
  100. erroCode = FOC_Unknow_Cmd;
  101. break;
  102. }
  103. }
  104. can_send_ack(command->can_src, CMD_2_CAN_KEY(command->cmd), (u8)erroCode);
  105. }