mc_config.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  1. #include "foc/mc_config.h"
  2. #include "foc/core/controller.h"
  3. #include "libs/utils.h"
  4. #include "libs/crc16.h"
  5. #include "app/nv_storage.h"
  6. #include "libs/logger.h"
  7. #include "foc/mc_error.h"
  8. mc_config conf;
  9. void mc_conf_default(void);
  10. static u8 *conf_buff = NULL;
  11. static int conf_len = 0;
  12. static int conf_idx = 0;
  13. bool mc_conf_begin_recv(int len) {
  14. if (conf_buff) {
  15. os_free(conf_buff);
  16. conf_buff = NULL;
  17. }
  18. if (len > sizeof(mc_config)) {
  19. return false;
  20. }
  21. conf_len = len;
  22. conf_idx = 0;
  23. conf_buff = os_alloc(len);
  24. return conf_buff != NULL;
  25. }
  26. bool mc_conf_recv_data(u8 *buff, int offset, int len) {
  27. if (offset != conf_idx || conf_buff == NULL) {
  28. return false;
  29. }
  30. if (offset + len > conf_len) {
  31. return false;
  32. }
  33. memcpy(conf_buff + offset, buff, len);
  34. conf_idx = offset + len;
  35. return true;
  36. }
  37. bool mc_conf_finish_recv(u16 crc) {
  38. if (crc != crc16_get(conf_buff, conf_len)) {
  39. return false;
  40. }
  41. mc_conf_decode_configs();
  42. conf.crc = crc16_get((u8 *)&conf, (u8 *)&conf.crc - (u8 *)&conf);
  43. mc_conf_save();
  44. if (conf_buff) {
  45. os_free(conf_buff);
  46. conf_buff = NULL;
  47. }
  48. return true;
  49. }
  50. static u16 crc16;
  51. bool mc_conf_begin_send(void) {
  52. if (conf_buff) {
  53. os_free(conf_buff);
  54. conf_buff = NULL;
  55. }
  56. conf_idx = 0;
  57. conf_buff = os_alloc(sizeof(mc_config));
  58. if (conf_buff) {
  59. conf_len = mc_conf_encode_configs(conf_buff);
  60. crc16 = crc16_get(conf_buff, conf_len);
  61. }
  62. sys_debug("%x, %d, %d\n", conf_buff, conf_len, crc16);
  63. return conf_buff != NULL;
  64. }
  65. int mc_conf_send_data(u8 *buff, int offset, int len) {
  66. sys_debug("off %d, %d\n", offset, conf_idx);
  67. if (offset == 0 && !mc_conf_begin_send()) {
  68. return -2;
  69. }
  70. if (conf_len == conf_idx) {
  71. return 0;
  72. }
  73. if (offset != conf_idx || conf_buff == NULL) {
  74. return -1;
  75. }
  76. len = min(len, conf_len - conf_idx);
  77. memcpy(buff, conf_buff + offset, len);
  78. conf_idx = (offset + len);
  79. return len;
  80. }
  81. int mc_conf_finish_send(u8 *buff) {
  82. if (conf_buff) {
  83. os_free(conf_buff);
  84. conf_buff = NULL;
  85. }
  86. encode_u16(buff, crc16);
  87. return sizeof(u16);
  88. }
  89. void mc_conf_save(void) {
  90. nv_write_config_block(0, (u8 *)&conf, sizeof(conf));
  91. nv_write_config_block(1, (u8 *)&conf, sizeof(conf));
  92. }
  93. void mc_conf_load(void) {
  94. mc_config temp;
  95. nv_read_config_block(0, (u8 *)&temp, sizeof(temp));
  96. u16 crc16 = crc16_get((u8 *)&temp, (u8 *)&temp.crc - (u8 *)&temp);
  97. bool idx0_success = crc16 == temp.crc;
  98. if (idx0_success) {
  99. sys_debug("conf block 0 OK!\n");
  100. memcpy(&conf, &temp, sizeof(temp));
  101. }
  102. nv_read_config_block(1, (u8 *)&temp, sizeof(temp));
  103. crc16 = crc16_get((u8 *)&temp, (u8 *)&temp.crc - (u8 *)&temp);
  104. if (crc16 == temp.crc && !idx0_success) {
  105. sys_debug("conf block0 Bad!\n");
  106. sys_debug("conf block 1 OK!\n");
  107. memcpy(&conf, &temp, sizeof(temp));
  108. nv_write_config_block(0, (u8 *)&temp, sizeof(temp));
  109. mc_crit_err_add(FOC_NV_Err, 0, 0);
  110. }else if (crc16 != temp.crc) {
  111. if (idx0_success) {
  112. sys_debug("conf block1 Bad!\n");
  113. nv_write_config_block(1, (u8 *)&conf, sizeof(conf));
  114. mc_crit_err_add(FOC_NV_Err, 1, 1);
  115. }else {
  116. sys_debug("conf block0&1 Bad!\n");
  117. mc_conf_default();
  118. nv_write_config_block(0, (u8 *)&conf, sizeof(conf));
  119. nv_write_config_block(1, (u8 *)&conf, sizeof(conf));
  120. mc_crit_err_add(FOC_NV_Err, 0, 1);
  121. }
  122. }else {
  123. sys_debug("conf block 1 OK!\n");
  124. }
  125. }
  126. #define Gear_torque(l, t) conf.g_n[l].torque[t] = CONFIG_Gear##l##_Torque##t
  127. #define Gear_Config_default(l) \
  128. do { \
  129. conf.g_n[l].max_speed = CONFIG_Gear##l##_MaxSpeed;\
  130. conf.g_n[l].max_idc = CONFIG_Gear##l##_MaxIdc;\
  131. conf.g_n[l].max_torque = CONFIG_Gear##l##_MaxTorque;\
  132. conf.g_n[l].zero_accl = CONFIG_Gear##l##_ZeroAccl;\
  133. conf.g_n[l].accl_time = CONFIG_Gear##l##_NormalAccl;\
  134. Gear_torque(l,0);Gear_torque(l,1);Gear_torque(l,2);Gear_torque(l,3);Gear_torque(l,4); \
  135. Gear_torque(l,5);Gear_torque(l,6);Gear_torque(l,7);Gear_torque(l,8);Gear_torque(l,9); \
  136. }while(0);
  137. #define GearLow_torque(l, t) conf.g_l[l].torque[t] = CONFIG_GearLow##l##_Torque##t
  138. #define GearLow_Config_default(l) \
  139. do { \
  140. conf.g_l[l].max_speed = CONFIG_GearLow##l##_MaxSpeed;\
  141. conf.g_l[l].max_idc = CONFIG_GearLow##l##_MaxIdc;\
  142. conf.g_l[l].max_torque = CONFIG_GearLow##l##_MaxTorque;\
  143. conf.g_l[l].zero_accl = CONFIG_GearLow##l##_ZeroAccl;\
  144. conf.g_l[l].accl_time = CONFIG_GearLow##l##_NormalAccl;\
  145. GearLow_torque(l,0);GearLow_torque(l,1);GearLow_torque(l,2);GearLow_torque(l,3);GearLow_torque(l,4); \
  146. GearLow_torque(l,5);GearLow_torque(l,6);GearLow_torque(l,7);GearLow_torque(l,8);GearLow_torque(l,9); \
  147. }while(0);
  148. #define Limter_Config_default(item, type, n) \
  149. do { \
  150. conf.item[n].enter_pointer = CONFIG_Protect_##type##_Level##n##_Entry;\
  151. conf.item[n].exit_pointer = CONFIG_Protect_##type##_Level##n##_Exit;\
  152. conf.item[n].limit_value = CONFIG_Protect_##type##_Level##n##_Value;\
  153. }while(0);
  154. #define EnReco_Config_default(n) \
  155. do {\
  156. conf.e_r[n].torque = CONFIG_EnergyRecovery_Level##n##_Torque;\
  157. conf.e_r[n].time = CONFIG_EnergyRecovery_Level##n##_Time;\
  158. }while(0);
  159. void mc_conf_default(void) {
  160. conf.version = CONFIG_Version;
  161. conf.m.poles = CONFIG_Motor_Poles;
  162. conf.m.ld = CONFIG_Motor_Ld;
  163. conf.m.lq = CONFIG_Motor_Lq;
  164. conf.m.rs = CONFIG_Motor_Rs;
  165. conf.m.flux = CONFIG_Motor_Flux;
  166. conf.m.nor_pll_band = CONFIG_Motor_PLLBand;
  167. conf.m.epm_pll_band = CONFIG_Motor_EpmPLL;
  168. conf.m.pos_pll_band = CONFIG_Motor_PosPLL;
  169. conf.m.vehicle_w = CONFIG_Motor_VehicleW;
  170. conf.m.wheel_c = CONFIG_Motor_WheelC;
  171. conf.m.gear_ratio = CONFIG_Motor_GearRatio;
  172. conf.m.max_fw_id = CONFIG_Motor_MaxFwDCurr;
  173. conf.m.max_torque = CONFIG_Motor_MaxTorque;
  174. conf.m.encoder_offset = CONFIG_Motor_EncOffset;
  175. conf.c.max_dc_vol = CONFIG_Foc_MaxDCVol;
  176. conf.c.min_dc_vol = CONFIG_Foc_MinDCVol;
  177. conf.c.max_phase_curr = CONFIG_Foc_MaxPhaseCurr;
  178. conf.c.max_torque = CONFIG_Foc_MaxTorque;
  179. conf.c.max_rpm = CONFIG_Foc_MaxRPM;
  180. conf.c.max_epm_rpm = CONFIG_Foc_MaxEPMRPM;
  181. conf.c.max_epm_torque = CONFIG_Foc_MaxEPMTorque;
  182. conf.c.max_epm_back_rpm = CONFIG_Foc_MaxEPMRPMBk;
  183. conf.c.max_epm_back_torque = CONFIG_Foc_MaxEPMTorqueBk;
  184. conf.c.max_ebrk_torque = CONFIG_Foc_MaxEbrkTorque;
  185. conf.c.max_idc = CONFIG_Foc_MaxIDC;
  186. conf.c.dq_loop_bandwith = CONFIG_Foc_CurrCtrlBandWith;
  187. conf.c.thro_start_vol = CONFIG_Foc_ThroStartVol;
  188. conf.c.thro_end_vol = CONFIG_Foc_ThroEndVol;
  189. conf.c.thro_max_vol = CONFIG_Foc_ThroMaxVol;
  190. conf.c.thro_min_vol = CONFIG_Foc_ThroMinVol;
  191. conf.c.thro_dec_time = CONFIG_Foc_ThroDecTime;
  192. conf.c.max_autohold_torque = CONFIG_Foc_MaxAutoHoldTorque;
  193. conf.c.pid[PID_ID_ID].kp = (float)conf.c.dq_loop_bandwith * 2.0f * 3.14f * conf.m.ld;
  194. conf.c.pid[PID_ID_ID].ki = conf.m.rs / conf.m.ld;
  195. conf.c.pid[PID_ID_ID].kd = 0;
  196. conf.c.pid[PID_IQ_ID].kp = (float)conf.c.dq_loop_bandwith * 2.0f * 3.14f * conf.m.lq;
  197. conf.c.pid[PID_IQ_ID].ki = conf.m.rs / conf.m.lq;
  198. conf.c.pid[PID_IQ_ID].kd = 0;
  199. conf.c.pid[PID_VelLim_ID].kp = CONFIG_Foc_PID_VelLim_Kp;
  200. conf.c.pid[PID_VelLim_ID].ki = CONFIG_Foc_PID_VelLim_Ki;
  201. conf.c.pid[PID_VelLim_ID].kd = CONFIG_Foc_PID_VelLim_Kd;
  202. conf.c.pid[PID_Vel_ID].kp = CONFIG_Foc_PID_VelCtrl_Kp;
  203. conf.c.pid[PID_Vel_ID].ki = CONFIG_Foc_PID_VelCtrl_Ki;
  204. conf.c.pid[PID_Vel_ID].kd = CONFIG_Foc_PID_VelCtrl_Kd;
  205. conf.c.pid[PID_AutoHold_ID].kp = CONFIG_Foc_PID_Autohold_Kp;
  206. conf.c.pid[PID_AutoHold_ID].ki = CONFIG_Foc_PID_Autohold_Ki;
  207. conf.c.pid[PID_AutoHold_ID].kd = CONFIG_Foc_PID_Autohold_Kd;
  208. conf.c.pid[PID_IDCLim_ID].kp = CONFIG_Foc_PID_IDCLim_Kp;
  209. conf.c.pid[PID_IDCLim_ID].ki = CONFIG_Foc_PID_IDCLim_Ki;
  210. conf.c.pid[PID_IDCLim_ID].kd = CONFIG_Foc_PID_IDCLim_Kd;
  211. conf.s.auto_hold = CONFIG_Settings_AutoHold;
  212. conf.s.brk_shut_power = CONFIG_Settings_BrkShutPower;
  213. conf.s.tcs_enable = CONFIG_Settings_TcsEnable;
  214. Gear_Config_default(0);
  215. Gear_Config_default(1);
  216. Gear_Config_default(2);
  217. Gear_Config_default(3);
  218. GearLow_Config_default(0);
  219. GearLow_Config_default(1);
  220. GearLow_Config_default(2);
  221. GearLow_Config_default(3);
  222. Limter_Config_default(p_mot, Motor, 0);
  223. Limter_Config_default(p_mot, Motor, 1);
  224. Limter_Config_default(p_mot, Motor,2);
  225. Limter_Config_default(p_mos, MosFet, 0);
  226. Limter_Config_default(p_mos, MosFet, 1);
  227. Limter_Config_default(p_mos, MosFet, 2);
  228. conf.p_vol.enter_pointer = CONFIG_Protect_Voltage_Level0_Entry;
  229. conf.p_vol.exit_pointer = CONFIG_Protect_Voltage_Level0_Exit;
  230. conf.p_vol.limit_value = CONFIG_Protect_Voltage_Level0_Value;
  231. EnReco_Config_default(0);
  232. EnReco_Config_default(1);
  233. EnReco_Config_default(2);
  234. EnReco_Config_default(3);
  235. EnReco_Config_default(4);
  236. EnReco_Config_default(5);
  237. EnReco_Config_default(6);
  238. EnReco_Config_default(7);
  239. EnReco_Config_default(8);
  240. EnReco_Config_default(9);
  241. conf.cz.low = CONFIG_CrossZero_Low;
  242. conf.cz.high = CONFIG_CrossZero_High;
  243. conf.cz.min_step = CONFIG_CrossZero_MinStep;
  244. conf.cz.normal_step = CONFIG_CrossZero_NorStep;
  245. conf.crc = crc16_get((u8 *)&conf, (u8 *)&conf.crc - (u8 *)&conf);
  246. }
  247. void mc_conf_init(void) {
  248. mc_conf_load();
  249. if (conf.version != CONFIG_Version) {
  250. sys_debug("mc conf ver %d != %d\n", conf.version, CONFIG_Version);
  251. mc_conf_default();
  252. mc_conf_save();
  253. }
  254. sys_debug("mc size %d - %d\n", sizeof(conf), sizeof(conf.c));
  255. sys_debug("mc conf band %d, pid: %f, %f, %f, %f, %d\n", conf.c.dq_loop_bandwith, conf.c.pid[PID_ID_ID].kp, conf.c.pid[PID_ID_ID].ki, conf.c.pid[PID_IQ_ID].kp, conf.c.pid[PID_IQ_ID].ki, mc_conf()->m.encoder_offset);
  256. }
  257. int mc_conf_decode_motor(u8 *buff) {
  258. u8 *ori = buff;
  259. conf.m.poles = decode_u8(buff++);
  260. conf.m.ld = decode_float(buff);buff += 4;
  261. conf.m.lq = decode_float(buff);buff += 4;
  262. conf.m.rs = decode_float(buff);buff += 4;
  263. conf.m.flux = decode_float(buff);buff +=4;
  264. conf.m.nor_pll_band = (float)decode_u16(buff); buff += 2;
  265. conf.m.epm_pll_band = (float)decode_u16(buff); buff += 2;
  266. conf.m.pos_pll_band = (float)decode_u16(buff); buff += 2;
  267. conf.m.vehicle_w = decode_u16(buff);buff += 2;
  268. conf.m.wheel_c = decode_u16(buff);buff += 2;
  269. conf.m.gear_ratio = decode_float(buff);buff += 4;
  270. conf.m.max_fw_id = decode_u16(buff); buff += 2;
  271. conf.m.max_torque = decode_u16(buff); buff += 2;
  272. conf.m.encoder_offset = decode_s16(buff); buff += 2;
  273. return buff - ori;
  274. }
  275. int mc_conf_encode_motor(u8 *buff) {
  276. u8 *ori = buff;
  277. encode_u8(buff++, conf.m.poles);
  278. encode_float(buff, conf.m.ld);buff += 4;
  279. encode_float(buff, conf.m.lq);buff += 4;
  280. encode_float(buff, conf.m.rs);buff += 4;
  281. encode_float(buff, conf.m.flux);buff +=4;
  282. encode_u16(buff, (u16)conf.m.nor_pll_band); buff += 2;
  283. encode_u16(buff, (u16)conf.m.epm_pll_band); buff += 2;
  284. encode_u16(buff, (u16)conf.m.pos_pll_band); buff += 2;
  285. encode_u16(buff, conf.m.vehicle_w);buff += 2;
  286. encode_u16(buff, conf.m.wheel_c);buff += 2;
  287. encode_float(buff, conf.m.gear_ratio);buff += 4;
  288. encode_u16(buff, conf.m.max_fw_id); buff += 2;
  289. encode_u16(buff, conf.m.max_torque); buff += 2;
  290. encode_s16(buff, conf.m.encoder_offset); buff += 2;
  291. return buff - ori;
  292. }
  293. int mc_conf_decode_pid(pid_t *pid, u8 *buff) {
  294. u8 *ori = buff;
  295. pid->kp = decode_float(buff);buff += 4;
  296. pid->ki = decode_float(buff);buff += 4;
  297. pid->kd = decode_float(buff);buff += 4;
  298. return buff - ori;
  299. }
  300. int mc_conf_encode_pid(pid_t *pid, u8 *buff) {
  301. u8 *ori = buff;
  302. encode_float(buff, pid->kp);buff += 4;
  303. encode_float(buff, pid->ki);buff += 4;
  304. encode_float(buff, pid->kd);buff += 4;
  305. return buff - ori;
  306. }
  307. int mc_conf_decode_controler(u8 *buff) {
  308. u8 *ori = buff;
  309. conf.c.max_dc_vol = decode_s16(buff);buff += 2;
  310. conf.c.min_dc_vol = decode_s16(buff);buff += 2;
  311. conf.c.max_phase_curr = decode_s16(buff);buff += 2;
  312. conf.c.max_torque = decode_s16(buff);buff += 2;
  313. conf.c.max_rpm = decode_s16(buff);buff += 2;
  314. conf.c.max_epm_rpm = decode_s16(buff);buff += 2;
  315. conf.c.max_epm_torque = decode_s16(buff);buff += 2;
  316. conf.c.max_epm_back_rpm = decode_s16(buff);buff += 2;
  317. conf.c.max_epm_back_torque = decode_s16(buff);buff += 2;
  318. conf.c.max_ebrk_torque = decode_s16(buff);buff += 2;
  319. conf.c.max_idc = decode_s16(buff);buff += 2;
  320. conf.c.max_autohold_torque = decode_s16(buff);buff += 2;
  321. conf.c.dq_loop_bandwith = decode_s16(buff);buff += 2;
  322. conf.c.thro_start_vol = decode_float(buff);buff += 4;
  323. conf.c.thro_end_vol = decode_float(buff);buff += 4;
  324. conf.c.thro_min_vol = decode_float(buff);buff += 4;
  325. conf.c.thro_max_vol = decode_float(buff);buff += 4;
  326. conf.c.thro_dec_time = decode_u16(buff);buff += 2;
  327. conf.c.max_torque = min(conf.c.max_torque, conf.m.max_torque);
  328. conf.c.max_dc_vol = min(conf.c.max_dc_vol, CONFIG_BOARD_MAX_VOLTAGE);
  329. conf.c.min_dc_vol = MAX(conf.c.min_dc_vol, CONFIG_BOARD_MIN_VOLTAGE);
  330. buff += mc_conf_decode_pid(&conf.c.pid[PID_VelLim_ID], buff);
  331. buff += mc_conf_decode_pid(&conf.c.pid[PID_Vel_ID], buff);
  332. buff += mc_conf_decode_pid(&conf.c.pid[PID_AutoHold_ID], buff);
  333. buff += mc_conf_decode_pid(&conf.c.pid[PID_IDCLim_ID], buff);
  334. buff += mc_conf_decode_pid(&conf.c.epm_pid, buff);
  335. conf.c.pid[PID_ID_ID].kp = (float)conf.c.dq_loop_bandwith * 2.0f * 3.14f * conf.m.ld;
  336. conf.c.pid[PID_ID_ID].ki = conf.m.rs / conf.m.ld;
  337. conf.c.pid[PID_ID_ID].kd = 0;
  338. conf.c.pid[PID_IQ_ID].kp = (float)conf.c.dq_loop_bandwith * 2.0f * 3.14f * conf.m.lq;
  339. conf.c.pid[PID_IQ_ID].ki = conf.m.rs / conf.m.lq;
  340. conf.c.pid[PID_IQ_ID].kd = 0;
  341. return buff - ori;
  342. }
  343. int mc_conf_encode_controler(u8 *buff) {
  344. u8 *ori = buff;
  345. encode_s16(buff, conf.c.max_dc_vol);buff += 2;
  346. encode_s16(buff, conf.c.min_dc_vol);buff += 2;
  347. encode_s16(buff, conf.c.max_phase_curr);buff += 2;
  348. encode_s16(buff, conf.c.max_torque);buff += 2;
  349. encode_s16(buff, conf.c.max_rpm);buff += 2;
  350. encode_s16(buff, conf.c.max_epm_rpm);buff += 2;
  351. encode_s16(buff, conf.c.max_epm_torque);buff += 2;
  352. encode_s16(buff, conf.c.max_epm_back_rpm);buff += 2;
  353. encode_s16(buff, conf.c.max_epm_back_torque);buff += 2;
  354. encode_s16(buff, conf.c.max_ebrk_torque);buff += 2;
  355. encode_s16(buff, conf.c.max_idc);buff += 2;
  356. encode_s16(buff, conf.c.max_autohold_torque);buff += 2;
  357. encode_s16(buff, conf.c.dq_loop_bandwith);buff += 2;
  358. encode_float(buff, conf.c.thro_start_vol);buff += 4;
  359. encode_float(buff, conf.c.thro_end_vol);buff += 4;
  360. encode_float(buff, conf.c.thro_min_vol);buff += 4;
  361. encode_float(buff, conf.c.thro_max_vol);buff += 4;
  362. encode_u16(buff, conf.c.thro_dec_time);buff += 2;
  363. buff += mc_conf_encode_pid(&conf.c.pid[PID_VelLim_ID], buff);
  364. buff += mc_conf_encode_pid(&conf.c.pid[PID_Vel_ID], buff);
  365. buff += mc_conf_encode_pid(&conf.c.pid[PID_AutoHold_ID], buff);
  366. buff += mc_conf_encode_pid(&conf.c.pid[PID_IDCLim_ID], buff);
  367. buff += mc_conf_encode_pid(&conf.c.epm_pid, buff);
  368. return buff - ori;
  369. }
  370. int mc_conf_decode_setting(u8 *buff) {
  371. u8 *ori = buff;
  372. conf.s.auto_hold = decode_u8(buff++)==1?true:false;
  373. conf.s.brk_shut_power = decode_u8(buff++)==1?true:false;
  374. conf.s.tcs_enable = decode_u8(buff++)==1?true:false;
  375. return buff - ori;
  376. }
  377. int mc_conf_encode_setting(u8 *buff) {
  378. u8 *ori = buff;
  379. encode_u8(buff++, conf.s.auto_hold?1:0);
  380. encode_u8(buff++, conf.s.brk_shut_power?1:0);
  381. encode_u8(buff++, conf.s.tcs_enable?1:0);
  382. return buff - ori;
  383. }
  384. int mc_conf_decode_cross_zero(u8 *buff) {
  385. u8 *ori = buff;
  386. conf.cz.low = (float)decode_s16(buff); buff += 2;
  387. conf.cz.high = (float)decode_s16(buff); buff += 2;
  388. conf.cz.min_step = decode_float(buff); buff += 4;
  389. conf.cz.normal_step = (float)decode_s16(buff); buff += 2;
  390. return buff - ori;
  391. }
  392. int mc_conf_encode_cross_zero(u8 *buff) {
  393. u8 *ori = buff;
  394. encode_s16(buff, (s16)conf.cz.low); buff += 2;
  395. encode_s16(buff, (s16)conf.cz.high); buff += 2;
  396. encode_float(buff, conf.cz.min_step); buff += 4;
  397. encode_s16(buff, (s16)conf.cz.normal_step); buff += 2;
  398. return buff - ori;
  399. }
  400. int mc_conf_decode_gear(gear_t *g , u8 *buff) {
  401. u8 *ori = buff;
  402. for (int i = 0; i < CONFIG_MAX_GEARS; i++) {
  403. g[i].max_speed = decode_s16(buff); buff += 2;
  404. g[i].max_torque = decode_s16(buff); buff += 2;
  405. g[i].max_idc = decode_s16(buff); buff += 2;
  406. g[i].zero_accl = decode_u16(buff); buff += 2;
  407. g[i].accl_time = decode_u16(buff); buff += 2;
  408. for (int j = 0; j < CONFIG_GEAR_SPEED_TRQ_NUM; j++) {
  409. g[i].torque[j] = decode_u8(buff++);
  410. if (g[i].torque[j] > 100) {
  411. g[i].torque[j] = 100;
  412. }
  413. }
  414. g[i].max_torque = min(g[i].max_torque, conf.c.max_torque);
  415. g[i].max_idc = min(g[i].max_idc, conf.c.max_idc);
  416. }
  417. return buff - ori;
  418. }
  419. int mc_conf_encode_gear(gear_t *g, u8 *buff) {
  420. u8 *ori = buff;
  421. for (int i = 0; i < CONFIG_MAX_GEARS; i++) {
  422. encode_s16(buff, g[i].max_speed); buff += 2;
  423. encode_s16(buff, g[i].max_torque); buff += 2;
  424. encode_s16(buff, g[i].max_idc); buff += 2;
  425. encode_u16(buff, g[i].zero_accl); buff += 2;
  426. encode_u16(buff, g[i].accl_time); buff += 2;
  427. for (int j = 0; j < CONFIG_GEAR_SPEED_TRQ_NUM; j++) {
  428. encode_u8(buff++, g[i].torque[j]);
  429. }
  430. }
  431. return buff - ori;
  432. }
  433. int mc_conf_decode_limiter(limiter_t *l, int size, u8 *buff) {
  434. u8 *ori = buff;
  435. for (int i = 0; i < size; i++) {
  436. l->enter_pointer = decode_s16(buff);buff += 2;
  437. l->exit_pointer = decode_s16(buff);buff += 2;
  438. l->limit_value = decode_s16(buff);buff += 2;
  439. l++;
  440. }
  441. return buff - ori;
  442. }
  443. int mc_conf_encode_limiter(limiter_t *l, int size, u8 *buff) {
  444. u8 *ori = buff;
  445. for (int i = 0; i < size; i++) {
  446. encode_s16(buff, l->enter_pointer);buff += 2;
  447. encode_s16(buff, l->exit_pointer);buff += 2;
  448. encode_s16(buff, l->limit_value);buff += 2;
  449. l++;
  450. }
  451. return buff - ori;
  452. }
  453. int mc_conf_decode_engreco(u8 *buff) {
  454. u8 *ori = buff;
  455. for (int i = 0; i < CONFIG_EBRK_LVL_NUM; i++) {
  456. conf.e_r[i].torque = decode_s16(buff); buff += 2;
  457. conf.e_r[i].time = decode_s16(buff); buff += 2;
  458. conf.e_r[i].torque = min(conf.e_r[i].torque, conf.c.max_ebrk_torque);
  459. }
  460. return buff - ori;
  461. }
  462. int mc_conf_encode_engreco(u8 *buff) {
  463. u8 *ori = buff;
  464. for (int i = 0; i < CONFIG_EBRK_LVL_NUM; i++) {
  465. encode_s16(buff, conf.e_r[i].torque); buff += 2;
  466. encode_s16(buff, conf.e_r[i].time); buff += 2;
  467. }
  468. return buff - ori;
  469. }
  470. void mc_conf_decode_configs(void) {
  471. u8 *buff = conf_buff;
  472. conf.version = decode_u8(buff++);
  473. buff += mc_conf_decode_motor(buff);
  474. buff += mc_conf_decode_controler(buff);
  475. buff += mc_conf_decode_setting(buff);
  476. buff += mc_conf_decode_gear(conf.g_n, buff);
  477. buff += mc_conf_decode_gear(conf.g_l, buff);
  478. buff += mc_conf_decode_limiter(conf.p_mot, CONFIG_TEMP_PROT_NUM, buff);
  479. buff += mc_conf_decode_limiter(conf.p_mos, CONFIG_TEMP_PROT_NUM, buff);
  480. buff += mc_conf_decode_limiter(&conf.p_vol, 1, buff);
  481. buff += mc_conf_decode_engreco(buff);
  482. buff += mc_conf_decode_cross_zero(buff);
  483. }
  484. int mc_conf_encode_configs(u8 *buff) {
  485. u8 *ori = buff;
  486. encode_u8(buff++, conf.version);
  487. buff += mc_conf_encode_motor(buff);
  488. buff += mc_conf_encode_controler(buff);
  489. buff += mc_conf_encode_setting(buff);
  490. buff += mc_conf_encode_gear(conf.g_n, buff);
  491. buff += mc_conf_encode_gear(conf.g_l, buff);
  492. buff += mc_conf_encode_limiter(conf.p_mot, CONFIG_TEMP_PROT_NUM, buff);
  493. buff += mc_conf_encode_limiter(conf.p_mos, CONFIG_TEMP_PROT_NUM, buff);
  494. buff += mc_conf_encode_limiter(&conf.p_vol, 1, buff);
  495. buff += mc_conf_encode_engreco(buff);
  496. buff += mc_conf_encode_cross_zero(buff);
  497. return buff - ori;
  498. }
  499. void mc_conf_set_pid(u8 id, pid_t *pid) {
  500. if (id < PID_Max_ID) {
  501. conf.c.pid[id] = *pid;
  502. }else if (id == PID_EPM_ExtID){
  503. conf.c.epm_pid = *pid;
  504. }
  505. }
  506. void mc_conf_get_pid(u8 id, pid_t *pid) {
  507. if (id < PID_Max_ID) {
  508. *pid = conf.c.pid[id];
  509. }else if (id == PID_EPM_ExtID){
  510. *pid = conf.c.epm_pid;
  511. }
  512. }
  513. bool mc_conf_set_gear(u8 mode, u8 *data, int len) {
  514. gear_t *g = conf.g_n;
  515. if (mode == 0) {
  516. g = conf.g_l;
  517. }
  518. if (mc_conf_decode_gear(g, data) > len) {
  519. return false;
  520. }
  521. return true;
  522. }
  523. int mc_conf_get_gear(u8 mode, u8 *data) {
  524. gear_t *g = conf.g_n;
  525. if (mode == 0) {
  526. g = conf.g_l;
  527. }
  528. return mc_conf_encode_gear(g, data);
  529. }
  530. bool mc_conf_set_limter(u8 *data, int len) {
  531. int l = mc_conf_decode_limiter(conf.p_mot, CONFIG_TEMP_PROT_NUM, data);
  532. l += mc_conf_decode_limiter(conf.p_mos, CONFIG_TEMP_PROT_NUM, data + l);
  533. l += mc_conf_decode_limiter(&conf.p_vol, 1, data + l);
  534. return len>=l;
  535. }
  536. int mc_conf_get_limter(u8 *data) {
  537. u8 *ori = data;
  538. data += mc_conf_encode_limiter(conf.p_mot, CONFIG_TEMP_PROT_NUM, data);
  539. data += mc_conf_encode_limiter(conf.p_mos, CONFIG_TEMP_PROT_NUM, data);
  540. data += mc_conf_encode_limiter(&conf.p_vol, 1, data);
  541. return data-ori;
  542. }