Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Diff: main.cpp
- Revision:
- 2:6d472c554758
- Parent:
- 1:dc799997391e
- Child:
- 3:1a4ad4c82046
--- a/main.cpp Mon Aug 06 17:24:16 2012 +0000 +++ b/main.cpp Tue Aug 07 12:07:32 2012 +0000 @@ -9,6 +9,7 @@ #include "mbed.h" DigitalOut adcCS(p8); +DigitalOut cyc(p10); AnalogOut vca(p18); SPI adcSPI(p5,p6,p7); //talk to ad7921 Serial pc(USBTX, USBRX); @@ -19,21 +20,13 @@ Ticker intTick; float pGain = 0.5; -float iGain = 5000.; -float integrator = 0; //the building up integrator value - -//for keeping track of the cycle rate -float cycT=0; -float cycN=0; +float iGain = 1000.; +float integrator = 0.; //the building up integrator value void serialComm(){ char c; float f; - pc.printf("Average cycle time [us]: %f\n",cycT/cycN); - cycT = 0; - cycN = 0; - if (pc.readable()) { c = pc.getc(); pc.scanf("%f", &f); @@ -45,6 +38,8 @@ case 'i': iGain = f; pc.printf("Changed i gain to %f.\n",iGain); + if (f==0) + integrator = 0.; break; default: pc.printf("Command not understood.\n",iGain); @@ -58,48 +53,49 @@ if (integrator > 3.3) { integrator = 3.3; } - if (integrator < -3.3) { - integrator = -3.3; + if (integrator < 0) { + integrator = 0; } } int main() { - float err=0.; + float err=0; + float out=0.; int ctl=0; int pd=0; pc.printf("mbed restarted!\n"); - adcSPI.format(16,3); - adcSPI.frequency(2000000); //2MHz + adcSPI.format(16,2); + adcSPI.frequency(5000000); - //commTick.attach(&serialComm,0.5); //check serial every half second - //intTick.attach(&limitIntegrator,0.001); //check integrator overrun every ms + commTick.attach(&serialComm,0.5); //check serial every half second + intTick.attach(&limitIntegrator,0.001); //check integrator overrun every ms t.start(); - + cyc = 0; while(1) { + cyc = cyc ^ 1; //toggle debug pin + //read in from AD7921 over SPI adcCS = 0; - ctl = adcSPI.write(3<<13); //select ch 1 + pd = adcSPI.write(3<<13); //select ch 1 + pd = (pd) & 0xFFF; adcCS = 1; - wait_us(20); + adcCS = 0; - pd = adcSPI.write(1); // select ch 0 + ctl = adcSPI.write(1); // select ch 0 //take the last 12 bits that carry data - // for some reason, it seems shifted up by one... - pd = (pd>>1) & 0xFFF; - ctl = (ctl>>1) & 0xFFF; - pc.printf("Read %i from V0 and %i from V1.\n",ctl,pd); - adcCS = 1; - wait_ms(100); - /* err = ctl.read() - pd.read(); - cycT += t.read_us(); - cycN += 1.; - integrator += err * iGain * t.read_us(); + ctl = (ctl) & 0xFFF; + adcCS = 1; + + err = ctl*3.3/4095. - pd*3.3/4095.; //convert to volt + integrator += err * iGain*1.e-6 * t.read_us(); t.reset(); //reset timer to get next integration time - vca = err * pGain + integrator; //analog output + out = 1.0* err * pGain + integrator; //analog output + vca.write(out); - */ + // pc.printf("Measuring ctl=%i and pd=%i, err=%f, vca is %f\n",ctl,pd,err,out); + } }