commands.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739
  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_driver.h"
  7. #include "foc/motor/motor.h"
  8. #include "foc/commands.h"
  9. #include "prot/can_foc_msg.h"
  10. #include "app/nv_storage.h"
  11. #include "foc/core/foc_observer.h"
  12. #include "foc/mc_error.h"
  13. #include "foc/motor/mot_params_ind.h"
  14. #include "foc/mc_config.h"
  15. #include "foc/motor/throttle.h"
  16. extern int plot_type;
  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. static bool _pc_connect = false;
  23. bool can_is_connect_pc(void) {
  24. return _pc_connect;
  25. }
  26. void can_debug(bool enable) {
  27. _pc_connect = enable;
  28. set_log_level(MOD_SYSTEM, enable?L_debug:L_disable);
  29. }
  30. void foc_command_init(void) {
  31. _cmd_queue = queue_create(16, sizeof(foc_cmd_body_t));
  32. shark_task_create(foc_command_task, NULL);
  33. }
  34. bool foc_send_command(foc_cmd_body_t *command) {
  35. if (!queue_put(_cmd_queue, command)) {
  36. if (command->data) {
  37. os_free(command->data);
  38. }
  39. return false;
  40. }
  41. return true;
  42. }
  43. static u32 foc_command_task(void *args) {
  44. foc_cmd_body_t command;
  45. if (queue_get(_cmd_queue, &command)) {
  46. process_foc_command(&command);
  47. if (command.data) {
  48. os_free(command.data);
  49. }
  50. }
  51. return 0;
  52. }
  53. static void process_ext_command(foc_cmd_body_t *command) {
  54. if (command->ext_key == 0x1A01) {
  55. return;
  56. }else if (command->ext_key == 0x1A02) {
  57. u8 b0 = decode_u8(command->data);
  58. u8 p_mode = decode_8bits(b0, 0, 1);
  59. if (p_mode == 1) {
  60. if (!mc_start(CTRL_MODE_TRQ)) {
  61. mc_crit_err_add(FOC_START_Err_Code, (s16)(throttle_get_signal()*100.0f), (s16)mot_contrl_get_speed(&motor.controller));
  62. }
  63. }else if (p_mode == 2) {
  64. mc_stop();
  65. }
  66. s8 ext_gear = decode_8bits(b0, 5, 7);
  67. sys_debug("gear %d\n", ext_gear);
  68. if (ext_gear >= 1 && ext_gear <= 4) {
  69. if (ext_gear == 4) {
  70. mc_set_gear(3);
  71. }else {
  72. mc_set_gear(ext_gear - 1);
  73. }
  74. }
  75. u8 b1 = decode_u8((u8 *)command->data + 1);
  76. u8 cruise = decode_8bits(b1, 0, 1);
  77. if (cruise == 2) {
  78. mc_enable_cruise(true);
  79. sys_debug("cruise enable: %d\n", mc_is_cruise_enabled());
  80. }else if (cruise == 1) {
  81. mc_enable_cruise(false);
  82. sys_debug("cruise disable: %d\n", mc_is_cruise_enabled());
  83. }
  84. u8 epm = decode_8bits(b0, 2, 3);
  85. if (epm == 2) {
  86. mc_start_epm(true);
  87. }else if(epm == 1) {
  88. mc_start_epm(false);
  89. }
  90. u8 m_4896 = decode_8bits(b1, 4, 5);
  91. u8 epm_dir = decode_8bits(b1, 6, 7);
  92. if (epm_dir == 0) {
  93. mc_command_epm_move(EPM_Dir_None);
  94. }else if (epm_dir == 1) {
  95. mc_command_epm_move(EPM_Dir_Back);
  96. }else if (epm_dir == 2) {
  97. mc_command_epm_move(EPM_Dir_Forward);
  98. }
  99. u16 cruise_spd = decode_u16((u8 *)command->data + 3);
  100. sys_debug("crui spd %d\n", cruise_spd);
  101. if ((cruise_spd > 0) && (cruise_spd != 0xFFFF)) {
  102. mc_set_cruise_speed(true, (float)cruise_spd * 4.0f);
  103. }
  104. u8 response[8] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
  105. response[0] &= 0xFC;
  106. response[0] |= (mc_is_start()?1:2);
  107. response[0] |= (mc_get_gear() << 5);
  108. response[1] &= 0xC0;
  109. response[1] |= (mc_is_cruise_enabled()?2:1);
  110. response[1] |= (mc_is_epm()?1:2) << 2;
  111. response[1] |= m_4896<<4;
  112. shark_can0_send_ext_message(0x1A024D43, response, sizeof(response));
  113. }else if (command->ext_key == 0x1A05) {
  114. u16 idc_lim = decode_u16((u8 *)command->data) / 10;
  115. sys_debug("idc %d\n", idc_lim);
  116. mc_set_idc_limit((s16)idc_lim);
  117. shark_can0_send_ext_message(0x1A054D43, command->data, command->len);
  118. }
  119. }
  120. static u8 ignore_with_speed[] = {Foc_Set_Adrc_Params, Foc_Set_Gear_Limit, Foc_Conf_Pid, Foc_Set_Throttle_throld, Foc_Set_Config, Foc_Set_eBrake_Throld, Foc_Set_Limiter_Config, Foc_Write_Config, Foc_SN_Write};
  121. static bool _can_process_with_speed(u8 cmd) {
  122. int size = ARRAY_SIZE(ignore_with_speed);
  123. if (!mc_is_start() || mot_contrl_get_speed_abs(&motor.controller) < CONFIG_ZERO_SPEED_RPM) {
  124. return true;
  125. }
  126. for (int i = 0; i < size; i++) {
  127. if (ignore_with_speed[i] == cmd) {
  128. return false;
  129. }
  130. }
  131. return true;
  132. }
  133. static void process_foc_command(foc_cmd_body_t *command) {
  134. u8 erroCode = 0;
  135. u8 response[128];
  136. int len = 3;
  137. if ((command->ext_key != 0) && (command->cmd == 0)) {
  138. process_ext_command(command);
  139. return;
  140. }
  141. if (!_can_process_with_speed(command->cmd)) {
  142. erroCode = FOC_NowAllowed_With_Speed;
  143. goto cmd_end;
  144. }
  145. switch (command->cmd) {
  146. case Foc_Start_Motor:
  147. {
  148. bool success;
  149. foc_start_cmd_t *scmd = (foc_start_cmd_t *)command->data;
  150. sys_debug("start cmd %d\n", scmd->start_stop);
  151. if (scmd->start_stop == Foc_Start) {
  152. success = mc_start(CTRL_MODE_TRQ);
  153. }else if (scmd->start_stop == Foc_Stop) {
  154. success = mc_stop();
  155. }
  156. if (!success) {
  157. erroCode = mot_contrl_get_errcode(&motor.controller);
  158. }else {
  159. if (command->len > sizeof(foc_start_cmd_t)) {
  160. u8 *p = (u8 *)command->data + sizeof(foc_start_cmd_t);
  161. u8 config = decode_u8(p);
  162. u8 ext_gear = config & 0x0f;
  163. sys_debug("gear %d\n", ext_gear);
  164. if (ext_gear >= 1 && ext_gear <= 4) {
  165. if (ext_gear == 4) {
  166. mc_set_gear(3);
  167. }else {
  168. mc_set_gear(ext_gear - 1);
  169. }
  170. }
  171. config = (config >> 4);
  172. mc_enable_brkshutpower(config & 0x01);
  173. mc_enable_tcs((config & 0x02)?true:false);
  174. }
  175. }
  176. sys_debug("start motor %d\n", erroCode);
  177. break;
  178. }
  179. case Foc_IF_Start:
  180. {
  181. if (command->len < 9) {
  182. erroCode = FOC_Param_Err;
  183. }else {
  184. bool start = decode_u8(command->data) == 1?true:false;
  185. s16 accl = decode_s16((u8 *)command->data + 1);
  186. s16 max_vel = decode_s16((u8 *)command->data + 3);
  187. s16 vd = decode_s16((u8 *)command->data + 5);
  188. s16 iq = decode_s16((u8 *)command->data + 7);
  189. sys_debug("IF:%d, %d, %d, %d, %d\n", start, accl, max_vel, vd, iq);
  190. if (!mc_start_IF_mode(start, accl, max_vel, vd, iq)) {
  191. erroCode = FOC_NotAllowed;
  192. }
  193. }
  194. break;
  195. }
  196. case Foc_Set_DQ_Current:
  197. {
  198. #ifdef CONFIG_DQ_STEP_RESPONSE
  199. if (command->len == 2) {
  200. dq_t tgt_dq;
  201. tgt_dq.d = (float)decode_s08(command->data);
  202. tgt_dq.q = (float)decode_s08((u8 *)command->data + 1);
  203. foc_set_target_idq(foc(), &tgt_dq);
  204. sys_debug("step res %f, %f\n", tgt_dq.d, tgt_dq.q);
  205. }else {
  206. erroCode = FOC_Param_Err;
  207. }
  208. #else
  209. erroCode = FOC_NotAllowed;
  210. #endif
  211. break;
  212. }
  213. case Foc_Set_Gear_Mode:
  214. {
  215. u8 gear = decode_u8(command->data);
  216. if (gear > 3) {
  217. erroCode = FOC_Param_Err;
  218. }else {
  219. sys_debug("set gear %d\n", gear);
  220. mc_set_gear(gear);
  221. response[3] = gear;
  222. len += 1;
  223. }
  224. break;
  225. }
  226. case Foc_Set_Cruise_Mode:
  227. {
  228. u8 enable = decode_u8(command->data);
  229. if (!mc_enable_cruise(enable)) {
  230. erroCode = mot_contrl_get_errcode(&motor.controller);
  231. }
  232. break;
  233. }
  234. case Foc_Set_Cruise_Speed:
  235. {
  236. u8 mode = decode_u8(command->data);
  237. float rpm = (float)decode_s16((u8 *)command->data + 1);
  238. if (!mc_set_cruise_speed(mode?true:false, rpm)) {
  239. erroCode = mot_contrl_get_errcode(&motor.controller);
  240. }
  241. sys_debug("Cruise RPM %d\n", (int)rpm);
  242. encode_u16(response + 3, (s16)rpm);
  243. len += 2;
  244. break;
  245. }
  246. case Foc_Set_Ctrl_Mode:
  247. {
  248. u8 mode = decode_u8(command->data);
  249. sys_debug("ctl mode = %d, len = %d\n", mode, command->len);
  250. if (!mc_set_ctrl_mode(mode)) {
  251. erroCode = mot_contrl_get_errcode(&motor.controller);
  252. }
  253. response[len++] = motor.controller.mode_req;
  254. break;
  255. }
  256. case Foc_Set_Gear_Limit:
  257. {
  258. sys_debug("len = %d\n", command->len);
  259. u8 mode = decode_u8(command->data);
  260. if (!mc_conf_set_gear(mode, (u8 *)command->data+1, command->len-1)){
  261. erroCode = FOC_Param_Err;
  262. }
  263. break;
  264. }
  265. case Foc_Get_Gear_Limit:
  266. {
  267. u8 mode = decode_u8(command->data);
  268. u8 *data = os_alloc(256 + 3);
  269. int config_len = mc_conf_get_gear(mode, data + 3);
  270. data[0] = command->cmd;
  271. data[1] = CAN_MY_ADDRESS;
  272. data[2] = mode;
  273. can_send_response(command->can_src, data, config_len + 3);
  274. os_free(data);
  275. return;
  276. }
  277. case Foc_Set_Speed_Limit:
  278. {
  279. s16 speed = decode_s16(((u8 *)command->data));
  280. mot_contrl_set_vel_limit(&motor.controller ,speed);
  281. encode_u16(response + 3, (u16)motor.controller.userlim.mot_vel);
  282. len += 2;
  283. break;
  284. }
  285. case Foc_Set_iDC_Limit:
  286. {
  287. u16 current = decode_u16(((u8 *)command->data));
  288. mc_set_idc_limit((float)current);
  289. encode_u16(response + 3, (u16)motor.controller.userlim.dc_curr);
  290. len += 2;
  291. break;
  292. }
  293. case Foc_Set_Phase_CurrLim:
  294. {
  295. s16 curr = decode_s16(((u8 *)command->data));
  296. mot_contrl_set_torque_limit(&motor.controller, (float)curr);
  297. encode_u16(response + 3, (u16)motor.controller.userlim.torque);
  298. len += 2;
  299. break;
  300. }
  301. case Foc_Cali_Hall_Phase:
  302. {
  303. s16 vd = decode_s16((u8 *)command->data);
  304. sys_debug("cali encoder %d\n", vd);
  305. erroCode = mc_encoder_zero_calibrate(vd)? FOC_Success:FOC_Param_Err;
  306. break;
  307. }
  308. case Foc_Enc_Zero_Cali_Result:
  309. {
  310. #ifndef CONFIG_USE_ENCODER_HALL
  311. response[2] = encoder_get_cali_error()?1:0;
  312. u32 off = encoder_get_cnt_offset();
  313. encode_u32(response + 3, off);
  314. len += 4;
  315. #else
  316. erroCode = FOC_Param_Err;
  317. #endif
  318. break;
  319. }
  320. case Foc_Force_Open_Run:
  321. {
  322. s16 vd = decode_s16((u8 *)command->data);
  323. bool align = false;
  324. if (command->len > 2) {
  325. align = decode_u8((u8 *)command->data + 2)==1?true:false;
  326. }
  327. mc_force_run_open(vd, 0, align);
  328. break;
  329. }
  330. case Foc_Set_Open_Dq_Vol:
  331. {
  332. s16 vd = decode_s16(((u8 *)command->data));
  333. s16 vq = decode_s16(((u8 *)command->data) + 2);
  334. sys_debug("set v_q %d, %d\n", vd, vq);
  335. mot_contrl_set_vdq(&motor.controller, vd, vq);
  336. break;
  337. }
  338. case Foc_Conf_Pid:
  339. {
  340. if (command->len < 13) {
  341. erroCode = FOC_Param_Err;
  342. break;
  343. }
  344. pid_t pid;
  345. u8 id = decode_u8((u8 *)command->data);
  346. mc_conf_decode_pid(&pid, (u8 *)command->data + 1);
  347. sys_debug("set id = %d, kp = %f, ki = %f, kd = %f\n", id, pid.kp, pid.ki, pid.kd);
  348. mc_conf_set_pid(id, &pid);
  349. if (id < PID_Max_ID) {
  350. mot_contrl_set_pid(&motor.controller, id, pid.kp, pid.ki, pid.kd);
  351. }
  352. break;
  353. }
  354. case Foc_Get_Pid:
  355. {
  356. pid_t pid;
  357. u8 id = decode_u8((u8 *)command->data);
  358. if ((id < PID_Max_ID) || (id == PID_EPM_ExtID)) {
  359. mc_conf_get_pid(id, &pid);
  360. erroCode = id;
  361. len += mc_conf_encode_pid(&pid, response + 3);
  362. sys_debug("get id = %d, kp = %f, ki = %f, kd = %f\n", id, pid.kp, pid.ki, pid.kd);
  363. }else {
  364. erroCode = 1;
  365. len = 3;
  366. }
  367. break;
  368. }
  369. case Foc_Set_Adrc_Params:
  370. {
  371. if (command->len < 24) {
  372. erroCode = FOC_Param_Err;
  373. break;
  374. }
  375. /*
  376. nv_get_foc_params()->f_adrc_vel_lim_Wo = decode_float(command->data);
  377. nv_get_foc_params()->f_adrc_vel_lim_Wcv = decode_float((u8 *)command->data + 4);
  378. nv_get_foc_params()->f_adrc_vel_lim_B0 = decode_float((u8 *)command->data + 8);
  379. nv_get_foc_params()->f_adrc_vel_Wo = decode_float((u8 *)command->data + 12);
  380. nv_get_foc_params()->f_adrc_vel_Wcv = decode_float((u8 *)command->data + 16);
  381. nv_get_foc_params()->f_adrc_vel_B0 = decode_float((u8 *)command->data + 20);
  382. nv_save_foc_params(); */
  383. break;
  384. }
  385. case Foc_Get_Adrc_Params:
  386. {
  387. /*encode_float(response+3, nv_get_foc_params()->f_adrc_vel_lim_Wo);
  388. encode_float(response+7, nv_get_foc_params()->f_adrc_vel_lim_Wcv);
  389. encode_float(response+11, nv_get_foc_params()->f_adrc_vel_lim_B0);
  390. encode_float(response+15, nv_get_foc_params()->f_adrc_vel_Wo);
  391. encode_float(response+19, nv_get_foc_params()->f_adrc_vel_Wcv);
  392. encode_float(response+23, nv_get_foc_params()->f_adrc_vel_B0); */
  393. len += 24;
  394. break;
  395. }
  396. case Foc_Set_EPM_Mode:
  397. {
  398. bool mode = decode_u8((u8 *)command->data) == 0?false:true;
  399. if (!mc_start_epm(mode)) {
  400. erroCode = mot_contrl_get_errcode(&motor.controller);
  401. }
  402. break;
  403. }
  404. case Foc_Set_Thro_Ration:
  405. {
  406. if (command->len >= 2) {
  407. bool use = decode_u8(command->data)==0?false:true;
  408. u8 r = decode_u8((u8 *)command->data + 1);
  409. sys_debug("set thro %d, r: %d\n", use, r);
  410. mc_set_throttle_r(use, r);
  411. }
  412. break;
  413. }
  414. case Foc_Lock_Motor:
  415. {
  416. u8 lock = decode_u8((u8 *)command->data);
  417. if (lock == Foc_Start) {
  418. mc_lock_motor(true);
  419. }else {
  420. mc_lock_motor(false);
  421. }
  422. erroCode = mot_contrl_get_errcode(&motor.controller);
  423. break;
  424. }
  425. case Foc_Auto_Hold:
  426. {
  427. u8 hold = decode_u8((u8 *)command->data);
  428. if (hold == Foc_Start) {
  429. mc_auto_hold(true);
  430. }else {
  431. mc_auto_hold(false);
  432. }
  433. erroCode = mot_contrl_get_errcode(&motor.controller);
  434. break;
  435. }
  436. case Foc_Start_EPM_Move:
  437. {
  438. epm_dir_t dir = (epm_dir_t)decode_u8((u8 *)command->data);
  439. if(!mc_command_epm_move(dir)) {
  440. erroCode = mot_contrl_get_errcode(&motor.controller);
  441. }
  442. break;
  443. }
  444. case Foc_Start_DQ_Calibrate:
  445. {
  446. u8 start = decode_u8((u8 *)command->data);
  447. if (start == 1) {
  448. sys_debug("start mpta cali\n");
  449. if (mc_set_ctrl_mode(CTRL_MODE_CURRENT)) {
  450. mot_contrl_mtpa_calibrate(&motor.controller, true);
  451. }
  452. }else {
  453. mot_contrl_mtpa_calibrate(&motor.controller, false);
  454. mc_set_ctrl_mode(CTRL_MODE_TRQ);
  455. }
  456. break;
  457. }
  458. case Foc_Set_IS_Curr_Angle:
  459. {
  460. if (command->len != 4) {
  461. erroCode = FOC_Param_Err;
  462. }else {
  463. s16 is_curr = decode_s16((u8 *)command->data);
  464. s16 is_angle = decode_s16((u8 *)command->data + 2);
  465. sys_debug("curr %d, angle %d\n", is_curr, is_angle);
  466. mot_contrl_set_current(&motor.controller, is_curr);
  467. mot_contrl_set_adv_angle(&motor.controller, is_angle);
  468. }
  469. break;
  470. }
  471. case Foc_Set_Plot_Type:
  472. {
  473. plot_type = (int)decode_u8((u8 *)command->data);
  474. #if (CONFIG_ENABLE_IAB_REC==1)
  475. if (plot_type == 6) {
  476. mc_start_current_rec(true, true);
  477. }else if (plot_type == 11) {
  478. mc_start_current_rec(false, true);
  479. }else if (plot_type == 0) {
  480. mc_start_current_rec(false, false);
  481. }
  482. #endif
  483. sys_debug("plot type %d\n", plot_type);
  484. break;
  485. }
  486. case Foc_Set_Throttle_throld:
  487. {
  488. if (mc_is_start()) {
  489. erroCode = FOC_NotAllowed;
  490. }else {
  491. u16 start = decode_u16((u8 *)command->data);
  492. u16 end = decode_u16((u8 *)command->data + 2);
  493. mc_conf()->c.thro_start_vol = (float)start/100.0f;
  494. mc_conf()->c.thro_end_vol = (float)end/100.0f;
  495. mc_conf_save();
  496. }
  497. break;
  498. }
  499. case Foc_Set_Config:
  500. {
  501. u8 conf_cmd = decode_u8(command->data);
  502. sys_debug("cmd %d\n", conf_cmd);
  503. if (conf_cmd == 1) { //start
  504. int len = decode_u16((u8 *)command->data+1);
  505. if (!mc_conf_begin_recv(len)) {
  506. erroCode = FOC_MEM_Err;
  507. }
  508. }else if (conf_cmd == 2) { //finish
  509. u16 crc = decode_u16((u8 *)command->data+1);
  510. if (!mc_conf_finish_recv(crc)) {
  511. erroCode = FOC_CRC_Err;
  512. }else {
  513. shark_timer_post(&_reboot_timer, 200);
  514. }
  515. }else if (conf_cmd == 0) { //recv config data
  516. int offset = decode_u16((u8 *)command->data+1);
  517. sys_debug("offset = %d\n", offset);
  518. if (!mc_conf_recv_data((u8 *)command->data + 3, offset, command->len - 3)){
  519. erroCode = FOC_Param_Err;
  520. }
  521. }else {
  522. erroCode = FOC_Unknow_Cmd;
  523. }
  524. break;
  525. }
  526. case Foc_Get_Config:
  527. {
  528. int offset = decode_u16((u8 *)command->data);
  529. if (offset == 0) {
  530. response[3] = 1;
  531. }else {
  532. response[3] = 0;
  533. }
  534. len += 1;
  535. int ret = mc_conf_send_data(response + 4, offset, sizeof(response) - 4);
  536. if (ret == -2) {
  537. erroCode = FOC_MEM_Err;
  538. }else if (ret == -1) {
  539. erroCode = FOC_Param_Err;
  540. }else if (ret == 0){
  541. response[3] = 2;
  542. len += mc_conf_finish_send(response + 4);
  543. }else {
  544. len += ret;
  545. }
  546. break;
  547. }
  548. case Foc_Fan_Duty:
  549. {
  550. u8 duty = decode_u8(command->data);
  551. mc_set_fan_duty(duty);
  552. break;
  553. }
  554. case Foc_Set_eBrake_Throld:
  555. {
  556. if (command->len >= 1) {
  557. u8 level = decode_u8((u8 *)command->data);
  558. mc_set_ebrk_level(level);
  559. }
  560. break;
  561. }
  562. case Foc_Use_SensorLess_Angle:
  563. {
  564. bool sensorless = decode_u8((u8 *)command->data)?true:false;
  565. sys_debug("use smo %d\n", sensorless);
  566. #if 1
  567. if (sensorless && mc_is_start() && foc_observer_sensorless_stable()) {
  568. sys_debug("use smo %d\n", sensorless);
  569. foc_observer_use_sensorless(sensorless);
  570. }else {
  571. sys_debug("unuse smo\n");
  572. foc_observer_use_sensorless(false);
  573. }
  574. #else
  575. if (sensorless && mc_is_start()){
  576. motor_encoder_produce_error(sensorless);
  577. }else {
  578. motor_encoder_produce_error(false);
  579. }
  580. #endif
  581. break;
  582. }
  583. case Foc_Set_Limiter_Config:
  584. {
  585. sys_debug("limter %d\n", command->len);
  586. if (!mc_conf_set_limter((u8 *)command->data, command->len)) {
  587. erroCode = FOC_Param_Err;
  588. }
  589. break;
  590. }
  591. case Foc_Get_Limiter_Config:
  592. {
  593. u8 *data = os_alloc(256 + 3);
  594. int config_len = mc_conf_get_limter(data + 3);
  595. data[0] = command->cmd;
  596. data[1] = CAN_MY_ADDRESS;
  597. data[2] = 0;
  598. can_send_response(command->can_src, data, config_len+3);
  599. os_free(data);
  600. return;
  601. }
  602. case Foc_SN_Write:
  603. {
  604. if (command->len < 18) {
  605. erroCode = FOC_Param_Err;
  606. }else{
  607. erroCode = nv_write_sn((u8 *)command->data, command->len)>0?0:1 ;
  608. }
  609. break;
  610. }
  611. case Foc_SN_Read:
  612. {
  613. if (nv_read_sn(response + 3, 18) == 0) {
  614. memset(response + 3, '0', 18);
  615. }
  616. len += 18;
  617. break;
  618. }
  619. case Foc_Get_MC_NV_Crit_Err:
  620. {
  621. s16 offset = decode_s16((u8 *)command->data);
  622. len += mc_crit_err_get(offset, response+3, sizeof(response) - 3);
  623. break;
  624. }
  625. case Foc_Get_MC_NV_Err_RT:
  626. {
  627. s16 offset = decode_s16((u8 *)command->data);
  628. len += mc_err_runtime_get(offset, response+3, sizeof(response) - 3);
  629. break;
  630. }
  631. case Foc_Set_LogLevel:
  632. {
  633. u8 level = decode_u8((u8 *)command->data);
  634. _pc_connect = (level != 0)?true:false;
  635. set_log_level(MOD_SYSTEM, (level != 0)?L_debug:L_disable);
  636. break;
  637. }
  638. case Foc_MotPara_Ind:
  639. {
  640. _pc_connect = true;
  641. set_log_level(MOD_SYSTEM, L_debug);
  642. bool start = decode_u8((u8 *)command->data)?true:false;
  643. if (!start) {
  644. mc_ind_motor_start(start);
  645. mot_params_ind_stop();
  646. sys_debug("stop mot ind\n");
  647. }else {
  648. u8 type = decode_u8((u8 *)command->data + 1);
  649. sys_debug("start mot ind %d\n", type);
  650. if (type == R_TYPE) { // rs ind
  651. u8 v_max = decode_u8((u8 *)command->data + 2);
  652. u8 i_max = decode_u8((u8 *)command->data + 3);
  653. u8 time = decode_u8((u8 *)command->data + 4);
  654. sys_debug("rs ind %d, %d, %d\n", v_max, i_max, time);
  655. if (mc_ind_motor_start(true)) {
  656. mot_params_ind_rs((float)v_max, (float)i_max, (s32)time);
  657. }
  658. }else if (type == L_TYPE_D || type == L_TYPE_Q) { //ld/lq ind
  659. u8 v = decode_u8((u8 *)command->data + 2);
  660. u16 freq = decode_u16((u8 *)command->data + 3);
  661. sys_debug("ldq ind %d, %d\n", v, freq);
  662. if (mc_ind_motor_start(true)) {
  663. if (type == L_TYPE_D) {
  664. mot_params_ind_ld((float)v, (float)freq);
  665. }else {
  666. mot_params_ind_lq((float)v, (float)freq);
  667. }
  668. }
  669. }else if (type == FLUX_TYPE) {
  670. u8 iq = decode_u8((u8 *)command->data + 2);
  671. sys_debug("ind flux iq = %d\n", iq);
  672. if (mc_ind_motor_start(true)) {
  673. mot_params_ind_flux(0, (float)iq);
  674. }
  675. }else{
  676. erroCode = FOC_Param_Err;
  677. }
  678. }
  679. break;
  680. }
  681. case Foc_Set_Speed_Target:
  682. {
  683. if (command->len == 2) {
  684. s16 tgt_speed = decode_s16((u8 *)command->data);
  685. mc_set_target_vel(tgt_speed);
  686. }
  687. break;
  688. }
  689. case Foc_Set_Force_Torque:
  690. {
  691. if (command->len == 2) {
  692. s16 tgt_torque = decode_s16((u8 *)command->data);
  693. mc_set_force_torque(tgt_torque);
  694. sys_debug("torque:%d-%d\n", tgt_torque, motor.s_force_torque);
  695. }
  696. break;
  697. }
  698. default:
  699. {
  700. erroCode = FOC_Unknow_Cmd;
  701. break;
  702. }
  703. }
  704. cmd_end:
  705. sys_debug("err = %d\n", erroCode);
  706. response[0] = command->cmd;
  707. response[1] = CAN_MY_ADDRESS;
  708. response[2] = erroCode;
  709. can_send_response(command->can_src, response, len);
  710. }
  711. static void _reboot_timer_handler(shark_timer_t *t) {
  712. system_reboot();
  713. }