AssistCurve.h 12 KB

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