closedloop

Dependencies:   PID mbed

Committer:
DrT
Date:
Mon Jun 30 11:10:21 2014 +0000
Revision:
0:b0f441e496c1
mm

Who changed what in which revision?

UserRevisionLine numberNew contents of line
DrT 0:b0f441e496c1 1 #include "PID.h"
DrT 0:b0f441e496c1 2 #include "mbed.h"
DrT 0:b0f441e496c1 3
DrT 0:b0f441e496c1 4 #define RATE 0.0001
DrT 0:b0f441e496c1 5
DrT 0:b0f441e496c1 6 float setpoint,kp,ki,kd,pv_new;
DrT 0:b0f441e496c1 7 PID controller(kp, ki, kd, RATE);
DrT 0:b0f441e496c1 8 AnalogIn pv(p15);
DrT 0:b0f441e496c1 9 PwmOut co(p21);
DrT 0:b0f441e496c1 10 Serial pc(USBTX, USBRX);
DrT 0:b0f441e496c1 11
DrT 0:b0f441e496c1 12
DrT 0:b0f441e496c1 13
DrT 0:b0f441e496c1 14 int main(){
DrT 0:b0f441e496c1 15 pc.baud(19200);
DrT 0:b0f441e496c1 16
DrT 0:b0f441e496c1 17 //Set PWM period to 10us
DrT 0:b0f441e496c1 18 co.period(100e-6);
DrT 0:b0f441e496c1 19
DrT 0:b0f441e496c1 20
DrT 0:b0f441e496c1 21 //Analog input from 0.0 to 1V
DrT 0:b0f441e496c1 22 controller.setInputLimits(0.0,1);
DrT 0:b0f441e496c1 23 //Pwm output from 0.0 to 1.0
DrT 0:b0f441e496c1 24 controller.setOutputLimits(0.0,1);
DrT 0:b0f441e496c1 25 //Add some bias to counter nonlinear effects
DrT 0:b0f441e496c1 26 controller.setBias(0.0);
DrT 0:b0f441e496c1 27 //Set mode to auto
DrT 0:b0f441e496c1 28 controller.setMode(AUTO_MODE);
DrT 0:b0f441e496c1 29 //controller.setMode(MANUAL_MODE);
DrT 0:b0f441e496c1 30
DrT 0:b0f441e496c1 31
DrT 0:b0f441e496c1 32 while(1){
DrT 0:b0f441e496c1 33
DrT 0:b0f441e496c1 34 //Scan the labview environment for the setpoint and tuning parameters
DrT 0:b0f441e496c1 35 pc.scanf("%f,%f,%f,%f",&setpoint,&kp,&ki,&kd);
DrT 0:b0f441e496c1 36
DrT 0:b0f441e496c1 37 //set the controller setpoint
DrT 0:b0f441e496c1 38 controller.setSetPoint(setpoint);
DrT 0:b0f441e496c1 39
DrT 0:b0f441e496c1 40 //set the controller PID values
DrT 0:b0f441e496c1 41 controller.setTunings(kp, ki, kd);
DrT 0:b0f441e496c1 42
DrT 0:b0f441e496c1 43 pv_new = pv.read();
DrT 0:b0f441e496c1 44
DrT 0:b0f441e496c1 45 controller.setProcessValue(pv_new);
DrT 0:b0f441e496c1 46
DrT 0:b0f441e496c1 47 //Set the new PWM output.
DrT 0:b0f441e496c1 48 co = controller.compute();
DrT 0:b0f441e496c1 49
DrT 0:b0f441e496c1 50 //Send the PWM and PV values to Labview
DrT 0:b0f441e496c1 51 pc.printf("%f,%f,%f",co.read(),pv_new,setpoint);
DrT 0:b0f441e496c1 52
DrT 0:b0f441e496c1 53
DrT 0:b0f441e496c1 54 //Wait for another loop calculation.
DrT 0:b0f441e496c1 55 wait(RATE);
DrT 0:b0f441e496c1 56 }
DrT 0:b0f441e496c1 57
DrT 0:b0f441e496c1 58
DrT 0:b0f441e496c1 59 }