commands.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. mc_need_update();//重新获取转把相电流和速度
  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_GetDCCurrLimit());
  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_DCCurrLimit((float)current);
  129. encode_u8(response + 3, (u8)PMSM_FOC_GetDCCurrLimit());
  130. len += 1;
  131. break;
  132. }
  133. case Foc_Set_Phase_CurrLim:
  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. nv_set_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 (!mc_start_epm(mode)) {
  170. erroCode = PMSM_FOC_GetErrCode();
  171. }
  172. break;
  173. }
  174. case Foc_Start_EPM_Move:
  175. {
  176. EPM_Dir_t dir = (EPM_Dir_t)decode_u8((u8 *)command->data);
  177. if(!mc_command_epm_move(dir)) {
  178. erroCode = PMSM_FOC_GetErrCode();
  179. }
  180. break;
  181. }
  182. case Foc_Start_DQ_Calibrate:
  183. {
  184. u8 start = decode_u8((u8 *)command->data);
  185. if (start == Foc_Start) {
  186. sys_debug("start mpta cali\n");
  187. mc_set_foc_mode(CTRL_MODE_CURRENT);
  188. PMSM_FOC_MTPA_Calibrate(true);
  189. }else {
  190. PMSM_FOC_MTPA_Calibrate(false);
  191. mc_set_foc_mode(CTRL_MODE_TRQ);
  192. }
  193. break;
  194. }
  195. case Foc_Set_IS_Curr_Angle:
  196. {
  197. if (command->len != 4) {
  198. erroCode = FOC_Param_Err;
  199. }else {
  200. s16 is_curr = decode_s16((u8 *)command->data);
  201. s16 is_angle = decode_s16((u8 *)command->data + 2);
  202. sys_debug("curr %d, angle %d\n", is_curr, is_angle);
  203. PMSM_FOC_Set_Current(is_curr);
  204. PMSM_FOC_Set_Angle(is_angle);
  205. }
  206. break;
  207. }
  208. case Foc_Set_Plot_Type:
  209. {
  210. u8 plot = decode_u8((u8 *)command->data);
  211. if (plot >= Plot_t_Max) {
  212. erroCode = FOC_Param_Err;
  213. }else {
  214. PMSM_FOC_Set_PlotType((Plot_t)plot);
  215. }
  216. break;
  217. }
  218. case Foc_Set_Throttle_throld:
  219. {
  220. u16 min = decode_u16((u8 *)command->data);
  221. u16 max = decode_u16((u8 *)command->data + 2);
  222. nv_get_foc_params()->n_minThroVol = (float)min/100.0f;
  223. nv_get_foc_params()->n_maxThroVol = (float)max/100.0f;
  224. nv_save_foc_params();
  225. break;
  226. }
  227. case Foc_Set_eBrake_Throld:
  228. {
  229. }
  230. default:
  231. {
  232. erroCode = FOC_Unknow_Cmd;
  233. break;
  234. }
  235. }
  236. response[0] = command->cmd;
  237. response[1] = CAN_MY_ADDRESS;
  238. response[2] = erroCode;
  239. can_send_response(command->can_src, response, len);
  240. }