AssistCurve.h 14 KB

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