AssistCurve.h 12 KB

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