Fa2018-es200-1121-project2-herndon-toss / Mbed 2 deprecated Project2

Dependencies:   Motor Servo mbed mbed-rtos

Committer:
m215676
Date:
Tue Oct 16 13:38:55 2018 +0000
Revision:
6:3dc339508843
Parent:
5:27689dbb0c6e
Child:
7:adcdd7556997
Added comments in class 16OCT

Who changed what in which revision?

UserRevisionLine numberNew contents of line
m215676 0:9347308e8e1a 1 #include "mbed.h"
m215676 0:9347308e8e1a 2 #include "stdio.h"
m215676 0:9347308e8e1a 3 #include "Motor.h"
m215676 0:9347308e8e1a 4 #include "math.h"
m215676 2:60a8105d0d20 5 #include "Servo.h"
m215676 6:3dc339508843 6 #include "rtos.h" //need to add this library for multiple functions running at once
m215676 6:3dc339508843 7
m215676 6:3dc339508843 8
m215676 0:9347308e8e1a 9
m215676 1:7133819e4265 10 int s1, s3, s4, i;
m215676 2:60a8105d0d20 11 float x;
m215676 5:27689dbb0c6e 12 float y;
m215676 6:3dc339508843 13 AnalogIn turn(p15); //turn dial to adjust servo for horizontal angle of arm SWITCH 2
m215676 6:3dc339508843 14 DigitalIn swtch1(p19); //moves herndon monument on turn table continuously while switch is on
m215676 6:3dc339508843 15 DigitalIn swtch3(p20); //releases the cover when switch 3 is turned on
m215676 6:3dc339508843 16 DigitalIn swtch4(p21); //retracts arm back to starting position to try again
m215676 6:3dc339508843 17 Motor m(p26, p30, p29); //launches the cover
m215676 6:3dc339508843 18 //DigitalIn sw1 (p16) ; not sure why this is here - maybe broken pin
m215676 1:7133819e4265 19
m215676 5:27689dbb0c6e 20 Servo back(p21); //servo to move monument on turn table
m215676 5:27689dbb0c6e 21 Servo angle(p22); //servo to adjust horizontal angle of arm
m215676 0:9347308e8e1a 22
m215676 0:9347308e8e1a 23 int main() {
m215676 1:7133819e4265 24 s1 = swtch1.read(); //read state of switch 1
m215676 5:27689dbb0c6e 25 angle.calibrate(0.0009, 90.0);
m215676 1:7133819e4265 26 back.calibrate(0.0009, 90.0); //calculate's servos timing by setting pulse width and range
m215676 0:9347308e8e1a 27
m215676 6:3dc339508843 28 while (s1 == 1) //moves herndon monument continuously on turn table
m215676 1:7133819e4265 29 {
m215676 5:27689dbb0c6e 30 for (i = 0; i < 18; i++) //moves monument 180 degrees one way
m215676 1:7133819e4265 31 {
m215676 1:7133819e4265 32 back = 0;
m215676 1:7133819e4265 33 back = (0.0 + (i/18.0));
m215676 1:7133819e4265 34 s1 = swtch1.read(); //read state of switch 1
m215676 1:7133819e4265 35 }//end of for loop
m215676 1:7133819e4265 36
m215676 6:3dc339508843 37 i=0;
m215676 6:3dc339508843 38
m215676 6:3dc339508843 39 for (i = 0; i < 18; i++) //moves monument back and another 180 degress the opposite way
m215676 1:7133819e4265 40 {
m215676 1:7133819e4265 41 back = 1;
m215676 1:7133819e4265 42 back = (1.0 - (i/18.0));
m215676 1:7133819e4265 43 s1 = swtch1.read(); //read state of switch 1
m215676 1:7133819e4265 44 }//end of for loop
m215676 1:7133819e4265 45
m215676 6:3dc339508843 46 i = 0;
m215676 6:3dc339508843 47
m215676 1:7133819e4265 48 }//end of while loop
m215676 1:7133819e4265 49
m215676 2:60a8105d0d20 50
m215676 5:27689dbb0c6e 51
m215676 5:27689dbb0c6e 52 //Adjust horiztonal angle with switch 2/dial
m215676 5:27689dbb0c6e 53 while(1) {
m215676 5:27689dbb0c6e 54 y = turn.read(); //reads the state of the dial the user changes
m215676 5:27689dbb0c6e 55 printf("The dial is at %f\n", x);
m215676 5:27689dbb0c6e 56 angle.write(y); //makes the servo turn according to the dial
m215676 5:27689dbb0c6e 57 printf("The servo is at %f\n", x);
m215676 5:27689dbb0c6e 58
m215676 5:27689dbb0c6e 59 } //end of while loop
m215676 5:27689dbb0c6e 60
m215676 5:27689dbb0c6e 61
nhersom 4:a3e95c9d56d5 62
m215676 5:27689dbb0c6e 63 sw3.read(); //read state of switch 3 (launch the cover)
m215676 5:27689dbb0c6e 64 if (sw3==1) {
nhersom 3:50cee0fef078 65 while(1) {
m215676 6:3dc339508843 66 printf("motor on");
m215676 6:3dc339508843 67 m.speed(1.0);
m215676 6:3dc339508843 68 wait (.4);
m215676 6:3dc339508843 69 printf("motor off");
m215676 6:3dc339508843 70 m.speed(0.0);
nhersom 3:50cee0fef078 71 wait (1);
nhersom 3:50cee0fef078 72 break;
m215676 5:27689dbb0c6e 73 } //end of while
m215676 5:27689dbb0c6e 74 } //end of if
m215676 5:27689dbb0c6e 75
m215676 5:27689dbb0c6e 76
m215676 5:27689dbb0c6e 77
m215676 5:27689dbb0c6e 78
m215676 5:27689dbb0c6e 79 sw4.read(); //read state of switch 4 (retract arm to try again)
m215676 5:27689dbb0c6e 80 if (s4 == 1){
nhersom 3:50cee0fef078 81 while(1) {
m215676 6:3dc339508843 82 printf("reversed");
nhersom 3:50cee0fef078 83 m.speed(-.25);
nhersom 3:50cee0fef078 84 wait(.6);
m215676 6:3dc339508843 85 printf("motor off");
nhersom 3:50cee0fef078 86 m.speed(0);
nhersom 3:50cee0fef078 87 wait(1);
nhersom 3:50cee0fef078 88 break;
m215676 5:27689dbb0c6e 89 } //end of while
m215676 5:27689dbb0c6e 90 } //end of if
m215676 6:3dc339508843 91