Aukie Hooglugt / Mbed 2 deprecated EMG_Project

Dependencies:   HIDScope MODSERIAL- mbed-dsp mbed

Project_main.cpp

Committer:
Hooglugt
Date:
2014-10-06
Revision:
31:d8eaf0ce8517
Parent:
30:5e5098b0cca6
Child:
32:aaf01b1bf05d

File content as of revision 31:d8eaf0ce8517:

#include "mbed.h"
#include "MODSERIAL.h"
#include "HIDScope.h"

#define TIMEB4NEXTCHOICE 1  // 1s keuzelampje blijft aan
#define TIMEBETWEENBLINK 10 // 1s voor volgende blink

//Define objects
AnalogIn    emg0(PTB1);         //Analog input biceps
AnalogIn    emg1(PTB2);         //Analog input triceps

PwmOut      emg_bifloat(PTD4);   //Float voor EMG-waarde biceps, VRAAG: waarom we hier een PwmOut voor gebruiken
PwmOut      emg_trifloat(PTA4);  //Float voor EMG-waarde triceps

PwmOut      red(LED_RED);
PwmOut      green(LED_GREEN);
PwmOut      blue(LED_BLUE);

int         direction = 0;
int         force = 0;

Ticker log_timer;
Ticker reset_timer;
MODSERIAL pc(USBTX,USBRX);
HIDScope scope(4);

void looper() //nieuwe looper maken, die om een nog te bepalen tijdseenheid de emgtrifloat checked en anders nieuwe goto maakt naar vorige selectiekeuze
{
    /*put raw emg value of biceps both in emg_bifloat and in emg_bivalue*/
    emg_bifloat.write(emg0.read());         // read float value (0..1 = 0..3.3V) biceps
    uint16_t emg_bivalue;
    emg_bivalue = emg0.read_u16();          // read direct ADC result (0..4096 = 0..3.3V) biceps

    /*put raw emg value of triceps both in emg_trifloat and in emg_trivalue*/
    emg_trifloat.write(emg1.read());        // read float value (0..1 = 0..3.3V) triiceps
    uint16_t emg_trivalue;
    emg_trivalue = emg1.read_u16();         // read direct ADC result (0..4096 = 0..3.3V) biceps

    /*send value to PC. Line below is used to prevent buffer overrun */
    if(pc.rxBufferGetSize(0)-pc.rxBufferGetCount() > 30) { //VRAAG: praktisch nut hiervan? print emg value wanneer buffercount groter dan 30 is
        //pc.printf("%u\n",emg_bivalue);
    }
    scope.set(0,emg_bivalue);
    scope.set(1,emg_bifloat.read());
    scope.set(2,emg_trivalue);
    scope.set(3,emg_trifloat.read());
    scope.send();
}

void resetlooper()
{
    if(emg_trifloat.read()>0.8 && direction != 0) { //dit is alleen mogelijk wanneer directionchoice is gemaakt
        direction = 0;
        force = 0;              // WEGHALEN, wanneer in uiteindelijke script na force keuzen niet meer gereset kan worden (voor nu wel handig)
        pc.printf("reset ");
        goto directionchoice; //<--- dit kan NATUURLIJK NIET, dus iets wat vervolgens checked of directionchoice en/of force is bepaald in de mainloop en dan een goto maken
    }
}

int main()
{
    pc.baud(115200); //baudrate instellen
    emg_bifloat.period_ms(2); //sets period for the PWM to the emgfloat PTD4
    emg_trifloat.period_ms(2);
    log_timer.attach(looper, 0.001); // The looper() function will be called every 0.001 seconds (with the ticker object)
    reset_timer.attach(resetlooper, 0.1); //

    goto directionchoice; // goes to first while(1) for the deciding the direction

    while(1) { //Loop keuze DIRECTION
directionchoice:
        for(int i=1; i<4; i++) {
            if(i==1) {           //red
                red=0;
                green=1;
                blue=1;
                for (int lag=0; lag<TIMEBETWEENBLINK; lag++) {
                    if(emg_bifloat.read()>0.8) {                   // 0.8 klopt niet als grenswaarde. #nofilter
                        direction = 1;
                        red=1;
                        green = 0;
                        blue = 0;
                        pc.printf("links ");
                        wait(TIMEB4NEXTCHOICE);                 // Tijdelijke wait om cyaan lampje aan te zetten ter controle selectie
                        goto forcechoice;                       // goes to second while(1) for the deciding the force
                    } else {
                        wait(0.1);
                    }
                }
            }
            if(i==2) {           //green
                red =1;
                green=0;
                blue=1;
                for (int lag=0; lag<TIMEBETWEENBLINK; lag++) {
                    if(emg_bifloat.read()>0.8) {                    //0.8 klopt niet als grenswaarde. #nofilter
                        direction = 2;
                        red=0;
                        green = 1;
                        blue = 0;
                        pc.printf("mid ");
                        wait(TIMEB4NEXTCHOICE);                            // Tijdelijke wait om paars lampje aan te zetten ter controle selectie
                        goto forcechoice;
                    } else {
                        wait(0.1);
                    }
                }
            }
            if(i==3) {           //blue
                red=1;
                green=1;
                blue=0;
                for (int lag=0; lag<TIMEBETWEENBLINK; lag++) {
                    if(emg_bifloat.read()>0.8) {                    //0.8 klopt niet als grenswaarde. #nofilter
                        direction = 3;
                        red=0;
                        green = 0;
                        blue = 1;
                        pc.printf("rechts ");
                        wait(TIMEB4NEXTCHOICE);                            // Tijdelijke wait om oranje lampje aan te zetten ter controle selectie
                        goto forcechoice;
                    } else {
                        wait(0.1);
                    }
                }
            }
        }
    }
    while(1) { //Loop keuze FORCE
forcechoice:
        for(int i=1; i<4; i++) {
            if(i==1) {           //red
                red=0;
                green=1;
                blue=1;
                for (int lag=0; lag<TIMEBETWEENBLINK; lag++) {
                    if(emg_bifloat.read()>0.8) {                    // 0.8 klopt niet als grenswaarde. #nofilter
                        force = 1;
                        red=1;
                        green = 0;
                        blue = 0;
                        pc.printf("zwak ");
                        wait(TIMEB4NEXTCHOICE);                            // Tijdelijke wait om cyaan lampje aan te zetten ter controle selectie
                        goto choicesmade;
                    } else {
                        wait(0.1);
                    }
                }
            }
            if(i==2) {           //green
                red =1;
                green=0;
                blue=1;
                for (int lag=0; lag<TIMEBETWEENBLINK; lag++) {
                    if(emg_bifloat.read()>0.8) {                    //0.8 klopt niet als grenswaarde. #nofilter
                        force = 2;
                        red=0;
                        green = 1;
                        blue = 0;
                        pc.printf("normaal ");
                        wait(TIMEB4NEXTCHOICE);                            // Tijdelijke wait om paars lampje aan te zetten ter controle selectie
                        goto choicesmade;
                    } else {
                        wait(0.1);
                    }
                }
            }
            if(i==3) {           //blue
                red=1;
                green=1;
                blue=0;
                for (int lag=0; lag<TIMEBETWEENBLINK; lag++) {
                    if(emg_bifloat.read()>0.8) {                    //0.8 klopt niet als grenswaarde. #nofilter
                        force = 3;
                        red=0;
                        green = 0;
                        blue = 1;
                        pc.printf("sterk ");
                        wait(TIMEB4NEXTCHOICE);                            // Tijdelijke wait om oranje lampje aan te zetten ter controle selectie
                        goto choicesmade;
                    } else {
                        wait(0.1);
                    }
                }
            }
        }
    }
choicesmade:

    red = 0;
    green = 0;
    blue = 0;           // wit lampje

    if(direction == 0 || force == 0) {
        pc.printf("error");
    }
    if(direction == 1 && force == 1) {            // links zwak
        pc.printf("links zwak");
    }
    if(direction == 1 && force == 2) {            // links normaal
        pc.printf("links normaal");
    }
    if(direction == 1 && force == 3) {            // links sterk
        pc.printf("links sterk");
    }
    if(direction == 2 && force == 1) {            // mid zwak
        pc.printf("mid zwak");
    }
    if(direction == 2 && force == 2) {            // mid normaal
        pc.printf("mid normaal");
    }
    if(direction == 2 && force == 3) {            // mid sterk
        pc.printf("mid sterk");
    }
    if(direction == 3 && force == 1) {            // rechts zwak
        pc.printf("rechts zwak");
    }
    if(direction == 3 && force == 2) {            // rechts normaal
        pc.printf("rechts normaal");
    }
    if(direction == 3 && force == 3) {            // rechts sterk
        pc.printf("rechts sterk");
    }
}