当前位置:文档之家› 直流电机PID控制

直流电机PID控制

请高手指点一下,RMPCNTL,RMP2,SPEED_PR,DATALOG这几个模块到底是什么意思啊?#include "DSP2833x_Device.h"
#include "IQmathLib.h"
#include "bldc3_1.h"
#include "parameter.h"
#include "build.h"
#include
#define LED (*(unsigned short int *)0x180000)
// Prototype statements for functions found within this file.
interrupt void MainISR(void);

void Delay(unsigned int nDelay);

// Create an instance of the ADC driver
long lnADCValues;
int curadcvalue=0;
int voladcvalue=0;
int16 nADCValue;
float32 fSpeedSet=0.2;
unsigned int uWork=0;
int nWork=0,nWork1;

// Global variables used in this system
float32 SpeedRef = 0.50; // Speed reference (pu)速度参考值(标幺值)
float32 T = 0.001/ISR_FREQUENCY; // Samping period (sec), see parameter.h PWM频率20KHZ,周期50us

Uint32 VirtualTimer = 0;
Uint16 ILoopFlag = FALSE;
Uint16 SpeedLoopFlag = FALSE;
int16 DFuncDesired = 7000;//0x03d7; // Desired duty cycle (Q15)

float32 CurrentSet = 0.0031;

Uint16 IsrTicker = 0;
Uint16 BackTicker = 0;

Uint16 hallcap=0;
int32 speedcur=0;
int i=0;
float zkb=0.0;
int32 Tmp;
int16 Period, GPR0_BLDC_PWM;
int16 DlogCh1 = 0;
int16 DlogCh2 = 0;
int16 DlogCh3 = 0;
int16 DlogCh4 = 0;

Uint16 uISRWork;
int16 nISRWork;
int16 nISRWork1;
int16 nNowStatus=0;
Uint16 uISRWork1=3300;
Uint16 uISRWork2;
Uint16 bISRWork;
int16 md=0.2,mdst=0;

int16 nHallABC[6]={4,0,5,2,3,1};//{ 4,2,3,0,5,1 };

//Uint16
zhengzhuan[6]={4,0,5,2,3,1};
//Uint16 fanzhuan[6]={2,4,3,0,1,5};
Uint16
zhengzhuan[6]={4,0,5,2,3,1};
Uint16 fanzhuan[6]={1,3,2,5,0,4};

volatile Uint16 EnableFlag = FALSE;

// Instance PID regulator to regulate the DC-bus current and speed
PIDREG3 pid1_idc = PIDREG3_DEFAULTS;//采用PID控制器调节无刷直流电动机的电流和速度
PIDREG3 pid1_spd = PIDREG3_DEFAULTS;

// Instance a PWM driver instance
PWMGEN pwm1 = PWMGEN_DEFAULTS;

// Create an instance of the ADC driver
ADCVALS adc1 = ADCVALS_DEFAULTS;

// Instance a enable PWM drive driver (only for DMC1500)
//DRIVE drv1 = DRIVE_DEFAULTS;

// Instance a Hall effect driver
HALL3 hall1 = HALL3_DEFAULTS;

// Instance a ramp controller to smoothly ramp the frequency
RMPCNTL rc1 = RMPCNTL_DEFAULTS;//定义斜波控制器平滑斜坡信号

// Instance a RAMP2 Module
RMP2 rmp2 = RMP2_DEFAULTS;// RAMP2模块初始化

// Instance a MOD6 Module
MOD6CNT mod1 = MOD6CNT_DEFAULTS;//MOD6CNT 模块初始化

// Instance a SPEED_PR Module
SPEED_MEAS_CAP speed1 = SPEED_MEAS_CAP_DEFAULTS;//SPEED_PR模块初始化

// Create an instance of DATALOG Module
DLOG_4CH dlog = DLOG_4CH_DEFAULTS; //创建并初始化DATALOG模块

void main(void)
{

// Initialize System Control registers, PLL, WatchDog, Clocks to default state:
// This function is found in the DSP280x_SysCtrl.c file.

InitSysCtrl();//关闭看门狗,设置PLL(5*30MHZ);设置并打开默认高低速外设时钟

// Glob

ally synchronize all ePWM modules to the time base clock (TBCLK)
EALLOW;
SysCtrlRegs.PCLKCR0.bit.TBCLKSYNC = 1;
EDIS;//禁止并清除所有CPU中断

// HISPCP prescale register settings, normally it will be set to default values
EALLOW; // This is needed to write to EALLOW protected registers
SysCtrlRegs.HISPCP.all = 0x0000; // SYSCLKOUT/1 设置高速外设时钟150MHZ
EDIS; // This is needed to disable write to EALLOW protected registers

// Disable and clear all CPU interrupts:

DINT;

IER = 0x0000;

IFR = 0x0000;//填充中断向量表

// Initialize Pie Control Registers To Default State:
// This function is found in the DSP280x_PieCtrl.c file.

InitPieCtrl();//DINT 不能使PIE 清除12组PIEIER 和PIEIFR

// Initialize the PIE Vector Table To a Known State:
// This function is found in DSP280x_PieVect.c.

// This function populates the PIE vector table with pointers
// to the shell ISR functions found in DSP280x_DefaultIsr.c.

InitPieVectTable();
//把中断向量表区装满指向形式ISR的指针 使能PIE
InitXintf16Gpio();

// User specific functions, Reassign vectors (optional), Enable Interrupts:

// Waiting for enable flag set
// while (EnableFlag==FALSE)
{
BackTicker++;
}

// Initialize all the Device Peripherals:
// This function is found in DSP280x_CpuTimers.c
InitCpuTimers();

// Configure CPU-Timer 0 to interrupt every ISR Period:
// 100MHz CPU Freq, ISR Period (in uSeconds)
// This function is found in DSP280x_CpuTimers.c
ConfigCpuTimer(&CpuTimer0, 100, SYSTEM_FREQUENCY*10/ISR_FREQUENCY);
StartCpuTimer0();

// Reassign ISRs.
// Reassign the PIE vector for TINT0 to point to a different
// ISR then the shell routine found in DSP280x_DefaultIsr.c.
// This is done if the user does not want to use the shell ISR routine
// but instead wants to use their own ISR.


EALLOW;
// This is needed to write to EALLOW protected registers

PieVectTable.TINT0 = &MainISR;

EDIS; // This is needed to disable write to EALLOW protected registers

// Enable PIE group 1 interrupt 7 for TINT0
PieCtrlRegs.PIEIER1.all = M_INT7;

// Enable CPU INT1 for TINT0:

IER |= M_INT1;

// Initialize PWM module
pwm1.PeriodMax = (SYSTEM_FREQUENCY/PWM_FREQUENCY)*1000; // Asymmetric PWM
pwm1.DutyFunc = DFuncDesired; // DutyFunc = Q15
pwm1.init(&pwm1);

// Initialize DATALOG module 初始化 DATALOG 模块.
dlog.iptr1 = &DlogCh1;
dlog.iptr2 = &DlogCh2;
dlog.iptr3 = &DlogCh3;
dlog.iptr4 = &DlogCh4;
dlog.trig_value = 0x01;
dlog.size = 0x400;
dlog.prescalar = 1;
dlog.init(&dlog);

// Initialize ADC module
adc1.ChSelect = 0x7710;//6543; // for DMC1500 and eZdsp2812/eZdsp2808 boards

adc1.init(&adc1);

// Initialize enable drive module
// drv1.init(&drv1);


// Initialize the SPEED_PR module (150 MHz, N = 1 event period/rev)

speed1.InputSelect = 0;

speed1.BaseRpm = 6000;//120*(BASE_FREQ/P);//电同步转速

speed1.SpeedScaler = (Uint32)(ISR_FREQUENCY/(1*BASE_FREQ*0.001));

// Initialize RMPCNTL module
rc1.RampDelayMax = 20;
rc1.RampLowLimit = (0.001);
rc1.RampHighLimit = (0.9);

// Initialize Hall module
hall1.DebounceAmount = 5;
hall1.Revolutions = -10;
hall1.init(&hall1);

// Initialize RMP2 module

rmp2.Out = (int32)DFuncDesired;

rmp2.Ramp2Delay = 0x00000050;
rmp2.Ramp2Max = 0x00007FFF;
rmp2.Ramp2Min = 0x0000000F;

// Initialize the PID_REG3 module for dc-bus current
pid1_idc.Kp = (1);

pid1_idc.Ki = (T/0.003);


pid1_idc.Kd = (0/T);


pid1_idc.Kc = (0.2);
pid1_idc.OutMax = (0.99);
pid1_idc.OutMin = (0);

// Initialize the PID_REG3 module for speed
pid1_spd.Kp = (0.8);

pid1_spd.Ki = (T/0.1);

pid1_spd.Kd = (0/T);

pid1_spd.Kc = (0.2);
pid1_spd.OutMax = (0.55);
pid1_spd.OutMin = (-0.45);

// Enable global Interrupts and higher priority real-time debug events:

EINT; // Enable Global interrupt INTM

ERTM;
// Enable Global realtime interrupt DBGM

// IDLE loop. Just sit and loop forever:


for(;;)

{

BackTicker++;


}
}

unsigned int www=0;
interrupt void MainISR(void)
{

// Verifying the ISR
IsrTicker++;
// ------------------------------------------------------------------------------
// Call the ADC04U_DRV read function.
// ------------------------------------------------------------------------------

/* adc1.read(&adc1);

voladcvalue=adc1.Ch1Out;


curadcvalue=adc1.Ch2Out;*/
// ------------------------------------------------------------------------------
// Connect inputs of the RMP module and call the Ramp control
// calculation function.
// ------------------------------------------------------------------------------
rc1.TargetValue = (SpeedRef);
rc1.calc(&rc1);

// ------------------------------------------------------------------------------
// Connect inputs of the HALL module and call the Hall sensor
// read function.
// ------------------------------------------------------------------------------
hall1.HallMapPointer = (int16)mod1.Counter;
hall1.read(&hall1);

hallcap=hall1.HallGpio;
// ------------------------------------------------------------------------------
// Connect inputs of the MOD6 module and call the Modulo 6 counter
// calculation function.
// ------------------------------------------------------------------------------
mod1.TrigInput =(int32)hall1.CmtnTrigHall;
mod1.Counter = (int32)hall1.HallMapPointer;
mod1.calc(&mod1);

// ------------------------------------------------------------------------------
// Connect inputs of the RMP2 module and call the Ramp control 2
// calculation function.


// ------------------------------------------------------------------------------
rmp2.DesiredInput = (int32)DFuncDesired;
rmp2.calc(&rmp2);

// ------------------------------------------------------------------------------
// Connect inputs of the PID_REG3 module and call the PID speed controller
// calculation function.
// ------------------------------------------------------------------------------
pid1_spd.Ref = rc1.SetpointValue;
pid1_spd.Fdb = speed1.Speed;
pid1_spd.calc(&pid1_spd);

// Check PwmActive setting

if (pwm1.PwmActive==1) // PWM active high
GPR0_BLDC_PWM = 0x7FFF - pwm1.DutyFunc;
else if (pwm1.PwmActive==0) // PWM active low
GPR0_BLDC_PWM = pwm1.DutyFunc;

// Convert "DutyFunc" or "GPR0_BLDC_PWM" (Q15) duty modulation function to Q0
Tmp = (int32)pwm1.PeriodMax*(int32)GPR0_BLDC_PWM; // Q15 = Q0*Q15
// Q15 -> Q

i=(int16)(Tmp>>15);
zkb= (15000.0-i)/ 15000.0;//计算占空比
// ------------------------------------------------------------------------------
// Set the speed closed loop flag once the speed is built up to a desired value.
// ------------------------------------------------------------------------------
if (rc1.EqualFlag == 0x7FFFFFFF) {
SpeedLoopFlag = TRUE; //开环时要注释掉,闭环工作时要打开。
rc1.RampDelayMax = 300;
}

// ------------------------------------------------------------------------------
// Connect inputs of the PWM_DRV module and call the PWM signal generation
// update function.
// ------------------------------------------------------------------------------
// Switch from fixed duty-cycle or controlled Speed duty-cycle by SpeedLoopFlag variable
if (SpeedLoopFlag == FALSE)
pwm1.DutyFunc = (int16)rmp2.Out; // fixed duty-cycle
else
pwm1.DutyFunc = (int16)((pid1_spd.Out)*pwm1.PeriodMax)+DFuncDesired; // controlled Speed duty-cycle

// pwm1.CmtnPointer = (int16)mod1.Counter;

pwm1.CmtnPointer = zhengzhuan[hallcap-1];
//
pwm1.CmtnPointer = fanzhuan[hallcap-1];
pwm1.update(&pwm1);


// ------------------------------------------------------------------------------
// Connect inputs of the SPEED_PR module and call the speed calculation
// function.
// ------------------------------------------------------------------------------
if ((mod1.Counter == 5)&(hall1.CmtnTrigHall == 0x7FFF)) {
speed1.TimeStamp = VirtualTimer;
speed1.calc(speed1);

speedcur=speed1.SpeedRpm*2;
}


// ------------------------------------------------------------------------------
// Connect inputs of the DATALOG module
// ------------------------------------------------------------------------------
DlogCh1 = (int16)mod1.Counter;
DlogCh2 = (int16)mod1.TrigInput;

DlogCh3 = (int16)(pid1_spd.Ref);
DlogCh4 = (int16)(pid1_spd.Fdb);


// ------------------------------------------------------------------------------
// Increase virtual timer and force 15 bit wrap around
// ------------------------------------------------------------------------------

VirtualTimer++;

VirtualTimer &= 0x00007FFF;

if(VirtualTimer == 0x00007FFF)

{
LED^=1;
}

// ------------------------------------------------------------------------------
// Call the DATALOG update function.
// ------------------------------------------------------------------------------
dlog.update(&dlog);

// Acknowledge interrupt to recieve more interrupts from PIE group 1

PieCtrlRegs.PIEACK.all = PIEACK_GROUP1;
}

void Delay(unsigned int nDelay)
{

int ii,jj,kk=0;

for ( ii=0;ii
{

for ( jj=0;jj<1024;jj++ )

{

kk++;

}

}
}


相关主题
文本预览
相关文档 最新文档