PMSM_FOC_Core.c 47 KB

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