commands.c 19 KB

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