commands.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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 void _reboot_timer_handler(shark_timer_t *);
  18. static shark_timer_t _reboot_timer = TIMER_INIT(_reboot_timer, _reboot_timer_handler);
  19. static u32 foc_command_task(void *args);
  20. static void process_foc_command(foc_cmd_body_t *command);
  21. static co_queue_t _cmd_queue;
  22. void foc_command_init(void) {
  23. _cmd_queue = queue_create(16, sizeof(foc_cmd_body_t));
  24. shark_task_create(foc_command_task, NULL);
  25. }
  26. bool foc_send_command(foc_cmd_body_t *command) {
  27. if (!queue_put(_cmd_queue, command)) {
  28. if (command->data) {
  29. os_free(command->data);
  30. }
  31. return false;
  32. }
  33. return true;
  34. }
  35. static u32 foc_command_task(void *args) {
  36. foc_cmd_body_t command;
  37. if (queue_get(_cmd_queue, &command)) {
  38. process_foc_command(&command);
  39. if (command.data) {
  40. os_free(command.data);
  41. }
  42. }
  43. return 0;
  44. }
  45. static void process_ext_command(foc_cmd_body_t *command) {
  46. if (command->ext_key == 0x1A01) {
  47. return;
  48. }else if (command->ext_key == 0x1A02) {
  49. u8 b0 = decode_u8(command->data);
  50. u8 p_mode = decode_8bits(b0, 0, 1);
  51. if (p_mode == 1) {
  52. mc_start(CTRL_MODE_TRQ);
  53. }else if (p_mode == 2) {
  54. mc_stop();
  55. }
  56. s8 ext_gear = decode_8bits(b0, 5, 7);
  57. if (ext_gear >= 0 && ext_gear <= 5) {
  58. if (ext_gear == 0) {
  59. mc_set_gear(3);
  60. }else {
  61. mc_set_gear(ext_gear - 1);
  62. }
  63. }
  64. sys_debug("gear %d\n", ext_gear);
  65. u8 b1 = decode_u8((u8 *)command->data + 1);
  66. u8 cruise = decode_8bits(b1, 0, 1);
  67. if (cruise == 2) {
  68. PMSM_FOC_EnableCruise(true);
  69. }else if (cruise == 1) {
  70. PMSM_FOC_EnableCruise(false);
  71. }
  72. u8 epm = decode_8bits(b0, 2, 3);
  73. if (epm == 2) {
  74. mc_start_epm(true);
  75. }else if(epm == 1) {
  76. mc_start_epm(false);
  77. }
  78. u8 m_4896 = decode_8bits(b1, 4, 5);
  79. u8 epm_dir = decode_8bits(b1, 6, 7);
  80. if (epm_dir == 0) {
  81. mc_command_epm_move(EPM_Dir_None);
  82. }else if (epm_dir == 1) {
  83. mc_command_epm_move(EPM_Dir_Back);
  84. }else if (epm_dir == 2) {
  85. mc_command_epm_move(EPM_Dir_Forward);
  86. }
  87. u16 cruise_spd = decode_u16((u8 *)command->data + 3);
  88. if ((cruise_spd > 0) && (cruise_spd != 0xFFFF)) {
  89. PMSM_FOC_Set_CruiseSpeed((float)cruise_spd);
  90. }
  91. u8 response[8] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
  92. response[0] &= 0xFC;
  93. response[0] |= (mc_is_start()?1:2);
  94. response[0] |= (mc_get_gear() << 5);
  95. response[1] &= 0xC0;
  96. response[1] |= (PMSM_FOC_Is_CruiseEnabled()?2:1);
  97. response[1] |= (mc_is_epm()?1:2) << 2;
  98. response[1] |= m_4896<<4;
  99. shark_can0_send_ext_message(0x1A024D43, response, sizeof(response));
  100. }else if (command->ext_key == 0x1A05) {
  101. shark_can0_send_ext_message(0x1A054D43, command->data, command->len);
  102. }
  103. }
  104. static void process_foc_command(foc_cmd_body_t *command) {
  105. u8 erroCode = 0;
  106. u8 response[32];
  107. int len = 3;
  108. if ((command->ext_key != 0) && (command->cmd == 0)) {
  109. process_ext_command(command);
  110. return;
  111. }
  112. switch (command->cmd) {
  113. case Foc_Start_Motor:
  114. {
  115. bool success;
  116. foc_start_cmd_t *scmd = (foc_start_cmd_t *)command->data;
  117. sys_debug("start cmd %d\n", scmd->start_stop);
  118. if (scmd->start_stop == Foc_Start) {
  119. success = mc_start(CTRL_MODE_TRQ);
  120. }else if (scmd->start_stop == Foc_Stop) {
  121. success = mc_stop();
  122. }
  123. if (!success) {
  124. erroCode = PMSM_FOC_GetErrCode();
  125. }
  126. sys_debug("start motor %d\n", erroCode);
  127. break;
  128. }
  129. case Foc_Set_Cruise_Mode:
  130. {
  131. u8 enable = decode_u8(command->data);
  132. if (!PMSM_FOC_EnableCruise(enable)) {
  133. erroCode = PMSM_FOC_GetErrCode();
  134. }
  135. break;
  136. }
  137. case Foc_Set_Cruise_Speed:
  138. {
  139. u8 mode = decode_u8(command->data);
  140. float rpm = (float)decode_s16((u8 *)command->data + 1);
  141. if (mode == 0) {
  142. rpm = PMSM_FOC_GetSpeed() + rpm;
  143. }
  144. if (!PMSM_FOC_Set_CruiseSpeed(rpm)) {
  145. erroCode = PMSM_FOC_GetErrCode();
  146. }
  147. sys_debug("Cruise RPM %d\n", (int)rpm);
  148. encode_u16(response + 3, (s16)rpm);
  149. len += 2;
  150. break;
  151. }
  152. case Foc_Set_Ctrl_Mode:
  153. {
  154. u8 mode = decode_u8(command->data);
  155. sys_debug("mode = %d\n", mode);
  156. if (!mc_set_foc_mode(mode)) {
  157. erroCode = PMSM_FOC_GetErrCode();
  158. }
  159. response[len++] = PMSM_FOC_GetCtrlMode();
  160. break;
  161. }
  162. case Foc_Set_Gear_Limit:
  163. {
  164. if (command->len < 7) {
  165. erroCode = FOC_Param_Err;
  166. }else {
  167. u8 gear = decode_u8(command->data);
  168. u16 maxRPM = decode_u16((u8 *)command->data + 1);
  169. u16 maxPhaseCurr = decode_u8((u8 *)command->data + 3);
  170. u16 maxiDC = decode_u8((u8 *)command->data + 5);
  171. u16 accline = decode_u8((u8 *)command->data + 7);
  172. if (!nv_set_gear_config((gear>>4 & 0xF), (gear & 0xF), maxRPM, maxPhaseCurr, maxiDC, accline)){
  173. erroCode = FOC_Param_Err;
  174. }
  175. }
  176. break;
  177. }
  178. case Foc_Get_Gear_Limit:
  179. {
  180. u8 gear = decode_u8(command->data);
  181. u16 maxRPM;
  182. u16 maxPhaseCurr;
  183. u16 maxiDC;
  184. u16 accline;
  185. if (!nv_get_gear_config((gear>>4 & 0xF), (gear & 0xF), &maxRPM, &maxPhaseCurr, &maxiDC, &accline)){
  186. erroCode = FOC_Param_Err;
  187. }else {
  188. encode_u8(response + 3, gear);
  189. encode_u8(response + 4, maxRPM);
  190. encode_u8(response + 6, maxPhaseCurr);
  191. encode_u8(response + 8, maxiDC);
  192. encode_u8(response + 10, accline);
  193. len += 9;
  194. }
  195. break;
  196. }
  197. case Foc_Set_Speed_Limit:
  198. {
  199. s16 speed = decode_s16(((u8 *)command->data));
  200. PMSM_FOC_SpeedLimit(speed);
  201. encode_u16(response + 3, (u16)PMSM_FOC_GetSpeedLimit());
  202. len += 2;
  203. break;
  204. }
  205. case Foc_Set_iDC_Limit:
  206. {
  207. u8 current = decode_u8(((u8 *)command->data));
  208. PMSM_FOC_DCCurrLimit((float)current);
  209. encode_u8(response + 3, (u8)PMSM_FOC_GetDCCurrLimit());
  210. len += 1;
  211. break;
  212. }
  213. case Foc_Set_Phase_CurrLim:
  214. {
  215. s16 curr = decode_s16(((u8 *)command->data));
  216. PMSM_FOC_PhaseCurrLim((float)curr);
  217. encode_u16(response + 3, (u16)PMSM_FOC_GetPhaseCurrLim());
  218. len += 2;
  219. break;
  220. }
  221. case Foc_Cali_Hall_Phase:
  222. {
  223. s16 vd = decode_s16((u8 *)command->data);
  224. sys_debug("cali encoder %d\n", vd);
  225. //mc_encoder_off_calibrate((vd));
  226. mc_encoder_zero_calibrate(vd);
  227. break;
  228. }
  229. case Foc_Set_Open_Dq_Vol:
  230. {
  231. s16 vd = decode_s16(((u8 *)command->data));
  232. s16 vq = decode_s16(((u8 *)command->data) + 2);
  233. sys_debug("set v_q %d, %d\n", vd, vq);
  234. PMSM_FOC_SetOpenVdq(vd, (vq));
  235. break;
  236. }
  237. case Foc_Conf_Pid:
  238. {
  239. if (command->len < 13) {
  240. erroCode = FOC_Param_Err;
  241. break;
  242. }
  243. pid_conf_t pid;
  244. u8 id = decode_u8((u8 *)command->data);
  245. memcpy((char *)&pid, (char *)command->data + 1, sizeof(pid_conf_t));
  246. sys_debug("id = %d, kp = %f, ki = %f, kb = %f\n", id, pid.kp, pid.ki, pid.kb);
  247. PMSM_FOC_SetPid(id, pid.kp, pid.ki, pid.kb);
  248. nv_set_pid(id, &pid);
  249. break;
  250. }
  251. case Foc_Get_Pid:
  252. {
  253. pid_conf_t pid;
  254. u8 id = decode_u8((u8 *)command->data);
  255. if (id < PID_Max_id) {
  256. nv_get_pid(id, &pid);
  257. erroCode = id;
  258. memcpy(response+3, &pid, sizeof(pid));
  259. len = sizeof(pid) + 3;
  260. sys_debug("id = %d, kp = %f, ki = %f, kb = %f\n", id, pid.kp, pid.ki, pid.kb);
  261. }else {
  262. erroCode = 1;
  263. len = 3;
  264. }
  265. break;
  266. }
  267. case Foc_Set_EPM_Mode:
  268. {
  269. bool mode = decode_u8((u8 *)command->data) == 0?false:true;
  270. if (!mc_start_epm(mode)) {
  271. erroCode = PMSM_FOC_GetErrCode();
  272. }
  273. break;
  274. }
  275. case Foc_Lock_Motor:
  276. {
  277. u8 lock = decode_u8((u8 *)command->data);
  278. if (lock == Foc_Start) {
  279. mc_lock_motor(true);
  280. }else {
  281. mc_lock_motor(false);
  282. }
  283. erroCode = PMSM_FOC_GetErrCode();
  284. break;
  285. }
  286. case Foc_Auto_Hold:
  287. {
  288. u8 hold = decode_u8((u8 *)command->data);
  289. if (hold == Foc_Start) {
  290. mc_auto_hold(true);
  291. }else {
  292. mc_auto_hold(false);
  293. }
  294. erroCode = PMSM_FOC_GetErrCode();
  295. break;
  296. }
  297. case Foc_Start_EPM_Move:
  298. {
  299. EPM_Dir_t dir = (EPM_Dir_t)decode_u8((u8 *)command->data);
  300. if(!mc_command_epm_move(dir)) {
  301. erroCode = PMSM_FOC_GetErrCode();
  302. }
  303. break;
  304. }
  305. case Foc_Start_DQ_Calibrate:
  306. {
  307. u8 start = decode_u8((u8 *)command->data);
  308. if (start == Foc_Start) {
  309. sys_debug("start mpta cali\n");
  310. mc_set_foc_mode(CTRL_MODE_CURRENT);
  311. PMSM_FOC_MTPA_Calibrate(true);
  312. }else {
  313. PMSM_FOC_MTPA_Calibrate(false);
  314. mc_set_foc_mode(CTRL_MODE_TRQ);
  315. }
  316. break;
  317. }
  318. case Foc_Set_IS_Curr_Angle:
  319. {
  320. if (command->len != 4) {
  321. erroCode = FOC_Param_Err;
  322. }else {
  323. s16 is_curr = decode_s16((u8 *)command->data);
  324. s16 is_angle = decode_s16((u8 *)command->data + 2);
  325. sys_debug("curr %d, angle %d\n", is_curr, is_angle);
  326. PMSM_FOC_Set_Current(is_curr);
  327. PMSM_FOC_Set_Angle(is_angle);
  328. }
  329. break;
  330. }
  331. case Foc_Set_Plot_Type:
  332. {
  333. u8 plot = decode_u8((u8 *)command->data);
  334. if (plot >= Plot_t_Max) {
  335. erroCode = FOC_Param_Err;
  336. }else {
  337. PMSM_FOC_Set_PlotType((Plot_t)plot);
  338. }
  339. break;
  340. }
  341. case Foc_Set_Throttle_throld:
  342. {
  343. if (mc_is_start()) {
  344. erroCode = FOC_NotAllowed;
  345. }else {
  346. u16 min = decode_u16((u8 *)command->data);
  347. u16 max = decode_u16((u8 *)command->data + 2);
  348. nv_get_foc_params()->n_minThroVol = (float)min/100.0f;
  349. nv_get_foc_params()->n_maxThroVol = (float)max/100.0f;
  350. nv_save_foc_params();
  351. }
  352. break;
  353. }
  354. case Foc_Get_Config:
  355. {
  356. len = sizeof(foc_params_t) + 2 - sizeof(pid_conf_t) * PID_Max_id - 2;
  357. u8 *config = os_alloc(len);
  358. if (config == NULL) {
  359. erroCode = FOC_MEM_Err;
  360. break;
  361. }
  362. memcpy((void *)(config + 2), (void *)nv_get_foc_params(), sizeof(foc_params_t) - sizeof(pid_conf_t) * PID_Max_id - 2);
  363. config[0] = command->cmd;
  364. config[1] = CAN_MY_ADDRESS;
  365. can_send_response(command->can_src, config, len);
  366. os_free(config);
  367. return;
  368. }
  369. case Foc_Set_Config:
  370. {
  371. if (mc_is_start()) {
  372. erroCode = FOC_NotAllowed;
  373. }else if (command->len < 32) {
  374. erroCode = FOC_Param_Err;
  375. }else {
  376. nv_get_foc_params()->s_PhaseCurrLim = decode_s16((u8 *)command->data);
  377. nv_get_foc_params()->s_maxRPM = decode_s16((u8 *)command->data + 2);
  378. nv_get_foc_params()->s_PhaseCurreBrkLim = decode_s16((u8 *)command->data + 4);
  379. nv_get_foc_params()->s_iDCeBrkLim = decode_s16((u8 *)command->data + 6);
  380. nv_get_foc_params()->s_LimitiDC = decode_s16((u8 *)command->data + 8);
  381. nv_get_foc_params()->n_minThroVol = (float)decode_s16((u8 *)command->data + 10)/100.0f;
  382. nv_get_foc_params()->n_maxThroVol = (float)decode_s16((u8 *)command->data + 12)/100.0f;
  383. nv_get_foc_params()->s_maxEpmRPM = decode_s16((u8 *)command->data + 14);
  384. nv_get_foc_params()->s_maxEpmPhaseCurrLim = decode_s16((u8 *)command->data + 16);
  385. nv_get_foc_params()->n_brkShutPower = decode_u8((u8 *)command->data + 18);
  386. nv_get_foc_params()->n_autoHold = decode_u8((u8 *)command->data + 19);
  387. nv_get_foc_params()->n_acc_time = decode_u32((u8 *)command->data + 20);
  388. nv_get_foc_params()->n_dec_time = decode_u32((u8 *)command->data + 24);
  389. nv_get_foc_params()->n_ebrk_time = decode_u32((u8 *)command->data + 28);
  390. nv_save_foc_params();
  391. shark_timer_post(&_reboot_timer, 200);
  392. }
  393. break;
  394. }
  395. case Foc_Fan_Duty:
  396. {
  397. u8 duty = decode_u8(command->data);
  398. mc_set_fan_duty(duty);
  399. break;
  400. }
  401. case Foc_Set_eBrake_Throld:
  402. {
  403. break;
  404. }
  405. default:
  406. {
  407. erroCode = FOC_Unknow_Cmd;
  408. break;
  409. }
  410. }
  411. sys_debug("err = %d\n", erroCode);
  412. response[0] = command->cmd;
  413. response[1] = CAN_MY_ADDRESS;
  414. response[2] = erroCode;
  415. can_send_response(command->can_src, response, len);
  416. }
  417. static void _reboot_timer_handler(shark_timer_t *t) {
  418. system_reboot();
  419. }