commands.c 19 KB

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