first draft

Dependencies:   PID mbed

main.cpp

Committer:
openg
Date:
2016-03-15
Revision:
0:87a63b2d26ef

File content as of revision 0:87a63b2d26ef:

#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

}