AssistCurve.h 12 KB

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