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-06
- Revision:
- 0:3e6ae21a6bb1
- Child:
- 1:dc799997391e
File content as of revision 0:3e6ae21a6bb1:
/* 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" AnalogIn ctl(p20); AnalogIn pd(p16); AnalogOut vca(p18); DigitalOut led1(LED1); Serial pc(USBTX, USBRX); Timer t; Ticker commTick; Ticker intTick; float pGain = 1.; 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); break; default: pc.printf("Command not understood.\n",iGain); pc.printf("Read %c and %lf.\n",c,f); break; } } } void limitIntegrator(){ if (integrator > 3.3) { integrator = 3.3; } if (integrator < -3.3) { integrator = -3.3; } } int main() { float err; pc.printf("mbed restarted!\n"); commTick.attach(&serialComm,0.5); //check serial every half second intTick.attach(&limitIntegrator,0.001); //check integrator overrun every ms t.start(); while(1) { err = ctl - pd; integrator += err * iGain*1e-6 * t.read_us(); //iGain is in Hz! t.reset(); //reset timer to get next integration time vca = err * pGain + integrator; } }