commands.c 19 KB

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