commands.c 18 KB

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