EMG aansluiten op motor

Dependencies:   FastPWM HIDScope MODSERIAL mbed

Committer:
MAHCSnijders
Date:
Thu Oct 11 13:58:41 2018 +0000
Revision:
0:73cf7fc57ab7
Child:
1:83531c955134
Version 1, not tested yet; Incomplete

Who changed what in which revision?

UserRevisionLine numberNew contents of line
MAHCSnijders 0:73cf7fc57ab7 1 #include "mbed.h"
MAHCSnijders 0:73cf7fc57ab7 2 #include "FastPWM.h"
MAHCSnijders 0:73cf7fc57ab7 3 #include "MODSERIAL.h"
MAHCSnijders 0:73cf7fc57ab7 4 #include "HIDScope.h"
MAHCSnijders 0:73cf7fc57ab7 5
MAHCSnijders 0:73cf7fc57ab7 6 // Define Pins
MAHCSnijders 0:73cf7fc57ab7 7 FastPWM pwmpin1(D5); // SPECIFIC PIN (hoeft niet aangesloten te worden) Tells you how fast the motor has to go (later: pwmpin.write will tell you the duty cycle, aka how much voltage the motor gets)
MAHCSnijders 0:73cf7fc57ab7 8 FastPWM pwmpin2(D6); // SPECIFIC PIN (hoeft niet aangesloten te worden) Tells you how fast the motor has to go (later: pwmpin.write will tell you the duty cycle, aka how much voltage the motor gets)
MAHCSnijders 0:73cf7fc57ab7 9 DigitalOut directionpin1(D4); // SPECIFIC PIN (hoeft niet aangesloten te worden) Direction value (0-1) that the mbed will give the motor: in which direction the motor must rotate
MAHCSnijders 0:73cf7fc57ab7 10 DigitalOut directionpin2(D7); // SPECIFIC PIN (hoeft niet aangesloten te worden) Direction value (0-1) that the mbed will give the motor: in which direction the motor must rotate
MAHCSnijders 0:73cf7fc57ab7 11 AnalogIn emg0( A0 ); // EMG on Bicep 1
MAHCSnijders 0:73cf7fc57ab7 12 AnalogIn emg1( A1 ); // EMG on Bicep 2
MAHCSnijders 0:73cf7fc57ab7 13 DigitalOut led(LED1); // Check for Sample function
MAHCSnijders 0:73cf7fc57ab7 14 InterruptIn button1(PTC6); // Interrupt1 for EMG 1
MAHCSnijders 0:73cf7fc57ab7 15 InterruptIn button2(
MAHCSnijders 0:73cf7fc57ab7 16
MAHCSnijders 0:73cf7fc57ab7 17 // Define variables
MAHCSnijders 0:73cf7fc57ab7 18 Ticker sample_timer; // Ticker for EMG sample function
MAHCSnijders 0:73cf7fc57ab7 19 HIDScope scope( 2 ); // HIDScope for EMG
MAHCSnijders 0:73cf7fc57ab7 20 Ticker motor; // Ticker function for motor function
MAHCSnijders 0:73cf7fc57ab7 21 volatile float u1; // Motor 1 control signal
MAHCSnijders 0:73cf7fc57ab7 22 volatile float u2; // Motor 2 control signal
MAHCSnijders 0:73cf7fc57ab7 23
MAHCSnijders 0:73cf7fc57ab7 24 void sample() // Function for getting EMG signals
MAHCSnijders 0:73cf7fc57ab7 25 {
MAHCSnijders 0:73cf7fc57ab7 26 /* Set the sampled emg values in channel 0 (the first channel) and 1 (the second channel) in the 'HIDScope' instance named 'scope' */
MAHCSnijders 0:73cf7fc57ab7 27 scope.set(0, emg0.read() );
MAHCSnijders 0:73cf7fc57ab7 28 scope.set(1, emg1.read() );
MAHCSnijders 0:73cf7fc57ab7 29 /* Repeat the step above if required for more channels of required (channel 0 up to 5 = 6 channels)
MAHCSnijders 0:73cf7fc57ab7 30 * Ensure that enough channels are available (HIDScope scope( 2 ))
MAHCSnijders 0:73cf7fc57ab7 31 * Finally, send all channels to the PC at once */
MAHCSnijders 0:73cf7fc57ab7 32 scope.send();
MAHCSnijders 0:73cf7fc57ab7 33 /* To indicate that the function is working, the LED is toggled */
MAHCSnijders 0:73cf7fc57ab7 34 led = !led;
MAHCSnijders 0:73cf7fc57ab7 35 }
MAHCSnijders 0:73cf7fc57ab7 36
MAHCSnijders 0:73cf7fc57ab7 37 void motorfunction() // Function for motor control
MAHCSnijders 0:73cf7fc57ab7 38 {
MAHCSnijders 0:73cf7fc57ab7 39 u1 = EMG1_scale; // motor control signal
MAHCSnijders 0:73cf7fc57ab7 40 u2 = EMG2_scale; // motor control signal
MAHCSnijders 0:73cf7fc57ab7 41 directionpin1 = u1 > 0.0f; // either true or false, determines direction (0 or 1)
MAHCSnijders 0:73cf7fc57ab7 42 directionpin2 = u2 > 0.0f; // either true or false, determines direction (0 or 1)
MAHCSnijders 0:73cf7fc57ab7 43 pwmpin1 = fabs(u1); // pwm duty cycle can only be positive, floating point absolute value (if value is >0, there still will be a positive value).
MAHCSnijders 0:73cf7fc57ab7 44 pwmpin2 = fabs(u2); // pwm duty cycle can only be positive, floating point absolute value (if value is >0, there still will be a positive value).
MAHCSnijders 0:73cf7fc57ab7 45 }
MAHCSnijders 0:73cf7fc57ab7 46
MAHCSnijders 0:73cf7fc57ab7 47 int main()
MAHCSnijders 0:73cf7fc57ab7 48 {
MAHCSnijders 0:73cf7fc57ab7 49 sample_timer.attach(&sample, 0.002); // 500 Hz
MAHCSnijders 0:73cf7fc57ab7 50 pwmpin1.period_us(60.0); //60 microseconds PWM period, 16.7 kHz, defines all PWM pins (only needs to be done once)
MAHCSnijders 0:73cf7fc57ab7 51 motor.attach(motorfunction,0.5);
MAHCSnijders 0:73cf7fc57ab7 52 while(1) {}
MAHCSnijders 0:73cf7fc57ab7 53 }