P, PI, PID-control

Dependencies:   HIDScope MODSERIAL QEI biquadFilter mbed

Fork of control by Maik Overmars

main.cpp

Committer:
bjonkheer
Date:
2018-10-15
Revision:
1:a3af4c5b59bb
Parent:
0:545f4726720c
Child:
2:3d4fc510f3d5

File content as of revision 1:a3af4c5b59bb:

#include "mbed.h"
#include "MODSERIAL.h"
#include "QEI.h"

PwmOut pwmpin(D5);
DigitalOut directionpin(D4);
AnalogIn potmeter(A0);
InterruptIn button(D0);
Ticker Sample;
Timer t;

MODSERIAL pc(USBTX, USBRX);
QEI wheel (D13, D12, NC, 32);

float angle_resolution = (360.0/32.0)*(1.0/131.0);  //degrees/counts 
float samplingfreq = 100.0;        //Hz
float u = 0;
int pulses = 0;
float angle;
float oldangle;
double time_s;
double Kp = 17.5;
double error;

double P_controller(error)
{   // Proportional part:
    double u_k = Kp * error;
   // Sum all parts and return it
    return u_k;   }


float mean(float *samples, int n) {
    float sum = 0.0;
    for (int i=0; i<n; i++) {
        sum += samples[i];
    }
    return sum / (float)n;
}

void sampling()
{
    
}

void changedirection()
{
    directionpin = !directionpin;
}

int main()
{   
    t.start();
    pc.baud(115200);
    pwmpin.period_us(60);
    button.fall(&changedirection);
    //refvel.attach(&sampling, 1/samplingfreq);
    while (true) {
        pulses = wheel.getPulses();
        angle = pulses*angle_resolution; 
        time_s = t.read();
        u = sin(time_s);
        pulses = wheel.getPulses();
        angle = pulses*angle_resolution;
        directionpin = u > 0.0f;      
        pwmpin = fabs(u);
        
        pc.printf("Reference velocity is: %f degrees per second \r\n", u);  
        oldangle = angle;  
        wait(1/samplingfreq); 
        
    }
}