TIMotorLIBExampleSw/main.c

This is an example on how to use some of the control and signal processing utilities.

//-----------------------------------------------------------
// Includes
// -------------------------------------------------------------
#include "ExampleSw.h"  // includes for our particular project

//-----------------------------------------------------------
// Define
//-----------------------------------------------------------
#define VBUS                  50
// A/D #defines
#define IUR_CH                0                   // iu (RECTIFIER) A/D channel
#define IVR_CH                0                   // iv (RECTIFIER) A/D channel
#define VUVR_CH               2                   // vuv (RECTIFIER) A/D channel
#define VVWR_CH               2                   // vvw (RECTIFIER) A/D channel
#define VBUS1_CH    1                   // Vbus A/D channel
#define IUI_CH                3                   // iu (INVERTER) A/D channel
#define IVI_CH                3                   // iv (INVERTER) A/D channel
#define VUVI_CH               4                   // vuv (INVERTER) A/D channel
#define VVWI_CH               4                   // vvw (INVERTER) A/D channel

//-----------------------------------------------------------
// Implemented Functions
// -------------------------------------------------------------
interrupt void ADInterrupt(void);
interrupt void ePWMAInterrupt(void);
interrupt void ePWMBInterrupt(void);
interrupt void QEP1Interrupt(void);
interrupt void Timer0Interrupt(void);

//-----------------------------------------------------------
// Globals
//-----------------------------------------------------------
const Uint16 HSPCLK = 1;                // high speed preescaler divider
const Uint16 LSPCLK = 1;                // low speed preescaler divider
const _iq Vbus = _IQ(VBUS);             // bus voltage
const Uint16 TPWMA_US = 100;  // EPWMA period (us)
const Uint16 TPWMB_US = 100;  // EPWMB period (us)
const Uint16 DB_US = 3;                           // PWM dead time (us)
const _iq F_AD = _IQ(18.75);  // ADC clk (Mhz)
const Uint16 T_SH = 500;                // ADC Sample & Hold window width (ns)
const Uint16 T_QEP = 10000;             // QEP UTE period (us)
const Uint16 ENCODER_LINES=1024;// encoder lines
const Uint16 POLE_PAIRS=2;              // machine pole pairs
const Uint32 T_T0=10000;                // Timer 0 period (us)
volatile _iq vas, vbs, vcs;             // V/Hz generated voltages
const _iq fvhz=_IQ(10);                           // V/Hz frequency
const _iq fmax = _IQ(50);               // max frequency

// A/D variables
volatile _iq iuI, ivI;
volatile _iq iuR, ivR, vuvI, vvwI, icL;           // measured currents
volatile _iq vuvL, vvwL;                                              // measured voltages
volatile _iq vbus;                                                              // measured vbus

// realizable references
volatile _iq r = _IQ(0);                                                // reference
commandsData* r_cmd;                                                    // reference command (ramp)
commandsData* r_spline_cmd;                                             // reference command (s-pline)
volatile unsigned char init_command = 0;
DECLARE_DATA(e);
DECLARE_DATA(u); 
DECLARE_DATA(usat); 
DECLARE_DATA(i_sim);
const _iq b0r = _IQ(1.781283034585413e+002);
const _iq b1r = _IQ(-1.737300737435156e+002);
const _iq a1s = _IQ(0.975309912028333);
const _iq b0s = _IQ(0.0);
const _iq b1s = _IQ(0.004938017594333);
const _iq umax = _IQ(100.0);
const _iq kp = _IQ(1.759291886010284e+002);
const _iq ki = _IQ(250.0);

// encoder
Uint16 qep1Counter;


//-----------------------------------------------------------

interrupt void ADInterrupt(void)
{
        ToggleGPIOPort(_GPIO32);
        
        // R/L system simulation for evaluation of realizable references
        WriteIQData(&r);
        WriteIQData(&(e[N]));
          WriteIQData(&(u[N]));
          WriteIQData(&(usat[N]));
          WriteIQData(&(i_sim[N]));
          UPDATE_STATE(e);
        UPDATE_STATE(u);
        UPDATE_STATE(usat);
        UPDATE_STATE(i_sim);
        if(init_command){
                // update ramp commands
                UpdateCommand(r_cmd);
        }
        e[N] = r - i_sim[N1];
        // PI regulator
    u[N] = u[N1] + _IQmpy(e[N],b0r) + _IQmpy(e[N1],b1r);
    RealizableReference(r, i_sim, e, u, umax, kp, ki);
    // actuator saturation
    if(_IQabs(u[N])>=umax)
        usat[N] = _IQmpy(umax,_IQ(SIGN(u[N])));
    else
        usat[N] = u[N];
        // system simulation
        i_sim[N] = _IQmpy(i_sim[N1],a1s) + _IQmpy(usat[N],b0s) + _IQmpy(usat[N1],b1s);
        
        // reading of AD channels
        vbus = ReadADInput(ADCINA0);
        iuR  = ReadADInput(ADCINA1);
          ivR  = ReadADInput(ADCINB1);
          iuI  = ReadADInput(ADCINA2);
          ivI  = ReadADInput(ADCINB2);
          vuvL = ReadADInput(ADCINA3);
          vvwL = ReadADInput(ADCINB3);  
          vuvI = ReadADInput(ADCINA4);
          vvwI = ReadADInput(ADCINB4);
          // prepare next adquisition
          UpdatePWMADuty3ph(_IQ(0.4),_IQ(0.6),_IQ(0.8));
          ReinitADSEQ1();
}

//-----------------------------------------------------------

//-----------------------------------------------------------
// ISR on PWMA
//-----------------------------------------------------------
interrupt void ePWMAInterrupt(void)
{
          // process .....
          ToggleGPIOPort(_GPIO18);
          // generate phase voltages under v/hz strategy
          VHz(fvhz, &vas, &vbs, &vcs);
          // zero sequence injection
          HomopolarInjection(&vas, &vbs, &vcs);
          // Acknowledge this interrupt
          EpwmAPIEAck();
}

//-----------------------------------------------------------

//-----------------------------------------------------------
// ISR on PWMB
//-----------------------------------------------------------
interrupt void ePWMBInterrupt(void)
{
          // process .....
          //ToggleGPIOPort(_GPIO19);
          UpdatePWMBDuty3ph(_IQ(0.2),_IQ(0.4),_IQ(0.6));
          // Acknowledge this interrupt
          EpwmBPIEAck();
}

//-----------------------------------------------------------

// ----------------------------------------------------------------------------
// ISR on QEP1
// ----------------------------------------------------------------------------
interrupt void QEP1Interrupt(void)
{
          // process .....
          ToggleGPIOPort(_GPIO19);
          // calculate velocity and position
          QEP1VelocityAndPosition();
          Eqep1PIEAck();
}

//-----------------------------------------------------------

// ----------------------------------------------------------------------------
// ISR on Timer0
// ----------------------------------------------------------------------------
interrupt void Timer0Interrupt(void)
{
        //ToggleGPIOPort(_GPIO19);
        init_command = 1;
        // Acknowledge this interrupt to receive more interrupts from group 1
        Timer0PIEAck();
}

// ----------------------------------------------------------------------------

//-----------------------------------------------------------
// main
// -------------------------------------------------------------
void main(void)
{
          // Enable GPIO outputs on GPIO18, 19, 30, 32 set it high
          // GPIO18
          ConfigureGPIOPort(_GPIO18, GPIO_OUTPUT, GPIO, GPIO_PULL_EN, GPIO_HIGH);
          // GPIO19
          ConfigureGPIOPort(_GPIO19, GPIO_OUTPUT, GPIO, GPIO_PULL_EN, GPIO_HIGH);
          // GPIO30
          ConfigureGPIOPort(_GPIO30, GPIO_OUTPUT, GPIO, GPIO_PULL_EN, GPIO_HIGH);
          // GPIO32
          ConfigureGPIOPort(_GPIO32, GPIO_OUTPUT, GPIO, GPIO_PULL_EN, GPIO_HIGH);

          // SYSCLKOUT = 150Mhz
          // HSPCLK = SYSCLKOUT
          // LSPCLK = SYSCLKOUT
          InitCPU(OSCCLK, DSP28_CLKINDIV, DSP28_PLLCR, HSPCLK, LSPCLK);

          // Init three phase PWMA
          Init3phPWM(ePWMAInterrupt, EPWMA, TPWMA_US, DB_US, TRIANGLE, SYMMETRICAL, 
                    CTR_ZERO, Vbus, SOCA, DB_ACTV_HIC);
          // Init three phase PWMB
          Init3phPWM(ePWMBInterrupt, EPWMB, TPWMB_US, DB_US, TRIANGLE, SYMMETRICAL, 
                    CTR_ZERO, Vbus, SOCNULL, DB_ACTV_LOC);
  
          // AD initialization
          ConfigureAD(ADInterrupt, F_AD, T_SH, CASCADED_SEQ_MODE, SIMULTANEOUS_MODE, PWM_SOC, 0);

          // ------------------ Add AD channels
          // ------------------- Current Sensor data -----------------------------
          // loops = 2;
          // rt = 1/1000;
          // Rm = 150;     (143 measured between input and GND)
          // op_gain = 0.5;
          // MAXBITS = 4096;
          // Vmax = 3
          // GAIN: % 1/(loops*rt_sensor*Rm*op_gain*MAXBITS/Vmax)
          // ------------------- Current Sensor data------ ------------------------

          AddChannel(ADCINA1, IUR_CH, _IQ(6.389431840973869), _IQ(0.0388196));  // iu (RECTIFIER) current
          AddChannel(ADCINB1, IVR_CH, _IQ(6.071838412173394), _IQ(0.0388196));  // iv (RECTIFIER) current
          AddChannel(ADCINA2, IUI_CH, _IQ(4.4377), _IQ(0.03319049));                                // iu (INVERTER) current
          AddChannel(ADCINB2, IVI_CH, _IQ(5.2879), _IQ(0.03319049));                                // iv (INVERTER) current
          AddChannel(ADCINA0, VBUS1_CH, _IQ(5.081364916335725e+02), _IQ(0.647901387693130));                                      // vbus 1 voltage
          AddChannel(ADCINA3, VUVR_CH, _IQ(55.984477040846890), _IQ(0.321979324581005));                                // vuv (RECTIFIER) voltage
          AddChannel(ADCINB3, VVWR_CH, _IQ(52.629935447699701), _IQ(0.321979324581005));                                // vvw (RECTIFIER) voltage
          AddChannel(ADCINA4, VUVI_CH, _IQ(48.583587866666690), _IQ(0.3228));                                 // vuv (INVERTER) voltage
          AddChannel(ADCINB4, VVWI_CH, _IQ(50.492447733333343), _IQ(0.3228));                                 // vvw (INVERTER) voltage

          // Enable trigger to store AD samples
          SetTrigger();       
          // Configure Memory to store AD samples (OVERWRITE/NOVERWRITE)
          InitDataStorageBuffer(NOVERWRITE);

          // QEP1
          ConfigureQEP1(QEP1Interrupt, T_QEP, PCRM_IE, ENCODER_LINES, POLE_PAIRS);

          // CPU timers                                                                                                 
          // Timer structure, T(us), timer ISR
          ConfigureCpuTimer(CPUTIMER0, T_T0, Timer0Interrupt);
          
          // Enable QEP
          EnableQEP1();
          // Start timer
          StartCpuTimer0();   
          // Enable PWM clock
          EnableEPWMModuleTBCLK();

        // Init Vhz
        InitVHz(Vbus, fmax, DT);
        // Command shaping
        ConfigureCommandGenerator(_IQ(100),_IQ(0.0002));
        RegisterCommand(_IQ(0), _IQ(10), _IQ(1000), &r, &r_cmd, &RampCommandFcn,0);
        RegisterCommand(_IQ(0), _IQ(10), _IQ(1000), &r, &r_spline_cmd, &SCommandFcn,0);
        StartCommand(r_cmd);

        // enable PIE module and CPU interrupts
        EnableInterrupts();     // <-- PABLO: This call MUST BE the last one
          // main loop
          while(1){
          IDLE;
  }
}
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines
Generated on Sat Apr 16 11:42:43 2011 for TIMotorLIB by  doxygen 1.6.3