commands.c 17 KB

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