State Machine, bezig met mooimaken

Dependencies:   Encoder HIDScope MODSERIAL biquadFilter mbed

Fork of vanEMGnaarMOTORPauline_States_nacht by Projectgroep 20 Biorobotics

main.cpp

Committer:
paulineoonk
Date:
2017-10-31
Revision:
8:c4ec359af35d
Parent:
7:05c71a859d27
Child:
9:285499f48cdd

File content as of revision 8:c4ec359af35d:

//libaries
#include "mbed.h"
#include "BiQuad.h"
#include "HIDScope.h"
#include "encoder.h"
#include "MODSERIAL.h"

//globalvariables Motor
Ticker Treecko;             //We make a awesome ticker for our control system
Ticker printer;
//PwmOut M1E(D6);             //Biorobotics Motor 1 PWM control of the speed 
//DigitalOut M1D(D7);         //Biorobotics Motor 1 diraction control

//Encoder motor1(D13,D12,true);
MODSERIAL pc(USBTX,USBRX);

//double PwmPeriod = 1.0/5000.0;           //set up of PWM periode (5000 Hz, want 5000 periodes in 1 seconde)
const double Ts = 0.1;                   // tickettijd/ sample time
//double e_prev = 0; 
//double e_int = 0;
double tijdstap = 0.002;
volatile double LBF;
volatile double RBF;
volatile double LTF;
volatile double RTF;

//buttons  en leds voor calibration
DigitalIn button1(PTA4);
DigitalOut ledred(LED_RED); 
DigitalOut ledblue(LED_BLUE);

//double maxiLB = 0;
//double maxiRB = 0;
//double maxiLT = 0;
//double maxiRT = 0;

bool caldone = false; 
int CalibrationSample = 1000; //How long will we calibrate? Timersampletime*Calibrationsample

int TimescalibrationLB = 0;
int TimescalibrationRB = 0;
int TimescalibrationLT = 0;
int TimescalibrationRT = 0;

// Biquad filters van respectievelijk Notch, High-pass en Low-pass filter
BiQuad N1( 8.63271e-01, -1.39680e+00, 8.63271e-01, -1.39680e+00, 7.26543e-01 );
BiQuadChain NF;
BiQuad HP1( 9.63001e-01, -9.62990e-01, 0.00000e+00, -9.62994e-01, 0.00000e+00 );
BiQuad HP2( 1.00000e+00, -2.00001e+00, 1.00001e+00, -1.96161e+00, 9.63007e-01 ); 
BiQuadChain HPF;
BiQuad LP1( 2.56971e-06, 2.56968e-06, 0.00000e+00, -9.72729e-01, 0.00000e+00 );
BiQuad LP2( 1.00000e+00, 2.00001e+00, 1.00001e+00, -1.97198e+00, 9.72734e-01 );
BiQuadChain LPF;

Timer looptime; //moetuiteindelijk weg

//filters
double emgNotchLB;
double emgHPLB;
double emgAbsHPLB;
double emgLPLB;

double emgNotchRB;
double emgHPRB;
double emgAbsHPRB;
double emgLPRB;

double emgNotchLT;
double emgHPLT;
double emgAbsHPLT;
double emgLPLT;

double emgNotchRT;
double emgHPRT;
double emgAbsHPRT;
double emgLPRT;

double f = 500;       // frequency
double dt = 1/f;      // sample frequency

AnalogIn emgLB(A0);   // EMG lezen
AnalogIn emgRB(A1);
AnalogIn emgLT(A2);
AnalogIn emgRT(A3);

float maxiLB = 0.3;
float maxiRB = 0.3;
float maxiLT = 0.3;
float maxiRT = 0.3;

void Filteren() 
{
    looptime.reset();
    looptime.start();
    
    //EMG 1
    
    emgNotchLB = NF.step(emgLB.read() );  // Notch filter
    emgHPLB = HPF.step(emgNotchLB);       // High-pass filter: also normalises around 0.
    emgAbsHPLB = abs(emgHPLB);            // Take absolute value
    emgLPLB = LPF.step(emgAbsHPLB);       // Low-pass filter: creates envelope
    LBF = emgLPLB/maxiLB;       // Scale to maximum signal: useful for motor   
    /*
    emgNotchRB = NF.step(emgRB.read() );  // Notch filter
    emgHPRB = HPF.step(emgNotchRB);       // High-pass filter: also normalises around 0.
    emgAbsHPRB = abs(emgHPRB);            // Take absolute value
    emgLPRB = LPF.step(emgAbsHPRB);       // Low-pass filter: creates envelope
    RBF = emgLPRB/maxiRB;       // Scale to maximum signal: useful for motor
    
    emgNotchLT = NF.step(emgLT.read() );  // Notch filter
    emgHPLT = HPF.step(emgNotchLT);       // High-pass filter: also normalises around 0.
    emgAbsHPLT = abs(emgHPLT);            // Take absolute value
    emgLPLT = LPF.step(emgAbsHPLT);       // Low-pass filter: creates envelope
    LTF = emgLPLT/maxiLT;       // Scale to maximum signal: useful for motor
    
    emgNotchRT = NF.step(emgRT.read() );  // Notch filter
    emgHPRT = HPF.step(emgNotchRT);       // High-pass filter: also normalises around 0.
    emgAbsHPRT = abs(emgHPRT);            // Take absolute value
    emgLPRT = LPF.step(emgAbsHPRT);       // Low-pass filter: creates envelope
    RTF = emgLPRT/maxiRT;       // Scale to maximum signal: useful for motor
        
   //if (emgFiltered >1)
    //{
    //    emgFiltered=1.00;
    //}
//pc.printf("emgreadLB = %f , emgFiltered = %f, maxi = %f, loop = %f \r\n, emgreadRB = %f , emgFiltered = %f, maxi = %f \r\n, emgreadLT = %f , emgFiltered = %f, maxi = %f \r\n, emgreadRT = %f , emgFiltered = %f, maxi = %f \r\n",emgLB.read(), LBF, maxiLB,looptime.read(),emgRB.read(), RBF, maxiRB,emgLT.read(), LTF, maxiLT, emgRT.read(), RTF, maxiRT);
    //int maxwaarde = 4096;                   // = 64x64
    //double refP = emgFiltered*maxwaarde;
    //return refP;                            // value between 0 and 4096 
 */   
}
/*
void Calibration()
{
    Timescalibration++;
    emgNotch = NF.step(emg.read());             // Notch filter
    emgHP = HPF.step(emgNotch);                 // High-pass filter: also normalises around 0.
    emgAbsHP = abs(emgHP);                      // Take absolute value
    emgLP = LPF.step(emgAbsHP);                 // Low-pass filter: creates envelope
    double emgfinal = emgLP;
    if (emgfinal > maxi)
    {                                       //determine what the highest reachable emg signal is
          maxi = emgfinal;
    }    
    if(Timescalibration>5000)
    {
         caldone=true; 
    }
   // pc.printf("caldone = %i , Timescalibration = %i maxi = %f \r\n",caldone,Timescalibration,maxi);
        
        //for(int n =0; n<CalibrationSample;n++)          //read for 5000 samples as calibration
        //{                          
         //   emgNotch = NF.step(emg.read());             // Notch filter
           // emgHP = HPF.step(emgNotch);                 // High-pass filter: also normalises around 0.
            //emgAbsHP = abs(emgHP);                      // Take absolute value
            //emgLP = LPF.step(emgAbsHP);                 // Low-pass filter: creates envelope
            //double emgfinal = emgLP;
            //if (emgfinal > maxi)
              //  {                                       //determine what the highest reachable emg signal is
               // maxi = emgfinal;
                //}
           // pc.printf("maxi waarde = %f emgfinal = %f \r\n",maxi,emgfinal);
        //}
                                         //PAS ALS DEZE TRUE IS, MOET DE MOTOR PAS BEWEGEN!!!
  //return maxi;
}
        
*/      

/*
double Encoder ()
{
    double Huidigepositie = motor1.getPosition ();
    return Huidigepositie;             // huidige positie = current position
}

double FeedBackControl(double error, double &e_prev, double &e_int)   // schaalt de snelheid naar de snelheid zodat onze chip het begrijpt (is nog niet in werking)
{
    double kp = 0.001;                           // has jet to be scaled
    double Proportional= kp*error;
    
    double kd = 0.0004;                            // has jet to be scaled
    double VelocityError = (error - e_prev)/Ts; 
    double Derivative = kd*VelocityError;
    e_prev = error;
    
    double ki = 0.00005;                           // has jet to be scaled 
    e_int = e_int+Ts*error;
    double Integrator = ki*e_int;
    
    
    double motorValue = Proportional + Integrator + Derivative;
    return motorValue;
}

void SetMotor1(double motorValue)
{
    if (motorValue >= 0)
    {
        M1D = 0;
    }
    else 
    {
        M1D = 1;
    }

    if  (fabs(motorValue) > 1)    
    {
        M1E = 1;                    //de snelheid wordt teruggeschaald naar 8.4 rad/s (maximale snelheid, dus waarde 1)
    }
    else
    {    
        M1E = fabs(motorValue);      //de absolute snelheid wordt bepaald, de motor staat uit bij een waarde 0
    }
}
*/
void MeasureAndControl ()
{
        // hier the control of the control system
     
    //if(caldone==false)
    //{
    //    if(button1.read()==false)  
    //    {
    //        Calibration();
    //    }
    //}
    //if (caldone==true)
    
    //{
       Filteren();
        //rest
    //}
    
    //double Huidigepositie = Encoder(); 
    //double error = (refP - Huidigepositie);// make an error
    //double motorValue = FeedBackControl(error, e_prev, e_int);
    //double motorValue = refP;
    //SetMotor1(motorValue);
}

void Tickerfunctie()
{
    pc.printf("emgreadLB = %f , emgFiltered = %f, maxi = %f, loop = %f \r\n",emgLB.read(), LBF, maxiLB,looptime.read());
    //emgreadRB = %f , emgFiltered = %f, maxi = %f \r\n, emgreadLT = %f , emgFiltered = %f, maxi = %f \r\n, emgreadRT = %f , emgFiltered = %f, maxi = %f \r\n",emgLB.read(), LBF, maxiLB,looptime.read(),emgRB.read(), RBF, maxiRB,emgLT.read(), LTF, maxiLT, emgRT.read(), RTF, maxiRT);
}

int main()
{
    //voor EMG filteren
    NF.add( &N1 );
    HPF.add( &HP1 ).add( &HP2 );
    LPF.add( &LP1 ).add( &LP2 );
    
    //voor serial
    pc.baud(115200);
    
    //motor
   // M1E.period(PwmPeriod); //set PWMposition at 5000hz
    //Ticker
    Treecko.attach(MeasureAndControl, tijdstap);   //Elke 1 seconde zorgt de ticker voor het runnen en uitlezen van de verschillende 
                                            //functies en analoge signalen. Veranderingen worden elke 1 seconde doorgevoerd.
    printer.attach(Tickerfunctie,0.1);
    while(true)
    {
        }
    
    
}