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.
main.cpp
- Committer:
- Reignbow
- Date:
- 2012-08-07
- Revision:
- 2:6d472c554758
- Parent:
- 1:dc799997391e
- Child:
- 3:1a4ad4c82046
File content as of revision 2:6d472c554758:
/* 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 adcSPI(p5,p6,p7); //talk to ad7921 Serial pc(USBTX, USBRX); Timer t; Ticker commTick; Ticker intTick; float pGain = 0.5; float iGain = 1000.; float integrator = 0.; //the building up integrator value void serialComm(){ char c; float f; if (pc.readable()) { c = pc.getc(); pc.scanf("%f", &f); switch (c) { case 'p': pGain = f; pc.printf("Changed p gain to %f.\n",pGain); break; 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); pc.printf("Read %c and %f.\n",c,f); break; } } } void limitIntegrator(){ if (integrator > 3.3) { integrator = 3.3; } if (integrator < 0) { integrator = 0; } } int main() { float err=0; float out=0.; int ctl=0; int pd=0; pc.printf("mbed restarted!\n"); 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 t.start(); cyc = 0; while(1) { cyc = cyc ^ 1; //toggle debug pin //read in from AD7921 over SPI adcCS = 0; pd = adcSPI.write(3<<13); //select ch 1 pd = (pd) & 0xFFF; adcCS = 1; adcCS = 0; ctl = adcSPI.write(1); // select ch 0 //take the last 12 bits that carry data 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 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); } }