motor_control.c 59 KB

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