Ollie Peng
/
EE192_PID
first draft
Diff: main.cpp
- Revision:
- 0:87a63b2d26ef
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Tue Mar 15 21:18:47 2016 +0000 @@ -0,0 +1,37 @@ +#include "mbed.h" + +Serial pc(USBTX, USBRX); // tx, rx +PwmOut Clock(PTD4); +PwmOut Output(////); +AnalogIn Sensor(////); + +float adjustmentFactor = 1.0; //change so that sensor data is calibrated +float ref = 3.0; +float error = 0.0; +float totalError = 0.0; +int period = 1000; +int PW = period/2; + +int outPeriod = period; //assume period always is the same +int outPW; + +float Ki = 0; //adjust +float Kp = 30; //adjust + +Ticker Controller; + +void Control(){ + error = ref - Sensor.read()*adjustmentFactor; + totalError += error; + outPW = Kp*error + Ki*(totalError); + Output.period_us(outPeriod); + Output.pulsewidth_us(outPW); + } + +int main() { + Clock.period_us(period); + Clock.pulsewidth_us(PW); + Controller.attach_us(Control,period*128); //assumed that we should wait 128 clock cycles before activating controller + //because line camera requires 128 cycles. should verify + +}