AssistCurve.h 14 KB

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