AssistCurve.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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 70 // 0.1Nm
  51. #define TORQUE_STOP_THRESHOLD 30 // 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. Prepare = 7
  162. } ASS_FSM_STATUS;
  163. /**
  164. * @brief AssistCurve Para Configure of Bike
  165. *
  166. */
  167. typedef struct
  168. {
  169. // Bike info
  170. UWORD uwWheelPerimeter; // Q0 0.1cm
  171. UWORD uwMechRationMotor;
  172. UWORD uwAssistMaxSpdKmH;
  173. UWORD uwThrottleMaxSpdKmH;
  174. UWORD uwNmFrontChainring;
  175. UWORD uwNmBackChainring;
  176. UWORD uwAssistSelect1;
  177. UWORD uwAssistSelect2;
  178. UWORD uwLightVoltage;
  179. SWORD swDeltPerimeter;
  180. UWORD uwStartMode;
  181. UWORD uwAutoPowerOffTime;
  182. UWORD uwThrottleSmooth;
  183. UWORD uwCadPulsePerCirc;
  184. UWORD uwBikeAssTorMaxPu;
  185. // Drive info
  186. UWORD uwMotorPoles;
  187. UWORD uwCofCurMaxPu; // Q14,allowed max Current
  188. UWORD uwCofTorMaxPu; // Q14,allowed max Torque
  189. } ASS_PARA_CONFIGURE;
  190. /**
  191. * @brief AssistCurve Para Set From extern
  192. *
  193. */
  194. typedef struct
  195. {
  196. UWORD uwStartupCoef; // Q12 assist gain during startup
  197. UWORD uwStartupCruiseCoef;
  198. UWORD uwAssistStartNm;
  199. UWORD uwAssistStopNm;
  200. UWORD uwStartUpGainStep;
  201. UWORD uwStartUpCadNm;
  202. UWORD uwTorLPFCadNm;
  203. UWORD uwSpeedAssistSpdRpm;
  204. UWORD uwSpeedAssistIMaxA;
  205. UWORD uwAssistLimitBikeSpdStart;
  206. UWORD uwAssistLimitBikeSpdStop;
  207. UWORD uwCadenceWeight; // Q12 0-4094 Cadence sensor assist weight of the whole assist current��the torque weight = 4096 - uwTorWeight
  208. UWORD uwTorWeight; // Q12 0-4094
  209. UWORD uwTorAssAjstGain; // Q12 0-4094
  210. UWORD uwCadenceAssAjstGain; // Q12 0-4094
  211. UWORD uwAsssistSelectNum;
  212. UWORD uwSpdRegion[3]; // Q15 MOTOR SPEED
  213. UWORD uwSpdRegionGain[3]; // Q12 0-4094
  214. } ASS_PARA_SET;
  215. /**
  216. * @brief AssistCurve persentage input
  217. *
  218. */
  219. typedef struct
  220. {
  221. UWORD uwGearSt; // Assist Gear
  222. SWORD swDirection; // Motor direction
  223. UWORD uwtorque;
  224. UWORD uwtorquelpf;
  225. UWORD uwtorquePer; // Q15, Torque sensor output persentage Tmax = 80Nm
  226. UWORD uwcadancePer; // Q15, Cadance output persentage Fmax = 5Hz
  227. UWORD uwcadance; // Q20, Cadance frequency
  228. UWORD uwcadancelast; // Q20, last step Cadance frequency
  229. UWORD uwcadanceFWCnt; // Cadance pulse count number
  230. UWORD uwbikespeed; // Q20, bikespeed hz
  231. UWORD uwSpdFbkAbsPu; // Q15, bikespeed hz
  232. SWORD swSpdFbkPu;
  233. UWORD uwBaseSpdrpm;
  234. SWORD swFlxIqLimit; // Q14
  235. SWORD swPwrIqLimit; // Q14
  236. UWORD SOCValue;
  237. SWORD swCurFdbPu; //Q14
  238. SWORD swCurRefPu;
  239. } ASS_PER_IN;
  240. typedef struct
  241. {
  242. POLY_COEF uwTorqueAssGain[GEAR_NUM];
  243. POLY_COEF uwCadencAsseGain[GEAR_NUM];
  244. UWORD uwStartupGain;
  245. UWORD uwStartupCruiseGain; // Q12 assist gain during startup cruise
  246. SWORD swCoefStep;
  247. UWORD uwAssThreshold;
  248. UWORD uwAssStopThreshold;
  249. UWORD StartFlag;
  250. SWORD swSmoothGain; // Q12
  251. SWORD swSmoothStopGain; /* 停机电流系数 */
  252. UWORD uwStartUpGainAddStep; // Q12
  253. UWORD uwStartUpTargetGain; // Q12
  254. UWORD uwStartUpTimeCadenceCnt;
  255. UWORD uwSwitch2TorqThreshold;
  256. UWORD uwSwitch1TorqThreshold;
  257. ULONG ulStartupDeltInv;
  258. SWORD swTorqFilterGain; // Q14 Torque filter change from lpf to move average filter
  259. SWORD swCadanceGain;
  260. UWORD uwCadencePeriodCNT;
  261. SWORD swCadanceCNT;
  262. SWORD sw2StopCNT;
  263. SWORD swAss2SpdCNT;
  264. UWORD uwSpeedConstantCommand;
  265. UWORD swSpdLoopAbsCurMax;
  266. SWORD swSpeedlimtrpm;
  267. SWORD swBikeSpeedGain;
  268. UWORD uwCurrentMaxPu;
  269. SWORD swCurrentmax_torAssPu;
  270. SWORD swCurrentmax_cadAssPu;
  271. BOOL blAssistflag;
  272. BOOL blHCadLTorflag;
  273. } ASS_PER_COEF;
  274. typedef struct
  275. {
  276. POLY_COEF uwTorqueAssGain[GEAR_NUM];
  277. POLY_COEF uwCadencAsseGain[GEAR_NUM];
  278. } ASS_PER_COEFIN;
  279. typedef struct
  280. {
  281. UWORD swTorAssistSum1;
  282. UWORD swTorAssistSum2;
  283. SWORD swTorAss2CurrentTemp;
  284. SWORD swCadAss2CurrentTemp;
  285. SWORD swTorAssistCurrentTemp;
  286. SWORD swTorSpdLoopCurrentTemp;
  287. SWORD swTorAssistCurrent;
  288. SWORD swTorRefTarget;
  289. SWORD swTorRefEnd;
  290. SWORD swSpeedRef;
  291. SWORD swCadSpd2MotSpd;
  292. SWORD swVoltLimitPu;
  293. BOOL blAssHoldFlag;
  294. BOOL blTorqPIFlg;
  295. SWORD swAssitCurRef;
  296. } ASS_PER_OUT;
  297. /**
  298. * @brief AssistCurve current limit input
  299. *
  300. */
  301. typedef struct
  302. {
  303. UWORD uwIqlimit; // Q14, Iq limit
  304. } ASS_CURLIM_OUT;
  305. /**
  306. * @brief AssistCurve current limit input
  307. *
  308. */
  309. typedef struct
  310. {
  311. UWORD uwLimitGain[GEAR_NUM];
  312. UWORD uwSpdThresHold; // Q15 motor speed to decided whether ass or not
  313. UWORD uwBikeSpdThresHold1; // Q15 the start bike speed to limit iqref
  314. UWORD uwBikeSpdThresHold2; // Q15 the end bike speed to limit iqref
  315. ULONG ulBikeSpdDeltInv; // Q14
  316. UWORD uwBikeSpdIqLimitK;
  317. } ASS_CURLIM_COEF;
  318. typedef struct
  319. {
  320. SWORD swValue;
  321. UWORD uwLength; // 6: 2^6 value to average
  322. SLONG slSum;
  323. SLONG slAverValue;
  324. SWORD *swBuffer;
  325. UWORD uwIndex;
  326. BOOL blSecFlag;
  327. } MAF_IN;
  328. typedef struct
  329. {
  330. UWORD uwMotprPolePairs;
  331. UWORD uwMotorFluxWb;
  332. SLONG swCalCoefINV;
  333. } TOR2CURRENT_CAL_COEF;
  334. typedef struct
  335. {
  336. UWORD uwIqLimitInitAbs;
  337. UWORD uwIqLimitStartSoc;
  338. UWORD uwIqLimitEndSoc;
  339. SWORD swIqLImitK;
  340. } ASS_LIMIT_ACCORDING_VOL_COF;
  341. typedef struct
  342. {
  343. UWORD uwIqLimitAbs;
  344. } ASS_LIMIT_ACCORDING_VOL_OUT;
  345. typedef struct
  346. {
  347. SWORD swImaxPu;
  348. SWORD swIminPu;
  349. SWORD swTorqRefPu;
  350. SWORD swTorqFdbPu;
  351. } ASS_TORQ_PI_IN;
  352. typedef struct
  353. {
  354. SWORD swErrZ1Pu;
  355. SLONG slIRefPu;
  356. SWORD swIRefPu;
  357. } ASS_TORQ_PI_OUT;
  358. /****************************************
  359. *
  360. * Exported variable
  361. *
  362. ****************************************/
  363. extern ASS_FSM_STATUS Ass_FSM;
  364. extern ASS_PER_IN ass_stCalIn;
  365. extern ASS_PER_COEF ass_stCalCoef;
  366. extern ASS_PER_OUT ass_stCalOut;
  367. extern ASS_PARA_CONFIGURE ass_stParaCong;
  368. extern ASS_PARA_SET ass_stParaSet;
  369. extern ASS_LIMIT_ACCORDING_VOL_COF ass_stCurLimCalBMSCoef;
  370. extern ASS_LIMIT_ACCORDING_VOL_OUT ass_stCurLimitCalBMSOut;
  371. extern ASS_CURLIM_COEF ass_stCurLimCoef;
  372. extern ASS_CURLIM_OUT ass_stCurLimOut;
  373. extern ASR_SPDPI_IN asr_stTorqSpdPIIn;
  374. extern ASR_SPDPI_OUT asr_stTorqSpdPIOut;
  375. extern ASR_SPDPI_COF asr_stTorqSpdPICoef;
  376. extern ASR_SPDPI_COFIN asr_stTorqSpdPICoefIn;
  377. extern ASS_TORQ_PI_IN ass_stTorqPIIn;
  378. extern ASS_TORQ_PI_OUT ass_stTorqPIOut;
  379. extern SWORD ass_swTorqMafBuf[64];
  380. extern MAF_IN ass_stTorqMafValue;
  381. extern SWORD ass_swUqLimMafBuf[64];
  382. extern MAF_IN ass_stUqLimMafValue;
  383. extern LPF_OUT ass_pvt_stCurLpf;
  384. /************************************************************************
  385. Ram Allocation
  386. *************************************************************************/
  387. /***************************************
  388. *
  389. * Function Definations
  390. *
  391. ***************************************/
  392. void ass_voAssitEEInit(void);
  393. void ass_voAssitCoef(void);
  394. void ass_voAssist(void);
  395. void ass_voMoveAverageFilter(MAF_IN *in);
  396. void ass_voMoveAverageFilterClear(MAF_IN *in);
  397. //void ass_voAssitTorqPI(ASS_TORQ_PI_IN *in, ASS_TORQ_PI_OUT *out);
  398. #ifdef __cplusplus
  399. }
  400. #endif // __cplusplus
  401. #endif