Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: FastPWM HIDScope MODSERIAL mbed
main.cpp@3:4b3ccfbae1c1, 2018-10-16 (annotated)
- Committer:
- MAHCSnijders
- Date:
- Tue Oct 16 11:34:12 2018 +0000
- Revision:
- 3:4b3ccfbae1c1
- Parent:
- 2:9c1bdcf6bc26
Alles bij elkaar so far
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 | 2:9c1bdcf6bc26 | 7 | FastPWM pwmpin1(D5); // SPECIFIC PIN (does not need to be plugged in) 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 | 2:9c1bdcf6bc26 | 8 | FastPWM pwmpin2(D6); // SPECIFIC PIN (does not need to be plugged in) 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 | 2:9c1bdcf6bc26 | 9 | DigitalOut directionpin1(D4); // SPECIFIC PIN (does not need to be plugged in) Direction value (0-1) that the mbed will give the motor: in which direction the motor must rotate |
| MAHCSnijders | 2:9c1bdcf6bc26 | 10 | DigitalOut directionpin2(D7); // SPECIFIC PIN (does not need to be plugged in) Direction value (0-1) that the mbed will give the motor: in which direction the motor must rotate |
| MAHCSnijders | 2:9c1bdcf6bc26 | 11 | AnalogIn emg0( A0 ); // EMG on Bicep 1a |
| MAHCSnijders | 2:9c1bdcf6bc26 | 12 | AnalogIn emg1( A1 ); // EMG on Bicep 1b |
| MAHCSnijders | 3:4b3ccfbae1c1 | 13 | //AnalogIn emg2( A2 ); // EMG on Bicep 2a CHECK IF THIS WORKS |
| MAHCSnijders | 3:4b3ccfbae1c1 | 14 | //AnalogIn emg3( A3 ); // EMG on Bicep 2b CHECK IF THIS WORKS |
| MAHCSnijders | 2:9c1bdcf6bc26 | 15 | DigitalOut led(LED1); // LED check for Sample function |
| MAHCSnijders | 2:9c1bdcf6bc26 | 16 | InterruptIn button1(PTC6); // Button interrupt for EMG 1 |
| MAHCSnijders | 3:4b3ccfbae1c1 | 17 | //InterruptIn button2(PTA4); // Button interrupt for EMG 2 |
| MAHCSnijders | 2:9c1bdcf6bc26 | 18 | DigitalOut ledb1(LED_RED); // LED check for button_pressed1 function |
| MAHCSnijders | 3:4b3ccfbae1c1 | 19 | //DigitalOut ledb2(LED_GREEN); // LED check for button_pressed2 function |
| MAHCSnijders | 0:73cf7fc57ab7 | 20 | |
| MAHCSnijders | 0:73cf7fc57ab7 | 21 | // Define variables |
| MAHCSnijders | 0:73cf7fc57ab7 | 22 | Ticker sample_timer; // Ticker for EMG sample function |
| MAHCSnijders | 3:4b3ccfbae1c1 | 23 | HIDScope scope( 2 ); // HIDScope for EMG |
| MAHCSnijders | 0:73cf7fc57ab7 | 24 | Ticker motor; // Ticker function for motor function |
| MAHCSnijders | 0:73cf7fc57ab7 | 25 | volatile float u1; // Motor 1 control signal |
| MAHCSnijders | 3:4b3ccfbae1c1 | 26 | //volatile float u2; // Motor 2 control signal |
| MAHCSnijders | 0:73cf7fc57ab7 | 27 | |
| MAHCSnijders | 0:73cf7fc57ab7 | 28 | void sample() // Function for getting EMG signals |
| MAHCSnijders | 0:73cf7fc57ab7 | 29 | { |
| MAHCSnijders | 2:9c1bdcf6bc26 | 30 | // 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 | 31 | scope.set(0, emg0.read() ); |
| MAHCSnijders | 0:73cf7fc57ab7 | 32 | scope.set(1, emg1.read() ); |
| MAHCSnijders | 3:4b3ccfbae1c1 | 33 | // scope.set(0, emg2.read() ); // CHECK IF THIS WORKS |
| MAHCSnijders | 3:4b3ccfbae1c1 | 34 | // scope.set(1, emg3.read() ); // CHECK IF THIS WORKS |
| MAHCSnijders | 2:9c1bdcf6bc26 | 35 | // Repeat the step above if required for more channels of required (channel 0 up to 5 = 6 channels) |
| MAHCSnijders | 2:9c1bdcf6bc26 | 36 | // Ensure that enough channels are available (HIDScope scope( 2 )) |
| MAHCSnijders | 2:9c1bdcf6bc26 | 37 | // Finally, send all channels to the PC at once |
| MAHCSnijders | 0:73cf7fc57ab7 | 38 | scope.send(); |
| MAHCSnijders | 2:9c1bdcf6bc26 | 39 | // To indicate that the function is working, the LED is toggled |
| MAHCSnijders | 0:73cf7fc57ab7 | 40 | led = !led; |
| MAHCSnijders | 0:73cf7fc57ab7 | 41 | } |
| MAHCSnijders | 0:73cf7fc57ab7 | 42 | |
| MAHCSnijders | 2:9c1bdcf6bc26 | 43 | void button_pressed1() // Indicates direction of motor 1 becomes negative |
| MAHCSnijders | 1:83531c955134 | 44 | { |
| MAHCSnijders | 1:83531c955134 | 45 | ledb1 = 0; // Turns on red led as indication of negative direction for motor 1 |
| MAHCSnijders | 2:9c1bdcf6bc26 | 46 | directionpin1 = 0; // Direction for motor 1 becomes negative CHECK IF THIS WORKS |
| MAHCSnijders | 1:83531c955134 | 47 | } |
| MAHCSnijders | 1:83531c955134 | 48 | |
| MAHCSnijders | 3:4b3ccfbae1c1 | 49 | //void button_pressed2() // Indicates direction of motor 2 becomes negative |
| MAHCSnijders | 3:4b3ccfbae1c1 | 50 | // { |
| MAHCSnijders | 3:4b3ccfbae1c1 | 51 | // ledb2 = 0; // Turns on green led as indication of negative direction for motor 2 |
| MAHCSnijders | 3:4b3ccfbae1c1 | 52 | // directionpin2 = 0; // Direction for motor 2 becomes negative CHECK IF THIS WORKS |
| MAHCSnijders | 3:4b3ccfbae1c1 | 53 | // } |
| MAHCSnijders | 1:83531c955134 | 54 | |
| MAHCSnijders | 0:73cf7fc57ab7 | 55 | void motorfunction() // Function for motor control |
| MAHCSnijders | 0:73cf7fc57ab7 | 56 | { |
| MAHCSnijders | 3:4b3ccfbae1c1 | 57 | u1 = emg0.read(); // EMG1_scale; // Motor control signal |
| MAHCSnijders | 3:4b3ccfbae1c1 | 58 | // u2 = emg0.read(); // EMG2_scale; // Motor control signal |
| MAHCSnijders | 2:9c1bdcf6bc26 | 59 | 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 | 3:4b3ccfbae1c1 | 60 | // 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 | 61 | } |
| MAHCSnijders | 0:73cf7fc57ab7 | 62 | |
| MAHCSnijders | 0:73cf7fc57ab7 | 63 | int main() |
| MAHCSnijders | 0:73cf7fc57ab7 | 64 | { |
| MAHCSnijders | 3:4b3ccfbae1c1 | 65 | led = 0; |
| MAHCSnijders | 3:4b3ccfbae1c1 | 66 | ledb1 = 0; |
| MAHCSnijders | 3:4b3ccfbae1c1 | 67 | // ledb2 = 1; |
| MAHCSnijders | 0:73cf7fc57ab7 | 68 | sample_timer.attach(&sample, 0.002); // 500 Hz |
| MAHCSnijders | 2:9c1bdcf6bc26 | 69 | 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 | 70 | motor.attach(motorfunction,0.5); |
| MAHCSnijders | 2:9c1bdcf6bc26 | 71 | button1.fall(&button_pressed1); // Whenever button 1 falls, execute button_pressed1 function (blinks led red and changes motor direction) |
| MAHCSnijders | 3:4b3ccfbae1c1 | 72 | // button2.fall(&button_pressed2); // Whenever button 2 falls, execute button_pressed1 function (blinks led green and changes motor direction) |
| MAHCSnijders | 0:73cf7fc57ab7 | 73 | while(1) {} |
| MAHCSnijders | 0:73cf7fc57ab7 | 74 | } |