Andreas Steffen / Mbed 2 deprecated PIservo

Dependencies:   mbed

main.cpp

Committer:
Reignbow
Date:
2012-08-07
Revision:
3:1a4ad4c82046
Parent:
2:6d472c554758
Child:
4:c38f7e4e8fda

File content as of revision 3:1a4ad4c82046:

/* PIservo
 implement a PI controller with USB serial gain setting to stabilize dipole trap power.
 Two analog inputs (control and sensor), one analog output (to RF VCA).

20120806 Andreas Steffen
*/


#include "mbed.h"

DigitalOut adcCS(p8);
DigitalOut cyc(p10);
AnalogOut vca(p18);
SPI adc(p5,p6,p7); //talk to ad7921
Serial pc(USBTX, USBRX);


Timer t;
Ticker commTick;
Ticker intTick;

int pGain = 1;
int iGain = 100;
int integrator = 0; //the building up integrator value

void serialComm(){
    char c;
    int i;
    
    if (pc.readable()) { 
        c = pc.getc();
        pc.scanf("%i", &i);
        switch (c) {
            case 'p':
                pGain = i;   
                pc.printf("Changed p gain to %i.\n",pGain);
                break;
            case 'i':
                iGain = i;   
                pc.printf("Changed i gain to %i.\n",iGain);
                if (i==0)
                    integrator = 0;
                break;
            default:
                pc.printf("Command not understood.\n",iGain);
                pc.printf("Read %c and %i.\n",c,i);
                break;
        }
    }
}


int main() {
    int ctl=0;
    int pd=0;
    int err=0;
    pc.printf("mbed restarted!\n");
    
    adc.format(16,2);
    adc.frequency(5000000); 
    
    commTick.attach(&serialComm,0.5); //check serial every half second    
    cyc = 0;    

    while(1) {
        cyc = cyc^1; //toggle debug pin
        
        //stagger read and output
        //The SPI read takes a long time, so start it by writing to the data
        //register, do sth. else (create new output), then get the value 
        //and start the read from the other channel
       
       //begin SPI transfer
        adcCS = 0;
        LPC_SSP1->DR = 3<<13; //initialize SSP1 transfer by writing to data register; select ch 1
        
        err = ctl - pd; //convert to volt   
        integrator += err*iGain; 
        t.reset(); //reset timer to get next integration time       
        vca.write_u16( (err * pGain  + integrator)>>10 );        //analog output       
        
        //read SPI transfer results
        while(!(LPC_SSP1->SR & 0x04)) {}
        pd =  LPC_SSP1->DR & 0xFFF; //last 12 bits are data
        adcCS = 1;      
        
        //begin transfer from other channel
        adcCS = 0;
        LPC_SSP1->DR = 1; //initialize SSP1 transfer by writing to data register; select ch 0
        
        err = ctl-pd; //convert to volt   
        integrator += err*iGain;   
        vca.write_u16( (err * pGain  + integrator)>>10 );        //analog output
        
        //read SPI transfer results
        while(!(LPC_SSP1->SR & 0x04)) {}
        ctl = LPC_SSP1->DR & 0xFFF; //last 12 bits are data
        adcCS = 1;      
        
        //pc.printf("Measuring ctl=%i and pd=%i, err=%i, integrator= %i\n",ctl,pd,err,integrator);
        
    }
}