4 directional EMG control of the XY table. Made during my bachelor end assignment.

Dependencies:   C12832_lcd HIDScope mbed-dsp mbed

main.cpp

Committer:
jessekaiser
Date:
2015-05-01
Revision:
18:71a01477e264
Parent:
17:03f523424eb2
Child:
19:f0875bc3b72f

File content as of revision 18:71a01477e264:

#include "mbed.h"
#include "C12832_lcd.h"
#include "arm_math.h"

#define P_GAIN 0.998

DigitalOut Dir(p21);
PwmOut Step(p22);
DigitalOut Enable(p14);
DigitalOut MS1(p27);
DigitalOut MS2(p28);
DigitalOut MS3(p29);
AnalogIn Pot1(p19);
AnalogIn Pot2(p20);
C12832_LCD lcd;

BusIn Joystick(p12,p13,p14,p15,p16);
DigitalIn Up(p15);
DigitalIn Down(p12);


// Filter
arm_biquad_casd_df1_inst_f32 lowpass_p1;

//lowpass filter settings: Fc = 2 Hz, Fs = 100 Hz, Gain = 6 dB
float lowpass_const[] = {0.007820199259120319, 0.015640398518240638, 0.007820199259120319, 1.7347238224240125, -0.7660046194604936};

//state values
float lowpass_p1_states[4];
float filtered_p1;

// EMG looper
void looper()
{
    /*variable to store value in*/
    uint16_t p1;
    
    float p1_f32;
    /*put raw emg value both in red and in emg_value*/
    p1 = Pot1.read_u16(); // read direct ADC result, converted to 16 bit integer (0..2^16 = 0..65536 = 0..3.3V)
    p1_f32 = Pot1.read()-0.5;

    //process emg biceps
    
    arm_biquad_cascade_df1_f32(&lowpass_p1, &filtered_p1, &filtered_p1, 1 );
   
 float setpoint = 7000; //Frequentie
    float step_freq = 1;
int main()
{   Ticker log_timer;
        //set up filters. Use external array for constants
        arm_biquad_cascade_df1_init_f32(&lowpass_p1,1 , lowpass_const, lowpass_p1_states);
        log_timer.attach(looper, 0.01); 
    Enable = 0;
   
    MS1 = 1;
    MS2 = 0;
    MS3 = 0;
    //float p1;
   

    Step.period(1./step_freq); // 1 kHz, vanaf 2,5 kHz doet de motor het niet meer.
    Step.write(0.5); // Duty cycle van 50%
    // Dir = Pot1; // Dir 1 is naar boven, Dir 0 naar onder.
    Enable = 1;
    while (1) {
        
        if (p1_f32 < 0) { //Directie controle. 
            Dir = 0;
        } else if (p1_f32 > 0) {
            Dir = 1;
        }
        //p1 = Pot1.read() - 0.500; //Offset creëren [-0.500;0.500]
        //Dir = dir1;
        float new_step_freq;
        //new_step_freq = ((1-P_GAIN)*setpoint) + (P_GAIN*step_freq);
        new_step_freq = (setpoint*filtered_p1*2);
        step_freq = abs(new_step_freq); //Geeft een frequentie in 100 stappen. 
        Step.period(1.0/step_freq); 
        wait(0.01); //Hier nog ticker inbouwen
        lcd.printf("Spd %.0f Hz p1 %.2f \n", step_freq, filtered_p1); //snelheid meting op lcd, zonder decimalen

    }