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

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers lab3integrated.cpp Source File

lab3integrated.cpp

00001 //Integrated Servo and Motor
00002 // MIDN 3/C Stites and Maly 
00003 // Modified 10/2/18
00004 
00005 #include "mbed.h"
00006 #include "Servo.h"
00007 #include "Motor.h" //Preprocessors to include Servo and Motor
00008 
00009 Servo kenny(p21); // Assigns pins to control two servos (kenny and barb)
00010 Servo barb(p22);
00011 Motor m(p26, p29, p30); // Assigns the PwmOut (pwm) pin, DigitalOut (fwd) pin, and Digital Out (rev) pin
00012 
00013 int main() 
00014 {
00015     barb.calibrate(0.0009,90); // Calibrates the servo motors
00016     kenny.calibrate(0.0009,90);
00017     kenny = 0; // Sets servo kenny at the begining of his 180 degree rotation
00018     barb = 1; // Sets servo barb at the end of her 180 degree rotation
00019     
00020     float k = 0.2; // value of initial speed & variable to define speed 
00021     float i, j; // used in 'for' loops
00022     
00023     while(1) // Continue to repeat
00024     {
00025         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
00026             {
00027             m.speed(k); // Sets the speed of the DC Motor to whatever value k is as it increases 
00028             for (i=0; i<90; i++) // This 'for' loop turns the two Servos in opposite directions. 
00029                 { // It is set after the speed of the Motor is set so they move along with the DC Motor 
00030                  barb = 1 - i*0.0111; // Moves barb backwards 2 degrees
00031                  kenny = i*0.0111; // Moves kenny forwards 2 degrees
00032                   wait(.0555); //Wait .0555 seconds before moving the servos again (when this loop is repeated 90 times it takes 5 seconds)
00033                 }
00034             m.speed(-k); //Sets the speed of the DC motor in the opposite direction at k speed. 
00035             for (j=0; j<90; j++)// This 'for' loop turns the two Servos in opposite directions from each other and from the step before. 
00036                  {// It is set after the speed/direction change of the DC Motor so the Servos change direction the same time the DC motors do. 
00037                  kenny = 1 - j*0.0111; //Now that the starting positions of kenny and barb are reversed this reverses the above process.
00038                  barb = j*0.0111;
00039                  wait(.0555);
00040                  }
00041             }
00042         
00043         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).  
00044          for (i=0; i<90; i++)// Again, a 'for' loop to turn the two Servo motors in opposite directions while the DC motor turns. 
00045                 {
00046                  barb = 1 - i*0.0111; //Repeats above process indefinetly
00047                  kenny = i*0.0111;
00048                   wait(.0555);
00049                 }
00050         m.speed(-1); // Turns the DC motor at full speed in the opposite direction.
00051         for (j=0; j<90; j++)// Again, a 'for' loop to turn the two Servo motors in opposite directions while the DC motor turns. 
00052                  {
00053                  kenny = 1 - j*0.0111; //Reverses above process indefinetly
00054                  barb = j*0.0111;
00055                  wait(.0555);
00056                  }
00057         
00058         
00059         }            
00060 }