commands.c 18 KB

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