commands.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722
  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. #ifdef CONFIG_DQ_STEP_RESPONSE
  15. extern float target_d;
  16. extern float target_q;
  17. #endif
  18. extern int plot_type;
  19. static void _reboot_timer_handler(shark_timer_t *);
  20. static shark_timer_t _reboot_timer = TIMER_INIT(_reboot_timer, _reboot_timer_handler);
  21. static u32 foc_command_task(void *args);
  22. static void process_foc_command(foc_cmd_body_t *command);
  23. static co_queue_t _cmd_queue;
  24. static bool _pc_connect = false;
  25. bool can_is_connect_pc(void) {
  26. return _pc_connect;
  27. }
  28. void can_debug(bool enable) {
  29. _pc_connect = enable;
  30. set_log_level(MOD_SYSTEM, enable?L_debug:L_disable);
  31. }
  32. void foc_command_init(void) {
  33. _cmd_queue = queue_create(16, sizeof(foc_cmd_body_t));
  34. shark_task_create(foc_command_task, NULL);
  35. }
  36. bool foc_send_command(foc_cmd_body_t *command) {
  37. if (!queue_put(_cmd_queue, command)) {
  38. if (command->data) {
  39. os_free(command->data);
  40. }
  41. return false;
  42. }
  43. return true;
  44. }
  45. static u32 foc_command_task(void *args) {
  46. foc_cmd_body_t command;
  47. if (queue_get(_cmd_queue, &command)) {
  48. process_foc_command(&command);
  49. if (command.data) {
  50. os_free(command.data);
  51. }
  52. }
  53. return 0;
  54. }
  55. static void process_ext_command(foc_cmd_body_t *command) {
  56. if (command->ext_key == 0x1A01) {
  57. return;
  58. }else if (command->ext_key == 0x1A02) {
  59. u8 b0 = decode_u8(command->data);
  60. u8 p_mode = decode_8bits(b0, 0, 1);
  61. if (p_mode == 1) {
  62. mc_start(CTRL_MODE_TRQ);
  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_End_Write_TRQ_Table, 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() || PMSM_FOC_GetSpeed() < 0.1f) {
  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 = PMSM_FOC_GetErrCode();
  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_Set_DQ_Current:
  180. {
  181. #ifdef CONFIG_DQ_STEP_RESPONSE
  182. if (command->len == 2) {
  183. target_d = (float)decode_s08(command->data);
  184. target_q = (float)decode_s08((u8 *)command->data + 1);
  185. sys_debug("step res %f, %f\n", target_d, target_q);
  186. }else {
  187. erroCode = FOC_Param_Err;
  188. }
  189. #else
  190. erroCode = FOC_NotAllowed;
  191. #endif
  192. break;
  193. }
  194. case Foc_Set_Gear_Mode:
  195. {
  196. u8 gear = decode_u8(command->data);
  197. if (gear > 3) {
  198. erroCode = FOC_Param_Err;
  199. }else {
  200. sys_debug("set gear %d\n", gear);
  201. mc_set_gear(gear);
  202. response[3] = gear;
  203. len += 1;
  204. }
  205. break;
  206. }
  207. case Foc_Set_Cruise_Mode:
  208. {
  209. u8 enable = decode_u8(command->data);
  210. if (!mc_enable_cruise(enable)) {
  211. erroCode = PMSM_FOC_GetErrCode();
  212. }
  213. break;
  214. }
  215. case Foc_Set_Cruise_Speed:
  216. {
  217. u8 mode = decode_u8(command->data);
  218. float rpm = (float)decode_s16((u8 *)command->data + 1);
  219. if (!mc_set_cruise_speed(mode?true:false, rpm)) {
  220. erroCode = PMSM_FOC_GetErrCode();
  221. }
  222. sys_debug("Cruise RPM %d\n", (int)rpm);
  223. encode_u16(response + 3, (s16)rpm);
  224. len += 2;
  225. break;
  226. }
  227. case Foc_Set_Ctrl_Mode:
  228. {
  229. u8 mode = decode_u8(command->data);
  230. sys_debug("ctl mode = %d, len = %d\n", mode, command->len);
  231. if (!mc_set_foc_mode(mode)) {
  232. erroCode = PMSM_FOC_GetErrCode();
  233. }
  234. response[len++] = PMSM_FOC_GetCtrlMode();
  235. break;
  236. }
  237. case Foc_Set_Gear_Limit:
  238. {
  239. sys_debug("len = %d\n", command->len);
  240. u8 mode = decode_u8(command->data);
  241. if (!nv_set_gear_config(mode, (u8 *)command->data+1, command->len-1)){
  242. erroCode = FOC_Param_Err;
  243. }
  244. break;
  245. }
  246. case Foc_Get_Gear_Limit:
  247. {
  248. u8 mode = decode_u8(command->data);
  249. int config_len = 0;
  250. void *config = nv_get_gear_config(mode, &config_len);
  251. u8 *data = os_alloc(config_len + 3);
  252. data[0] = command->cmd;
  253. data[1] = CAN_MY_ADDRESS;
  254. data[2] = 0;
  255. memcpy(data + 3, config, config_len);
  256. can_send_response(command->can_src, data, config_len + 3);
  257. os_free(data);
  258. return;
  259. }
  260. case Foc_Set_Speed_Limit:
  261. {
  262. s16 speed = decode_s16(((u8 *)command->data));
  263. PMSM_FOC_SpeedLimit(speed);
  264. encode_u16(response + 3, (u16)PMSM_FOC_GetSpeedLimit());
  265. len += 2;
  266. break;
  267. }
  268. case Foc_Set_iDC_Limit:
  269. {
  270. u16 current = decode_u16(((u8 *)command->data));
  271. mc_set_idc_limit((float)current);
  272. encode_u16(response + 3, (u16)PMSM_FOC_GetDCCurrLimit());
  273. len += 2;
  274. break;
  275. }
  276. case Foc_Set_Phase_CurrLim:
  277. {
  278. s16 curr = decode_s16(((u8 *)command->data));
  279. PMSM_FOC_PhaseCurrLim((float)curr);
  280. encode_u16(response + 3, (u16)PMSM_FOC_GetPhaseCurrLim());
  281. len += 2;
  282. break;
  283. }
  284. case Foc_Cali_Hall_Phase:
  285. {
  286. s16 vd = decode_s16((u8 *)command->data);
  287. sys_debug("cali encoder %d\n", vd);
  288. erroCode = mc_encoder_zero_calibrate(vd)? FOC_Success:FOC_Param_Err;
  289. break;
  290. }
  291. case Foc_Enc_Zero_Cali_Result:
  292. {
  293. response[2] = encoder_get_cali_error()?1:0;
  294. u32 off = encoder_get_cnt_offset();
  295. encode_u32(response + 3, off);
  296. len += 4;
  297. break;
  298. }
  299. case Foc_Force_Open_Run:
  300. {
  301. s16 vd = decode_s16((u8 *)command->data);
  302. mc_force_run_open(vd, 0);
  303. break;
  304. }
  305. case Foc_Set_Open_Dq_Vol:
  306. {
  307. s16 vd = decode_s16(((u8 *)command->data));
  308. s16 vq = decode_s16(((u8 *)command->data) + 2);
  309. sys_debug("set v_q %d, %d\n", vd, vq);
  310. PMSM_FOC_SetOpenVdq(vd, (vq));
  311. break;
  312. }
  313. case Foc_Conf_Pid:
  314. {
  315. if (command->len < 13) {
  316. erroCode = FOC_Param_Err;
  317. break;
  318. }
  319. pid_conf_t pid;
  320. u8 id = decode_u8((u8 *)command->data);
  321. memcpy((char *)&pid, (char *)command->data + 1, sizeof(pid_conf_t));
  322. sys_debug("set id = %d, kp = %f, ki = %f, kd = %f\n", id, pid.kp, pid.ki, pid.kd);
  323. PMSM_FOC_SetPid(id, pid.kp, pid.ki, pid.kd);
  324. nv_set_pid(id, &pid);
  325. break;
  326. }
  327. case Foc_Get_Pid:
  328. {
  329. pid_conf_t pid;
  330. u8 id = decode_u8((u8 *)command->data);
  331. if (id < PID_Max_id) {
  332. nv_get_pid(id, &pid);
  333. erroCode = id;
  334. memcpy(response+3, &pid, sizeof(pid));
  335. len = sizeof(pid) + 3;
  336. sys_debug("get id = %d, kp = %f, ki = %f, kd = %f\n", id, pid.kp, pid.ki, pid.kd);
  337. }else {
  338. erroCode = 1;
  339. len = 3;
  340. }
  341. break;
  342. }
  343. case Foc_Set_Adrc_Params:
  344. {
  345. if (command->len < 24) {
  346. erroCode = FOC_Param_Err;
  347. break;
  348. }
  349. nv_get_foc_params()->f_adrc_vel_lim_Wo = decode_float(command->data);
  350. nv_get_foc_params()->f_adrc_vel_lim_Wcv = decode_float((u8 *)command->data + 4);
  351. nv_get_foc_params()->f_adrc_vel_lim_B0 = decode_float((u8 *)command->data + 8);
  352. nv_get_foc_params()->f_adrc_vel_Wo = decode_float((u8 *)command->data + 12);
  353. nv_get_foc_params()->f_adrc_vel_Wcv = decode_float((u8 *)command->data + 16);
  354. nv_get_foc_params()->f_adrc_vel_B0 = decode_float((u8 *)command->data + 20);
  355. nv_save_foc_params();
  356. break;
  357. }
  358. case Foc_Get_Adrc_Params:
  359. {
  360. encode_float(response+3, nv_get_foc_params()->f_adrc_vel_lim_Wo);
  361. encode_float(response+7, nv_get_foc_params()->f_adrc_vel_lim_Wcv);
  362. encode_float(response+11, nv_get_foc_params()->f_adrc_vel_lim_B0);
  363. encode_float(response+15, nv_get_foc_params()->f_adrc_vel_Wo);
  364. encode_float(response+19, nv_get_foc_params()->f_adrc_vel_Wcv);
  365. encode_float(response+23, nv_get_foc_params()->f_adrc_vel_B0);
  366. len += 24;
  367. break;
  368. }
  369. case Foc_Set_EPM_Mode:
  370. {
  371. bool mode = decode_u8((u8 *)command->data) == 0?false:true;
  372. if (!mc_start_epm(mode)) {
  373. erroCode = PMSM_FOC_GetErrCode();
  374. }
  375. break;
  376. }
  377. case Foc_Set_Thro_Ration:
  378. {
  379. if (command->len >= 2) {
  380. bool use = decode_u8(command->data)==0?false:true;
  381. u8 r = decode_u8((u8 *)command->data + 1);
  382. mc_set_throttle_r(use, r);
  383. }
  384. break;
  385. }
  386. case Foc_Lock_Motor:
  387. {
  388. u8 lock = decode_u8((u8 *)command->data);
  389. if (lock == Foc_Start) {
  390. mc_lock_motor(true);
  391. }else {
  392. mc_lock_motor(false);
  393. }
  394. erroCode = PMSM_FOC_GetErrCode();
  395. break;
  396. }
  397. case Foc_Auto_Hold:
  398. {
  399. u8 hold = decode_u8((u8 *)command->data);
  400. if (hold == Foc_Start) {
  401. mc_auto_hold(true);
  402. }else {
  403. mc_auto_hold(false);
  404. }
  405. erroCode = PMSM_FOC_GetErrCode();
  406. break;
  407. }
  408. case Foc_Start_EPM_Move:
  409. {
  410. EPM_Dir_t dir = (EPM_Dir_t)decode_u8((u8 *)command->data);
  411. if(!mc_command_epm_move(dir)) {
  412. erroCode = PMSM_FOC_GetErrCode();
  413. }
  414. break;
  415. }
  416. case Foc_Start_DQ_Calibrate:
  417. {
  418. u8 start = decode_u8((u8 *)command->data);
  419. if (start == Foc_Start) {
  420. sys_debug("start mpta cali\n");
  421. mc_set_foc_mode(CTRL_MODE_CURRENT);
  422. PMSM_FOC_MTPA_Calibrate(true);
  423. }else {
  424. PMSM_FOC_MTPA_Calibrate(false);
  425. mc_set_foc_mode(CTRL_MODE_TRQ);
  426. }
  427. break;
  428. }
  429. case Foc_Set_IS_Curr_Angle:
  430. {
  431. if (command->len != 4) {
  432. erroCode = FOC_Param_Err;
  433. }else {
  434. s16 is_curr = decode_s16((u8 *)command->data);
  435. s16 is_angle = decode_s16((u8 *)command->data + 2);
  436. sys_debug("curr %d, angle %d\n", is_curr, is_angle);
  437. PMSM_FOC_Set_Current(is_curr);
  438. PMSM_FOC_Set_Dq_Angle(is_angle);
  439. }
  440. break;
  441. }
  442. case Foc_Set_Plot_Type:
  443. {
  444. plot_type = (int)decode_u8((u8 *)command->data);
  445. #if (CONFIG_ENABLE_IAB_REC==1)
  446. if (plot_type == 6) {
  447. mc_start_current_rec(true);
  448. }else if (plot_type == 0) {
  449. mc_start_current_rec(false);
  450. }
  451. #endif
  452. sys_debug("plot type %d\n", plot_type);
  453. break;
  454. }
  455. case Foc_Set_Throttle_throld:
  456. {
  457. if (mc_is_start()) {
  458. erroCode = FOC_NotAllowed;
  459. }else {
  460. u16 start = decode_u16((u8 *)command->data);
  461. u16 end = decode_u16((u8 *)command->data + 2);
  462. nv_get_foc_params()->n_startThroVol = (float)start/100.0f;
  463. nv_get_foc_params()->n_endThroVol = (float)end/100.0f;
  464. nv_save_foc_params();
  465. }
  466. break;
  467. }
  468. case Foc_Get_Config:
  469. {
  470. int config_len = 128;
  471. int len = 0;
  472. u8 *config = os_alloc(config_len);
  473. if (config == NULL) {
  474. erroCode = FOC_MEM_Err;
  475. break;
  476. }
  477. config[0] = command->cmd;
  478. config[1] = CAN_MY_ADDRESS;
  479. config[2] = 0;
  480. len = 3;
  481. encode_s16(config+len, (s16)nv_get_foc_params()->s_PhaseCurrLim);
  482. len += 2;
  483. encode_s16(config+len, (s16)nv_get_foc_params()->s_maxDCVol);
  484. len += 2;
  485. encode_s16(config+len, (s16)nv_get_foc_params()->s_minDCVol);
  486. len += 2;
  487. encode_s16(config+len, (s16)nv_get_foc_params()->s_maxRPM);
  488. len += 2;
  489. encode_s16(config+len, (s16)nv_get_foc_params()->s_maxEpmRPM);
  490. len += 2;
  491. encode_s16(config+len, (s16)nv_get_foc_params()->s_maxEpmTorqueLim);
  492. len += 2;
  493. encode_s16(config+len, (s16)nv_get_foc_params()->s_maxTorque);
  494. len += 2;
  495. encode_s16(config+len, (s16)nv_get_foc_params()->s_TorqueBrkLim);
  496. len += 2;
  497. encode_s16(config+len, (s16)nv_get_foc_params()->s_iDCeBrkLim);
  498. len += 2;
  499. encode_s16(config+len, (s16)nv_get_foc_params()->s_LimitiDC);
  500. len += 2;
  501. encode_s16(config+len, (s16)(nv_get_foc_params()->n_startThroVol * 100.0f));
  502. len += 2;
  503. encode_s16(config+len, (s16)(nv_get_foc_params()->n_endThroVol * 100.0f));
  504. len += 2;
  505. encode_u8(config+len, (u8)nv_get_foc_params()->n_brkShutPower);
  506. len += 1;
  507. encode_u8(config+len, (u8)nv_get_foc_params()->n_autoHold);
  508. len += 1;
  509. encode_u32(config+len, nv_get_foc_params()->n_dec_time);
  510. len += 4;
  511. encode_u32(config+len, nv_get_foc_params()->n_ebrk_time);
  512. len += 4;
  513. encode_s16(config+len, nv_get_foc_params()->s_maxEpmRPMBck);
  514. len += 2;
  515. encode_s16(config+len, nv_get_foc_params()->s_maxEpmTorqueLimBck);
  516. len += 2;
  517. can_send_response(command->can_src, config, len);
  518. os_free(config);
  519. return;
  520. }
  521. case Foc_Set_Config:
  522. {
  523. if (mc_is_start()) {
  524. erroCode = FOC_NotAllowed;
  525. }else if (command->len < 28) {
  526. erroCode = FOC_Param_Err;
  527. }else {
  528. nv_get_foc_params()->s_TorqueBrkLim = decode_s16((u8 *)command->data);
  529. nv_get_foc_params()->s_iDCeBrkLim = decode_s16((u8 *)command->data + 2);
  530. nv_get_foc_params()->s_LimitiDC = decode_s16((u8 *)command->data + 4);
  531. nv_get_foc_params()->n_startThroVol = (float)decode_s16((u8 *)command->data + 6)/100.0f;
  532. nv_get_foc_params()->n_endThroVol = (float)decode_s16((u8 *)command->data + 8)/100.0f;
  533. nv_get_foc_params()->s_maxEpmRPM = decode_s16((u8 *)command->data + 10);
  534. nv_get_foc_params()->s_maxEpmTorqueLim = decode_s16((u8 *)command->data + 12);
  535. nv_get_foc_params()->n_brkShutPower = decode_u8((u8 *)command->data + 14);
  536. nv_get_foc_params()->n_autoHold = decode_u8((u8 *)command->data + 15);
  537. nv_get_foc_params()->n_dec_time = decode_u32((u8 *)command->data + 16);
  538. nv_get_foc_params()->n_ebrk_time = decode_u32((u8 *)command->data + 20);
  539. nv_get_foc_params()->s_maxEpmRPMBck = decode_s16((u8 *)command->data + 24);
  540. nv_get_foc_params()->s_maxEpmTorqueLimBck = decode_s16((u8 *)command->data + 26);
  541. nv_save_foc_params();
  542. shark_timer_post(&_reboot_timer, 200);
  543. }
  544. break;
  545. }
  546. case Foc_Fan_Duty:
  547. {
  548. u8 duty = decode_u8(command->data);
  549. mc_set_fan_duty(duty);
  550. break;
  551. }
  552. case Foc_Set_eBrake_Throld:
  553. {
  554. if (command->len >= 4) {
  555. s16 torque = decode_s16((u8 *)command->data);
  556. u16 time = decode_u16((u8 *)command->data + 2);
  557. mc_set_ebrk_level(torque, time);
  558. }
  559. break;
  560. }
  561. case Foc_Use_SensorLess_Angle:
  562. {
  563. bool sensorless = decode_u8((u8 *)command->data)?true:false;
  564. sys_debug("use smo %d\n", sensorless);
  565. #if 1
  566. if (sensorless && mc_is_start() && foc_observer_sensorless_stable()) {
  567. sys_debug("use smo %d\n", sensorless);
  568. foc_observer_use_sensorless(sensorless);
  569. }else {
  570. sys_debug("unuse smo\n");
  571. foc_observer_use_sensorless(false);
  572. }
  573. #else
  574. if (sensorless && mc_is_start()){
  575. motor_encoder_produce_error(sensorless);
  576. }else {
  577. motor_encoder_produce_error(false);
  578. }
  579. #endif
  580. break;
  581. }
  582. case Foc_Set_Limiter_Config:
  583. {
  584. sys_debug("limter %d\n", command->len);
  585. if (!nv_set_limit_config((u8 *)command->data, command->len)) {
  586. erroCode = FOC_Param_Err;
  587. }
  588. break;
  589. }
  590. case Foc_Get_Limiter_Config:
  591. {
  592. int config_len;
  593. u8 *config = nv_get_limit_config(&config_len);
  594. u8 *data = os_alloc(config_len + 3);
  595. data[0] = command->cmd;
  596. data[1] = CAN_MY_ADDRESS;
  597. data[2] = 0;
  598. memcpy(data + 3, config, config_len);
  599. can_send_response(command->can_src, data, config_len+3);
  600. os_free(data);
  601. return;
  602. }
  603. case Foc_SN_Write:
  604. {
  605. if (command->len < 18) {
  606. erroCode = FOC_Param_Err;
  607. }else{
  608. erroCode = nv_write_sn((u8 *)command->data, command->len)>0?0:1 ;
  609. }
  610. break;
  611. }
  612. case Foc_SN_Read:
  613. {
  614. if (nv_read_sn(response + 3, 18) == 0) {
  615. memset(response + 3, '0', 18);
  616. }
  617. len += 18;
  618. break;
  619. }
  620. case Foc_Get_MC_NV_Crit_Err:
  621. {
  622. s16 offset = decode_s16((u8 *)command->data);
  623. len += mc_crit_err_get(offset, response+3, sizeof(response) - 3);
  624. break;
  625. }
  626. case Foc_Get_MC_NV_Err_RT:
  627. {
  628. s16 offset = decode_s16((u8 *)command->data);
  629. len += mc_err_runtime_get(offset, response+3, sizeof(response) - 3);
  630. break;
  631. }
  632. case Foc_Set_LogLevel:
  633. {
  634. u8 level = decode_u8((u8 *)command->data);
  635. _pc_connect = (level != 0)?true:false;
  636. set_log_level(MOD_SYSTEM, (level != 0)?L_debug:L_disable);
  637. break;
  638. }
  639. case Foc_MotPara_Ind:
  640. {
  641. _pc_connect = true;
  642. set_log_level(MOD_SYSTEM, L_debug);
  643. bool start = decode_u8((u8 *)command->data)?true:false;
  644. if (!start) {
  645. mc_ind_motor_start(start);
  646. mot_params_ind_stop();
  647. sys_debug("stop mot ind\n");
  648. }else {
  649. u8 type = decode_u8((u8 *)command->data + 1);
  650. sys_debug("start mot ind %d\n", type);
  651. if (type == R_TYPE) { // rs ind
  652. u8 v_max = decode_u8((u8 *)command->data + 2);
  653. u8 i_max = decode_u8((u8 *)command->data + 3);
  654. u8 time = decode_u8((u8 *)command->data + 4);
  655. sys_debug("rs ind %d, %d, %d\n", v_max, i_max, time);
  656. if (mc_ind_motor_start(true)) {
  657. mot_params_ind_rs((float)v_max, (float)i_max, (s32)time);
  658. }
  659. }else if (type == L_TYPE_D || type == L_TYPE_Q) { //ld/lq ind
  660. u8 v = decode_u8((u8 *)command->data + 2);
  661. u16 freq = decode_u16((u8 *)command->data + 3);
  662. sys_debug("ldq ind %d, %d\n", v, freq);
  663. if (mc_ind_motor_start(true)) {
  664. if (type == L_TYPE_D) {
  665. mot_params_ind_ld((float)v, (float)freq);
  666. }else {
  667. mot_params_ind_lq((float)v, (float)freq);
  668. }
  669. }
  670. }else if (type == FLUX_TYPE) {
  671. u8 iq = decode_u8((u8 *)command->data + 2);
  672. sys_debug("ind flux iq = %d\n", iq);
  673. if (mc_ind_motor_start(true)) {
  674. mot_params_ind_flux(0, (float)iq);
  675. }
  676. }else{
  677. erroCode = FOC_Param_Err;
  678. }
  679. }
  680. break;
  681. }
  682. default:
  683. {
  684. erroCode = FOC_Unknow_Cmd;
  685. break;
  686. }
  687. }
  688. cmd_end:
  689. sys_debug("err = %d\n", erroCode);
  690. response[0] = command->cmd;
  691. response[1] = CAN_MY_ADDRESS;
  692. response[2] = erroCode;
  693. can_send_response(command->can_src, response, len);
  694. }
  695. static void _reboot_timer_handler(shark_timer_t *t) {
  696. system_reboot();
  697. }