电机的PID控制
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
#include "DSP28_Device.h"
#include "pmsm3_1.h"
#include "parameter.h"
#include "build.h"
// 函数声明
interrupt void EvaTimer1(void);
interrupt void EvaTimer2(void);
// 全局变量定义
float Vd_testing = 0; /* Vd testing (pu) */
float Vq_testing = 0.25; /* Vq testing (pu) */
float Id_ref = 0; /* Id reference (pu) */
float Iq_ref = 0.4; /* Iq reference (pu) */
float speed_ref = 0.2; /* Speed reference (pu) */
float T = 0.001/ISR_FREQUENCY; /* Samping period (sec), see parameter.h */
int isr_ticker = 0;
int pwmdac_ch1=0;
int pwmdac_ch2=0;
int pwmdac_ch3=0;
volatile int enable_flg=0;
int lockrtr_flg=1;
int speed_loop_ps = 10; // 速度环定标器
int speed_loop_count = 1; // 速度环计数器
CLARKE clarke1 = CLARKE_DEFAULTS;
PARK park1 = PARK_DEFAULTS;
IPARK ipark1 = IPARK_DEFAULTS;
PIDREG3 pid1_id = PIDREG3_DEFAULTS;
PIDREG3 pid1_iq = PIDREG3_DEFAULTS;
PIDREG3 pid1_spd = PIDREG3_DEFAULTS;
PWMGEN pwm1 = PWMGEN_DEFAULTS;
PWMDAC pwmdac1 = PWMDAC_DEFAULTS;
SVGENDQ svgen_dq1 = SVGENDQ_DEFAULTS;
QEP qep1 = QEP_DEFAULTS;
SPEED_MEAS_QEP speed1 = SPEED_MEAS_QEP_DEFAULTS;
DRIVE drv1 = DRIVE_DEFAULTS;
RMPCNTL rc1 = RMPCNTL_DEFAULTS;
RAMPGEN rg1 = RAMPGEN_DEFAULTS;
ILEG2DCBUSMEAS ilg2_vdc1 = ILEG2DCBUSMEAS_DEFAULTS; // 主函数
void main(void)
{
// 系统初始化
InitSysCtrl();
// HISPCP 设置
EALLOW;
SysCtrlRegs.HISPCP.all = 0x0000; /* SYSCLKOUT/1 */ EDIS;
// 禁止并清除所有CPU中断:
DINT;
IER = 0x0000;
IFR = 0x0000;
// 初始化Pie到默认状态
InitPieCtrl();
// 初始化PIE相量表
InitPieVectTable();
// 初始化EV A 定时器1:
//设置定时器1寄存器(EV A)
EvaRegs.GPTCONA.all = 0;
//等待使能标志位
while (enable_flg==0)
{
// 使能定时器1的下溢中断
EvaRegs.EV AIMRA.bit.T1UFINT = 1;
EvaRegs.EV AIFRA.bit.T1UFINT = 1;
// 使能CAP3中断(定时器2)
EvaRegs.EV AIMRC.bit.CAP3INT = 1;
EvaRegs.EV AIMRC.bit.CAP3INT = 1;
};
// 重新分配中断向量
EALLOW;
PieVectTable.T1UFINT = &EvaTimer1;
PieVectTable.CAPINT3 = &EvaTimer2;
EDIS;
// 使能PIE组2的中断6(T1UFINT)
PieCtrlRegs.PIEIER2.all = M_INT6;
// 使能PIE组3的中断7(CAPINT3)
PieCtrlRegs.PIEIER3.all = M_INT7;
// 使能CPU INT2(T1UFINT)和INT3(CAPINT3):
IER |= (M_INT2 | M_INT3);
// 使能全局中断和最高优先级适时调试事件管理器功能:
EINT; //使能全局中断INTM
ERTM; // 使能适时调试中断DBGM
/* 模块初始化*/
pwm1.n_period = SYSTEM_FREQUENCY*1000000*T/2; /* 预定标器X1 (T1), ISR周期= T x 1 */
pwm1.init(&pwm1);
pwmdac1.pwmdac_period = 2500; /* PWM频率= 30 kHz */
pwmdac1.PWM_DAC_IPTR0 = &pwmdac_ch1;
pwmdac1.PWM_DAC_IPTR1 = &pwmdac_ch2;
pwmdac1.PWM_DAC_IPTR2 = &pwmdac_ch3;
pwmdac1.init(&pwmdac1);
qep1.init(&qep1);
drv1.init(&drv1);
ilg2_vdc1.init(&ilg2_vdc1);
/* 初始化SPEED_FRQ模块*/
speed1.K1 = _IQ21(1/(BASE_FREQ*T));
speed1.K2 = _IQ(1/(1+T*2*PI*30)); /* 低通截至频率= 30 Hz */
speed1.K3 = _IQ(1)-speed1.K2;
speed1.rpm_max = 120*BASE_FREQ/P;