Includes both the servo and Dc motor. Needs more comments on the servo side.

Dependencies:   Motor Servo mbed

Fork of lab3integrated by Actuator Control Lab

Committer:
corwinstites
Date:
Wed Oct 03 01:46:15 2018 +0000
Revision:
2:df17a1850217
Parent:
1:efdd0b0bbdbc
Updated

Who changed what in which revision?

UserRevisionLine numberNew 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"
corwinstites 2:df17a1850217 7 #include "Motor.h" //Preprocessors to include Servo and Motor
corwinstites 0:f5f82650e679 8
corwinstites 2:df17a1850217 9 Servo kenny(p21); // Assigns pins to control two servos (kenny and barb)
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 2:df17a1850217 15 barb.calibrate(0.0009,90); // Calibrates the servo motors
corwinstites 0:f5f82650e679 16 kenny.calibrate(0.0009,90);
corwinstites 2:df17a1850217 17 kenny = 0; // Sets servo kenny at the begining of his 180 degree rotation
corwinstites 2:df17a1850217 18 barb = 1; // Sets servo barb at the end of her 180 degree rotation
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 2:df17a1850217 23 while(1) // Continue to repeat
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
corwinstites 2:df17a1850217 30 barb = 1 - i*0.0111; // Moves barb backwards 2 degrees
corwinstites 2:df17a1850217 31 kenny = i*0.0111; // Moves kenny forwards 2 degrees
corwinstites 2:df17a1850217 32 wait(.0555); //Wait .0555 seconds before moving the servos again (when this loop is repeated 90 times it takes 5 seconds)
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.
corwinstites 2:df17a1850217 37 kenny = 1 - j*0.0111; //Now that the starting positions of kenny and barb are reversed this reverses the above process.
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 {
corwinstites 2:df17a1850217 46 barb = 1 - i*0.0111; //Repeats above process indefinetly
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 {
corwinstites 2:df17a1850217 53 kenny = 1 - j*0.0111; //Reverses above process indefinetly
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 }