motor_control.c 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830
  1. #include "motor_control.h"
  2. #include "MC_FOC_driver.h"
  3. #include "MC_PID_regulators.h"
  4. #include "MC_Globals.h"
  5. #include "stm32f10x_svpwm_3shunt.h"
  6. #include "hall_sensor.h"
  7. #include "cadence_sensor.h"
  8. #include "speed_sensor.h"
  9. #include "torque_sensor.h"
  10. #include "gas_sensor.h"
  11. #include "key_driver.h"
  12. #include "pwm_driver.h"
  13. #include "math_tools.h"
  14. #include "power12V_driver.h"
  15. #include "ICM20600.h"
  16. /************************全局变量定义************************/
  17. //工作模式
  18. MC_WorkMode_Struct_t MC_WorkMode = MC_WorkMode_Run;
  19. MC_WorkMode_Struct_t MC_WorkMode_Back = ~MC_WorkMode_Run;
  20. //MC_CTL控制指令
  21. MC_ControlCode_Struct_t MC_ControlCode= {MC_GearSt_OFF, MC_LightSwitch_OFF};
  22. MC_ControlCode_Struct_t MC_ControlCode_Back = {(MC_GearSt_Struct_t)~MC_GearSt_OFF, (MC_LightSwitch_Struct_t)~MC_LightSwitch_OFF};
  23. //电机控制计算参数
  24. MC_CalParam_Struct_t MC_CalParam = {MC_AssistRunMode_INVALID, 0, 0, RESET};
  25. MC_CalParam_Struct_t MC_CalParam_Back = {(MC_AssistRunMode_Struct_t)~MC_AssistRunMode_INVALID, ~0, ~0, (FlagStatus)~RESET};
  26. //踏频限流系数
  27. uint8_t MC_CadenceLimit_K = 100;
  28. //力矩助力控制参数
  29. MC_TorqueProcess_Param_Struct_t MC_TorqueProcess_Param = {SET, 0, 0, 0};
  30. //推行助力控制参数
  31. MC_WalkProcess_Param_Struct_t MC_WalkProcess_Param = {FALSE, 0};
  32. //电机启动标志
  33. FlagStatus MC_StarFlag = RESET;
  34. /*************************局部函数定义***********************/
  35. //设定值线性变化处理
  36. uint16_t MC_DataSet_Linear_Process(uint16_t SetData, uint16_t PresentData, uint16_t AddCnt, uint16_t DecCnt)
  37. {
  38. int16_t ErrorData;
  39. uint16_t Result;
  40. ErrorData = SetData - PresentData;
  41. if(ErrorData > 0) //升速
  42. {
  43. if(ErrorData >= AddCnt)
  44. {
  45. Result = PresentData + AddCnt;
  46. }
  47. else
  48. {
  49. Result = SetData;
  50. }
  51. }
  52. else if(ErrorData < 0) //降速
  53. {
  54. if((-ErrorData) >= DecCnt)
  55. {
  56. Result = PresentData - DecCnt;
  57. }
  58. else
  59. {
  60. Result = SetData;
  61. }
  62. }
  63. else
  64. {
  65. Result = SetData;
  66. }
  67. return Result;
  68. }
  69. //随电压计算助力衰减系数
  70. uint16_t MC_Cal_K_ByVoltage(uint16_t Voltage, uint16_t DesignVol, uint16_t K_Voltage_Old)
  71. {
  72. uint32_t Cal_Temp;
  73. uint16_t SetVol_Th = 0;
  74. uint16_t ResetVol_Th = 0;
  75. uint16_t Result = 1024;
  76. //根据马达额定电压设定衰减点、衰减系数和恢复点
  77. switch(DesignVol)
  78. {
  79. case 24:
  80. {
  81. SetVol_Th = 33 * 7;
  82. ResetVol_Th = 36 * 7;
  83. Cal_Temp = (Voltage > (SetVol_Th * 100)) ? 1024 : (uint16_t)((uint32_t)(Voltage) / SetVol_Th);//Voltage单位0.001V, SetVol_Th单位0.1V, 系数放大100倍
  84. break;
  85. }
  86. case 36:
  87. {
  88. SetVol_Th = 33 * 10;
  89. ResetVol_Th = 36 * 10;
  90. Cal_Temp = (Voltage > SetVol_Th * 100) ? 1024 : (uint16_t)((uint32_t)(Voltage) / SetVol_Th);//Voltage单位0.001V, SetVol_Th单位0.1V, 系数放大100倍
  91. break;
  92. }
  93. case 48:
  94. {
  95. SetVol_Th = 33 * 13;
  96. ResetVol_Th = 36 * 13;
  97. Cal_Temp = (Voltage > SetVol_Th * 100) ? 1024 : (uint16_t)((uint32_t)(Voltage) / SetVol_Th);//Voltage单位0.001V, SetVol_Th单位0.1V, 系数放大100倍
  98. break;
  99. }
  100. default:
  101. {
  102. Cal_Temp = 100;
  103. break;
  104. }
  105. }
  106. Cal_Temp = Cal_Temp * Cal_Temp / 100 * Cal_Temp / 100 * Cal_Temp * 1024 / 10000; //f(x) = x^4
  107. Cal_Temp = (Cal_Temp < 820) ? 820 : Cal_Temp;
  108. //系数仅衰减
  109. if(Cal_Temp < K_Voltage_Old)
  110. {
  111. Result = Cal_Temp;
  112. }
  113. else
  114. {
  115. Result = K_Voltage_Old;
  116. }
  117. //高于设定恢复电压后,恢复系数
  118. if(Voltage > (ResetVol_Th * 100))
  119. {
  120. Result = 1024;
  121. }
  122. return(Result);
  123. }
  124. //随温度计算助力衰减系数
  125. uint16_t MC_Cal_K_ByTemperature(uint16_t CoilTemp, uint16_t AlarmTempTH)
  126. {
  127. uint32_t CalTemp;
  128. uint16_t Result = 1024;
  129. if(CoilTemp > AlarmTempTH)
  130. {
  131. CalTemp = (uint32_t)AlarmTempTH * AlarmTempTH * 1024;
  132. Result = (uint16_t)(CalTemp / CoilTemp / CoilTemp);
  133. }
  134. else
  135. {
  136. Result = 1024;
  137. }
  138. return(Result);
  139. }
  140. //助力模式判断处理
  141. MC_AssistRunMode_Struct_t MC_JudgeAsistRunMode_Process(MC_SupportFlag_Struct_t GasCtrlMode, uint16_t GasSensorData, MC_GearSt_Struct_t GearSt, TrueOrFalse_Flag_Struct_t StopFlag)
  142. {
  143. MC_AssistRunMode_Struct_t MC_AssistRunMode_Result;
  144. if(MC_ErrorCode.Code == 0) // 无故障
  145. {
  146. if((GearSt != MC_GearSt_OFF) && (StopFlag == FALSE))
  147. {
  148. //进入指拨模式
  149. if((GasSensorData > 100) && (GasCtrlMode == MC_SUPPORT_ENABLE))
  150. {
  151. MC_AssistRunMode_Result = MC_AssistRunMode_GAS;
  152. }
  153. //退出指拨模式
  154. else if(GasSensorData < 50)
  155. {
  156. //进入推行模式
  157. if(GearSt == MC_GearSt_WALK)
  158. {
  159. MC_AssistRunMode_Result = MC_AssistRunMode_WALK;
  160. }
  161. else
  162. {
  163. //进入踏频模式
  164. if(((GearSt & 0xF0) != 0) && (GearSt != MC_GearSt_SMART))
  165. {
  166. MC_AssistRunMode_Result = MC_AssistRunMode_CADENCE;
  167. }
  168. //进入力矩模式
  169. else
  170. {
  171. MC_AssistRunMode_Result = MC_AssistRunMode_TORQUE;
  172. }
  173. }
  174. }
  175. }
  176. else
  177. {
  178. MC_AssistRunMode_Result = MC_AssistRunMode_INVALID;
  179. }
  180. Power12V_Driver_Process(SET);
  181. }
  182. else //存在故障
  183. {
  184. MC_AssistRunMode_Result = MC_AssistRunMode_INVALID;
  185. MC_ControlCode.GearSt = MC_GearSt_OFF;
  186. MC_ControlCode_Back.GearSt = (MC_GearSt_Struct_t)~MC_ControlCode.GearSt;
  187. #if 0
  188. Power12V_Driver_Process(RESET);
  189. #endif
  190. }
  191. return MC_AssistRunMode_Result;
  192. }
  193. /*指拨模式相关变量*/
  194. static int32_t SpdMotorDivWheelFlt=0;
  195. int16_t SpdProportion=490; //车轮电机速度比
  196. static uint16_t SpdProportion_buff_CNT=0;
  197. uint8_t SpdProportion_CAL_flag=0;
  198. static uint16_t SpdProportion_Save_CNT=0;
  199. uint16_t SpdProportion_buff[100]={0};
  200. float SpdProportion_StandardDeviation=0;
  201. int32_t test_StandardDeviation=0;
  202. uint16_t test_SpdProportionAver=0;
  203. int32_t SpeedSetMiddle=0;
  204. int16_t dbSpdWheelSet=0; //调试用
  205. int16_t wheelSpeed=0;
  206. static int16_t DbSpdMotorPre=0;
  207. static int16_t wheelSpeedPre=0;
  208. int16_t SpdMotorDivWheel=0;
  209. int16_t SpdMotorDivWheelFlted=0;
  210. int16_t SpeedMax = 0; // 最高时速
  211. int16_t SpeedSet = 0; // 速度设定值
  212. uint32_t accStep = 0; // 加速时间步进
  213. uint32_t decStep = 0; // 减速时间步进
  214. int16_t SpeedSetReal = 0; // 速度设定真实值
  215. /*指拨模式相关变量*/
  216. //指拨模式处理
  217. MC_CalParam_Struct_t MC_AssistRunMode_Gas_Process(uint16_t SensorData, MC_GearSt_Struct_t GearSt)
  218. {
  219. int32_t Tmp;
  220. int16_t TorQueBySpd = 0;
  221. int32_t Ref_Speed_Temp;
  222. int16_t SpdMotorByIdc = 0;
  223. MC_CalParam_Struct_t p_MC_CalParam = {MC_AssistRunMode_INVALID, 0, 0, RESET};
  224. //...插入指拨处理
  225. /*车轮速度使用原始数据,滤波后的数据有滞后,影响控制回路*/
  226. wheelSpeed = (int16_t)MC_SpeedSensorData.Speed_Data;
  227. /*实时计算电机转速与车轮速的比值*/
  228. SpdMotorDivWheel = (uint32_t)(MC_RunInfo.MotorSpeed * 100) / wheelSpeed ;
  229. Tmp = SpdMotorDivWheel;
  230. SpdMotorDivWheelFlt += ((Tmp<<8) - SpdMotorDivWheelFlt) >> 6;
  231. SpdMotorDivWheelFlted = SpdMotorDivWheelFlt>>8;
  232. /*在电机转速与车轮速比值,与实际速比一致时,更新速比*/
  233. if( ( MC_RunInfo.MotorSpeed > 100 ) && ( wheelSpeed > 0 ))
  234. {
  235. /*加速时,更新速比,比较法*/
  236. if((wheelSpeed - wheelSpeedPre ) > 5)
  237. {
  238. if(( MC_RunInfo.MotorSpeed - DbSpdMotorPre )>0)
  239. {
  240. SpdProportion = SpdMotorDivWheel ;
  241. }
  242. DbSpdMotorPre = MC_RunInfo.MotorSpeed;
  243. }
  244. wheelSpeedPre = wheelSpeed;
  245. /*求标准差,速比稳定后,更新速比*/
  246. /*此处将数据保存到数组中,标准差计算,时间较长,放在主循环进行*/
  247. if((SpdProportion_CAL_flag==0) && (MC_CalParam.Ref_Speed > 25)) //电机力矩控制量低于25时,认为是空载,此时不更新速比
  248. {
  249. SpdProportion_Save_CNT++;
  250. /*40ms保存一次数据到数组*/
  251. if(SpdProportion_Save_CNT >= 40 )
  252. {
  253. SpdProportion_Save_CNT = 0;
  254. SpdProportion_buff[SpdProportion_buff_CNT] = SpdMotorDivWheelFlted;
  255. SpdProportion_buff_CNT++;
  256. if( SpdProportion_buff_CNT >=50 )
  257. {
  258. SpdProportion_buff_CNT = 0;
  259. /*标志位置1,主循环里求标准差*/
  260. SpdProportion_CAL_flag = 1;
  261. }
  262. }
  263. }
  264. }
  265. /*电机最高速度,上位机配置参数*/
  266. SpeedMax = MC_MotorParam.Rate_Speed;
  267. Tmp = SensorData + 50 ; //加50偏移量,确保能达到最大值2048
  268. Tmp = Tmp > 2048 ? 2048 : Tmp;
  269. // /*调试用,根据车速限速值,换算指拨对应的设定车速*/
  270. dbSpdWheelSet = (Tmp * MC_ConfigParam1.SpeedLimit * 10 )>> 11;
  271. //if(((int16_t)wheelSpeed - (int16_t)dbSpdWheelSet) < 100 )
  272. //|| (dbSpdWheelSet < 50 ) )
  273. // {
  274. /*电机转速设定,根据指拨大小、车轮限速值和速比,换算*/
  275. SpeedSet = ((Tmp * MC_ConfigParam1.SpeedLimit * SpdProportion) / 10 >> 11); //(Tmp >> 11) * cd_Speedlimit * ( SpdProportion / 10)
  276. //SpeedSet = ((Tmp * cd_MotorSpeed) >> 11); //调试
  277. //超过限速值,设定电机转速为0
  278. if(wheelSpeed > (MC_ConfigParam1.SpeedLimit * 10 + 10))
  279. {
  280. SpeedSet = 0;
  281. }
  282. SpeedSet = (SpeedSet > 0) ? SpeedSet : 0;
  283. SpeedSet = (SpeedSet < SpeedMax) ? SpeedSet : SpeedMax;
  284. switch (GearSt & 0x0F)//Help_mode:bit4: 0-力矩模式,1-踏频模式;低四位表示助力档位
  285. {
  286. case 0x01:
  287. accStep = StepCalc(SpeedMax,1,6500);
  288. break;
  289. case 0x02:
  290. accStep = StepCalc(SpeedMax,1,6000);
  291. break;
  292. case 0x03:
  293. accStep = StepCalc(SpeedMax,1,5500);
  294. break;
  295. case 0x04:
  296. accStep = StepCalc(SpeedMax,1,5000);
  297. break;
  298. default:
  299. /*计算周期1ms, 加减速时间为 5.00s 加减速步进计算*/
  300. accStep = StepCalc(SpeedMax,1,5000);
  301. break;
  302. }
  303. /*减速步进*/
  304. decStep = StepCalc(SpeedMax,1,1000);
  305. /* 跟踪启动 */
  306. if(MC_CalParam.Foc_Flag == RESET)
  307. {
  308. //MotorStartFlg = 1;
  309. if(MC_RunInfo.MotorSpeed > 100)
  310. {
  311. SpeedSetReal = MC_RunInfo.MotorSpeed;
  312. SpeedSetMiddle = SpeedSetReal << 16;
  313. }
  314. }
  315. /*速度指令的加减速处理*/
  316. SpeedSetReal = accDecProcess(SpeedSet,accStep,decStep,&SpeedSetMiddle);
  317. /*母线电流限流*/
  318. PID_IMax.hLower_Limit_Output= -(MC_AssisParam.Gear_TURBO.Upper_Iq / 2); //Lower Limit for Output limitation
  319. PID_IMax.hUpper_Limit_Output= 0; //Upper Limit for Output limitation
  320. PID_IMax.wLower_Limit_Integral = -((MC_AssisParam.Gear_TURBO.Upper_Iq / 2) << 10); // 放大1024
  321. PID_IMax.wUpper_Limit_Integral = 0; // 放大1024
  322. SpdMotorByIdc = PID_Regulator((MC_ConfigParam1.CurrentLimit * 1000) >> 7, MC_RunInfo.BusCurrent >> 7, &PID_ConstantPower); // 母线电流闭环
  323. /* 电机速度闭环 */
  324. /*最大力矩为4档的力矩参数*/
  325. PID_MotorSpd.hLower_Limit_Output= -(MC_AssisParam.Gear_TURBO.Upper_Iq / 2);
  326. PID_MotorSpd.hUpper_Limit_Output= (MC_AssisParam.Gear_TURBO.Upper_Iq / 2);
  327. // PID_MotorSpd.wLower_Limit_Integral = -((ContrlParam.Gear_4st.Upper_Iq / 2) << 10); // 放大1024
  328. // PID_MotorSpd.wUpper_Limit_Integral = ((ContrlParam.Gear_4st.Upper_Iq / 2) << 10); // 放大1024
  329. TorQueBySpd = PID_Regulator(SpeedSetReal, \
  330. MC_RunInfo.MotorSpeed , \
  331. &PID_MotorSpd); // 电机速度闭环输出
  332. TorQueBySpd += SpdMotorByIdc;
  333. #if 1
  334. static uint16_t K_ByVoltage_Set_Old = 1024;
  335. uint16_t K_ByVoltage_Set;
  336. static uint16_t K_ByVoltage_Result;
  337. uint16_t K_ByTemperature_Set;
  338. static uint16_t K_ByTemperature_Result;
  339. //根据电压调节输出
  340. K_ByVoltage_Set = MC_Cal_K_ByVoltage(MC_RunInfo.BusVoltage, MC_MotorParam.Rate_Voltage, K_ByVoltage_Set_Old);//根据母线电压计算衰减比例,递减
  341. K_ByVoltage_Set_Old = K_ByVoltage_Set;
  342. K_ByVoltage_Result = MC_DataSet_Linear_Process(K_ByVoltage_Set, K_ByVoltage_Result, 1, 1); //设定值与给定值线性处理
  343. //根据温度调节输出
  344. K_ByTemperature_Set = MC_Cal_K_ByTemperature(MC_RunInfo.T_Coil, MC_ConfigParam1.TempTH_Alarm); //根据温度计算衰减比例
  345. K_ByTemperature_Result = MC_DataSet_Linear_Process(K_ByTemperature_Set, K_ByTemperature_Result, 1, 1); //设定值与给定值线性处理
  346. #else
  347. uint16_t K_ByVoltage_Result = 1024;
  348. uint16_t K_ByTemperature_Result = 1024;
  349. #endif
  350. //速度环控制量为0时停机,防止电机出现异响
  351. if(SpeedSetReal == 0)
  352. {
  353. MC_MotorStop(&MC_StarFlag);
  354. }
  355. else
  356. {
  357. //电机启动
  358. MC_MotorStar(&MC_StarFlag);
  359. }
  360. Ref_Speed_Temp = ((int32_t)TorQueBySpd * K_ByVoltage_Result) >> 10;
  361. Ref_Speed_Temp = ((int32_t)Ref_Speed_Temp * K_ByTemperature_Result) >> 10;
  362. p_MC_CalParam.Ref_Speed = (int16_t)(Ref_Speed_Temp);
  363. p_MC_CalParam.Foc_Flag = SET;
  364. p_MC_CalParam.AssistRunMode = MC_AssistRunMode_GAS;
  365. return (p_MC_CalParam);
  366. }
  367. //推行模式处理
  368. MC_CalParam_Struct_t MC_AssistRunMode_Walk_Process(MC_WorkMode_Struct_t p_MC_WorkMode)
  369. {
  370. MC_CalParam_Struct_t p_MC_CalParam = {MC_AssistRunMode_INVALID, 0, 0, RESET};
  371. int16_t TorQueBySpd = 0;
  372. int32_t Ref_Speed_Temp;
  373. int16_t SpdMotorByIdc = 0;
  374. uint8_t StepData = 0;
  375. //配置模式,设定转速 = 最高转速
  376. if(p_MC_WorkMode == MC_WorkMode_Config)
  377. {
  378. StepData = (MC_MotorParam.Rate_Speed << 5) / 5000;//设计5s加速到最大值
  379. StepData = (StepData < 1) ? 1 : StepData;
  380. if(MC_WalkProcess_Param.MotorSpeedSetBigin < (MC_MotorParam.Rate_Speed << 5) * MC_WalkMode_Persent / 100 - 10)
  381. {
  382. MC_WalkProcess_Param.MotorSpeedSetBigin += StepData;
  383. }
  384. else if(MC_WalkProcess_Param.MotorSpeedSetBigin > (MC_MotorParam.Rate_Speed << 5) * MC_WalkMode_Persent / 100 + 10)
  385. {
  386. if(MC_WalkProcess_Param.MotorSpeedSetBigin > StepData)
  387. {
  388. MC_WalkProcess_Param.MotorSpeedSetBigin -= StepData;
  389. }
  390. else
  391. {
  392. MC_WalkProcess_Param.MotorSpeedSetBigin = 0;
  393. }
  394. }
  395. else
  396. {
  397. MC_WalkProcess_Param.MotorSpeedSetBigin = (MC_MotorParam.Rate_Speed << 5 ) * MC_WalkMode_Persent / 100;
  398. }
  399. SpdMotorByIdc = PID_Regulator((MC_ConfigParam1.CurrentLimit * 1000) >> 7, MC_RunInfo.BusCurrent >> 7, &PID_ConstantPower); // 母线电流闭环
  400. }
  401. //运行模式,设定转速 = 135rpm
  402. else
  403. {
  404. if(MC_WalkProcess_Param.MotorSpeedSetBigin < (135 << 5) - 10)
  405. {
  406. MC_WalkProcess_Param.MotorSpeedSetBigin += 1;
  407. }
  408. else if(MC_WalkProcess_Param.MotorSpeedSetBigin > (135 << 5) + 10)
  409. {
  410. MC_WalkProcess_Param.MotorSpeedSetBigin -= 1;
  411. }
  412. else
  413. {
  414. MC_WalkProcess_Param.MotorSpeedSetBigin = 135 << 5;
  415. }
  416. SpdMotorByIdc = PID_Regulator((MC_ConfigParam1.CurrentLimit * 500) >> 7, MC_RunInfo.BusCurrent >> 7, &PID_ConstantPower); // 母线电流闭环
  417. }
  418. //速度环
  419. TorQueBySpd = PID_Regulator((MC_WalkProcess_Param.MotorSpeedSetBigin >> 5), MC_RunInfo.MotorSpeed, &PID_MotorSpd); // 电机速度闭环输出
  420. TorQueBySpd += SpdMotorByIdc;
  421. //限制车速低于6km/h
  422. if(p_MC_WorkMode != MC_WorkMode_Config) //运行模式,推行限速6km/h
  423. {
  424. TorQueBySpd = (uint16_t)((uint32_t)(TorQueBySpd * Function_Linear_3Stage(55, 0, 55, 128, MC_RunInfo.BikeSpeed)) >> 10);
  425. if(MC_RunInfo.BikeSpeed > 60)
  426. {
  427. MC_WalkProcess_Param.MotorSpeedSetBigin = 0;
  428. MC_MotorStop(&MC_StarFlag);
  429. }
  430. else
  431. {
  432. //电机启动
  433. MC_MotorStar(&MC_StarFlag);
  434. }
  435. }
  436. else //配置模式不限速
  437. {
  438. //电机启动
  439. MC_MotorStar(&MC_StarFlag);
  440. }
  441. #if 1
  442. static uint16_t K_ByVoltage_Set_Old = 1024;
  443. uint16_t K_ByVoltage_Set;
  444. static uint16_t K_ByVoltage_Result;
  445. uint16_t K_ByTemperature_Set;
  446. static uint16_t K_ByTemperature_Result;
  447. //根据电压调节输出
  448. K_ByVoltage_Set = MC_Cal_K_ByVoltage(MC_RunInfo.BusVoltage, MC_MotorParam.Rate_Voltage, K_ByVoltage_Set_Old);//根据母线电压计算衰减比例,递减
  449. K_ByVoltage_Set_Old = K_ByVoltage_Set;
  450. K_ByVoltage_Result = MC_DataSet_Linear_Process(K_ByVoltage_Set, K_ByVoltage_Result, 1, 1); //设定值与给定值线性处理
  451. //根据温度调节输出
  452. K_ByTemperature_Set = MC_Cal_K_ByTemperature(MC_RunInfo.T_Coil, MC_ConfigParam1.TempTH_Alarm); //根据温度计算衰减比例
  453. K_ByTemperature_Result = MC_DataSet_Linear_Process(K_ByTemperature_Set, K_ByTemperature_Result, 1, 1); //设定值与给定值线性处理
  454. #else
  455. uint16_t K_ByVoltage_Result = 1024;
  456. uint16_t K_ByTemperature_Result = 1024;
  457. #endif
  458. #if 0
  459. //限制最大输出功率为250W
  460. static uint16_t IqsMax;
  461. if(MC_RunInfo.MotorSpeed < 10)
  462. {
  463. IqsMax = 1050;
  464. }
  465. else
  466. {
  467. IqsMax = 235000 / MC_RunInfo.MotorSpeed;
  468. }
  469. IqsMax = (IqsMax > 1050) ? 1050 : IqsMax;
  470. if(TorQueBySpd > IqsMax)
  471. {
  472. TorQueBySpd = IqsMax;
  473. }
  474. #elif 0
  475. if(TorQueBySpd > 450)
  476. {
  477. TorQueBySpd = 450;
  478. }
  479. #endif
  480. Ref_Speed_Temp = ((int32_t)TorQueBySpd * K_ByVoltage_Result) >> 10;
  481. Ref_Speed_Temp = ((int32_t)Ref_Speed_Temp * K_ByTemperature_Result) >> 10;
  482. p_MC_CalParam.Ref_Speed = (int16_t)(Ref_Speed_Temp);
  483. p_MC_CalParam.Foc_Flag = SET;
  484. p_MC_CalParam.AssistRunMode = MC_AssistRunMode_WALK;
  485. return (p_MC_CalParam);
  486. }
  487. //踏频模式处理
  488. MC_CalParam_Struct_t MC_AssistRunMode_Cadence_Process(MC_GearSt_Struct_t GearSt)
  489. {
  490. MC_CalParam_Struct_t p_MC_CalParam = {MC_AssistRunMode_INVALID, 0, 0, RESET};
  491. //...插入踏频处理
  492. //电机启动
  493. MC_MotorStar(&MC_StarFlag);
  494. p_MC_CalParam.Foc_Flag = SET;
  495. p_MC_CalParam.AssistRunMode = MC_AssistRunMode_CADENCE;
  496. return (p_MC_CalParam);
  497. }
  498. //力矩模式处理 运动版
  499. MC_CalParam_Struct_t MC_AssistRunMode_Torque_Process(uint16_t SenorData, MC_GearSt_Struct_t GearSt)
  500. {
  501. MC_CalParam_Struct_t p_MC_CalParam = {MC_AssistRunMode_INVALID, 0, 0, RESET};
  502. uint8_t TorqueAccStep = 0;//力矩上升斜率
  503. uint8_t TorqueDecStep = 0;//力矩下降斜率
  504. uint16_t TorqueStartData, TorqueStopData;//力矩启动值,力矩停机值
  505. int16_t Torque_Temp;
  506. int32_t Torque_Ref_Temp;
  507. static uint32_t TorqueStopDelayTimeCnt = 0; //低力矩停机计时
  508. uint16_t TorqueStopDelayTime;
  509. static int16_t IqRefByInPower; //限流计算结果
  510. static uint16_t CurrentLimitPresent; //限流实际值,做升降速处理
  511. uint16_t CurrentLimitSet; //限流设置值,不同助力档位更新
  512. static uint8_t TorqueRefEndUpdateCount = 0;
  513. static FlagStatus SoftStartFlag = SET;
  514. static uint16_t SoftStartDelayTimeCount = 0;
  515. uint16_t SoftStartDelayTime = 0;
  516. uint16_t SoftStartAcc = 0;
  517. #if 1
  518. //踩踏力矩输入
  519. MC_TorqueProcess_Param.TorqueApp = SenorData;
  520. #elif 1
  521. //输入阶跃
  522. MC_TorqueProcess_Param.TorqueApp = 1000;
  523. //踏频设为启动
  524. MC_CadenceResult.Cadence_Dir = MC_Cadence_Forward;
  525. MC_CadenceResult.IsStopFlag = FALSE;
  526. #elif 1
  527. //输入斜坡
  528. static uint32_t WaveTime_Zero = 0;
  529. static uint32_t Time_Enter = 0;
  530. if((HAL_GetTick() - Time_Enter) > 10) // 超时10ms未进入,波形发生初始时刻清零
  531. {
  532. WaveTime_Zero = HAL_GetTick();
  533. }
  534. Time_Enter = HAL_GetTick();
  535. MC_TorqueProcess_Param.TorqueApp = RampWaveGenerate(WaveTime_Zero, 6000, 2100);
  536. //踏频设为启动
  537. MC_CadenceResult.Cadence_Dir = MC_Cadence_Forward;
  538. MC_CadenceResult.IsStopFlag = FALSE;
  539. #elif 1
  540. //输入三角波,测试输出响应
  541. static uint32_t WaveTime_Zero = 0;
  542. static uint32_t Time_Enter = 0;
  543. if((HAL_GetTick() - Time_Enter) > 10) // 超时10ms未进入,波形发生初始时刻清零
  544. {
  545. WaveTime_Zero = HAL_GetTick();
  546. }
  547. Time_Enter = HAL_GetTick();
  548. MC_TorqueProcess_Param.TorqueApp = TriangleWaveGenerate(WaveTime_Zero, 500, 1000 ,1500);
  549. //踏频设为启动
  550. MC_CadenceResult.Cadence_Dir = MC_Cadence_Forward;
  551. MC_CadenceResult.IsStopFlag = FALSE;
  552. #elif 1
  553. //输入方波,测试输出响应
  554. static uint32_t WaveTime_Zero = 0;
  555. static uint32_t Time_Enter = 0;
  556. if((HAL_GetTick() - Time_Enter) > 10) // 超时10ms未进入,波形发生初始时刻清零
  557. {
  558. WaveTime_Zero = HAL_GetTick();
  559. }
  560. Time_Enter = HAL_GetTick();
  561. MC_TorqueProcess_Param.TorqueApp = SquareWaveGenerate(WaveTime_Zero, 5000, 8000, 1500);
  562. //踏频设为启动
  563. MC_CadenceResult.Cadence_Dir = MC_Cadence_Forward;
  564. MC_CadenceResult.IsStopFlag = FALSE;
  565. #endif
  566. //低力矩停机
  567. TorqueStopData = (MC_TorqueCorrectParam.StarData < 200) ? 100 : (MC_TorqueCorrectParam.StarData >> 1);
  568. if(MC_TorqueProcess_Param.TorqueApp >= (TorqueStopData))
  569. {
  570. TorqueStopDelayTimeCnt = HAL_GetTick();
  571. }
  572. else
  573. {
  574. if(MC_RunInfo.MotorSpeed > 200)
  575. {
  576. TorqueStopDelayTime = 218400 / MC_RunInfo.MotorSpeed; //60s / (电机转速 / 4.55 / 2.4) / 3,曲柄1/3圈
  577. }
  578. else
  579. {
  580. TorqueStopDelayTime = 1200;
  581. }
  582. TorqueStopDelayTime= (TorqueStopDelayTime < 500) ? 500 : TorqueStopDelayTime;
  583. if((HAL_GetTick() - TorqueStopDelayTimeCnt) > TorqueStopDelayTime)//超时1200ms
  584. {
  585. MC_TorqueProcess_Param.MotorStopLock_Flag = SET;
  586. }
  587. }
  588. //启动值判断
  589. if(MC_RunInfo.BikeSpeed > 60)
  590. {
  591. TorqueStartData = (MC_TorqueCorrectParam.StarData < 200 ? 150 \
  592. : (MC_TorqueCorrectParam.StarData > 700 ? 525 \
  593. : (MC_TorqueCorrectParam.StarData * 3 >> 2)));
  594. }
  595. else
  596. {
  597. TorqueStartData = (MC_TorqueCorrectParam.StarData < 200 ? 150 \
  598. : (MC_TorqueCorrectParam.StarData > 700 ? 525 \
  599. : MC_TorqueCorrectParam.StarData));
  600. }
  601. if(MC_TorqueProcess_Param.TorqueApp >= TorqueStartData)
  602. {
  603. MC_TorqueProcess_Param.MotorStopLock_Flag = RESET;
  604. }
  605. //踏频反向或踏频停止停机
  606. if((MC_CadenceResult.Cadence_Dir == MC_Cadence_Backward) ||
  607. (MC_CadenceResult.IsStopFlag == TRUE)
  608. )
  609. {
  610. MC_TorqueProcess_Param.MotorStopLock_Flag = SET;
  611. }
  612. //停机状态,延时处理
  613. if(MC_TorqueProcess_Param.MotorStopLock_Flag == SET)
  614. {
  615. if(MC_TorqueProcess_Param.TorqueRefEnd <= 7)
  616. {
  617. MC_TorqueProcess_Param.TorqueRefEnd = 0;
  618. //停机处理
  619. MC_MotorStop(&MC_StarFlag);
  620. //缓启动标志置位
  621. SoftStartFlag = SET;
  622. SoftStartDelayTimeCount = 0;
  623. }
  624. else
  625. {
  626. MC_TorqueProcess_Param.TorqueRefEnd -= 7; //这里影响到停止踩踏后的断电时间
  627. MC_MotorStar(&MC_StarFlag);
  628. }
  629. }
  630. //力矩给定升降速处理
  631. else
  632. {
  633. //按照助力档位调节力矩输入值
  634. switch(GearSt)
  635. {
  636. case MC_GearSt_Torque_ECO:
  637. {
  638. //控制输入给定加速斜率
  639. if(MC_ConfigParam1.UserAdjParam_ECO.StarModel_GAIN <= 80)
  640. {
  641. TorqueAccStep = MC_AssisParam.Gear_ECO.AccCnt - 1;
  642. }
  643. else if(MC_ConfigParam1.UserAdjParam_ECO.StarModel_GAIN >= 120)
  644. {
  645. TorqueAccStep = MC_AssisParam.Gear_ECO.AccCnt + 1;
  646. }
  647. else
  648. {
  649. TorqueAccStep = MC_AssisParam.Gear_ECO.AccCnt;
  650. }
  651. TorqueAccStep = (TorqueAccStep <= 0) ? 1 : TorqueAccStep;
  652. //控制输入给定减速斜率
  653. TorqueDecStep = MC_AssisParam.Gear_ECO.DecCnt;
  654. //随力矩输入调节助力比
  655. Torque_Temp = (uint16_t)((uint32_t)(MC_TorqueProcess_Param.TorqueApp * Coefficient_GainCal(100, MC_AssisParam.Gear_ECO.Gain_K, MC_AssisParam.Gear_ECO.Upper_Iq, MC_TorqueProcess_Param.TorqueApp)) >> 10);
  656. //根据助力增益调节助力比
  657. Torque_Temp = Torque_Temp * MC_ConfigParam1.UserAdjParam_ECO.Assist_K_GAIN / 100;
  658. //给定下限
  659. Torque_Temp = (Torque_Temp < MC_AssisParam.Gear_ECO.Lower_Iq) ? MC_AssisParam.Gear_ECO.Lower_Iq : Torque_Temp;
  660. //给定上限
  661. Torque_Temp = (Torque_Temp > MC_AssisParam.Gear_ECO.Upper_Iq) ? MC_AssisParam.Gear_ECO.Upper_Iq : Torque_Temp;
  662. //限流参数设置
  663. CurrentLimitSet = (uint32_t)(MC_AssisParam.Gear_ECO.CurrentMax_K * MC_ConfigParam1.CurrentLimit * 1000 >> 17) * 100;
  664. CurrentLimitPresent = MC_DataSet_Linear_Process(CurrentLimitSet, CurrentLimitPresent, 5 ,1);
  665. PID_IMax.hLower_Limit_Output = -(MC_AssisParam.Gear_ECO.Upper_Iq); //Lower Limit for Output limitation
  666. PID_IMax.hUpper_Limit_Output = 0; //Upper Limit for Output limitation
  667. PID_IMax.wLower_Limit_Integral = -(MC_AssisParam.Gear_ECO.Upper_Iq << 10); // 放大1024
  668. PID_IMax.wUpper_Limit_Integral = 0; // 放大1024
  669. break;
  670. }
  671. case MC_GearSt_Torque_NORM:
  672. {
  673. //控制输入给定加速斜率
  674. if(MC_ConfigParam1.UserAdjParam_NORM.StarModel_GAIN <= 80)
  675. {
  676. TorqueAccStep = MC_AssisParam.Gear_NORM.AccCnt - 1;
  677. }
  678. else if(MC_ConfigParam1.UserAdjParam_NORM.StarModel_GAIN >= 120)
  679. {
  680. TorqueAccStep = MC_AssisParam.Gear_NORM.AccCnt + 1;
  681. }
  682. else
  683. {
  684. TorqueAccStep = MC_AssisParam.Gear_NORM.AccCnt;
  685. }
  686. TorqueAccStep = (TorqueAccStep <= 0) ? 1 : TorqueAccStep;
  687. //控制输入给定减速斜率
  688. TorqueDecStep = MC_AssisParam.Gear_NORM.DecCnt;
  689. //随力矩输入调节助力比
  690. Torque_Temp = (uint16_t)((uint32_t)(MC_TorqueProcess_Param.TorqueApp * Coefficient_GainCal(100, MC_AssisParam.Gear_NORM.Gain_K, MC_AssisParam.Gear_NORM.Upper_Iq, MC_TorqueProcess_Param.TorqueApp)) >> 10);
  691. //根据助力增益调节助力比
  692. Torque_Temp = Torque_Temp * MC_ConfigParam1.UserAdjParam_NORM.Assist_K_GAIN / 100;
  693. //给定下限
  694. Torque_Temp = (Torque_Temp < MC_AssisParam.Gear_NORM.Lower_Iq) ? MC_AssisParam.Gear_NORM.Lower_Iq : Torque_Temp;
  695. //给定上限
  696. Torque_Temp = (Torque_Temp > MC_AssisParam.Gear_NORM.Upper_Iq) ? MC_AssisParam.Gear_NORM.Upper_Iq : Torque_Temp;
  697. //限流参数设置
  698. CurrentLimitSet = (uint32_t)(MC_AssisParam.Gear_NORM.CurrentMax_K * MC_ConfigParam1.CurrentLimit * 1000 >> 17) * 100;
  699. CurrentLimitPresent = MC_DataSet_Linear_Process(CurrentLimitSet, CurrentLimitPresent, 5 ,1);
  700. PID_IMax.hLower_Limit_Output = -(MC_AssisParam.Gear_NORM.Upper_Iq); //Lower Limit for Output limitation
  701. PID_IMax.hUpper_Limit_Output = 0; //Upper Limit for Output limitation
  702. PID_IMax.wLower_Limit_Integral = -(MC_AssisParam.Gear_NORM.Upper_Iq << 10); // 放大1024
  703. PID_IMax.wUpper_Limit_Integral = 0; // 放大1024
  704. break;
  705. }
  706. case MC_GearSt_Torque_SPORT:
  707. {
  708. //控制输入给定加速斜率
  709. if(MC_ConfigParam1.UserAdjParam_SPORT.StarModel_GAIN <= 80)
  710. {
  711. TorqueAccStep = MC_AssisParam.Gear_SPORT.AccCnt - 1;
  712. }
  713. else if(MC_ConfigParam1.UserAdjParam_SPORT.StarModel_GAIN >= 120)
  714. {
  715. TorqueAccStep = MC_AssisParam.Gear_SPORT.AccCnt + 1;
  716. }
  717. else
  718. {
  719. TorqueAccStep = MC_AssisParam.Gear_SPORT.AccCnt;
  720. }
  721. TorqueAccStep = (TorqueAccStep <= 0) ? 1 : TorqueAccStep;
  722. //控制输入给定减速斜率
  723. TorqueDecStep = MC_AssisParam.Gear_SPORT.DecCnt;
  724. //随力矩输入调节助力比
  725. Torque_Temp = (uint16_t)((uint32_t)(MC_TorqueProcess_Param.TorqueApp * Coefficient_GainCal(100, MC_AssisParam.Gear_SPORT.Gain_K, MC_AssisParam.Gear_SPORT.Upper_Iq, MC_TorqueProcess_Param.TorqueApp)) >> 10);
  726. //根据助力增益调节助力比
  727. Torque_Temp = Torque_Temp * MC_ConfigParam1.UserAdjParam_SPORT.Assist_K_GAIN / 100;
  728. //给定下限
  729. Torque_Temp = (Torque_Temp < MC_AssisParam.Gear_SPORT.Lower_Iq) ? MC_AssisParam.Gear_SPORT.Lower_Iq : Torque_Temp;
  730. //给定上限
  731. Torque_Temp = (Torque_Temp > MC_AssisParam.Gear_SPORT.Upper_Iq) ? MC_AssisParam.Gear_SPORT.Upper_Iq : Torque_Temp;
  732. //限流参数设置
  733. CurrentLimitSet = (uint32_t)(MC_AssisParam.Gear_SPORT.CurrentMax_K * MC_ConfigParam1.CurrentLimit * 1000 >> 17) * MC_CadenceLimit_K;
  734. CurrentLimitPresent = MC_DataSet_Linear_Process(CurrentLimitSet, CurrentLimitPresent, 5 ,1);
  735. PID_IMax.hLower_Limit_Output = -(MC_AssisParam.Gear_SPORT.Upper_Iq); //Lower Limit for Output limitation
  736. PID_IMax.hUpper_Limit_Output = 0; //Upper Limit for Output limitation
  737. PID_IMax.wLower_Limit_Integral = -(MC_AssisParam.Gear_SPORT.Upper_Iq << 10); // 放大1024
  738. PID_IMax.wUpper_Limit_Integral = 0; // 放大1024
  739. break;
  740. }
  741. case MC_GearSt_Torque_TURBO:
  742. {
  743. //控制输入给定加速斜率
  744. if(MC_ConfigParam1.UserAdjParam_TURBO.StarModel_GAIN <= 80)
  745. {
  746. TorqueAccStep = MC_AssisParam.Gear_TURBO.AccCnt - 1;
  747. }
  748. else if(MC_ConfigParam1.UserAdjParam_TURBO.StarModel_GAIN >= 120)
  749. {
  750. TorqueAccStep = MC_AssisParam.Gear_TURBO.AccCnt + 1;
  751. }
  752. else
  753. {
  754. TorqueAccStep = MC_AssisParam.Gear_TURBO.AccCnt;
  755. }
  756. TorqueAccStep = (TorqueAccStep <= 0) ? 1 : TorqueAccStep;
  757. //控制输入给定减速斜率
  758. TorqueDecStep = MC_AssisParam.Gear_TURBO.DecCnt;
  759. //随力矩输入调节助力比
  760. Torque_Temp = (uint16_t)((uint32_t)(MC_TorqueProcess_Param.TorqueApp * Coefficient_GainCal(100, MC_AssisParam.Gear_TURBO.Gain_K, MC_AssisParam.Gear_TURBO.Upper_Iq, MC_TorqueProcess_Param.TorqueApp)) >> 10);
  761. //根据助力增益调节助力比
  762. Torque_Temp = Torque_Temp * MC_ConfigParam1.UserAdjParam_TURBO.Assist_K_GAIN / 100;
  763. //给定下限
  764. Torque_Temp = (Torque_Temp < MC_AssisParam.Gear_TURBO.Lower_Iq) ? MC_AssisParam.Gear_TURBO.Lower_Iq : Torque_Temp;
  765. //给定上限
  766. Torque_Temp = (Torque_Temp > MC_AssisParam.Gear_TURBO.Upper_Iq) ? MC_AssisParam.Gear_TURBO.Upper_Iq : Torque_Temp;
  767. //限流参数设置
  768. CurrentLimitSet = (uint32_t)(MC_AssisParam.Gear_TURBO.CurrentMax_K * MC_ConfigParam1.CurrentLimit * 1000 >> 17) * MC_CadenceLimit_K;
  769. CurrentLimitPresent = MC_DataSet_Linear_Process(CurrentLimitSet, CurrentLimitPresent, 5 ,1);
  770. PID_IMax.hLower_Limit_Output = -(MC_AssisParam.Gear_TURBO.Upper_Iq); //Lower Limit for Output limitation
  771. PID_IMax.hUpper_Limit_Output = 0; //Upper Limit for Output limitation
  772. PID_IMax.wLower_Limit_Integral = -(MC_AssisParam.Gear_TURBO.Upper_Iq << 10); // 放大1024
  773. PID_IMax.wUpper_Limit_Integral = 0; // 放大1024
  774. break;
  775. }
  776. case MC_GearSt_SMART:
  777. {
  778. //控制输入给定加速斜率
  779. if(MC_ConfigParam1.UserAdjParam_SMART.StarModel_GAIN <= 80)
  780. {
  781. TorqueAccStep = MC_AssisParam.Gear_SMART.AccCnt - 1;
  782. }
  783. else if(MC_ConfigParam1.UserAdjParam_SMART.StarModel_GAIN >= 120)
  784. {
  785. TorqueAccStep = MC_AssisParam.Gear_SMART.AccCnt + 1;
  786. }
  787. else
  788. {
  789. TorqueAccStep = MC_AssisParam.Gear_SMART.AccCnt;
  790. }
  791. TorqueAccStep = (TorqueAccStep <= 0) ? 1 : TorqueAccStep;
  792. //控制输入给定减速斜率
  793. TorqueDecStep = MC_AssisParam.Gear_SMART.DecCnt;
  794. //助力比控制系数
  795. Torque_Temp = (uint32_t)(MC_TorqueProcess_Param.TorqueApp * MC_TorqueProcess_Param.TorqueApp) / (MC_AssisParam.Gear_SMART.i_Sport_TH);
  796. //根据助力增益调节助力比
  797. Torque_Temp = Torque_Temp * MC_ConfigParam1.UserAdjParam_SMART.Assist_K_GAIN / 100;
  798. //给定下限
  799. Torque_Temp = (Torque_Temp < MC_AssisParam.Gear_SMART.Lower_Iq) ? MC_AssisParam.Gear_SMART.Lower_Iq : Torque_Temp;
  800. //给定上限
  801. Torque_Temp = (Torque_Temp > MC_AssisParam.Gear_SMART.Upper_Iq) ? MC_AssisParam.Gear_SMART.Upper_Iq : Torque_Temp;
  802. //限流参数设置
  803. CurrentLimitSet = (uint32_t)(MC_AssisParam.Gear_SMART.CurrentMax_K * MC_ConfigParam1.CurrentLimit * 1000 >> 17) * MC_CadenceLimit_K;
  804. CurrentLimitPresent = MC_DataSet_Linear_Process(CurrentLimitSet, CurrentLimitPresent, 5 ,1);
  805. PID_IMax.hLower_Limit_Output = -(MC_AssisParam.Gear_SMART.Upper_Iq); //Lower Limit for Output limitation
  806. PID_IMax.hUpper_Limit_Output = 0; //Upper Limit for Output limitation
  807. PID_IMax.wLower_Limit_Integral = -(MC_AssisParam.Gear_SMART.Upper_Iq << 10); // 放大1024
  808. PID_IMax.wUpper_Limit_Integral = 0; // 放大1024
  809. break;
  810. }
  811. default:
  812. {
  813. TorqueAccStep = 0;
  814. TorqueDecStep = 0;
  815. Torque_Temp = 0;
  816. break;
  817. }
  818. }
  819. //随车速调节助力比
  820. Torque_Temp = (uint16_t)((uint32_t)(Torque_Temp * Function_Linear_3Stage(MC_ConfigParam1.SpeedLimit * 10, 0, MC_ConfigParam1.SpeedLimit * 10, 52, MC_SpeedSensorData.Speed_Data)) >> 10);
  821. //助力输出
  822. MC_TorqueProcess_Param.TorqueRef = Torque_Temp;
  823. if(MC_TorqueProcess_Param.TorqueRef <= 0)
  824. {
  825. MC_TorqueProcess_Param.TorqueRef = 0;
  826. }
  827. //升降速曲线计算
  828. if( MC_SpeedSensorData.Speed_Data > (MC_ConfigParam1.SpeedLimit * 10) ) //限速处理
  829. {
  830. if((MC_TorqueProcess_Param.TorqueRef - MC_TorqueProcess_Param.TorqueRefEnd) > 2)
  831. {
  832. MC_TorqueProcess_Param.TorqueRefEnd += 1;
  833. }
  834. else if((MC_TorqueProcess_Param.TorqueRef - MC_TorqueProcess_Param.TorqueRefEnd) < (- 1))
  835. {
  836. MC_TorqueProcess_Param.TorqueRefEnd -= 1;
  837. }
  838. }
  839. else if( (Bike_Attitude.UpWardSlope_flag == TRUE)&&(MC_SpeedSensorData.Speed_Data < 100)) //上坡处理
  840. {
  841. if((MC_TorqueProcess_Param.TorqueRef - MC_TorqueProcess_Param.TorqueRefEnd) > 2)
  842. {
  843. MC_TorqueProcess_Param.TorqueRefEnd += TorqueAccStep;
  844. }
  845. else if((MC_TorqueProcess_Param.TorqueRef - MC_TorqueProcess_Param.TorqueRefEnd) < (- 1))
  846. {
  847. TorqueRefEndUpdateCount++;
  848. if(TorqueRefEndUpdateCount >=3)
  849. {
  850. TorqueRefEndUpdateCount = 0;
  851. MC_TorqueProcess_Param.TorqueRefEnd -= TorqueDecStep;
  852. }
  853. }
  854. }
  855. else if(SoftStartFlag == SET) //启动处理
  856. {
  857. if(MC_ConfigParam1.StarModel == MC_StarMode_DYNAMIC)
  858. {
  859. SoftStartDelayTime = 100; //启动处理延时100ms
  860. SoftStartAcc = 10; //10ms递增0.1倍
  861. }
  862. else if(MC_ConfigParam1.StarModel == MC_StarMode_SOFT)
  863. {
  864. SoftStartDelayTime = 900; //启动处理延时900ms
  865. SoftStartAcc = 90; //90ms递增0.1倍
  866. }
  867. else
  868. {
  869. SoftStartDelayTime = 300; //启动处理延时300ms
  870. SoftStartAcc = 30; //30ms递增0.1倍
  871. }
  872. SoftStartDelayTimeCount++;
  873. if(SoftStartDelayTimeCount <= SoftStartDelayTime) // 缓启动过程,按照0.1倍率逐步增加加减速斜率
  874. {
  875. if((MC_TorqueProcess_Param.TorqueRef - MC_TorqueProcess_Param.TorqueRefEnd) > 2)
  876. {
  877. if((SoftStartDelayTimeCount % (10 - SoftStartDelayTimeCount / SoftStartAcc)) == 0)
  878. {
  879. MC_TorqueProcess_Param.TorqueRefEnd += TorqueAccStep;
  880. }
  881. }
  882. else if((MC_TorqueProcess_Param.TorqueRef - MC_TorqueProcess_Param.TorqueRefEnd) < (- 1))
  883. {
  884. MC_TorqueProcess_Param.TorqueRefEnd -= TorqueDecStep;
  885. }
  886. }
  887. else
  888. {
  889. SoftStartDelayTimeCount = 0;
  890. SoftStartFlag = RESET;
  891. }
  892. }
  893. else //正常骑行
  894. {
  895. if((MC_TorqueProcess_Param.TorqueRef - MC_TorqueProcess_Param.TorqueRefEnd) > 2)
  896. {
  897. MC_TorqueProcess_Param.TorqueRefEnd += TorqueAccStep;
  898. }
  899. else if((MC_TorqueProcess_Param.TorqueRef - MC_TorqueProcess_Param.TorqueRefEnd) < (- 1))
  900. {
  901. MC_TorqueProcess_Param.TorqueRefEnd -= TorqueDecStep;
  902. }
  903. }
  904. MC_TorqueProcess_Param.TorqueRefEnd = (MC_TorqueProcess_Param.TorqueRefEnd < 6) ? 6 : MC_TorqueProcess_Param.TorqueRefEnd;
  905. //限速点处理
  906. if( MC_SpeedSensorData.Speed_Data > (MC_ConfigParam1.SpeedLimit * 10 + 20) ) //限速值+2
  907. {
  908. MC_TorqueProcess_Param.MotorStopLock_Flag = SET;
  909. MC_TorqueProcess_Param.TorqueRefEnd = 0;
  910. //停机处理
  911. MC_MotorStop(&MC_StarFlag);
  912. }
  913. #if 0 //低于限速点启动电机
  914. else if(MC_RunInfo.BikeSpeed < ((MC_ConfigParam1.SpeedLimit) * 10))
  915. {
  916. MC_MotorStar(&MC_StarFlag);
  917. }
  918. #elif 1 //低于断电点即启动电机
  919. else
  920. {
  921. MC_MotorStar(&MC_StarFlag);
  922. }
  923. #endif
  924. }
  925. #if 1
  926. static uint16_t K_ByVoltage_Set_Old = 1024;
  927. uint16_t K_ByVoltage_Set;
  928. static uint16_t K_ByVoltage_Result;
  929. uint16_t K_ByTemperature_Set;
  930. static uint16_t K_ByTemperature_Result;
  931. //根据电压调节输出
  932. K_ByVoltage_Set = MC_Cal_K_ByVoltage(MC_RunInfo.BusVoltage, MC_MotorParam.Rate_Voltage, K_ByVoltage_Set_Old);//根据母线电压计算衰减比例,递减
  933. K_ByVoltage_Set_Old = K_ByVoltage_Set;
  934. K_ByVoltage_Result = MC_DataSet_Linear_Process(K_ByVoltage_Set, K_ByVoltage_Result, 1, 1); //设定值与给定值线性处理
  935. //根据温度调节输出
  936. K_ByTemperature_Set = MC_Cal_K_ByTemperature(MC_RunInfo.T_Coil, MC_ConfigParam1.TempTH_Alarm); //根据温度计算衰减比例
  937. K_ByTemperature_Result = MC_DataSet_Linear_Process(K_ByTemperature_Set, K_ByTemperature_Result, 1, 1); //设定值与给定值线性处理
  938. #else
  939. uint16_t K_ByVoltage_Result = 1024;
  940. uint16_t K_ByTemperature_Result = 1024;
  941. #endif
  942. //限流计算
  943. IqRefByInPower = PID_Regulator(CurrentLimitPresent / 100, (MC_RunInfo.BusCurrent >> 7), &PID_IMax);
  944. Torque_Ref_Temp = ((int32_t)MC_TorqueProcess_Param.TorqueRefEnd * K_ByVoltage_Result) >> 10;
  945. Torque_Ref_Temp = (Torque_Ref_Temp * K_ByTemperature_Result) >> 10;
  946. Torque_Ref_Temp = (Torque_Ref_Temp + IqRefByInPower) >> 1;
  947. p_MC_CalParam.Ref_Torque = (int16_t)Torque_Ref_Temp;
  948. p_MC_CalParam.Foc_Flag = SET;
  949. p_MC_CalParam.AssistRunMode = MC_AssistRunMode_TORQUE;
  950. return (p_MC_CalParam);
  951. }
  952. //力矩模式处理 城市版
  953. uint16_t candenceCount; //踏频磁环信号计数变量,全局变量
  954. uint16_t torqueApp;
  955. uint16_t torque_NM; //力矩值,单位NM
  956. uint16_t torqueRank=0; //由踏频采样后的力矩值,得出的力矩给定
  957. static uint16_t torqueRankLast=0; //力矩给定历史值
  958. static uint16_t torqueRankBak=0; //力矩给定历史值备份
  959. static uint8_t flagUpDown=0; //输入力矩上升、下降的标志位
  960. static int32_t tmpFltSum=0; //输入力矩滤波的静态变量
  961. int16_t tmpFlted=0; //输入力矩滤波的临时变量
  962. int16_t torqueApp2; //力矩计算的临时变量
  963. uint8_t flagCandence=0; //踏频信号更新标志
  964. int16_t torqueAppLag=0; //力局地经过加减速处理后的临时变量
  965. int16_t torqueAppRatio=0; //力矩值计算助力比后的临时变量
  966. int16_t torqueAppAccStep = 4; //力矩给定升速斜率
  967. int16_t torqueAppDecStep = 1; //力矩给定减速斜率
  968. uint16_t candenceBakAtTop=0; //踏频最大值备份
  969. uint16_t torqueByCandenceDec=0; //由踏频变化计算的力矩减弱量
  970. int16_t torqueDataFromCadence=0; //经过踏频信号采样的力矩值
  971. int32_t Taping_activeFlt=0; //踏频实时值滤波中间量
  972. int32_t Taping_activeFlted = 0; //踏频实时值滤波值
  973. #define TORQUE_TMP 28 //力矩AD值与1NM的换算系数
  974. MC_CalParam_Struct_t MC_AssistRunMode_Torque_compensation(uint16_t SenorData, MC_GearSt_Struct_t GearSt)
  975. {
  976. MC_CalParam_Struct_t p_MC_CalParam = {MC_AssistRunMode_INVALID, 0, 0, RESET};
  977. uint16_t TorqueStartData, TorqueStopData;//力矩启动值,力矩停机值
  978. int32_t Torque_Ref_Temp;
  979. static uint32_t TorqueStopDelayTimeCnt = 0;//低力矩停机计时
  980. uint16_t TorqueStopDelayTime;
  981. static int16_t IqRefByInPower;//限流计算结果
  982. static uint16_t CurrentLimitPresent; //限流实际值,做升降速处理
  983. uint16_t CurrentLimitSet;
  984. static FlagStatus SoftStartFlag = SET;
  985. static uint16_t SoftStartDelayTimeCount = 0;
  986. uint16_t SoftStartDelayTime = 0;
  987. uint16_t SoftStartAcc = 0;
  988. #if 0
  989. //输入三角波,用于测试
  990. static uint32_t WaveTime_Zero = 0;
  991. static uint32_t Time_Enter = 0;
  992. static uint16_t SensorDataDebug = 0;
  993. if((HAL_GetTick() - Time_Enter) > 10) // 超时10ms未进入,波形发生初始时刻清零
  994. {
  995. WaveTime_Zero = HAL_GetTick();
  996. }
  997. Time_Enter = HAL_GetTick();
  998. SensorDataDebug = TriangleWaveGenerate(WaveTime_Zero, 125, 250 ,1500);
  999. SenorData = SensorDataDebug;
  1000. #endif
  1001. MC_TorqueProcess_Param.TorqueApp = SenorData;
  1002. /*为下一步抓取最大值,力矩需要滤波*/
  1003. tmpFltSum += (((int32_t )SenorData << 10) - tmpFltSum) >> 7;
  1004. tmpFlted = (int16_t)(tmpFltSum >> 10);
  1005. torque_NM = tmpFlted / TORQUE_TMP ; //转换为单位NM 的变量
  1006. //低力矩停机
  1007. TorqueStopData = (MC_TorqueCorrectParam.StarData < 200) ? 100 : (MC_TorqueCorrectParam.StarData >> 1);
  1008. if(MC_TorqueProcess_Param.TorqueApp >= (TorqueStopData))
  1009. {
  1010. TorqueStopDelayTimeCnt = HAL_GetTick();
  1011. }
  1012. else
  1013. {
  1014. if(MC_RunInfo.MotorSpeed > 200)
  1015. {
  1016. TorqueStopDelayTime = 163800 / MC_RunInfo.MotorSpeed;
  1017. }
  1018. else
  1019. {
  1020. TorqueStopDelayTime = 1200;
  1021. }
  1022. if((HAL_GetTick() - TorqueStopDelayTimeCnt) > TorqueStopDelayTime)//超时1200ms
  1023. {
  1024. MC_TorqueProcess_Param.MotorStopLock_Flag = SET;
  1025. }
  1026. }
  1027. //启动值判断
  1028. if(MC_RunInfo.BikeSpeed > 60)
  1029. {
  1030. TorqueStartData = (MC_TorqueCorrectParam.StarData < 200 ? 150 \
  1031. : (MC_TorqueCorrectParam.StarData > 700 ? 525 \
  1032. : (MC_TorqueCorrectParam.StarData * 3 / 4)));
  1033. }
  1034. else
  1035. {
  1036. TorqueStartData = (MC_TorqueCorrectParam.StarData < 200 ? 150 \
  1037. : (MC_TorqueCorrectParam.StarData > 700 ? 525 \
  1038. : MC_TorqueCorrectParam.StarData));
  1039. }
  1040. if(MC_TorqueProcess_Param.TorqueApp >= TorqueStartData)
  1041. {
  1042. MC_TorqueProcess_Param.MotorStopLock_Flag = RESET;
  1043. }
  1044. //踏频反向或踏频停止停机
  1045. if((MC_CadenceResult.Cadence_Dir == MC_Cadence_Backward) ||
  1046. (MC_CadenceResult.IsStopFlag == TRUE)
  1047. )
  1048. {
  1049. MC_TorqueProcess_Param.MotorStopLock_Flag = SET;
  1050. }
  1051. //停机状态,延时处理
  1052. if(MC_TorqueProcess_Param.MotorStopLock_Flag == SET)
  1053. {
  1054. torqueRankBak = 0;
  1055. torqueAppDecStep = 6;//84;
  1056. if(MC_TorqueProcess_Param.TorqueRefEnd < 10)
  1057. {
  1058. MC_TorqueProcess_Param.TorqueRefEnd = 0;
  1059. //停机处理
  1060. MC_MotorStop(&MC_StarFlag);
  1061. p_MC_CalParam.Foc_Flag = RESET;
  1062. //缓启动标志置位
  1063. SoftStartFlag = SET;
  1064. SoftStartDelayTimeCount = 0;
  1065. }
  1066. else
  1067. {
  1068. MC_TorqueProcess_Param.TorqueRefEnd -= 7;
  1069. MC_MotorStar(&MC_StarFlag);
  1070. }
  1071. torqueAppLag = MC_TorqueProcess_Param.TorqueRefEnd;
  1072. }
  1073. else
  1074. {
  1075. if(flagCandence==1)
  1076. {
  1077. flagCandence = 0;
  1078. torqueRank = torqueDataFromCadence; //根据踏频磁环信号采样力矩值
  1079. if(torqueRank > torqueRankLast) //比较本次与上次的值
  1080. {
  1081. if(flagUpDown==0)
  1082. {
  1083. //最低点
  1084. }
  1085. if(torqueRank > torqueRankBak) //与当前使用的值比较
  1086. {
  1087. torqueRankBak = torqueRank;
  1088. candenceCount = 0;
  1089. torqueAppDecStep = 1;
  1090. torqueByCandenceDec = 0;
  1091. }
  1092. flagUpDown=1;// 力矩上升标志
  1093. }
  1094. else if(torqueRank < torqueRankLast)
  1095. {
  1096. if(flagUpDown==1)
  1097. {
  1098. //最高点
  1099. torqueRankBak = torqueRankLast;
  1100. candenceCount = 0;
  1101. torqueAppDecStep = 1;//14;
  1102. /*备份力矩最高点的踏频*/
  1103. candenceBakAtTop = Taping_activeFlted;
  1104. torqueByCandenceDec = 0;
  1105. }
  1106. flagUpDown=0; //降
  1107. }
  1108. torqueRankLast = torqueRank; //记录上一次的值
  1109. if(candenceCount > 60) //没有力矩信号, 踏频信号超过360度,加大降速值,清零给定
  1110. {
  1111. torqueRankBak = 0;
  1112. torqueAppDecStep = 6;//84;
  1113. }
  1114. else if(candenceCount > 35) //超过210度,没有力矩信号
  1115. {
  1116. torqueRankBak = 0;
  1117. torqueAppDecStep = 2;
  1118. if((Taping_activeFlted < (candenceBakAtTop - 3)) && (tmpFlted<100))
  1119. {
  1120. torqueAppDecStep = 6;
  1121. }
  1122. }
  1123. else // (candenceCount < 35)
  1124. {
  1125. /*踏频降低,且力矩低*/
  1126. if((Taping_activeFlted < (candenceBakAtTop - 3)) && (tmpFlted<100))
  1127. {
  1128. torqueByCandenceDec +=3;
  1129. torqueAppDecStep = 6;
  1130. }
  1131. }
  1132. torqueApp2 = torqueRankBak - torqueByCandenceDec;
  1133. //torqueApp2 = torqueRankBak;
  1134. torqueApp2 = torqueApp2 > 0 ? torqueApp2 : 0;
  1135. //转换力矩值为力矩控制量
  1136. torqueApp = torqueApp2 * TORQUE_TMP ;
  1137. }/*if(flagCandence==1)*/
  1138. if(GearSt == 0x33)
  1139. {
  1140. torqueAppRatio = ((int32_t)torqueApp * torqueApp / (MC_AssisParam.Gear_SMART.i_Sport_TH));
  1141. torqueAppRatio = (int16_t)((int32_t)torqueAppRatio * MC_ConfigParam1.UserAdjParam_SMART.Assist_K_GAIN / 100); //上位机助力比增益调节
  1142. //加速增益调节
  1143. if(MC_ConfigParam1.UserAdjParam_SMART.StarModel_GAIN <= 80)
  1144. {
  1145. torqueAppAccStep = MC_AssisParam.Gear_SMART.AccCnt - 1;
  1146. }
  1147. else if(MC_ConfigParam1.UserAdjParam_SMART.StarModel_GAIN >= 120)
  1148. {
  1149. torqueAppAccStep = MC_AssisParam.Gear_SMART.AccCnt + 1;
  1150. }
  1151. else
  1152. {
  1153. torqueAppAccStep = MC_AssisParam.Gear_SMART.AccCnt;
  1154. }
  1155. //给定上限
  1156. torqueAppRatio = (torqueAppRatio > MC_AssisParam.Gear_SMART.Upper_Iq) ? MC_AssisParam.Gear_SMART.Upper_Iq : torqueAppRatio;
  1157. //限流参数设置
  1158. CurrentLimitSet = (uint32_t)(MC_AssisParam.Gear_SMART.CurrentMax_K * MC_ConfigParam1.CurrentLimit * 1000 >> 17) * MC_CadenceLimit_K;
  1159. CurrentLimitPresent = MC_DataSet_Linear_Process(CurrentLimitSet, CurrentLimitPresent, 5 ,1);
  1160. PID_IMax.hLower_Limit_Output = -(MC_AssisParam.Gear_SMART.Upper_Iq); //Lower Limit for Output limitation
  1161. PID_IMax.hUpper_Limit_Output = 0; //Upper Limit for Output limitation
  1162. PID_IMax.wLower_Limit_Integral = -(MC_AssisParam.Gear_SMART.Upper_Iq << 10); // 放大1024
  1163. PID_IMax.wUpper_Limit_Integral = 0; // 放大1024
  1164. }
  1165. else
  1166. {
  1167. switch( GearSt & 0x0F )
  1168. {
  1169. case 1:
  1170. //torqueAppRatio = torqueApp * 0.5;
  1171. //torqueAppRatio = (int16_t)((uint32_t)(torqueApp * Coefficient_GainCal( 51 ,410, 1300, torqueApp)) >> 10 ); //0.4
  1172. torqueAppRatio = (int16_t)((uint32_t)(torqueApp * Coefficient_GainCal( 51 ,MC_AssisParam.Gear_ECO.Gain_K, 1300, torqueApp)) >> 10 ); //0.4
  1173. torqueAppRatio = (int16_t)((int32_t)torqueAppRatio * MC_ConfigParam1.UserAdjParam_ECO.Assist_K_GAIN / 100); //上位机助力比增益调节
  1174. //加速增益调节
  1175. if(MC_ConfigParam1.UserAdjParam_ECO.StarModel_GAIN <= 80)
  1176. {
  1177. torqueAppAccStep = MC_AssisParam.Gear_ECO.AccCnt - 1;
  1178. }
  1179. else if(MC_ConfigParam1.UserAdjParam_ECO.StarModel_GAIN >= 120)
  1180. {
  1181. torqueAppAccStep = MC_AssisParam.Gear_ECO.AccCnt + 1;
  1182. }
  1183. else
  1184. {
  1185. torqueAppAccStep = MC_AssisParam.Gear_ECO.AccCnt;
  1186. }
  1187. //给定上限
  1188. torqueAppRatio = (torqueAppRatio > MC_AssisParam.Gear_ECO.Upper_Iq) ? MC_AssisParam.Gear_ECO.Upper_Iq : torqueAppRatio;
  1189. //限流参数设置
  1190. CurrentLimitSet = (uint32_t)(MC_AssisParam.Gear_ECO.CurrentMax_K * MC_ConfigParam1.CurrentLimit * 1000 >> 17) * 100;
  1191. CurrentLimitPresent = MC_DataSet_Linear_Process(CurrentLimitSet, CurrentLimitPresent, 5 ,1);
  1192. PID_IMax.hLower_Limit_Output = -(MC_AssisParam.Gear_ECO.Upper_Iq); //Lower Limit for Output limitation
  1193. PID_IMax.hUpper_Limit_Output = 0; //Upper Limit for Output limitation
  1194. PID_IMax.wLower_Limit_Integral = -(MC_AssisParam.Gear_ECO.Upper_Iq << 10); // 放大1024
  1195. PID_IMax.wUpper_Limit_Integral = 0; // 放大1024
  1196. break;
  1197. case 2:
  1198. //torqueAppRatio = torqueApp * 0.8;
  1199. //torqueAppRatio = (int16_t)((uint32_t)(torqueApp * Coefficient_GainCal( 102 ,717, 1300, torqueApp)) >> 10 ); //0.7
  1200. torqueAppRatio = (int16_t)((uint32_t)(torqueApp * Coefficient_GainCal( 102 ,MC_AssisParam.Gear_NORM.Gain_K, 1300, torqueApp)) >> 10 ); //0.7
  1201. torqueAppRatio = (int16_t)((int32_t)torqueAppRatio * MC_ConfigParam1.UserAdjParam_NORM.Assist_K_GAIN / 100); //上位机助力比增益调节
  1202. //加速增益调节
  1203. if(MC_ConfigParam1.UserAdjParam_NORM.StarModel_GAIN <= 80)
  1204. {
  1205. torqueAppAccStep = MC_AssisParam.Gear_NORM.AccCnt - 1;
  1206. }
  1207. else if(MC_ConfigParam1.UserAdjParam_NORM.StarModel_GAIN >= 120)
  1208. {
  1209. torqueAppAccStep = MC_AssisParam.Gear_NORM.AccCnt + 1;
  1210. }
  1211. else
  1212. {
  1213. torqueAppAccStep = MC_AssisParam.Gear_NORM.AccCnt;
  1214. }
  1215. //给定上限
  1216. torqueAppRatio = (torqueAppRatio > MC_AssisParam.Gear_NORM.Upper_Iq) ? MC_AssisParam.Gear_NORM.Upper_Iq : torqueAppRatio;
  1217. //限流参数设置
  1218. CurrentLimitSet = (uint32_t)(MC_AssisParam.Gear_NORM.CurrentMax_K * MC_ConfigParam1.CurrentLimit * 1000 >> 17) * 100;
  1219. CurrentLimitPresent = MC_DataSet_Linear_Process(CurrentLimitSet, CurrentLimitPresent, 5 ,1);
  1220. PID_IMax.hLower_Limit_Output = -(MC_AssisParam.Gear_NORM.Upper_Iq); //Lower Limit for Output limitation
  1221. PID_IMax.hUpper_Limit_Output = 0; //Upper Limit for Output limitation
  1222. PID_IMax.wLower_Limit_Integral = -(MC_AssisParam.Gear_NORM.Upper_Iq << 10); // 放大1024
  1223. PID_IMax.wUpper_Limit_Integral = 0; // 放大1024
  1224. break;
  1225. case 3:
  1226. //torqueAppRatio = torqueApp * 1.0;
  1227. //torqueAppRatio = (int16_t)((uint32_t)(torqueApp * Coefficient_GainCal( 204 ,1024, 1300, torqueApp)) >> 10 ); //1.0
  1228. torqueAppRatio = (int16_t)((uint32_t)(torqueApp * Coefficient_GainCal( 204 , MC_AssisParam.Gear_SPORT.Gain_K, 1300, torqueApp)) >> 10 ); //1.0
  1229. torqueAppRatio = (int16_t)((int32_t)torqueAppRatio * MC_ConfigParam1.UserAdjParam_SPORT.Assist_K_GAIN / 100); //上位机助力比增益调节
  1230. //加速增益调节
  1231. if(MC_ConfigParam1.UserAdjParam_SPORT.StarModel_GAIN <= 80)
  1232. {
  1233. torqueAppAccStep = MC_AssisParam.Gear_SPORT.AccCnt - 1;
  1234. }
  1235. else if(MC_ConfigParam1.UserAdjParam_SPORT.StarModel_GAIN >= 120)
  1236. {
  1237. torqueAppAccStep = MC_AssisParam.Gear_SPORT.AccCnt + 1;
  1238. }
  1239. else
  1240. {
  1241. torqueAppAccStep = MC_AssisParam.Gear_SPORT.AccCnt;
  1242. }
  1243. //给定上限
  1244. torqueAppRatio = (torqueAppRatio > MC_AssisParam.Gear_SPORT.Upper_Iq) ? MC_AssisParam.Gear_SPORT.Upper_Iq : torqueAppRatio;;
  1245. //限流参数设置
  1246. CurrentLimitSet = (uint32_t)(MC_AssisParam.Gear_SPORT.CurrentMax_K * MC_ConfigParam1.CurrentLimit * 1000 >> 17) * MC_CadenceLimit_K;
  1247. CurrentLimitPresent = MC_DataSet_Linear_Process(CurrentLimitSet, CurrentLimitPresent, 5 ,1);
  1248. PID_IMax.hLower_Limit_Output = -(MC_AssisParam.Gear_SPORT.Upper_Iq); //Lower Limit for Output limitation
  1249. PID_IMax.hUpper_Limit_Output = 0; //Upper Limit for Output limitation
  1250. PID_IMax.wLower_Limit_Integral = -(MC_AssisParam.Gear_SPORT.Upper_Iq << 10); // 放大1024
  1251. PID_IMax.wUpper_Limit_Integral = 0; // 放大1024
  1252. break;
  1253. case 4:
  1254. //torqueAppRatio = torqueApp * 1.5;
  1255. //torqueAppRatio = (int16_t)((uint32_t)(torqueApp * Coefficient_GainCal( 307 ,1536, 1300, torqueApp)) >> 10 ); // 1.5
  1256. torqueAppRatio = (int16_t)((uint32_t)(torqueApp * Coefficient_GainCal( 307 ,MC_AssisParam.Gear_TURBO.Gain_K, 1300, torqueApp)) >> 10 ); // 1.5
  1257. torqueAppRatio = (int16_t)((int32_t)torqueAppRatio * MC_ConfigParam1.UserAdjParam_TURBO.Assist_K_GAIN / 100); //上位机助力比增益调节
  1258. //加速增益调节
  1259. if(MC_ConfigParam1.UserAdjParam_TURBO.StarModel_GAIN <= 80)
  1260. {
  1261. torqueAppAccStep = MC_AssisParam.Gear_TURBO.AccCnt - 1;
  1262. }
  1263. else if(MC_ConfigParam1.UserAdjParam_TURBO.StarModel_GAIN >= 120)
  1264. {
  1265. torqueAppAccStep = MC_AssisParam.Gear_TURBO.AccCnt + 1;
  1266. }
  1267. else
  1268. {
  1269. torqueAppAccStep = MC_AssisParam.Gear_TURBO.AccCnt;
  1270. }
  1271. //给定上限
  1272. torqueAppRatio = (torqueAppRatio > MC_AssisParam.Gear_TURBO.Upper_Iq) ? MC_AssisParam.Gear_TURBO.Upper_Iq : torqueAppRatio;
  1273. //限流参数设置
  1274. CurrentLimitSet = (uint32_t)(MC_AssisParam.Gear_TURBO.CurrentMax_K * MC_ConfigParam1.CurrentLimit * 1000 >> 17) * MC_CadenceLimit_K;
  1275. CurrentLimitPresent = MC_DataSet_Linear_Process(CurrentLimitSet, CurrentLimitPresent, 5 ,1);
  1276. PID_IMax.hLower_Limit_Output = -(MC_AssisParam.Gear_TURBO.Upper_Iq); //Lower Limit for Output limitation
  1277. PID_IMax.hUpper_Limit_Output = 0; //Upper Limit for Output limitation
  1278. PID_IMax.wLower_Limit_Integral = -(MC_AssisParam.Gear_TURBO.Upper_Iq << 10); // 放大1024
  1279. PID_IMax.wUpper_Limit_Integral = 0; // 放大1024
  1280. break;
  1281. default:break;
  1282. }
  1283. }
  1284. //给定上限
  1285. torqueAppRatio = (torqueAppRatio > MC_AssisParam.Gear_TURBO.Upper_Iq) ? MC_AssisParam.Gear_TURBO.Upper_Iq : torqueAppRatio;
  1286. //随车速调节助力比
  1287. #if 0
  1288. if(MC_ConfigParam1.StarModel == MC_StarMode_DYNAMIC)
  1289. {
  1290. torqueAppRatio = (uint16_t)((uint32_t)(torqueAppRatio * Function_Linear_3Stage(90, -2, MC_ConfigParam1.SpeedLimit * 10, 160, MC_RunInfo.BikeSpeed)) >> 10);
  1291. }
  1292. else if(MC_ConfigParam1.StarModel == MC_StarMode_SOFT)
  1293. {
  1294. torqueAppRatio = (uint16_t)((uint32_t)(torqueAppRatio * Function_Linear_3Stage(120, -3, MC_ConfigParam1.SpeedLimit * 10, 160, MC_RunInfo.BikeSpeed)) >> 10);
  1295. }
  1296. else
  1297. {
  1298. torqueAppRatio = (uint16_t)((uint32_t)(torqueAppRatio * Function_Linear_3Stage(MC_ConfigParam1.SpeedLimit * 10, 0, MC_ConfigParam1.SpeedLimit * 10, 160, MC_RunInfo.BikeSpeed)) >> 10);
  1299. }
  1300. #elif 1
  1301. torqueAppRatio = (uint16_t)((uint32_t)(torqueAppRatio * Function_Linear_3Stage(MC_ConfigParam1.SpeedLimit * 10, 0, MC_ConfigParam1.SpeedLimit * 10, 160, MC_RunInfo.BikeSpeed)) >> 10);
  1302. #endif
  1303. //力矩给定处理
  1304. if(SoftStartFlag == SET) //启动处理
  1305. {
  1306. if(MC_ConfigParam1.StarModel == MC_StarMode_DYNAMIC)
  1307. {
  1308. SoftStartDelayTime = 100; //启动处理延时100ms
  1309. SoftStartAcc = 10; //10ms递增0.1倍
  1310. }
  1311. else if(MC_ConfigParam1.StarModel == MC_StarMode_SOFT)
  1312. {
  1313. SoftStartDelayTime = 900; //启动处理延时900ms
  1314. SoftStartAcc = 90; //90ms递增0.1倍
  1315. }
  1316. else
  1317. {
  1318. SoftStartDelayTime = 300; //启动处理延时300ms
  1319. SoftStartAcc = 30; //30ms递增0.1倍
  1320. }
  1321. SoftStartDelayTimeCount++;
  1322. if(SoftStartDelayTimeCount <= SoftStartDelayTime) // 缓启动过程,按照0.1倍率逐步增加加减速斜率
  1323. {
  1324. if(torqueAppRatio > torqueAppLag)
  1325. {
  1326. if((SoftStartDelayTimeCount % (10 - SoftStartDelayTimeCount / SoftStartAcc)) == 0)
  1327. {
  1328. torqueAppLag += torqueAppAccStep;
  1329. if(torqueAppLag > torqueAppRatio)
  1330. {
  1331. torqueAppLag = torqueAppRatio;
  1332. }
  1333. }
  1334. }
  1335. else if(torqueAppRatio < torqueAppLag)
  1336. {
  1337. torqueAppLag -= torqueAppDecStep;
  1338. if(torqueAppLag < torqueAppRatio)
  1339. {
  1340. torqueAppLag = torqueAppRatio;
  1341. }
  1342. }
  1343. }
  1344. else
  1345. {
  1346. SoftStartDelayTimeCount = 0;
  1347. SoftStartFlag = RESET;
  1348. }
  1349. }
  1350. else //正常骑行
  1351. {
  1352. if(torqueAppRatio > torqueAppLag)
  1353. {
  1354. torqueAppLag += torqueAppAccStep;
  1355. if(torqueAppLag > torqueAppRatio)
  1356. {
  1357. torqueAppLag = torqueAppRatio;
  1358. }
  1359. }
  1360. else if(torqueAppRatio < torqueAppLag)
  1361. {
  1362. torqueAppLag -= torqueAppDecStep;
  1363. if(torqueAppLag < torqueAppRatio)
  1364. {
  1365. torqueAppLag = torqueAppRatio;
  1366. }
  1367. }
  1368. }
  1369. MC_TorqueProcess_Param.TorqueRefEnd = torqueAppLag;
  1370. MC_TorqueProcess_Param.TorqueRefEnd = (MC_TorqueProcess_Param.TorqueRefEnd < 6) ? 6 : MC_TorqueProcess_Param.TorqueRefEnd;
  1371. //限速点处理
  1372. if( MC_SpeedSensorData.Speed_Data > (MC_ConfigParam1.SpeedLimit * 10 + 20) ) //限速值+2
  1373. {
  1374. MC_TorqueProcess_Param.MotorStopLock_Flag = SET;
  1375. MC_TorqueProcess_Param.TorqueRefEnd = 0;
  1376. //停机处理
  1377. MC_MotorStop(&MC_StarFlag);
  1378. }
  1379. #if 0 //低于限速点启动电机
  1380. else if(MC_RunInfo.BikeSpeed < ((MC_ConfigParam.SpeedLimit) * 10))
  1381. {
  1382. MC_MotorStar(&MC_StarFlag);
  1383. }
  1384. #elif 1 //低于断电点即启动电机
  1385. else
  1386. {
  1387. MC_MotorStar(&MC_StarFlag);
  1388. }
  1389. #endif
  1390. }
  1391. #if 1
  1392. static uint16_t K_ByVoltage_Set_Old = 1024;
  1393. uint16_t K_ByVoltage_Set;
  1394. static uint16_t K_ByVoltage_Result;
  1395. uint16_t K_ByTemperature_Set;
  1396. static uint16_t K_ByTemperature_Result;
  1397. //根据电压调节输出
  1398. K_ByVoltage_Set = MC_Cal_K_ByVoltage(MC_RunInfo.BusVoltage, MC_MotorParam.Rate_Voltage, K_ByVoltage_Set_Old);//根据母线电压计算衰减比例,递减
  1399. K_ByVoltage_Set_Old = K_ByVoltage_Set;
  1400. K_ByVoltage_Result = MC_DataSet_Linear_Process(K_ByVoltage_Set, K_ByVoltage_Result, 1, 1); //设定值与给定值线性处理
  1401. //根据温度调节输出
  1402. K_ByTemperature_Set = MC_Cal_K_ByTemperature(MC_RunInfo.T_Coil, MC_ConfigParam1.TempTH_Alarm); //根据温度计算衰减比例
  1403. K_ByTemperature_Result = MC_DataSet_Linear_Process(K_ByTemperature_Set, K_ByTemperature_Result, 1, 1); //设定值与给定值线性处理
  1404. #else
  1405. uint16_t K_ByVoltage_Result = 1024;
  1406. uint16_t K_ByTemperature_Result = 1024;
  1407. #endif
  1408. //限流计算
  1409. IqRefByInPower = PID_Regulator(CurrentLimitPresent / 100, (MC_RunInfo.BusCurrent >> 7), &PID_IMax);
  1410. Torque_Ref_Temp = ((int32_t)MC_TorqueProcess_Param.TorqueRefEnd * K_ByVoltage_Result) >> 10;
  1411. Torque_Ref_Temp = (Torque_Ref_Temp * K_ByTemperature_Result) >> 10;
  1412. Torque_Ref_Temp = (Torque_Ref_Temp + IqRefByInPower) >> 1;
  1413. p_MC_CalParam.Ref_Torque = (int16_t)Torque_Ref_Temp;
  1414. p_MC_CalParam.Foc_Flag = SET;
  1415. p_MC_CalParam.AssistRunMode = MC_AssistRunMode_TORQUE;
  1416. return (p_MC_CalParam);
  1417. }
  1418. /******************************全局函数定义*****************************/
  1419. //传感器初始化
  1420. void MC_SensorInit(void)
  1421. {
  1422. //霍尔传感器IO设置
  1423. HallSensor_GPIO_Init();
  1424. //霍尔电角度初始化
  1425. HallSensorAngle_Init();
  1426. //踏频传感器IO设置
  1427. CadenceSensor_GPIO_Init();
  1428. //速度传感器IO设置
  1429. SpeedSensor_GPIO_Init();
  1430. //刹车信号和Gear信号检测IO设置
  1431. KeyInitial();
  1432. //力矩传感器零点初值
  1433. TorqueOffSetDefaultData_Init(&TorqueOffSetData, ADC1_Result[ADC1_RANK_TORQUE_SENSOR]);
  1434. //指拨零点初值
  1435. GasSensorOffSet_Init(&GasSensor_OffSet, ADC1_Result[ADC1_RANK_GAS]);
  1436. //ICM20600初始化
  1437. ICM20600_initialize();
  1438. if(ICM20600_OK_Flag == TRUE)
  1439. {
  1440. ICM20600_coefficientinitialize(RANGE_250_DPS, RANGE_2G, &ICM20600Sensor);
  1441. }
  1442. }
  1443. //MC控制初始化
  1444. void MC_Init(void)
  1445. {
  1446. //PID参数初始化
  1447. PID_Init(MC_ConfigParam1.SerialNum);
  1448. //助力参数初始化
  1449. UpdateGearParam(MC_ConfigParam1.SerialNum);
  1450. //三相电流零点校准
  1451. SVPWM_3ShuntCurrentReadingCalibration(&MC_ErrorCode);
  1452. //母线电流零点校准
  1453. CurrentReadingCalibration(&MC_ErrorCode);
  1454. //力矩传感器零点值处理
  1455. TorqueOffSetData_Process(&TorqueOffSetData, ADC1_Result[ADC1_RANK_TORQUE_SENSOR]);//145ms
  1456. //智能档位初始化处理
  1457. if(MC_ConfigParam1.NoPBU_Flag == MC_SUPPORT_ENABLE)
  1458. {
  1459. MC_ControlCode.GearSt = MC_GearSt_SMART;
  1460. Update_MC_ControlCode_Back();
  1461. }
  1462. //12V驱动电源初始化
  1463. Power12V_Driver_Init();
  1464. //打开12V驱动电源
  1465. Power12V_Driver_Process(SET);
  1466. }
  1467. //MC控制参数初始化
  1468. void MC_ControlParam_Init(void)
  1469. {
  1470. //清除推行模式初始变量
  1471. MC_WalkProcess_Param.IsEnterFlag = FALSE;
  1472. MC_WalkProcess_Param.MotorSpeedSetBigin = 0;
  1473. //清除力矩模式初始变量
  1474. MC_TorqueProcess_Param.MotorStopLock_Flag = SET;
  1475. MC_TorqueProcess_Param.TorqueApp = 0;
  1476. MC_TorqueProcess_Param.TorqueRef = 0;
  1477. MC_TorqueProcess_Param.TorqueRefEnd = 0;
  1478. //全局运算变量归零
  1479. IqFdbFlt =0;
  1480. IdFdbFlt = 0;
  1481. VoltSquareFlt = 0;
  1482. UqVoltFlt = 0;
  1483. UdVoltFlt = 0;
  1484. //PDI积分清零
  1485. PID_Flux_InitStructure.wIntegral = 0;
  1486. PID_Torque_InitStructure.wIntegral = 0;
  1487. PID_Weak_InitStructure.wIntegral = 0;
  1488. PID_IMax.wIntegral = 0;
  1489. PID_MotorSpd.wIntegral = 0;
  1490. PID_ConstantPower.wIntegral = 0;
  1491. }
  1492. //控制参数输入值计算
  1493. void MC_CalParam_Cal(MC_WorkMode_Struct_t p_MC_WorkMode, \
  1494. ADC_SensorData_Struct_t p_ADC_SensorData, \
  1495. MC_GearSt_Struct_t GearSt, \
  1496. TrueOrFalse_Flag_Struct_t Break_Flag, \
  1497. TrueOrFalse_Flag_Struct_t GearSensor_Flag, \
  1498. MC_CalParam_Struct_t* p_MC_CalParam)
  1499. {
  1500. MC_AssistRunMode_Struct_t MC_AssistRunMode_Temp;
  1501. static FlagStatus MC_AssistRunMode_ShiftFlag = RESET; //电机助力模式切换标志
  1502. //根据指拨信号、助力档位指令、刹车信号判断助力模式
  1503. MC_AssistRunMode_Temp = MC_JudgeAsistRunMode_Process(MC_ConfigParam1.GasCtrlMode_Flag, p_ADC_SensorData.GasSensor, GearSt, (TrueOrFalse_Flag_Struct_t)(Break_Flag & GearSensor_Flag & Bike_Attitude.FellDown_flag));//TRUE 0, FALSE 1
  1504. //发生助力模式切换时,清空变量
  1505. if(MC_AssistRunMode_Temp != p_MC_CalParam->AssistRunMode)
  1506. {
  1507. if(MC_AssistRunMode_ShiftFlag == RESET)
  1508. {
  1509. MC_AssistRunMode_Temp = MC_AssistRunMode_INVALID;
  1510. MC_AssistRunMode_ShiftFlag = SET;
  1511. SpeedSetMiddle = 0; //指拨模式,清零速度中间量
  1512. SpdMotorDivWheelFlt = 0;
  1513. }
  1514. }
  1515. //助力模式处理
  1516. switch(MC_AssistRunMode_Temp)
  1517. {
  1518. //指拨模式
  1519. case MC_AssistRunMode_GAS:
  1520. {
  1521. //计算FOC控制输入
  1522. *p_MC_CalParam = MC_AssistRunMode_Gas_Process(p_ADC_SensorData.GasSensor, (MC_GearSt_Struct_t)(GearSt & 0x0F));
  1523. //助力模式切换标志复位
  1524. MC_AssistRunMode_ShiftFlag = RESET;
  1525. break;
  1526. }
  1527. //推行模式
  1528. case MC_AssistRunMode_WALK:
  1529. {
  1530. //计算FOC控制输入
  1531. if(MC_WalkProcess_Param.IsEnterFlag == FALSE)
  1532. {
  1533. MC_WalkProcess_Param.MotorSpeedSetBigin = (uint32_t)MC_RunInfo.MotorSpeed << 5;
  1534. MC_WalkProcess_Param.IsEnterFlag = TRUE;
  1535. }
  1536. *p_MC_CalParam = MC_AssistRunMode_Walk_Process(p_MC_WorkMode);
  1537. //助力模式切换标志复位
  1538. MC_AssistRunMode_ShiftFlag = RESET;
  1539. break;
  1540. }
  1541. //踏频模式
  1542. case MC_AssistRunMode_CADENCE:
  1543. {
  1544. //计算FOC控制输入
  1545. *p_MC_CalParam = MC_AssistRunMode_Cadence_Process(GearSt);
  1546. //助力模式切换标志复位
  1547. MC_AssistRunMode_ShiftFlag = RESET;
  1548. break;
  1549. }
  1550. //力矩模式
  1551. case MC_AssistRunMode_TORQUE:
  1552. {
  1553. //计算FOC控制输入
  1554. //*p_MC_CalParam = MC_AssistRunMode_Torque_Process(p_ADC_SensorData.TorqueSensor, GearSt);
  1555. *p_MC_CalParam = MC_AssistRunMode_Torque_compensation(p_ADC_SensorData.TorqueSensor, GearSt);
  1556. //助力模式切换标志复位
  1557. MC_AssistRunMode_ShiftFlag = RESET;
  1558. break;
  1559. }
  1560. //空闲模式或存在故障
  1561. case MC_AssistRunMode_INVALID: default:
  1562. {
  1563. //停机处理
  1564. MC_MotorStop(&MC_StarFlag);
  1565. //更新母线电流零点值
  1566. CurrentReadingCalibration(&MC_ErrorCode);
  1567. //控制计算值初始化为默认值
  1568. p_MC_CalParam->AssistRunMode = MC_AssistRunMode_INVALID;
  1569. p_MC_CalParam->Foc_Flag = RESET;
  1570. p_MC_CalParam->Ref_Torque = 0;
  1571. p_MC_CalParam->Ref_Speed = 0;
  1572. break;
  1573. }
  1574. }
  1575. }
  1576. void MC_MotorStop(FlagStatus* StarFlag)
  1577. {
  1578. //关闭PWM输出
  1579. Pwm_Timer_Stop();
  1580. //FOC运算停止
  1581. FOC_Disable();
  1582. //控制参数归零
  1583. MC_ControlParam_Init();
  1584. //电机启动标志复位
  1585. *StarFlag = RESET;
  1586. }
  1587. void MC_MotorStar(FlagStatus* StarFlag)
  1588. {
  1589. if(*StarFlag == RESET)
  1590. {
  1591. //开启PWM输出
  1592. Enable_Pwm_Output();
  1593. //霍尔电角度初始化
  1594. HallSensorAngle_Init();
  1595. //FOC运算启动
  1596. FOC_Enable();
  1597. //电机启动标志置位
  1598. *StarFlag = SET;
  1599. }
  1600. }
  1601. /*
  1602. 指拨模式计算速比,计算费时,在主循环调用
  1603. */
  1604. void SpdProportion_calculate(void)
  1605. {
  1606. if(SpdProportion_CAL_flag==1)
  1607. {
  1608. SpdProportion_StandardDeviation = Standard_deviation_aver(SpdProportion_buff, 50, &test_SpdProportionAver);
  1609. test_StandardDeviation = (int32_t)(SpdProportion_StandardDeviation );
  1610. SpdProportion_CAL_flag = 0;
  1611. /*更新速比*/
  1612. if(test_StandardDeviation < 20)
  1613. {
  1614. SpdProportion = test_SpdProportionAver;
  1615. }
  1616. }
  1617. }