AssistCurve.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. /**
  2. * @file AssistCurve.c
  3. * @author Zhang, Kai(zhangkai71@midea.com)
  4. * @brief
  5. * @version 0.1
  6. * @date 2021-11-15
  7. *
  8. * @copyright Copyright (c) 2021
  9. *
  10. */
  11. #ifndef ASSISTCURVE_H
  12. #define ASSISTCURVE_H
  13. #include "asr.h"
  14. #include "mathtool.h"
  15. /****************************************
  16. *
  17. * Definitions & Macros
  18. *
  19. ****************************************/
  20. #define TIMEUNIT 1 // run time unit 1ms
  21. #define CURSWITCH 1// 开关电流斜坡
  22. #define GEAR_NUM 6 // number of gear
  23. #define CADENCE_PULSES_PER_CIRC 64
  24. #define BIKE_WHEEL_PERIMETER 219 // CM
  25. #define BIKE_MECH_RATION 35
  26. #define BIKE_SPEED_IQLIMIT_THRESHOLD1 25 // Km/h
  27. #define BIKE_SPEED_IQLIMIT_THRESHOLD2 (BIKE_SPEED_IQLIMIT_THRESHOLD1 + 2) // Km/h
  28. #define BIKE_SPEED_THROTTLE_MAX 6 // Km/h
  29. #define BIKE_FRONTTEETH_NMB 38 // number
  30. #define BIKE_BACKTEETH_NMB 11 // number
  31. #define BIKE_BACKLIGHT_MODE 1 //mode 1-6
  32. #define BIKE_BACKLIGHT_VOLTAGE 6 //6 or 12V
  33. #define BIKE_FORWARDLEDLIGHT_VOLTAGE 6 //6 or 12V
  34. #define BIKE_LIGHT_PARA ((BIKE_BACKLIGHT_MODE<<12)|(BIKE_BACKLIGHT_VOLTAGE<<8)|BIKE_FORWARDLEDLIGHT_VOLTAGE)
  35. #define BIKE_WHEEL_SIZE_ADJUST 0 //cm
  36. #define BIKE_START_MODE 2 //Normal
  37. #define BIKE_POWERSTART_DELAY_TIME 0 //0.2s
  38. #define BIKE_POWEROFF_DELAY_TIME 0 //0.2s
  39. #define BIKE_AUTO_POWER_OFF_TIME 15 //min
  40. #define BIKE_POWER_PARA ((BIKE_POWERSTART_DELAY_TIME<<12)|(BIKE_POWEROFF_DELAY_TIME<<8)|BIKE_AUTO_POWER_OFF_TIME)
  41. #define BIKE_ASSIST_MODE1 0x0155
  42. #define BIKE_ASSIST_MODE2 0x02AA
  43. #define TORQUE_START_THRESHOLD 50 // 0.1Nm
  44. #define TORQUE_STOP_THRESHOLD 50 // 0.1Nm
  45. #define TORQUE_SWITCH2_THRESHOLD 120 // 0.1Nm
  46. #define TORQUE_SWITCH1_THRESHOLD 100 // 0.1Nm
  47. #define BIKE_ASS_MOTOR_CURRENT_MAX 5000 //5500 // 0.01A
  48. #define BIKE_ASS_MOTOR_TORQUE_MAX 30 // 0.1Nm
  49. #define BIKE_SPD_MOTOR_CURRENT_MAX 5 /* 电流系数初始值 */
  50. #define BIKE_SPD_MOTOR_CONSTANT_COMMAND 4096 /* 速度波动补偿系数初始值 */
  51. #define ASS_LIM_DEFAULT \
  52. { \
  53. {0, 716, 870, 1024, 1024, 1024}, 0 \
  54. } // Q10 percentage 0 70% 85% 100% 100% 100%
  55. #define TORQUE_CAL_IN_DEFAULT \
  56. { \
  57. 0, 0, 0, 0, 0, 0, 0, 0, 0 \
  58. }
  59. #define START_GAIN_DEFAULT \
  60. { \
  61. 16384, 10240, 6144, 4778, 4096 \
  62. } // Q12
  63. #define ASS_LINER_TORQUE_DEFAULT \
  64. { \
  65. 2048, 2867, 4096, 10000, 2867 \
  66. } // Q12
  67. //#define TORQUE_ASSIST_DEFAULT \
  68. // { \
  69. // 0, 1290, 1228, 91,\
  70. // 0, 2212, 1024, 91,\
  71. // 0, 3355, 819, 91, \
  72. // 0, 4792, 614, 0,\
  73. // 0, 6636, 409, 0,\
  74. // 0, 8478, 409, 0,\
  75. // 0, 11059, 0, 0,\
  76. // 0, 13639, 0, 0, \
  77. // 0, 16957, 0, 0,\
  78. // 0, 22118, 0, 0,\
  79. // 16589, 22118, 0, 0,\
  80. // 33178, 25805, 0, 0, \
  81. // 66355, 29491, 0, 0, \
  82. // 132710, 29491, 0, 0,\
  83. // 232243, 29491, 0, 0 \
  84. // }
  85. #define TORQUE_ASSIST_DEFAULT \
  86. { \
  87. 0, 0, 2048, 137,\
  88. 0, 0, 2458, 137,\
  89. 0, 0, 2867, 137, \
  90. 0, 7004, 410, 273,\
  91. 0, 8663, 410, 273,\
  92. 0, 10506, 410, 273,\
  93. 9953, 16957, 0, 273,\
  94. 16589, 21012, 0, 273, \
  95. 36495, 22856, 0, 273,\
  96. 82944, 29491, 0, 137,\
  97. 149299, 29491, 0, 137,\
  98. 232243, 29491, 0, 137, \
  99. 82944, 29491, 0, -683,\
  100. 149299, 29491, 0, -683,\
  101. 232243, 29491, 0, -683, \
  102. }
  103. #define CADENCE_ASSIST_DEFAULT \
  104. { \
  105. 0, 0, 10000, 0, 0, 0, 10000, 0, 0, 0, 10000, 0, 0, 0, 10000, 0, 0, 0, 10000, 0 \
  106. }
  107. #define ASSISTMOD_SELECT_DEFAULT 0x0000
  108. //////////////////////////////// just for Assist Module It may has bug when u used in other Module//////////////
  109. #define TIME_MS2CNT(X) ((ULONG)(X) / TIMEUNIT) // X unit:ms
  110. #define PERCENT30(x) (((ULONG)(x)*300) >> 10) // X is variable needed to percentage
  111. #define PERCENT50(x) (((ULONG)(x)*500) >> 10) // X is variable needed to percentage
  112. #define Q12_1 4096 // Q12(1)=4096
  113. #define Q14_1 16384 // Q14(1)=16384
  114. /***************************************
  115. *
  116. * Type Definations
  117. *
  118. ***************************************/
  119. /**
  120. * @brief Polynomial coefficient
  121. * Y = a*X^3 + b*X^2 + c*x +d
  122. *
  123. */
  124. typedef struct
  125. {
  126. SLONG a; // Q12
  127. SLONG b; // Q12
  128. SLONG c; // Q12
  129. SLONG d; // Q12
  130. } POLY_COEF;
  131. /**
  132. * @brief Polynomial coefficient
  133. * Y = z*(x-h)^2 + k
  134. *
  135. */
  136. typedef struct
  137. {
  138. SLONG z; // Q12
  139. SLONG h; // Q12
  140. SLONG k; // Q12
  141. } ORIG_COEF;
  142. /**
  143. * @brief Type of Assist FSM
  144. *
  145. */
  146. typedef enum
  147. {
  148. Startup = 0,
  149. TorqueAssit = 1,
  150. SpeedAssit = 2,
  151. Spd2Torq = 3,
  152. StartupCruise = 4,
  153. StopAssit = 5,
  154. ReduceCurrent = 6
  155. } ASS_FSM_STATUS;
  156. /**
  157. * @brief AssistCurve Para Configure of Bike
  158. *
  159. */
  160. typedef struct
  161. {
  162. // Bike info
  163. UWORD uwWheelPerimeter; // Q0 0.1cm
  164. UWORD uwMechRationMotor;
  165. UWORD uwAssistMaxSpdKmH;
  166. UWORD uwThrottleMaxSpdKmH;
  167. UWORD uwNmFrontChainring;
  168. UWORD uwNmBackChainring;
  169. UWORD uwAssistSelect1;
  170. UWORD uwAssistSelect2;
  171. UWORD uwLightVoltage;
  172. SWORD swDeltPerimeter;
  173. UWORD uwStartMode;
  174. UWORD uwAutoPowerOffTime;
  175. UWORD uwCadPulsePerCirc;
  176. UWORD uwBikeAssTorMaxPu;
  177. // Drive info
  178. UWORD uwMotorPoles;
  179. UWORD uwCofCurMaxPu; // Q14,allowed max Current
  180. UWORD uwCofTorMaxPu; // Q14,allowed max Torque
  181. } ASS_PARA_CONFIGURE;
  182. /**
  183. * @brief AssistCurve Para Set From extern
  184. *
  185. */
  186. typedef struct
  187. {
  188. UWORD uwStartupCoef; // Q12 assist gain during startup
  189. UWORD uwStartupCruiseCoef;
  190. UWORD uwAssistStartNm;
  191. UWORD uwAssistStopNm;
  192. UWORD uwStartUpGainStep;
  193. UWORD uwStartUpCadNm;
  194. UWORD uwTorLPFCadNm;
  195. UWORD uwSpeedAssistSpdRpm;
  196. UWORD uwSpeedAssistIMaxA;
  197. UWORD uwAssistLimitBikeSpdStart;
  198. UWORD uwAssistLimitBikeSpdStop;
  199. UWORD uwCadenceWeight; // Q12 0-4094 Cadence sensor assist weight of the whole assist current��the torque weight = 4096 - uwTorWeight
  200. UWORD uwTorWeight; // Q12 0-4094
  201. UWORD uwTorAssAjstGain; // Q12 0-4094
  202. UWORD uwCadenceAssAjstGain; // Q12 0-4094
  203. UWORD uwAsssistSelectNum;
  204. UWORD uwSpdRegion[3]; // Q15 MOTOR SPEED
  205. UWORD uwSpdRegionGain[3]; // Q12 0-4094
  206. } ASS_PARA_SET;
  207. /**
  208. * @brief AssistCurve persentage input
  209. *
  210. */
  211. typedef struct
  212. {
  213. UWORD uwGearSt; // Assist Gear
  214. SWORD swDirection; // Motor direction
  215. UWORD uwtorque;
  216. UWORD uwtorquelpf;
  217. UWORD uwtorquePer; // Q15, Torque sensor output persentage Tmax = 80Nm
  218. UWORD uwcadancePer; // Q15, Cadance output persentage Fmax = 5Hz
  219. UWORD uwcadance; // Q20, Cadance frequency
  220. UWORD uwcadancelast; // Q20, last step Cadance frequency
  221. UWORD uwcadanceFWCnt; // Cadance pulse count number
  222. UWORD uwbikespeed; // Q20, bikespeed hz
  223. UWORD uwSpdFbkAbsPu; // Q15, bikespeed hz
  224. SWORD swSpdFbkPu;
  225. UWORD uwBaseSpdrpm;
  226. SWORD swFlxIqLimit; // Q14
  227. SWORD swPwrIqLimit; // Q14
  228. UWORD SOCValue;
  229. SWORD swCurFdbPu; //Q14
  230. SWORD swCurRefPu;
  231. } ASS_PER_IN;
  232. typedef struct
  233. {
  234. POLY_COEF uwTorqueAssGain[GEAR_NUM];
  235. POLY_COEF uwCadencAsseGain[GEAR_NUM];
  236. UWORD uwStartupGain;
  237. UWORD uwStartupCruiseGain; // Q12 assist gain during startup cruise
  238. SWORD swCoefStep;
  239. UWORD uwAssThreshold;
  240. UWORD uwAssStopThreshold;
  241. UWORD StartFlag;
  242. SWORD swSmoothGain; // Q12
  243. SWORD swSmoothStopGain; /* 停机电流系数 */
  244. UWORD uwStartUpGainAddStep; // Q12
  245. UWORD uwStartUpTargetGain; // Q12
  246. UWORD uwStartUpTimeCadenceCnt;
  247. UWORD uwSwitch2TorqThreshold;
  248. UWORD uwSwitch1TorqThreshold;
  249. ULONG ulStartupDeltInv;
  250. SWORD swTorqFilterGain; // Q14 Torque filter change from lpf to move average filter
  251. SWORD swCadanceGain;
  252. UWORD uwCadencePeriodCNT;
  253. SWORD swCadanceCNT;
  254. SWORD sw2StopCNT;
  255. SWORD swAss2SpdCNT;
  256. UWORD uwSpeedConstantCommand;
  257. UWORD swSpdLoopAbsCurMax;
  258. SWORD swSpeedlimtrpm;
  259. SWORD swBikeSpeedGain;
  260. UWORD uwCurrentMaxPu;
  261. SWORD swCurrentmax_torAssPu;
  262. SWORD swCurrentmax_cadAssPu;
  263. BOOL blAssistflag;
  264. BOOL blHCadLTorflag;
  265. } ASS_PER_COEF;
  266. typedef struct
  267. {
  268. POLY_COEF uwTorqueAssGain[GEAR_NUM];
  269. POLY_COEF uwCadencAsseGain[GEAR_NUM];
  270. } ASS_PER_COEFIN;
  271. typedef struct
  272. {
  273. UWORD swTorAssistSum1;
  274. UWORD swTorAssistSum2;
  275. SWORD swTorAss2CurrentTemp;
  276. SWORD swCadAss2CurrentTemp;
  277. SWORD swTorAssistCurrentTemp;
  278. SWORD swTorSpdLoopCurrentTemp;
  279. SWORD swTorAssistCurrent;
  280. SWORD swTorRefTarget;
  281. SWORD swTorRefEnd;
  282. SWORD swSpeedRef;
  283. SWORD swCadSpd2MotSpd;
  284. SWORD swVoltLimitPu;
  285. BOOL blAssHoldFlag;
  286. BOOL blTorqPIFlg;
  287. SWORD swAssitCurRef;
  288. } ASS_PER_OUT;
  289. /**
  290. * @brief AssistCurve current limit input
  291. *
  292. */
  293. typedef struct
  294. {
  295. UWORD uwIqlimit; // Q14, Iq limit
  296. } ASS_CURLIM_OUT;
  297. /**
  298. * @brief AssistCurve current limit input
  299. *
  300. */
  301. typedef struct
  302. {
  303. UWORD uwLimitGain[GEAR_NUM];
  304. UWORD uwSpdThresHold; // Q15 motor speed to decided whether ass or not
  305. UWORD uwBikeSpdThresHold1; // Q15 the start bike speed to limit iqref
  306. UWORD uwBikeSpdThresHold2; // Q15 the end bike speed to limit iqref
  307. ULONG ulBikeSpdDeltInv; // Q14
  308. UWORD uwBikeSpdIqLimitK;
  309. } ASS_CURLIM_COEF;
  310. typedef struct
  311. {
  312. SWORD swValue;
  313. UWORD uwLength; // 6: 2^6 value to average
  314. SLONG slSum;
  315. SLONG slAverValue;
  316. SWORD *swBuffer;
  317. UWORD uwIndex;
  318. BOOL blSecFlag;
  319. } MAF_IN;
  320. typedef struct
  321. {
  322. UWORD uwMotprPolePairs;
  323. UWORD uwMotorFluxWb;
  324. SLONG swCalCoefINV;
  325. } TOR2CURRENT_CAL_COEF;
  326. typedef struct
  327. {
  328. UWORD uwIqLimitInitAbs;
  329. UWORD uwIqLimitStartSoc;
  330. UWORD uwIqLimitEndSoc;
  331. SWORD swIqLImitK;
  332. } ASS_LIMIT_ACCORDING_VOL_COF;
  333. typedef struct
  334. {
  335. UWORD uwIqLimitAbs;
  336. } ASS_LIMIT_ACCORDING_VOL_OUT;
  337. typedef struct
  338. {
  339. SWORD swImaxPu;
  340. SWORD swIminPu;
  341. SWORD swTorqRefPu;
  342. SWORD swTorqFdbPu;
  343. } ASS_TORQ_PI_IN;
  344. typedef struct
  345. {
  346. SWORD swErrZ1Pu;
  347. SLONG slIRefPu;
  348. SWORD swIRefPu;
  349. } ASS_TORQ_PI_OUT;
  350. /****************************************
  351. *
  352. * Exported variable
  353. *
  354. ****************************************/
  355. extern ASS_FSM_STATUS Ass_FSM;
  356. extern ASS_PER_IN ass_stCalIn;
  357. extern ASS_PER_COEF ass_stCalCoef;
  358. extern ASS_PER_OUT ass_stCalOut;
  359. extern ASS_PARA_CONFIGURE ass_stParaCong;
  360. extern ASS_PARA_SET ass_stParaSet;
  361. extern ASS_LIMIT_ACCORDING_VOL_COF ass_stCurLimCalBMSCoef;
  362. extern ASS_LIMIT_ACCORDING_VOL_OUT ass_stCurLimitCalBMSOut;
  363. extern ASS_CURLIM_COEF ass_stCurLimCoef;
  364. extern ASS_CURLIM_OUT ass_stCurLimOut;
  365. extern ASR_SPDPI_IN asr_stTorqSpdPIIn;
  366. extern ASR_SPDPI_OUT asr_stTorqSpdPIOut;
  367. extern ASR_SPDPI_COF asr_stTorqSpdPICoef;
  368. extern ASR_SPDPI_COFIN asr_stTorqSpdPICoefIn;
  369. extern ASS_TORQ_PI_IN ass_stTorqPIIn;
  370. extern ASS_TORQ_PI_OUT ass_stTorqPIOut;
  371. extern SWORD ass_swTorqMafBuf[64];
  372. extern MAF_IN ass_stTorqMafValue;
  373. extern SWORD ass_swUqLimMafBuf[64];
  374. extern MAF_IN ass_stUqLimMafValue;
  375. extern LPF_OUT ass_pvt_stCurLpf;
  376. /************************************************************************
  377. Ram Allocation
  378. *************************************************************************/
  379. /***************************************
  380. *
  381. * Function Definations
  382. *
  383. ***************************************/
  384. void ass_voAssitEEInit(void);
  385. void ass_voAssitCoef(void);
  386. void ass_voAssist(void);
  387. void ass_voMoveAverageFilter(MAF_IN *in);
  388. void ass_voMoveAverageFilterClear(MAF_IN *in);
  389. void ass_voAssitTorqPI(ASS_TORQ_PI_IN *in, ASS_TORQ_PI_OUT *out);
  390. #endif