Overzetten

Dependencies:   HIDScope MODSERIAL mbed

main.cpp

Committer:
ThomBMT
Date:
2018-10-01
Revision:
1:f6b25b03b8e2
Parent:
0:877f950fdfb5
Child:
2:4d593341fd7a
Child:
3:4f5a67f4170b

File content as of revision 1:f6b25b03b8e2:

#include "mbed.h"
#include "MODSERIAL.h"
#include "HIDScope.h"
MODSERIAL pc(USBTX, USBRX);

DigitalOut DirectionPin1(D4);
PwmOut PwmPin1(D5);
DigitalOut DirectionPin2(D7);
PwmOut PwmPin2(D6);
DigitalIn Knop1(D3);
DigitalIn Knop2(D2);
DigitalIn Knop3(PTA4);
DigitalIn Knop4(PTC6);

//Define objects
AnalogIn    emg0( A0 );
AnalogIn    emg1( A1 );

Ticker      sample_timer;
HIDScope    scope( 2 );
DigitalOut  led(LED1);

/** Sample function
 * this function samples the emg and sends it to HIDScope
 **/
void sample()
{
    /* Set the sampled emg values in channel 0 (the first channel) and 1 (the second channel) in the 'HIDScope' instance named 'scope' */
    scope.set(0, emg0.read() );
    scope.set(1, emg1.read() );
    /* Repeat the step above if required for more channels of required (channel 0 up to 5 = 6 channels) 
    *  Ensure that enough channels are available (HIDScope scope( 2 ))
    *  Finally, send all channels to the PC at once */
    scope.send();
    /* To indicate that the function is working, the LED is toggled */
    led = !led;
}


int main()
{
    
    pc.baud(115200);     
    sample_timer.attach(&sample, 0.002);
    PwmPin1.period_us(120); //60 microseconds pwm period, 16.7 kHz 
    
    for(;;)
    {
        if(Knop1==false) // Motor 1 rotates CW
        {
            float u = 0.8f; //determine useful value, this is not final
            DirectionPin1 = u > 0.0f; //either true or false
                                    // True = CW, for False = CW
            PwmPin1 = fabs(u);
            PwmPin2 = fabs(0.0); 
        }
        
        else if (Knop2==false)// We see that Motor2 keeps rotating if we leave out the "else" statement, somehow the signal leaks
        {
            float u = 0.8f;
            DirectionPin2 = u < 0.0f;
            PwmPin1 = fabs(0.0);
            PwmPin2 = fabs(u);
        }
    
        else if (Knop3==false)
        {
            float u = 0.8f;
            DirectionPin1 = u < 0.0f;
            PwmPin1 = fabs(u);
            PwmPin2 = fabs(0.0);  
        }
        
        else if (Knop4==false)
        {
            float u = 0.8f;
            DirectionPin2 = u > 0.0f;
            PwmPin1 = fabs(0.0);
            PwmPin2 = fabs(u);

        }
        
        else
        {  
            float u = 0.0f;
            PwmPin1 = PwmPin2 = fabs(u);
        }
    }
}