commands.c 18 KB

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