AssistCurve.h 13 KB

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