Integrated Lab
Dependencies: Motor Servo mbed
lab3integrated.cpp@1:efdd0b0bbdbc, 2018-10-03 (annotated)
- Committer:
- tmaly45
- Date:
- Wed Oct 03 00:57:21 2018 +0000
- Revision:
- 1:efdd0b0bbdbc
- Parent:
- 0:f5f82650e679
should be updated, still need some comments about the SERVO
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tmaly45 | 1:efdd0b0bbdbc | 1 | //Integrated Servo and Motor |
tmaly45 | 1:efdd0b0bbdbc | 2 | // MIDN 3/C Stites and Maly |
tmaly45 | 1:efdd0b0bbdbc | 3 | // Modified 10/2/18 |
tmaly45 | 1:efdd0b0bbdbc | 4 | |
corwinstites | 0:f5f82650e679 | 5 | #include "mbed.h" |
corwinstites | 0:f5f82650e679 | 6 | #include "Servo.h" |
tmaly45 | 1:efdd0b0bbdbc | 7 | #include "Motor.h" //preprocessors to include Servo and Motor |
corwinstites | 0:f5f82650e679 | 8 | |
tmaly45 | 1:efdd0b0bbdbc | 9 | Servo kenny(p21); |
corwinstites | 0:f5f82650e679 | 10 | Servo barb(p22); |
tmaly45 | 1:efdd0b0bbdbc | 11 | Motor m(p26, p29, p30); // Assigns the PwmOut (pwm) pin, DigitalOut (fwd) pin, and Digital Out (rev) pin |
corwinstites | 0:f5f82650e679 | 12 | |
corwinstites | 0:f5f82650e679 | 13 | int main() |
corwinstites | 0:f5f82650e679 | 14 | { |
corwinstites | 0:f5f82650e679 | 15 | barb.calibrate(0.0009,90); |
corwinstites | 0:f5f82650e679 | 16 | kenny.calibrate(0.0009,90); |
corwinstites | 0:f5f82650e679 | 17 | kenny = 0; |
corwinstites | 0:f5f82650e679 | 18 | barb = 1; |
tmaly45 | 1:efdd0b0bbdbc | 19 | |
tmaly45 | 1:efdd0b0bbdbc | 20 | float k = 0.2; // value of initial speed & variable to define speed |
tmaly45 | 1:efdd0b0bbdbc | 21 | float i, j; // used in 'for' loops |
tmaly45 | 1:efdd0b0bbdbc | 22 | |
corwinstites | 0:f5f82650e679 | 23 | while(1) |
corwinstites | 0:f5f82650e679 | 24 | { |
tmaly45 | 1:efdd0b0bbdbc | 25 | for(k=0.2; k<1.0; k += 0.1) //Increases the value of k from 0.2 to 0.99. k is the speed of DC Motor |
tmaly45 | 1:efdd0b0bbdbc | 26 | { |
tmaly45 | 1:efdd0b0bbdbc | 27 | m.speed(k); // Sets the speed of the DC Motor to whatever value k is as it increases |
tmaly45 | 1:efdd0b0bbdbc | 28 | for (i=0; i<90; i++) // This 'for' loop turns the two Servos in opposite directions. |
tmaly45 | 1:efdd0b0bbdbc | 29 | { // It is set after the speed of the Motor is set so they move along with the DC Motor |
tmaly45 | 1:efdd0b0bbdbc | 30 | barb = 1 - i*0.0111; |
tmaly45 | 1:efdd0b0bbdbc | 31 | kenny = i*0.0111; |
tmaly45 | 1:efdd0b0bbdbc | 32 | wait(.0555); |
tmaly45 | 1:efdd0b0bbdbc | 33 | } |
tmaly45 | 1:efdd0b0bbdbc | 34 | m.speed(-k); //Sets the speed of the DC motor in the opposite direction at k speed. |
tmaly45 | 1:efdd0b0bbdbc | 35 | for (j=0; j<90; j++)// This 'for' loop turns the two Servos in opposite directions from each other and from the step before. |
tmaly45 | 1:efdd0b0bbdbc | 36 | {// It is set after the speed/direction change of the DC Motor so the Servos change direction the same time the DC motors do. |
tmaly45 | 1:efdd0b0bbdbc | 37 | kenny = 1 - j*0.0111; |
tmaly45 | 1:efdd0b0bbdbc | 38 | barb = j*0.0111; |
tmaly45 | 1:efdd0b0bbdbc | 39 | wait(.0555); |
tmaly45 | 1:efdd0b0bbdbc | 40 | } |
tmaly45 | 1:efdd0b0bbdbc | 41 | } |
tmaly45 | 1:efdd0b0bbdbc | 42 | |
tmaly45 | 1:efdd0b0bbdbc | 43 | m.speed(1); // This is for when the DC motor reaches full speed, which is 1. This will start at the end of the 'for' loop of the DC motor speed (k). |
tmaly45 | 1:efdd0b0bbdbc | 44 | for (i=0; i<90; i++)// Again, a 'for' loop to turn the two Servo motors in opposite directions while the DC motor turns. |
tmaly45 | 1:efdd0b0bbdbc | 45 | { |
tmaly45 | 1:efdd0b0bbdbc | 46 | barb = 1 - i*0.0111; |
tmaly45 | 1:efdd0b0bbdbc | 47 | kenny = i*0.0111; |
tmaly45 | 1:efdd0b0bbdbc | 48 | wait(.0555); |
tmaly45 | 1:efdd0b0bbdbc | 49 | } |
tmaly45 | 1:efdd0b0bbdbc | 50 | m.speed(-1); // Turns the DC motor at full speed in the opposite direction. |
tmaly45 | 1:efdd0b0bbdbc | 51 | for (j=0; j<90; j++)// Again, a 'for' loop to turn the two Servo motors in opposite directions while the DC motor turns. |
tmaly45 | 1:efdd0b0bbdbc | 52 | { |
tmaly45 | 1:efdd0b0bbdbc | 53 | kenny = 1 - j*0.0111; |
tmaly45 | 1:efdd0b0bbdbc | 54 | barb = j*0.0111; |
tmaly45 | 1:efdd0b0bbdbc | 55 | wait(.0555); |
tmaly45 | 1:efdd0b0bbdbc | 56 | } |
tmaly45 | 1:efdd0b0bbdbc | 57 | |
tmaly45 | 1:efdd0b0bbdbc | 58 | |
tmaly45 | 1:efdd0b0bbdbc | 59 | } |
corwinstites | 0:f5f82650e679 | 60 | } |