commands.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. #ifdef CONFIG_DQ_STEP_RESPONSE
  14. extern float target_d;
  15. extern float target_q;
  16. #endif
  17. static u32 foc_command_task(void *args);
  18. static void process_foc_command(foc_cmd_body_t *command);
  19. static co_queue_t _cmd_queue;
  20. void foc_command_init(void) {
  21. _cmd_queue = queue_create(16, sizeof(foc_cmd_body_t));
  22. shark_task_create(foc_command_task, NULL);
  23. }
  24. bool foc_send_command(foc_cmd_body_t *command) {
  25. if (!queue_put(_cmd_queue, command)) {
  26. if (command->data) {
  27. os_free(command->data);
  28. }
  29. return false;
  30. }
  31. return true;
  32. }
  33. static u32 foc_command_task(void *args) {
  34. foc_cmd_body_t command;
  35. if (queue_get(_cmd_queue, &command)) {
  36. process_foc_command(&command);
  37. if (command.data) {
  38. os_free(command.data);
  39. }
  40. }
  41. return 0;
  42. }
  43. static void process_foc_command(foc_cmd_body_t *command) {
  44. u8 erroCode = 0;
  45. u8 response[32];
  46. int len = 3;
  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. sys_debug("start cmd %d\n", scmd->start_stop);
  54. if (scmd->start_stop == Foc_Start) {
  55. success = mc_start(CTRL_MODE_TRQ);
  56. }else if (scmd->start_stop == Foc_Stop) {
  57. success = mc_stop();
  58. }
  59. if (!success) {
  60. erroCode = PMSM_FOC_GetErrCode();
  61. }
  62. sys_debug("start motor %d\n", erroCode);
  63. break;
  64. }
  65. case Foc_Set_Cruise_Mode:
  66. {
  67. u8 enable = decode_u8(command->data);
  68. if (!PMSM_FOC_EnableCruise(enable)) {
  69. erroCode = PMSM_FOC_GetErrCode();
  70. }
  71. break;
  72. }
  73. case Foc_Set_Cruise_Speed:
  74. {
  75. u8 mode = decode_u8(command->data);
  76. float rpm = (float)decode_s16((u8 *)command->data + 1);
  77. if (mode == 0) {
  78. rpm = PMSM_FOC_GetSpeed() + rpm;
  79. }
  80. if (!PMSM_FOC_Set_CruiseSpeed(rpm)) {
  81. erroCode = PMSM_FOC_GetErrCode();
  82. }
  83. sys_debug("Cruise RPM %d\n", (int)rpm);
  84. encode_u16(response + 3, (s16)rpm);
  85. len += 2;
  86. break;
  87. }
  88. case Foc_Set_Ctrl_Mode:
  89. {
  90. u8 mode = decode_u8(command->data);
  91. sys_debug("mode = %d\n", mode);
  92. if (!mc_set_foc_mode(mode)) {
  93. erroCode = PMSM_FOC_GetErrCode();
  94. }
  95. response[len++] = PMSM_FOC_GetCtrlMode();
  96. break;
  97. }
  98. case Foc_Set_Gear_Limit:
  99. {
  100. if (command->len < 5) {
  101. erroCode = FOC_Param_Err;
  102. }else {
  103. u16 maxRPM = decode_u16(command->data);
  104. u16 maxPhaseCurr = decode_u8((u8 *)command->data + 2);
  105. u8 maxiDC = decode_u8((u8 *)command->data + 4);
  106. PMSM_FOC_SpeedLimit((float)maxRPM);
  107. PMSM_FOC_PhaseCurrLim((float)maxPhaseCurr);
  108. PMSM_FOC_DCCurrLimit((float)maxiDC);
  109. encode_u16(response + 3, (u16)PMSM_FOC_GetSpeedLimit());
  110. encode_u16(response + 5, (u16)PMSM_FOC_GetPhaseCurrLim());
  111. encode_u8(response + 7, (u8)PMSM_FOC_GetDCCurrLimit());
  112. len += 5;
  113. }
  114. break;
  115. }
  116. case Foc_Set_Speed_Limit:
  117. {
  118. s16 speed = decode_s16(((u8 *)command->data));
  119. PMSM_FOC_SpeedLimit(speed);
  120. encode_u16(response + 3, (u16)PMSM_FOC_GetSpeedLimit());
  121. len += 2;
  122. break;
  123. }
  124. case Foc_Set_iDC_Limit:
  125. {
  126. u8 current = decode_u8(((u8 *)command->data));
  127. PMSM_FOC_DCCurrLimit((float)current);
  128. encode_u8(response + 3, (u8)PMSM_FOC_GetDCCurrLimit());
  129. len += 1;
  130. break;
  131. }
  132. case Foc_Set_Phase_CurrLim:
  133. {
  134. s16 curr = decode_s16(((u8 *)command->data));
  135. PMSM_FOC_PhaseCurrLim((float)curr);
  136. encode_u16(response + 3, (u16)PMSM_FOC_GetPhaseCurrLim());
  137. len += 2;
  138. break;
  139. }
  140. case Foc_Cali_Hall_Phase:
  141. {
  142. s16 vd = decode_s16((u8 *)command->data);
  143. sys_debug("cali encoder %d\n", vd);
  144. mc_encoder_off_calibrate((vd));
  145. break;
  146. }
  147. case Foc_Set_Open_Dq_Vol:
  148. {
  149. s16 vd = decode_s16(((u8 *)command->data));
  150. s16 vq = decode_s16(((u8 *)command->data) + 2);
  151. sys_debug("set v_q %d, %d\n", vd, vq);
  152. PMSM_FOC_SetOpenVdq(vd, (vq));
  153. break;
  154. }
  155. case Foc_Conf_Pid:
  156. {
  157. pid_conf_t pid;
  158. u8 id = decode_u8((u8 *)command->data);
  159. memcpy((char *)&pid, (char *)command->data + 1, sizeof(pid_conf_t));
  160. sys_debug("id = %d, kp = %f, ki = %f, kb = %f\n", id, pid.kp, pid.ki, pid.kb);
  161. PMSM_FOC_SetPid(id, pid.kp, pid.ki, pid.kb);
  162. nv_set_pid(id, &pid);
  163. break;
  164. }
  165. case Foc_Set_EPM_Mode:
  166. {
  167. bool mode = decode_u8((u8 *)command->data) == 0?false:true;
  168. if (!mc_start_epm(mode)) {
  169. erroCode = PMSM_FOC_GetErrCode();
  170. }
  171. break;
  172. }
  173. case Foc_Start_EPM_Move:
  174. {
  175. EPM_Dir_t dir = (EPM_Dir_t)decode_u8((u8 *)command->data);
  176. if(!mc_command_epm_move(dir)) {
  177. erroCode = PMSM_FOC_GetErrCode();
  178. }
  179. break;
  180. }
  181. case Foc_Start_DQ_Calibrate:
  182. {
  183. u8 start = decode_u8((u8 *)command->data);
  184. if (start == Foc_Start) {
  185. PMSM_FOC_MTPA_Calibrate(true);
  186. }else {
  187. PMSM_FOC_MTPA_Calibrate(false);
  188. }
  189. break;
  190. }
  191. case Foc_Set_IS_Curr_Angle:
  192. {
  193. if (command->len != 4) {
  194. erroCode = FOC_Param_Err;
  195. }else {
  196. s16 is_curr = decode_s16((u8 *)command->data);
  197. s16 is_angle = decode_s16((u8 *)command->data + 2);
  198. PMSM_FOC_Set_Current(is_curr);
  199. PMSM_FOC_Set_Angle(is_angle);
  200. }
  201. break;
  202. }
  203. case Foc_Set_Plot_Type:
  204. {
  205. u8 plot = decode_u8((u8 *)command->data);
  206. if (plot >= Plot_t_Max) {
  207. erroCode = FOC_Param_Err;
  208. }else {
  209. PMSM_FOC_Set_PlotType((Plot_t)plot);
  210. }
  211. break;
  212. }
  213. default:
  214. {
  215. erroCode = FOC_Unknow_Cmd;
  216. break;
  217. }
  218. }
  219. response[0] = command->cmd;
  220. response[1] = CAN_MY_ADDRESS;
  221. response[2] = erroCode;
  222. can_send_response(command->can_src, response, len);
  223. }