ese519

Dependencies:   Servo mbed

Committer:
mfrede
Date:
Wed Oct 07 01:08:21 2015 +0000
Revision:
1:c37400704a2f
Parent:
0:3009a71333b6
Child:
2:27a9092dcc6c
change dc enable to on/off, not pwm and correct open and closing doors.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ashrekhi 0:3009a71333b6 1 #include "mbed.h"
ashrekhi 0:3009a71333b6 2 #include "PwmOut.h"
ashrekhi 0:3009a71333b6 3
mfrede 1:c37400704a2f 4 unsigned int freqcnt=0;
mfrede 1:c37400704a2f 5 unsigned int floorat = 1;
mfrede 1:c37400704a2f 6 PwmOut servo1(p22);
mfrede 1:c37400704a2f 7 PwmOut servo2(p23);
mfrede 1:c37400704a2f 8 Serial pc(USBTX, USBRX); // tx, rx
mfrede 1:c37400704a2f 9 DigitalOut DCenable(p21);
ashrekhi 0:3009a71333b6 10
ashrekhi 0:3009a71333b6 11 void freq_counter() {
ashrekhi 0:3009a71333b6 12 freqcnt++;
mfrede 1:c37400704a2f 13 DCenable = 0;
mfrede 1:c37400704a2f 14 pc.printf("%d cnt\r\n",freqcnt);
ashrekhi 0:3009a71333b6 15 }
ashrekhi 0:3009a71333b6 16
mfrede 1:c37400704a2f 17 void timerfunc() { // what it does
mfrede 1:c37400704a2f 18
mfrede 1:c37400704a2f 19 floorat = 0;
ashrekhi 0:3009a71333b6 20 if (freqcnt != 0) {
mfrede 1:c37400704a2f 21 if (freqcnt <= 1 && freqcnt>0)
ashrekhi 0:3009a71333b6 22 floorat = 1;
ashrekhi 0:3009a71333b6 23 else if (freqcnt <= 3)
ashrekhi 0:3009a71333b6 24 floorat = 2;
ashrekhi 0:3009a71333b6 25 else if (freqcnt <= 5)
ashrekhi 0:3009a71333b6 26 floorat = 3;
ashrekhi 0:3009a71333b6 27 else if (freqcnt <= 7)
ashrekhi 0:3009a71333b6 28 floorat = 4;
ashrekhi 0:3009a71333b6 29 else if (freqcnt <= 10)
ashrekhi 0:3009a71333b6 30 floorat = 5;
ashrekhi 0:3009a71333b6 31 }
ashrekhi 0:3009a71333b6 32 freqcnt = 0;
ashrekhi 0:3009a71333b6 33 }
ashrekhi 0:3009a71333b6 34
mfrede 1:c37400704a2f 35 void open_door()
mfrede 1:c37400704a2f 36 {
mfrede 1:c37400704a2f 37 servo1 = 0.03f; // right
mfrede 1:c37400704a2f 38 servo2 = 0.10f; // left
mfrede 1:c37400704a2f 39 wait(1);
mfrede 1:c37400704a2f 40 }
mfrede 1:c37400704a2f 41
mfrede 1:c37400704a2f 42 void close_door()
mfrede 1:c37400704a2f 43 {
mfrede 1:c37400704a2f 44 servo1 = 0.08f; //right
mfrede 1:c37400704a2f 45 servo2 = 0.05f; //left
mfrede 1:c37400704a2f 46 wait(1);
mfrede 1:c37400704a2f 47 }
mfrede 1:c37400704a2f 48
ashrekhi 0:3009a71333b6 49
ashrekhi 0:3009a71333b6 50 int main() {
mfrede 1:c37400704a2f 51
mfrede 1:c37400704a2f 52
ashrekhi 0:3009a71333b6 53 InterruptIn floor(p17);
ashrekhi 0:3009a71333b6 54 AnalogIn request(p18);
ashrekhi 0:3009a71333b6 55 servo1.period(0.020f); //set 20ms period
ashrekhi 0:3009a71333b6 56 servo2.period(0.020f); //set 20ms period
mfrede 1:c37400704a2f 57 DigitalOut motor1(p19);
mfrede 1:c37400704a2f 58 DigitalOut motor2(p20);
ashrekhi 0:3009a71333b6 59 AnalogIn floor2(p17);
ashrekhi 0:3009a71333b6 60
mfrede 1:c37400704a2f 61
mfrede 1:c37400704a2f 62
mfrede 1:c37400704a2f 63 Ticker check_floor; //?
ashrekhi 0:3009a71333b6 64 check_floor.attach(&timerfunc,0.01f);
mfrede 1:c37400704a2f 65 floor.fall(&freq_counter);
ashrekhi 0:3009a71333b6 66
mfrede 1:c37400704a2f 67 close_door();
mfrede 1:c37400704a2f 68
mfrede 1:c37400704a2f 69
ashrekhi 0:3009a71333b6 70 while(1){
mfrede 1:c37400704a2f 71 pc.printf("%d floor\n\r",floorat);
mfrede 1:c37400704a2f 72 pc.printf("%f requested\n\r",request.read());
mfrede 1:c37400704a2f 73 if(DCenable == 0 && floorat == 0) { //if we are not starting on a known floor
mfrede 1:c37400704a2f 74 motor1 = 1; //just go up until you get to a floor
mfrede 1:c37400704a2f 75 motor2 = 0;
mfrede 1:c37400704a2f 76 DCenable = 1;
mfrede 1:c37400704a2f 77 pc.printf("go up to start \r\n");
mfrede 1:c37400704a2f 78 }
mfrede 1:c37400704a2f 79 if(request <= 0.2f) { //first floor
mfrede 1:c37400704a2f 80 pc.printf("go to the first floor\r\n");
ashrekhi 0:3009a71333b6 81 if(floorat > 1) { //if we're above the first floor
ashrekhi 0:3009a71333b6 82 //set DC motor to go down
ashrekhi 0:3009a71333b6 83 motor1 = 1;
ashrekhi 0:3009a71333b6 84 motor2 = 0;
mfrede 1:c37400704a2f 85 DCenable = 1;
mfrede 1:c37400704a2f 86 } else if (floorat == 1) {
mfrede 1:c37400704a2f 87 DCenable = 0;
mfrede 1:c37400704a2f 88 open_door();
mfrede 1:c37400704a2f 89 wait(10);
mfrede 1:c37400704a2f 90 close_door();
ashrekhi 0:3009a71333b6 91 }
mfrede 1:c37400704a2f 92 } else if (request <= 0.4f) { // second floor
mfrede 1:c37400704a2f 93 pc.printf("go to the second floor\r\n");
ashrekhi 0:3009a71333b6 94 if (floorat > 2) {
ashrekhi 0:3009a71333b6 95 //set DC motor to go down
ashrekhi 0:3009a71333b6 96 motor1 = 1;
ashrekhi 0:3009a71333b6 97 motor2 = 0;
mfrede 1:c37400704a2f 98 DCenable = 1;
ashrekhi 0:3009a71333b6 99 } else if (floorat < 2) {
ashrekhi 0:3009a71333b6 100 //set DC motor to go up
ashrekhi 0:3009a71333b6 101 motor1 = 0;
ashrekhi 0:3009a71333b6 102 motor2 = 1;
mfrede 1:c37400704a2f 103 DCenable = 1;
mfrede 1:c37400704a2f 104 } else {
mfrede 1:c37400704a2f 105 DCenable = 0;
mfrede 1:c37400704a2f 106 open_door();
mfrede 1:c37400704a2f 107 wait(10);
mfrede 1:c37400704a2f 108 close_door();
ashrekhi 0:3009a71333b6 109 }
mfrede 1:c37400704a2f 110 } else if (request*5 <= 0.6f) { // third floor
mfrede 1:c37400704a2f 111 pc.printf("go to the third floor\r\n");
ashrekhi 0:3009a71333b6 112 if (floorat > 3) {
ashrekhi 0:3009a71333b6 113 //set DC motor to go down
ashrekhi 0:3009a71333b6 114 motor1 = 1;
ashrekhi 0:3009a71333b6 115 motor2 = 0;
mfrede 1:c37400704a2f 116 DCenable = 1;
ashrekhi 0:3009a71333b6 117 } else if (floorat < 3) {
ashrekhi 0:3009a71333b6 118 //set DC motor to go up
ashrekhi 0:3009a71333b6 119 motor1 = 0;
ashrekhi 0:3009a71333b6 120 motor2 = 1;
mfrede 1:c37400704a2f 121 DCenable = 1;
mfrede 1:c37400704a2f 122 } else {
mfrede 1:c37400704a2f 123 DCenable = 0;
mfrede 1:c37400704a2f 124 open_door();
mfrede 1:c37400704a2f 125 wait(10);
mfrede 1:c37400704a2f 126 close_door();
ashrekhi 0:3009a71333b6 127 }
mfrede 1:c37400704a2f 128 } else if (request <= 0.8f) { // fourth floor
mfrede 1:c37400704a2f 129 pc.printf("go to the fourth floor\r\n");
ashrekhi 0:3009a71333b6 130 if (floorat > 4) {
ashrekhi 0:3009a71333b6 131 //set DC motor to go down
ashrekhi 0:3009a71333b6 132 motor1 = 1;
ashrekhi 0:3009a71333b6 133 motor2 = 0;
mfrede 1:c37400704a2f 134 DCenable = 1;
ashrekhi 0:3009a71333b6 135 } else if (floorat < 4) {
ashrekhi 0:3009a71333b6 136 //set DC motor to go up
ashrekhi 0:3009a71333b6 137 motor1 = 0;
ashrekhi 0:3009a71333b6 138 motor2 = 1;
mfrede 1:c37400704a2f 139 DCenable = 1;
mfrede 1:c37400704a2f 140 } else {
mfrede 1:c37400704a2f 141 DCenable = 0;
mfrede 1:c37400704a2f 142 open_door();
mfrede 1:c37400704a2f 143 wait(10);
mfrede 1:c37400704a2f 144 close_door();
ashrekhi 0:3009a71333b6 145 }
ashrekhi 0:3009a71333b6 146 } else { //fifth floor
mfrede 1:c37400704a2f 147 pc.printf("go to the fifth floor\r\n");
ashrekhi 0:3009a71333b6 148 if (floorat < 5) {
ashrekhi 0:3009a71333b6 149 //set DC motor to go up
ashrekhi 0:3009a71333b6 150 motor1 = 0;
ashrekhi 0:3009a71333b6 151 motor2 = 1;
mfrede 1:c37400704a2f 152 DCenable = 1;
mfrede 1:c37400704a2f 153 } else {
mfrede 1:c37400704a2f 154 DCenable = 0;
mfrede 1:c37400704a2f 155 open_door();
mfrede 1:c37400704a2f 156 wait(10);
mfrede 1:c37400704a2f 157 close_door();
ashrekhi 0:3009a71333b6 158 }
ashrekhi 0:3009a71333b6 159 } //end floor checks
mfrede 1:c37400704a2f 160 wait(10);
mfrede 1:c37400704a2f 161 }
mfrede 1:c37400704a2f 162 //while (floorat == 0) { //wait endlessly until we're on a floor
mfrede 1:c37400704a2f 163 // if(DCenable == 0 || freqcnt != 0)
mfrede 1:c37400704a2f 164 // break;
mfrede 1:c37400704a2f 165 // wait(1);
mfrede 1:c37400704a2f 166 // }
mfrede 1:c37400704a2f 167 while(1) {
ashrekhi 0:3009a71333b6 168
ashrekhi 0:3009a71333b6 169 }
ashrekhi 0:3009a71333b6 170
ashrekhi 0:3009a71333b6 171
ashrekhi 0:3009a71333b6 172 return 0;
ashrekhi 0:3009a71333b6 173 }