AssistCurve.h 12 KB

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