
Code to fire the ball
main.cpp@1:3db7a3534296, 2017-11-28 (annotated)
- Committer:
- ihmclachlan
- Date:
- Tue Nov 28 16:33:52 2017 +0000
- Revision:
- 1:3db7a3534296
- Parent:
- 0:6ca20e4aa3f0
change slight code for servo;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ihmclachlan | 0:6ca20e4aa3f0 | 1 | /************************************************************** |
ihmclachlan | 0:6ca20e4aa3f0 | 2 | / ME31001 Group: 1-04 |
ihmclachlan | 0:6ca20e4aa3f0 | 3 | / |
ihmclachlan | 0:6ca20e4aa3f0 | 4 | / Final Project |
ihmclachlan | 0:6ca20e4aa3f0 | 5 | / |
ihmclachlan | 0:6ca20e4aa3f0 | 6 | / The program controls the operation of a ball launcher that fires a |
ihmclachlan | 0:6ca20e4aa3f0 | 7 | ball in a random direction and changes distance fired based on how long |
ihmclachlan | 0:6ca20e4aa3f0 | 8 | it took ball to be returned |
ihmclachlan | 0:6ca20e4aa3f0 | 9 | /**************************************************************/ |
ihmclachlan | 0:6ca20e4aa3f0 | 10 | |
ihmclachlan | 0:6ca20e4aa3f0 | 11 | #include "mbed.h" |
ihmclachlan | 0:6ca20e4aa3f0 | 12 | #include "Servo.h" |
ihmclachlan | 0:6ca20e4aa3f0 | 13 | #include "stdlib.h" |
ihmclachlan | 0:6ca20e4aa3f0 | 14 | #include "time.h" |
ihmclachlan | 0:6ca20e4aa3f0 | 15 | |
ihmclachlan | 0:6ca20e4aa3f0 | 16 | |
ihmclachlan | 0:6ca20e4aa3f0 | 17 | PwmOut FMotor(p23); // set the firing motors as an output |
ihmclachlan | 0:6ca20e4aa3f0 | 18 | Servo myservo1 (p22); // servo to rotate the base |
ihmclachlan | 0:6ca20e4aa3f0 | 19 | Servo myservo2 (p21); // servo to release the ball |
ihmclachlan | 0:6ca20e4aa3f0 | 20 | |
ihmclachlan | 0:6ca20e4aa3f0 | 21 | DigitalIn PB(p17); // a push button that when switched turns on the program and when switched again turns it off |
ihmclachlan | 0:6ca20e4aa3f0 | 22 | DigitalIn LDR(p19); // An LDR sensor to check if the ball is in the right position |
ihmclachlan | 0:6ca20e4aa3f0 | 23 | |
ihmclachlan | 0:6ca20e4aa3f0 | 24 | Timer t; // we need a timer to see how long it has been since the ball was in the chute |
ihmclachlan | 0:6ca20e4aa3f0 | 25 | |
ihmclachlan | 0:6ca20e4aa3f0 | 26 | int main() { |
ihmclachlan | 0:6ca20e4aa3f0 | 27 | |
ihmclachlan | 0:6ca20e4aa3f0 | 28 | int x = 0; // a counter to check if the push button switch has been pressed |
ihmclachlan | 0:6ca20e4aa3f0 | 29 | |
ihmclachlan | 0:6ca20e4aa3f0 | 30 | while(1) { |
ihmclachlan | 0:6ca20e4aa3f0 | 31 | |
ihmclachlan | 0:6ca20e4aa3f0 | 32 | if (PB.read() == 1) //if the pushbutton is pressed then we change the variable x |
ihmclachlan | 0:6ca20e4aa3f0 | 33 | { |
ihmclachlan | 0:6ca20e4aa3f0 | 34 | x = !x; |
ihmclachlan | 0:6ca20e4aa3f0 | 35 | wait (0.25); |
ihmclachlan | 0:6ca20e4aa3f0 | 36 | } |
ihmclachlan | 0:6ca20e4aa3f0 | 37 | |
ihmclachlan | 0:6ca20e4aa3f0 | 38 | |
ihmclachlan | 0:6ca20e4aa3f0 | 39 | if (PB.read() == 0 && x == 0) // if the pushbutton is not currently pressed and x is in the 0 state run the rest of the program |
ihmclachlan | 0:6ca20e4aa3f0 | 40 | { |
ihmclachlan | 0:6ca20e4aa3f0 | 41 | if (LDR) { // if the ball is in position |
ihmclachlan | 0:6ca20e4aa3f0 | 42 | FMotor.period(0.010); // set PWM period to 10 ms |
ihmclachlan | 0:6ca20e4aa3f0 | 43 | FMotor = 0.5; // start the motors at 50% duty cycle |
ihmclachlan | 0:6ca20e4aa3f0 | 44 | |
ihmclachlan | 0:6ca20e4aa3f0 | 45 | int r = rand() % 50; // make r be a random number between 1 and 50 |
ihmclachlan | 0:6ca20e4aa3f0 | 46 | |
ihmclachlan | 0:6ca20e4aa3f0 | 47 | |
ihmclachlan | 0:6ca20e4aa3f0 | 48 | for(float p=0; p<1.0 && r != 35; p += 0.05) { // if r is 35 (could be any number) then the servo will stop turning otherwise it sweeps through its full range |
ihmclachlan | 0:6ca20e4aa3f0 | 49 | myservo1 = p; |
ihmclachlan | 0:6ca20e4aa3f0 | 50 | int r = rand() % 50; |
ihmclachlan | 0:6ca20e4aa3f0 | 51 | wait(0.2); } |
ihmclachlan | 0:6ca20e4aa3f0 | 52 | |
ihmclachlan | 0:6ca20e4aa3f0 | 53 | for(float p=1; p>0 && r != 35; p -= 0.05) { |
ihmclachlan | 0:6ca20e4aa3f0 | 54 | myservo1 = p; |
ihmclachlan | 0:6ca20e4aa3f0 | 55 | int r = rand() % 50; |
ihmclachlan | 0:6ca20e4aa3f0 | 56 | wait(0.2); |
ihmclachlan | 0:6ca20e4aa3f0 | 57 | } |
ihmclachlan | 0:6ca20e4aa3f0 | 58 | |
ihmclachlan | 1:3db7a3534296 | 59 | for(float q=0; q<0.5; q += 0.05) { // remove the lever holding the ball up |
ihmclachlan | 1:3db7a3534296 | 60 | myservo2 = q; |
ihmclachlan | 1:3db7a3534296 | 61 | wait(0.2); } |
ihmclachlan | 1:3db7a3534296 | 62 | |
ihmclachlan | 0:6ca20e4aa3f0 | 63 | wait (1); |
ihmclachlan | 1:3db7a3534296 | 64 | |
ihmclachlan | 1:3db7a3534296 | 65 | for(float q=0.5; q>0; q -= 0.05) { // put the leaver back into place |
ihmclachlan | 1:3db7a3534296 | 66 | myservo2 = q; |
ihmclachlan | 1:3db7a3534296 | 67 | wait(0.2); } |
ihmclachlan | 1:3db7a3534296 | 68 | |
ihmclachlan | 0:6ca20e4aa3f0 | 69 | wait (3); // give time for the ball to fire |
ihmclachlan | 0:6ca20e4aa3f0 | 70 | t.start(); // start the timer |
ihmclachlan | 0:6ca20e4aa3f0 | 71 | FMotor = 0; // turn off motors |
ihmclachlan | 0:6ca20e4aa3f0 | 72 | |
ihmclachlan | 0:6ca20e4aa3f0 | 73 | } |
ihmclachlan | 0:6ca20e4aa3f0 | 74 | do // create a new loop so that the program doesn't keep on repeating the first part |
ihmclachlan | 0:6ca20e4aa3f0 | 75 | { |
ihmclachlan | 0:6ca20e4aa3f0 | 76 | |
ihmclachlan | 0:6ca20e4aa3f0 | 77 | if (LDR) { // see if the ball is back in position in position |
ihmclachlan | 0:6ca20e4aa3f0 | 78 | t.stop(); // stop the timer to see how long it took the ball to get back |
ihmclachlan | 0:6ca20e4aa3f0 | 79 | } |
ihmclachlan | 0:6ca20e4aa3f0 | 80 | |
ihmclachlan | 0:6ca20e4aa3f0 | 81 | if (LDR && t.read() <5) { // if the ball in back in position and it took less than 5 seconds for the ball to get back |
ihmclachlan | 0:6ca20e4aa3f0 | 82 | FMotor.period(0.010); // set PWM period to 10 ms |
ihmclachlan | 0:6ca20e4aa3f0 | 83 | FMotor = 1; // start the motors at 100% duty cycle |
ihmclachlan | 0:6ca20e4aa3f0 | 84 | |
ihmclachlan | 0:6ca20e4aa3f0 | 85 | int r = rand() % 50; // make r be a random number between 1 and 50 |
ihmclachlan | 0:6ca20e4aa3f0 | 86 | |
ihmclachlan | 0:6ca20e4aa3f0 | 87 | |
ihmclachlan | 0:6ca20e4aa3f0 | 88 | for(float p=0; p<1.0 && r != 35; p += 0.05) { // if r is 35 (could be any number) then the servo will stop turning otherwise it sweeps through its full range |
ihmclachlan | 0:6ca20e4aa3f0 | 89 | myservo1 = p; |
ihmclachlan | 0:6ca20e4aa3f0 | 90 | int r = rand() % 50; |
ihmclachlan | 0:6ca20e4aa3f0 | 91 | wait(0.2); } |
ihmclachlan | 0:6ca20e4aa3f0 | 92 | |
ihmclachlan | 0:6ca20e4aa3f0 | 93 | for(float p=1; p>0 && r != 35; p -= 0.05) { |
ihmclachlan | 0:6ca20e4aa3f0 | 94 | myservo1 = p; |
ihmclachlan | 0:6ca20e4aa3f0 | 95 | int r = rand() % 50; |
ihmclachlan | 0:6ca20e4aa3f0 | 96 | wait(0.2); |
ihmclachlan | 0:6ca20e4aa3f0 | 97 | } |
ihmclachlan | 1:3db7a3534296 | 98 | for(float q=0; q<0.5; q += 0.05) { // remove the lever holding the ball up |
ihmclachlan | 1:3db7a3534296 | 99 | myservo2 = q; |
ihmclachlan | 1:3db7a3534296 | 100 | wait(0.2); } |
ihmclachlan | 1:3db7a3534296 | 101 | |
ihmclachlan | 0:6ca20e4aa3f0 | 102 | wait (1); |
ihmclachlan | 1:3db7a3534296 | 103 | |
ihmclachlan | 1:3db7a3534296 | 104 | for(float q=0.5; q>0; q -= 0.05) { // put the leaver back into place |
ihmclachlan | 1:3db7a3534296 | 105 | myservo2 = q; |
ihmclachlan | 1:3db7a3534296 | 106 | wait(0.2); } |
ihmclachlan | 1:3db7a3534296 | 107 | |
ihmclachlan | 0:6ca20e4aa3f0 | 108 | wait (3); // give time for the ball to fire |
ihmclachlan | 0:6ca20e4aa3f0 | 109 | t.start(); // start the timer |
ihmclachlan | 0:6ca20e4aa3f0 | 110 | FMotor = 0; // turn off motors |
ihmclachlan | 0:6ca20e4aa3f0 | 111 | |
ihmclachlan | 0:6ca20e4aa3f0 | 112 | |
ihmclachlan | 0:6ca20e4aa3f0 | 113 | if (LDR) { // see if the ball is back in position in position |
ihmclachlan | 0:6ca20e4aa3f0 | 114 | t.stop(); // stop the timer to see how long it took the ball to get back |
ihmclachlan | 0:6ca20e4aa3f0 | 115 | } |
ihmclachlan | 0:6ca20e4aa3f0 | 116 | } |
ihmclachlan | 0:6ca20e4aa3f0 | 117 | if (LDR && t.read() >5) { // if the ball in back in position and it took less than 5 seconds for the ball to get back |
ihmclachlan | 0:6ca20e4aa3f0 | 118 | FMotor.period(0.010); // set PWM period to 10 ms |
ihmclachlan | 0:6ca20e4aa3f0 | 119 | FMotor = 0.25; // start the motors at 25% duty cycle |
ihmclachlan | 0:6ca20e4aa3f0 | 120 | |
ihmclachlan | 0:6ca20e4aa3f0 | 121 | int r = rand() % 50; // make r be a random number between 1 and 50 |
ihmclachlan | 0:6ca20e4aa3f0 | 122 | |
ihmclachlan | 0:6ca20e4aa3f0 | 123 | |
ihmclachlan | 0:6ca20e4aa3f0 | 124 | for(float p=0; p<1.0 && r != 35; p += 0.05) { // if r is 35 (could be any number) then the servo will stop turning otherwise it sweeps through its full range |
ihmclachlan | 0:6ca20e4aa3f0 | 125 | myservo1 = p; |
ihmclachlan | 0:6ca20e4aa3f0 | 126 | int r = rand() % 50; |
ihmclachlan | 0:6ca20e4aa3f0 | 127 | wait(0.2); } |
ihmclachlan | 0:6ca20e4aa3f0 | 128 | |
ihmclachlan | 0:6ca20e4aa3f0 | 129 | for(float p=1; p>0 && r != 35; p -= 0.05) { |
ihmclachlan | 0:6ca20e4aa3f0 | 130 | myservo1 = p; |
ihmclachlan | 0:6ca20e4aa3f0 | 131 | int r = rand() % 50; |
ihmclachlan | 0:6ca20e4aa3f0 | 132 | wait(0.2); |
ihmclachlan | 0:6ca20e4aa3f0 | 133 | } |
ihmclachlan | 0:6ca20e4aa3f0 | 134 | |
ihmclachlan | 1:3db7a3534296 | 135 | for(float q=0; q<0.5; q += 0.05) { // remove the lever holding the ball up |
ihmclachlan | 1:3db7a3534296 | 136 | myservo2 = q; |
ihmclachlan | 1:3db7a3534296 | 137 | wait(0.2); } |
ihmclachlan | 1:3db7a3534296 | 138 | |
ihmclachlan | 0:6ca20e4aa3f0 | 139 | wait (1); |
ihmclachlan | 1:3db7a3534296 | 140 | |
ihmclachlan | 1:3db7a3534296 | 141 | for(float q=0.5; q>0; q -= 0.05) { // put the leaver back into place |
ihmclachlan | 1:3db7a3534296 | 142 | myservo2 = q; |
ihmclachlan | 1:3db7a3534296 | 143 | wait(0.2); } |
ihmclachlan | 1:3db7a3534296 | 144 | |
ihmclachlan | 0:6ca20e4aa3f0 | 145 | wait (3); // give time for the ball to fire |
ihmclachlan | 0:6ca20e4aa3f0 | 146 | t.start(); // start the timer |
ihmclachlan | 0:6ca20e4aa3f0 | 147 | FMotor = 0; // turn off motors |
ihmclachlan | 0:6ca20e4aa3f0 | 148 | } |
ihmclachlan | 0:6ca20e4aa3f0 | 149 | } |
ihmclachlan | 0:6ca20e4aa3f0 | 150 | while ( x == 0); |
ihmclachlan | 0:6ca20e4aa3f0 | 151 | } |
ihmclachlan | 0:6ca20e4aa3f0 | 152 | } |
ihmclachlan | 0:6ca20e4aa3f0 | 153 | |
ihmclachlan | 0:6ca20e4aa3f0 | 154 | } |