EMG aansluiten op motor
Dependencies: FastPWM HIDScope MODSERIAL mbed
main.cpp@1:83531c955134, 2018-10-11 (annotated)
- Committer:
- MAHCSnijders
- Date:
- Thu Oct 11 14:17:03 2018 +0000
- Revision:
- 1:83531c955134
- Parent:
- 0:73cf7fc57ab7
- Child:
- 2:9c1bdcf6bc26
Added button interrupts + LED check.; NOT tested yet.; EMG NOT scaled/filtered.
Who changed what in which revision?
User | Revision | Line number | New 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 | 1:83531c955134 | 11 | AnalogIn emg0( A0 ); // EMG on Bicep 1 |
MAHCSnijders | 1:83531c955134 | 12 | AnalogIn emg1( A1 ); // EMG on Bicep 2 |
MAHCSnijders | 1:83531c955134 | 13 | DigitalOut led(LED1); // Check for Sample function |
MAHCSnijders | 1:83531c955134 | 14 | InterruptIn button1(PTC6); // Interrupt 1 for EMG 1 |
MAHCSnijders | 1:83531c955134 | 15 | DigitalOut ledb1(LED_RED); // Check for button_pressed1 function |
MAHCSnijders | 1:83531c955134 | 16 | InterruptIn button2(PTA4); // Interrupt 2 for EMG 2 |
MAHCSnijders | 1:83531c955134 | 17 | DigitalOut ledb2(LED_GREEN); // check for button_pressed2 function |
MAHCSnijders | 0:73cf7fc57ab7 | 18 | |
MAHCSnijders | 0:73cf7fc57ab7 | 19 | // Define variables |
MAHCSnijders | 0:73cf7fc57ab7 | 20 | Ticker sample_timer; // Ticker for EMG sample function |
MAHCSnijders | 0:73cf7fc57ab7 | 21 | HIDScope scope( 2 ); // HIDScope for EMG |
MAHCSnijders | 0:73cf7fc57ab7 | 22 | Ticker motor; // Ticker function for motor function |
MAHCSnijders | 0:73cf7fc57ab7 | 23 | volatile float u1; // Motor 1 control signal |
MAHCSnijders | 0:73cf7fc57ab7 | 24 | volatile float u2; // Motor 2 control signal |
MAHCSnijders | 0:73cf7fc57ab7 | 25 | |
MAHCSnijders | 0:73cf7fc57ab7 | 26 | void sample() // Function for getting EMG signals |
MAHCSnijders | 0:73cf7fc57ab7 | 27 | { |
MAHCSnijders | 0:73cf7fc57ab7 | 28 | /* 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 | 29 | scope.set(0, emg0.read() ); |
MAHCSnijders | 0:73cf7fc57ab7 | 30 | scope.set(1, emg1.read() ); |
MAHCSnijders | 0:73cf7fc57ab7 | 31 | /* Repeat the step above if required for more channels of required (channel 0 up to 5 = 6 channels) |
MAHCSnijders | 0:73cf7fc57ab7 | 32 | * Ensure that enough channels are available (HIDScope scope( 2 )) |
MAHCSnijders | 0:73cf7fc57ab7 | 33 | * Finally, send all channels to the PC at once */ |
MAHCSnijders | 0:73cf7fc57ab7 | 34 | scope.send(); |
MAHCSnijders | 0:73cf7fc57ab7 | 35 | /* To indicate that the function is working, the LED is toggled */ |
MAHCSnijders | 0:73cf7fc57ab7 | 36 | led = !led; |
MAHCSnijders | 0:73cf7fc57ab7 | 37 | } |
MAHCSnijders | 0:73cf7fc57ab7 | 38 | |
MAHCSnijders | 1:83531c955134 | 39 | void button_pressed1() // Indicates direction of motor 1 becomes negative |
MAHCSnijders | 1:83531c955134 | 40 | { |
MAHCSnijders | 1:83531c955134 | 41 | ledb1 = 0; // Turns on red led as indication of negative direction for motor 1 |
MAHCSnijders | 1:83531c955134 | 42 | u1 = -u1; // Direction for motor 1 becomes negative |
MAHCSnijders | 1:83531c955134 | 43 | } |
MAHCSnijders | 1:83531c955134 | 44 | |
MAHCSnijders | 1:83531c955134 | 45 | void button_pressed2() // Indicates direction of motor 2 becomes negative |
MAHCSnijders | 1:83531c955134 | 46 | { |
MAHCSnijders | 1:83531c955134 | 47 | ledb2 = 0; // Turns on green led as indication of negative direction for motor 2 |
MAHCSnijders | 1:83531c955134 | 48 | u2 = -u2; // Direction for motor 2 becomes negative |
MAHCSnijders | 1:83531c955134 | 49 | } |
MAHCSnijders | 1:83531c955134 | 50 | |
MAHCSnijders | 0:73cf7fc57ab7 | 51 | void motorfunction() // Function for motor control |
MAHCSnijders | 0:73cf7fc57ab7 | 52 | { |
MAHCSnijders | 0:73cf7fc57ab7 | 53 | u1 = EMG1_scale; // motor control signal |
MAHCSnijders | 0:73cf7fc57ab7 | 54 | u2 = EMG2_scale; // motor control signal |
MAHCSnijders | 0:73cf7fc57ab7 | 55 | directionpin1 = u1 > 0.0f; // either true or false, determines direction (0 or 1) |
MAHCSnijders | 0:73cf7fc57ab7 | 56 | directionpin2 = u2 > 0.0f; // either true or false, determines direction (0 or 1) |
MAHCSnijders | 0:73cf7fc57ab7 | 57 | 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 | 58 | 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 | 59 | } |
MAHCSnijders | 0:73cf7fc57ab7 | 60 | |
MAHCSnijders | 0:73cf7fc57ab7 | 61 | int main() |
MAHCSnijders | 0:73cf7fc57ab7 | 62 | { |
MAHCSnijders | 0:73cf7fc57ab7 | 63 | sample_timer.attach(&sample, 0.002); // 500 Hz |
MAHCSnijders | 0:73cf7fc57ab7 | 64 | 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 | 65 | motor.attach(motorfunction,0.5); |
MAHCSnijders | 1:83531c955134 | 66 | button1.fall(&button_pressed1); // whenever button 1 falls, execute button_pressed1 function (blinks led red and changes motor direction) |
MAHCSnijders | 1:83531c955134 | 67 | button2.fall(&button_pressed2); // whenever button 2 falls, execute button_pressed1 function (blinks led green and changes motor direction) |
MAHCSnijders | 0:73cf7fc57ab7 | 68 | while(1) {} |
MAHCSnijders | 0:73cf7fc57ab7 | 69 | } |