PMSM_FOC_Core.c 42 KB

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