PMSM_FOC_Core.c 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496
  1. #include "arm_math.h"
  2. #include "PMSM_FOC_Core.h"
  3. #include "foc/foc_config.h"
  4. #include "foc/mc_config.h"
  5. #include "foc/motor/motor_param.h"
  6. #include "foc/core/e_ctrl.h"
  7. #include "foc/core/etcs.h"
  8. #include "math/fix_math.h"
  9. #include "math/fast_math.h"
  10. #include "foc/motor/current.h"
  11. #include "foc/motor/motor.h"
  12. #include "foc/core/svpwm.h"
  13. #include "foc/core/thro_torque.h"
  14. #include "foc/core/foc_observer.h"
  15. #include "foc/core/F_Calc.h"
  16. #include "foc/samples.h"
  17. #include "foc/limit.h"
  18. #include "foc/mc_error.h"
  19. #include "app/nv_storage.h"
  20. #include "bsp/bsp_driver.h"
  21. #include "libs/logger.h"
  22. #include "math/fir.h"
  23. #define _DEBUG(fmt, args...) sys_debug(fmt, ##args)
  24. PMSM_FOC_Ctrl gFoc_Ctrl;
  25. static bool g_focinit = false;
  26. static u32 PMSM_FOC_Debug_Task(void *p);
  27. static __INLINE void RevPark(DQ_t *dq, float angle, AB_t *alpha_beta) {
  28. float c,s;
  29. #if 0
  30. SinCos_Lut(angle, &s, &c);
  31. #else
  32. s = gFoc_Ctrl.out.sin;
  33. c = gFoc_Ctrl.out.cos;
  34. #endif
  35. alpha_beta->a = dq->d * c - dq->q * s;
  36. alpha_beta->b = dq->d * s + dq->q * c;
  37. }
  38. static __INLINE void RevClark(AB_t *alpha_beta, float *ABC){
  39. ABC[0] = alpha_beta->a;
  40. ABC[1] = -alpha_beta->a * 0.5f + alpha_beta->b * SQRT3_BY_2;
  41. ABC[2] = -alpha_beta->a * 0.5f - alpha_beta->b * SQRT3_BY_2;
  42. }
  43. static __INLINE void Clark(float A, float B, float C, AB_t *alpha_beta){
  44. alpha_beta->a = A;
  45. alpha_beta->b = ONE_BY_SQRT3 * (B - C);
  46. }
  47. static __INLINE void Park(AB_t *alpha_beta, float angle, DQ_t *dq) {
  48. float c,s;
  49. #if 0
  50. SinCos_Lut(angle, &s, &c);
  51. #else
  52. s = gFoc_Ctrl.out.sin;
  53. c = gFoc_Ctrl.out.cos;
  54. #endif
  55. dq->d = alpha_beta->a * c + alpha_beta->b * s;
  56. dq->q = -alpha_beta->a * s + alpha_beta->b * c;
  57. }
  58. void PMSM_FOC_ABC2Dq(float a, float b, float c, float *d, float *q) {
  59. AB_t ab;
  60. DQ_t dq;
  61. Clark(a, b, c, &ab);
  62. Park(&ab, 0, &dq);
  63. *d = dq.d;
  64. *q = dq.q;
  65. }
  66. #if 0
  67. #define VD_PRIO_HIGH
  68. static __INLINE float Circle_Limitation(DQ_t *vdq, float vDC, float module, DQ_t *out) {
  69. float sq_vdq = vdq->d * vdq->d + vdq->q * vdq->q;
  70. float vDC_m = vDC * module * SQRT3_BY_2;
  71. float sq_vDC = vDC_m * vDC_m;
  72. if (sq_vdq > sq_vDC) {
  73. #ifdef VD_PRIO_HIGH
  74. out->d = vdq->d;
  75. out->q = sqrtf(sq_vDC - out->d*out->d);
  76. #else
  77. float r = sqrtf(sq_vDC / sq_vdq);
  78. out->d = vdq->d * r;
  79. out->q = vdq->q * r;
  80. #endif
  81. }else {
  82. out->d = vdq->d;
  83. out->q = vdq->q;
  84. }
  85. return sqrtf(sq_vdq/sq_vDC);
  86. }
  87. #endif
  88. static __INLINE void FOC_Set_DqRamp(dq_Rctrl *c, float target, int time) {
  89. float cp = c->s_Cp;
  90. c->s_FinalTgt = target;
  91. c->s_Step = (c->s_FinalTgt - cp) / (float)time;
  92. }
  93. static __INLINE float FOC_Get_DqRamp(dq_Rctrl *c) {
  94. if (++c->n_StepCount == c->n_CtrlCount) {
  95. c->s_Cp += c->s_Step;
  96. if (c->s_Step < 0) {
  97. if (c->s_Cp < c->s_FinalTgt) {
  98. c->s_Cp = c->s_FinalTgt;
  99. }
  100. }else {
  101. if (c->s_Cp > c->s_FinalTgt) {
  102. c->s_Cp = c->s_FinalTgt;
  103. }
  104. }
  105. c->n_StepCount = 0;
  106. }
  107. return c->s_Cp;
  108. }
  109. static __INLINE void FOC_DqRamp_init(dq_Rctrl *c, int count) {
  110. c->n_CtrlCount = count;
  111. c->n_StepCount = 0;
  112. c->s_Cp = 0;
  113. c->s_FinalTgt = 0;
  114. c->s_Step = 0;
  115. }
  116. static __INLINE void FOC_Set_iDqRamp(dq_Rctrl *c, float target) {
  117. FOC_Set_DqRamp(c, target, (/*CONFIG_IDQ_CTRL_TS/CONFIG_SPD_CTRL_TS - 1*/CURRENT_LOOP_RAMP_COUNT));
  118. }
  119. static __INLINE void FOC_Set_vDqRamp(dq_Rctrl *c, float target) {
  120. FOC_Set_DqRamp(c, target, (CONFIG_FOC_VDQ_RAMP_FINAL_TIME/1000*((CONFIG_IDQ_CTRL_TS/CONFIG_FOC_VDQ_RAMP_TS))));
  121. }
  122. static void PMSM_FOC_Reset_PID(void) {
  123. PI_Controller_Reset(&gFoc_Ctrl.pi_id, 0);
  124. PI_Controller_Reset(&gFoc_Ctrl.pi_iq, 0);
  125. PI_Controller_Reset(&gFoc_Ctrl.pi_lock, 0);
  126. PI_Controller_Reset(&gFoc_Ctrl.pi_power, 0);
  127. #ifdef CONFIG_SPEED_LADRC
  128. ladrc_reset(&gFoc_Ctrl.vel_lim_adrc, 0, 0);
  129. ladrc_reset(&gFoc_Ctrl.vel_adrc, 0, 0);
  130. #else
  131. PI_Controller_Reset(&gFoc_Ctrl.pi_vel, 0);
  132. PI_Controller_Reset(&gFoc_Ctrl.pi_vel_lim, 0);
  133. #endif
  134. }
  135. static void PMSM_FOC_Conf_PID(void) {
  136. float slow_ctrl_ts = (1.0f/(float)CONFIG_SPD_CTRL_TS);
  137. gFoc_Ctrl.pi_id.kp = mc_conf()->c.pid[PID_ID_ID].kp;
  138. gFoc_Ctrl.pi_id.ki = mc_conf()->c.pid[PID_ID_ID].ki;
  139. gFoc_Ctrl.pi_id.kd = mc_conf()->c.pid[PID_ID_ID].kd;
  140. gFoc_Ctrl.pi_id.DT = (1.0f/(float)CONFIG_IDQ_CTRL_TS);
  141. gFoc_Ctrl.pi_iq.kp = mc_conf()->c.pid[PID_IQ_ID].kp;
  142. gFoc_Ctrl.pi_iq.ki = mc_conf()->c.pid[PID_IQ_ID].ki;
  143. gFoc_Ctrl.pi_iq.kd = mc_conf()->c.pid[PID_IQ_ID].kd;
  144. gFoc_Ctrl.pi_iq.DT = (1.0f/(float)CONFIG_IDQ_CTRL_TS);
  145. gFoc_Ctrl.pi_power.kp = mc_conf()->c.pid[PID_IDCLim_ID].kp;
  146. gFoc_Ctrl.pi_power.ki = mc_conf()->c.pid[PID_IDCLim_ID].ki;
  147. gFoc_Ctrl.pi_power.kd = mc_conf()->c.pid[PID_IDCLim_ID].kd;
  148. gFoc_Ctrl.pi_power.DT = slow_ctrl_ts;
  149. gFoc_Ctrl.pi_lock.kp = mc_conf()->c.pid[PID_AutoHold_ID].kp;
  150. gFoc_Ctrl.pi_lock.ki = mc_conf()->c.pid[PID_AutoHold_ID].ki;
  151. gFoc_Ctrl.pi_lock.kd = mc_conf()->c.pid[PID_AutoHold_ID].kd;
  152. gFoc_Ctrl.pi_lock.DT = slow_ctrl_ts;
  153. #ifdef CONFIG_SPEED_LADRC
  154. ladrc_init(&gFoc_Ctrl.vel_lim_adrc, slow_ctrl_ts, nv_get_foc_params()->f_adrc_vel_lim_Wo, nv_get_foc_params()->f_adrc_vel_lim_Wcv, nv_get_foc_params()->f_adrc_vel_lim_B0);
  155. ladrc_init(&gFoc_Ctrl.vel_adrc, slow_ctrl_ts, nv_get_foc_params()->f_adrc_vel_Wo, nv_get_foc_params()->f_adrc_vel_Wcv, nv_get_foc_params()->f_adrc_vel_B0);
  156. #else
  157. gFoc_Ctrl.pi_vel_lim.kp = mc_conf()->c.pid[PID_VelLim_ID].kp;
  158. gFoc_Ctrl.pi_vel_lim.ki = mc_conf()->c.pid[PID_VelLim_ID].ki;
  159. gFoc_Ctrl.pi_vel_lim.kd = mc_conf()->c.pid[PID_VelLim_ID].kd;
  160. gFoc_Ctrl.pi_vel_lim.DT = slow_ctrl_ts;
  161. gFoc_Ctrl.pi_vel.kp = mc_conf()->c.pid[PID_Vel_ID].kp;
  162. gFoc_Ctrl.pi_vel.ki = mc_conf()->c.pid[PID_Vel_ID].ki;
  163. gFoc_Ctrl.pi_vel.kd = mc_conf()->c.pid[PID_Vel_ID].kd;
  164. gFoc_Ctrl.pi_vel.DT = slow_ctrl_ts;
  165. #endif
  166. }
  167. static void PMSM_FOC_UserInit(void) {
  168. memset(&gFoc_Ctrl.userLim, 0, sizeof(gFoc_Ctrl.userLim));
  169. gFoc_Ctrl.userLim.s_iDCLim = min(mc_conf()->c.max_idc, gFoc_Ctrl.hwLim.s_iDCMax);
  170. gFoc_Ctrl.userLim.s_motRPMLim = min(mc_conf()->c.max_rpm, gFoc_Ctrl.hwLim.s_motRPMMax);
  171. gFoc_Ctrl.userLim.s_torqueLim = mc_conf()->c.max_torque;//MAX_TORQUE;
  172. gFoc_Ctrl.userLim.s_PhaseCurrLim = min(mc_conf()->c.max_phase_curr, gFoc_Ctrl.hwLim.s_PhaseCurrMax);
  173. gFoc_Ctrl.userLim.s_vDCMaxLim = mc_conf()->c.max_dc_vol;
  174. gFoc_Ctrl.userLim.s_vDCMinLim = mc_conf()->c.min_dc_vol;
  175. gFoc_Ctrl.userLim.s_iDCeBrkLim = 0xFF;
  176. gFoc_Ctrl.userLim.s_PhaseVoleBrkLim = gFoc_Ctrl.hwLim.s_PhaseVolMax;
  177. }
  178. void PMSM_FOC_RT_LimInit(void) {
  179. gFoc_Ctrl.protLim.s_iDCLim = HW_LIMIT_NONE;
  180. gFoc_Ctrl.protLim.s_TorqueLim = HW_LIMIT_NONE;
  181. eRamp_init_target2(&gFoc_Ctrl.rtLim.rpmLimRamp, gFoc_Ctrl.userLim.s_motRPMLim, CONFIG_LIMIT_RAMP_TIME);
  182. eRamp_init_target2(&gFoc_Ctrl.rtLim.torqueLimRamp, gFoc_Ctrl.userLim.s_torqueLim, CONFIG_LIMIT_RAMP_TIME);
  183. eRamp_init_target2(&gFoc_Ctrl.rtLim.DCCurrLimRamp, gFoc_Ctrl.userLim.s_iDCLim, CONFIG_LIMIT_RAMP_TIME);
  184. }
  185. void PMSM_FOC_CoreInit(void) {
  186. PMSM_FOC_Conf_PID();
  187. memset(&gFoc_Ctrl.in, 0, sizeof(FOC_InP));
  188. memset(&gFoc_Ctrl.out, 0, sizeof(FOC_OutP));
  189. gFoc_Ctrl.hwLim.s_iDCMax = CONFIG_HW_MAX_DC_CURRENT;
  190. gFoc_Ctrl.hwLim.s_motRPMMax = CONFIG_HW_MAX_MOTOR_RPM;
  191. gFoc_Ctrl.hwLim.s_PhaseCurrMax = CONFIG_HW_MAX_PHASE_CURR;
  192. gFoc_Ctrl.hwLim.s_PhaseVolMax = CONFIG_HW_MAX_PHASE_VOL;
  193. gFoc_Ctrl.hwLim.s_vDCMax = CONFIG_HW_MAX_DC_VOLTAGE;
  194. gFoc_Ctrl.hwLim.s_torqueMax = mc_conf()->m.max_torque; //电机的最大扭矩
  195. gFoc_Ctrl.hwLim.s_FWDCurrMax = mc_conf()->m.max_fw_id; //电池能支持的最大弱磁电流
  196. if (!g_focinit) {
  197. PMSM_FOC_UserInit();
  198. PMSM_FOC_RT_LimInit();
  199. shark_task_create(PMSM_FOC_Debug_Task, NULL);
  200. g_focinit = true;
  201. //_DEBUG("User Limit:\n");
  202. //_DEBUG("dc %f, rpm %f, torque %f, phase %f, vDCmax %f, vDCmin %f, ebrk %f\n", gFoc_Ctrl.userLim.s_iDCLim, gFoc_Ctrl.userLim.s_motRPMLim, gFoc_Ctrl.userLim.s_torqueLim,
  203. // gFoc_Ctrl.userLim.s_PhaseCurrLim, gFoc_Ctrl.userLim.s_vDCMaxLim, gFoc_Ctrl.userLim.s_vDCMinLim, gFoc_Ctrl.userLim.s_TorqueBrkLim);
  204. //_DEBUG("Hw Limit:\n");
  205. //_DEBUG("dc %f, rpm %f, torque %f, phase %f\n", gFoc_Ctrl.hwLim.s_iDCMax, gFoc_Ctrl.hwLim.s_motRPMMax, gFoc_Ctrl.hwLim.s_torqueMax, gFoc_Ctrl.hwLim.s_PhaseCurrMax);
  206. }
  207. gFoc_Ctrl.userLim.s_TorqueBrkLim = mc_get_ebrk_torque();
  208. gFoc_Ctrl.params.n_modulation = CONFIG_SVM_MODULATION;//SVM_Modulation;
  209. gFoc_Ctrl.params.n_poles = mc_conf()->m.poles;//MOTOR_POLES;
  210. gFoc_Ctrl.params.lq = mc_conf()->m.lq;
  211. gFoc_Ctrl.params.ld = mc_conf()->m.lq;
  212. gFoc_Ctrl.params.flux = mc_conf()->m.flux;
  213. gFoc_Ctrl.in.s_manualAngle = INVALID_ANGLE;
  214. gFoc_Ctrl.in.s_dqAngle = INVALID_ANGLE;
  215. gFoc_Ctrl.in.s_vDC = sample_vbus_raw();
  216. gFoc_Ctrl.in.s_angleLast = INVALID_ANGLE;
  217. gFoc_Ctrl.out.n_RunMode = CTRL_MODE_OPEN;
  218. gFoc_Ctrl.out.f_vdqRation = 0;
  219. eRamp_init_target2(&gFoc_Ctrl.in.cruiseRpmRamp, 0, CONFIG_CRUISE_RAMP_TIME);
  220. FOC_DqRamp_init(&gFoc_Ctrl.idq_ctl[0], 1);
  221. FOC_DqRamp_init(&gFoc_Ctrl.idq_ctl[1], 1);
  222. FOC_DqRamp_init(&gFoc_Ctrl.vdq_ctl[0], (CONFIG_FOC_VDQ_RAMP_TS));
  223. FOC_DqRamp_init(&gFoc_Ctrl.vdq_ctl[1], (CONFIG_FOC_VDQ_RAMP_TS));
  224. PMSM_FOC_Reset_PID();
  225. foc_observer_init();
  226. gFoc_Ctrl.plot_type = Plot_None;
  227. }
  228. /* 通过三相电流重构母线电流,和单电阻采样正好相反,原理一致 */
  229. static __INLINE void PMSM_FOC_Calc_iDC_Fast(void) {
  230. #if 0
  231. float deadtime = (float)(NS_2_TCLK(PWM_DEAD_TIME_NS + HW_DEAD_TIME_NS))/(float)FOC_PWM_Half_Period;
  232. float duty_pu[3];
  233. duty_pu[0] = (float)gFoc_Ctrl.out.n_Duty[0] / (float)FOC_PWM_Half_Period;
  234. duty_pu[1] = (float)gFoc_Ctrl.out.n_Duty[1] / (float)FOC_PWM_Half_Period;
  235. duty_pu[2] = (float)gFoc_Ctrl.out.n_Duty[2] / (float)FOC_PWM_Half_Period;
  236. float *iABC = gFoc_Ctrl.in.s_iABC;
  237. float iDC;
  238. if ((duty_pu[0] >= duty_pu[1]) && (duty_pu[1] >= duty_pu[2])) {
  239. iDC = iABC[0] * MAX(duty_pu[0] - duty_pu[1] - deadtime, 0) + (iABC[0] + iABC[1]) * MAX(duty_pu[1] - duty_pu[2] - deadtime, 0);
  240. if (iABC[0] < 0) {
  241. iDC = iDC + iABC[0] * deadtime;
  242. }
  243. if (iABC[1] > 0) {
  244. iDC = iDC + iABC[0] * deadtime;
  245. }else {
  246. iDC = iDC + (iABC[0] + iABC[1]) * deadtime;
  247. }
  248. if (iABC[2] > 0) {
  249. iDC = iDC + (iABC[0] + iABC[1]) * deadtime;
  250. }
  251. }else if ((duty_pu[0] >= duty_pu[2]) && (duty_pu[2] >= duty_pu[1])) {
  252. iDC = iABC[0] * MAX(duty_pu[0] - duty_pu[2] - deadtime, 0) + (iABC[0] + iABC[2]) * MAX(duty_pu[2] - duty_pu[1] - deadtime, 0);
  253. if (iABC[0] < 0) {
  254. iDC = iDC + iABC[0] * deadtime;
  255. }
  256. if (iABC[2] > 0) {
  257. iDC = iDC + iABC[0] * deadtime;
  258. }else {
  259. iDC = iDC + (iABC[0] + iABC[2]) * deadtime;
  260. }
  261. if (iABC[1] > 0) {
  262. iDC = iDC + (iABC[0] + iABC[2]) * deadtime;
  263. }
  264. }else if ((duty_pu[1] >= duty_pu[0]) && (duty_pu[0] >= duty_pu[2])) {
  265. iDC = iABC[1] * MAX(duty_pu[1] - duty_pu[0] - deadtime, 0) + (iABC[1] + iABC[0]) * MAX(duty_pu[0] - duty_pu[2] - deadtime, 0);
  266. if (iABC[1] < 0) {
  267. iDC = iDC + iABC[1] * deadtime;
  268. }
  269. if (iABC[0] > 0) {
  270. iDC = iDC + iABC[1] * deadtime;
  271. }else {
  272. iDC = iDC + (iABC[1] + iABC[0]) * deadtime;
  273. }
  274. if (iABC[2] > 0) {
  275. iDC = iDC + (iABC[1] + iABC[0]) * deadtime;
  276. }
  277. }else if ((duty_pu[1] >= duty_pu[2]) && (duty_pu[2] >= duty_pu[0])) {
  278. iDC = iABC[1] * MAX(duty_pu[1] - duty_pu[2] - deadtime, 0) + (iABC[1] + iABC[2]) * MAX(duty_pu[2] - duty_pu[0] - deadtime, 0);
  279. if (iABC[1] < 0) {
  280. iDC = iDC + iABC[1] * deadtime;
  281. }
  282. if (iABC[2] > 0) {
  283. iDC = iDC + iABC[1] * deadtime;
  284. }else {
  285. iDC = iDC + (iABC[1] + iABC[2]) * deadtime;
  286. }
  287. if (iABC[0] > 0) {
  288. iDC = iDC + (iABC[1] + iABC[2]) * deadtime;
  289. }
  290. }else if ((duty_pu[2] >= duty_pu[0]) && (duty_pu[0] >= duty_pu[1])) {
  291. iDC = iABC[2] * MAX(duty_pu[2] - duty_pu[0] - deadtime, 0) + (iABC[2] + iABC[0]) * MAX(duty_pu[0] - duty_pu[1] - deadtime, 0);
  292. if (iABC[2] < 0) {
  293. iDC = iDC + iABC[2] * deadtime;
  294. }
  295. if (iABC[0] > 0) {
  296. iDC = iDC + iABC[2] * deadtime;
  297. }else {
  298. iDC = iDC + (iABC[2] + iABC[0]) * deadtime;
  299. }
  300. if (iABC[1] > 0) {
  301. iDC = iDC + (iABC[2] + iABC[0]) * deadtime;
  302. }
  303. }else { // duty_pu[2] >= duty_pu[1] && duty_pu[1] >= duty_pu[0]
  304. iDC = iABC[2] * MAX(duty_pu[2] - duty_pu[1] - deadtime, 0) + (iABC[2] + iABC[1]) * MAX(duty_pu[1] - duty_pu[0] - deadtime, 0);
  305. if (iABC[2] < 0) {
  306. iDC = iDC + iABC[2] * deadtime;
  307. }
  308. if (iABC[1] > 0) {
  309. iDC = iDC + iABC[2] * deadtime;
  310. }else {
  311. iDC = iDC + (iABC[2] + iABC[1]) * deadtime;
  312. }
  313. if (iABC[0] > 0) {
  314. iDC = iDC + (iABC[2] + iABC[1]) * deadtime;
  315. }
  316. }
  317. LowPass_Filter(gFoc_Ctrl.out.s_CalciDC2, iDC, 0.005f);
  318. #endif
  319. }
  320. #define CONFIG_PEAK_CNT 3 //计算经过的电周期内的最大值(peak 峰值)
  321. #define CONFIG_PHASE_UNBALANCE_THROLD 4.0F
  322. #define CONFIG_PHASE_UNBALANCE_R 0.1F
  323. static float phase_unbalance_r = 0.0f;
  324. static float phase_a_max, phase_b_max, phase_c_max;
  325. static u32 phase_unbalance_cnt;
  326. static __INLINE void PMSM_FOC_Phase_Unbalance(void) {
  327. static u32 _cycle_cnt = 0, _last_mod_cnt = 0;
  328. static float a_max = 0, b_max = 0, c_max = 0;
  329. static u32 _unbalance_cnt = 0;
  330. static u32 _unbalance_time = 0;
  331. float lowpass = gFoc_Ctrl.in.s_motVelRadusPers * FOC_CTRL_US / 2.0f;
  332. if (lowpass > 1.0f) {
  333. lowpass = 1.0f;
  334. }
  335. LowPass_Filter(gFoc_Ctrl.in.s_iABCFilter[0], gFoc_Ctrl.in.s_iABC[0], lowpass);
  336. LowPass_Filter(gFoc_Ctrl.in.s_iABCFilter[1], gFoc_Ctrl.in.s_iABC[1], lowpass);
  337. gFoc_Ctrl.in.s_iABCFilter[2] = -(gFoc_Ctrl.in.s_iABCFilter[0] + gFoc_Ctrl.in.s_iABCFilter[1]);
  338. if ((gFoc_Ctrl.in.s_angleLast == INVALID_ANGLE) || (gFoc_Ctrl.in.s_motVelRadusPers < 100)) {
  339. gFoc_Ctrl.in.s_angleLast = gFoc_Ctrl.in.s_motAngle;
  340. a_max = b_max = c_max = 0;
  341. _unbalance_cnt = 0;
  342. _unbalance_time = get_tick_ms();
  343. _cycle_cnt = 0;
  344. _last_mod_cnt = 0;
  345. phase_unbalance_r = 0;
  346. return;
  347. }
  348. float delta_angle = gFoc_Ctrl.in.s_motAngle - gFoc_Ctrl.in.s_angleLast;
  349. if (delta_angle > 200 || delta_angle < -200) { //one cycle
  350. _cycle_cnt ++;
  351. }
  352. gFoc_Ctrl.in.s_angleLast = gFoc_Ctrl.in.s_motAngle;
  353. u32 mod_cnt = _cycle_cnt % CONFIG_PEAK_CNT;
  354. bool trigger = false;
  355. if ((mod_cnt == 0) && (_last_mod_cnt != mod_cnt)) {
  356. trigger = true;
  357. }
  358. _last_mod_cnt = mod_cnt;
  359. a_max = MAX(a_max, gFoc_Ctrl.in.s_iABCFilter[0] * (2.2f));
  360. b_max = MAX(b_max, gFoc_Ctrl.in.s_iABCFilter[1] * (2.2f));
  361. c_max = MAX(c_max, gFoc_Ctrl.in.s_iABCFilter[2] * (2.2f));
  362. if (trigger) { //经过CONFIG_PEAK_CNT个周期,已经得到peak值
  363. float i_min = 1000.0f, i_max = 0;
  364. if (a_max > i_max) {
  365. i_max = a_max;
  366. }
  367. if (a_max < i_min) {
  368. i_min = a_max;
  369. }
  370. if (b_max > i_max) {
  371. i_max = b_max;
  372. }
  373. if (b_max < i_min) {
  374. i_min = b_max;
  375. }
  376. if (c_max > i_max) {
  377. i_max = c_max;
  378. }
  379. if (c_max < i_min) {
  380. i_min = c_max;
  381. }
  382. float unbalance_r = (i_max - i_min - CONFIG_PHASE_UNBALANCE_THROLD)/(i_max + 1e-8f);
  383. if (unbalance_r >= CONFIG_PHASE_UNBALANCE_R) {
  384. if ((_unbalance_cnt++ >= 500) || (get_delta_ms(_unbalance_time) >= 1000*10)) {
  385. if (mc_set_critical_error(FOC_CRIT_PHASE_UNBalance_Err)) {
  386. mc_crit_err_add(FOC_CRIT_PHASE_UNBalance_Err, (s16)i_max, (s16)i_min);
  387. }
  388. }
  389. }else {
  390. _unbalance_cnt = 0;
  391. _unbalance_time = get_tick_ms();
  392. }
  393. phase_unbalance_r = unbalance_r;
  394. phase_a_max = a_max;
  395. phase_b_max = b_max;
  396. phase_c_max = c_max;
  397. phase_unbalance_cnt = _unbalance_cnt;
  398. a_max = b_max = c_max = 0;
  399. }
  400. }
  401. /* 死区补偿 */
  402. static __INLINE void PMSM_FOC_DeadTime_Compensate(s32 PWM_Half_Period) {
  403. #ifdef CONFIG_START_LINE_DTC_CURRENT
  404. float deadTime = (float)CONFIG_HW_DeadTime/2.0f;
  405. s32 dutyDTCA = 0;
  406. s32 dutyDTCB = 0;
  407. s32 dutyDTCC = 0;
  408. float r, delta;
  409. float iabs = ABS(gFoc_Ctrl.in.s_iABC_DT[0]);
  410. if (iabs > CONFIG_START_LINE_DTC_CURRENT) {
  411. delta = iabs - CONFIG_START_LINE_DTC_CURRENT;
  412. r = delta / (COMFIG_END_LINE_DTC_CURRENT - CONFIG_START_LINE_DTC_CURRENT);
  413. if (r > 1.0f) {
  414. r = 1.0f;
  415. }
  416. if (gFoc_Ctrl.in.s_iABC_DT[0] < 0) {
  417. r = -r;
  418. }
  419. dutyDTCA = (s32)(r * deadTime);
  420. }
  421. iabs = ABS(gFoc_Ctrl.in.s_iABC_DT[1]);
  422. if (iabs > CONFIG_START_LINE_DTC_CURRENT) {
  423. delta = iabs - CONFIG_START_LINE_DTC_CURRENT;
  424. r = delta / (COMFIG_END_LINE_DTC_CURRENT - CONFIG_START_LINE_DTC_CURRENT);
  425. if (r > 1.0f) {
  426. r = 1.0f;
  427. }
  428. if (gFoc_Ctrl.in.s_iABC_DT[1] < 0) {
  429. r = -r;
  430. }
  431. dutyDTCB = (s32)(r * deadTime);
  432. }
  433. iabs = ABS(gFoc_Ctrl.in.s_iABC_DT[2]);
  434. if (iabs > CONFIG_START_LINE_DTC_CURRENT) {
  435. delta = iabs - CONFIG_START_LINE_DTC_CURRENT;
  436. r = delta / (COMFIG_END_LINE_DTC_CURRENT - CONFIG_START_LINE_DTC_CURRENT);
  437. if (r > 1.0f) {
  438. r = 1.0f;
  439. }
  440. if (gFoc_Ctrl.in.s_iABC_DT[2] < 0) {
  441. r = -r;
  442. }
  443. dutyDTCC = (s32)(r * deadTime);
  444. }
  445. s32 dutyA = (s32)gFoc_Ctrl.out.n_Duty[0] + dutyDTCA;
  446. s32 dutyB = (s32)gFoc_Ctrl.out.n_Duty[1] + dutyDTCB;
  447. s32 dutyC = (s32)gFoc_Ctrl.out.n_Duty[2] + dutyDTCC;
  448. gFoc_Ctrl.out.n_Duty[0] = sclamp(dutyA, 0, PWM_Half_Period);
  449. gFoc_Ctrl.out.n_Duty[1] = sclamp(dutyB, 0, PWM_Half_Period);
  450. gFoc_Ctrl.out.n_Duty[2] = sclamp(dutyC, 0, PWM_Half_Period);
  451. #endif
  452. }
  453. static __INLINE void Phase_Voltage_update(float lowpass) {
  454. float v_ABC[3];
  455. get_uvw_phases_raw(v_ABC);
  456. LowPass_Filter(gFoc_Ctrl.in.s_SamplePhaseV[0], v_ABC[0], lowpass);
  457. LowPass_Filter(gFoc_Ctrl.in.s_SamplePhaseV[1], v_ABC[1], lowpass);
  458. LowPass_Filter(gFoc_Ctrl.in.s_SamplePhaseV[2], v_ABC[2], lowpass);
  459. /* phase voltage = phase-phase voltage / sqrt(3), 1.4是滤波器幅值补偿系数 */
  460. float phase_vAN = (gFoc_Ctrl.in.s_SamplePhaseV[0] - gFoc_Ctrl.in.s_SamplePhaseV[1]) * ONE_BY_SQRT3 * 1.4f;
  461. float phase_vBN = (gFoc_Ctrl.in.s_SamplePhaseV[1] - gFoc_Ctrl.in.s_SamplePhaseV[2]) * ONE_BY_SQRT3 * 1.4f;
  462. float phase_vCN = (gFoc_Ctrl.in.s_SamplePhaseV[2] - gFoc_Ctrl.in.s_SamplePhaseV[0]) * ONE_BY_SQRT3 * 1.4f;
  463. Clark(phase_vAN, phase_vBN, phase_vCN, &gFoc_Ctrl.out.s_SampleAB);
  464. Park(&gFoc_Ctrl.out.s_SampleAB, gFoc_Ctrl.in.s_motAngle, &gFoc_Ctrl.out.s_SamplevDQ);
  465. }
  466. //#define UPDATE_Lq_By_iq /* Q轴电感 通过Iq电流补偿 */
  467. #define CONFIG_Volvec_Delay_Comp /* 电压矢量角度补偿 */
  468. #define CONFIG_Volvec_Delay_Comp_Start_Vel 500 // rpm
  469. static float encoder_angle,obser_angle, obser_vel = 111111;
  470. static __INLINE bool PMSM_FOC_Update_Input(void) {
  471. AB_t iAB;
  472. float *iabc = gFoc_Ctrl.in.s_iABC;
  473. phase_current_get(iabc);
  474. PMSM_FOC_Calc_iDC_Fast();
  475. Clark(iabc[0], iabc[1], iabc[2], &iAB);
  476. foc_observer_update(gFoc_Ctrl.out.s_OutVAB.a * TWO_BY_THREE, gFoc_Ctrl.out.s_OutVAB.b * TWO_BY_THREE, iAB.a, iAB.b);
  477. float enc_angle = motor_encoder_get_angle();
  478. float enc_vel = motor_encoder_get_speed();
  479. if (!foc_observer_diagnostic(enc_angle, enc_vel)){
  480. /* detect encoder angle error, do something here */
  481. if (!foc_observer_sensorless_stable()) {
  482. gFoc_Ctrl.in.s_motVelocity = 0;
  483. return false;
  484. }
  485. if (obser_vel == 111111) {
  486. obser_vel = foc_observer_sensorless_speed();
  487. obser_angle = foc_observer_sensorless_angle();
  488. encoder_angle = enc_angle;
  489. }
  490. enc_angle = foc_observer_sensorless_angle();
  491. enc_vel = foc_observer_sensorless_speed();
  492. }
  493. if (!gFoc_Ctrl.in.b_MTPA_calibrate && (gFoc_Ctrl.in.s_manualAngle != INVALID_ANGLE)) {
  494. gFoc_Ctrl.in.s_motAngle = gFoc_Ctrl.in.s_manualAngle;
  495. }else {
  496. gFoc_Ctrl.in.s_motAngle = enc_angle;
  497. }
  498. gFoc_Ctrl.in.s_motVelocity = enc_vel;
  499. LowPass_Filter(gFoc_Ctrl.in.s_motVelocityFiltered, gFoc_Ctrl.in.s_motVelocity, 0.01f);
  500. gFoc_Ctrl.in.s_motVelRadusPers = gFoc_Ctrl.in.s_motVelocityFiltered / 30.0f * PI * gFoc_Ctrl.params.n_poles;
  501. PMSM_FOC_Phase_Unbalance();
  502. #ifdef CONFIG_DQ_STEP_RESPONSE
  503. gFoc_Ctrl.in.s_motAngle = 0;
  504. #endif
  505. gFoc_Ctrl.in.s_vDC = get_vbus_float();
  506. SinCos_Lut(gFoc_Ctrl.in.s_motAngle, &gFoc_Ctrl.out.sin, &gFoc_Ctrl.out.cos);
  507. Park(&iAB, gFoc_Ctrl.in.s_motAngle, &gFoc_Ctrl.out.s_RealIdq);
  508. float lowpass = gFoc_Ctrl.in.s_motVelRadusPers * FOC_CTRL_US;
  509. float iqLowPass = lowpass * 2.0f;
  510. if (iqLowPass > 1.0f) {
  511. iqLowPass = 1.0f;
  512. }else if (iqLowPass <= 0.0001f) {
  513. iqLowPass = 0.001f;
  514. }
  515. LowPass_Filter(gFoc_Ctrl.out.s_FilterIdq.d, gFoc_Ctrl.out.s_RealIdq.d, iqLowPass);
  516. LowPass_Filter(gFoc_Ctrl.out.s_FilterIdq.q, gFoc_Ctrl.out.s_RealIdq.q, iqLowPass);
  517. /* 使用低通后的dq电流重新变换得到abc电流,给死区补偿使用 */
  518. RevPark(&gFoc_Ctrl.out.s_FilterIdq, gFoc_Ctrl.in.s_motAngle, &iAB);
  519. RevClark(&iAB, gFoc_Ctrl.in.s_iABC_DT);
  520. Phase_Voltage_update(lowpass);
  521. #ifdef CONFIG_START_LINE_DTC_CURRENT
  522. gFoc_Ctrl.out.s_OutVdqDTC.d = 0;
  523. gFoc_Ctrl.out.s_OutVdqDTC.q = 0;
  524. #else
  525. AB_t vAB;
  526. vAB.a = (1.0f / 3.0f) * (2.0f * SIGN(gFoc_Ctrl.in.s_iABC_DT[0]) - SIGN(gFoc_Ctrl.in.s_iABC_DT[1]) - SIGN(gFoc_Ctrl.in.s_iABC_DT[2]));
  527. vAB.b = ONE_BY_SQRT3 * (SIGN(gFoc_Ctrl.in.s_iABC_DT[1]) - SIGN(gFoc_Ctrl.in.s_iABC_DT[2]));
  528. float dtc = ((float)CONFIG_HW_DeadTime/(float)FOC_PWM_Half_Period) * gFoc_Ctrl.in.s_vDC;
  529. vAB.a = vAB.a * dtc;
  530. vAB.b = vAB.b * dtc;
  531. Park(&vAB, gFoc_Ctrl.in.s_motAngle, &gFoc_Ctrl.out.s_OutVdqDTC); //used for vbus current calc
  532. #endif
  533. #ifdef CONFIG_Volvec_Delay_Comp
  534. if (gFoc_Ctrl.in.s_motVelocityFiltered >= CONFIG_Volvec_Delay_Comp_Start_Vel) {
  535. float next_angle = gFoc_Ctrl.in.s_motAngle + gFoc_Ctrl.in.s_motVelRadusPers / PI * 180.0f * (FOC_CTRL_US * 0.8f);
  536. rand_angle(next_angle);
  537. SinCos_Lut(next_angle, &gFoc_Ctrl.out.sin, &gFoc_Ctrl.out.cos);
  538. }
  539. #endif
  540. return true;
  541. }
  542. #ifdef CONFIG_DQ_STEP_RESPONSE
  543. float target_d = 0.0f;
  544. float target_q = 0.0f;
  545. #endif
  546. static u32 PMSM_FOC_Debug_Task(void *p) {
  547. if (gFoc_Ctrl.in.b_motEnable) {
  548. #ifdef CONFIG_DQ_STEP_RESPONSE
  549. if (gFoc_Ctrl.plot_type == Plot_D_Step) {
  550. plot_2data16(FtoS16x10(target_d), FtoS16x10(gFoc_Ctrl.out.s_RealIdq.d));
  551. }if (gFoc_Ctrl.plot_type == Plot_Q_Step) {
  552. plot_2data16(FtoS16x10(target_q), FtoS16x10(gFoc_Ctrl.out.s_RealIdq.q));
  553. }
  554. #else
  555. if (gFoc_Ctrl.plot_type == Plot_D_flow) {
  556. plot_2data16(FtoS16x10(gFoc_Ctrl.idq_ctl[0].s_Cp), FtoS16x10(gFoc_Ctrl.out.s_RealIdq.d));
  557. }else if (gFoc_Ctrl.plot_type == Plot_Q_flow) {
  558. plot_2data16(FtoS16x10(gFoc_Ctrl.idq_ctl[1].s_Cp), FtoS16x10(gFoc_Ctrl.out.s_RealIdq.q));
  559. }else if (gFoc_Ctrl.plot_type == Plot_DQ_Curr) {
  560. plot_3data16(FtoS16x10(gFoc_Ctrl.out.s_RealIdq.d), FtoS16x10(gFoc_Ctrl.out.s_RealIdq.q), FtoS16x10(gFoc_Ctrl.out.s_FilteriDC));
  561. }else if (gFoc_Ctrl.plot_type == Plot_Spd_flow) {
  562. plot_2data16(gFoc_Ctrl.in.s_targetRPM, gFoc_Ctrl.in.s_motVelocity);
  563. }
  564. #endif
  565. }
  566. return 1;
  567. }
  568. static __INLINE float id_feedforward(float eW) {
  569. #ifdef CONFIG_CURRENT_LOOP_DECOUPE
  570. return -(gFoc_Ctrl.params.lq * gFoc_Ctrl.out.s_RealIdq.q * eW);
  571. #else
  572. return 0;
  573. #endif
  574. }
  575. static __INLINE float iq_feedforward(float eW) {
  576. #ifdef CONFIG_CURRENT_LOOP_DECOUPE
  577. return (gFoc_Ctrl.params.ld * gFoc_Ctrl.out.s_RealIdq.d + gFoc_Ctrl.params.flux) * eW;
  578. #else
  579. return 0;
  580. #endif
  581. }
  582. bool PMSM_FOC_Schedule(void) {
  583. gFoc_Ctrl.ctrl_count++;
  584. if (!PMSM_FOC_Update_Input()){
  585. return false;
  586. }
  587. if (gFoc_Ctrl.out.n_RunMode != CTRL_MODE_OPEN) {
  588. float max_Vdc = gFoc_Ctrl.in.s_vDC * CONFIG_SVM_MODULATION;
  589. float max_vd = max_Vdc * SQRT3_BY_2;
  590. /* limiter Vd output for PI controller */
  591. gFoc_Ctrl.pi_id.max = max_vd;
  592. gFoc_Ctrl.pi_id.min = -max_vd;
  593. #ifndef CONFIG_DQ_STEP_RESPONSE
  594. float target_d = FOC_Get_DqRamp(&gFoc_Ctrl.idq_ctl[0]);
  595. #endif
  596. float err = target_d - gFoc_Ctrl.out.s_RealIdq.d;
  597. float id_ff = id_feedforward(gFoc_Ctrl.in.s_motVelRadusPers);
  598. gFoc_Ctrl.in.s_targetVdq.d = PI_Controller_Current(&gFoc_Ctrl.pi_id, err, id_ff);
  599. #ifdef UPDATE_Lq_By_iq
  600. /* update kp&ki from lq for iq PI controller */
  601. float lq = motor_get_lq_from_iq((s16)gFoc_Ctrl.out.s_FilterIdq.q);
  602. LowPass_Filter(gFoc_Ctrl.params.lq, lq, 0.01f);
  603. gFoc_Ctrl.pi_iq.kp = ((float)nv_get_foc_params()->n_currentBand * gFoc_Ctrl.params.lq);
  604. gFoc_Ctrl.pi_iq.ki = (nv_get_motor_params()->r/gFoc_Ctrl.params.lq);
  605. #endif
  606. /* limiter Vq output for PI controller */
  607. float max_vq = sqrtf(SQ(max_vd) - SQ(gFoc_Ctrl.in.s_targetVdq.d));
  608. gFoc_Ctrl.pi_iq.max = max_vq;
  609. gFoc_Ctrl.pi_iq.min = -max_vq;
  610. #ifndef CONFIG_DQ_STEP_RESPONSE
  611. float target_q = FOC_Get_DqRamp(&gFoc_Ctrl.idq_ctl[1]);
  612. #endif
  613. err = target_q - gFoc_Ctrl.out.s_RealIdq.q;
  614. float iq_ff = iq_feedforward(gFoc_Ctrl.in.s_motVelRadusPers);
  615. gFoc_Ctrl.in.s_targetVdq.q = PI_Controller_Current(&gFoc_Ctrl.pi_iq, err, iq_ff);
  616. }else {
  617. float max_Vdc = gFoc_Ctrl.in.s_vDC * CONFIG_SVM_MODULATION;
  618. float max_vd = max_Vdc * SQRT3_BY_2;
  619. float vd_ref = FOC_Get_DqRamp(&gFoc_Ctrl.vdq_ctl[0]);
  620. gFoc_Ctrl.in.s_targetVdq.d = fclamp(vd_ref, -max_vd, max_vd);
  621. float max_vq = sqrtf(SQ(max_vd) - SQ(gFoc_Ctrl.in.s_targetVdq.d));
  622. float vq_ref = FOC_Get_DqRamp(&gFoc_Ctrl.vdq_ctl[1]);
  623. gFoc_Ctrl.in.s_targetVdq.q = fclamp(vq_ref, -max_vq, max_vq);
  624. }
  625. #if 0
  626. gFoc_Ctrl.out.f_vdqRation = Circle_Limitation(&gFoc_Ctrl.in.s_targetVdq, gFoc_Ctrl.in.s_vDC, gFoc_Ctrl.params.n_modulation, &gFoc_Ctrl.out.s_OutVdq);
  627. gFoc_Ctrl.out.s_OutVdq.d *= SQRT3_BY_2;
  628. gFoc_Ctrl.out.s_OutVdq.q *= SQRT3_BY_2;
  629. #else
  630. gFoc_Ctrl.out.s_OutVdq.d = gFoc_Ctrl.in.s_targetVdq.d;
  631. gFoc_Ctrl.out.s_OutVdq.q = gFoc_Ctrl.in.s_targetVdq.q;
  632. #endif
  633. RevPark(&gFoc_Ctrl.out.s_OutVdq, gFoc_Ctrl.in.s_motAngle, &gFoc_Ctrl.out.s_OutVAB);
  634. SVM_Duty_Fix(&gFoc_Ctrl.out.s_OutVAB, gFoc_Ctrl.in.s_vDC, FOC_PWM_Half_Period, &gFoc_Ctrl.out);
  635. PMSM_FOC_DeadTime_Compensate((s32)FOC_PWM_Half_Period);
  636. phase_current_point(&gFoc_Ctrl.out);
  637. pwm_update_duty(gFoc_Ctrl.out.n_Duty[0], gFoc_Ctrl.out.n_Duty[1], gFoc_Ctrl.out.n_Duty[2]);
  638. pwm_update_sample(gFoc_Ctrl.out.n_Sample1, gFoc_Ctrl.out.n_Sample2, gFoc_Ctrl.out.n_CPhases);
  639. return true;
  640. }
  641. void PMSM_FOC_LogDebug(void) {
  642. sys_debug("DC curr %f --- %f, %f\n", gFoc_Ctrl.out.s_CalciDC, gFoc_Ctrl.out.s_FilteriDC, gFoc_Ctrl.out.s_CalciDC2);
  643. sys_debug("%s\n", gFoc_Ctrl.out.empty_load?"NoLoad Running":"Load Runing");
  644. sys_debug("unbalance: %d, %f, %f, %f, %f\n", phase_unbalance_cnt, phase_unbalance_r, phase_a_max, phase_b_max, phase_c_max);
  645. if (obser_vel != 111111) {
  646. sys_debug("AB error: %f,%f,%f\n", obser_angle, encoder_angle, obser_vel);
  647. }
  648. }
  649. /*called in media task */
  650. u8 PMSM_FOC_CtrlMode(void) {
  651. u8 preMode = gFoc_Ctrl.out.n_RunMode;
  652. if (!gFoc_Ctrl.in.b_motEnable) {
  653. gFoc_Ctrl.out.n_RunMode = CTRL_MODE_OPEN;
  654. }else if (gFoc_Ctrl.in.n_ctlMode == CTRL_MODE_OPEN) {
  655. gFoc_Ctrl.out.n_RunMode = CTRL_MODE_OPEN;
  656. }else if (gFoc_Ctrl.in.n_ctlMode == CTRL_MODE_SPD || gFoc_Ctrl.in.b_cruiseEna){
  657. gFoc_Ctrl.out.n_RunMode = CTRL_MODE_SPD;
  658. }else if (gFoc_Ctrl.in.n_ctlMode == CTRL_MODE_CURRENT) {
  659. gFoc_Ctrl.out.n_RunMode = CTRL_MODE_CURRENT;
  660. }else if (gFoc_Ctrl.in.n_ctlMode == CTRL_MODE_EBRAKE) {
  661. gFoc_Ctrl.out.n_RunMode = CTRL_MODE_EBRAKE;
  662. }else {
  663. if (!gFoc_Ctrl.in.b_cruiseEna) {
  664. gFoc_Ctrl.out.n_RunMode = CTRL_MODE_TRQ;
  665. }
  666. }
  667. if (preMode != gFoc_Ctrl.out.n_RunMode) {
  668. if ((preMode == CTRL_MODE_SPD) && (gFoc_Ctrl.out.n_RunMode == CTRL_MODE_TRQ)) {
  669. #ifdef CONFIG_SPEED_LADRC
  670. //ladrc_reset(&gFoc_Ctrl.vel_lim_adrc, gFoc_Ctrl.in.s_motVelocity, gFoc_Ctrl.in.s_targetTorque);
  671. ladrc_copy(&gFoc_Ctrl.vel_lim_adrc, &gFoc_Ctrl.vel_adrc);
  672. #else
  673. PI_Controller_Reset(&gFoc_Ctrl.pi_vel_lim, gFoc_Ctrl.in.s_targetTorque);
  674. #endif
  675. }else if ((preMode == CTRL_MODE_TRQ) && (gFoc_Ctrl.out.n_RunMode == CTRL_MODE_SPD)) {
  676. #ifdef CONFIG_SPEED_LADRC
  677. ladrc_copy(&gFoc_Ctrl.vel_adrc, &gFoc_Ctrl.vel_lim_adrc);
  678. #else
  679. PI_Controller_Reset(&gFoc_Ctrl.pi_vel, gFoc_Ctrl.in.s_targetTorque);
  680. #endif
  681. }else if ((preMode == CTRL_MODE_CURRENT) && (gFoc_Ctrl.out.n_RunMode == CTRL_MODE_TRQ)) {
  682. #ifdef CONFIG_SPEED_LADRC
  683. ladrc_reset(&gFoc_Ctrl.vel_lim_adrc, gFoc_Ctrl.in.s_motVelocity, gFoc_Ctrl.in.s_targetTorque);
  684. #else
  685. PI_Controller_Reset(&gFoc_Ctrl.pi_vel_lim, gFoc_Ctrl.in.s_targetTorque);
  686. #endif
  687. }else if ((preMode != gFoc_Ctrl.out.n_RunMode) && (gFoc_Ctrl.out.n_RunMode == CTRL_MODE_EBRAKE)) {
  688. eCtrl_reset_Torque(gFoc_Ctrl.in.s_targetTorque);
  689. eCtrl_set_TgtTorque(motor_get_ebreak_toruqe(gFoc_Ctrl.in.s_motVelocity));
  690. }else if ((preMode == CTRL_MODE_EBRAKE) && (gFoc_Ctrl.out.n_RunMode == CTRL_MODE_SPD)) {
  691. #ifdef CONFIG_SPEED_LADRC
  692. ladrc_reset(&gFoc_Ctrl.vel_adrc, 0, F_get_air());
  693. #else
  694. PI_Controller_Reset(&gFoc_Ctrl.pi_vel, F_get_air());
  695. #endif
  696. }
  697. }
  698. return gFoc_Ctrl.out.n_RunMode;
  699. }
  700. #define RAMPE_1 CONFIG_RAMP_FIRST_TARGET
  701. #define NORMAL_STEP 3.0F
  702. static void crosszero_step_towards(float *value, float target) {
  703. static float no_cro_step = NORMAL_STEP;
  704. float v_now = *value;
  705. bool cross_zero = false;
  706. float high_ramp_torque = CONFIG_RAMP_SECOND_TARGET;
  707. if (target > 0) {
  708. if (v_now < -RAMPE_1) {
  709. step_towards(value, -RAMPE_1 + 0.001f, NORMAL_STEP);
  710. cross_zero = true;
  711. }else if (v_now >= -RAMPE_1 && v_now <= high_ramp_torque) {
  712. step_towards(value, target, 0.03f);
  713. cross_zero = true;
  714. }
  715. }else if (target == 0) {
  716. if (v_now > high_ramp_torque) {
  717. step_towards(value, high_ramp_torque - 0.001f, NORMAL_STEP);
  718. cross_zero = true;
  719. }else if (v_now >= RAMPE_1 && v_now <= high_ramp_torque) {
  720. step_towards(value, target, 0.03f);
  721. cross_zero = true;
  722. }
  723. }else {
  724. if (v_now > high_ramp_torque) {
  725. step_towards(value, high_ramp_torque - 0.001f, NORMAL_STEP);
  726. cross_zero = true;
  727. }else if (v_now >= -RAMPE_1 && v_now <= high_ramp_torque) {
  728. step_towards(value, target, 0.03f);
  729. cross_zero = true;
  730. }
  731. }
  732. if (!cross_zero) {
  733. step_towards(&no_cro_step, NORMAL_STEP, 0.1f);
  734. step_towards(value, target, no_cro_step);
  735. }else {
  736. no_cro_step = 0.5f;
  737. }
  738. }
  739. /* MPTA, 弱磁, 功率限制,主要是分配DQ轴电流 */
  740. #define CHANGE_MAX_CNT 3
  741. static __INLINE void PMSM_FOC_VelCtrl_Decide(void) {
  742. #if 0
  743. static int change_cnt = 0;
  744. static bool change_done = false;
  745. static u32 change_time = 0xFFFFFFFF;
  746. float f_te = F_get_Te();
  747. float f_accl = F_get_accl();
  748. if (mc_is_epm()) {
  749. change_cnt = 0;
  750. change_time = 0xFFFFFFFF;
  751. change_done = false;
  752. gFoc_Ctrl.out.empty_load = false;
  753. return;
  754. }
  755. if (gFoc_Ctrl.in.s_motVelocity == 0.0f || gFoc_Ctrl.out.n_RunMode == CTRL_MODE_OPEN) {
  756. change_cnt = 0;
  757. change_time = 0xFFFFFFFF;
  758. change_done = false;
  759. gFoc_Ctrl.out.empty_load = false;
  760. return;
  761. }
  762. if (f_te <= 0.0f) {
  763. change_cnt = 0;
  764. change_time = 0xFFFFFFFF;
  765. return;
  766. }
  767. if (change_done) {
  768. /* 误判空转,发现电机给定的N大于空气阻力,说明不是空转 */
  769. if (gFoc_Ctrl.out.empty_load) {
  770. float f_air = F_get_air();
  771. if ((f_accl > 1.0f) && (f_te >= (f_air + f_accl))) {
  772. change_cnt ++;
  773. }else {
  774. change_cnt = 0;
  775. }
  776. if (change_cnt >= 500) {
  777. gFoc_Ctrl.out.empty_load = false;
  778. #ifdef CONFIG_SPEED_LADRC
  779. PMSM_FOC_Change_TrqLoop_Params(nv_get_foc_params()->f_adrc_vel_lim_Wcv, nv_get_foc_params()->f_adrc_vel_lim_B0);
  780. PMSM_FOC_Change_VelLoop_Params(nv_get_foc_params()->f_adrc_vel_Wcv, nv_get_foc_params()->f_adrc_vel_B0);
  781. #endif
  782. }
  783. }
  784. return;
  785. }
  786. if (change_time == 0xFFFFFFFF) {
  787. change_time = get_tick_ms();
  788. }else { //起步3s内检测是否空转
  789. if (get_delta_ms(change_time) > 3000) {
  790. return;
  791. }
  792. }
  793. if ((f_accl > 200.0f) && (f_accl/f_te > 3.0f )) {
  794. change_cnt++;
  795. }else if ((F_get_MotAccl() >= 10.0f) && (f_accl/f_te > 1.2f )) {
  796. change_cnt = CHANGE_MAX_CNT;
  797. }
  798. else {
  799. if ((f_te > 50) && (f_accl > 0) && (f_te > f_accl)) {
  800. change_cnt --;
  801. }else {
  802. change_cnt = 0;
  803. }
  804. }
  805. if (!change_done && (change_cnt >= CHANGE_MAX_CNT)) {
  806. change_done = true;
  807. change_cnt = 0;
  808. gFoc_Ctrl.out.empty_load = change_done;
  809. #ifdef CONFIG_SPEED_LADRC
  810. PMSM_FOC_Change_TrqLoop_Params(CONFIG_LADRC_NOLOAD_Wcv, CONFIG_LADRC_NOLOAD_B0);
  811. PMSM_FOC_Change_VelLoop_Params(CONFIG_LADRC_NOLOAD_Wcv, CONFIG_LADRC_NOLOAD_B0);
  812. #endif
  813. }else if (!change_done && (change_cnt <= -200)) {
  814. change_done = true;
  815. change_cnt = 0;
  816. gFoc_Ctrl.out.empty_load = false;
  817. }
  818. #endif
  819. }
  820. static __INLINE float PMSM_FOC_Limit_iDC(float maxTrq) {
  821. #if 1
  822. gFoc_Ctrl.pi_power.max = maxTrq;
  823. float errRef = eRamp_get_intepolation(&gFoc_Ctrl.rtLim.DCCurrLimRamp) - (gFoc_Ctrl.out.s_FilteriDC);
  824. return PI_Controller_Run(&gFoc_Ctrl.pi_power, errRef);
  825. #else
  826. return maxTrq;
  827. #endif
  828. }
  829. static __INLINE float PMSM_FOC_Limit_Speed(float maxTrq) {
  830. #ifdef CONFIG_SPEED_LADRC
  831. float lim = eRamp_get_intepolation(&gFoc_Ctrl.rtLim.rpmLimRamp);
  832. ladrc_set_range(&gFoc_Ctrl.vel_lim_adrc, 0, maxTrq);
  833. return ladrc_run(&gFoc_Ctrl.vel_lim_adrc, lim, gFoc_Ctrl.in.s_motVelocity);
  834. #else
  835. gFoc_Ctrl.pi_vel_lim.max = maxTrq;
  836. gFoc_Ctrl.pi_vel_lim.min = 0;
  837. float err = eRamp_get_intepolation(&gFoc_Ctrl.rtLim.rpmLimRamp) - gFoc_Ctrl.in.s_motVelocity;
  838. return PI_Controller_RunVel(&gFoc_Ctrl.pi_vel_lim, err);
  839. #endif
  840. }
  841. static __INLINE void PMSM_FOC_idq_Assign(void) {
  842. if (gFoc_Ctrl.out.n_RunMode == CTRL_MODE_CURRENT) {
  843. if (gFoc_Ctrl.in.b_MTPA_calibrate && (gFoc_Ctrl.in.s_dqAngle != INVALID_ANGLE)) {
  844. float s, c;
  845. normal_sincosf(degree_2_pi(gFoc_Ctrl.in.s_dqAngle + 90.0f), &s, &c);
  846. gFoc_Ctrl.in.s_targetIdq.d = gFoc_Ctrl.in.s_targetCurrent * c;
  847. if (gFoc_Ctrl.in.s_targetIdq.d > gFoc_Ctrl.hwLim.s_FWDCurrMax) {
  848. gFoc_Ctrl.in.s_targetIdq.d = gFoc_Ctrl.hwLim.s_FWDCurrMax;
  849. }else if (gFoc_Ctrl.in.s_targetIdq.d < -gFoc_Ctrl.hwLim.s_FWDCurrMax) {
  850. gFoc_Ctrl.in.s_targetIdq.d = -gFoc_Ctrl.hwLim.s_FWDCurrMax;
  851. }
  852. gFoc_Ctrl.in.s_targetIdq.q = sqrtf(SQ(gFoc_Ctrl.in.s_targetCurrent) - SQ(gFoc_Ctrl.in.s_targetIdq.d));
  853. if (s < 0) {
  854. gFoc_Ctrl.in.s_targetIdq.q = -gFoc_Ctrl.in.s_targetIdq.q;
  855. }
  856. }else {
  857. gFoc_Ctrl.in.s_targetIdq.d = 0;
  858. gFoc_Ctrl.in.s_targetIdq.q = gFoc_Ctrl.in.s_targetCurrent;
  859. }
  860. }else if ((gFoc_Ctrl.out.n_RunMode == CTRL_MODE_TRQ) || (gFoc_Ctrl.out.n_RunMode == CTRL_MODE_SPD) ||
  861. (gFoc_Ctrl.out.n_RunMode == CTRL_MODE_EBRAKE)) {
  862. motor_mpta_fw_lookup(gFoc_Ctrl.in.s_motVelocity, gFoc_Ctrl.in.s_targetTorque, &gFoc_Ctrl.in.s_targetIdq);
  863. }
  864. u32 mask = cpu_enter_critical();
  865. FOC_Set_iDqRamp(&gFoc_Ctrl.idq_ctl[0], gFoc_Ctrl.in.s_targetIdq.d);
  866. FOC_Set_iDqRamp(&gFoc_Ctrl.idq_ctl[1], gFoc_Ctrl.in.s_targetIdq.q);
  867. cpu_exit_critical(mask);
  868. }
  869. /*called in media task */
  870. void PMSM_FOC_idqCalc(void) {
  871. if (gFoc_Ctrl.in.b_AutoHold) {
  872. float hold_torque = min(gFoc_Ctrl.protLim.s_TorqueLim, mc_conf()->c.max_autohold_torque);
  873. gFoc_Ctrl.pi_lock.max = hold_torque;
  874. gFoc_Ctrl.pi_lock.min = -hold_torque;
  875. float vel_count = motor_encoder_get_vel_count();
  876. float errRef = 0 - vel_count;
  877. gFoc_Ctrl.in.s_targetTorque = PI_Controller_Run(&gFoc_Ctrl.pi_lock ,errRef);
  878. PMSM_FOC_idq_Assign();
  879. return;
  880. }
  881. if (gFoc_Ctrl.out.n_RunMode == CTRL_MODE_CURRENT) {
  882. gFoc_Ctrl.in.s_targetCurrent = eCtrl_get_RefCurrent();
  883. }else if (gFoc_Ctrl.out.n_RunMode == CTRL_MODE_EBRAKE) {
  884. float maxTrq = eCtrl_get_RefTorque();
  885. if (eCtrl_get_FinalTorque() < 0.0001f && gFoc_Ctrl.in.s_motVelocity < CONFIG_MIN_RPM_EXIT_EBRAKE) {
  886. maxTrq = 0;
  887. }
  888. crosszero_step_towards(&gFoc_Ctrl.in.s_targetTorque, maxTrq);
  889. }else if (gFoc_Ctrl.out.n_RunMode == CTRL_MODE_TRQ) {
  890. float refTorque = min(eCtrl_get_RefTorque(), eRamp_get_intepolation(&gFoc_Ctrl.rtLim.torqueLimRamp));
  891. float maxTrq = PMSM_FOC_Limit_Speed(refTorque);
  892. maxTrq = PMSM_FOC_Limit_iDC(maxTrq);
  893. crosszero_step_towards(&gFoc_Ctrl.in.s_targetTorque, maxTrq);
  894. }else if (gFoc_Ctrl.out.n_RunMode == CTRL_MODE_SPD){
  895. float maxSpeed = eCtrl_get_FinalSpeed();
  896. float refSpeed = eCtrl_get_RefSpeed();
  897. if (gFoc_Ctrl.in.b_cruiseEna) {
  898. maxSpeed = eRamp_get_target(&gFoc_Ctrl.in.cruiseRpmRamp);
  899. refSpeed = eRamp_get_intepolation(&gFoc_Ctrl.in.cruiseRpmRamp);//gFoc_Ctrl.in.s_cruiseRPM;
  900. }
  901. #ifdef CONFIG_SPEED_LADRC
  902. if (maxSpeed >= 0) {
  903. ladrc_set_range(&gFoc_Ctrl.vel_adrc, -CONFIG_MAX_NEG_TORQUE, eRamp_get_intepolation(&gFoc_Ctrl.rtLim.torqueLimRamp));
  904. }else if (maxSpeed < 0) {
  905. ladrc_set_range(&gFoc_Ctrl.vel_adrc, -eRamp_get_intepolation(&gFoc_Ctrl.rtLim.torqueLimRamp), CONFIG_MAX_NEG_TORQUE);
  906. }
  907. if ((maxSpeed == 0) && (gFoc_Ctrl.in.s_motVelocity < CONFIG_MIN_RPM_EXIT_EBRAKE)) {
  908. ladrc_set_range(&gFoc_Ctrl.vel_adrc, 0, 0);
  909. }
  910. gFoc_Ctrl.in.s_targetRPM = refSpeed;
  911. float maxTrq = ladrc_run(&gFoc_Ctrl.vel_adrc, refSpeed, gFoc_Ctrl.in.s_motVelocity);
  912. #else
  913. if (maxSpeed >= 0) {
  914. gFoc_Ctrl.pi_vel.max = eRamp_get_intepolation(&gFoc_Ctrl.rtLim.torqueLimRamp);
  915. gFoc_Ctrl.pi_vel.min = -CONFIG_MAX_NEG_TORQUE;
  916. }else if (maxSpeed < 0) {
  917. gFoc_Ctrl.pi_vel.min = -eRamp_get_intepolation(&gFoc_Ctrl.rtLim.torqueLimRamp);
  918. gFoc_Ctrl.pi_vel.max = CONFIG_MAX_NEG_TORQUE;
  919. }
  920. if ((maxSpeed == 0) && (gFoc_Ctrl.in.s_motVelocity < CONFIG_MIN_RPM_EXIT_EBRAKE)) {
  921. gFoc_Ctrl.pi_vel.max = 0;
  922. gFoc_Ctrl.pi_vel.min = 0; //防止倒转
  923. }
  924. gFoc_Ctrl.in.s_targetRPM = refSpeed;
  925. float errRef = refSpeed - gFoc_Ctrl.in.s_motVelocity;
  926. float maxTrq = PI_Controller_RunVel(&gFoc_Ctrl.pi_vel, errRef);
  927. #endif
  928. maxTrq = PMSM_FOC_Limit_iDC(maxTrq);
  929. crosszero_step_towards(&gFoc_Ctrl.in.s_targetTorque, maxTrq);
  930. }
  931. PMSM_FOC_idq_Assign();
  932. }
  933. u8 PMSM_FOC_RunTime_Limit(void) {
  934. u8 changed = FOC_LIM_NO_CHANGE;
  935. float dc_lim = (float)vbus_under_vol_limit();
  936. float torque_lim = (float)min(mos_temp_high_limit(), motor_temp_high_limit());
  937. if (gFoc_Ctrl.protLim.s_iDCLim != dc_lim || gFoc_Ctrl.protLim.s_TorqueLim != torque_lim) {
  938. if ((dc_lim > gFoc_Ctrl.protLim.s_iDCLim) || (torque_lim > gFoc_Ctrl.protLim.s_TorqueLim)) {
  939. changed = FOC_LIM_CHANGE_H;
  940. }else {
  941. changed = FOC_LIM_CHANGE_L;
  942. }
  943. gFoc_Ctrl.protLim.s_iDCLim = dc_lim;
  944. gFoc_Ctrl.protLim.s_TorqueLim = torque_lim;
  945. }
  946. return changed;
  947. }
  948. bool PMSM_FOC_iDC_is_Limited(void) {
  949. return (gFoc_Ctrl.protLim.s_iDCLim != HW_LIMIT_NONE);
  950. }
  951. bool PMSM_FOC_Torque_is_Limited(void) {
  952. return (gFoc_Ctrl.protLim.s_TorqueLim != HW_LIMIT_NONE);
  953. }
  954. void PMSM_FOC_Slow_Task(void) {
  955. eRamp_running(&gFoc_Ctrl.rtLim.torqueLimRamp);
  956. eRamp_running(&gFoc_Ctrl.rtLim.DCCurrLimRamp);
  957. eRamp_running(&gFoc_Ctrl.rtLim.rpmLimRamp);
  958. eRamp_running(&gFoc_Ctrl.in.cruiseRpmRamp);
  959. PMSM_FOC_VelCtrl_Decide();
  960. PMSM_FOC_idqCalc();
  961. }
  962. void PMSM_FOC_Change_VelLoop_Params(float wcv, float b0) {
  963. #ifdef CONFIG_SPEED_LADRC
  964. ladrc_change_b0(&gFoc_Ctrl.vel_adrc, b0);
  965. ladrc_change_K(&gFoc_Ctrl.vel_adrc, wcv);
  966. #else
  967. PI_Controller_Change_Kpi(&gFoc_Ctrl.pi_vel, wcv, b0);
  968. #endif
  969. }
  970. void PMSM_FOC_Change_TrqLoop_Params(float wcv, float b0) {
  971. #ifdef CONFIG_SPEED_LADRC
  972. ladrc_change_b0(&gFoc_Ctrl.vel_lim_adrc, b0);
  973. ladrc_change_K(&gFoc_Ctrl.vel_lim_adrc, wcv);
  974. #else
  975. PI_Controller_Change_Kpi(&gFoc_Ctrl.pi_vel_lim, wcv, b0);
  976. #endif
  977. }
  978. float PMSM_FOC_Get_Real_dqVector(void) {
  979. if (gFoc_Ctrl.out.s_RealCurrentFiltered == 0) {
  980. gFoc_Ctrl.out.s_RealCurrentFiltered = sqrtf(SQ(gFoc_Ctrl.out.s_FilterIdq.d) + SQ(gFoc_Ctrl.out.s_FilterIdq.q));
  981. }
  982. return gFoc_Ctrl.out.s_RealCurrentFiltered;
  983. }
  984. PMSM_FOC_Ctrl *PMSM_FOC_Get(void) {
  985. return &gFoc_Ctrl;
  986. }
  987. void PMSM_FOC_Start(u8 nCtrlMode) {
  988. if (gFoc_Ctrl.in.b_motEnable) {
  989. return;
  990. }
  991. PMSM_FOC_CoreInit();
  992. eCtrl_Reset();
  993. gFoc_Ctrl.in.n_ctlMode = nCtrlMode;
  994. gFoc_Ctrl.in.b_motEnable = true;
  995. }
  996. void PMSM_FOC_Stop(void) {
  997. if (!gFoc_Ctrl.in.b_motEnable) {
  998. return;
  999. }
  1000. PMSM_FOC_CoreInit();
  1001. gFoc_Ctrl.in.b_motEnable = false;
  1002. }
  1003. bool PMSM_FOC_Is_Start(void) {
  1004. return gFoc_Ctrl.in.b_motEnable;
  1005. }
  1006. void PMSM_FOC_DCCurrLimit(float ibusLimit) {
  1007. if (ibusLimit > gFoc_Ctrl.hwLim.s_iDCMax) {
  1008. ibusLimit = gFoc_Ctrl.hwLim.s_iDCMax;
  1009. }
  1010. if (gFoc_Ctrl.protLim.s_iDCLim != HW_LIMIT_NONE) {
  1011. ibusLimit = min(ibusLimit, gFoc_Ctrl.protLim.s_iDCLim);
  1012. }
  1013. gFoc_Ctrl.userLim.s_iDCLim = ibusLimit;
  1014. if (ABS(gFoc_Ctrl.in.s_motVelocity) <= CONFIG_ZERO_SPEED_RPM){
  1015. eRamp_reset_target(&gFoc_Ctrl.rtLim.DCCurrLimRamp, ibusLimit);
  1016. }else {
  1017. eRamp_set_step_target(&gFoc_Ctrl.rtLim.DCCurrLimRamp, ibusLimit, CONFIG_eCTRL_STEP_TS);
  1018. }
  1019. }
  1020. float PMSM_FOC_GetDCCurrLimit(void) {
  1021. return gFoc_Ctrl.userLim.s_iDCLim;
  1022. }
  1023. void PMSM_FOC_SpeedRampLimit(float speedLimit, float speed) {
  1024. if (speedLimit > gFoc_Ctrl.hwLim.s_motRPMMax) {
  1025. speedLimit = gFoc_Ctrl.hwLim.s_motRPMMax;
  1026. }
  1027. gFoc_Ctrl.userLim.s_motRPMLim = (speedLimit);
  1028. if (ABS(speed) <= CONFIG_ZERO_SPEED_RPM) {
  1029. eRamp_reset_target(&gFoc_Ctrl.rtLim.rpmLimRamp, speedLimit);
  1030. }else {
  1031. eRamp_set_step_target(&gFoc_Ctrl.rtLim.rpmLimRamp, speedLimit, CONFIG_eCTRL_STEP_TS);
  1032. }
  1033. }
  1034. void PMSM_FOC_SpeedLimit(float speedLimit) {
  1035. PMSM_FOC_SpeedRampLimit(speedLimit, gFoc_Ctrl.in.s_motVelocity);
  1036. }
  1037. void PMSM_FOC_SpeedDirectLimit(float limit) {
  1038. PMSM_FOC_SpeedRampLimit(limit, 0);
  1039. }
  1040. float PMSM_FOC_GetSpeedLimit(void) {
  1041. return gFoc_Ctrl.userLim.s_motRPMLim;
  1042. }
  1043. void PMSM_FOC_TorqueLimit(float torqueLimit) {
  1044. if (torqueLimit > gFoc_Ctrl.hwLim.s_torqueMax) {
  1045. torqueLimit = gFoc_Ctrl.hwLim.s_torqueMax;
  1046. }
  1047. if (gFoc_Ctrl.protLim.s_TorqueLim != HW_LIMIT_NONE) {
  1048. torqueLimit = min(torqueLimit, gFoc_Ctrl.protLim.s_TorqueLim);
  1049. }
  1050. gFoc_Ctrl.userLim.s_torqueLim = torqueLimit;
  1051. if (ABS(gFoc_Ctrl.in.s_motVelocity) <= CONFIG_ZERO_SPEED_RPM){
  1052. eRamp_reset_target(&gFoc_Ctrl.rtLim.torqueLimRamp, torqueLimit);
  1053. }else {
  1054. eRamp_set_step_target(&gFoc_Ctrl.rtLim.torqueLimRamp, torqueLimit, CONFIG_eCTRL_STEP_TS);
  1055. }
  1056. }
  1057. float PMSM_FOC_GetTorqueLimit(void) {
  1058. return gFoc_Ctrl.userLim.s_torqueLim;
  1059. }
  1060. void PMSM_FOC_SetEbrkTorque(s16 torque) {
  1061. gFoc_Ctrl.userLim.s_TorqueBrkLim = (float)torque;
  1062. //gFoc_Ctrl.userLim.s_iDCeBrkLim = fclamp(dc_curr, 0, nv_get_foc_params()->s_iDCeBrkLim);
  1063. }
  1064. float PMSM_FOC_GetEbrkTorque(void) {
  1065. if (!foc_observer_is_encoder()) {
  1066. return 0; //无感运行关闭能量回收
  1067. }
  1068. return gFoc_Ctrl.userLim.s_TorqueBrkLim;
  1069. }
  1070. float PMSM_FOC_GetVbusVoltage(void) {
  1071. return gFoc_Ctrl.in.s_vDC;
  1072. }
  1073. float PMSM_FOC_GetVbusCurrent(void) {
  1074. return gFoc_Ctrl.out.s_FilteriDC;
  1075. }
  1076. DQ_t* PMSM_FOC_GetDQCurrent(void) {
  1077. return &gFoc_Ctrl.out.s_RealIdq;
  1078. }
  1079. bool PMSM_FOC_SetCtrlMode(u8 mode) {
  1080. if (mode > CTRL_MODE_EBRAKE) {
  1081. PMSM_FOC_SetErrCode(FOC_Param_Err);
  1082. return false;
  1083. }
  1084. gFoc_Ctrl.in.n_ctlMode = mode;
  1085. return true;
  1086. }
  1087. u8 PMSM_FOC_GetCtrlMode(void) {
  1088. return gFoc_Ctrl.in.n_ctlMode;
  1089. }
  1090. void PMSM_FOC_PhaseCurrLim(float lim) {
  1091. if (lim > gFoc_Ctrl.hwLim.s_PhaseCurrMax) {
  1092. lim = gFoc_Ctrl.hwLim.s_PhaseCurrMax;
  1093. }
  1094. gFoc_Ctrl.userLim.s_PhaseCurrLim = lim;
  1095. }
  1096. void PMSM_FOC_RT_PhaseCurrLim(float lim) {
  1097. if (lim > gFoc_Ctrl.hwLim.s_PhaseCurrMax) {
  1098. lim = gFoc_Ctrl.hwLim.s_PhaseCurrMax;
  1099. }
  1100. eRamp_init_target2(&gFoc_Ctrl.rtLim.torqueLimRamp, lim, CONFIG_LIMIT_RAMP_TIME);
  1101. }
  1102. float PMSM_FOC_GetPhaseCurrLim(void) {
  1103. return gFoc_Ctrl.userLim.s_PhaseCurrLim;
  1104. }
  1105. void PMSM_FOC_SetOpenVdq(float vd, float vq) {
  1106. FOC_Set_vDqRamp(&gFoc_Ctrl.vdq_ctl[0], vd);
  1107. FOC_Set_vDqRamp(&gFoc_Ctrl.vdq_ctl[1], vq);
  1108. }
  1109. void PMSM_FOC_SetOpenVdq_Immediate(float vd, float vq) {
  1110. gFoc_Ctrl.vdq_ctl[0].s_Step = 0;
  1111. gFoc_Ctrl.vdq_ctl[0].s_FinalTgt = vd;
  1112. gFoc_Ctrl.vdq_ctl[0].s_Cp = vd;
  1113. gFoc_Ctrl.vdq_ctl[1].s_Step = 0;
  1114. gFoc_Ctrl.vdq_ctl[1].s_FinalTgt = vq;
  1115. gFoc_Ctrl.vdq_ctl[1].s_Cp = vq;
  1116. }
  1117. bool PMSM_FOC_EnableCruise(bool enable) {
  1118. if (enable != gFoc_Ctrl.in.b_cruiseEna) {
  1119. float motSpd = PMSM_FOC_GetSpeed();
  1120. if (enable && (motSpd < CONFIG_MIN_CRUISE_RPM)) { //
  1121. PMSM_FOC_SetErrCode(FOC_NowAllowed_With_Speed);
  1122. return false;
  1123. }
  1124. eRamp_reset_target(&gFoc_Ctrl.in.cruiseRpmRamp, motSpd);
  1125. gFoc_Ctrl.in.s_cruiseRPM = motSpd;
  1126. gFoc_Ctrl.in.b_cruiseEna = enable;
  1127. }
  1128. return true;
  1129. }
  1130. bool PMSM_FOC_PauseCruise(void) {
  1131. gFoc_Ctrl.in.b_cruiseEna = false;
  1132. return true;
  1133. }
  1134. bool PMSM_FOC_ResumeCruise(void) {
  1135. gFoc_Ctrl.in.b_cruiseEna = true;
  1136. eRamp_init_target2(&gFoc_Ctrl.in.cruiseRpmRamp, PMSM_FOC_GetSpeed(), CONFIG_CRUISE_RAMP_TIME);
  1137. eRamp_set_step_target(&gFoc_Ctrl.in.cruiseRpmRamp, gFoc_Ctrl.in.s_cruiseRPM, CONFIG_eCTRL_STEP_TS);
  1138. return true;
  1139. }
  1140. bool PMSM_FOC_Is_CruiseEnabled(void) {
  1141. return (gFoc_Ctrl.in.b_cruiseEna && (gFoc_Ctrl.out.n_RunMode == CTRL_MODE_SPD));
  1142. }
  1143. bool PMSM_FOC_Set_TgtSpeed(float rpm) {
  1144. if (gFoc_Ctrl.in.b_cruiseEna) {
  1145. return false;
  1146. }
  1147. eCtrl_set_TgtSpeed(min(ABS(rpm), gFoc_Ctrl.userLim.s_motRPMLim)*SIGN(rpm));
  1148. return true;
  1149. }
  1150. bool PMSM_FOC_Set_Current(float is) {
  1151. if (is > gFoc_Ctrl.userLim.s_PhaseCurrLim) {
  1152. is = gFoc_Ctrl.userLim.s_PhaseCurrLim;
  1153. }else if (is < -gFoc_Ctrl.userLim.s_PhaseCurrLim) {
  1154. is = -gFoc_Ctrl.userLim.s_PhaseCurrLim;
  1155. }
  1156. eCtrl_set_TgtCurrent(is);
  1157. return true;
  1158. }
  1159. bool PMSM_FOC_Set_Torque(float trq) {
  1160. if (trq > gFoc_Ctrl.userLim.s_torqueLim) {
  1161. trq = gFoc_Ctrl.userLim.s_torqueLim;
  1162. }else if (trq < -gFoc_Ctrl.userLim.s_torqueLim) {
  1163. trq = -gFoc_Ctrl.userLim.s_torqueLim;
  1164. }
  1165. eCtrl_set_TgtTorque(trq);
  1166. return true;
  1167. }
  1168. void PMSM_FOC_Reset_Torque(void) {
  1169. float real_trq = PMSM_FOC_Get_Real_dqVector();
  1170. eCtrl_reset_Torque(real_trq);
  1171. }
  1172. bool PMSM_FOC_Set_CruiseSpeed(float rpm) {
  1173. if (PMSM_FOC_Is_CruiseEnabled()) {
  1174. if (rpm < CONFIG_MIN_CRUISE_RPM) {
  1175. rpm = CONFIG_MIN_CRUISE_RPM;
  1176. }
  1177. gFoc_Ctrl.in.s_cruiseRPM = min(ABS(rpm), gFoc_Ctrl.userLim.s_motRPMLim)*SIGN(rpm);
  1178. eRamp_set_step_target(&gFoc_Ctrl.in.cruiseRpmRamp, gFoc_Ctrl.in.s_cruiseRPM, CONFIG_eCTRL_STEP_TS);
  1179. return true;
  1180. }
  1181. PMSM_FOC_SetErrCode(FOC_NotCruiseMode);
  1182. return false;
  1183. }
  1184. void PMSM_FOC_MTPA_Calibrate(bool enable) {
  1185. if (enable) {
  1186. gFoc_Ctrl.in.b_MTPA_calibrate = true;
  1187. gFoc_Ctrl.in.s_dqAngle = 0;
  1188. }else {
  1189. gFoc_Ctrl.in.s_dqAngle = INVALID_ANGLE;
  1190. gFoc_Ctrl.in.b_MTPA_calibrate = false;
  1191. }
  1192. }
  1193. void PMSM_FOC_Set_MotAngle(float angle) {
  1194. gFoc_Ctrl.in.s_manualAngle = (angle);
  1195. }
  1196. void PMSM_FOC_Set_Dq_Angle(float angle) {
  1197. gFoc_Ctrl.in.s_dqAngle = (angle);
  1198. }
  1199. void PMSM_FOC_Get_TgtIDQ(DQ_t * dq) {
  1200. dq->d = gFoc_Ctrl.in.s_targetIdq.d;
  1201. dq->q = gFoc_Ctrl.in.s_targetIdq.q;
  1202. }
  1203. float PMSM_FOC_GetSpeed(void) {
  1204. float speed = gFoc_Ctrl.in.s_motVelocity;
  1205. if (!gFoc_Ctrl.in.b_motEnable || foc_observer_is_encoder()) {
  1206. speed = motor_encoder_get_speed();
  1207. }else {
  1208. if (foc_observer_sensorless_stable()) {
  1209. speed = foc_observer_sensorless_speed();
  1210. }else {
  1211. speed = 0;
  1212. }
  1213. }
  1214. return speed;
  1215. }
  1216. void PMSM_FOC_AutoHold(bool lock) {
  1217. if (gFoc_Ctrl.in.b_AutoHold != lock) {
  1218. motor_encoder_lock_pos(lock);
  1219. PI_Controller_Reset(&gFoc_Ctrl.pi_lock, 0);
  1220. if (!lock) {
  1221. float hold_torque = gFoc_Ctrl.in.s_targetTorque * 1.1f;
  1222. if (gFoc_Ctrl.out.n_RunMode == CTRL_MODE_TRQ) {
  1223. #ifdef CONFIG_SPEED_LADRC
  1224. ladrc_reset(&gFoc_Ctrl.vel_lim_adrc, 0, hold_torque);
  1225. #else
  1226. PI_Controller_Reset(&gFoc_Ctrl.pi_vel_lim, hold_torque);
  1227. #endif
  1228. }else if (gFoc_Ctrl.out.n_RunMode == CTRL_MODE_SPD) {
  1229. #ifdef CONFIG_SPEED_LADRC
  1230. ladrc_reset(&gFoc_Ctrl.vel_adrc, 0, hold_torque);
  1231. #else
  1232. PI_Controller_Reset(&gFoc_Ctrl.pi_vel, hold_torque);
  1233. #endif
  1234. }
  1235. etcs_reset_torque(hold_torque);
  1236. gFoc_Ctrl.out.f_autohold_trq = hold_torque;
  1237. }else {
  1238. gFoc_Ctrl.out.f_autohold_trq = 0;
  1239. }
  1240. gFoc_Ctrl.in.b_AutoHold = lock;
  1241. }
  1242. }
  1243. bool PMSM_FOC_AutoHoldding(void) {
  1244. return gFoc_Ctrl.in.b_AutoHold;
  1245. }
  1246. static PI_Controller *_pid(u8 id) {
  1247. PI_Controller *pi = NULL;
  1248. if (id == PID_ID_ID) {
  1249. pi = &gFoc_Ctrl.pi_id;
  1250. }else if (id == PID_IQ_ID) {
  1251. pi = &gFoc_Ctrl.pi_iq;
  1252. }else if (id == PID_VelLim_ID) {
  1253. #ifndef CONFIG_SPEED_LADRC
  1254. pi = &gFoc_Ctrl.pi_vel_lim;
  1255. #endif
  1256. }else if (id == PID_Vel_ID) {
  1257. #ifndef CONFIG_SPEED_LADRC
  1258. pi = &gFoc_Ctrl.pi_vel;
  1259. #endif
  1260. }else if (id == PID_AutoHold_ID) {
  1261. pi = &gFoc_Ctrl.pi_lock;
  1262. }
  1263. return pi;
  1264. }
  1265. void PMSM_FOC_SetPid(u8 id, float kp, float ki, float kd) {
  1266. if (id > PID_Max_ID) {
  1267. return;
  1268. }
  1269. PI_Controller *pi = _pid(id);
  1270. if (pi != NULL) {
  1271. pi->kp = kp;
  1272. pi->ki = ki;
  1273. pi->kd = kd;
  1274. }
  1275. }
  1276. void PMSM_FOC_GetPid(u8 id, float *kp, float *ki, float *kd) {
  1277. if (id > PID_Max_ID) {
  1278. return;
  1279. }
  1280. PI_Controller *pi = _pid(id);
  1281. if (pi != NULL) {
  1282. *kp = pi->kp;
  1283. *ki = pi->ki;
  1284. *kd = pi->kd;
  1285. }
  1286. }
  1287. void PMSM_FOC_SetErrCode(u8 error) {
  1288. if (gFoc_Ctrl.out.n_Error != error) {
  1289. gFoc_Ctrl.out.n_Error = error;
  1290. }
  1291. }
  1292. u8 PMSM_FOC_GetErrCode(void) {
  1293. return gFoc_Ctrl.out.n_Error;
  1294. }
  1295. void PMSM_FOC_Set_PlotType(Plot_t t) {
  1296. gFoc_Ctrl.plot_type = t;
  1297. }
  1298. //获取母线电流和实际输出电流矢量大小
  1299. void PMSM_FOC_Calc_Current(void) {
  1300. float vd = gFoc_Ctrl.out.s_OutVdq.d - gFoc_Ctrl.out.s_OutVdqDTC.d * TWO_BY_THREE;
  1301. float vq = gFoc_Ctrl.out.s_OutVdq.q - gFoc_Ctrl.out.s_OutVdqDTC.q * TWO_BY_THREE;
  1302. float id = gFoc_Ctrl.out.s_FilterIdq.d;
  1303. float iq = gFoc_Ctrl.out.s_FilterIdq.q;
  1304. /*
  1305. 根据公式(等幅值变换,功率不等):
  1306. iDC x vDC = 3/2(iq x vq + id x vd);
  1307. */
  1308. float m_pow = (vd * id + vq * iq);
  1309. float raw_idc = 0.0f;
  1310. float v_dc = get_vbus_float();
  1311. if (v_dc != 0.0f) {
  1312. raw_idc = m_pow / v_dc;
  1313. }
  1314. LowPass_Filter(gFoc_Ctrl.out.s_CalciDC, raw_idc, 0.02f);
  1315. m_pow = (gFoc_Ctrl.out.s_SamplevDQ.d * id + gFoc_Ctrl.out.s_SamplevDQ.q * iq) * 1.5f;
  1316. if (v_dc != 0.0f) {
  1317. raw_idc = m_pow / v_dc;
  1318. }
  1319. LowPass_Filter(gFoc_Ctrl.out.s_CalciDC2, raw_idc, 0.02f);
  1320. #ifdef VBUS_I_CHAN
  1321. raw_idc = get_vbus_current();
  1322. LowPass_Filter(gFoc_Ctrl.out.s_FilteriDC, raw_idc, 0.05f);
  1323. #else
  1324. gFoc_Ctrl.out.s_FilteriDC = gFoc_Ctrl.out.s_CalciDC;
  1325. #endif
  1326. gFoc_Ctrl.out.s_RealCurrentFiltered = sqrtf(SQ(gFoc_Ctrl.out.s_FilterIdq.d) + SQ(gFoc_Ctrl.out.s_FilterIdq.q));
  1327. }
  1328. void PMSM_FOC_Brake(bool brake) {
  1329. gFoc_Ctrl.in.b_eBrake = brake;
  1330. if (gFoc_Ctrl.in.b_eBrake & gFoc_Ctrl.in.b_cruiseEna) {
  1331. gFoc_Ctrl.in.b_cruiseEna = false;
  1332. }
  1333. eCtrl_brake_signal(brake);
  1334. }