Motor control

Dependencies:   mbed QEI HIDScope biquadFilter MODSERIAL FastPWM

main.cpp

Committer:
BasB
Date:
2019-10-14
Revision:
10:b871d1b05787
Parent:
9:08a7a8e59a6a
Child:
11:94a4dd7ed05c

File content as of revision 10:b871d1b05787:

#include "mbed.h"
#include "HIDScope.h"
#include "QEI.h"
#include "MODSERIAL.h"
#include "BiQuad.h"
#include "FastPWM.h"

// Button and potmeter1 control
InterruptIn button1(D11);
InterruptIn button2(D10);
InterruptIn buttonsw2(SW2);
InterruptIn buttonsw3(SW3);
AnalogIn potmeter1(A0);
AnalogIn potmeter2(A1);
AnalogIn potmeter3(A2);
AnalogIn potmeter4(A3);
// Encoder
DigitalIn encA(D13);
DigitalIn encB(D12);
QEI encoder(D13,D12,NC,64,QEI::X4_ENCODING);
float Ts = 0.01;
float angle;
float omega;


// Motor
DigitalOut motor2Direction(D4);
FastPWM motor2Power(D5);
DigitalOut motor1Direction(D7);
FastPWM motor1Power(D6);

volatile int motor1Toggle = 1;

//Motorcontrol
bool motordir;
double motorpwm;
double premotorpwm;
float u1= 0;
double u2;
double potValue;
double pi2= 6.283185;
float e; //e = error
float Kp=0.49;
float Ki;
float u_k;
float u_i;

//Hidscope
HIDScope scope(3); //Going to send 3 channels of data. To access data go to 'http:/localhost:18082/' after starting HIDScope application.
// PC connection
MODSERIAL pc(USBTX, USBRX);

// Intializing tickers
Ticker motorTicker;
Ticker controlTicker;
Ticker directionTicker;
Ticker encoderTicker;
Ticker scopeTicker;

const float PWM_period = 1e-6;

volatile int counts; // Encoder counts
volatile int countsPrev = 0;
volatile int deltaCounts;

float factorin = 6.23185/64; // Convert encoder counts to angle in rad
float gearratio = 131.25; // Gear ratio of gearbox


float PID_controller(float e){
    static float error_integral=0;
    static float e_prev=e;
    
    //Proportional part:
    Kp=potmeter1.read();
    u_k=Kp*e;
    
    //Integral part
    Ki=potmeter2.read();
    error_integral=error_integral+e*Ts;
    u_i=Ki*error_integral;
    
    // Sum and return
    return u_k+u_i;    
}


void readEncoder()
{
    counts = encoder.getPulses();
    deltaCounts = counts - countsPrev;

    countsPrev = counts;
}

void togglehoek(){
    static float t = 0;
    u1= pi2/3.0f*sin(5.0f*t)*motor1Toggle;
    t+=0.01;
    }
    
void motorControl()
{
    togglehoek();
    //button1.fall(&togglehoek);
    angle = counts * factorin / gearratio; // Angle of motor shaft in rad
    omega = deltaCounts / Ts * factorin / gearratio; // Angular velocity of motor shaft in rad/s
    potValue= potmeter1.read();
    //u1= (potValue*2*pi2)-pi2;
    e=u1-angle;
    
    u2=PID_controller(e);
    
    premotorpwm= fabs(u2);
    if (premotorpwm>1.0){
        motorpwm=1;}
    else {
        motorpwm=premotorpwm;}    
    if (u2<0){
        motordir= 0;}
    else {
         motordir= 1;}
    motor1Power.write(motorpwm);
    motor1Direction= motordir;
}

void Plotje(){
    scope.set(0,u1); //gewenste hoek
    scope.set(1,angle); //Gemeten hoek
    scope.set(2,e); //verschil in gewenste en gemeten hoek

    scope.send(); //send what's in scope memory to PC
}

void toggleMotor()
{
    motor1Toggle = !motor1Toggle;
}

int main()
{
    pc.baud(115200);
    pc.printf("\r\nStarting...\r\n\r\n");
    
    motor1Power.period(PWM_period);
    motorTicker.attach(motorControl, 0.01);
    scopeTicker.attach(Plotje, 0.01);
    encoderTicker.attach(readEncoder, Ts);
    
    button2.fall(&toggleMotor);
    
    while (true) {

        //pc.printf("Potmeter: %d \r\n", potValue,);
        //pc.printf("Counts: %i   DeltaCounts: %i\r\n", counts, deltaCounts);
        pc.printf("Angle:  %f   Omega:       %f\r\n", angle, omega);
        pc.printf("U1: %f   Error:  %f     \r\n",u1, e);
        pc.printf("Kp: %f   Ki:    %f \r\n", Kp, Ki);
        
        wait(0.5);
    }
}