commands.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  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. #include "foc/core/foc_observer.h"
  14. #ifdef CONFIG_DQ_STEP_RESPONSE
  15. extern float target_d;
  16. extern float target_q;
  17. #endif
  18. static void _reboot_timer_handler(shark_timer_t *);
  19. static shark_timer_t _reboot_timer = TIMER_INIT(_reboot_timer, _reboot_timer_handler);
  20. static u32 foc_command_task(void *args);
  21. static void process_foc_command(foc_cmd_body_t *command);
  22. static co_queue_t _cmd_queue;
  23. void foc_command_init(void) {
  24. _cmd_queue = queue_create(16, sizeof(foc_cmd_body_t));
  25. shark_task_create(foc_command_task, NULL);
  26. }
  27. bool foc_send_command(foc_cmd_body_t *command) {
  28. if (!queue_put(_cmd_queue, command)) {
  29. if (command->data) {
  30. os_free(command->data);
  31. }
  32. return false;
  33. }
  34. return true;
  35. }
  36. static u32 foc_command_task(void *args) {
  37. foc_cmd_body_t command;
  38. if (queue_get(_cmd_queue, &command)) {
  39. process_foc_command(&command);
  40. if (command.data) {
  41. os_free(command.data);
  42. }
  43. }
  44. return 0;
  45. }
  46. static void process_ext_command(foc_cmd_body_t *command) {
  47. if (command->ext_key == 0x1A01) {
  48. return;
  49. }else if (command->ext_key == 0x1A02) {
  50. u8 b0 = decode_u8(command->data);
  51. u8 p_mode = decode_8bits(b0, 0, 1);
  52. if (p_mode == 1) {
  53. mc_start(CTRL_MODE_TRQ);
  54. }else if (p_mode == 2) {
  55. mc_stop();
  56. }
  57. s8 ext_gear = decode_8bits(b0, 5, 7);
  58. if (ext_gear >= 0 && ext_gear <= 5) {
  59. sys_debug("gear %d\n", ext_gear);
  60. if (ext_gear == 0) {
  61. mc_set_gear(3);
  62. }else {
  63. mc_set_gear(ext_gear - 1);
  64. }
  65. }
  66. u8 b1 = decode_u8((u8 *)command->data + 1);
  67. u8 cruise = decode_8bits(b1, 0, 1);
  68. if (cruise == 2) {
  69. PMSM_FOC_EnableCruise(true);
  70. }else if (cruise == 1) {
  71. PMSM_FOC_EnableCruise(false);
  72. }
  73. u8 epm = decode_8bits(b0, 2, 3);
  74. if (epm == 2) {
  75. mc_start_epm(true);
  76. }else if(epm == 1) {
  77. mc_start_epm(false);
  78. }
  79. u8 m_4896 = decode_8bits(b1, 4, 5);
  80. u8 epm_dir = decode_8bits(b1, 6, 7);
  81. if (epm_dir == 0) {
  82. mc_command_epm_move(EPM_Dir_None);
  83. }else if (epm_dir == 1) {
  84. mc_command_epm_move(EPM_Dir_Back);
  85. }else if (epm_dir == 2) {
  86. mc_command_epm_move(EPM_Dir_Forward);
  87. }
  88. u16 cruise_spd = decode_u16((u8 *)command->data + 3);
  89. if ((cruise_spd > 0) && (cruise_spd != 0xFFFF)) {
  90. PMSM_FOC_Set_CruiseSpeed((float)cruise_spd);
  91. }
  92. u8 response[8] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
  93. response[0] &= 0xFC;
  94. response[0] |= (mc_is_start()?1:2);
  95. response[0] |= (mc_get_gear() << 5);
  96. response[1] &= 0xC0;
  97. response[1] |= (PMSM_FOC_Is_CruiseEnabled()?2:1);
  98. response[1] |= (mc_is_epm()?1:2) << 2;
  99. response[1] |= m_4896<<4;
  100. shark_can0_send_ext_message(0x1A024D43, response, sizeof(response));
  101. }else if (command->ext_key == 0x1A05) {
  102. shark_can0_send_ext_message(0x1A054D43, command->data, command->len);
  103. }
  104. }
  105. static u8 ignore_with_speed[] = {Foc_Set_Gear_Limit, Foc_Conf_Pid, Foc_Set_Throttle_throld, Foc_Set_Config, Foc_Set_eBrake_Throld, Foc_Start_Write_TRQ_Table, Foc_Write_TRQ_Table, Foc_End_Write_TRQ_Table, Foc_SN_Write};
  106. static bool _can_process_with_speed(u8 cmd) {
  107. int size = ARRAY_SIZE(ignore_with_speed);
  108. if (!mc_is_start() || PMSM_FOC_GetSpeed() < 0.1f) {
  109. return true;
  110. }
  111. for (int i = 0; i < size; i++) {
  112. if (ignore_with_speed[i] == cmd) {
  113. return false;
  114. }
  115. }
  116. return true;
  117. }
  118. static void process_foc_command(foc_cmd_body_t *command) {
  119. u8 erroCode = 0;
  120. u8 response[32];
  121. int len = 3;
  122. if ((command->ext_key != 0) && (command->cmd == 0)) {
  123. process_ext_command(command);
  124. return;
  125. }
  126. if (!_can_process_with_speed(command->cmd)) {
  127. erroCode = FOC_NowAllowed_With_Speed;
  128. goto cmd_end;
  129. }
  130. switch (command->cmd) {
  131. case Foc_Start_Motor:
  132. {
  133. bool success;
  134. foc_start_cmd_t *scmd = (foc_start_cmd_t *)command->data;
  135. sys_debug("start cmd %d\n", scmd->start_stop);
  136. if (scmd->start_stop == Foc_Start) {
  137. success = mc_start(CTRL_MODE_TRQ);
  138. }else if (scmd->start_stop == Foc_Stop) {
  139. success = mc_stop();
  140. }
  141. if (!success) {
  142. erroCode = PMSM_FOC_GetErrCode();
  143. }
  144. sys_debug("start motor %d\n", erroCode);
  145. break;
  146. }
  147. case Foc_Set_DQ_Current:
  148. {
  149. #ifdef CONFIG_DQ_STEP_RESPONSE
  150. if (command->len == 2) {
  151. target_d = (float)decode_s08(command->data);
  152. target_q = (float)decode_s08((u8 *)command->data + 1);
  153. sys_debug("step res %f, %f\n", target_d, target_q);
  154. }else {
  155. erroCode = FOC_Param_Err;
  156. }
  157. #else
  158. erroCode = FOC_NotAllowed;
  159. #endif
  160. break;
  161. }
  162. case Foc_Set_Gear_Mode:
  163. {
  164. u8 gear = decode_u8(command->data);
  165. if (gear > 3) {
  166. erroCode = FOC_Param_Err;
  167. }else {
  168. sys_debug("set gear %d\n", gear);
  169. mc_set_gear(gear);
  170. response[3] = gear;
  171. len += 1;
  172. }
  173. break;
  174. }
  175. case Foc_Set_Cruise_Mode:
  176. {
  177. u8 enable = decode_u8(command->data);
  178. if (!mc_enable_cruise(enable)) {
  179. erroCode = PMSM_FOC_GetErrCode();
  180. }
  181. break;
  182. }
  183. case Foc_Set_Cruise_Speed:
  184. {
  185. u8 mode = decode_u8(command->data);
  186. float rpm = (float)decode_s16((u8 *)command->data + 1);
  187. if (!mc_set_cruise_speed(mode?true:false, rpm)) {
  188. erroCode = PMSM_FOC_GetErrCode();
  189. }
  190. sys_debug("Cruise RPM %d\n", (int)rpm);
  191. encode_u16(response + 3, (s16)rpm);
  192. len += 2;
  193. break;
  194. }
  195. case Foc_Set_Ctrl_Mode:
  196. {
  197. u8 mode = decode_u8(command->data);
  198. sys_debug("mode = %d\n", mode);
  199. if (!mc_set_foc_mode(mode)) {
  200. erroCode = PMSM_FOC_GetErrCode();
  201. }
  202. response[len++] = PMSM_FOC_GetCtrlMode();
  203. break;
  204. }
  205. case Foc_Set_Gear_Limit:
  206. {
  207. sys_debug("len = %d\n", command->len);
  208. if (command->len < 9) {
  209. erroCode = FOC_Param_Err;
  210. }else {
  211. u8 gear = decode_u8(command->data);
  212. u16 maxRPM = decode_u16((u8 *)command->data + 1);
  213. u16 maxPhaseCurr = decode_u16((u8 *)command->data + 3);
  214. u16 maxiDC = decode_u16((u8 *)command->data + 5);
  215. u16 accline = decode_u16((u8 *)command->data + 7);
  216. sys_debug("gear 0x%x, rpm %d, phase %d idc %d acc %d\n", gear, maxRPM, maxPhaseCurr, maxiDC, accline);
  217. if (!nv_set_gear_config((gear>>4 & 0xF), (gear & 0xF), maxRPM, maxPhaseCurr, maxiDC, accline)){
  218. erroCode = FOC_Param_Err;
  219. }
  220. }
  221. break;
  222. }
  223. case Foc_Get_Gear_Limit:
  224. {
  225. u8 gear = decode_u8(command->data);
  226. u16 maxRPM;
  227. u16 maxPhaseCurr;
  228. u16 maxiDC;
  229. u16 accline;
  230. sys_debug("gear = 0x%x\n", gear);
  231. if (!nv_get_gear_config((gear>>4 & 0xF), (gear & 0xF), &maxRPM, &maxPhaseCurr, &maxiDC, &accline)){
  232. erroCode = FOC_Param_Err;
  233. }else {
  234. encode_u8(response + 3, gear);
  235. encode_u16(response + 4, maxRPM);
  236. encode_u16(response + 6, maxPhaseCurr);
  237. encode_u16(response + 8, maxiDC);
  238. encode_u16(response + 10, accline);
  239. len += 9;
  240. }
  241. break;
  242. }
  243. case Foc_Set_Speed_Limit:
  244. {
  245. s16 speed = decode_s16(((u8 *)command->data));
  246. PMSM_FOC_SpeedLimit(speed);
  247. encode_u16(response + 3, (u16)PMSM_FOC_GetSpeedLimit());
  248. len += 2;
  249. break;
  250. }
  251. case Foc_Set_iDC_Limit:
  252. {
  253. u8 current = decode_u8(((u8 *)command->data));
  254. mc_set_idc_limit((float)current);
  255. encode_u8(response + 3, (u8)PMSM_FOC_GetDCCurrLimit());
  256. len += 1;
  257. break;
  258. }
  259. case Foc_Set_Phase_CurrLim:
  260. {
  261. s16 curr = decode_s16(((u8 *)command->data));
  262. PMSM_FOC_PhaseCurrLim((float)curr);
  263. encode_u16(response + 3, (u16)PMSM_FOC_GetPhaseCurrLim());
  264. len += 2;
  265. break;
  266. }
  267. case Foc_Cali_Hall_Phase:
  268. {
  269. s16 vd = decode_s16((u8 *)command->data);
  270. sys_debug("cali encoder %d\n", vd);
  271. erroCode = mc_encoder_zero_calibrate(vd)? FOC_Success:FOC_Param_Err;
  272. break;
  273. }
  274. case Foc_Enc_Zero_Cali_Result:
  275. {
  276. response[2] = encoder_get_cali_error()?1:0;
  277. u32 off = encoder_get_cnt_offset();
  278. encode_u32(response + 3, off);
  279. len += 4;
  280. break;
  281. }
  282. case Foc_Force_Open_Run:
  283. {
  284. s16 vd = decode_s16((u8 *)command->data);
  285. mc_force_run_open(vd, 0);
  286. break;
  287. }
  288. case Foc_Set_Open_Dq_Vol:
  289. {
  290. s16 vd = decode_s16(((u8 *)command->data));
  291. s16 vq = decode_s16(((u8 *)command->data) + 2);
  292. sys_debug("set v_q %d, %d\n", vd, vq);
  293. PMSM_FOC_SetOpenVdq(vd, (vq));
  294. break;
  295. }
  296. case Foc_Conf_Pid:
  297. {
  298. if (command->len < 13) {
  299. erroCode = FOC_Param_Err;
  300. break;
  301. }
  302. pid_conf_t pid;
  303. u8 id = decode_u8((u8 *)command->data);
  304. memcpy((char *)&pid, (char *)command->data + 1, sizeof(pid_conf_t));
  305. sys_debug("set id = %d, kp = %f, ki = %f, kd = %f\n", id, pid.kp, pid.ki, pid.kd);
  306. PMSM_FOC_SetPid(id, pid.kp, pid.ki, pid.kd);
  307. nv_set_pid(id, &pid);
  308. break;
  309. }
  310. case Foc_Get_Pid:
  311. {
  312. pid_conf_t pid;
  313. u8 id = decode_u8((u8 *)command->data);
  314. if (id < PID_Max_id) {
  315. nv_get_pid(id, &pid);
  316. erroCode = id;
  317. memcpy(response+3, &pid, sizeof(pid));
  318. len = sizeof(pid) + 3;
  319. sys_debug("get id = %d, kp = %f, ki = %f, kd = %f\n", id, pid.kp, pid.ki, pid.kd);
  320. }else {
  321. erroCode = 1;
  322. len = 3;
  323. }
  324. break;
  325. }
  326. case Foc_Set_EPM_Mode:
  327. {
  328. bool mode = decode_u8((u8 *)command->data) == 0?false:true;
  329. if (!mc_start_epm(mode)) {
  330. erroCode = PMSM_FOC_GetErrCode();
  331. }
  332. break;
  333. }
  334. case Foc_Set_Thro_Ration:
  335. {
  336. if (command->len >= 2) {
  337. bool use = decode_u8(command->data)==0?false:true;
  338. u8 r = decode_u8((u8 *)command->data + 1);
  339. mc_set_throttle_r(use, r);
  340. }
  341. break;
  342. }
  343. case Foc_Lock_Motor:
  344. {
  345. u8 lock = decode_u8((u8 *)command->data);
  346. if (lock == Foc_Start) {
  347. mc_lock_motor(true);
  348. }else {
  349. mc_lock_motor(false);
  350. }
  351. erroCode = PMSM_FOC_GetErrCode();
  352. break;
  353. }
  354. case Foc_Auto_Hold:
  355. {
  356. u8 hold = decode_u8((u8 *)command->data);
  357. if (hold == Foc_Start) {
  358. mc_auto_hold(true);
  359. }else {
  360. mc_auto_hold(false);
  361. }
  362. erroCode = PMSM_FOC_GetErrCode();
  363. break;
  364. }
  365. case Foc_Start_EPM_Move:
  366. {
  367. EPM_Dir_t dir = (EPM_Dir_t)decode_u8((u8 *)command->data);
  368. if(!mc_command_epm_move(dir)) {
  369. erroCode = PMSM_FOC_GetErrCode();
  370. }
  371. break;
  372. }
  373. case Foc_Start_DQ_Calibrate:
  374. {
  375. u8 start = decode_u8((u8 *)command->data);
  376. if (start == Foc_Start) {
  377. sys_debug("start mpta cali\n");
  378. mc_set_foc_mode(CTRL_MODE_CURRENT);
  379. PMSM_FOC_MTPA_Calibrate(true);
  380. }else {
  381. PMSM_FOC_MTPA_Calibrate(false);
  382. mc_set_foc_mode(CTRL_MODE_TRQ);
  383. }
  384. break;
  385. }
  386. case Foc_Set_IS_Curr_Angle:
  387. {
  388. if (command->len != 4) {
  389. erroCode = FOC_Param_Err;
  390. }else {
  391. s16 is_curr = decode_s16((u8 *)command->data);
  392. s16 is_angle = decode_s16((u8 *)command->data + 2);
  393. sys_debug("curr %d, angle %d\n", is_curr, is_angle);
  394. PMSM_FOC_Set_Current(is_curr);
  395. PMSM_FOC_Set_Angle(is_angle);
  396. }
  397. break;
  398. }
  399. case Foc_Set_Plot_Type:
  400. {
  401. u8 plot = decode_u8((u8 *)command->data);
  402. if (plot >= Plot_t_Max) {
  403. erroCode = FOC_Param_Err;
  404. }else {
  405. PMSM_FOC_Set_PlotType((Plot_t)plot);
  406. }
  407. break;
  408. }
  409. case Foc_Set_Throttle_throld:
  410. {
  411. if (mc_is_start()) {
  412. erroCode = FOC_NotAllowed;
  413. }else {
  414. u16 start = decode_u16((u8 *)command->data);
  415. u16 end = decode_u16((u8 *)command->data + 2);
  416. nv_get_foc_params()->n_startThroVol = (float)start/100.0f;
  417. nv_get_foc_params()->n_endThroVol = (float)end/100.0f;
  418. nv_save_foc_params();
  419. }
  420. break;
  421. }
  422. case Foc_Get_Config:
  423. {
  424. len = sizeof(foc_params_t) + 2 - sizeof(pid_conf_t) * PID_Max_id - 2;
  425. u8 *config = os_alloc(len);
  426. if (config == NULL) {
  427. erroCode = FOC_MEM_Err;
  428. break;
  429. }
  430. memcpy((void *)(config + 2), (void *)nv_get_foc_params(), sizeof(foc_params_t) - sizeof(pid_conf_t) * PID_Max_id - 2);
  431. config[0] = command->cmd;
  432. config[1] = CAN_MY_ADDRESS;
  433. can_send_response(command->can_src, config, len);
  434. os_free(config);
  435. return;
  436. }
  437. case Foc_Set_Config:
  438. {
  439. if (mc_is_start()) {
  440. erroCode = FOC_NotAllowed;
  441. }else if (command->len < 32) {
  442. erroCode = FOC_Param_Err;
  443. }else {
  444. nv_get_foc_params()->s_PhaseCurrLim = decode_s16((u8 *)command->data);
  445. nv_get_foc_params()->s_maxRPM = decode_s16((u8 *)command->data + 2);
  446. nv_get_foc_params()->s_TorqueBrkLim = decode_s16((u8 *)command->data + 4);
  447. nv_get_foc_params()->s_iDCeBrkLim = decode_s16((u8 *)command->data + 6);
  448. nv_get_foc_params()->s_LimitiDC = decode_s16((u8 *)command->data + 8);
  449. nv_get_foc_params()->n_startThroVol = (float)decode_s16((u8 *)command->data + 10)/100.0f;
  450. nv_get_foc_params()->n_endThroVol = (float)decode_s16((u8 *)command->data + 12)/100.0f;
  451. nv_get_foc_params()->s_maxEpmRPM = decode_s16((u8 *)command->data + 14);
  452. nv_get_foc_params()->s_maxEpmTorqueLim = decode_s16((u8 *)command->data + 16);
  453. nv_get_foc_params()->n_brkShutPower = decode_u8((u8 *)command->data + 18);
  454. nv_get_foc_params()->n_autoHold = decode_u8((u8 *)command->data + 19);
  455. nv_get_foc_params()->n_acc_time = decode_u32((u8 *)command->data + 20);
  456. nv_get_foc_params()->n_dec_time = decode_u32((u8 *)command->data + 24);
  457. nv_get_foc_params()->n_ebrk_time = decode_u32((u8 *)command->data + 28);
  458. nv_save_foc_params();
  459. shark_timer_post(&_reboot_timer, 200);
  460. }
  461. break;
  462. }
  463. case Foc_Fan_Duty:
  464. {
  465. u8 duty = decode_u8(command->data);
  466. mc_set_fan_duty(duty);
  467. break;
  468. }
  469. case Foc_Set_eBrake_Throld:
  470. {
  471. if (command->len >= 4) {
  472. float phase_curr = (float)decode_s16((u8 *)command->data);
  473. float dc_curr = (float)decode_s16((u8 *)command->data + 2);
  474. PMSM_FOC_SetEbrkTorque(phase_curr, dc_curr);
  475. }
  476. break;
  477. }
  478. case Foc_Use_SensorLess_Angle:
  479. {
  480. bool sensorless = decode_u8((u8 *)command->data)?true:false;
  481. if (sensorless && mc_is_start() && PMSM_FOC_GetSpeed() >= CONFIG_SMO_MIN_SPEED) {
  482. sys_debug("use smo %d\n", sensorless);
  483. foc_observer_use_smo(sensorless);
  484. }else {
  485. sys_debug("unuse smo\n");
  486. foc_observer_use_smo(false);
  487. }
  488. break;
  489. }
  490. case Foc_Start_Write_TRQ_Table:
  491. {
  492. nv_write_trq_table_begin(0);
  493. break;
  494. }
  495. case Foc_Write_TRQ_Table:
  496. {
  497. int pos = nv_write_trq_table_continue((u8 *)command->data, command->len);
  498. if (pos < 0) {
  499. erroCode = FOC_NotAllowed;
  500. }else {
  501. encode_u24(response+3, pos);
  502. len += 3;
  503. }
  504. break;
  505. }
  506. case Foc_End_Write_TRQ_Table:
  507. {
  508. erroCode = nv_write_trq_table_check((u8 *)command->data, command->len, 0)?FOC_CRC_Err:FOC_Success;
  509. break;
  510. }
  511. case Foc_SN_Write:
  512. {
  513. if (command->len < 18) {
  514. erroCode = FOC_Param_Err;
  515. }else{
  516. erroCode = nv_write_sn((u8 *)command->data, command->len)>0?0:1 ;
  517. }
  518. break;
  519. }
  520. case Foc_SN_Read:
  521. {
  522. if (nv_read_sn(response + 3, 18) == 0) {
  523. memset(response + 3, '0', 18);
  524. }
  525. len += 18;
  526. break;
  527. }
  528. default:
  529. {
  530. erroCode = FOC_Unknow_Cmd;
  531. break;
  532. }
  533. }
  534. cmd_end:
  535. sys_debug("err = %d\n", erroCode);
  536. response[0] = command->cmd;
  537. response[1] = CAN_MY_ADDRESS;
  538. response[2] = erroCode;
  539. can_send_response(command->can_src, response, len);
  540. }
  541. static void _reboot_timer_handler(shark_timer_t *t) {
  542. system_reboot();
  543. }