mc_config.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  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.fw_enable = CONFIG_Motor_FwEnable;
  170. conf.m.fw_duty_start = CONFIG_Motor_FwDutyStart;
  171. conf.m.max_fw_id = CONFIG_Motor_MaxFwDCurr;
  172. conf.m.max_torque = CONFIG_Motor_MaxTorque;
  173. conf.m.encoder_offset = CONFIG_Motor_EncOffset;
  174. conf.c.max_dc_vol = CONFIG_Foc_MaxDCVol;
  175. conf.c.min_dc_vol = CONFIG_Foc_MinDCVol;
  176. conf.c.max_phase_curr = CONFIG_Foc_MaxPhaseCurr;
  177. conf.c.max_torque = CONFIG_Foc_MaxTorque;
  178. conf.c.max_rpm = CONFIG_Foc_MaxRPM;
  179. conf.c.max_epm_rpm = CONFIG_Foc_MaxEPMRPM;
  180. conf.c.max_epm_torque = CONFIG_Foc_MaxEPMTorque;
  181. conf.c.max_epm_back_rpm = CONFIG_Foc_MaxEPMRPMBk;
  182. conf.c.max_epm_back_torque = CONFIG_Foc_MaxEPMTorqueBk;
  183. conf.c.max_ebrk_torque = CONFIG_Foc_MaxEbrkTorque;
  184. conf.c.max_idc = CONFIG_Foc_MaxIDC;
  185. conf.c.dq_loop_bandwith = CONFIG_Foc_CurrCtrlBandWith;
  186. conf.c.thro_start_vol = CONFIG_Foc_ThroStartVol;
  187. conf.c.thro_end_vol = CONFIG_Foc_ThroEndVol;
  188. conf.c.thro_max_vol = CONFIG_Foc_ThroMaxVol;
  189. conf.c.thro_min_vol = CONFIG_Foc_ThroMinVol;
  190. conf.c.thro_dec_time = CONFIG_Foc_ThroDecTime;
  191. conf.c.max_autohold_torque = CONFIG_Foc_MaxAutoHoldTorque;
  192. conf.c.pid[PID_ID_ID].kp = (float)conf.c.dq_loop_bandwith * 2.0f * 3.14f * conf.m.ld;
  193. conf.c.pid[PID_ID_ID].ki = conf.m.rs / conf.m.ld;
  194. conf.c.pid[PID_ID_ID].kd = 0;
  195. conf.c.pid[PID_IQ_ID].kp = (float)conf.c.dq_loop_bandwith * 2.0f * 3.14f * conf.m.lq;
  196. conf.c.pid[PID_IQ_ID].ki = conf.m.rs / conf.m.lq;
  197. conf.c.pid[PID_IQ_ID].kd = 0;
  198. conf.c.pid[PID_VelLim_ID].kp = CONFIG_Foc_PID_VelLim_Kp;
  199. conf.c.pid[PID_VelLim_ID].ki = CONFIG_Foc_PID_VelLim_Ki;
  200. conf.c.pid[PID_VelLim_ID].kd = CONFIG_Foc_PID_VelLim_Kd;
  201. conf.c.pid[PID_Vel_ID].kp = CONFIG_Foc_PID_VelCtrl_Kp;
  202. conf.c.pid[PID_Vel_ID].ki = CONFIG_Foc_PID_VelCtrl_Ki;
  203. conf.c.pid[PID_Vel_ID].kd = CONFIG_Foc_PID_VelCtrl_Kd;
  204. conf.c.pid[PID_AutoHold_ID].kp = CONFIG_Foc_PID_Autohold_Kp;
  205. conf.c.pid[PID_AutoHold_ID].ki = CONFIG_Foc_PID_Autohold_Ki;
  206. conf.c.pid[PID_AutoHold_ID].kd = CONFIG_Foc_PID_Autohold_Kd;
  207. conf.c.pid[PID_IDCLim_ID].kp = CONFIG_Foc_PID_IDCLim_Kp;
  208. conf.c.pid[PID_IDCLim_ID].ki = CONFIG_Foc_PID_IDCLim_Ki;
  209. conf.c.pid[PID_IDCLim_ID].kd = CONFIG_Foc_PID_IDCLim_Kd;
  210. conf.c.epm_pid.kp = CONFIG_Foc_PID_EPM_Kp;
  211. conf.c.epm_pid.ki = CONFIG_Foc_PID_EPM_Ki;
  212. conf.c.epm_pid.kd = CONFIG_Foc_PID_EPM_Kd;
  213. conf.s.auto_hold = CONFIG_Settings_AutoHold;
  214. conf.s.brk_shut_power = CONFIG_Settings_BrkShutPower;
  215. conf.s.tcs_enable = CONFIG_Settings_TcsEnable;
  216. Gear_Config_default(0);
  217. Gear_Config_default(1);
  218. Gear_Config_default(2);
  219. Gear_Config_default(3);
  220. GearLow_Config_default(0);
  221. GearLow_Config_default(1);
  222. GearLow_Config_default(2);
  223. GearLow_Config_default(3);
  224. Limter_Config_default(p_mot, Motor, 0);
  225. Limter_Config_default(p_mot, Motor, 1);
  226. Limter_Config_default(p_mot, Motor,2);
  227. Limter_Config_default(p_mos, MosFet, 0);
  228. Limter_Config_default(p_mos, MosFet, 1);
  229. Limter_Config_default(p_mos, MosFet, 2);
  230. conf.p_vol.enter_pointer = CONFIG_Protect_Voltage_Level0_Entry;
  231. conf.p_vol.exit_pointer = CONFIG_Protect_Voltage_Level0_Exit;
  232. conf.p_vol.limit_value = CONFIG_Protect_Voltage_Level0_Value;
  233. EnReco_Config_default(0);
  234. EnReco_Config_default(1);
  235. EnReco_Config_default(2);
  236. EnReco_Config_default(3);
  237. EnReco_Config_default(4);
  238. EnReco_Config_default(5);
  239. EnReco_Config_default(6);
  240. EnReco_Config_default(7);
  241. EnReco_Config_default(8);
  242. EnReco_Config_default(9);
  243. conf.cz.low = CONFIG_CrossZero_Low;
  244. conf.cz.high = CONFIG_CrossZero_High;
  245. conf.cz.min_step = CONFIG_CrossZero_MinStep;
  246. conf.cz.normal_step = CONFIG_CrossZero_NorStep;
  247. conf.crc = crc16_get((u8 *)&conf, (u8 *)&conf.crc - (u8 *)&conf);
  248. }
  249. void mc_conf_init(void) {
  250. mc_conf_load();
  251. if (conf.version != CONFIG_Version) {
  252. sys_debug("mc conf ver %d != %d\n", conf.version, CONFIG_Version);
  253. mc_conf_default();
  254. mc_conf_save();
  255. }
  256. sys_debug("mc size %d - %d\n", sizeof(conf), sizeof(conf.c));
  257. 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);
  258. }
  259. int mc_conf_decode_motor(u8 *buff) {
  260. u8 *ori = buff;
  261. conf.m.poles = decode_u8(buff++);
  262. conf.m.ld = decode_float(buff);buff += 4;
  263. conf.m.lq = decode_float(buff);buff += 4;
  264. conf.m.rs = decode_float(buff);buff += 4;
  265. conf.m.flux = decode_float(buff);buff +=4;
  266. conf.m.nor_pll_band = (float)decode_u16(buff); buff += 2;
  267. conf.m.epm_pll_band = (float)decode_u16(buff); buff += 2;
  268. conf.m.pos_pll_band = (float)decode_u16(buff); buff += 2;
  269. conf.m.fw_enable = decode_u8(buff);buff += 1;
  270. conf.m.fw_duty_start = decode_u8(buff);buff += 1;
  271. conf.m.max_fw_id = decode_u16(buff); buff += 2;
  272. conf.m.max_torque = decode_u16(buff); buff += 2;
  273. conf.m.encoder_offset = decode_s16(buff); buff += 2;
  274. if (conf.m.fw_duty_start > 100) {
  275. conf.m.fw_duty_start = 100;
  276. }
  277. return buff - ori;
  278. }
  279. int mc_conf_encode_motor(u8 *buff) {
  280. u8 *ori = buff;
  281. encode_u8(buff++, conf.m.poles);
  282. encode_float(buff, conf.m.ld);buff += 4;
  283. encode_float(buff, conf.m.lq);buff += 4;
  284. encode_float(buff, conf.m.rs);buff += 4;
  285. encode_float(buff, conf.m.flux);buff +=4;
  286. encode_u16(buff, (u16)conf.m.nor_pll_band); buff += 2;
  287. encode_u16(buff, (u16)conf.m.epm_pll_band); buff += 2;
  288. encode_u16(buff, (u16)conf.m.pos_pll_band); buff += 2;
  289. encode_u8(buff, (u8)conf.m.fw_enable);buff += 1;
  290. encode_u8(buff, conf.m.fw_duty_start);buff += 1;
  291. encode_u16(buff, conf.m.max_fw_id); buff += 2;
  292. encode_u16(buff, conf.m.max_torque); buff += 2;
  293. encode_s16(buff, conf.m.encoder_offset); buff += 2;
  294. return buff - ori;
  295. }
  296. int mc_conf_decode_pid(pid_t *pid, u8 *buff) {
  297. u8 *ori = buff;
  298. pid->kp = decode_float(buff);buff += 4;
  299. pid->ki = decode_float(buff);buff += 4;
  300. pid->kd = decode_float(buff);buff += 4;
  301. return buff - ori;
  302. }
  303. int mc_conf_encode_pid(pid_t *pid, u8 *buff) {
  304. u8 *ori = buff;
  305. encode_float(buff, pid->kp);buff += 4;
  306. encode_float(buff, pid->ki);buff += 4;
  307. encode_float(buff, pid->kd);buff += 4;
  308. return buff - ori;
  309. }
  310. int mc_conf_decode_controler(u8 *buff) {
  311. u8 *ori = buff;
  312. conf.c.max_dc_vol = decode_s16(buff);buff += 2;
  313. conf.c.min_dc_vol = decode_s16(buff);buff += 2;
  314. conf.c.max_phase_curr = decode_s16(buff);buff += 2;
  315. conf.c.max_torque = decode_s16(buff);buff += 2;
  316. conf.c.max_rpm = decode_s16(buff);buff += 2;
  317. conf.c.max_epm_rpm = decode_s16(buff);buff += 2;
  318. conf.c.max_epm_torque = decode_s16(buff);buff += 2;
  319. conf.c.max_epm_back_rpm = decode_s16(buff);buff += 2;
  320. conf.c.max_epm_back_torque = decode_s16(buff);buff += 2;
  321. conf.c.max_ebrk_torque = decode_s16(buff);buff += 2;
  322. conf.c.max_idc = decode_s16(buff);buff += 2;
  323. conf.c.max_autohold_torque = decode_s16(buff);buff += 2;
  324. conf.c.dq_loop_bandwith = decode_s16(buff);buff += 2;
  325. conf.c.thro_start_vol = decode_float(buff);buff += 4;
  326. conf.c.thro_end_vol = decode_float(buff);buff += 4;
  327. conf.c.thro_min_vol = decode_float(buff);buff += 4;
  328. conf.c.thro_max_vol = decode_float(buff);buff += 4;
  329. conf.c.thro_dec_time = decode_u16(buff);buff += 2;
  330. conf.c.max_torque = min(conf.c.max_torque, conf.m.max_torque);
  331. conf.c.max_dc_vol = min(conf.c.max_dc_vol, CONFIG_BOARD_MAX_VOLTAGE);
  332. conf.c.min_dc_vol = MAX(conf.c.min_dc_vol, CONFIG_BOARD_MIN_VOLTAGE);
  333. buff += mc_conf_decode_pid(&conf.c.pid[PID_VelLim_ID], buff);
  334. buff += mc_conf_decode_pid(&conf.c.pid[PID_Vel_ID], buff);
  335. buff += mc_conf_decode_pid(&conf.c.pid[PID_AutoHold_ID], buff);
  336. buff += mc_conf_decode_pid(&conf.c.pid[PID_IDCLim_ID], buff);
  337. buff += mc_conf_decode_pid(&conf.c.epm_pid, buff);
  338. conf.c.pid[PID_ID_ID].kp = (float)conf.c.dq_loop_bandwith * 2.0f * 3.14f * conf.m.ld;
  339. conf.c.pid[PID_ID_ID].ki = conf.m.rs / conf.m.ld;
  340. conf.c.pid[PID_ID_ID].kd = 0;
  341. conf.c.pid[PID_IQ_ID].kp = (float)conf.c.dq_loop_bandwith * 2.0f * 3.14f * conf.m.lq;
  342. conf.c.pid[PID_IQ_ID].ki = conf.m.rs / conf.m.lq;
  343. conf.c.pid[PID_IQ_ID].kd = 0;
  344. return buff - ori;
  345. }
  346. int mc_conf_encode_controler(u8 *buff) {
  347. u8 *ori = buff;
  348. encode_s16(buff, conf.c.max_dc_vol);buff += 2;
  349. encode_s16(buff, conf.c.min_dc_vol);buff += 2;
  350. encode_s16(buff, conf.c.max_phase_curr);buff += 2;
  351. encode_s16(buff, conf.c.max_torque);buff += 2;
  352. encode_s16(buff, conf.c.max_rpm);buff += 2;
  353. encode_s16(buff, conf.c.max_epm_rpm);buff += 2;
  354. encode_s16(buff, conf.c.max_epm_torque);buff += 2;
  355. encode_s16(buff, conf.c.max_epm_back_rpm);buff += 2;
  356. encode_s16(buff, conf.c.max_epm_back_torque);buff += 2;
  357. encode_s16(buff, conf.c.max_ebrk_torque);buff += 2;
  358. encode_s16(buff, conf.c.max_idc);buff += 2;
  359. encode_s16(buff, conf.c.max_autohold_torque);buff += 2;
  360. encode_s16(buff, conf.c.dq_loop_bandwith);buff += 2;
  361. encode_float(buff, conf.c.thro_start_vol);buff += 4;
  362. encode_float(buff, conf.c.thro_end_vol);buff += 4;
  363. encode_float(buff, conf.c.thro_min_vol);buff += 4;
  364. encode_float(buff, conf.c.thro_max_vol);buff += 4;
  365. encode_u16(buff, conf.c.thro_dec_time);buff += 2;
  366. buff += mc_conf_encode_pid(&conf.c.pid[PID_VelLim_ID], buff);
  367. buff += mc_conf_encode_pid(&conf.c.pid[PID_Vel_ID], buff);
  368. buff += mc_conf_encode_pid(&conf.c.pid[PID_AutoHold_ID], buff);
  369. buff += mc_conf_encode_pid(&conf.c.pid[PID_IDCLim_ID], buff);
  370. buff += mc_conf_encode_pid(&conf.c.epm_pid, buff);
  371. return buff - ori;
  372. }
  373. int mc_conf_decode_setting(u8 *buff) {
  374. u8 *ori = buff;
  375. conf.s.auto_hold = decode_u8(buff++)==1?true:false;
  376. conf.s.brk_shut_power = decode_u8(buff++)==1?true:false;
  377. conf.s.tcs_enable = decode_u8(buff++)==1?true:false;
  378. return buff - ori;
  379. }
  380. int mc_conf_encode_setting(u8 *buff) {
  381. u8 *ori = buff;
  382. encode_u8(buff++, conf.s.auto_hold?1:0);
  383. encode_u8(buff++, conf.s.brk_shut_power?1:0);
  384. encode_u8(buff++, conf.s.tcs_enable?1:0);
  385. return buff - ori;
  386. }
  387. int mc_conf_decode_cross_zero(u8 *buff) {
  388. u8 *ori = buff;
  389. conf.cz.low = (float)decode_s16(buff); buff += 2;
  390. conf.cz.high = (float)decode_s16(buff); buff += 2;
  391. conf.cz.min_step = decode_float(buff); buff += 4;
  392. conf.cz.normal_step = (float)decode_s16(buff); buff += 2;
  393. return buff - ori;
  394. }
  395. int mc_conf_encode_cross_zero(u8 *buff) {
  396. u8 *ori = buff;
  397. encode_s16(buff, (s16)conf.cz.low); buff += 2;
  398. encode_s16(buff, (s16)conf.cz.high); buff += 2;
  399. encode_float(buff, conf.cz.min_step); buff += 4;
  400. encode_s16(buff, (s16)conf.cz.normal_step); buff += 2;
  401. return buff - ori;
  402. }
  403. int mc_conf_decode_gear(gear_t *g , u8 *buff) {
  404. u8 *ori = buff;
  405. for (int i = 0; i < CONFIG_MAX_GEARS; i++) {
  406. g[i].max_speed = decode_s16(buff); buff += 2;
  407. g[i].max_torque = decode_s16(buff); buff += 2;
  408. g[i].max_idc = decode_s16(buff); buff += 2;
  409. g[i].zero_accl = decode_u16(buff); buff += 2;
  410. g[i].accl_time = decode_u16(buff); buff += 2;
  411. for (int j = 0; j < CONFIG_GEAR_SPEED_TRQ_NUM; j++) {
  412. g[i].torque[j] = decode_u8(buff++);
  413. if (g[i].torque[j] > 100) {
  414. g[i].torque[j] = 100;
  415. }
  416. }
  417. g[i].max_torque = min(g[i].max_torque, conf.c.max_torque);
  418. g[i].max_idc = min(g[i].max_idc, conf.c.max_idc);
  419. }
  420. return buff - ori;
  421. }
  422. int mc_conf_encode_gear(gear_t *g, u8 *buff) {
  423. u8 *ori = buff;
  424. for (int i = 0; i < CONFIG_MAX_GEARS; i++) {
  425. encode_s16(buff, g[i].max_speed); buff += 2;
  426. encode_s16(buff, g[i].max_torque); buff += 2;
  427. encode_s16(buff, g[i].max_idc); buff += 2;
  428. encode_u16(buff, g[i].zero_accl); buff += 2;
  429. encode_u16(buff, g[i].accl_time); buff += 2;
  430. for (int j = 0; j < CONFIG_GEAR_SPEED_TRQ_NUM; j++) {
  431. encode_u8(buff++, g[i].torque[j]);
  432. }
  433. }
  434. return buff - ori;
  435. }
  436. int mc_conf_decode_limiter(limiter_t *l, int size, u8 *buff) {
  437. u8 *ori = buff;
  438. for (int i = 0; i < size; i++) {
  439. l->enter_pointer = decode_s16(buff);buff += 2;
  440. l->exit_pointer = decode_s16(buff);buff += 2;
  441. l->limit_value = decode_s16(buff);buff += 2;
  442. l++;
  443. }
  444. return buff - ori;
  445. }
  446. int mc_conf_encode_limiter(limiter_t *l, int size, u8 *buff) {
  447. u8 *ori = buff;
  448. for (int i = 0; i < size; i++) {
  449. encode_s16(buff, l->enter_pointer);buff += 2;
  450. encode_s16(buff, l->exit_pointer);buff += 2;
  451. encode_s16(buff, l->limit_value);buff += 2;
  452. l++;
  453. }
  454. return buff - ori;
  455. }
  456. int mc_conf_decode_engreco(u8 *buff) {
  457. u8 *ori = buff;
  458. for (int i = 0; i < CONFIG_EBRK_LVL_NUM; i++) {
  459. conf.e_r[i].torque = decode_s16(buff); buff += 2;
  460. conf.e_r[i].time = decode_s16(buff); buff += 2;
  461. conf.e_r[i].torque = min(conf.e_r[i].torque, conf.c.max_ebrk_torque);
  462. }
  463. return buff - ori;
  464. }
  465. int mc_conf_encode_engreco(u8 *buff) {
  466. u8 *ori = buff;
  467. for (int i = 0; i < CONFIG_EBRK_LVL_NUM; i++) {
  468. encode_s16(buff, conf.e_r[i].torque); buff += 2;
  469. encode_s16(buff, conf.e_r[i].time); buff += 2;
  470. }
  471. return buff - ori;
  472. }
  473. void mc_conf_decode_configs(void) {
  474. u8 *buff = conf_buff;
  475. conf.version = decode_u8(buff++);
  476. buff += mc_conf_decode_motor(buff);
  477. buff += mc_conf_decode_controler(buff);
  478. buff += mc_conf_decode_setting(buff);
  479. buff += mc_conf_decode_gear(conf.g_n, buff);
  480. buff += mc_conf_decode_gear(conf.g_l, buff);
  481. buff += mc_conf_decode_limiter(conf.p_mot, CONFIG_TEMP_PROT_NUM, buff);
  482. buff += mc_conf_decode_limiter(conf.p_mos, CONFIG_TEMP_PROT_NUM, buff);
  483. buff += mc_conf_decode_limiter(&conf.p_vol, 1, buff);
  484. buff += mc_conf_decode_engreco(buff);
  485. buff += mc_conf_decode_cross_zero(buff);
  486. }
  487. int mc_conf_encode_configs(u8 *buff) {
  488. u8 *ori = buff;
  489. encode_u8(buff++, conf.version);
  490. buff += mc_conf_encode_motor(buff);
  491. buff += mc_conf_encode_controler(buff);
  492. buff += mc_conf_encode_setting(buff);
  493. buff += mc_conf_encode_gear(conf.g_n, buff);
  494. buff += mc_conf_encode_gear(conf.g_l, buff);
  495. buff += mc_conf_encode_limiter(conf.p_mot, CONFIG_TEMP_PROT_NUM, buff);
  496. buff += mc_conf_encode_limiter(conf.p_mos, CONFIG_TEMP_PROT_NUM, buff);
  497. buff += mc_conf_encode_limiter(&conf.p_vol, 1, buff);
  498. buff += mc_conf_encode_engreco(buff);
  499. buff += mc_conf_encode_cross_zero(buff);
  500. return buff - ori;
  501. }
  502. void mc_conf_set_pid(u8 id, pid_t *pid) {
  503. if (id < PID_Max_ID) {
  504. conf.c.pid[id] = *pid;
  505. }else if (id == PID_EPM_ExtID){
  506. conf.c.epm_pid = *pid;
  507. }
  508. }
  509. void mc_conf_get_pid(u8 id, pid_t *pid) {
  510. if (id < PID_Max_ID) {
  511. *pid = conf.c.pid[id];
  512. }else if (id == PID_EPM_ExtID){
  513. *pid = conf.c.epm_pid;
  514. }
  515. }
  516. bool mc_conf_set_gear(u8 mode, u8 *data, int len) {
  517. gear_t *g = conf.g_n;
  518. if (mode == 0) {
  519. g = conf.g_l;
  520. }
  521. if (mc_conf_decode_gear(g, data) > len) {
  522. return false;
  523. }
  524. return true;
  525. }
  526. int mc_conf_get_gear(u8 mode, u8 *data) {
  527. gear_t *g = conf.g_n;
  528. if (mode == 0) {
  529. g = conf.g_l;
  530. }
  531. return mc_conf_encode_gear(g, data);
  532. }
  533. bool mc_conf_set_limter(u8 *data, int len) {
  534. int l = mc_conf_decode_limiter(conf.p_mot, CONFIG_TEMP_PROT_NUM, data);
  535. l += mc_conf_decode_limiter(conf.p_mos, CONFIG_TEMP_PROT_NUM, data + l);
  536. l += mc_conf_decode_limiter(&conf.p_vol, 1, data + l);
  537. return len>=l;
  538. }
  539. int mc_conf_get_limter(u8 *data) {
  540. u8 *ori = data;
  541. data += mc_conf_encode_limiter(conf.p_mot, CONFIG_TEMP_PROT_NUM, data);
  542. data += mc_conf_encode_limiter(conf.p_mos, CONFIG_TEMP_PROT_NUM, data);
  543. data += mc_conf_encode_limiter(&conf.p_vol, 1, data);
  544. return data-ori;
  545. }