commands.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. #if 0
  40. static void conf_foc_pid(u8 id, pid_conf_t *pid) {
  41. nv_set_pid(id, pid);
  42. }
  43. #endif
  44. static void process_foc_command(foc_cmd_body_t *command) {
  45. u8 erroCode = 0;
  46. u8 response[32];
  47. int len = 3;
  48. sys_debug("command %d\n", command->cmd);
  49. switch (command->cmd) {
  50. case Foc_Start_Motor:
  51. {
  52. bool success;
  53. foc_start_cmd_t *scmd = (foc_start_cmd_t *)command->data;
  54. sys_debug("start cmd %d\n", scmd->start_stop);
  55. if (scmd->start_stop == Foc_Start) {
  56. success = mc_start(CTRL_MODE_TRQ);
  57. }else if (scmd->start_stop == Foc_Stop) {
  58. success = mc_stop();
  59. }
  60. if (!success) {
  61. erroCode = PMSM_FOC_GetErrCode();
  62. }
  63. sys_debug("start motor %d\n", erroCode);
  64. break;
  65. }
  66. case Foc_Set_Cruise_Mode:
  67. {
  68. u8 enable = decode_u8(command->data);
  69. if (!PMSM_FOC_EnableCruise(enable)) {
  70. erroCode = PMSM_FOC_GetErrCode();
  71. }
  72. break;
  73. }
  74. case Foc_Set_Cruise_Speed:
  75. {
  76. u8 mode = decode_u8(command->data);
  77. float rpm = (float)decode_s16((u8 *)command->data + 1);
  78. if (mode == 0) {
  79. rpm = PMSM_FOC_GetSpeed() + rpm;
  80. }
  81. if (!PMSM_FOC_Set_CruiseSpeed(rpm)) {
  82. erroCode = PMSM_FOC_GetErrCode();
  83. }
  84. sys_debug("Cruise RPM %d\n", (int)rpm);
  85. encode_u16(response + 3, (s16)rpm);
  86. len += 2;
  87. break;
  88. }
  89. case Foc_Set_Ctrl_Mode:
  90. {
  91. u8 mode = decode_u8(command->data);
  92. sys_debug("mode = %d\n", mode);
  93. if (!mc_set_foc_mode(mode)) {
  94. erroCode = PMSM_FOC_GetErrCode();
  95. }
  96. response[len++] = PMSM_FOC_GetCtrlMode();
  97. break;
  98. }
  99. case Foc_Set_Gear_Limit:
  100. {
  101. if (command->len < 5) {
  102. erroCode = FOC_Param_Err;
  103. }else {
  104. u16 maxRPM = decode_u16(command->data);
  105. u16 maxPhaseCurr = decode_u8((u8 *)command->data + 2);
  106. u8 maxiDC = decode_u8((u8 *)command->data + 4);
  107. PMSM_FOC_SpeedLimit((float)maxRPM);
  108. PMSM_FOC_PhaseCurrLim((float)maxPhaseCurr);
  109. PMSM_FOC_iBusLimit((float)maxiDC);
  110. encode_u16(response + 3, (u16)PMSM_FOC_GetSpeedLimit());
  111. encode_u16(response + 5, (u16)PMSM_FOC_GetPhaseCurrLim());
  112. encode_u8(response + 7, (u8)PMSM_FOC_GetiBusLimit());
  113. len += 5;
  114. }
  115. break;
  116. }
  117. case Foc_Set_Speed_Limit:
  118. {
  119. s16 speed = decode_s16(((u8 *)command->data));
  120. PMSM_FOC_SpeedLimit(speed);
  121. encode_u16(response + 3, (u16)PMSM_FOC_GetSpeedLimit());
  122. len += 2;
  123. break;
  124. }
  125. case Foc_Set_iDC_Limit:
  126. {
  127. u8 current = decode_u8(((u8 *)command->data));
  128. PMSM_FOC_iBusLimit((float)current);
  129. encode_u8(response + 3, (u8)PMSM_FOC_GetiBusLimit());
  130. len += 1;
  131. break;
  132. }
  133. case Foc_Set_Phase_Current:
  134. {
  135. s16 curr = decode_s16(((u8 *)command->data));
  136. PMSM_FOC_PhaseCurrLim((float)curr);
  137. encode_u16(response + 3, (u16)PMSM_FOC_GetPhaseCurrLim());
  138. len += 2;
  139. break;
  140. }
  141. case Foc_Cali_Hall_Phase:
  142. {
  143. s16 vd = decode_s16((u8 *)command->data);
  144. sys_debug("cali encoder %d\n", vd);
  145. mc_encoder_off_calibrate((vd));
  146. break;
  147. }
  148. case Foc_Set_Open_Dq_Vol:
  149. {
  150. s16 vd = decode_s16(((u8 *)command->data));
  151. s16 vq = decode_s16(((u8 *)command->data) + 2);
  152. sys_debug("set v_q %d, %d\n", vd, vq);
  153. PMSM_FOC_SetOpenVdq(vd, (vq));
  154. break;
  155. }
  156. case Foc_Conf_Pid:
  157. {
  158. pid_conf_t pid;
  159. u8 id = decode_u8((u8 *)command->data);
  160. memcpy((char *)&pid, (char *)command->data + 1, sizeof(pid_conf_t));
  161. sys_debug("id = %d, kp = %f, ki = %f, kb = %f\n", id, pid.kp, pid.ki, pid.kb);
  162. PMSM_FOC_SetPid(id, pid.kp, pid.ki, pid.kb);
  163. //conf_foc_pid(id, &pid);
  164. break;
  165. }
  166. case Foc_Set_EPM_Mode:
  167. {
  168. bool mode = decode_u8((u8 *)command->data) == 0?false:true;
  169. if (!PMSM_FOC_Set_epmMode(mode)) {
  170. erroCode = PMSM_FOC_GetErrCode();
  171. }
  172. break;
  173. }
  174. case Foc_Start_EPM_Move:
  175. {
  176. bool move = decode_u8((u8 *)command->data) == 0?false:true;
  177. EPM_Dir_t dir = (EPM_Dir_t)decode_u8((u8 *)command->data + 1);
  178. if(!PMSM_FOC_Start_epmMove(move, dir)) {
  179. erroCode = PMSM_FOC_GetErrCode();
  180. }
  181. break;
  182. }
  183. default:
  184. {
  185. erroCode = FOC_Unknow_Cmd;
  186. break;
  187. }
  188. }
  189. response[0] = command->cmd;
  190. response[1] = CAN_MY_ADDRESS;
  191. response[2] = erroCode;
  192. can_send_response(command->can_src, response, len);
  193. }