commands.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. pid += 1;
  43. PMSM_FOC_SetFW_I((float)pid->ki/pid->gain, (float)pid->kb/pid->gain);
  44. }
  45. static void process_foc_command(foc_cmd_body_t *command) {
  46. u8 erroCode = 0;
  47. sys_debug("command %d\n", command->cmd);
  48. switch (command->cmd) {
  49. case Foc_Start_Motor:
  50. {
  51. bool success;
  52. foc_start_cmd_t *scmd = (foc_start_cmd_t *)command->data;
  53. if (scmd->start_stop == Foc_Start) {
  54. success = mc_start(TRQ_MODE);
  55. }else if (scmd->start_stop == Foc_Stop) {
  56. success = mc_stop();
  57. }
  58. if (!success) {
  59. erroCode = PMSM_FOC_GetErrCode();
  60. }
  61. sys_debug("start motor %d\n", erroCode);
  62. break;
  63. }
  64. case Foc_Set_Cruise_Mode:
  65. {
  66. u8 enable = decode_u8(command->data);
  67. if (!PMSM_FOC_EnableCruise(enable)) {
  68. erroCode = PMSM_FOC_GetErrCode();
  69. }
  70. break;
  71. }
  72. case Foc_Set_Speed_Limit:
  73. {
  74. s16 speed = decode_s16(((u8 *)command->data));
  75. PMSM_FOC_SpeedLimit(speed);
  76. }
  77. case Foc_Set_Current_Limit:
  78. {
  79. s16 current = decode_s16(((u8 *)command->data));
  80. PMSM_FOC_iBusLimit(current);
  81. }
  82. case Foc_Cali_Hall_Phase:
  83. {
  84. PMSM_FOC_HallCalibrate(true, decode_s32((u8 *)command->data));
  85. break;
  86. }
  87. case Foc_Cali_Hall_Offset:
  88. {
  89. mc_hall_calibrate(decode_s16((u8 *)command->data));
  90. break;
  91. }
  92. case Foc_Set_Open_Dq_Vol:
  93. {
  94. s32 vq = decode_s32(((u8 *)command->data) + 4);
  95. sys_debug("set v_q %d\n", vq);
  96. PMSM_FOC_SetOpenVdq(0, vq);
  97. break;
  98. }
  99. case Foc_Conf_Pid:
  100. {
  101. pid_conf_t pid[5];
  102. memcpy((char *)pid, (char *)command->data, 5 * sizeof(pid_conf_t));
  103. conf_foc_pid(pid);
  104. }
  105. default:
  106. {
  107. erroCode = FOC_Unknow_Cmd;
  108. break;
  109. }
  110. }
  111. can_send_ack(command->can_src, CMD_2_CAN_KEY(command->cmd), (u8)erroCode);
  112. }