attempt 1
Dependencies: mbed
main.cpp@1:54ae0e3ae1b7, 2015-03-25 (annotated)
- Committer:
- jaredwil
- Date:
- Wed Mar 25 00:35:40 2015 +0000
- Revision:
- 1:54ae0e3ae1b7
- Parent:
- 0:fda0081e634b
- Child:
- 2:dcd904176b27
final to be tested
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jaredwil | 0:fda0081e634b | 1 | #include "mbed.h" |
jaredwil | 0:fda0081e634b | 2 | #include <map> |
jaredwil | 0:fda0081e634b | 3 | |
jaredwil | 0:fda0081e634b | 4 | //elevator I/O |
jaredwil | 0:fda0081e634b | 5 | BusOut leds(LED1,LED2,LED3,LED4); |
jaredwil | 1:54ae0e3ae1b7 | 6 | PwmOut EN(p23); |
jaredwil | 1:54ae0e3ae1b7 | 7 | DigitalOut IN1(p24); |
jaredwil | 1:54ae0e3ae1b7 | 8 | DigitalOut IN2(p25); |
jaredwil | 1:54ae0e3ae1b7 | 9 | |
jaredwil | 1:54ae0e3ae1b7 | 10 | |
jaredwil | 1:54ae0e3ae1b7 | 11 | //p23 LEFT p24 RIGHT |
jaredwil | 1:54ae0e3ae1b7 | 12 | PwmOut leftDoor(p21); |
jaredwil | 1:54ae0e3ae1b7 | 13 | PwmOut rightDoor(p22); |
jaredwil | 1:54ae0e3ae1b7 | 14 | |
jaredwil | 1:54ae0e3ae1b7 | 15 | InterruptIn irIN(p16); |
jaredwil | 0:fda0081e634b | 16 | |
jaredwil | 0:fda0081e634b | 17 | Serial pc(USBTX, USBRX); |
jaredwil | 0:fda0081e634b | 18 | |
jaredwil | 0:fda0081e634b | 19 | |
jaredwil | 0:fda0081e634b | 20 | //itizilize digital I/O for keypad |
jaredwil | 1:54ae0e3ae1b7 | 21 | DigitalOut row1(p27); |
jaredwil | 1:54ae0e3ae1b7 | 22 | DigitalOut row2(p28); |
jaredwil | 0:fda0081e634b | 23 | DigitalIn col1(p20); |
jaredwil | 0:fda0081e634b | 24 | DigitalIn col2(p19); |
jaredwil | 0:fda0081e634b | 25 | DigitalIn col3(p18); |
jaredwil | 0:fda0081e634b | 26 | |
jaredwil | 0:fda0081e634b | 27 | //timer for row switch |
jaredwil | 0:fda0081e634b | 28 | Timer t1; |
jaredwil | 0:fda0081e634b | 29 | Timer irT; |
jaredwil | 0:fda0081e634b | 30 | //init keypad map |
jaredwil | 0:fda0081e634b | 31 | map<int, int> keypadMap; |
jaredwil | 0:fda0081e634b | 32 | |
jaredwil | 1:54ae0e3ae1b7 | 33 | int curFloor = 1; //initialize at 1 |
jaredwil | 1:54ae0e3ae1b7 | 34 | int floorDesired = 1; //init floor buffer |
jaredwil | 1:54ae0e3ae1b7 | 35 | bool floors[5]; |
jaredwil | 1:54ae0e3ae1b7 | 36 | |
jaredwil | 1:54ae0e3ae1b7 | 37 | int count = 0; |
jaredwil | 1:54ae0e3ae1b7 | 38 | int nextFloor; |
jaredwil | 1:54ae0e3ae1b7 | 39 | |
jaredwil | 1:54ae0e3ae1b7 | 40 | //direction 1:up; 2: down; |
jaredwil | 1:54ae0e3ae1b7 | 41 | int direction = 0; |
jaredwil | 1:54ae0e3ae1b7 | 42 | |
jaredwil | 1:54ae0e3ae1b7 | 43 | float first = 10000.0; |
jaredwil | 1:54ae0e3ae1b7 | 44 | float second = 5000.0; |
jaredwil | 1:54ae0e3ae1b7 | 45 | float third = 3333.0; |
jaredwil | 1:54ae0e3ae1b7 | 46 | float fourth = 2500.0; |
jaredwil | 1:54ae0e3ae1b7 | 47 | float fifth = 2000.0; |
jaredwil | 1:54ae0e3ae1b7 | 48 | |
jaredwil | 1:54ae0e3ae1b7 | 49 | float tol = 250.0; |
jaredwil | 1:54ae0e3ae1b7 | 50 | |
jaredwil | 0:fda0081e634b | 51 | void init(){ |
jaredwil | 1:54ae0e3ae1b7 | 52 | /* |
jaredwil | 0:fda0081e634b | 53 | keypadMap[0x11]= 0x1; |
jaredwil | 0:fda0081e634b | 54 | keypadMap[0x12]= 0x2; |
jaredwil | 0:fda0081e634b | 55 | keypadMap[0x14]= 0x4; |
jaredwil | 0:fda0081e634b | 56 | keypadMap[0x18]= 0x0; |
jaredwil | 0:fda0081e634b | 57 | keypadMap[0x21]= 0x8; |
jaredwil | 0:fda0081e634b | 58 | keypadMap[0x22]= 0x0; |
jaredwil | 0:fda0081e634b | 59 | keypadMap[0x24]= 0x0; |
jaredwil | 0:fda0081e634b | 60 | keypadMap[0x28]= 0x0; |
jaredwil | 1:54ae0e3ae1b7 | 61 | */ |
jaredwil | 1:54ae0e3ae1b7 | 62 | |
jaredwil | 1:54ae0e3ae1b7 | 63 | keypadMap[0x11]= 1; |
jaredwil | 1:54ae0e3ae1b7 | 64 | keypadMap[0x12]= 2; |
jaredwil | 1:54ae0e3ae1b7 | 65 | keypadMap[0x14]= 3; |
jaredwil | 1:54ae0e3ae1b7 | 66 | keypadMap[0x21]= 4; |
jaredwil | 1:54ae0e3ae1b7 | 67 | keypadMap[0x22]= 5; |
jaredwil | 0:fda0081e634b | 68 | } |
jaredwil | 0:fda0081e634b | 69 | |
jaredwil | 1:54ae0e3ae1b7 | 70 | void openDoors(){ |
jaredwil | 1:54ae0e3ae1b7 | 71 | |
jaredwil | 1:54ae0e3ae1b7 | 72 | leftDoor= 0.075; //init door closed |
jaredwil | 1:54ae0e3ae1b7 | 73 | rightDoor= 0.125; //itit door closed] |
jaredwil | 1:54ae0e3ae1b7 | 74 | |
jaredwil | 1:54ae0e3ae1b7 | 75 | } |
jaredwil | 1:54ae0e3ae1b7 | 76 | void closeDoors(){ |
jaredwil | 1:54ae0e3ae1b7 | 77 | |
jaredwil | 1:54ae0e3ae1b7 | 78 | leftDoor= 0.125; //init door closed |
jaredwil | 1:54ae0e3ae1b7 | 79 | rightDoor= 0.075; //itit door closed |
jaredwil | 1:54ae0e3ae1b7 | 80 | } |
jaredwil | 1:54ae0e3ae1b7 | 81 | |
jaredwil | 1:54ae0e3ae1b7 | 82 | void up(){ |
jaredwil | 1:54ae0e3ae1b7 | 83 | EN = 0.2; |
jaredwil | 1:54ae0e3ae1b7 | 84 | IN1 = 1; |
jaredwil | 1:54ae0e3ae1b7 | 85 | IN2 = 0.0; |
jaredwil | 1:54ae0e3ae1b7 | 86 | } |
jaredwil | 1:54ae0e3ae1b7 | 87 | void down(){ |
jaredwil | 1:54ae0e3ae1b7 | 88 | EN = 0.2; |
jaredwil | 1:54ae0e3ae1b7 | 89 | IN1 = 0.0; |
jaredwil | 1:54ae0e3ae1b7 | 90 | IN2 = 1; |
jaredwil | 1:54ae0e3ae1b7 | 91 | } |
jaredwil | 1:54ae0e3ae1b7 | 92 | |
jaredwil | 1:54ae0e3ae1b7 | 93 | void stop(){ |
jaredwil | 1:54ae0e3ae1b7 | 94 | EN = 0.0; |
jaredwil | 1:54ae0e3ae1b7 | 95 | IN1 = 0.0; |
jaredwil | 1:54ae0e3ae1b7 | 96 | IN2 = 0.0; |
jaredwil | 1:54ae0e3ae1b7 | 97 | } |
jaredwil | 1:54ae0e3ae1b7 | 98 | |
jaredwil | 1:54ae0e3ae1b7 | 99 | int getFloor(float period) { |
jaredwil | 1:54ae0e3ae1b7 | 100 | if (first - tol < period && first + tol >= period) |
jaredwil | 1:54ae0e3ae1b7 | 101 | return 1; |
jaredwil | 1:54ae0e3ae1b7 | 102 | else if (second - tol < period && second + tol >= period) |
jaredwil | 1:54ae0e3ae1b7 | 103 | return 2; |
jaredwil | 1:54ae0e3ae1b7 | 104 | else if (third - tol < period && third + tol >= period) |
jaredwil | 1:54ae0e3ae1b7 | 105 | return 3; |
jaredwil | 1:54ae0e3ae1b7 | 106 | else if (fourth - tol < period && fourth + tol >= period) |
jaredwil | 1:54ae0e3ae1b7 | 107 | return 4; |
jaredwil | 1:54ae0e3ae1b7 | 108 | else if (fifth - tol < period && fifth + tol >= period) |
jaredwil | 1:54ae0e3ae1b7 | 109 | return 5; |
jaredwil | 1:54ae0e3ae1b7 | 110 | else return 0; |
jaredwil | 1:54ae0e3ae1b7 | 111 | } |
jaredwil | 1:54ae0e3ae1b7 | 112 | |
jaredwil | 1:54ae0e3ae1b7 | 113 | void irRise() { |
jaredwil | 1:54ae0e3ae1b7 | 114 | irT.start(); |
jaredwil | 1:54ae0e3ae1b7 | 115 | } |
jaredwil | 1:54ae0e3ae1b7 | 116 | |
jaredwil | 1:54ae0e3ae1b7 | 117 | int temp; |
jaredwil | 1:54ae0e3ae1b7 | 118 | void irFall() { |
jaredwil | 1:54ae0e3ae1b7 | 119 | irT.stop(); |
jaredwil | 1:54ae0e3ae1b7 | 120 | float irPeriod = 2 * irT.read_us(); |
jaredwil | 1:54ae0e3ae1b7 | 121 | |
jaredwil | 1:54ae0e3ae1b7 | 122 | temp = getFloor(irPeriod); |
jaredwil | 1:54ae0e3ae1b7 | 123 | if(temp != 0) |
jaredwil | 1:54ae0e3ae1b7 | 124 | curFloor = temp; |
jaredwil | 1:54ae0e3ae1b7 | 125 | irT.reset(); |
jaredwil | 1:54ae0e3ae1b7 | 126 | } |
jaredwil | 1:54ae0e3ae1b7 | 127 | |
jaredwil | 1:54ae0e3ae1b7 | 128 | int getNextFloor() { |
jaredwil | 1:54ae0e3ae1b7 | 129 | //up |
jaredwil | 1:54ae0e3ae1b7 | 130 | if (direction == 1){ |
jaredwil | 1:54ae0e3ae1b7 | 131 | for (int i = curFloor; i< 4; ++i){ |
jaredwil | 1:54ae0e3ae1b7 | 132 | if(floors[i]) |
jaredwil | 1:54ae0e3ae1b7 | 133 | return i+1; |
jaredwil | 1:54ae0e3ae1b7 | 134 | } |
jaredwil | 1:54ae0e3ae1b7 | 135 | for (int i = curFloor - 2; i >=0 ; --i){ |
jaredwil | 1:54ae0e3ae1b7 | 136 | if(floors[i]) |
jaredwil | 1:54ae0e3ae1b7 | 137 | return i+1; |
jaredwil | 1:54ae0e3ae1b7 | 138 | } |
jaredwil | 1:54ae0e3ae1b7 | 139 | } |
jaredwil | 1:54ae0e3ae1b7 | 140 | //down |
jaredwil | 1:54ae0e3ae1b7 | 141 | if (direction == 2){ |
jaredwil | 1:54ae0e3ae1b7 | 142 | for (int i = curFloor - 2; i >=0 ; --i){ |
jaredwil | 1:54ae0e3ae1b7 | 143 | if(floors[i]) |
jaredwil | 1:54ae0e3ae1b7 | 144 | return i+1; |
jaredwil | 1:54ae0e3ae1b7 | 145 | } |
jaredwil | 1:54ae0e3ae1b7 | 146 | for (int i = curFloor; i< 4; ++i){ |
jaredwil | 1:54ae0e3ae1b7 | 147 | if(floors[i]) |
jaredwil | 1:54ae0e3ae1b7 | 148 | return i+1; |
jaredwil | 1:54ae0e3ae1b7 | 149 | } |
jaredwil | 1:54ae0e3ae1b7 | 150 | } |
jaredwil | 1:54ae0e3ae1b7 | 151 | else return floorDesired; |
jaredwil | 1:54ae0e3ae1b7 | 152 | } |
jaredwil | 1:54ae0e3ae1b7 | 153 | |
jaredwil | 0:fda0081e634b | 154 | int main() { |
jaredwil | 0:fda0081e634b | 155 | |
jaredwil | 0:fda0081e634b | 156 | init(); |
jaredwil | 0:fda0081e634b | 157 | //Initialize rows to 0 and keypad stuff |
jaredwil | 0:fda0081e634b | 158 | row1 = 0; |
jaredwil | 0:fda0081e634b | 159 | row2 = 0; |
jaredwil | 0:fda0081e634b | 160 | t1.start(); |
jaredwil | 0:fda0081e634b | 161 | int cur_input = 0x00; |
jaredwil | 0:fda0081e634b | 162 | int prev_input = 0x00; |
jaredwil | 1:54ae0e3ae1b7 | 163 | //float irPeriod = 0; |
jaredwil | 1:54ae0e3ae1b7 | 164 | irIN.rise(&irRise); |
jaredwil | 1:54ae0e3ae1b7 | 165 | irIN.fall(&irFall); |
jaredwil | 0:fda0081e634b | 166 | |
jaredwil | 1:54ae0e3ae1b7 | 167 | //ELEVATOR STATE |
jaredwil | 1:54ae0e3ae1b7 | 168 | //int nofloor = 0; //set nofloor state to 0 |
jaredwil | 1:54ae0e3ae1b7 | 169 | |
jaredwil | 1:54ae0e3ae1b7 | 170 | int Serv = 0; |
jaredwil | 0:fda0081e634b | 171 | //initialize servo and dc motor |
jaredwil | 1:54ae0e3ae1b7 | 172 | float freq = 50; //set 50 HZ freq for PWM and motor |
jaredwil | 1:54ae0e3ae1b7 | 173 | EN.period(1/freq); |
jaredwil | 1:54ae0e3ae1b7 | 174 | |
jaredwil | 1:54ae0e3ae1b7 | 175 | |
jaredwil | 1:54ae0e3ae1b7 | 176 | leftDoor.period(1/freq); |
jaredwil | 1:54ae0e3ae1b7 | 177 | leftDoor= 0.125; //init door closed |
jaredwil | 1:54ae0e3ae1b7 | 178 | rightDoor.period(1/freq); |
jaredwil | 1:54ae0e3ae1b7 | 179 | rightDoor= 0.075; //itit door closed |
jaredwil | 0:fda0081e634b | 180 | |
jaredwil | 0:fda0081e634b | 181 | while(1) { |
jaredwil | 1:54ae0e3ae1b7 | 182 | pc.printf("Current Floor = %d, Next Floor = %d\r",curFloor,nextFloor); |
jaredwil | 0:fda0081e634b | 183 | |
jaredwil | 0:fda0081e634b | 184 | /////////////Below contains all code in order to detect current input from keypad//////////////////////////// |
jaredwil | 0:fda0081e634b | 185 | if(prev_input == 0){ |
jaredwil | 0:fda0081e634b | 186 | |
jaredwil | 0:fda0081e634b | 187 | row1 = 0; |
jaredwil | 0:fda0081e634b | 188 | row2 = 0; |
jaredwil | 0:fda0081e634b | 189 | //Keep each pin on for 4ms |
jaredwil | 0:fda0081e634b | 190 | //"turn on" appropriat bit in map when on. |
jaredwil | 0:fda0081e634b | 191 | switch(t1.read_ms()%8){ |
jaredwil | 0:fda0081e634b | 192 | case 0: |
jaredwil | 0:fda0081e634b | 193 | row1 = 1; |
jaredwil | 0:fda0081e634b | 194 | cur_input |= 0x10; |
jaredwil | 0:fda0081e634b | 195 | break; |
jaredwil | 0:fda0081e634b | 196 | case 4: |
jaredwil | 0:fda0081e634b | 197 | row2 = 1; |
jaredwil | 0:fda0081e634b | 198 | cur_input |= 0x20; |
jaredwil | 0:fda0081e634b | 199 | break; |
jaredwil | 0:fda0081e634b | 200 | } |
jaredwil | 0:fda0081e634b | 201 | } |
jaredwil | 0:fda0081e634b | 202 | |
jaredwil | 0:fda0081e634b | 203 | //Check each colum to see if it is high |
jaredwil | 0:fda0081e634b | 204 | //if it is "turn on" that respective bit |
jaredwil | 0:fda0081e634b | 205 | if(col1 == 1) |
jaredwil | 0:fda0081e634b | 206 | cur_input |= 0x01; |
jaredwil | 0:fda0081e634b | 207 | else if(col2 == 1) |
jaredwil | 0:fda0081e634b | 208 | cur_input |= 0x02; |
jaredwil | 0:fda0081e634b | 209 | else if(col3 == 1) |
jaredwil | 0:fda0081e634b | 210 | cur_input |= 0x04; |
jaredwil | 0:fda0081e634b | 211 | else cur_input = 0; //Detect button release or no input set input to 0 |
jaredwil | 1:54ae0e3ae1b7 | 212 | |
jaredwil | 1:54ae0e3ae1b7 | 213 | //leds = keypadMap[cur_input]; //KEYPAD DEBUG |
jaredwil | 1:54ae0e3ae1b7 | 214 | if(cur_input != prev_input && cur_input != 0){ |
jaredwil | 1:54ae0e3ae1b7 | 215 | floorDesired = keypadMap[cur_input]; |
jaredwil | 1:54ae0e3ae1b7 | 216 | floors[floorDesired - 1] = true; |
jaredwil | 1:54ae0e3ae1b7 | 217 | nextFloor = getNextFloor(); |
jaredwil | 1:54ae0e3ae1b7 | 218 | //Serv++; |
jaredwil | 1:54ae0e3ae1b7 | 219 | } |
jaredwil | 0:fda0081e634b | 220 | //Maintain the past input |
jaredwil | 0:fda0081e634b | 221 | prev_input = cur_input; |
jaredwil | 1:54ae0e3ae1b7 | 222 | |
jaredwil | 1:54ae0e3ae1b7 | 223 | ////////////////////////MOVE FLOORS////////////////////////////////////////////////// |
jaredwil | 1:54ae0e3ae1b7 | 224 | |
jaredwil | 1:54ae0e3ae1b7 | 225 | |
jaredwil | 1:54ae0e3ae1b7 | 226 | if(curFloor == nextFloor){ |
jaredwil | 1:54ae0e3ae1b7 | 227 | stop(); |
jaredwil | 1:54ae0e3ae1b7 | 228 | floors[nextFloor-1] = false; |
jaredwil | 1:54ae0e3ae1b7 | 229 | nextFloor = getNextFloor(); |
jaredwil | 1:54ae0e3ae1b7 | 230 | } |
jaredwil | 1:54ae0e3ae1b7 | 231 | if(curFloor < nexFloor){ |
jaredwil | 1:54ae0e3ae1b7 | 232 | up(); |
jaredwil | 1:54ae0e3ae1b7 | 233 | direction = 1; |
jaredwil | 1:54ae0e3ae1b7 | 234 | } |
jaredwil | 1:54ae0e3ae1b7 | 235 | if(curFloor > nextFloor){ |
jaredwil | 1:54ae0e3ae1b7 | 236 | down(); |
jaredwil | 1:54ae0e3ae1b7 | 237 | direction = 2; |
jaredwil | 1:54ae0e3ae1b7 | 238 | } |
jaredwil | 1:54ae0e3ae1b7 | 239 | |
jaredwil | 1:54ae0e3ae1b7 | 240 | |
jaredwil | 0:fda0081e634b | 241 | /////////////////////////////////////////END KEYPAD////////////////////////////////////////////////////////// |
jaredwil | 0:fda0081e634b | 242 | |
jaredwil | 0:fda0081e634b | 243 | |
jaredwil | 0:fda0081e634b | 244 | } |
jaredwil | 0:fda0081e634b | 245 | |
jaredwil | 0:fda0081e634b | 246 | |
jaredwil | 0:fda0081e634b | 247 | } |