Controls the rovot with pot meters and button, fall back for if the filters fail
Dependencies: mbed
main.cpp@2:90a9ed488f08, 2015-10-29 (annotated)
- Committer:
- NickDGreg
- Date:
- Thu Oct 29 10:52:46 2015 +0000
- Revision:
- 2:90a9ed488f08
- Parent:
- 1:aa0d93df1af1
- Child:
- 3:1f77b5e4eb42
it worked. shoulda checked the plug!! (it was unplugged)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
NickDGreg | 0:7b9ca8e5811b | 1 | #include "mbed.h" |
NickDGreg | 0:7b9ca8e5811b | 2 | |
NickDGreg | 0:7b9ca8e5811b | 3 | Serial pc(USBTX, USBRX); |
NickDGreg | 0:7b9ca8e5811b | 4 | |
NickDGreg | 0:7b9ca8e5811b | 5 | |
NickDGreg | 1:aa0d93df1af1 | 6 | // Define pins for motor control |
NickDGreg | 1:aa0d93df1af1 | 7 | //horizontal motor |
NickDGreg | 0:7b9ca8e5811b | 8 | DigitalOut directionPin(D4); |
NickDGreg | 0:7b9ca8e5811b | 9 | PwmOut PWM(D5); |
NickDGreg | 1:aa0d93df1af1 | 10 | //keypress motor |
NickDGreg | 0:7b9ca8e5811b | 11 | DigitalOut directionPin_key(D7); |
NickDGreg | 0:7b9ca8e5811b | 12 | PwmOut PWM_key(D6); |
NickDGreg | 0:7b9ca8e5811b | 13 | |
NickDGreg | 1:aa0d93df1af1 | 14 | //button to press the key |
NickDGreg | 1:aa0d93df1af1 | 15 | DigitalIn button_key(PTC6); |
NickDGreg | 0:7b9ca8e5811b | 16 | |
NickDGreg | 1:aa0d93df1af1 | 17 | //pot meters to act as emg filter substitutes |
NickDGreg | 0:7b9ca8e5811b | 18 | AnalogIn left_pot(A0); |
NickDGreg | 0:7b9ca8e5811b | 19 | AnalogIn right_pot(A1); |
NickDGreg | 1:aa0d93df1af1 | 20 | |
NickDGreg | 1:aa0d93df1af1 | 21 | //variables to hold the values of the pot meters |
NickDGreg | 0:7b9ca8e5811b | 22 | double emg_left; |
NickDGreg | 0:7b9ca8e5811b | 23 | double emg_right; |
NickDGreg | 0:7b9ca8e5811b | 24 | |
NickDGreg | 1:aa0d93df1af1 | 25 | //int to check if the button is pressed |
NickDGreg | 0:7b9ca8e5811b | 26 | const int Button_move_pressed = 0; |
NickDGreg | 0:7b9ca8e5811b | 27 | |
NickDGreg | 1:aa0d93df1af1 | 28 | //values for clockwise and counter clockwise movement |
NickDGreg | 0:7b9ca8e5811b | 29 | const int cw = 1; |
NickDGreg | 0:7b9ca8e5811b | 30 | const int ccw = 0; |
NickDGreg | 0:7b9ca8e5811b | 31 | |
NickDGreg | 1:aa0d93df1af1 | 32 | |
NickDGreg | 0:7b9ca8e5811b | 33 | |
NickDGreg | 0:7b9ca8e5811b | 34 | int main() |
NickDGreg | 0:7b9ca8e5811b | 35 | { |
NickDGreg | 0:7b9ca8e5811b | 36 | while(1) { |
NickDGreg | 2:90a9ed488f08 | 37 | pc.printf("at the begin"); |
NickDGreg | 0:7b9ca8e5811b | 38 | |
NickDGreg | 1:aa0d93df1af1 | 39 | //read the 'emg' values |
NickDGreg | 0:7b9ca8e5811b | 40 | emg_left = left_pot.read(); |
NickDGreg | 0:7b9ca8e5811b | 41 | emg_right = right_pot.read(); |
NickDGreg | 0:7b9ca8e5811b | 42 | |
NickDGreg | 1:aa0d93df1af1 | 43 | //if left above threashold and right below; move left |
NickDGreg | 0:7b9ca8e5811b | 44 | while (emg_left > 0.5 and emg_right < 0.5) |
NickDGreg | 0:7b9ca8e5811b | 45 | { |
NickDGreg | 0:7b9ca8e5811b | 46 | directionPin.write(cw); |
NickDGreg | 0:7b9ca8e5811b | 47 | PWM.write(1); |
NickDGreg | 1:aa0d93df1af1 | 48 | //new left value to check (in while loop condition) if it has fallen below threashold |
NickDGreg | 0:7b9ca8e5811b | 49 | emg_left = left_pot.read(); |
NickDGreg | 2:90a9ed488f08 | 50 | pc.printf("left \n"); |
NickDGreg | 0:7b9ca8e5811b | 51 | } |
NickDGreg | 0:7b9ca8e5811b | 52 | |
NickDGreg | 0:7b9ca8e5811b | 53 | while (emg_right > 0.5 and emg_left < 0.5) |
NickDGreg | 0:7b9ca8e5811b | 54 | { |
NickDGreg | 0:7b9ca8e5811b | 55 | directionPin.write(ccw); |
NickDGreg | 0:7b9ca8e5811b | 56 | PWM.write(1); |
NickDGreg | 1:aa0d93df1af1 | 57 | //new right value, to check in while loop condition |
NickDGreg | 0:7b9ca8e5811b | 58 | emg_right = right_pot.read(); |
NickDGreg | 2:90a9ed488f08 | 59 | pc.printf("right \n"); |
NickDGreg | 0:7b9ca8e5811b | 60 | } |
NickDGreg | 1:aa0d93df1af1 | 61 | if (button_key.read() == Button_move_pressed) |
NickDGreg | 0:7b9ca8e5811b | 62 | { |
NickDGreg | 0:7b9ca8e5811b | 63 | //move to key |
NickDGreg | 0:7b9ca8e5811b | 64 | directionPin_key.write(cw); |
NickDGreg | 1:aa0d93df1af1 | 65 | PWM_key.write(1); |
NickDGreg | 1:aa0d93df1af1 | 66 | wait(0.14); //time it takes pwm of one to reach the key |
NickDGreg | 1:aa0d93df1af1 | 67 | //pause at the key for one second |
NickDGreg | 1:aa0d93df1af1 | 68 | PWM_key.write(0); |
NickDGreg | 1:aa0d93df1af1 | 69 | wait(1); |
NickDGreg | 1:aa0d93df1af1 | 70 | //move bakc to the starting position |
NickDGreg | 0:7b9ca8e5811b | 71 | directionPin_key.write(ccw); |
NickDGreg | 1:aa0d93df1af1 | 72 | PWM_key.write(1); |
NickDGreg | 1:aa0d93df1af1 | 73 | //time needs to be slightly longer than moving forward (no answer why) |
NickDGreg | 1:aa0d93df1af1 | 74 | wait(0.165f); |
NickDGreg | 2:90a9ed488f08 | 75 | pc.printf("keypress \n"); |
NickDGreg | 0:7b9ca8e5811b | 76 | |
NickDGreg | 0:7b9ca8e5811b | 77 | } |
NickDGreg | 1:aa0d93df1af1 | 78 | |
NickDGreg | 1:aa0d93df1af1 | 79 | /// set spped values of both motors to zero so if no muscles are contracted, no motors move |
NickDGreg | 1:aa0d93df1af1 | 80 | //more of a safety catch than actually necessary |
NickDGreg | 0:7b9ca8e5811b | 81 | PWM_key.write(0); |
NickDGreg | 0:7b9ca8e5811b | 82 | PWM.write(0); |
NickDGreg | 0:7b9ca8e5811b | 83 | } |
NickDGreg | 0:7b9ca8e5811b | 84 | } |
NickDGreg | 0:7b9ca8e5811b | 85 |