PMSM_FOC_Core.c 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469
  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 "math/fix_math.h"
  7. #include "math/fast_math.h"
  8. #include "foc/motor/current.h"
  9. #include "foc/motor/motor.h"
  10. #include "foc/core/svpwm.h"
  11. #include "foc/core/thro_torque.h"
  12. #include "foc/core/foc_observer.h"
  13. #include "foc/core/F_Calc.h"
  14. #include "foc/samples.h"
  15. #include "foc/limit.h"
  16. #include "foc/mc_error.h"
  17. #include "app/nv_storage.h"
  18. #include "bsp/bsp_driver.h"
  19. #include "libs/logger.h"
  20. #include "math/fir.h"
  21. #define _DEBUG(fmt, args...) sys_debug(fmt, ##args)
  22. PMSM_FOC_Ctrl gFoc_Ctrl;
  23. static bool g_focinit = false;
  24. static u32 PMSM_FOC_Debug_Task(void *p);
  25. static __INLINE void RevPark(DQ_t *dq, float angle, AB_t *alpha_beta) {
  26. float c,s;
  27. #if 0
  28. SinCos_Lut(angle, &s, &c);
  29. #else
  30. s = gFoc_Ctrl.out.sin;
  31. c = gFoc_Ctrl.out.cos;
  32. #endif
  33. alpha_beta->a = dq->d * c - dq->q * s;
  34. alpha_beta->b = dq->d * s + dq->q * c;
  35. }
  36. static __INLINE void RevClark(AB_t *alpha_beta, float *ABC){
  37. ABC[0] = alpha_beta->a;
  38. ABC[1] = -alpha_beta->a * 0.5f + alpha_beta->b * SQRT3_BY_2;
  39. ABC[2] = -alpha_beta->a * 0.5f - alpha_beta->b * SQRT3_BY_2;
  40. }
  41. static __INLINE void Clark(float A, float B, float C, AB_t *alpha_beta){
  42. alpha_beta->a = A;
  43. alpha_beta->b = ONE_BY_SQRT3 * (B - C);
  44. }
  45. static __INLINE void Park(AB_t *alpha_beta, float angle, DQ_t *dq) {
  46. float c,s;
  47. #if 0
  48. SinCos_Lut(angle, &s, &c);
  49. #else
  50. s = gFoc_Ctrl.out.sin;
  51. c = gFoc_Ctrl.out.cos;
  52. #endif
  53. dq->d = alpha_beta->a * c + alpha_beta->b * s;
  54. dq->q = -alpha_beta->a * s + alpha_beta->b * c;
  55. }
  56. void PMSM_FOC_ABC2Dq(float a, float b, float c, float *d, float *q) {
  57. AB_t ab;
  58. DQ_t dq;
  59. Clark(a, b, c, &ab);
  60. Park(&ab, 0, &dq);
  61. *d = dq.d;
  62. *q = dq.q;
  63. }
  64. #if 0
  65. #define VD_PRIO_HIGH
  66. static __INLINE float Circle_Limitation(DQ_t *vdq, float vDC, float module, DQ_t *out) {
  67. float sq_vdq = vdq->d * vdq->d + vdq->q * vdq->q;
  68. float vDC_m = vDC * module * SQRT3_BY_2;
  69. float sq_vDC = vDC_m * vDC_m;
  70. if (sq_vdq > sq_vDC) {
  71. #ifdef VD_PRIO_HIGH
  72. out->d = vdq->d;
  73. out->q = sqrtf(sq_vDC - out->d*out->d);
  74. #else
  75. float r = sqrtf(sq_vDC / sq_vdq);
  76. out->d = vdq->d * r;
  77. out->q = vdq->q * r;
  78. #endif
  79. }else {
  80. out->d = vdq->d;
  81. out->q = vdq->q;
  82. }
  83. return sqrtf(sq_vdq/sq_vDC);
  84. }
  85. #endif
  86. static __INLINE void FOC_Set_DqRamp(dq_Rctrl *c, float target, int time) {
  87. float cp = c->s_Cp;
  88. c->s_FinalTgt = target;
  89. c->s_Step = (c->s_FinalTgt - cp) / (float)time;
  90. }
  91. static __INLINE float FOC_Get_DqRamp(dq_Rctrl *c) {
  92. if (++c->n_StepCount == c->n_CtrlCount) {
  93. c->s_Cp += c->s_Step;
  94. if (c->s_Step < 0) {
  95. if (c->s_Cp < c->s_FinalTgt) {
  96. c->s_Cp = c->s_FinalTgt;
  97. }
  98. }else {
  99. if (c->s_Cp > c->s_FinalTgt) {
  100. c->s_Cp = c->s_FinalTgt;
  101. }
  102. }
  103. c->n_StepCount = 0;
  104. }
  105. return c->s_Cp;
  106. }
  107. static __INLINE void FOC_DqRamp_init(dq_Rctrl *c, int count) {
  108. c->n_CtrlCount = count;
  109. c->n_StepCount = 0;
  110. c->s_Cp = 0;
  111. c->s_FinalTgt = 0;
  112. c->s_Step = 0;
  113. }
  114. static __INLINE void FOC_Set_iDqRamp(dq_Rctrl *c, float target) {
  115. FOC_Set_DqRamp(c, target, (/*CONFIG_IDQ_CTRL_TS/CONFIG_SPD_CTRL_TS - 1*/CURRENT_LOOP_RAMP_COUNT));
  116. }
  117. static __INLINE void FOC_Set_vDqRamp(dq_Rctrl *c, float target) {
  118. FOC_Set_DqRamp(c, target, (CONFIG_FOC_VDQ_RAMP_FINAL_TIME/1000*((CONFIG_IDQ_CTRL_TS/CONFIG_FOC_VDQ_RAMP_TS))));
  119. }
  120. static void PMSM_FOC_Reset_PID(void) {
  121. PI_Controller_Reset(&gFoc_Ctrl.pi_id, 0);
  122. PI_Controller_Reset(&gFoc_Ctrl.pi_iq, 0);
  123. PI_Controller_Reset(&gFoc_Ctrl.pi_lock, 0);
  124. PI_Controller_Reset(&gFoc_Ctrl.pi_power, 0);
  125. #ifdef CONFIG_SPEED_LADRC
  126. ladrc_reset(&gFoc_Ctrl.vel_lim_adrc, 0, 0);
  127. ladrc_reset(&gFoc_Ctrl.vel_adrc, 0, 0);
  128. #else
  129. PI_Controller_Reset(&gFoc_Ctrl.pi_vel, 0);
  130. PI_Controller_Reset(&gFoc_Ctrl.pi_vel_lim, 0);
  131. #endif
  132. }
  133. static void PMSM_FOC_Conf_PID(void) {
  134. float slow_ctrl_ts = (1.0f/(float)CONFIG_SPD_CTRL_TS);
  135. gFoc_Ctrl.pi_id.kp = nv_get_foc_params()->pid_conf[PID_D_id].kp;
  136. gFoc_Ctrl.pi_id.ki = nv_get_foc_params()->pid_conf[PID_D_id].ki;
  137. gFoc_Ctrl.pi_id.kd = nv_get_foc_params()->pid_conf[PID_D_id].kd;
  138. gFoc_Ctrl.pi_id.DT = (1.0f/(float)CONFIG_IDQ_CTRL_TS);
  139. gFoc_Ctrl.pi_iq.kp = nv_get_foc_params()->pid_conf[PID_Q_id].kp;
  140. gFoc_Ctrl.pi_iq.ki = nv_get_foc_params()->pid_conf[PID_Q_id].ki;
  141. gFoc_Ctrl.pi_iq.kd = nv_get_foc_params()->pid_conf[PID_Q_id].kd;
  142. gFoc_Ctrl.pi_iq.DT = (1.0f/(float)CONFIG_IDQ_CTRL_TS);
  143. gFoc_Ctrl.pi_power.kp = nv_get_foc_params()->pid_conf[PID_Pow_id].kp;
  144. gFoc_Ctrl.pi_power.ki = nv_get_foc_params()->pid_conf[PID_Pow_id].ki;
  145. gFoc_Ctrl.pi_power.kd = nv_get_foc_params()->pid_conf[PID_Pow_id].kd;
  146. gFoc_Ctrl.pi_power.DT = slow_ctrl_ts;
  147. gFoc_Ctrl.pi_lock.kp = nv_get_foc_params()->pid_conf[PID_Lock_id].kp;
  148. gFoc_Ctrl.pi_lock.ki = nv_get_foc_params()->pid_conf[PID_Lock_id].ki;
  149. gFoc_Ctrl.pi_lock.kd = nv_get_foc_params()->pid_conf[PID_Lock_id].kd;
  150. gFoc_Ctrl.pi_lock.DT = slow_ctrl_ts;
  151. #ifdef CONFIG_SPEED_LADRC
  152. 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);
  153. 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);
  154. #else
  155. gFoc_Ctrl.pi_vel_lim.kp = nv_get_foc_params()->pid_conf[PID_TRQ_id].kp;
  156. gFoc_Ctrl.pi_vel_lim.ki = nv_get_foc_params()->pid_conf[PID_TRQ_id].ki;
  157. gFoc_Ctrl.pi_vel_lim.kd = nv_get_foc_params()->pid_conf[PID_TRQ_id].kd;
  158. gFoc_Ctrl.pi_vel_lim.DT = slow_ctrl_ts;
  159. gFoc_Ctrl.pi_vel.kp = nv_get_foc_params()->pid_conf[PID_Spd_id].kp;
  160. gFoc_Ctrl.pi_vel.ki = nv_get_foc_params()->pid_conf[PID_Spd_id].ki;
  161. gFoc_Ctrl.pi_vel.kd = nv_get_foc_params()->pid_conf[PID_Spd_id].kd;
  162. gFoc_Ctrl.pi_vel.DT = slow_ctrl_ts;
  163. #endif
  164. }
  165. static void PMSM_FOC_UserInit(void) {
  166. memset(&gFoc_Ctrl.userLim, 0, sizeof(gFoc_Ctrl.userLim));
  167. gFoc_Ctrl.userLim.s_iDCLim = min(nv_get_foc_params()->s_LimitiDC, gFoc_Ctrl.hwLim.s_iDCMax);
  168. gFoc_Ctrl.userLim.s_motRPMLim = min(nv_get_foc_params()->s_maxRPM, gFoc_Ctrl.hwLim.s_motRPMMax);
  169. gFoc_Ctrl.userLim.s_torqueLim = nv_get_foc_params()->s_maxTorque;//MAX_TORQUE;
  170. gFoc_Ctrl.userLim.s_PhaseCurrLim = min(nv_get_foc_params()->s_PhaseCurrLim, gFoc_Ctrl.hwLim.s_PhaseCurrMax);
  171. gFoc_Ctrl.userLim.s_vDCMaxLim = nv_get_foc_params()->s_maxDCVol;
  172. gFoc_Ctrl.userLim.s_vDCMinLim = nv_get_foc_params()->s_minDCVol;
  173. gFoc_Ctrl.userLim.s_iDCeBrkLim = nv_get_foc_params()->s_iDCeBrkLim;
  174. gFoc_Ctrl.userLim.s_PhaseVoleBrkLim = gFoc_Ctrl.hwLim.s_PhaseVolMax;
  175. }
  176. void PMSM_FOC_RT_LimInit(void) {
  177. gFoc_Ctrl.protLim.s_iDCLim = HW_LIMIT_NONE;
  178. gFoc_Ctrl.protLim.s_TorqueLim = HW_LIMIT_NONE;
  179. eRamp_init_target2(&gFoc_Ctrl.rtLim.rpmLimRamp, gFoc_Ctrl.userLim.s_motRPMLim, CONFIG_LIMIT_RAMP_TIME);
  180. eRamp_init_target2(&gFoc_Ctrl.rtLim.torqueLimRamp, gFoc_Ctrl.userLim.s_torqueLim, CONFIG_LIMIT_RAMP_TIME);
  181. eRamp_init_target2(&gFoc_Ctrl.rtLim.DCCurrLimRamp, gFoc_Ctrl.userLim.s_iDCLim, CONFIG_LIMIT_RAMP_TIME);
  182. }
  183. void PMSM_FOC_CoreInit(void) {
  184. PMSM_FOC_Conf_PID();
  185. memset(&gFoc_Ctrl.in, 0, sizeof(FOC_InP));
  186. memset(&gFoc_Ctrl.out, 0, sizeof(FOC_OutP));
  187. gFoc_Ctrl.hwLim.s_iDCMax = CONFIG_MAX_VBUS_CURRENT;
  188. gFoc_Ctrl.hwLim.s_motRPMMax = CONFIG_MAX_MOT_RPM;
  189. gFoc_Ctrl.hwLim.s_PhaseCurrMax = CONFIG_MAX_PHASE_CURR;
  190. gFoc_Ctrl.hwLim.s_PhaseVolMax = CONFIG_MAX_PHASE_VOL;
  191. gFoc_Ctrl.hwLim.s_vDCMax = CONFIG_MAX_DC_VOL;
  192. gFoc_Ctrl.hwLim.s_torqueMax = CONFIG_MAX_MOTOR_TORQUE;
  193. gFoc_Ctrl.hwLim.s_FWDCurrMax = CONFIG_MAX_FW_D_CURR;
  194. if (!g_focinit) {
  195. PMSM_FOC_UserInit();
  196. PMSM_FOC_RT_LimInit();
  197. shark_task_create(PMSM_FOC_Debug_Task, NULL);
  198. g_focinit = true;
  199. //_DEBUG("User Limit:\n");
  200. //_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,
  201. // gFoc_Ctrl.userLim.s_PhaseCurrLim, gFoc_Ctrl.userLim.s_vDCMaxLim, gFoc_Ctrl.userLim.s_vDCMinLim, gFoc_Ctrl.userLim.s_TorqueBrkLim);
  202. //_DEBUG("Hw Limit:\n");
  203. //_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);
  204. }
  205. gFoc_Ctrl.userLim.s_TorqueBrkLim = mc_get_ebrk_torque();
  206. gFoc_Ctrl.params.n_modulation = CONFIG_SVM_MODULATION;//SVM_Modulation;
  207. gFoc_Ctrl.params.n_PhaseFilterCeof = CONFIG_CURR_LP_CEOF;
  208. gFoc_Ctrl.params.n_poles = nv_get_motor_params()->poles;//MOTOR_POLES;
  209. gFoc_Ctrl.params.lq = nv_get_motor_params()->lq;
  210. gFoc_Ctrl.params.ld = nv_get_motor_params()->ld;
  211. gFoc_Ctrl.params.flux = nv_get_motor_params()->flux_linkage;
  212. gFoc_Ctrl.in.s_manualAngle = INVALID_ANGLE;
  213. gFoc_Ctrl.in.s_dqAngle = INVALID_ANGLE;
  214. gFoc_Ctrl.in.b_fwEnable = nv_get_foc_params()->n_FwEnable;
  215. gFoc_Ctrl.in.s_vDC = nv_get_foc_params()->s_maxDCVol;//(CONFIG_RATED_DC_VOL);
  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_motVelDegreePers * 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_motVelDegreePers < 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. //#define UPDATE_Lq_By_iq /* Q轴电感 通过Iq电流补偿 */
  454. #define CONFIG_Volvec_Delay_Comp /* 电压矢量角度补偿 */
  455. #define CONFIG_Volvec_Delay_Comp_Start_Vel 500 // rpm
  456. static float encoder_angle,obser_angle, obser_vel = 111111;
  457. static __INLINE bool PMSM_FOC_Update_Input(void) {
  458. AB_t iAB;
  459. float *iabc = gFoc_Ctrl.in.s_iABC;
  460. phase_current_get(iabc);
  461. PMSM_FOC_Calc_iDC_Fast();
  462. Clark(iabc[0], iabc[1], iabc[2], &iAB);
  463. 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);
  464. float enc_angle = motor_encoder_get_angle();
  465. float enc_vel = motor_encoder_get_speed();
  466. if (!foc_observer_diagnostic(enc_angle, enc_vel)){
  467. /* detect encoder angle error, do something here */
  468. if (!foc_observer_sensorless_stable()) {
  469. gFoc_Ctrl.in.s_motVelocity = 0;
  470. return false;
  471. }
  472. if (obser_vel == 111111) {
  473. obser_vel = foc_observer_sensorless_speed();
  474. obser_angle = foc_observer_sensorless_angle();
  475. encoder_angle = enc_angle;
  476. }
  477. enc_angle = foc_observer_sensorless_angle();
  478. enc_vel = foc_observer_sensorless_speed();
  479. }
  480. if (!gFoc_Ctrl.in.b_MTPA_calibrate && (gFoc_Ctrl.in.s_manualAngle != INVALID_ANGLE)) {
  481. gFoc_Ctrl.in.s_motAngle = gFoc_Ctrl.in.s_manualAngle;
  482. }else {
  483. gFoc_Ctrl.in.s_motAngle = enc_angle;
  484. }
  485. gFoc_Ctrl.in.s_motVelocity = enc_vel;
  486. LowPass_Filter(gFoc_Ctrl.in.s_motVelocityFiltered, gFoc_Ctrl.in.s_motVelocity, 0.01f);
  487. gFoc_Ctrl.in.s_motVelDegreePers = gFoc_Ctrl.in.s_motVelocityFiltered / 30.0f * PI * gFoc_Ctrl.params.n_poles;
  488. PMSM_FOC_Phase_Unbalance();
  489. #ifdef CONFIG_DQ_STEP_RESPONSE
  490. gFoc_Ctrl.in.s_motAngle = 0;
  491. #endif
  492. gFoc_Ctrl.in.s_vDC = get_vbus_float();
  493. get_phase_vols(gFoc_Ctrl.in.s_vABC);
  494. SinCos_Lut(gFoc_Ctrl.in.s_motAngle, &gFoc_Ctrl.out.sin, &gFoc_Ctrl.out.cos);
  495. Park(&iAB, gFoc_Ctrl.in.s_motAngle, &gFoc_Ctrl.out.s_RealIdq);
  496. float lowpass = gFoc_Ctrl.in.s_motVelDegreePers * FOC_CTRL_US * 2.0f;
  497. if (lowpass > 1.0f) {
  498. lowpass = 1.0f;
  499. }
  500. LowPass_Filter(gFoc_Ctrl.out.s_FilterIdq.d, gFoc_Ctrl.out.s_RealIdq.d, lowpass);
  501. LowPass_Filter(gFoc_Ctrl.out.s_FilterIdq.q, gFoc_Ctrl.out.s_RealIdq.q, lowpass);
  502. /* 使用低通后的dq电流重新变换得到abc电流,给死区补偿使用 */
  503. RevPark(&gFoc_Ctrl.out.s_FilterIdq, gFoc_Ctrl.in.s_motAngle, &iAB);
  504. RevClark(&iAB, gFoc_Ctrl.in.s_iABC_DT);
  505. #ifdef CONFIG_START_LINE_DTC_CURRENT
  506. gFoc_Ctrl.out.s_OutVdqDTC.d = 0;
  507. gFoc_Ctrl.out.s_OutVdqDTC.q = 0;
  508. #else
  509. AB_t vAB;
  510. 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]));
  511. vAB.b = ONE_BY_SQRT3 * (SIGN(gFoc_Ctrl.in.s_iABC_DT[1]) - SIGN(gFoc_Ctrl.in.s_iABC_DT[2]));
  512. float dtc = ((float)CONFIG_HW_DeadTime/(float)FOC_PWM_Half_Period) * gFoc_Ctrl.in.s_vDC;
  513. vAB.a = vAB.a * dtc;
  514. vAB.b = vAB.b * dtc;
  515. Park(&vAB, gFoc_Ctrl.in.s_motAngle, &gFoc_Ctrl.out.s_OutVdqDTC); //used for vbus current calc
  516. #endif
  517. #ifdef CONFIG_Volvec_Delay_Comp
  518. if (gFoc_Ctrl.in.s_motVelocityFiltered >= CONFIG_Volvec_Delay_Comp_Start_Vel) {
  519. float next_angle = gFoc_Ctrl.in.s_motAngle + gFoc_Ctrl.in.s_motVelDegreePers / PI * 180.0f * (FOC_CTRL_US * 0.8f);
  520. rand_angle(next_angle);
  521. SinCos_Lut(next_angle, &gFoc_Ctrl.out.sin, &gFoc_Ctrl.out.cos);
  522. }
  523. #endif
  524. return true;
  525. }
  526. #ifdef CONFIG_DQ_STEP_RESPONSE
  527. float target_d = 0.0f;
  528. float target_q = 0.0f;
  529. #endif
  530. static u32 PMSM_FOC_Debug_Task(void *p) {
  531. if (gFoc_Ctrl.in.b_motEnable) {
  532. #ifdef CONFIG_DQ_STEP_RESPONSE
  533. if (gFoc_Ctrl.plot_type == Plot_D_Step) {
  534. plot_2data16(FtoS16x10(target_d), FtoS16x10(gFoc_Ctrl.out.s_RealIdq.d));
  535. }if (gFoc_Ctrl.plot_type == Plot_Q_Step) {
  536. plot_2data16(FtoS16x10(target_q), FtoS16x10(gFoc_Ctrl.out.s_RealIdq.q));
  537. }
  538. #else
  539. if (gFoc_Ctrl.plot_type == Plot_D_flow) {
  540. plot_2data16(FtoS16x10(gFoc_Ctrl.idq_ctl[0].s_Cp), FtoS16x10(gFoc_Ctrl.out.s_RealIdq.d));
  541. }else if (gFoc_Ctrl.plot_type == Plot_Q_flow) {
  542. plot_2data16(FtoS16x10(gFoc_Ctrl.idq_ctl[1].s_Cp), FtoS16x10(gFoc_Ctrl.out.s_RealIdq.q));
  543. }else if (gFoc_Ctrl.plot_type == Plot_DQ_Curr) {
  544. plot_3data16(FtoS16x10(gFoc_Ctrl.out.s_RealIdq.d), FtoS16x10(gFoc_Ctrl.out.s_RealIdq.q), FtoS16x10(gFoc_Ctrl.out.s_FilteriDC));
  545. }else if (gFoc_Ctrl.plot_type == Plot_Spd_flow) {
  546. plot_2data16(gFoc_Ctrl.in.s_targetRPM, gFoc_Ctrl.in.s_motVelocity);
  547. }
  548. #endif
  549. }
  550. return 1;
  551. }
  552. static __INLINE float id_feedforward(float eW) {
  553. #ifdef CONFIG_CURRENT_LOOP_DECOUPE
  554. return -(gFoc_Ctrl.params.lq * gFoc_Ctrl.out.s_RealIdq.q * eW);
  555. #else
  556. return 0;
  557. #endif
  558. }
  559. static __INLINE float iq_feedforward(float eW) {
  560. #ifdef CONFIG_CURRENT_LOOP_DECOUPE
  561. return (gFoc_Ctrl.params.ld * gFoc_Ctrl.out.s_RealIdq.d + gFoc_Ctrl.params.flux) * eW;
  562. #else
  563. return 0;
  564. #endif
  565. }
  566. bool PMSM_FOC_Schedule(void) {
  567. gFoc_Ctrl.ctrl_count++;
  568. if (!PMSM_FOC_Update_Input()){
  569. return false;
  570. }
  571. if (gFoc_Ctrl.out.n_RunMode != CTRL_MODE_OPEN) {
  572. float max_Vdc = gFoc_Ctrl.in.s_vDC * CONFIG_SVM_MODULATION;
  573. float max_vd = max_Vdc * SQRT3_BY_2;
  574. /* limiter Vd output for PI controller */
  575. gFoc_Ctrl.pi_id.max = max_vd;
  576. gFoc_Ctrl.pi_id.min = -max_vd;
  577. #ifndef CONFIG_DQ_STEP_RESPONSE
  578. float target_d = FOC_Get_DqRamp(&gFoc_Ctrl.idq_ctl[0]);
  579. #endif
  580. float err = target_d - gFoc_Ctrl.out.s_RealIdq.d;
  581. float id_ff = id_feedforward(gFoc_Ctrl.in.s_motVelDegreePers);
  582. gFoc_Ctrl.in.s_targetVdq.d = PI_Controller_Current(&gFoc_Ctrl.pi_id, err, id_ff);
  583. #ifdef UPDATE_Lq_By_iq
  584. /* update kp&ki from lq for iq PI controller */
  585. float lq = motor_get_lq_from_iq((s16)gFoc_Ctrl.out.s_FilterIdq.q);
  586. LowPass_Filter(gFoc_Ctrl.params.lq, lq, 0.01f);
  587. gFoc_Ctrl.pi_iq.kp = ((float)nv_get_foc_params()->n_currentBand * gFoc_Ctrl.params.lq);
  588. gFoc_Ctrl.pi_iq.ki = (nv_get_motor_params()->r/gFoc_Ctrl.params.lq);
  589. #endif
  590. /* limiter Vq output for PI controller */
  591. float max_vq = sqrtf(SQ(max_vd) - SQ(gFoc_Ctrl.in.s_targetVdq.d));
  592. gFoc_Ctrl.pi_iq.max = max_vq;
  593. gFoc_Ctrl.pi_iq.min = -max_vq;
  594. #ifndef CONFIG_DQ_STEP_RESPONSE
  595. float target_q = FOC_Get_DqRamp(&gFoc_Ctrl.idq_ctl[1]);
  596. #endif
  597. err = target_q - gFoc_Ctrl.out.s_RealIdq.q;
  598. float iq_ff = iq_feedforward(gFoc_Ctrl.in.s_motVelDegreePers);
  599. gFoc_Ctrl.in.s_targetVdq.q = PI_Controller_Current(&gFoc_Ctrl.pi_iq, err, iq_ff);
  600. }else {
  601. float max_Vdc = gFoc_Ctrl.in.s_vDC * CONFIG_SVM_MODULATION;
  602. float max_vd = max_Vdc * SQRT3_BY_2;
  603. float vd_ref = FOC_Get_DqRamp(&gFoc_Ctrl.vdq_ctl[0]);
  604. gFoc_Ctrl.in.s_targetVdq.d = fclamp(vd_ref, -max_vd, max_vd);
  605. float max_vq = sqrtf(SQ(max_vd) - SQ(gFoc_Ctrl.in.s_targetVdq.d));
  606. float vq_ref = FOC_Get_DqRamp(&gFoc_Ctrl.vdq_ctl[1]);
  607. gFoc_Ctrl.in.s_targetVdq.q = fclamp(vq_ref, -max_vq, max_vq);
  608. }
  609. #if 0
  610. 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);
  611. gFoc_Ctrl.out.s_OutVdq.d *= SQRT3_BY_2;
  612. gFoc_Ctrl.out.s_OutVdq.q *= SQRT3_BY_2;
  613. #else
  614. gFoc_Ctrl.out.s_OutVdq.d = gFoc_Ctrl.in.s_targetVdq.d;
  615. gFoc_Ctrl.out.s_OutVdq.q = gFoc_Ctrl.in.s_targetVdq.q;
  616. #endif
  617. RevPark(&gFoc_Ctrl.out.s_OutVdq, gFoc_Ctrl.in.s_motAngle, &gFoc_Ctrl.out.s_OutVAB);
  618. SVM_Duty_Fix(&gFoc_Ctrl.out.s_OutVAB, gFoc_Ctrl.in.s_vDC, FOC_PWM_Half_Period, &gFoc_Ctrl.out);
  619. PMSM_FOC_DeadTime_Compensate((s32)FOC_PWM_Half_Period);
  620. phase_current_point(&gFoc_Ctrl.out);
  621. pwm_update_duty(gFoc_Ctrl.out.n_Duty[0], gFoc_Ctrl.out.n_Duty[1], gFoc_Ctrl.out.n_Duty[2]);
  622. pwm_update_sample(gFoc_Ctrl.out.n_Sample1, gFoc_Ctrl.out.n_Sample2, gFoc_Ctrl.out.n_CPhases);
  623. return true;
  624. }
  625. void PMSM_FOC_LogDebug(void) {
  626. sys_debug("DC curr %f --- %f, - %f\n", gFoc_Ctrl.out.s_CalciDC, gFoc_Ctrl.out.s_FilteriDC, gFoc_Ctrl.userLim.s_iDCLim);
  627. sys_debug("%s\n", gFoc_Ctrl.out.empty_load?"NoLoad Running":"Load Runing");
  628. sys_debug("unbalance: %d, %f, %f, %f, %f\n", phase_unbalance_cnt, phase_unbalance_r, phase_a_max, phase_b_max, phase_c_max);
  629. if (obser_vel != 111111) {
  630. sys_debug("AB error: %f,%f,%f\n", obser_angle, encoder_angle, obser_vel);
  631. }
  632. }
  633. /*called in media task */
  634. u8 PMSM_FOC_CtrlMode(void) {
  635. u8 preMode = gFoc_Ctrl.out.n_RunMode;
  636. if (!gFoc_Ctrl.in.b_motEnable) {
  637. gFoc_Ctrl.out.n_RunMode = CTRL_MODE_OPEN;
  638. }else if (gFoc_Ctrl.in.n_ctlMode == CTRL_MODE_OPEN) {
  639. gFoc_Ctrl.out.n_RunMode = CTRL_MODE_OPEN;
  640. }else if (gFoc_Ctrl.in.n_ctlMode == CTRL_MODE_SPD || gFoc_Ctrl.in.b_cruiseEna){
  641. gFoc_Ctrl.out.n_RunMode = CTRL_MODE_SPD;
  642. }else if (gFoc_Ctrl.in.n_ctlMode == CTRL_MODE_CURRENT) {
  643. gFoc_Ctrl.out.n_RunMode = CTRL_MODE_CURRENT;
  644. }else if (gFoc_Ctrl.in.n_ctlMode == CTRL_MODE_EBRAKE) {
  645. gFoc_Ctrl.out.n_RunMode = CTRL_MODE_EBRAKE;
  646. }else {
  647. if (!gFoc_Ctrl.in.b_cruiseEna) {
  648. gFoc_Ctrl.out.n_RunMode = CTRL_MODE_TRQ;
  649. }
  650. }
  651. if (preMode != gFoc_Ctrl.out.n_RunMode) {
  652. if ((preMode == CTRL_MODE_SPD) && (gFoc_Ctrl.out.n_RunMode == CTRL_MODE_TRQ)) {
  653. #ifdef CONFIG_SPEED_LADRC
  654. //ladrc_reset(&gFoc_Ctrl.vel_lim_adrc, gFoc_Ctrl.in.s_motVelocity, gFoc_Ctrl.in.s_targetTorque);
  655. ladrc_copy(&gFoc_Ctrl.vel_lim_adrc, &gFoc_Ctrl.vel_adrc);
  656. #else
  657. PI_Controller_Reset(&gFoc_Ctrl.pi_vel_lim, gFoc_Ctrl.in.s_targetTorque);
  658. #endif
  659. }else if ((preMode == CTRL_MODE_TRQ) && (gFoc_Ctrl.out.n_RunMode == CTRL_MODE_SPD)) {
  660. #ifdef CONFIG_SPEED_LADRC
  661. ladrc_copy(&gFoc_Ctrl.vel_adrc, &gFoc_Ctrl.vel_lim_adrc);
  662. #else
  663. PI_Controller_Reset(&gFoc_Ctrl.pi_vel, gFoc_Ctrl.in.s_targetTorque);
  664. #endif
  665. }else if ((preMode == CTRL_MODE_CURRENT) && (gFoc_Ctrl.out.n_RunMode == CTRL_MODE_TRQ)) {
  666. #ifdef CONFIG_SPEED_LADRC
  667. ladrc_reset(&gFoc_Ctrl.vel_lim_adrc, gFoc_Ctrl.in.s_motVelocity, gFoc_Ctrl.in.s_targetTorque);
  668. #else
  669. PI_Controller_Reset(&gFoc_Ctrl.pi_vel_lim, gFoc_Ctrl.in.s_targetTorque);
  670. #endif
  671. }else if ((preMode != gFoc_Ctrl.out.n_RunMode) && (gFoc_Ctrl.out.n_RunMode == CTRL_MODE_EBRAKE)) {
  672. eCtrl_reset_Torque(gFoc_Ctrl.in.s_targetTorque);
  673. eCtrl_set_TgtTorque(motor_get_ebreak_toruqe(gFoc_Ctrl.in.s_motVelocity));
  674. }else if ((preMode == CTRL_MODE_EBRAKE) && (gFoc_Ctrl.out.n_RunMode == CTRL_MODE_SPD)) {
  675. #ifdef CONFIG_SPEED_LADRC
  676. ladrc_reset(&gFoc_Ctrl.vel_adrc, 0, F_get_air());
  677. #else
  678. PI_Controller_Reset(&gFoc_Ctrl.pi_vel, F_get_air());
  679. #endif
  680. }
  681. }
  682. return gFoc_Ctrl.out.n_RunMode;
  683. }
  684. #define RAMPE_1 CONFIG_RAMP_FIRST_TARGET
  685. static void crosszero_step_towards(float *value, float target) {
  686. static float no_cro_step = 2.0f;
  687. float v_now = *value;
  688. bool cross_zero = false;
  689. float high_ramp_torque = CONFIG_RAMP_SECOND_TARGET;
  690. if (target > 0) {
  691. if (v_now < -RAMPE_1) {
  692. step_towards(value, -RAMPE_1, 2.0f);
  693. cross_zero = true;
  694. }else if (v_now >= -RAMPE_1 && v_now <= high_ramp_torque) {
  695. step_towards(value, target, 0.03f);
  696. cross_zero = true;
  697. }
  698. }else if (target == 0) {
  699. if (v_now > high_ramp_torque) {
  700. step_towards(value, high_ramp_torque, 1.0f);
  701. cross_zero = true;
  702. }else if (v_now >= RAMPE_1 && v_now <= high_ramp_torque) {
  703. step_towards(value, target, 0.03f);
  704. cross_zero = true;
  705. }
  706. }else {
  707. if (v_now > high_ramp_torque) {
  708. step_towards(value, high_ramp_torque, 20.0f);
  709. cross_zero = true;
  710. }else if (v_now >= -RAMPE_1 && v_now <= high_ramp_torque) {
  711. step_towards(value, target, 0.03f);
  712. cross_zero = true;
  713. }
  714. }
  715. if (!cross_zero) {
  716. step_towards(&no_cro_step, 3.0f, 0.1f);
  717. step_towards(value, target, no_cro_step);
  718. }else {
  719. no_cro_step = 0.5f;
  720. }
  721. }
  722. /* MPTA, 弱磁, 功率限制,主要是分配DQ轴电流 */
  723. #define CHANGE_MAX_CNT 3
  724. static __INLINE void PMSM_FOC_VelCtrl_Decide(void) {
  725. #if 0
  726. static int change_cnt = 0;
  727. static bool change_done = false;
  728. static u32 change_time = 0xFFFFFFFF;
  729. float f_te = F_get_Te();
  730. float f_accl = F_get_accl();
  731. if (mc_is_epm()) {
  732. change_cnt = 0;
  733. change_time = 0xFFFFFFFF;
  734. change_done = false;
  735. gFoc_Ctrl.out.empty_load = false;
  736. return;
  737. }
  738. if (gFoc_Ctrl.in.s_motVelocity == 0.0f || gFoc_Ctrl.out.n_RunMode == CTRL_MODE_OPEN) {
  739. change_cnt = 0;
  740. change_time = 0xFFFFFFFF;
  741. change_done = false;
  742. gFoc_Ctrl.out.empty_load = false;
  743. return;
  744. }
  745. if (f_te <= 0.0f) {
  746. change_cnt = 0;
  747. change_time = 0xFFFFFFFF;
  748. return;
  749. }
  750. if (change_done) {
  751. /* 误判空转,发现电机给定的N大于空气阻力,说明不是空转 */
  752. if (gFoc_Ctrl.out.empty_load) {
  753. float f_air = F_get_air();
  754. if ((f_accl > 1.0f) && (f_te >= (f_air + f_accl))) {
  755. change_cnt ++;
  756. }else {
  757. change_cnt = 0;
  758. }
  759. if (change_cnt >= 500) {
  760. gFoc_Ctrl.out.empty_load = false;
  761. #ifdef CONFIG_SPEED_LADRC
  762. PMSM_FOC_Change_TrqLoop_Params(nv_get_foc_params()->f_adrc_vel_lim_Wcv, nv_get_foc_params()->f_adrc_vel_lim_B0);
  763. PMSM_FOC_Change_VelLoop_Params(nv_get_foc_params()->f_adrc_vel_Wcv, nv_get_foc_params()->f_adrc_vel_B0);
  764. #endif
  765. }
  766. }
  767. return;
  768. }
  769. if (change_time == 0xFFFFFFFF) {
  770. change_time = get_tick_ms();
  771. }else { //起步3s内检测是否空转
  772. if (get_delta_ms(change_time) > 3000) {
  773. return;
  774. }
  775. }
  776. if ((f_accl > 200.0f) && (f_accl/f_te > 3.0f )) {
  777. change_cnt++;
  778. }else if ((F_get_MotAccl() >= 10.0f) && (f_accl/f_te > 1.2f )) {
  779. change_cnt = CHANGE_MAX_CNT;
  780. }
  781. else {
  782. if ((f_te > 50) && (f_accl > 0) && (f_te > f_accl)) {
  783. change_cnt --;
  784. }else {
  785. change_cnt = 0;
  786. }
  787. }
  788. if (!change_done && (change_cnt >= CHANGE_MAX_CNT)) {
  789. change_done = true;
  790. change_cnt = 0;
  791. gFoc_Ctrl.out.empty_load = change_done;
  792. #ifdef CONFIG_SPEED_LADRC
  793. PMSM_FOC_Change_TrqLoop_Params(CONFIG_LADRC_NOLOAD_Wcv, CONFIG_LADRC_NOLOAD_B0);
  794. PMSM_FOC_Change_VelLoop_Params(CONFIG_LADRC_NOLOAD_Wcv, CONFIG_LADRC_NOLOAD_B0);
  795. #endif
  796. }else if (!change_done && (change_cnt <= -200)) {
  797. change_done = true;
  798. change_cnt = 0;
  799. gFoc_Ctrl.out.empty_load = false;
  800. }
  801. #endif
  802. }
  803. static __INLINE float PMSM_FOC_Limit_iDC(float maxTrq) {
  804. #if 1
  805. gFoc_Ctrl.pi_power.max = maxTrq;
  806. float errRef = eRamp_get_intepolation(&gFoc_Ctrl.rtLim.DCCurrLimRamp) - (gFoc_Ctrl.out.s_FilteriDC);
  807. return PI_Controller_Run(&gFoc_Ctrl.pi_power, errRef);
  808. #else
  809. return maxTrq;
  810. #endif
  811. }
  812. static __INLINE float PMSM_FOC_Limit_Speed(float maxTrq) {
  813. #ifdef CONFIG_SPEED_LADRC
  814. float lim = eRamp_get_intepolation(&gFoc_Ctrl.rtLim.rpmLimRamp);
  815. ladrc_set_range(&gFoc_Ctrl.vel_lim_adrc, 0, maxTrq);
  816. return ladrc_run(&gFoc_Ctrl.vel_lim_adrc, lim, gFoc_Ctrl.in.s_motVelocity);
  817. #else
  818. gFoc_Ctrl.pi_vel_lim.max = maxTrq;
  819. gFoc_Ctrl.pi_vel_lim.min = 0;
  820. float err = eRamp_get_intepolation(&gFoc_Ctrl.rtLim.rpmLimRamp) - gFoc_Ctrl.in.s_motVelocity;
  821. return PI_Controller_RunVel(&gFoc_Ctrl.pi_vel_lim, err);
  822. #endif
  823. }
  824. static __INLINE void PMSM_FOC_idq_Assign(void) {
  825. if (gFoc_Ctrl.out.n_RunMode == CTRL_MODE_CURRENT) {
  826. if (gFoc_Ctrl.in.b_MTPA_calibrate && (gFoc_Ctrl.in.s_dqAngle != INVALID_ANGLE)) {
  827. float s, c;
  828. normal_sincosf(degree_2_pi(gFoc_Ctrl.in.s_dqAngle + 90.0f), &s, &c);
  829. gFoc_Ctrl.in.s_targetIdq.d = gFoc_Ctrl.in.s_targetCurrent * c;
  830. if (gFoc_Ctrl.in.s_targetIdq.d > gFoc_Ctrl.hwLim.s_FWDCurrMax) {
  831. gFoc_Ctrl.in.s_targetIdq.d = gFoc_Ctrl.hwLim.s_FWDCurrMax;
  832. }else if (gFoc_Ctrl.in.s_targetIdq.d < -gFoc_Ctrl.hwLim.s_FWDCurrMax) {
  833. gFoc_Ctrl.in.s_targetIdq.d = -gFoc_Ctrl.hwLim.s_FWDCurrMax;
  834. }
  835. gFoc_Ctrl.in.s_targetIdq.q = sqrtf(SQ(gFoc_Ctrl.in.s_targetCurrent) - SQ(gFoc_Ctrl.in.s_targetIdq.d));
  836. if (s < 0) {
  837. gFoc_Ctrl.in.s_targetIdq.q = -gFoc_Ctrl.in.s_targetIdq.q;
  838. }
  839. }else {
  840. gFoc_Ctrl.in.s_targetIdq.d = 0;
  841. gFoc_Ctrl.in.s_targetIdq.q = gFoc_Ctrl.in.s_targetCurrent;
  842. }
  843. }else if ((gFoc_Ctrl.out.n_RunMode == CTRL_MODE_TRQ) || (gFoc_Ctrl.out.n_RunMode == CTRL_MODE_SPD) ||
  844. (gFoc_Ctrl.out.n_RunMode == CTRL_MODE_EBRAKE)) {
  845. motor_mpta_fw_lookup(gFoc_Ctrl.in.s_motVelocity, gFoc_Ctrl.in.s_targetTorque, &gFoc_Ctrl.in.s_targetIdq);
  846. }
  847. u32 mask = cpu_enter_critical();
  848. FOC_Set_iDqRamp(&gFoc_Ctrl.idq_ctl[0], gFoc_Ctrl.in.s_targetIdq.d);
  849. FOC_Set_iDqRamp(&gFoc_Ctrl.idq_ctl[1], gFoc_Ctrl.in.s_targetIdq.q);
  850. cpu_exit_critical(mask);
  851. }
  852. /*called in media task */
  853. void PMSM_FOC_idqCalc(void) {
  854. if (gFoc_Ctrl.in.b_AutoHold) {
  855. gFoc_Ctrl.pi_lock.max = CONFIG_MAX_LOCK_TORQUE;
  856. gFoc_Ctrl.pi_lock.min = -CONFIG_MAX_LOCK_TORQUE;
  857. float vel_count = motor_encoder_get_vel_count();
  858. float errRef = 0 - vel_count;
  859. gFoc_Ctrl.in.s_targetTorque = PI_Controller_Run(&gFoc_Ctrl.pi_lock ,errRef);
  860. PMSM_FOC_idq_Assign();
  861. return;
  862. }
  863. if (gFoc_Ctrl.out.n_RunMode == CTRL_MODE_CURRENT) {
  864. gFoc_Ctrl.in.s_targetCurrent = eCtrl_get_RefCurrent();
  865. }else if (gFoc_Ctrl.out.n_RunMode == CTRL_MODE_EBRAKE) {
  866. float maxTrq = eCtrl_get_RefTorque();
  867. if (eCtrl_get_FinalTorque() < 0.0001f && gFoc_Ctrl.in.s_motVelocity < CONFIG_MIN_RPM_EXIT_EBRAKE) {
  868. maxTrq = 0;
  869. }
  870. crosszero_step_towards(&gFoc_Ctrl.in.s_targetTorque, maxTrq);
  871. }else if (gFoc_Ctrl.out.n_RunMode == CTRL_MODE_TRQ) {
  872. float refTorque = min(eCtrl_get_RefTorque(), eRamp_get_intepolation(&gFoc_Ctrl.rtLim.torqueLimRamp));
  873. float maxTrq = PMSM_FOC_Limit_Speed(refTorque);
  874. maxTrq = PMSM_FOC_Limit_iDC(maxTrq);
  875. crosszero_step_towards(&gFoc_Ctrl.in.s_targetTorque, maxTrq);
  876. }else if (gFoc_Ctrl.out.n_RunMode == CTRL_MODE_SPD){
  877. float maxSpeed = eCtrl_get_FinalSpeed();
  878. float refSpeed = eCtrl_get_RefSpeed();
  879. if (gFoc_Ctrl.in.b_cruiseEna) {
  880. maxSpeed = eRamp_get_target(&gFoc_Ctrl.in.cruiseRpmRamp);
  881. refSpeed = eRamp_get_intepolation(&gFoc_Ctrl.in.cruiseRpmRamp);//gFoc_Ctrl.in.s_cruiseRPM;
  882. }
  883. #ifdef CONFIG_SPEED_LADRC
  884. if (maxSpeed >= 0) {
  885. ladrc_set_range(&gFoc_Ctrl.vel_adrc, -CONFIG_MAX_NEG_TORQUE, eRamp_get_intepolation(&gFoc_Ctrl.rtLim.torqueLimRamp));
  886. }else if (maxSpeed < 0) {
  887. ladrc_set_range(&gFoc_Ctrl.vel_adrc, -eRamp_get_intepolation(&gFoc_Ctrl.rtLim.torqueLimRamp), CONFIG_MAX_NEG_TORQUE);
  888. }
  889. if ((maxSpeed == 0) && (gFoc_Ctrl.in.s_motVelocity < CONFIG_MIN_RPM_EXIT_EBRAKE)) {
  890. ladrc_set_range(&gFoc_Ctrl.vel_adrc, 0, 0);
  891. }
  892. gFoc_Ctrl.in.s_targetRPM = refSpeed;
  893. float maxTrq = ladrc_run(&gFoc_Ctrl.vel_adrc, refSpeed, gFoc_Ctrl.in.s_motVelocity);
  894. #else
  895. if (maxSpeed >= 0) {
  896. gFoc_Ctrl.pi_vel.max = eRamp_get_intepolation(&gFoc_Ctrl.rtLim.torqueLimRamp);
  897. gFoc_Ctrl.pi_vel.min = -CONFIG_MAX_NEG_TORQUE;
  898. }else if (maxSpeed < 0) {
  899. gFoc_Ctrl.pi_vel.min = -eRamp_get_intepolation(&gFoc_Ctrl.rtLim.torqueLimRamp);
  900. gFoc_Ctrl.pi_vel.max = CONFIG_MAX_NEG_TORQUE;
  901. }
  902. if ((maxSpeed == 0) && (gFoc_Ctrl.in.s_motVelocity < CONFIG_MIN_RPM_EXIT_EBRAKE)) {
  903. gFoc_Ctrl.pi_vel.max = 0;
  904. gFoc_Ctrl.pi_vel.min = 0; //防止倒转
  905. }
  906. gFoc_Ctrl.in.s_targetRPM = refSpeed;
  907. float errRef = refSpeed - gFoc_Ctrl.in.s_motVelocity;
  908. float maxTrq = PI_Controller_RunVel(&gFoc_Ctrl.pi_vel, errRef);
  909. #endif
  910. maxTrq = PMSM_FOC_Limit_iDC(maxTrq);
  911. crosszero_step_towards(&gFoc_Ctrl.in.s_targetTorque, maxTrq);
  912. }
  913. PMSM_FOC_idq_Assign();
  914. }
  915. u8 PMSM_FOC_RunTime_Limit(void) {
  916. u8 changed = FOC_LIM_NO_CHANGE;
  917. float dc_lim = (float)vbus_under_vol_limit();
  918. float torque_lim = (float)min(mos_temp_high_limit(), motor_temp_high_limit());
  919. if (gFoc_Ctrl.protLim.s_iDCLim != dc_lim || gFoc_Ctrl.protLim.s_TorqueLim != torque_lim) {
  920. if ((dc_lim > gFoc_Ctrl.protLim.s_iDCLim) || (torque_lim > gFoc_Ctrl.protLim.s_TorqueLim)) {
  921. changed = FOC_LIM_CHANGE_H;
  922. }else {
  923. changed = FOC_LIM_CHANGE_L;
  924. }
  925. gFoc_Ctrl.protLim.s_iDCLim = dc_lim;
  926. gFoc_Ctrl.protLim.s_TorqueLim = torque_lim;
  927. }
  928. return changed;
  929. }
  930. bool PMSM_FOC_iDC_is_Limited(void) {
  931. return (gFoc_Ctrl.protLim.s_iDCLim != HW_LIMIT_NONE);
  932. }
  933. bool PMSM_FOC_Torque_is_Limited(void) {
  934. return (gFoc_Ctrl.protLim.s_TorqueLim != HW_LIMIT_NONE);
  935. }
  936. void PMSM_FOC_Slow_Task(void) {
  937. eRamp_running(&gFoc_Ctrl.rtLim.torqueLimRamp);
  938. eRamp_running(&gFoc_Ctrl.rtLim.DCCurrLimRamp);
  939. eRamp_running(&gFoc_Ctrl.rtLim.rpmLimRamp);
  940. eRamp_running(&gFoc_Ctrl.in.cruiseRpmRamp);
  941. PMSM_FOC_VelCtrl_Decide();
  942. PMSM_FOC_idqCalc();
  943. }
  944. void PMSM_FOC_Change_VelLoop_Params(float wcv, float b0) {
  945. #ifdef CONFIG_SPEED_LADRC
  946. ladrc_change_b0(&gFoc_Ctrl.vel_adrc, b0);
  947. ladrc_change_K(&gFoc_Ctrl.vel_adrc, wcv);
  948. #else
  949. PI_Controller_Change_Kpi(&gFoc_Ctrl.pi_vel, wcv, b0);
  950. #endif
  951. }
  952. void PMSM_FOC_Change_TrqLoop_Params(float wcv, float b0) {
  953. #ifdef CONFIG_SPEED_LADRC
  954. ladrc_change_b0(&gFoc_Ctrl.vel_lim_adrc, b0);
  955. ladrc_change_K(&gFoc_Ctrl.vel_lim_adrc, wcv);
  956. #else
  957. PI_Controller_Change_Kpi(&gFoc_Ctrl.pi_vel_lim, wcv, b0);
  958. #endif
  959. }
  960. float PMSM_FOC_Get_Real_dqVector(void) {
  961. if (gFoc_Ctrl.out.s_RealCurrentFiltered == 0) {
  962. gFoc_Ctrl.out.s_RealCurrentFiltered = sqrtf(SQ(gFoc_Ctrl.out.s_FilterIdq.d) + SQ(gFoc_Ctrl.out.s_FilterIdq.q));
  963. }
  964. return gFoc_Ctrl.out.s_RealCurrentFiltered;
  965. }
  966. PMSM_FOC_Ctrl *PMSM_FOC_Get(void) {
  967. return &gFoc_Ctrl;
  968. }
  969. void PMSM_FOC_Start(u8 nCtrlMode) {
  970. if (gFoc_Ctrl.in.b_motEnable) {
  971. return;
  972. }
  973. PMSM_FOC_CoreInit();
  974. eCtrl_Reset();
  975. gFoc_Ctrl.in.n_ctlMode = nCtrlMode;
  976. gFoc_Ctrl.in.b_motEnable = true;
  977. }
  978. void PMSM_FOC_Stop(void) {
  979. if (!gFoc_Ctrl.in.b_motEnable) {
  980. return;
  981. }
  982. PMSM_FOC_CoreInit();
  983. gFoc_Ctrl.in.b_motEnable = false;
  984. }
  985. bool PMSM_FOC_Is_Start(void) {
  986. return gFoc_Ctrl.in.b_motEnable;
  987. }
  988. void PMSM_FOC_DCCurrLimit(float ibusLimit) {
  989. if (ibusLimit > gFoc_Ctrl.hwLim.s_iDCMax) {
  990. ibusLimit = gFoc_Ctrl.hwLim.s_iDCMax;
  991. }
  992. if (gFoc_Ctrl.protLim.s_iDCLim != HW_LIMIT_NONE) {
  993. ibusLimit = min(ibusLimit, gFoc_Ctrl.protLim.s_iDCLim);
  994. }
  995. gFoc_Ctrl.userLim.s_iDCLim = ibusLimit;
  996. if (ABS(gFoc_Ctrl.in.s_motVelocity) <= CONFIG_ZERO_SPEED_RPM){
  997. eRamp_reset_target(&gFoc_Ctrl.rtLim.DCCurrLimRamp, ibusLimit);
  998. }else {
  999. eRamp_set_step_target(&gFoc_Ctrl.rtLim.DCCurrLimRamp, ibusLimit, CONFIG_eCTRL_STEP_TS);
  1000. }
  1001. }
  1002. float PMSM_FOC_GetDCCurrLimit(void) {
  1003. return gFoc_Ctrl.userLim.s_iDCLim;
  1004. }
  1005. void PMSM_FOC_SpeedRampLimit(float speedLimit, float speed) {
  1006. if (speedLimit > gFoc_Ctrl.hwLim.s_motRPMMax) {
  1007. speedLimit = gFoc_Ctrl.hwLim.s_motRPMMax;
  1008. }
  1009. gFoc_Ctrl.userLim.s_motRPMLim = (speedLimit);
  1010. if (ABS(speed) <= CONFIG_ZERO_SPEED_RPM) {
  1011. eRamp_reset_target(&gFoc_Ctrl.rtLim.rpmLimRamp, speedLimit);
  1012. }else {
  1013. eRamp_set_step_target(&gFoc_Ctrl.rtLim.rpmLimRamp, speedLimit, CONFIG_eCTRL_STEP_TS);
  1014. }
  1015. }
  1016. void PMSM_FOC_SpeedLimit(float speedLimit) {
  1017. PMSM_FOC_SpeedRampLimit(speedLimit, gFoc_Ctrl.in.s_motVelocity);
  1018. }
  1019. void PMSM_FOC_SpeedDirectLimit(float limit) {
  1020. PMSM_FOC_SpeedRampLimit(limit, 0);
  1021. }
  1022. float PMSM_FOC_GetSpeedLimit(void) {
  1023. return gFoc_Ctrl.userLim.s_motRPMLim;
  1024. }
  1025. void PMSM_FOC_TorqueLimit(float torqueLimit) {
  1026. if (torqueLimit > gFoc_Ctrl.hwLim.s_torqueMax) {
  1027. torqueLimit = gFoc_Ctrl.hwLim.s_torqueMax;
  1028. }
  1029. if (gFoc_Ctrl.protLim.s_TorqueLim != HW_LIMIT_NONE) {
  1030. torqueLimit = min(torqueLimit, gFoc_Ctrl.protLim.s_TorqueLim);
  1031. }
  1032. gFoc_Ctrl.userLim.s_torqueLim = torqueLimit;
  1033. if (ABS(gFoc_Ctrl.in.s_motVelocity) <= CONFIG_ZERO_SPEED_RPM){
  1034. eRamp_reset_target(&gFoc_Ctrl.rtLim.torqueLimRamp, torqueLimit);
  1035. }else {
  1036. eRamp_set_step_target(&gFoc_Ctrl.rtLim.torqueLimRamp, torqueLimit, CONFIG_eCTRL_STEP_TS);
  1037. }
  1038. }
  1039. float PMSM_FOC_GetTorqueLimit(void) {
  1040. return gFoc_Ctrl.userLim.s_torqueLim;
  1041. }
  1042. void PMSM_FOC_SetEbrkTorque(s16 torque) {
  1043. gFoc_Ctrl.userLim.s_TorqueBrkLim = (float)torque;
  1044. //gFoc_Ctrl.userLim.s_iDCeBrkLim = fclamp(dc_curr, 0, nv_get_foc_params()->s_iDCeBrkLim);
  1045. }
  1046. float PMSM_FOC_GetEbrkTorque(void) {
  1047. if (!foc_observer_is_encoder()) {
  1048. return 0; //无感运行关闭能量回收
  1049. }
  1050. return gFoc_Ctrl.userLim.s_TorqueBrkLim;
  1051. }
  1052. float PMSM_FOC_GetVbusVoltage(void) {
  1053. return gFoc_Ctrl.in.s_vDC;
  1054. }
  1055. float PMSM_FOC_GetVbusCurrent(void) {
  1056. return gFoc_Ctrl.out.s_FilteriDC;
  1057. }
  1058. DQ_t* PMSM_FOC_GetDQCurrent(void) {
  1059. return &gFoc_Ctrl.out.s_RealIdq;
  1060. }
  1061. bool PMSM_FOC_SetCtrlMode(u8 mode) {
  1062. if (mode > CTRL_MODE_EBRAKE) {
  1063. PMSM_FOC_SetErrCode(FOC_Param_Err);
  1064. return false;
  1065. }
  1066. gFoc_Ctrl.in.n_ctlMode = mode;
  1067. return true;
  1068. }
  1069. u8 PMSM_FOC_GetCtrlMode(void) {
  1070. return gFoc_Ctrl.in.n_ctlMode;
  1071. }
  1072. void PMSM_FOC_PhaseCurrLim(float lim) {
  1073. if (lim > gFoc_Ctrl.hwLim.s_PhaseCurrMax) {
  1074. lim = gFoc_Ctrl.hwLim.s_PhaseCurrMax;
  1075. }
  1076. gFoc_Ctrl.userLim.s_PhaseCurrLim = lim;
  1077. }
  1078. void PMSM_FOC_RT_PhaseCurrLim(float lim) {
  1079. if (lim > gFoc_Ctrl.hwLim.s_PhaseCurrMax) {
  1080. lim = gFoc_Ctrl.hwLim.s_PhaseCurrMax;
  1081. }
  1082. eRamp_init_target2(&gFoc_Ctrl.rtLim.torqueLimRamp, lim, CONFIG_LIMIT_RAMP_TIME);
  1083. }
  1084. float PMSM_FOC_GetPhaseCurrLim(void) {
  1085. return gFoc_Ctrl.userLim.s_PhaseCurrLim;
  1086. }
  1087. void PMSM_FOC_SetOpenVdq(float vd, float vq) {
  1088. FOC_Set_vDqRamp(&gFoc_Ctrl.vdq_ctl[0], vd);
  1089. FOC_Set_vDqRamp(&gFoc_Ctrl.vdq_ctl[1], vq);
  1090. }
  1091. void PMSM_FOC_SetOpenVdq_Immediate(float vd, float vq) {
  1092. gFoc_Ctrl.vdq_ctl[0].s_Step = 0;
  1093. gFoc_Ctrl.vdq_ctl[0].s_FinalTgt = vd;
  1094. gFoc_Ctrl.vdq_ctl[0].s_Cp = vd;
  1095. gFoc_Ctrl.vdq_ctl[1].s_Step = 0;
  1096. gFoc_Ctrl.vdq_ctl[1].s_FinalTgt = vq;
  1097. gFoc_Ctrl.vdq_ctl[1].s_Cp = vq;
  1098. }
  1099. bool PMSM_FOC_EnableCruise(bool enable) {
  1100. if (enable != gFoc_Ctrl.in.b_cruiseEna) {
  1101. float motSpd = PMSM_FOC_GetSpeed();
  1102. if (enable && (motSpd < CONFIG_MIN_CRUISE_RPM)) { //
  1103. PMSM_FOC_SetErrCode(FOC_NowAllowed_With_Speed);
  1104. return false;
  1105. }
  1106. eRamp_reset_target(&gFoc_Ctrl.in.cruiseRpmRamp, motSpd);
  1107. gFoc_Ctrl.in.s_cruiseRPM = motSpd;
  1108. gFoc_Ctrl.in.b_cruiseEna = enable;
  1109. }
  1110. return true;
  1111. }
  1112. bool PMSM_FOC_PauseCruise(void) {
  1113. gFoc_Ctrl.in.b_cruiseEna = false;
  1114. return true;
  1115. }
  1116. bool PMSM_FOC_ResumeCruise(void) {
  1117. gFoc_Ctrl.in.b_cruiseEna = true;
  1118. eRamp_init_target2(&gFoc_Ctrl.in.cruiseRpmRamp, PMSM_FOC_GetSpeed(), CONFIG_CRUISE_RAMP_TIME);
  1119. eRamp_set_step_target(&gFoc_Ctrl.in.cruiseRpmRamp, gFoc_Ctrl.in.s_cruiseRPM, CONFIG_eCTRL_STEP_TS);
  1120. return true;
  1121. }
  1122. bool PMSM_FOC_Is_CruiseEnabled(void) {
  1123. return (gFoc_Ctrl.in.b_cruiseEna && (gFoc_Ctrl.out.n_RunMode == CTRL_MODE_SPD));
  1124. }
  1125. bool PMSM_FOC_Set_Speed(float rpm) {
  1126. if (gFoc_Ctrl.in.b_cruiseEna) {
  1127. return false;
  1128. }
  1129. eCtrl_set_TgtSpeed(min(ABS(rpm), gFoc_Ctrl.userLim.s_motRPMLim)*SIGN(rpm));
  1130. return true;
  1131. }
  1132. bool PMSM_FOC_Set_Current(float is) {
  1133. if (is > gFoc_Ctrl.userLim.s_PhaseCurrLim) {
  1134. is = gFoc_Ctrl.userLim.s_PhaseCurrLim;
  1135. }else if (is < -gFoc_Ctrl.userLim.s_PhaseCurrLim) {
  1136. is = -gFoc_Ctrl.userLim.s_PhaseCurrLim;
  1137. }
  1138. eCtrl_set_TgtCurrent(is);
  1139. return true;
  1140. }
  1141. bool PMSM_FOC_Set_Torque(float trq) {
  1142. if (trq > gFoc_Ctrl.userLim.s_torqueLim) {
  1143. trq = gFoc_Ctrl.userLim.s_torqueLim;
  1144. }else if (trq < -gFoc_Ctrl.userLim.s_torqueLim) {
  1145. trq = -gFoc_Ctrl.userLim.s_torqueLim;
  1146. }
  1147. eCtrl_set_TgtTorque(trq);
  1148. return true;
  1149. }
  1150. void PMSM_FOC_Reset_Torque(void) {
  1151. float real_trq = PMSM_FOC_Get_Real_dqVector();
  1152. eCtrl_reset_Torque(real_trq);
  1153. }
  1154. bool PMSM_FOC_Set_CruiseSpeed(float rpm) {
  1155. if (PMSM_FOC_Is_CruiseEnabled()) {
  1156. if (rpm < CONFIG_MIN_CRUISE_RPM) {
  1157. rpm = CONFIG_MIN_CRUISE_RPM;
  1158. }
  1159. gFoc_Ctrl.in.s_cruiseRPM = min(ABS(rpm), gFoc_Ctrl.userLim.s_motRPMLim)*SIGN(rpm);
  1160. eRamp_set_step_target(&gFoc_Ctrl.in.cruiseRpmRamp, gFoc_Ctrl.in.s_cruiseRPM, CONFIG_eCTRL_STEP_TS);
  1161. return true;
  1162. }
  1163. PMSM_FOC_SetErrCode(FOC_NotCruiseMode);
  1164. return false;
  1165. }
  1166. void PMSM_FOC_MTPA_Calibrate(bool enable) {
  1167. if (enable) {
  1168. gFoc_Ctrl.in.b_MTPA_calibrate = true;
  1169. gFoc_Ctrl.in.s_dqAngle = 0;
  1170. }else {
  1171. gFoc_Ctrl.in.s_dqAngle = INVALID_ANGLE;
  1172. gFoc_Ctrl.in.b_MTPA_calibrate = false;
  1173. }
  1174. }
  1175. void PMSM_FOC_Set_MotAngle(float angle) {
  1176. gFoc_Ctrl.in.s_manualAngle = (angle);
  1177. }
  1178. void PMSM_FOC_Set_Dq_Angle(float angle) {
  1179. gFoc_Ctrl.in.s_dqAngle = (angle);
  1180. }
  1181. void PMSM_FOC_Get_TgtIDQ(DQ_t * dq) {
  1182. dq->d = gFoc_Ctrl.in.s_targetIdq.d;
  1183. dq->q = gFoc_Ctrl.in.s_targetIdq.q;
  1184. }
  1185. float PMSM_FOC_GetSpeed(void) {
  1186. float speed = gFoc_Ctrl.in.s_motVelocity;
  1187. if (!gFoc_Ctrl.in.b_motEnable || foc_observer_is_encoder()) {
  1188. speed = motor_encoder_get_speed();
  1189. }else {
  1190. if (foc_observer_sensorless_stable()) {
  1191. speed = foc_observer_sensorless_speed();
  1192. }else {
  1193. speed = 0;
  1194. }
  1195. }
  1196. return speed;
  1197. }
  1198. void PMSM_FOC_AutoHold(bool lock) {
  1199. if (gFoc_Ctrl.in.b_AutoHold != lock) {
  1200. motor_encoder_lock_pos(lock);
  1201. PI_Controller_Reset(&gFoc_Ctrl.pi_lock, 0);
  1202. if (!lock) {
  1203. float hold_torque = gFoc_Ctrl.in.s_targetTorque * 1.1f;
  1204. if (gFoc_Ctrl.out.n_RunMode == CTRL_MODE_TRQ) {
  1205. #ifdef CONFIG_SPEED_LADRC
  1206. ladrc_reset(&gFoc_Ctrl.vel_lim_adrc, 0, hold_torque);
  1207. #else
  1208. PI_Controller_Reset(&gFoc_Ctrl.pi_vel_lim, hold_torque);
  1209. #endif
  1210. }else if (gFoc_Ctrl.out.n_RunMode == CTRL_MODE_SPD) {
  1211. #ifdef CONFIG_SPEED_LADRC
  1212. ladrc_reset(&gFoc_Ctrl.vel_adrc, 0, hold_torque);
  1213. #else
  1214. PI_Controller_Reset(&gFoc_Ctrl.pi_vel, hold_torque);
  1215. #endif
  1216. }
  1217. eCtrl_reset_Torque(hold_torque);
  1218. gFoc_Ctrl.out.f_autohold_trq = hold_torque;
  1219. }else {
  1220. gFoc_Ctrl.out.f_autohold_trq = 0;
  1221. }
  1222. gFoc_Ctrl.in.b_AutoHold = lock;
  1223. }
  1224. }
  1225. bool PMSM_FOC_AutoHoldding(void) {
  1226. return gFoc_Ctrl.in.b_AutoHold;
  1227. }
  1228. static PI_Controller *_pid(u8 id) {
  1229. PI_Controller *pi = NULL;
  1230. if (id == PID_D_id) {
  1231. pi = &gFoc_Ctrl.pi_id;
  1232. }else if (id == PID_Q_id) {
  1233. pi = &gFoc_Ctrl.pi_iq;
  1234. }else if (id == PID_TRQ_id) {
  1235. #ifndef CONFIG_SPEED_LADRC
  1236. pi = &gFoc_Ctrl.pi_vel_lim;
  1237. #endif
  1238. }else if (id == PID_Spd_id) {
  1239. #ifndef CONFIG_SPEED_LADRC
  1240. pi = &gFoc_Ctrl.pi_vel;
  1241. #endif
  1242. }
  1243. return pi;
  1244. }
  1245. void PMSM_FOC_SetPid(u8 id, float kp, float ki, float kd) {
  1246. if (id > PID_Max_id) {
  1247. return;
  1248. }
  1249. PI_Controller *pi = _pid(id);
  1250. if (pi != NULL) {
  1251. pi->kp = kp;
  1252. pi->ki = ki;
  1253. pi->kd = kd;
  1254. }
  1255. }
  1256. void PMSM_FOC_GetPid(u8 id, float *kp, float *ki, float *kd) {
  1257. if (id > PID_Max_id) {
  1258. return;
  1259. }
  1260. PI_Controller *pi = _pid(id);
  1261. if (pi != NULL) {
  1262. *kp = pi->kp;
  1263. *ki = pi->ki;
  1264. *kd = pi->kd;
  1265. }
  1266. }
  1267. void PMSM_FOC_SetErrCode(u8 error) {
  1268. if (gFoc_Ctrl.out.n_Error != error) {
  1269. gFoc_Ctrl.out.n_Error = error;
  1270. }
  1271. }
  1272. u8 PMSM_FOC_GetErrCode(void) {
  1273. return gFoc_Ctrl.out.n_Error;
  1274. }
  1275. void PMSM_FOC_Set_PlotType(Plot_t t) {
  1276. gFoc_Ctrl.plot_type = t;
  1277. }
  1278. //获取母线电流和实际输出电流矢量大小
  1279. void PMSM_FOC_Calc_Current(void) {
  1280. float vd = gFoc_Ctrl.out.s_OutVdq.d + gFoc_Ctrl.out.s_OutVdqDTC.d * 0.667f;
  1281. float vq = gFoc_Ctrl.out.s_OutVdq.q + gFoc_Ctrl.out.s_OutVdqDTC.q * 0.667f;
  1282. float id = gFoc_Ctrl.out.s_FilterIdq.d;
  1283. float iq = gFoc_Ctrl.out.s_FilterIdq.q;
  1284. /*
  1285. 根据公式(等幅值变换,功率不等):
  1286. iDC x vDC = 2/3(iq x vq + id x vd);
  1287. */
  1288. float m_pow = (vd * id + vq * iq); //s32q10
  1289. float raw_idc = 0.0f;
  1290. float v_dc = get_vbus_float();
  1291. if (v_dc != 0.0f) {
  1292. raw_idc = m_pow / v_dc;// * 1.5f * 0.66f; //s16q5
  1293. }
  1294. LowPass_Filter(gFoc_Ctrl.out.s_CalciDC, raw_idc, 0.02f);
  1295. #ifdef VBUS_I_CHAN
  1296. raw_idc = get_vbus_current();
  1297. LowPass_Filter(gFoc_Ctrl.out.s_FilteriDC, raw_idc, 0.05f);
  1298. #else
  1299. gFoc_Ctrl.out.s_FilteriDC = gFoc_Ctrl.out.s_CalciDC;
  1300. #endif
  1301. gFoc_Ctrl.out.s_RealCurrentFiltered = sqrtf(SQ(gFoc_Ctrl.out.s_FilterIdq.d) + SQ(gFoc_Ctrl.out.s_FilterIdq.q));
  1302. }
  1303. void PMSM_FOC_Brake(bool brake) {
  1304. gFoc_Ctrl.in.b_eBrake = brake;
  1305. if (gFoc_Ctrl.in.b_eBrake & gFoc_Ctrl.in.b_cruiseEna) {
  1306. gFoc_Ctrl.in.b_cruiseEna = false;
  1307. }
  1308. eCtrl_brake_signal(brake);
  1309. }