commands.c 17 KB

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