motor_control.c 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652
  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. #if 1
  514. //踩踏力矩输入
  515. MC_TorqueProcess_Param.TorqueApp = SenorData;
  516. #elif 1
  517. //输入阶跃
  518. MC_TorqueProcess_Param.TorqueApp = 1000;
  519. //踏频设为启动
  520. MC_CadenceResult.Cadence_Dir = MC_Cadence_Forward;
  521. MC_CadenceResult.IsStopFlag = FALSE;
  522. #elif 1
  523. //输入斜坡
  524. static uint32_t WaveTime_Zero = 0;
  525. static uint32_t Time_Enter = 0;
  526. if((HAL_GetTick() - Time_Enter) > 10) // 超时10ms未进入,波形发生初始时刻清零
  527. {
  528. WaveTime_Zero = HAL_GetTick();
  529. }
  530. Time_Enter = HAL_GetTick();
  531. MC_TorqueProcess_Param.TorqueApp = RampWaveGenerate(WaveTime_Zero, 6000, 2100);
  532. //踏频设为启动
  533. MC_CadenceResult.Cadence_Dir = MC_Cadence_Forward;
  534. MC_CadenceResult.IsStopFlag = FALSE;
  535. #elif 1
  536. //输入三角波,测试输出响应
  537. static uint32_t WaveTime_Zero = 0;
  538. static uint32_t Time_Enter = 0;
  539. if((HAL_GetTick() - Time_Enter) > 10) // 超时10ms未进入,波形发生初始时刻清零
  540. {
  541. WaveTime_Zero = HAL_GetTick();
  542. }
  543. Time_Enter = HAL_GetTick();
  544. MC_TorqueProcess_Param.TorqueApp = TriangleWaveGenerate(WaveTime_Zero, 500, 1000 ,1500);
  545. //踏频设为启动
  546. MC_CadenceResult.Cadence_Dir = MC_Cadence_Forward;
  547. MC_CadenceResult.IsStopFlag = FALSE;
  548. #elif 1
  549. //输入方波,测试输出响应
  550. static uint32_t WaveTime_Zero = 0;
  551. static uint32_t Time_Enter = 0;
  552. if((HAL_GetTick() - Time_Enter) > 10) // 超时10ms未进入,波形发生初始时刻清零
  553. {
  554. WaveTime_Zero = HAL_GetTick();
  555. }
  556. Time_Enter = HAL_GetTick();
  557. MC_TorqueProcess_Param.TorqueApp = SquareWaveGenerate(WaveTime_Zero, 5000, 8000, 1500);
  558. //踏频设为启动
  559. MC_CadenceResult.Cadence_Dir = MC_Cadence_Forward;
  560. MC_CadenceResult.IsStopFlag = FALSE;
  561. #endif
  562. //低力矩停机
  563. TorqueStopData = (MC_TorqueCorrectParam.StarData < 200) ? 100 : (MC_TorqueCorrectParam.StarData >> 1);
  564. if(MC_TorqueProcess_Param.TorqueApp >= (TorqueStopData))
  565. {
  566. TorqueStopDelayTimeCnt = HAL_GetTick();
  567. }
  568. else
  569. {
  570. if(MC_RunInfo.MotorSpeed > 200)
  571. {
  572. TorqueStopDelayTime = 218400 / MC_RunInfo.MotorSpeed; //60s / (电机转速 / 4.55 / 2.4) / 3,曲柄1/3圈
  573. }
  574. else
  575. {
  576. TorqueStopDelayTime = 1200;
  577. }
  578. TorqueStopDelayTime= (TorqueStopDelayTime < 500) ? 500 : TorqueStopDelayTime;
  579. if((HAL_GetTick() - TorqueStopDelayTimeCnt) > TorqueStopDelayTime)//超时1200ms
  580. {
  581. MC_TorqueProcess_Param.MotorStopLock_Flag = SET;
  582. }
  583. }
  584. //启动值判断
  585. if(MC_RunInfo.BikeSpeed > 60)
  586. {
  587. TorqueStartData = (MC_TorqueCorrectParam.StarData < 200 ? 150 \
  588. : (MC_TorqueCorrectParam.StarData > 700 ? 525 \
  589. : (MC_TorqueCorrectParam.StarData * 3 >> 2)));
  590. }
  591. else
  592. {
  593. TorqueStartData = (MC_TorqueCorrectParam.StarData < 200 ? 150 \
  594. : (MC_TorqueCorrectParam.StarData > 700 ? 525 \
  595. : MC_TorqueCorrectParam.StarData));
  596. }
  597. if(MC_TorqueProcess_Param.TorqueApp >= TorqueStartData)
  598. {
  599. MC_TorqueProcess_Param.MotorStopLock_Flag = RESET;
  600. }
  601. //踏频反向或踏频停止停机
  602. if((MC_CadenceResult.Cadence_Dir == MC_Cadence_Backward) ||
  603. (MC_CadenceResult.IsStopFlag == TRUE)
  604. )
  605. {
  606. MC_TorqueProcess_Param.MotorStopLock_Flag = SET;
  607. }
  608. //停机状态,延时处理
  609. if(MC_TorqueProcess_Param.MotorStopLock_Flag == SET)
  610. {
  611. if(MC_TorqueProcess_Param.TorqueRefEnd <= 14)
  612. {
  613. MC_TorqueProcess_Param.TorqueRefEnd = 0;
  614. //停机处理
  615. MC_MotorStop(&MC_StarFlag);
  616. }
  617. else
  618. {
  619. MC_TorqueProcess_Param.TorqueRefEnd -= 14; //这里影响到停止踩踏后的断电时间
  620. MC_MotorStar(&MC_StarFlag);
  621. }
  622. }
  623. //力矩给定升降速处理
  624. else
  625. {
  626. //按照助力档位调节力矩输入值
  627. switch(GearSt)
  628. {
  629. case MC_GearSt_Torque_ECO:
  630. {
  631. //控制输入给定加速斜率
  632. TorqueAccStep = MC_AssisParam.Gear_ECO.AccCnt;
  633. //控制输入给定减速斜率
  634. TorqueDecStep = MC_AssisParam.Gear_ECO.DecCnt;
  635. //随力矩输入调节助力比
  636. if(MC_ConfigParam1.StarModel == MC_StarMode_DYNAMIC)//强劲模式
  637. {
  638. Torque_Temp = (uint16_t)((uint32_t)(MC_TorqueProcess_Param.TorqueApp * Coefficient_GainCal(300, MC_AssisParam.Gear_ECO.Gain_K, MC_AssisParam.Gear_ECO.Upper_Iq, MC_TorqueProcess_Param.TorqueApp)) >> 10);
  639. TorqueAccStep <<= 1;
  640. TorqueDecStep <<= 1;
  641. }
  642. else
  643. {
  644. 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);
  645. }
  646. //给定下限
  647. Torque_Temp = (Torque_Temp < MC_AssisParam.Gear_ECO.Lower_Iq) ? MC_AssisParam.Gear_ECO.Lower_Iq : Torque_Temp;
  648. //给定上限
  649. Torque_Temp = (Torque_Temp > MC_AssisParam.Gear_ECO.Upper_Iq) ? MC_AssisParam.Gear_ECO.Upper_Iq : Torque_Temp;
  650. //限流参数设置
  651. CurrentLimitSet = (uint32_t)(MC_AssisParam.Gear_ECO.CurrentMax_K * MC_ConfigParam1.CurrentLimit * 1000 >> 17) * 100;
  652. CurrentLimitPresent = MC_DataSet_Linear_Process(CurrentLimitSet, CurrentLimitPresent, 5 ,1);
  653. PID_IMax.hLower_Limit_Output = -(MC_AssisParam.Gear_ECO.Upper_Iq); //Lower Limit for Output limitation
  654. PID_IMax.hUpper_Limit_Output = 0; //Upper Limit for Output limitation
  655. PID_IMax.wLower_Limit_Integral = -(MC_AssisParam.Gear_ECO.Upper_Iq << 10); // 放大1024
  656. PID_IMax.wUpper_Limit_Integral = 0; // 放大1024
  657. break;
  658. }
  659. case MC_GearSt_Torque_NORM:
  660. {
  661. //控制输入给定加速斜率
  662. TorqueAccStep = MC_AssisParam.Gear_NORM.AccCnt;
  663. //控制输入给定减速斜率
  664. TorqueDecStep = MC_AssisParam.Gear_NORM.DecCnt;
  665. //随力矩输入调节助力比
  666. if(MC_ConfigParam1.StarModel == MC_StarMode_DYNAMIC)//强劲模式
  667. {
  668. Torque_Temp = (uint16_t)((uint32_t)(MC_TorqueProcess_Param.TorqueApp * Coefficient_GainCal(300, MC_AssisParam.Gear_NORM.Gain_K, MC_AssisParam.Gear_NORM.Upper_Iq, MC_TorqueProcess_Param.TorqueApp)) >> 10);
  669. TorqueAccStep <<= 1;
  670. TorqueDecStep <<= 1;
  671. }
  672. else
  673. {
  674. 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);
  675. }
  676. //给定下限
  677. Torque_Temp = (Torque_Temp < MC_AssisParam.Gear_NORM.Lower_Iq) ? MC_AssisParam.Gear_NORM.Lower_Iq : Torque_Temp;
  678. //给定上限
  679. Torque_Temp = (Torque_Temp > MC_AssisParam.Gear_NORM.Upper_Iq) ? MC_AssisParam.Gear_NORM.Upper_Iq : Torque_Temp;
  680. //限流参数设置
  681. CurrentLimitSet = (uint32_t)(MC_AssisParam.Gear_NORM.CurrentMax_K * MC_ConfigParam1.CurrentLimit * 1000 >> 17) * 100;
  682. CurrentLimitPresent = MC_DataSet_Linear_Process(CurrentLimitSet, CurrentLimitPresent, 5 ,1);
  683. PID_IMax.hLower_Limit_Output = -(MC_AssisParam.Gear_NORM.Upper_Iq); //Lower Limit for Output limitation
  684. PID_IMax.hUpper_Limit_Output = 0; //Upper Limit for Output limitation
  685. PID_IMax.wLower_Limit_Integral = -(MC_AssisParam.Gear_NORM.Upper_Iq << 10); // 放大1024
  686. PID_IMax.wUpper_Limit_Integral = 0; // 放大1024
  687. break;
  688. }
  689. case MC_GearSt_Torque_SPORT:
  690. {
  691. //控制输入给定加速斜率
  692. TorqueAccStep = MC_AssisParam.Gear_SPORT.AccCnt;
  693. //控制输入给定减速斜率
  694. TorqueDecStep = MC_AssisParam.Gear_SPORT.DecCnt;
  695. //随力矩输入调节助力比
  696. if(MC_ConfigParam1.StarModel == MC_StarMode_DYNAMIC)//强劲模式
  697. {
  698. Torque_Temp = (uint16_t)((uint32_t)(MC_TorqueProcess_Param.TorqueApp * Coefficient_GainCal(300, MC_AssisParam.Gear_SPORT.Gain_K, MC_AssisParam.Gear_SPORT.Upper_Iq, MC_TorqueProcess_Param.TorqueApp)) >> 10);
  699. TorqueAccStep <<= 1;
  700. TorqueDecStep <<= 1;
  701. }
  702. else
  703. {
  704. 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);
  705. }
  706. //给定下限
  707. Torque_Temp = (Torque_Temp < MC_AssisParam.Gear_SPORT.Lower_Iq) ? MC_AssisParam.Gear_SPORT.Lower_Iq : Torque_Temp;
  708. //给定上限
  709. Torque_Temp = (Torque_Temp > MC_AssisParam.Gear_SPORT.Upper_Iq) ? MC_AssisParam.Gear_SPORT.Upper_Iq : Torque_Temp;
  710. //限流参数设置
  711. CurrentLimitSet = (uint32_t)(MC_AssisParam.Gear_SPORT.CurrentMax_K * MC_ConfigParam1.CurrentLimit * 1000 >> 17) * MC_CadenceLimit_K;
  712. CurrentLimitPresent = MC_DataSet_Linear_Process(CurrentLimitSet, CurrentLimitPresent, 5 ,1);
  713. PID_IMax.hLower_Limit_Output = -(MC_AssisParam.Gear_SPORT.Upper_Iq); //Lower Limit for Output limitation
  714. PID_IMax.hUpper_Limit_Output = 0; //Upper Limit for Output limitation
  715. PID_IMax.wLower_Limit_Integral = -(MC_AssisParam.Gear_SPORT.Upper_Iq << 10); // 放大1024
  716. PID_IMax.wUpper_Limit_Integral = 0; // 放大1024
  717. break;
  718. }
  719. case MC_GearSt_Torque_TURBO:
  720. {
  721. //控制输入给定加速斜率
  722. TorqueAccStep = MC_AssisParam.Gear_TURBO.AccCnt;
  723. //控制输入给定减速斜率
  724. TorqueDecStep = MC_AssisParam.Gear_TURBO.DecCnt;
  725. //随力矩输入调节助力比
  726. if(MC_ConfigParam1.StarModel == MC_StarMode_DYNAMIC)//强劲模式
  727. {
  728. Torque_Temp = (uint16_t)((uint32_t)(MC_TorqueProcess_Param.TorqueApp * Coefficient_GainCal(300, MC_AssisParam.Gear_TURBO.Gain_K, MC_AssisParam.Gear_TURBO.Upper_Iq, MC_TorqueProcess_Param.TorqueApp)) >> 10);
  729. TorqueAccStep <<= 1;
  730. TorqueDecStep <<= 1;
  731. }
  732. else
  733. {
  734. 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);
  735. }
  736. //给定下限
  737. Torque_Temp = (Torque_Temp < MC_AssisParam.Gear_TURBO.Lower_Iq) ? MC_AssisParam.Gear_TURBO.Lower_Iq : Torque_Temp;
  738. //给定上限
  739. Torque_Temp = (Torque_Temp > MC_AssisParam.Gear_TURBO.Upper_Iq) ? MC_AssisParam.Gear_TURBO.Upper_Iq : Torque_Temp;
  740. //限流参数设置
  741. CurrentLimitSet = (uint32_t)(MC_AssisParam.Gear_TURBO.CurrentMax_K * MC_ConfigParam1.CurrentLimit * 1000 >> 17) * MC_CadenceLimit_K;
  742. CurrentLimitPresent = MC_DataSet_Linear_Process(CurrentLimitSet, CurrentLimitPresent, 5 ,1);
  743. PID_IMax.hLower_Limit_Output = -(MC_AssisParam.Gear_TURBO.Upper_Iq); //Lower Limit for Output limitation
  744. PID_IMax.hUpper_Limit_Output = 0; //Upper Limit for Output limitation
  745. PID_IMax.wLower_Limit_Integral = -(MC_AssisParam.Gear_TURBO.Upper_Iq << 10); // 放大1024
  746. PID_IMax.wUpper_Limit_Integral = 0; // 放大1024
  747. break;
  748. }
  749. case MC_GearSt_SMART:
  750. {
  751. //控制输入给定加速斜率
  752. TorqueAccStep = MC_AssisParam.Gear_SMART.AccCnt;
  753. //控制输入给定减速斜率
  754. TorqueDecStep = MC_AssisParam.Gear_SMART.DecCnt;
  755. //助力比控制系数
  756. Torque_Temp = (uint32_t)(MC_TorqueProcess_Param.TorqueApp * MC_TorqueProcess_Param.TorqueApp) / (MC_AssisParam.Gear_SMART.i_Sport_TH);
  757. if(MC_ConfigParam1.StarModel == MC_StarMode_DYNAMIC)//强劲模式
  758. {
  759. Torque_Temp = (uint16_t)((uint32_t)(Torque_Temp * Coefficient_GainCal(100, 1382, MC_AssisParam.Gear_SMART.i_Sport_TH, Torque_Temp)) >> 10);
  760. TorqueAccStep <<= 1;
  761. TorqueDecStep <<= 1;
  762. }
  763. //给定下限
  764. Torque_Temp = (Torque_Temp < MC_AssisParam.Gear_SMART.Lower_Iq) ? MC_AssisParam.Gear_SMART.Lower_Iq : Torque_Temp;
  765. //给定上限
  766. Torque_Temp = (Torque_Temp > MC_AssisParam.Gear_SMART.Upper_Iq) ? MC_AssisParam.Gear_SMART.Upper_Iq : Torque_Temp;
  767. //限流参数设置
  768. CurrentLimitSet = (uint32_t)(MC_AssisParam.Gear_SMART.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_SMART.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_SMART.Upper_Iq << 10); // 放大1024
  773. PID_IMax.wUpper_Limit_Integral = 0; // 放大1024
  774. break;
  775. }
  776. default:
  777. {
  778. TorqueAccStep = 0;
  779. TorqueDecStep = 0;
  780. Torque_Temp = 0;
  781. break;
  782. }
  783. }
  784. //随车速调节助力比
  785. if(MC_ConfigParam1.StarModel == MC_StarMode_DYNAMIC)
  786. {
  787. Torque_Temp = (uint16_t)((uint32_t)(Torque_Temp * Function_Linear_3Stage(90, -2, MC_ConfigParam1.SpeedLimit * 10, 52, MC_SpeedSensorData.Speed_Data)) >> 10);
  788. }
  789. else if(MC_ConfigParam1.StarModel == MC_StarMode_SOFT)
  790. {
  791. Torque_Temp = (uint16_t)((uint32_t)(Torque_Temp * Function_Linear_3Stage(120, -3, MC_ConfigParam1.SpeedLimit * 10, 52, MC_SpeedSensorData.Speed_Data)) >> 10);
  792. }
  793. else
  794. {
  795. 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);
  796. }
  797. MC_TorqueProcess_Param.TorqueRef = Torque_Temp;
  798. if(MC_TorqueProcess_Param.TorqueRef <= 0)
  799. {
  800. MC_TorqueProcess_Param.TorqueRef = 0;
  801. }
  802. //升降速曲线计算
  803. if( MC_SpeedSensorData.Speed_Data > (MC_ConfigParam1.SpeedLimit * 10) ) //限速处理
  804. {
  805. if((MC_TorqueProcess_Param.TorqueRef - MC_TorqueProcess_Param.TorqueRefEnd) > 2)
  806. {
  807. MC_TorqueProcess_Param.TorqueRefEnd += 1;
  808. }
  809. else if((MC_TorqueProcess_Param.TorqueRef - MC_TorqueProcess_Param.TorqueRefEnd) < (- 1))
  810. {
  811. MC_TorqueProcess_Param.TorqueRefEnd -= 1;
  812. }
  813. }
  814. else if( (Bike_Attitude.UpWardSlope_flag == TRUE)&&(MC_SpeedSensorData.Speed_Data < 100)) //上坡处理
  815. {
  816. if((MC_TorqueProcess_Param.TorqueRef - MC_TorqueProcess_Param.TorqueRefEnd) > 2)
  817. {
  818. MC_TorqueProcess_Param.TorqueRefEnd += TorqueAccStep;
  819. }
  820. else if((MC_TorqueProcess_Param.TorqueRef - MC_TorqueProcess_Param.TorqueRefEnd) < (- 1))
  821. {
  822. TorqueRefEndUpdateCount++;
  823. if(TorqueRefEndUpdateCount >=3)
  824. {
  825. TorqueRefEndUpdateCount = 0;
  826. MC_TorqueProcess_Param.TorqueRefEnd -= TorqueDecStep;
  827. }
  828. }
  829. }
  830. else //正常骑行
  831. {
  832. if((MC_TorqueProcess_Param.TorqueRef - MC_TorqueProcess_Param.TorqueRefEnd) > 2)
  833. {
  834. MC_TorqueProcess_Param.TorqueRefEnd += TorqueAccStep;
  835. }
  836. else if((MC_TorqueProcess_Param.TorqueRef - MC_TorqueProcess_Param.TorqueRefEnd) < (- 1))
  837. {
  838. MC_TorqueProcess_Param.TorqueRefEnd -= TorqueDecStep;
  839. }
  840. }
  841. MC_TorqueProcess_Param.TorqueRefEnd = (MC_TorqueProcess_Param.TorqueRefEnd < 6) ? 6 : MC_TorqueProcess_Param.TorqueRefEnd;
  842. //限速点处理
  843. if( MC_SpeedSensorData.Speed_Data > (MC_ConfigParam1.SpeedLimit * 10 + 20) ) //限速值+2
  844. {
  845. MC_TorqueProcess_Param.MotorStopLock_Flag = SET;
  846. MC_TorqueProcess_Param.TorqueRefEnd = 0;
  847. //停机处理
  848. MC_MotorStop(&MC_StarFlag);
  849. }
  850. #if 0 //低于限速点启动电机
  851. else if(MC_RunInfo.BikeSpeed < ((MC_ConfigParam1.SpeedLimit) * 10))
  852. {
  853. MC_MotorStar(&MC_StarFlag);
  854. }
  855. #elif 1 //低于断电点即启动电机
  856. else
  857. {
  858. MC_MotorStar(&MC_StarFlag);
  859. }
  860. #endif
  861. }
  862. #if 1
  863. static uint16_t K_ByVoltage_Set_Old = 1024;
  864. uint16_t K_ByVoltage_Set;
  865. static uint16_t K_ByVoltage_Result;
  866. uint16_t K_ByTemperature_Set;
  867. static uint16_t K_ByTemperature_Result;
  868. //根据电压调节输出
  869. K_ByVoltage_Set = MC_Cal_K_ByVoltage(MC_RunInfo.BusVoltage, MC_MotorParam.Rate_Voltage, K_ByVoltage_Set_Old);//根据母线电压计算衰减比例,递减
  870. K_ByVoltage_Set_Old = K_ByVoltage_Set;
  871. K_ByVoltage_Result = MC_DataSet_Linear_Process(K_ByVoltage_Set, K_ByVoltage_Result, 1, 1); //设定值与给定值线性处理
  872. //根据温度调节输出
  873. K_ByTemperature_Set = MC_Cal_K_ByTemperature(MC_RunInfo.T_Coil, MC_ConfigParam1.TempTH_Alarm); //根据温度计算衰减比例
  874. K_ByTemperature_Result = MC_DataSet_Linear_Process(K_ByTemperature_Set, K_ByTemperature_Result, 1, 1); //设定值与给定值线性处理
  875. #else
  876. uint16_t K_ByVoltage_Result = 1024;
  877. uint16_t K_ByTemperature_Result = 1024;
  878. #endif
  879. //限流计算
  880. IqRefByInPower = PID_Regulator(CurrentLimitPresent / 100, (MC_RunInfo.BusCurrent >> 7), &PID_IMax);
  881. Torque_Ref_Temp = ((int32_t)MC_TorqueProcess_Param.TorqueRefEnd * K_ByVoltage_Result) >> 10;
  882. Torque_Ref_Temp = (Torque_Ref_Temp * K_ByTemperature_Result) >> 10;
  883. Torque_Ref_Temp = (Torque_Ref_Temp + IqRefByInPower) >> 1;
  884. p_MC_CalParam.Ref_Torque = (int16_t)Torque_Ref_Temp;
  885. p_MC_CalParam.Foc_Flag = SET;
  886. p_MC_CalParam.AssistRunMode = MC_AssistRunMode_TORQUE;
  887. return (p_MC_CalParam);
  888. }
  889. uint16_t candenceCount; //踏频磁环信号计数变量,全局变量
  890. uint16_t torqueApp;
  891. uint16_t torque_NM; //力矩值,单位NM
  892. uint16_t torqueRank=0; //由踏频采样后的力矩值,得出的力矩给定
  893. static uint16_t torqueRankLast=0; //力矩给定历史值
  894. static uint16_t torqueRankBak=0; //力矩给定历史值备份
  895. static uint8_t flagUpDown=0; //输入力矩上升、下降的标志位
  896. static int32_t tmpFltSum=0; //输入力矩滤波的静态变量
  897. int16_t tmpFlted=0; //输入力矩滤波的临时变量
  898. int16_t torqueApp2; //力矩计算的临时变量
  899. uint8_t flagCandence=0; //踏频信号更新标志
  900. int16_t torqueAppLag=0; //力局地经过加减速处理后的临时变量
  901. int16_t torqueAppRatio=0; //力矩值计算助力比后的临时变量
  902. int16_t torqueAppAccStep= 5; //力矩给定升速斜率
  903. int16_t torqueAppDecStep=1; //力矩给定减速斜率
  904. uint16_t candenceBakAtTop=0; //踏频最大值备份
  905. uint16_t torqueByCandenceDec=0; //由踏频变化计算的力矩减弱量
  906. int16_t torqueDataFromCadence=0; //经过踏频信号采样的力矩值
  907. int32_t Taping_activeFlt=0; //踏频实时值滤波中间量
  908. int32_t Taping_activeFlted = 0; //踏频实时值滤波值
  909. #define TORQUE_TMP 28 //力矩AD值与1NM的换算系数
  910. MC_CalParam_Struct_t MC_AssistRunMode_Torque_compensation(uint16_t SenorData, MC_GearSt_Struct_t GearSt)
  911. {
  912. MC_CalParam_Struct_t p_MC_CalParam = {MC_AssistRunMode_INVALID, 0, 0, RESET};
  913. // uint8_t TorqueAccStep = 0;//力矩上升斜率
  914. // uint8_t TorqueDecStep = 0;//力矩下降斜率
  915. uint16_t TorqueStartData, TorqueStopData;//力矩启动值,力矩停机值
  916. int16_t Torque_Temp;
  917. int32_t Torque_Ref_Temp;
  918. static uint32_t TorqueStopDelayTimeCnt = 0;//低力矩停机计时
  919. uint16_t TorqueStopDelayTime;
  920. static int16_t IqRefByInPower;//限流计算结果
  921. // uint16_t CurrentLimit ; //限流值,不同助力档位更新
  922. static uint16_t CurrentLimitPresent; //限流实际值,做升降速处理
  923. uint16_t CurrentLimitSet;
  924. static uint8_t timeFlag_2ms = 0;
  925. timeFlag_2ms++;
  926. // uint16_t StopTimeDelay = 0;//根据力矩停机延时
  927. MC_TorqueProcess_Param.TorqueApp = SenorData;
  928. /*为下一步抓取最大值,力矩需要滤波*/
  929. tmpFltSum += (((int32_t )SenorData << 10) - tmpFltSum) >> 7;
  930. tmpFlted = (int16_t)(tmpFltSum >> 10);
  931. torque_NM = tmpFlted / TORQUE_TMP ; //转换为单位NM 的变量
  932. //低力矩停机
  933. TorqueStopData = (MC_TorqueCorrectParam.StarData < 200) ? 100 : (MC_TorqueCorrectParam.StarData >> 1);
  934. if(MC_TorqueProcess_Param.TorqueApp >= (TorqueStopData))
  935. {
  936. TorqueStopDelayTimeCnt = HAL_GetTick();
  937. }
  938. else
  939. {
  940. if(MC_RunInfo.MotorSpeed > 200)
  941. {
  942. TorqueStopDelayTime = 163800 / MC_RunInfo.MotorSpeed;
  943. }
  944. else
  945. {
  946. TorqueStopDelayTime = 1200;
  947. }
  948. if((HAL_GetTick() - TorqueStopDelayTimeCnt) > TorqueStopDelayTime)//超时1200ms
  949. {
  950. MC_TorqueProcess_Param.MotorStopLock_Flag = SET;
  951. }
  952. }
  953. //启动值判断
  954. if(MC_RunInfo.BikeSpeed > 60)
  955. {
  956. TorqueStartData = (MC_TorqueCorrectParam.StarData < 200 ? 150 \
  957. : (MC_TorqueCorrectParam.StarData > 700 ? 525 \
  958. : (MC_TorqueCorrectParam.StarData * 3 / 4)));
  959. }
  960. else
  961. {
  962. TorqueStartData = (MC_TorqueCorrectParam.StarData < 200 ? 150 \
  963. : (MC_TorqueCorrectParam.StarData > 700 ? 525 \
  964. : MC_TorqueCorrectParam.StarData));
  965. }
  966. if(MC_TorqueProcess_Param.TorqueApp >= TorqueStartData)
  967. {
  968. MC_TorqueProcess_Param.MotorStopLock_Flag = RESET;
  969. }
  970. //踏频反向或踏频停止停机
  971. if((MC_CadenceResult.Cadence_Dir == MC_Cadence_Backward) ||
  972. (MC_CadenceResult.IsStopFlag == TRUE)
  973. )
  974. {
  975. MC_TorqueProcess_Param.MotorStopLock_Flag = SET;
  976. }
  977. //停机状态,延时处理
  978. if(MC_TorqueProcess_Param.MotorStopLock_Flag == SET)
  979. {
  980. torqueRankBak = 0;
  981. torqueAppDecStep = 6;//84;
  982. if(MC_TorqueProcess_Param.TorqueRefEnd < 10)
  983. {
  984. MC_TorqueProcess_Param.TorqueRefEnd = 0;
  985. //停机处理
  986. MC_MotorStop(&MC_StarFlag);
  987. p_MC_CalParam.Foc_Flag = RESET;
  988. }
  989. else
  990. {
  991. MC_TorqueProcess_Param.TorqueRefEnd -= 7;
  992. MC_MotorStar(&MC_StarFlag);
  993. }
  994. torqueAppLag = MC_TorqueProcess_Param.TorqueRefEnd;
  995. }
  996. else
  997. {
  998. if(flagCandence==1)
  999. {
  1000. flagCandence = 0;
  1001. torqueRank = torqueDataFromCadence; //根据踏频磁环信号采样力矩值
  1002. if(torqueRank > torqueRankLast) //比较本次与上次的值
  1003. {
  1004. if(flagUpDown==0)
  1005. {
  1006. //最低点
  1007. }
  1008. if(torqueRank > torqueRankBak) //与当前使用的值比较
  1009. {
  1010. torqueRankBak = torqueRank;
  1011. candenceCount = 0;
  1012. torqueAppDecStep = 1;
  1013. torqueByCandenceDec = 0;
  1014. }
  1015. flagUpDown=1;// 力矩上升标志
  1016. }
  1017. else if(torqueRank < torqueRankLast)
  1018. {
  1019. if(flagUpDown==1)
  1020. {
  1021. //最高点
  1022. torqueRankBak = torqueRankLast;
  1023. candenceCount = 0;
  1024. torqueAppDecStep = 1;//14;
  1025. /*备份力矩最高点的踏频*/
  1026. candenceBakAtTop = Taping_activeFlted;
  1027. torqueByCandenceDec = 0;
  1028. }
  1029. flagUpDown=0; //降
  1030. }
  1031. torqueRankLast = torqueRank; //记录上一次的值
  1032. if(candenceCount > 60) //没有力矩信号, 踏频信号超过360度,加大降速值,清零给定
  1033. {
  1034. torqueRankBak = 0;
  1035. torqueAppDecStep = 6;//84;
  1036. }
  1037. else if(candenceCount > 35) //超过210度,没有力矩信号
  1038. {
  1039. torqueRankBak = 0;
  1040. torqueAppDecStep = 2;
  1041. if((Taping_activeFlted < (candenceBakAtTop - 3)) && (tmpFlted<100))
  1042. {
  1043. torqueAppDecStep = 6;
  1044. }
  1045. }
  1046. else // (candenceCount < 35)
  1047. {
  1048. /*踏频降低,且力矩低*/
  1049. if((Taping_activeFlted < (candenceBakAtTop - 3)) && (tmpFlted<100))
  1050. {
  1051. torqueByCandenceDec +=3;
  1052. torqueAppDecStep = 6;
  1053. }
  1054. }
  1055. torqueApp2 = torqueRankBak - torqueByCandenceDec;
  1056. //torqueApp2 = torqueRankBak;
  1057. torqueApp2 = torqueApp2 > 0 ? torqueApp2 : 0;
  1058. //转换力矩值为力矩控制量
  1059. torqueApp = torqueApp2 * TORQUE_TMP ;
  1060. }/*if(flagCandence==1)*/
  1061. if(GearSt == 0x33)
  1062. {
  1063. torqueAppRatio = ((int32_t)torqueApp * torqueApp / (MC_AssisParam.Gear_SMART.i_Sport_TH));
  1064. torqueAppRatio = (int16_t)((int32_t)torqueAppRatio * MC_ConfigParam1.UserAdjParam_SMART.Assist_K_GAIN / 100); //上位机助力比增益调节
  1065. //给定上限
  1066. torqueAppRatio = (torqueAppRatio > MC_AssisParam.Gear_SMART.Upper_Iq) ? MC_AssisParam.Gear_SMART.Upper_Iq : torqueAppRatio;
  1067. //限流参数设置
  1068. CurrentLimitSet = (uint32_t)(MC_AssisParam.Gear_SMART.CurrentMax_K * MC_ConfigParam1.CurrentLimit * 1000 >> 17) * MC_CadenceLimit_K;
  1069. CurrentLimitPresent = MC_DataSet_Linear_Process(CurrentLimitSet, CurrentLimitPresent, 5 ,1);
  1070. PID_IMax.hLower_Limit_Output = -(MC_AssisParam.Gear_SMART.Upper_Iq); //Lower Limit for Output limitation
  1071. PID_IMax.hUpper_Limit_Output = 0; //Upper Limit for Output limitation
  1072. PID_IMax.wLower_Limit_Integral = -(MC_AssisParam.Gear_SMART.Upper_Iq << 10); // 放大1024
  1073. PID_IMax.wUpper_Limit_Integral = 0; // 放大1024
  1074. }
  1075. else
  1076. {
  1077. switch( GearSt & 0x0F )
  1078. {
  1079. case 1:
  1080. //torqueAppRatio = torqueApp * 0.5;
  1081. //torqueAppRatio = (int16_t)((uint32_t)(torqueApp * Coefficient_GainCal( 51 ,410, 1300, torqueApp)) >> 10 ); //0.4
  1082. torqueAppRatio = (int16_t)((uint32_t)(torqueApp * Coefficient_GainCal( 51 ,MC_AssisParam.Gear_ECO.Gain_K, 1300, torqueApp)) >> 10 ); //0.4
  1083. torqueAppRatio = (int16_t)((int32_t)torqueAppRatio * MC_ConfigParam1.UserAdjParam_ECO.Assist_K_GAIN / 100); //上位机助力比增益调节
  1084. //给定上限
  1085. torqueAppRatio = (torqueAppRatio > MC_AssisParam.Gear_ECO.Upper_Iq) ? MC_AssisParam.Gear_ECO.Upper_Iq : torqueAppRatio;
  1086. //限流参数设置
  1087. CurrentLimitSet = (uint32_t)(MC_AssisParam.Gear_ECO.CurrentMax_K * MC_ConfigParam1.CurrentLimit * 1000 >> 17) * 100;
  1088. CurrentLimitPresent = MC_DataSet_Linear_Process(CurrentLimitSet, CurrentLimitPresent, 5 ,1);
  1089. PID_IMax.hLower_Limit_Output = -(MC_AssisParam.Gear_ECO.Upper_Iq); //Lower Limit for Output limitation
  1090. PID_IMax.hUpper_Limit_Output = 0; //Upper Limit for Output limitation
  1091. PID_IMax.wLower_Limit_Integral = -(MC_AssisParam.Gear_ECO.Upper_Iq << 10); // 放大1024
  1092. PID_IMax.wUpper_Limit_Integral = 0; // 放大1024
  1093. break;
  1094. case 2:
  1095. //torqueAppRatio = torqueApp * 0.8;
  1096. //torqueAppRatio = (int16_t)((uint32_t)(torqueApp * Coefficient_GainCal( 102 ,717, 1300, torqueApp)) >> 10 ); //0.7
  1097. torqueAppRatio = (int16_t)((uint32_t)(torqueApp * Coefficient_GainCal( 102 ,MC_AssisParam.Gear_NORM.Gain_K, 1300, torqueApp)) >> 10 ); //0.7
  1098. torqueAppRatio = (int16_t)((int32_t)torqueAppRatio * MC_ConfigParam1.UserAdjParam_NORM.Assist_K_GAIN / 100); //上位机助力比增益调节
  1099. //给定上限
  1100. torqueAppRatio = (torqueAppRatio > MC_AssisParam.Gear_NORM.Upper_Iq) ? MC_AssisParam.Gear_NORM.Upper_Iq : torqueAppRatio;
  1101. //限流参数设置
  1102. CurrentLimitSet = (uint32_t)(MC_AssisParam.Gear_NORM.CurrentMax_K * MC_ConfigParam1.CurrentLimit * 1000 >> 17) * 100;
  1103. CurrentLimitPresent = MC_DataSet_Linear_Process(CurrentLimitSet, CurrentLimitPresent, 5 ,1);
  1104. PID_IMax.hLower_Limit_Output = -(MC_AssisParam.Gear_NORM.Upper_Iq); //Lower Limit for Output limitation
  1105. PID_IMax.hUpper_Limit_Output = 0; //Upper Limit for Output limitation
  1106. PID_IMax.wLower_Limit_Integral = -(MC_AssisParam.Gear_NORM.Upper_Iq << 10); // 放大1024
  1107. PID_IMax.wUpper_Limit_Integral = 0; // 放大1024
  1108. break;
  1109. case 3:
  1110. //torqueAppRatio = torqueApp * 1.0;
  1111. //torqueAppRatio = (int16_t)((uint32_t)(torqueApp * Coefficient_GainCal( 204 ,1024, 1300, torqueApp)) >> 10 ); //1.0
  1112. torqueAppRatio = (int16_t)((uint32_t)(torqueApp * Coefficient_GainCal( 204 , MC_AssisParam.Gear_SPORT.Gain_K, 1300, torqueApp)) >> 10 ); //1.0
  1113. torqueAppRatio = (int16_t)((int32_t)torqueAppRatio * MC_ConfigParam1.UserAdjParam_SPORT.Assist_K_GAIN / 100); //上位机助力比增益调节
  1114. //给定上限
  1115. torqueAppRatio = (torqueAppRatio > MC_AssisParam.Gear_SPORT.Upper_Iq) ? MC_AssisParam.Gear_SPORT.Upper_Iq : torqueAppRatio;;
  1116. //限流参数设置
  1117. CurrentLimitSet = (uint32_t)(MC_AssisParam.Gear_SPORT.CurrentMax_K * MC_ConfigParam1.CurrentLimit * 1000 >> 17) * MC_CadenceLimit_K;
  1118. CurrentLimitPresent = MC_DataSet_Linear_Process(CurrentLimitSet, CurrentLimitPresent, 5 ,1);
  1119. PID_IMax.hLower_Limit_Output = -(MC_AssisParam.Gear_SPORT.Upper_Iq); //Lower Limit for Output limitation
  1120. PID_IMax.hUpper_Limit_Output = 0; //Upper Limit for Output limitation
  1121. PID_IMax.wLower_Limit_Integral = -(MC_AssisParam.Gear_SPORT.Upper_Iq << 10); // 放大1024
  1122. PID_IMax.wUpper_Limit_Integral = 0; // 放大1024
  1123. break;
  1124. case 4:
  1125. //torqueAppRatio = torqueApp * 1.5;
  1126. //torqueAppRatio = (int16_t)((uint32_t)(torqueApp * Coefficient_GainCal( 307 ,1536, 1300, torqueApp)) >> 10 ); // 1.5
  1127. torqueAppRatio = (int16_t)((uint32_t)(torqueApp * Coefficient_GainCal( 307 ,MC_AssisParam.Gear_TURBO.Gain_K, 1300, torqueApp)) >> 10 ); // 1.5
  1128. torqueAppRatio = (int16_t)((int32_t)torqueAppRatio * MC_ConfigParam1.UserAdjParam_TURBO.Assist_K_GAIN / 100); //上位机助力比增益调节
  1129. //给定上限
  1130. torqueAppRatio = (torqueAppRatio > MC_AssisParam.Gear_TURBO.Upper_Iq) ? MC_AssisParam.Gear_TURBO.Upper_Iq : torqueAppRatio;
  1131. //限流参数设置
  1132. CurrentLimitSet = (uint32_t)(MC_AssisParam.Gear_TURBO.CurrentMax_K * MC_ConfigParam1.CurrentLimit * 1000 >> 17) * MC_CadenceLimit_K;
  1133. CurrentLimitPresent = MC_DataSet_Linear_Process(CurrentLimitSet, CurrentLimitPresent, 5 ,1);
  1134. PID_IMax.hLower_Limit_Output = -(MC_AssisParam.Gear_TURBO.Upper_Iq); //Lower Limit for Output limitation
  1135. PID_IMax.hUpper_Limit_Output = 0; //Upper Limit for Output limitation
  1136. PID_IMax.wLower_Limit_Integral = -(MC_AssisParam.Gear_TURBO.Upper_Iq << 10); // 放大1024
  1137. PID_IMax.wUpper_Limit_Integral = 0; // 放大1024
  1138. break;
  1139. default:break;
  1140. }
  1141. }
  1142. //给定上限
  1143. torqueAppRatio = (torqueAppRatio > MC_AssisParam.Gear_TURBO.Upper_Iq) ? MC_AssisParam.Gear_TURBO.Upper_Iq : torqueAppRatio;
  1144. // torqueAppRatio = torqueAppRatio < 2100 ? torqueAppRatio : 2100;
  1145. //随车速调节助力比
  1146. if(MC_ConfigParam1.StarModel == MC_StarMode_DYNAMIC)
  1147. {
  1148. torqueAppRatio = (uint16_t)((uint32_t)(torqueAppRatio * Function_Linear_3Stage(90, -2, MC_ConfigParam1.SpeedLimit * 10, 160, MC_RunInfo.BikeSpeed)) >> 10);
  1149. }
  1150. else if(MC_ConfigParam1.StarModel == MC_StarMode_SOFT)
  1151. {
  1152. torqueAppRatio = (uint16_t)((uint32_t)(torqueAppRatio * Function_Linear_3Stage(120, -3, MC_ConfigParam1.SpeedLimit * 10, 160, MC_RunInfo.BikeSpeed)) >> 10);
  1153. }
  1154. else
  1155. {
  1156. torqueAppRatio = (uint16_t)((uint32_t)(torqueAppRatio * Function_Linear_3Stage(MC_ConfigParam1.SpeedLimit * 10, 0, MC_ConfigParam1.SpeedLimit * 10, 160, MC_RunInfo.BikeSpeed)) >> 10);
  1157. }
  1158. //限流计算
  1159. // IqRefByInPower = PID_Regulator(CurrentLimitPresent / 100, (MC_RunInfo.BusCurrent >> 7), &PID_IMax);
  1160. //更新周期改为1ms
  1161. if(timeFlag_2ms >= 1 )
  1162. {
  1163. timeFlag_2ms = 0;
  1164. if(torqueAppRatio > torqueAppLag)
  1165. {
  1166. torqueAppLag += torqueAppAccStep;
  1167. if(torqueAppLag > torqueAppRatio)
  1168. {
  1169. torqueAppLag = torqueAppRatio;
  1170. }
  1171. }
  1172. else if(torqueAppRatio < torqueAppLag)
  1173. {
  1174. torqueAppLag -= torqueAppDecStep;
  1175. if(torqueAppLag < torqueAppRatio)
  1176. {
  1177. torqueAppLag = torqueAppRatio;
  1178. }
  1179. }
  1180. }
  1181. // MC_TorqueProcess_Param.TorqueRefEnd = torqueAppLag + IqRefByInPower ;
  1182. MC_TorqueProcess_Param.TorqueRefEnd = torqueAppLag;
  1183. MC_TorqueProcess_Param.TorqueRefEnd = (MC_TorqueProcess_Param.TorqueRefEnd < 6) ? 6 : MC_TorqueProcess_Param.TorqueRefEnd;
  1184. //限速点处理
  1185. if(MC_RunInfo.BikeSpeed > ((MC_ConfigParam1.SpeedLimit * 2690) >> 8)) //限速值*1.05
  1186. {
  1187. MC_TorqueProcess_Param.MotorStopLock_Flag = SET;
  1188. MC_TorqueProcess_Param.TorqueRefEnd = 0;
  1189. //停机处理
  1190. MC_MotorStop(&MC_StarFlag);
  1191. }
  1192. #if 0 //低于限速点启动电机
  1193. else if(MC_RunInfo.BikeSpeed < ((MC_ConfigParam.SpeedLimit) * 10))
  1194. {
  1195. MC_MotorStar(&MC_StarFlag);
  1196. }
  1197. #elif 1 //低于断电点即启动电机
  1198. else
  1199. {
  1200. MC_MotorStar(&MC_StarFlag);
  1201. }
  1202. #endif
  1203. }
  1204. #if 1
  1205. static uint16_t K_ByVoltage_Set_Old = 1024;
  1206. uint16_t K_ByVoltage_Set;
  1207. static uint16_t K_ByVoltage_Result;
  1208. uint16_t K_ByTemperature_Set;
  1209. static uint16_t K_ByTemperature_Result;
  1210. //根据电压调节输出
  1211. K_ByVoltage_Set = MC_Cal_K_ByVoltage(MC_RunInfo.BusVoltage, MC_MotorParam.Rate_Voltage, K_ByVoltage_Set_Old);//根据母线电压计算衰减比例,递减
  1212. K_ByVoltage_Set_Old = K_ByVoltage_Set;
  1213. K_ByVoltage_Result = MC_DataSet_Linear_Process(K_ByVoltage_Set, K_ByVoltage_Result, 1, 1); //设定值与给定值线性处理
  1214. //根据温度调节输出
  1215. K_ByTemperature_Set = MC_Cal_K_ByTemperature(MC_RunInfo.T_Coil, MC_ConfigParam1.TempTH_Alarm); //根据温度计算衰减比例
  1216. K_ByTemperature_Result = MC_DataSet_Linear_Process(K_ByTemperature_Set, K_ByTemperature_Result, 1, 1); //设定值与给定值线性处理
  1217. #else
  1218. uint16_t K_ByVoltage_Result = 1024;
  1219. uint16_t K_ByTemperature_Result = 1024;
  1220. #endif
  1221. //限流计算
  1222. IqRefByInPower = PID_Regulator(CurrentLimitPresent / 100, (MC_RunInfo.BusCurrent >> 7), &PID_IMax);
  1223. Torque_Ref_Temp = ((int32_t)MC_TorqueProcess_Param.TorqueRefEnd * K_ByVoltage_Result) >> 10;
  1224. Torque_Ref_Temp = (Torque_Ref_Temp * K_ByTemperature_Result) >> 10;
  1225. Torque_Ref_Temp = (Torque_Ref_Temp + IqRefByInPower) >> 1;
  1226. p_MC_CalParam.Ref_Torque = (int16_t)Torque_Ref_Temp;
  1227. p_MC_CalParam.Foc_Flag = SET;
  1228. p_MC_CalParam.AssistRunMode = MC_AssistRunMode_TORQUE;
  1229. return (p_MC_CalParam);
  1230. }
  1231. /******************************全局函数定义*****************************/
  1232. //传感器初始化
  1233. void MC_SensorInit(void)
  1234. {
  1235. //霍尔传感器IO设置
  1236. HallSensor_GPIO_Init();
  1237. //霍尔电角度初始化
  1238. HallSensorAngle_Init();
  1239. //踏频传感器IO设置
  1240. CadenceSensor_GPIO_Init();
  1241. //速度传感器IO设置
  1242. SpeedSensor_GPIO_Init();
  1243. //刹车信号和Gear信号检测IO设置
  1244. KeyInitial();
  1245. //力矩传感器零点初值
  1246. TorqueOffSetDefaultData_Init(&TorqueOffSetData, ADC1_Result[ADC1_RANK_TORQUE_SENSOR]);
  1247. //指拨零点初值
  1248. GasSensorOffSet_Init(&GasSensor_OffSet, ADC1_Result[ADC1_RANK_GAS]);
  1249. //ICM20600初始化
  1250. ICM20600_initialize();
  1251. if(ICM20600_OK_Flag == TRUE)
  1252. {
  1253. ICM20600_coefficientinitialize(RANGE_250_DPS, RANGE_2G, &ICM20600Sensor);
  1254. }
  1255. }
  1256. //MC控制初始化
  1257. void MC_Init(void)
  1258. {
  1259. //PID参数初始化
  1260. PID_Init(MC_ConfigParam1.SerialNum);
  1261. //助力参数初始化
  1262. UpdateGearParam(MC_ConfigParam1.SerialNum);
  1263. //三相电流零点校准
  1264. SVPWM_3ShuntCurrentReadingCalibration(&MC_ErrorCode);
  1265. //母线电流零点校准
  1266. CurrentReadingCalibration(&MC_ErrorCode);
  1267. //力矩传感器零点值处理
  1268. TorqueOffSetData_Process(&TorqueOffSetData, ADC1_Result[ADC1_RANK_TORQUE_SENSOR]);//145ms
  1269. //智能档位初始化处理
  1270. if(MC_ConfigParam1.NoPBU_Flag == MC_SUPPORT_ENABLE)
  1271. {
  1272. MC_ControlCode.GearSt = MC_GearSt_SMART;
  1273. Update_MC_ControlCode_Back();
  1274. }
  1275. //12V驱动电源初始化
  1276. Power12V_Driver_Init();
  1277. //打开12V驱动电源
  1278. Power12V_Driver_Process(SET);
  1279. }
  1280. //MC控制参数初始化
  1281. void MC_ControlParam_Init(void)
  1282. {
  1283. //清除推行模式初始变量
  1284. MC_WalkProcess_Param.IsEnterFlag = FALSE;
  1285. MC_WalkProcess_Param.MotorSpeedSetBigin = 0;
  1286. //清除力矩模式初始变量
  1287. MC_TorqueProcess_Param.MotorStopLock_Flag = SET;
  1288. MC_TorqueProcess_Param.TorqueApp = 0;
  1289. MC_TorqueProcess_Param.TorqueRef = 0;
  1290. MC_TorqueProcess_Param.TorqueRefEnd = 0;
  1291. //全局运算变量归零
  1292. IqFdbFlt =0;
  1293. IdFdbFlt = 0;
  1294. VoltSquareFlt = 0;
  1295. UqVoltFlt = 0;
  1296. UdVoltFlt = 0;
  1297. //PDI积分清零
  1298. PID_Flux_InitStructure.wIntegral = 0;
  1299. PID_Torque_InitStructure.wIntegral = 0;
  1300. PID_Weak_InitStructure.wIntegral = 0;
  1301. PID_IMax.wIntegral = 0;
  1302. PID_MotorSpd.wIntegral = 0;
  1303. PID_ConstantPower.wIntegral = 0;
  1304. }
  1305. //控制参数输入值计算
  1306. void MC_CalParam_Cal(MC_WorkMode_Struct_t p_MC_WorkMode, \
  1307. ADC_SensorData_Struct_t p_ADC_SensorData, \
  1308. MC_GearSt_Struct_t GearSt, \
  1309. TrueOrFalse_Flag_Struct_t Break_Flag, \
  1310. TrueOrFalse_Flag_Struct_t GearSensor_Flag, \
  1311. MC_CalParam_Struct_t* p_MC_CalParam)
  1312. {
  1313. MC_AssistRunMode_Struct_t MC_AssistRunMode_Temp;
  1314. static FlagStatus MC_AssistRunMode_ShiftFlag = RESET; //电机助力模式切换标志
  1315. //根据指拨信号、助力档位指令、刹车信号判断助力模式
  1316. 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
  1317. //发生助力模式切换时,清空变量
  1318. if(MC_AssistRunMode_Temp != p_MC_CalParam->AssistRunMode)
  1319. {
  1320. if(MC_AssistRunMode_ShiftFlag == RESET)
  1321. {
  1322. MC_AssistRunMode_Temp = MC_AssistRunMode_INVALID;
  1323. MC_AssistRunMode_ShiftFlag = SET;
  1324. SpeedSetMiddle = 0; //指拨模式,清零速度中间量
  1325. SpdMotorDivWheelFlt = 0;
  1326. }
  1327. }
  1328. //助力模式处理
  1329. switch(MC_AssistRunMode_Temp)
  1330. {
  1331. //指拨模式
  1332. case MC_AssistRunMode_GAS:
  1333. {
  1334. //计算FOC控制输入
  1335. *p_MC_CalParam = MC_AssistRunMode_Gas_Process(p_ADC_SensorData.GasSensor, (MC_GearSt_Struct_t)(GearSt & 0x0F));
  1336. //助力模式切换标志复位
  1337. MC_AssistRunMode_ShiftFlag = RESET;
  1338. break;
  1339. }
  1340. //推行模式
  1341. case MC_AssistRunMode_WALK:
  1342. {
  1343. //计算FOC控制输入
  1344. if(MC_WalkProcess_Param.IsEnterFlag == FALSE)
  1345. {
  1346. MC_WalkProcess_Param.MotorSpeedSetBigin = (uint32_t)MC_RunInfo.MotorSpeed << 5;
  1347. MC_WalkProcess_Param.IsEnterFlag = TRUE;
  1348. }
  1349. *p_MC_CalParam = MC_AssistRunMode_Walk_Process(p_MC_WorkMode);
  1350. //助力模式切换标志复位
  1351. MC_AssistRunMode_ShiftFlag = RESET;
  1352. break;
  1353. }
  1354. //踏频模式
  1355. case MC_AssistRunMode_CADENCE:
  1356. {
  1357. //计算FOC控制输入
  1358. *p_MC_CalParam = MC_AssistRunMode_Cadence_Process(GearSt);
  1359. //助力模式切换标志复位
  1360. MC_AssistRunMode_ShiftFlag = RESET;
  1361. break;
  1362. }
  1363. //力矩模式
  1364. case MC_AssistRunMode_TORQUE:
  1365. {
  1366. //计算FOC控制输入
  1367. //*p_MC_CalParam = MC_AssistRunMode_Torque_Process(p_ADC_SensorData.TorqueSensor, GearSt);
  1368. *p_MC_CalParam = MC_AssistRunMode_Torque_compensation(p_ADC_SensorData.TorqueSensor, GearSt);
  1369. //助力模式切换标志复位
  1370. MC_AssistRunMode_ShiftFlag = RESET;
  1371. break;
  1372. }
  1373. //空闲模式或存在故障
  1374. case MC_AssistRunMode_INVALID: default:
  1375. {
  1376. //停机处理
  1377. MC_MotorStop(&MC_StarFlag);
  1378. //更新母线电流零点值
  1379. CurrentReadingCalibration(&MC_ErrorCode);
  1380. //控制计算值初始化为默认值
  1381. p_MC_CalParam->AssistRunMode = MC_AssistRunMode_INVALID;
  1382. p_MC_CalParam->Foc_Flag = RESET;
  1383. p_MC_CalParam->Ref_Torque = 0;
  1384. p_MC_CalParam->Ref_Speed = 0;
  1385. break;
  1386. }
  1387. }
  1388. }
  1389. void MC_MotorStop(FlagStatus* StarFlag)
  1390. {
  1391. //关闭PWM输出
  1392. Pwm_Timer_Stop();
  1393. //FOC运算停止
  1394. FOC_Disable();
  1395. //控制参数归零
  1396. MC_ControlParam_Init();
  1397. //电机启动标志复位
  1398. *StarFlag = RESET;
  1399. }
  1400. void MC_MotorStar(FlagStatus* StarFlag)
  1401. {
  1402. if(*StarFlag == RESET)
  1403. {
  1404. //开启PWM输出
  1405. Enable_Pwm_Output();
  1406. //霍尔电角度初始化
  1407. HallSensorAngle_Init();
  1408. //FOC运算启动
  1409. FOC_Enable();
  1410. //电机启动标志置位
  1411. *StarFlag = SET;
  1412. }
  1413. }
  1414. /*
  1415. 指拨模式计算速比,计算费时,在主循环调用
  1416. */
  1417. void SpdProportion_calculate(void)
  1418. {
  1419. if(SpdProportion_CAL_flag==1)
  1420. {
  1421. SpdProportion_StandardDeviation = Standard_deviation_aver(SpdProportion_buff, 50, &test_SpdProportionAver);
  1422. test_StandardDeviation = (int32_t)(SpdProportion_StandardDeviation );
  1423. SpdProportion_CAL_flag = 0;
  1424. /*更新速比*/
  1425. if(test_StandardDeviation < 20)
  1426. {
  1427. SpdProportion = test_SpdProportionAver;
  1428. }
  1429. }
  1430. }