Lab02

Dependencies:   Servo mbed

Committer:
dannellyz
Date:
Fri Jan 17 02:39:11 2014 +0000
Revision:
1:1a7f5ec34d63
Parent:
0:f5f8ea653b56
added printf;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
dannellyz 0:f5f8ea653b56 1 //SCY202
dannellyz 0:f5f8ea653b56 2 //Zachary Dannelly
dannellyz 0:f5f8ea653b56 3 //Partner: Evan Philips
dannellyz 0:f5f8ea653b56 4 //Base code provided by CAPT Shorr
dannellyz 0:f5f8ea653b56 5 //Date Created: 16JAN14
dannellyz 0:f5f8ea653b56 6 //LAB02
dannellyz 0:f5f8ea653b56 7 /*************************Includes****************************/
dannellyz 0:f5f8ea653b56 8 #include "mbed.h"
dannellyz 0:f5f8ea653b56 9 #include "Servo.h"
dannellyz 0:f5f8ea653b56 10
dannellyz 0:f5f8ea653b56 11 /************************Pre-Processes******************************************/
dannellyz 0:f5f8ea653b56 12 Serial pc(USBTX,USBRX);
dannellyz 0:f5f8ea653b56 13 Servo myservo(p22);
dannellyz 0:f5f8ea653b56 14 PwmOut DCmotor(p21);
dannellyz 0:f5f8ea653b56 15 double turn = 0.0000001; //Unnoticable close to zero as to not skip the later while loop while not begging the rotation
dannellyz 0:f5f8ea653b56 16 double speed = 0.0;
dannellyz 0:f5f8ea653b56 17 int direction = 0;
dannellyz 0:f5f8ea653b56 18 double sign = -1.0;
dannellyz 0:f5f8ea653b56 19 /*******************************************************************************/
dannellyz 0:f5f8ea653b56 20 int main(){
dannellyz 1:1a7f5ec34d63 21 pc.printf("%s %s \n\r", "Servo","DC Motor");
dannellyz 0:f5f8ea653b56 22 while(1){
dannellyz 0:f5f8ea653b56 23 myservo.calibrate(.0009,90); //Code provideed by CAPT Shorr to calibrate Servo
dannellyz 0:f5f8ea653b56 24 sign = sign * -1.0; //Sign tells servo whether to increment or decrement based on which phase of the cycle it is in
dannellyz 0:f5f8ea653b56 25 while(turn > 0 && turn < 1){ //While the servo is completing one full rotation continue to turn...
dannellyz 0:f5f8ea653b56 26 myservo = turn;
dannellyz 0:f5f8ea653b56 27 turn += .005 * sign; //...based upon wether the servo is in the forward or backwrd phase of its ocilation
dannellyz 1:1a7f5ec34d63 28 pc.printf("%.2f %.2f\r", turn, speed);
dannellyz 0:f5f8ea653b56 29 wait(.0125);
dannellyz 1:1a7f5ec34d63 30
dannellyz 0:f5f8ea653b56 31 }
dannellyz 0:f5f8ea653b56 32 if(sign > 0){turn = .9999999;} //Same concept as on the comment when variable was initialized
dannellyz 0:f5f8ea653b56 33 else{turn = .000001;}
dannellyz 0:f5f8ea653b56 34
dannellyz 0:f5f8ea653b56 35 if (speed == 1.0){direction = 1;} //Once the speed of the DC Motor has reached 100% the direction is set to 1
dannellyz 0:f5f8ea653b56 36 if (speed < 0.1){direction = 0;} //Once the spped of the DC Motor has returned to 0 the direction is reset to 0**
dannellyz 0:f5f8ea653b56 37 //**Code had to be modifed to <0.1 as when at == 0.0 it would not reset
dannellyz 0:f5f8ea653b56 38
dannellyz 0:f5f8ea653b56 39 if(speed < 1 && sign < 0 && direction == 0){ // While the speed of the motor is less than 1 and the servo is back at its starting spot
dannellyz 0:f5f8ea653b56 40 speed += .2; // (as indicated by a -1 on the sign) and the motor is accelerating, increse the speed by 20%
dannellyz 0:f5f8ea653b56 41 DCmotor = speed;
dannellyz 0:f5f8ea653b56 42 }
dannellyz 0:f5f8ea653b56 43 else if(speed > 0 && sign < 0 && direction == 1){ // While the speed of the motor is greater than 0 and the servo is back at its starting spot
dannellyz 0:f5f8ea653b56 44 speed -= .2; // (as indicated by a -1 on the sign) and the motor is decellerating, decrease the speed by 20%
dannellyz 0:f5f8ea653b56 45 DCmotor = speed;
dannellyz 0:f5f8ea653b56 46 }
dannellyz 1:1a7f5ec34d63 47
dannellyz 0:f5f8ea653b56 48
dannellyz 1:1a7f5ec34d63 49 //pc.printf(" %.2f %d \r",speed);
dannellyz 0:f5f8ea653b56 50
dannellyz 0:f5f8ea653b56 51 }
dannellyz 0:f5f8ea653b56 52 }