the slap

Dependencies:   Encoder HIDScope MODSERIAL TextLCD mbed-dsp mbed

Fork of The_SLAP_5_1 by Daan

main.cpp

Committer:
Daanmk
Date:
2014-10-27
Revision:
4:a0b0c944846e
Parent:
3:81a6009303a9
Child:
5:5383d9a80307

File content as of revision 4:a0b0c944846e:

/***************************************/
/*                                     */
/*   BRONCODE GROEP 5, MODULE 9, 2014  */
/*       *****-THE SLAP-******         */
/*                                     */
/* -Dominique Clevers                  */
/* -Rianne van Dommelen                */
/* -Daan de Muinck Keizer              */
/* -David den Houting                  */
/* -Marjolein Thijssen                 */
/***************************************/
#include "mbed.h"
#include "HIDScope.h"
#include "arm_math.h"
#include "encoder.h"
#include "MODSERIAL.h"
#include "TextLCD.h"

#define M2_PWM PTC8 //blauw
#define M2_DIR PTC9 //groen
#define M1_PWM PTA5 //kleine motor
#define M1_DIR PTA4 //kleine motor
#define TSAMP 0.005  // Sampletijd, 200Hz

TextLCD lcd(PTE5, PTE3, PTE2, PTB11, PTB10, PTB9); // rs, e, d4-d7 CONTROLEREN!! (Pinnen wel vrij :) )! //TextLCD lcd(p15, p16, p17, p18, p19, p20, TextLCD::LCD16x4); // rs, e, d4-d7 ok

Encoder motor2(PTD2,PTD0); //geel,wit kleine mtor
Encoder motor1(PTD5,PTA13);//geel,wit
PwmOut pwm_motor1(M1_PWM);
PwmOut pwm_motor2(M2_PWM);
DigitalOut motordir2(M2_DIR);
DigitalOut motordir1(M1_DIR);
AnalogIn emg0(PTB0); //Biceps
AnalogIn emg1(PTB1); //Triceps
HIDScope scope(6);

MODSERIAL pc(USBTX,USBRX,64,1024);


float emg0_value_f32,filtered_emg0_notch,filtered_emg0_notch_highpass,filtered_emg0_notch_highpass_lowpass,filtered_emg0_eindsignaal_abs,envelop_emg0,pwm_to_motor1,max_value_biceps,min_value_biceps; //variable to store value in for biceps 
float emg1_value_f32,filtered_emg1_notch,filtered_emg1_notch_highpass,filtered_emg1_notch_highpass_lowpass,filtered_emg1_eindsignaal_abs,envelop_emg1,pwm_to_motor2,max_value_triceps,min_value_triceps; //variable to store value in for triceps 

arm_biquad_casd_df1_inst_f32 notch_biceps;
arm_biquad_casd_df1_inst_f32 notch_triceps;
float notch_const[] = {0.9695312529087462, -0.0, 0.9695312529087462, 0.0, -0.9390625058174924}; //constants for 50Hz notch
float notch_biceps_states[4]; //state values
float notch_triceps_states[4];

arm_biquad_casd_df1_inst_f32 highpass_biceps;
arm_biquad_casd_df1_inst_f32 highpass_triceps;
//constants for 20Hz highpass
float highpass_const[] = {0.638945525159022, -1.277891050318045, 0.638945525159022, 1.142980502539901, -0.412801598096189};
//state values
float highpass_biceps_states[4];
float highpass_triceps_states[4];
//constants for 80Hz lowpass
arm_biquad_casd_df1_inst_f32 lowpass_biceps;
arm_biquad_casd_df1_inst_f32 lowpass_triceps;
float lowpass_const[] = {0.638945525159022, 1.277891050318045, 0.638945525159022, -1.142980502539901, -0.412801598096189};
//state values
float lowpass_biceps_states[4];
float lowpass_triceps_states[4];

arm_biquad_casd_df1_inst_f32 envelop_biceps;
arm_biquad_casd_df1_inst_f32 envelop_triceps;
float envelop_const[] = {0.005542711916075981, 0.011085423832151962, 0.005542711916075981, 1.7786300789392977, -0.8008009266036016};
float envelop_biceps_states[4];
float envelop_triceps_states[4];    

enum slapstates {RUST,RUSTMETING,KALIBRATIE,BICEPSMAX,TRICEPSMAX,RICHTEN,SLAAN}; //verschillende stadia definieren voor gebruik in CASES
uint8_t state=RUST;

volatile bool looptimerflag;
void setlooptimerflag(void)
{
    looptimerflag = true;
}

void keep_in_range(float * in, float min, float max); //keep in range

void emgmeten(){
    /*put raw emg value in emg_value*/
    emg0_value_f32 = emg0.read();   
    emg1_value_f32 = emg1.read();
 
    //process emg biceps
    arm_biquad_cascade_df1_f32(&notch_biceps, &emg0_value_f32, &filtered_emg0_notch, 1 );
    arm_biquad_cascade_df1_f32(&highpass_biceps, &filtered_emg0_notch, &filtered_emg0_notch_highpass, 1 );
    arm_biquad_cascade_df1_f32(&lowpass_biceps, &filtered_emg0_notch_highpass, &filtered_emg0_notch_highpass_lowpass, 1 );
    filtered_emg0_eindsignaal_abs = fabs(filtered_emg0_notch_highpass_lowpass);  //gelijkrichter
    arm_biquad_cascade_df1_f32(&envelop_biceps, &filtered_emg0_eindsignaal_abs, &envelop_emg0, 1 );
    
    //process emg triceps
    arm_biquad_cascade_df1_f32(&notch_triceps, &emg1_value_f32, &filtered_emg1_notch, 1 );
    arm_biquad_cascade_df1_f32(&highpass_triceps, &filtered_emg1_notch, &filtered_emg1_notch_highpass, 1 );
    arm_biquad_cascade_df1_f32(&lowpass_triceps, &filtered_emg1_notch_highpass, &filtered_emg1_notch_highpass_lowpass, 1 );
    filtered_emg1_eindsignaal_abs = fabs(filtered_emg1_notch_highpass_lowpass);  //gelijkrichter
    arm_biquad_cascade_df1_f32(&envelop_triceps, &filtered_emg1_eindsignaal_abs, &envelop_emg1, 1 );
    }

int main()
{
    while(1) {
        pc.baud(38400); //PC baud rate is 38400 bits/seconde
        Ticker emg_timer;
        emg_timer.attach(emgmeten, TSAMP);
        Ticker looptimer;
        looptimer.attach(setlooptimerflag,TSAMP);
        Timer tijdtimer;
        arm_biquad_cascade_df1_init_f32(&notch_biceps,1 , notch_const, notch_biceps_states);
        arm_biquad_cascade_df1_init_f32(&highpass_biceps,1 ,highpass_const,highpass_biceps_states);
        arm_biquad_cascade_df1_init_f32(&lowpass_biceps,1 ,lowpass_const,lowpass_biceps_states);
        arm_biquad_cascade_df1_init_f32(&notch_triceps,1 , notch_const, notch_triceps_states);
        arm_biquad_cascade_df1_init_f32(&highpass_triceps,1 ,highpass_const,highpass_triceps_states);
        arm_biquad_cascade_df1_init_f32(&lowpass_triceps,1 ,lowpass_const,lowpass_triceps_states);
        arm_biquad_cascade_df1_init_f32(&envelop_triceps,1 ,envelop_const,envelop_triceps_states);
        arm_biquad_cascade_df1_init_f32(&envelop_biceps,1 ,envelop_const,envelop_biceps_states);
        
        switch(state) {
            case RUST: {                        //Aanzetten
                
                state = KALIBRATIE;
                break;                         
            }                               

            case KALIBRATIE: {
                lcd.cls();
                lcd.locate(0,0);         
                lcd.printf("Kalibratie");   //regel 1 LCD scherm
                lcd.locate(0,1);
                lcd.printf("1:BICEPS RUST");             //regel 2 LCD scherm
                wait(1); 
                max_value_biceps=0;
                max_value_triceps=0;
                min_value_biceps=0;
                min_value_triceps=0;
                int metingstatus=1;
                
                if (metingstatus==1) { //BICEPS RUST METING
                
                    lcd.cls();
                    lcd.locate(0,0);         
                    lcd.printf("BICEPS RUST");   //regel 1 LCD scherm
                    lcd.locate(0,1);
                    lcd.printf("ONTSPAN! (3SEC)");             //regel 2 LCD scherm
                    wait(1); 
                    tijdtimer.start();
                    
                    if (envelop_emg0 > min_value_biceps) {
                        min_value_biceps = envelop_emg0; 
                        }
                    if (tijdtimer == 3 ) {
                        tijdtimer.stop();
                        tijdtimer.reset();
                        metingstatus=2; 
                        }
                    }
                if (metingstatus==2) { //TRICEPS RUST METING
                    lcd.cls();
                    lcd.locate(0,0);         
                    lcd.printf("TRICEPS RUST");   //regel 1 LCD scherm
                    lcd.locate(0,1);
                    lcd.printf("ONTSPAN! (3SEC)");             //regel 2 LCD scherm
                    wait(1);                 
                    tijdtimer.start();
                    if (envelop_emg1 > min_value_triceps) {
                        min_value_triceps = envelop_emg1; }
                    if (tijdtimer == 3 ) {
                        tijdtimer.stop();
                        tijdtimer.reset();
                        metingstatus=3; 
                        }
                if (metingstatus==3) { //BICEPS KRACHT METING
                    lcd.cls();
                    lcd.locate(0,0);         
                    lcd.printf("BICEPS MAX");   //regel 1 LCD scherm
                    lcd.locate(0,1);
                    lcd.printf("SPAN AAN! (3SEC)");             //regel 2 LCD scherm
                    wait(1);                 
                    tijdtimer.start();
                    if (envelop_emg0 > max_value_biceps) {
                        max_value_biceps = envelop_emg0; }
                    if (tijdtimer == 3 ) {
                        tijdtimer.stop();
                        tijdtimer.reset();
                        metingstatus=4; 
                        }
                if (metingstatus==4) {//TRICEPS KRACHT METING
                    lcd.cls();
                    lcd.locate(0,0);         
                    lcd.printf("TRICEPS MAX");   //regel 1 LCD scherm
                    lcd.locate(0,1);
                    lcd.printf("SPAN AAN! (3SEC)");             //regel 2 LCD scherm                
                    tijdtimer.start();
                    if (envelop_emg1 > max_value_triceps) {
                        max_value_triceps = envelop_emg1; }
                    if (tijdtimer == 3 ) {
                        tijdtimer.stop();
                        tijdtimer.reset();
                        metingstatus=5; 
                        }                       
                if (metingstatus==5) {
                    state = RICHTEN; }                      
                break;                          
            }                               

           case RICHTEN: {                   //Batje richten
                lcd.printf("Richten");
                wait(1);
                max_value = 0;
                
                    
                keep_in_range(&pwm_to_motor, -1,1);
                if(pwm_to_motor > 0)
                    motordir1.write(1);
                else
                    motordir1.write(0);
                pwm_motor1.write(abs(pwm_to_motor));      
                state = SLAAN;
           break; 
            }                              
           case SLAAN: {                    //Balletje slaan
                lcd.printf("Slaan!");
                wait(1);
                state = RICHTEN;
           break; 
            }  

           default: {
                state = RUST;
            }                               

        }                              

    }                               
}