AssistCurve.h 13 KB

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