Mohammed Alshiekh
/
elevator_with_queue
course work
elevator.cpp@7:9ff7df122ff6, 2015-10-13 (annotated)
- Committer:
- sahabi
- Date:
- Tue Oct 13 20:41:36 2015 +0000
- Revision:
- 7:9ff7df122ff6
- Parent:
- 6:1f9baf8d833e
- Child:
- 8:dd34a528a47c
final
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sahabi | 0:69be3faae0a0 | 1 | #include "mbed.h" |
sahabi | 0:69be3faae0a0 | 2 | |
sahabi | 3:19106c977df1 | 3 | Serial pc(USBTX,USBRX); |
sahabi | 3:19106c977df1 | 4 | PwmOut dc_motor(p23); |
sahabi | 0:69be3faae0a0 | 5 | PwmOut servo1(p21); |
sahabi | 0:69be3faae0a0 | 6 | PwmOut servo2(p22); |
sahabi | 0:69be3faae0a0 | 7 | DigitalInOut in1(p5); |
sahabi | 0:69be3faae0a0 | 8 | DigitalInOut in2(p6); |
sahabi | 4:fba6c97a6192 | 9 | AnalogIn an(p16); |
sahabi | 3:19106c977df1 | 10 | InterruptIn firstfloor(p15); |
sahabi | 0:69be3faae0a0 | 11 | InterruptIn secondfloor(p8); |
sahabi | 0:69be3faae0a0 | 12 | InterruptIn thirdfloor(p9); |
sahabi | 0:69be3faae0a0 | 13 | InterruptIn fourthfloor(p10); |
sahabi | 0:69be3faae0a0 | 14 | InterruptIn fifthfloor(p11); |
sahabi | 1:28120714ad80 | 15 | InterruptIn sensor(p12); |
sahabi | 1:28120714ad80 | 16 | DigitalOut led4(LED4); |
sahabi | 1:28120714ad80 | 17 | DigitalOut led3(LED3); |
sahabi | 1:28120714ad80 | 18 | DigitalOut led2(LED2); |
sahabi | 1:28120714ad80 | 19 | DigitalOut led1(LED1); |
sahabi | 2:07f2fe858dbd | 20 | int first = 0; |
sahabi | 0:69be3faae0a0 | 21 | Timer time1; |
sahabi | 2:07f2fe858dbd | 22 | int once = 0; |
sahabi | 0:69be3faae0a0 | 23 | float stationary = 0.075; |
sahabi | 7:9ff7df122ff6 | 24 | float open = 0.085; // counter clock-wise |
sahabi | 7:9ff7df122ff6 | 25 | float closed = 0.040; // clock-wise |
sahabi | 7:9ff7df122ff6 | 26 | float open2 = 0.100; |
sahabi | 7:9ff7df122ff6 | 27 | float closed2 = 0.055; |
sahabi | 1:28120714ad80 | 28 | int reached = 0; |
sahabi | 3:19106c977df1 | 29 | int location; |
sahabi | 2:07f2fe858dbd | 30 | float frequency; |
sahabi | 4:fba6c97a6192 | 31 | int requested_floor1, requested_floor2, requested_floor; |
sahabi | 0:69be3faae0a0 | 32 | int busy = 0; |
sahabi | 2:07f2fe858dbd | 33 | int currentfloor; |
sahabi | 1:28120714ad80 | 34 | |
sahabi | 3:19106c977df1 | 35 | void serve(){ |
sahabi | 2:07f2fe858dbd | 36 | |
sahabi | 3:19106c977df1 | 37 | if(busy == 1){ |
sahabi | 3:19106c977df1 | 38 | if (first == 0){ |
sahabi | 3:19106c977df1 | 39 | time1.reset(); |
sahabi | 3:19106c977df1 | 40 | time1.start(); |
sahabi | 3:19106c977df1 | 41 | first = 1; |
sahabi | 3:19106c977df1 | 42 | } |
sahabi | 3:19106c977df1 | 43 | |
sahabi | 3:19106c977df1 | 44 | else if (first == 1){ |
sahabi | 3:19106c977df1 | 45 | time1.stop(); |
sahabi | 3:19106c977df1 | 46 | first = 0; |
sahabi | 3:19106c977df1 | 47 | once++; |
sahabi | 3:19106c977df1 | 48 | frequency = 1./time1.read(); |
sahabi | 6:1f9baf8d833e | 49 | |
sahabi | 7:9ff7df122ff6 | 50 | if (once == 1000){ |
sahabi | 3:19106c977df1 | 51 | once = 0; |
sahabi | 3:19106c977df1 | 52 | |
sahabi | 6:1f9baf8d833e | 53 | if (frequency >= 900.0f && frequency <= 1100.0f){currentfloor = 5;} |
sahabi | 6:1f9baf8d833e | 54 | else if (frequency >= 600.0f && frequency <= 800.0f){currentfloor = 4;} |
sahabi | 6:1f9baf8d833e | 55 | else if (frequency >= 400.0f && frequency <= 590.0f){currentfloor = 3;} |
sahabi | 6:1f9baf8d833e | 56 | else if (frequency >= 200.0f && frequency <= 300.0f){currentfloor = 2;} |
sahabi | 6:1f9baf8d833e | 57 | else if (frequency >= 50.0f && frequency <= 150.0f){currentfloor = 1;} |
sahabi | 3:19106c977df1 | 58 | |
sahabi | 3:19106c977df1 | 59 | if (currentfloor == requested_floor){ |
sahabi | 6:1f9baf8d833e | 60 | pc.printf("served: %d floor frequency: %f \n",currentfloor,frequency); |
sahabi | 6:1f9baf8d833e | 61 | |
sahabi | 3:19106c977df1 | 62 | dc_motor.write(0.0f); |
sahabi | 3:19106c977df1 | 63 | led1 = 0; |
sahabi | 3:19106c977df1 | 64 | led2 = 0; |
sahabi | 3:19106c977df1 | 65 | led3 = 0; |
sahabi | 3:19106c977df1 | 66 | led4 = 0; |
sahabi | 3:19106c977df1 | 67 | in1 = 0; |
sahabi | 3:19106c977df1 | 68 | in2 = 0; |
sahabi | 3:19106c977df1 | 69 | busy = 0; |
sahabi | 7:9ff7df122ff6 | 70 | servo1.write(open); |
sahabi | 7:9ff7df122ff6 | 71 | servo2.write(open2); |
sahabi | 3:19106c977df1 | 72 | location = requested_floor; |
sahabi | 6:1f9baf8d833e | 73 | pc.printf("at: %d \n",location); |
sahabi | 4:fba6c97a6192 | 74 | sensor.fall(0); |
sahabi | 3:19106c977df1 | 75 | } |
sahabi | 2:07f2fe858dbd | 76 | |
sahabi | 0:69be3faae0a0 | 77 | } |
sahabi | 2:07f2fe858dbd | 78 | |
sahabi | 2:07f2fe858dbd | 79 | } |
sahabi | 1:28120714ad80 | 80 | } |
sahabi | 0:69be3faae0a0 | 81 | } |
sahabi | 1:28120714ad80 | 82 | |
sahabi | 0:69be3faae0a0 | 83 | void moveUp(){ |
sahabi | 7:9ff7df122ff6 | 84 | dc_motor.write(1.0f); |
sahabi | 0:69be3faae0a0 | 85 | in1 = 1; |
sahabi | 0:69be3faae0a0 | 86 | in2 = 0; |
sahabi | 0:69be3faae0a0 | 87 | } |
sahabi | 0:69be3faae0a0 | 88 | |
sahabi | 0:69be3faae0a0 | 89 | void moveDown(){ |
sahabi | 3:19106c977df1 | 90 | |
sahabi | 7:9ff7df122ff6 | 91 | dc_motor.write(1.0f); |
sahabi | 0:69be3faae0a0 | 92 | in1 = 0; |
sahabi | 0:69be3faae0a0 | 93 | in2 = 1; |
sahabi | 0:69be3faae0a0 | 94 | } |
sahabi | 0:69be3faae0a0 | 95 | |
sahabi | 0:69be3faae0a0 | 96 | |
sahabi | 0:69be3faae0a0 | 97 | void closeDoor(){ |
sahabi | 7:9ff7df122ff6 | 98 | servo1.write(closed); |
sahabi | 7:9ff7df122ff6 | 99 | servo2.write(closed2); |
sahabi | 0:69be3faae0a0 | 100 | } |
sahabi | 0:69be3faae0a0 | 101 | |
sahabi | 0:69be3faae0a0 | 102 | void openDoor(){ |
sahabi | 7:9ff7df122ff6 | 103 | servo1.write(open); |
sahabi | 7:9ff7df122ff6 | 104 | servo2.write(open2); |
sahabi | 0:69be3faae0a0 | 105 | } |
sahabi | 0:69be3faae0a0 | 106 | |
sahabi | 0:69be3faae0a0 | 107 | void call(int next) { |
sahabi | 4:fba6c97a6192 | 108 | if (busy == 0 && next > 0.5){ |
sahabi | 1:28120714ad80 | 109 | requested_floor = next; |
sahabi | 1:28120714ad80 | 110 | if (location > next){ |
sahabi | 6:1f9baf8d833e | 111 | pc.printf("serving: %d \n",next); |
sahabi | 1:28120714ad80 | 112 | closeDoor(); |
sahabi | 1:28120714ad80 | 113 | moveDown(); |
sahabi | 1:28120714ad80 | 114 | busy = 1; |
sahabi | 4:fba6c97a6192 | 115 | sensor.fall(&serve); |
sahabi | 1:28120714ad80 | 116 | } |
sahabi | 1:28120714ad80 | 117 | |
sahabi | 4:fba6c97a6192 | 118 | else if (location < next ){ |
sahabi | 6:1f9baf8d833e | 119 | pc.printf("serving: %d \n",next); |
sahabi | 1:28120714ad80 | 120 | closeDoor(); |
sahabi | 1:28120714ad80 | 121 | moveUp(); |
sahabi | 1:28120714ad80 | 122 | busy = 1; |
sahabi | 4:fba6c97a6192 | 123 | sensor.fall(&serve); |
sahabi | 1:28120714ad80 | 124 | } |
sahabi | 0:69be3faae0a0 | 125 | } |
sahabi | 1:28120714ad80 | 126 | } |
sahabi | 0:69be3faae0a0 | 127 | |
sahabi | 0:69be3faae0a0 | 128 | int main() { |
sahabi | 0:69be3faae0a0 | 129 | servo1.period(0.02f); |
sahabi | 0:69be3faae0a0 | 130 | servo2.period(0.02f); |
sahabi | 0:69be3faae0a0 | 131 | in1.output(); |
sahabi | 0:69be3faae0a0 | 132 | in2.output(); |
sahabi | 0:69be3faae0a0 | 133 | in2 = 0; |
sahabi | 7:9ff7df122ff6 | 134 | in1 = 0; |
sahabi | 6:1f9baf8d833e | 135 | location = 1; |
sahabi | 2:07f2fe858dbd | 136 | |
sahabi | 3:19106c977df1 | 137 | while(1){ |
sahabi | 4:fba6c97a6192 | 138 | |
sahabi | 4:fba6c97a6192 | 139 | while(busy == 0){ |
sahabi | 4:fba6c97a6192 | 140 | |
sahabi | 6:1f9baf8d833e | 141 | if (an.read()*3.3f > 0.62f && an.read()*3.3f < 1.0f ){requested_floor1 = 1;} //1 |
sahabi | 6:1f9baf8d833e | 142 | else if (an.read()*3.3f > 1.20f && an.read()*3.3f < 1.60f ){requested_floor1 = 2;}//2 |
sahabi | 6:1f9baf8d833e | 143 | else if (an.read()*3.3f > 1.70f && an.read()*3.3f < 2.3f ){requested_floor1 = 3;} //3 |
sahabi | 6:1f9baf8d833e | 144 | else if (an.read()*3.3f > 2.4f && an.read()*3.3f < 2.9f ){requested_floor1 = 4;} //4 |
sahabi | 6:1f9baf8d833e | 145 | else if (an.read()*3.3f > 3.0f && an.read()*3.3f < 3.5f ){requested_floor1 = 5;} //5 |
sahabi | 4:fba6c97a6192 | 146 | else{requested_floor1 = 0;} |
sahabi | 4:fba6c97a6192 | 147 | |
sahabi | 6:1f9baf8d833e | 148 | if (an.read()*3.3f > 0.62f && an.read()*3.3f < 1.0f ){requested_floor2 = 1;led1 = 1;} |
sahabi | 6:1f9baf8d833e | 149 | else if (an.read()*3.3f > 1.20f && an.read()*3.3f < 1.60f ){requested_floor2 = 2;led2 = 1;} |
sahabi | 6:1f9baf8d833e | 150 | else if (an.read()*3.3f > 1.70f && an.read()*3.3f < 2.3f ){requested_floor2 = 3;led3 = 1;} |
sahabi | 6:1f9baf8d833e | 151 | else if (an.read()*3.3f > 2.4f && an.read()*3.3f < 2.9f ){requested_floor2 = 4; led4 = 1;} |
sahabi | 5:15bb8f6b6d85 | 152 | else if (an.read()*3.3f > 3.0f && an.read()*3.3f < 3.5f ){requested_floor2 = 5;} |
sahabi | 4:fba6c97a6192 | 153 | else{requested_floor2 = 0;} |
sahabi | 4:fba6c97a6192 | 154 | |
sahabi | 4:fba6c97a6192 | 155 | if (requested_floor1 != requested_floor2) {requested_floor = 0;} |
sahabi | 4:fba6c97a6192 | 156 | else{requested_floor = requested_floor1;} |
sahabi | 6:1f9baf8d833e | 157 | |
sahabi | 4:fba6c97a6192 | 158 | call(requested_floor); |
sahabi | 7:9ff7df122ff6 | 159 | } |
sahabi | 0:69be3faae0a0 | 160 | } |
sahabi | 7:9ff7df122ff6 | 161 | } |