Werkend aansturingsscript voor 2 motoren, incl werkende program switch. Motoren oscilleren nog iets. Vuur mechanisme ontbreekt nog.

Dependencies:   HIDScope MODSERIAL QEI biquadFilter mbed

Fork of frdm_Motor_V2_2 by Margreeth de Breij

main.cpp

Committer:
Rvs94
Date:
2015-10-02
Revision:
21:cd7eb62183da
Parent:
20:f5091e29cd26
Child:
23:6b7695ffadcb

File content as of revision 21:cd7eb62183da:

#include "mbed.h"
#include "MODSERIAL.h"
#include "HIDScope.h"
#include "QEI.h"
#include "biquadFilter.h"
 
Serial pc(USBTX, USBRX); // tx, rx
QEI Encoder(D3, D2, NC, 128);
HIDScope scope(3);

//Ledjes
DigitalOut LedR(LED_RED);
DigitalOut LedG(LED_GREEN);
DigitalOut LedB(LED_BLUE);

//Motor
DigitalOut motor2direction(D4); //D4 en D5 zijn motor 2 (op het motorshield)
PwmOut motor2speed(D5);

//Tickers
Ticker t1, t2, t3, t4;
volatile bool fn1_go = false, fn2_go = false, fn3_go = false, fn4_go = false;
void fn1_activate(){ fn1_go = true; }; //Activates the go−flag
void fn2_activate(){ fn2_go = true; };
void fn3_activate(){ fn3_go = true; };
void fn4_activate(){ fn4_go = true; };

//Startwaarden
double reference;
double position;
double m2_ref = 0;
int count = 0;

//Sample time (motor2-step)
const double m2_Ts = 0.01;

//Controller gain Motor 2
const double m2_Kp = 0.5,m2_Ki = 0.005, m2_Kd = 0.5;
double m2_err_int = 0, m2_prev_err = 0;

//Derivative filter coeffs Motor 2
const double BiGain = 0.016955;
const double m2_f_a1 = -0.96608908283*BiGain, m2_f_a2 = 0.0*BiGain, m2_f_b0 = 1.0*BiGain, m2_f_b1 = 1.0*BiGain, m2_f_b2 = 0.0*BiGain;

// Filter variables
double m2_f_v1 = 0, m2_f_v2 = 0;


//HIDScope
void ScopeSend()//Functie die de gegevens voor de scope uitleest en doorstuurt
{
    scope.set(0, motor2direction.read());
    scope.set(1, motor2speed.read());
    scope.set(2, position);

    scope.send();
    
}

// PC Printf
void PCPrintf()
{
    pc.printf("position = %f aantal degs = %f, count = %i\n",reference,position,count);
}

// Biquad filter
double biquad( double u, double &v1, double &v2, const double a1, const double a2, const double b0, const double b1, const double b2 )
{
    double v = u - a1*v1 - a2*v2;
    double y = b0*v + b1*v1 + b2*v2;
    v2 = v1; v1 = v;
    return y;
}    


// Reusable PID controller
double PID( double e, const double Kp, const double Ki, const double Kd, double Ts, double &e_int, double &e_prev, double &f_v1, double &f_v2,
const double f_a1,const double f_a2, const double f_b0, const double f_b1, const double f_b2)
{
    // Derivative
    double e_der = (e-e_prev)/Ts;
    e_der = biquad(e_der,f_v1,f_v2,f_a1,f_a2,f_b0,f_b1,f_b2);
    e_prev = e;
    // Integral
    e_int = e_int + Ts*e;
    // PID
    return Kp * e + Ki*e_int + Kd*e_der;
}

// Motor2 control
void motor2_Controller() 
{
    reference = m2_ref; // Setpoint
    position = Encoder.getPulses()*360/(0.5*128*131); // Aantal Degs
    double P2 = PID( reference - position, m2_Kp, m2_Ki, m2_Kd, m2_Ts, m2_err_int, m2_prev_err, m2_f_v1, m2_f_v2, m2_f_a1, m2_f_a2, 
    m2_f_b0, m2_f_b1, m2_f_b2);
    motor2speed = abs(P2); // Speed control
    if(P2 > 0) // Direction control
    {  
        motor2direction = 0;
    }
    else
    {
        motor2direction = 1;
    }
    
}

int main()
{   
    LedR.write(1);
    LedB.write(1);
    LedG.write(1);
    pc.baud(115200);
    pc.printf("Tot aan loop werkt\n");

    
    t1.attach( &fn1_activate, 0.01f); // Function@100hz
    t2.attach( &fn2_activate, 0.001f); // Function@ 1khz
    t3.attach( &fn3_activate, 0.2f); // Function@ 5hz
    t4.attach( &fn3_activate, 0.02f); // Function@ 50hz

    while(true)
    {
        char c = pc.getc();
        if(fn1_go)
        {
            fn1_go = false;
            motor2_Controller();
        }
        if(fn2_go)
        {
            fn2_go = false;
            ScopeSend();
        }
        if(fn3_go)
        {
            fn3_go = false;
            PCPrintf();
        }

        if(c == 'e') //Ga 1 programma omhoog
        {
             count = count + 1;
            if(count > 2)
            {
                count = 2;
            }
        }
        
        if(c == 'd') //Ga 1 programma omlaag
        {
            count = count - 1;
            if(count < 0)
            {
                count = 0;
            }
        }  
            
        if(count == 0) //Motor 1 control
        {
                
                LedR = LedB = 1;
                LedG = 0;
                if(c == 'r')
                {
                    m2_ref = m2_ref + 5;
                    if (m2_ref > 90)
                    {
                        m2_ref = 90;
                    }
                }
                if(c == 'f')
                {
                    m2_ref = m2_ref - 5;
                    if (m2_ref < -90)
                    {
                        m2_ref = -90;
                    }
                }
        }
        if(count == 1) //Motor 2 control
        {
                LedG = LedB = 1;
                LedR = 0;
        }
        if(count == 2) //Vuur mechanisme
        {

                LedR = LedG = 1;
                LedB = 0;   
        }
    }

}