switch box for pneumatic state machine. Simple version with no grab drop features. Mid state of converting script to function calls within main()
main.cpp@1:2ee833c4b76f, 2014-01-31 (annotated)
- Committer:
- joliu
- Date:
- Fri Jan 31 22:08:20 2014 +0000
- Revision:
- 1:2ee833c4b76f
- Parent:
- 0:ae0fe1adad01
before sending to Amando. Unsure of revision status or functionality as is.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
joliu | 0:ae0fe1adad01 | 1 | #include "mbed.h" |
joliu | 1:2ee833c4b76f | 2 | //Serial pc(USBTX, USBRX); |
joliu | 0:ae0fe1adad01 | 3 | Serial lcd(p28, p27); |
joliu | 0:ae0fe1adad01 | 4 | |
joliu | 1:2ee833c4b76f | 5 | // V+/V- outputs cylinder down |
joliu | 1:2ee833c4b76f | 6 | DigitalOut out1(p7); |
joliu | 1:2ee833c4b76f | 7 | DigitalOut out2(p8); |
joliu | 1:2ee833c4b76f | 8 | DigitalOut out3(p9); |
joliu | 1:2ee833c4b76f | 9 | DigitalOut out4(p10); |
joliu | 1:2ee833c4b76f | 10 | // V+/V- outputs cylinder up |
joliu | 1:2ee833c4b76f | 11 | DigitalOut out5(p11); |
joliu | 1:2ee833c4b76f | 12 | DigitalOut out6(p12); |
joliu | 1:2ee833c4b76f | 13 | DigitalOut out7(p13); |
joliu | 1:2ee833c4b76f | 14 | DigitalOut out8(p14); |
joliu | 1:2ee833c4b76f | 15 | // Four push buttons left to right |
joliu | 1:2ee833c4b76f | 16 | DigitalIn pushOne(p21); |
joliu | 1:2ee833c4b76f | 17 | DigitalIn pushTwo(p22); |
joliu | 1:2ee833c4b76f | 18 | DigitalIn pushThree(p23); |
joliu | 1:2ee833c4b76f | 19 | DigitalIn pushFour(p24); |
joliu | 1:2ee833c4b76f | 20 | // Run switch where p26 will be kept high |
joliu | 1:2ee833c4b76f | 21 | // Can probably connect it to the 3.3V out |
joliu | 1:2ee833c4b76f | 22 | DigitalIn runIn(p25); |
joliu | 1:2ee833c4b76f | 23 | DigitalOut runOut(p26); |
joliu | 1:2ee833c4b76f | 24 | |
joliu | 1:2ee833c4b76f | 25 | float upTime; |
joliu | 1:2ee833c4b76f | 26 | float downTime; |
joliu | 1:2ee833c4b76f | 27 | float waitTime; |
joliu | 1:2ee833c4b76f | 28 | unsigned int count = 0; |
joliu | 1:2ee833c4b76f | 29 | typedef char * string; |
joliu | 1:2ee833c4b76f | 30 | string strs[5] = { |
joliu | 1:2ee833c4b76f | 31 | "Up Time", // 0 |
joliu | 1:2ee833c4b76f | 32 | "Down Time", // 1 |
joliu | 1:2ee833c4b76f | 33 | "Wait Time", // 2 |
joliu | 1:2ee833c4b76f | 34 | "Target Count", // 3 |
joliu | 1:2ee833c4b76f | 35 | "Reset Counter" // 4 |
joliu | 1:2ee833c4b76f | 36 | }; |
joliu | 1:2ee833c4b76f | 37 | enum ValveMode { |
joliu | 1:2ee833c4b76f | 38 | UP, |
joliu | 1:2ee833c4b76f | 39 | DOWN |
joliu | 1:2ee833c4b76f | 40 | }; |
joliu | 1:2ee833c4b76f | 41 | |
joliu | 1:2ee833c4b76f | 42 | |
joliu | 1:2ee833c4b76f | 43 | |
joliu | 1:2ee833c4b76f | 44 | |
joliu | 1:2ee833c4b76f | 45 | |
joliu | 0:ae0fe1adad01 | 46 | void clearLCD() |
joliu | 0:ae0fe1adad01 | 47 | { |
joliu | 0:ae0fe1adad01 | 48 | lcd.printf("%c%c",0xFE,0x01); |
joliu | 0:ae0fe1adad01 | 49 | } |
joliu | 0:ae0fe1adad01 | 50 | void setLCDpos(int xpos, int ypos) |
joliu | 0:ae0fe1adad01 | 51 | { |
joliu | 0:ae0fe1adad01 | 52 | int pos = 0x80; |
joliu | 0:ae0fe1adad01 | 53 | switch (xpos) { |
joliu | 0:ae0fe1adad01 | 54 | case 1: |
joliu | 0:ae0fe1adad01 | 55 | pos +=0; |
joliu | 0:ae0fe1adad01 | 56 | break; |
joliu | 0:ae0fe1adad01 | 57 | case 2: |
joliu | 0:ae0fe1adad01 | 58 | pos +=64; |
joliu | 0:ae0fe1adad01 | 59 | break; |
joliu | 0:ae0fe1adad01 | 60 | case 3: |
joliu | 0:ae0fe1adad01 | 61 | pos +=20; |
joliu | 0:ae0fe1adad01 | 62 | break; |
joliu | 0:ae0fe1adad01 | 63 | case 4: |
joliu | 0:ae0fe1adad01 | 64 | pos +=84; |
joliu | 0:ae0fe1adad01 | 65 | break; |
joliu | 0:ae0fe1adad01 | 66 | } |
joliu | 0:ae0fe1adad01 | 67 | pos += (ypos-1); |
joliu | 0:ae0fe1adad01 | 68 | lcd.printf("%c%c",0xFE,pos); |
joliu | 0:ae0fe1adad01 | 69 | } |
joliu | 0:ae0fe1adad01 | 70 | void setBrightness(int lev) |
joliu | 0:ae0fe1adad01 | 71 | { |
joliu | 0:ae0fe1adad01 | 72 | lcd.printf("%c%c",0x7C,lev+127); |
joliu | 0:ae0fe1adad01 | 73 | wait(.1); |
joliu | 0:ae0fe1adad01 | 74 | } |
joliu | 0:ae0fe1adad01 | 75 | |
joliu | 0:ae0fe1adad01 | 76 | |
joliu | 0:ae0fe1adad01 | 77 | |
joliu | 1:2ee833c4b76f | 78 | void setValve(ValveMode dir, int val) |
joliu | 0:ae0fe1adad01 | 79 | { |
joliu | 1:2ee833c4b76f | 80 | switch(dir) { |
joliu | 1:2ee833c4b76f | 81 | case DOWN: |
joliu | 1:2ee833c4b76f | 82 | out1 = val; |
joliu | 1:2ee833c4b76f | 83 | out2 = val; |
joliu | 1:2ee833c4b76f | 84 | out3 = val; |
joliu | 1:2ee833c4b76f | 85 | out4 = val; |
joliu | 0:ae0fe1adad01 | 86 | break; |
joliu | 0:ae0fe1adad01 | 87 | case UP: |
joliu | 1:2ee833c4b76f | 88 | out5 = val; |
joliu | 1:2ee833c4b76f | 89 | out6 = val; |
joliu | 1:2ee833c4b76f | 90 | out7 = val; |
joliu | 1:2ee833c4b76f | 91 | out8 = val; |
joliu | 0:ae0fe1adad01 | 92 | break; |
joliu | 1:2ee833c4b76f | 93 | |
joliu | 0:ae0fe1adad01 | 94 | } |
joliu | 0:ae0fe1adad01 | 95 | } |
joliu | 0:ae0fe1adad01 | 96 | |
joliu | 0:ae0fe1adad01 | 97 | |
joliu | 1:2ee833c4b76f | 98 | /*void lcdShowParameters(int delay, int cycles) |
joliu | 0:ae0fe1adad01 | 99 | { |
joliu | 0:ae0fe1adad01 | 100 | clearLCD(); |
joliu | 0:ae0fe1adad01 | 101 | setLCDpos(1,1); |
joliu | 0:ae0fe1adad01 | 102 | lcd.printf(strs[strPos]); |
joliu | 0:ae0fe1adad01 | 103 | setLCDpos(2,1); |
joliu | 0:ae0fe1adad01 | 104 | if (strPos<=2) { |
joliu | 0:ae0fe1adad01 | 105 | lcd.printf("%f",times[strPos]); |
joliu | 0:ae0fe1adad01 | 106 | } else if(strPos==3) { |
joliu | 0:ae0fe1adad01 | 107 | lcd.printf("%d",target); |
joliu | 0:ae0fe1adad01 | 108 | } else if(strPos==4) { |
joliu | 0:ae0fe1adad01 | 109 | lcd.printf("Y/N?"); |
joliu | 0:ae0fe1adad01 | 110 | } |
joliu | 0:ae0fe1adad01 | 111 | } |
joliu | 0:ae0fe1adad01 | 112 | |
joliu | 0:ae0fe1adad01 | 113 | |
joliu | 0:ae0fe1adad01 | 114 | void lcdConfirmStart() |
joliu | 0:ae0fe1adad01 | 115 | { |
joliu | 0:ae0fe1adad01 | 116 | clearLCD(); |
joliu | 0:ae0fe1adad01 | 117 | setLCDpos(1,1); |
joliu | 0:ae0fe1adad01 | 118 | lcd.printf(strs[strPos]); |
joliu | 0:ae0fe1adad01 | 119 | setLCDpos(2,1); |
joliu | 0:ae0fe1adad01 | 120 | if (strPos<=2) { |
joliu | 0:ae0fe1adad01 | 121 | lcd.printf("%f",times[strPos]); |
joliu | 0:ae0fe1adad01 | 122 | } else if(strPos==3) { |
joliu | 0:ae0fe1adad01 | 123 | lcd.printf("%d",target); |
joliu | 0:ae0fe1adad01 | 124 | } else if(strPos==4) { |
joliu | 0:ae0fe1adad01 | 125 | lcd.printf("Y/N?"); |
joliu | 0:ae0fe1adad01 | 126 | } |
joliu | 0:ae0fe1adad01 | 127 | } |
joliu | 1:2ee833c4b76f | 128 | */ |
joliu | 0:ae0fe1adad01 | 129 | |
joliu | 1:2ee833c4b76f | 130 | void printCount (int count) |
joliu | 1:2ee833c4b76f | 131 | { |
joliu | 1:2ee833c4b76f | 132 | clearLCD(); |
joliu | 1:2ee833c4b76f | 133 | setLCDpos(1,1); |
joliu | 1:2ee833c4b76f | 134 | lcd.printf("CURRENT COUNT IS"); |
joliu | 1:2ee833c4b76f | 135 | setLCDpos(2,1); |
joliu | 1:2ee833c4b76f | 136 | lcd.printf("%d",count); |
joliu | 1:2ee833c4b76f | 137 | } |
joliu | 0:ae0fe1adad01 | 138 | |
joliu | 0:ae0fe1adad01 | 139 | |
joliu | 0:ae0fe1adad01 | 140 | int main() |
joliu | 0:ae0fe1adad01 | 141 | { |
joliu | 0:ae0fe1adad01 | 142 | unsigned int route1 = 0; |
joliu | 0:ae0fe1adad01 | 143 | //unsigned int route2 = 0; |
joliu | 0:ae0fe1adad01 | 144 | float times[3]; |
joliu | 0:ae0fe1adad01 | 145 | times[0] = 1.0; |
joliu | 0:ae0fe1adad01 | 146 | times[1] = 1.0; |
joliu | 0:ae0fe1adad01 | 147 | times[2] = 0.2; |
joliu | 0:ae0fe1adad01 | 148 | int target = 2000; |
joliu | 0:ae0fe1adad01 | 149 | int strPos = 0; |
joliu | 0:ae0fe1adad01 | 150 | Timer timer1; |
joliu | 0:ae0fe1adad01 | 151 | |
joliu | 0:ae0fe1adad01 | 152 | wait(1); |
joliu | 0:ae0fe1adad01 | 153 | clearLCD(); |
joliu | 0:ae0fe1adad01 | 154 | //setLCDpos(1,1); |
joliu | 0:ae0fe1adad01 | 155 | //lcd.printf("Current count is:"); |
joliu | 0:ae0fe1adad01 | 156 | //setLCDpos(2,1); |
joliu | 0:ae0fe1adad01 | 157 | //lcd.printf("%d",0); |
joliu | 0:ae0fe1adad01 | 158 | runOut = 1; |
joliu | 0:ae0fe1adad01 | 159 | while (1) { |
joliu | 0:ae0fe1adad01 | 160 | if (!runIn) { |
joliu | 0:ae0fe1adad01 | 161 | if (!route1) { |
joliu | 0:ae0fe1adad01 | 162 | clearLCD(); |
joliu | 0:ae0fe1adad01 | 163 | route1 = 1; |
joliu | 0:ae0fe1adad01 | 164 | timer1.start(); |
joliu | 0:ae0fe1adad01 | 165 | } |
joliu | 0:ae0fe1adad01 | 166 | |
joliu | 0:ae0fe1adad01 | 167 | else { |
joliu | 0:ae0fe1adad01 | 168 | if(pushOne && timer1.read_ms()>500) { |
joliu | 0:ae0fe1adad01 | 169 | //Go Left |
joliu | 0:ae0fe1adad01 | 170 | if(strPos<1) { |
joliu | 0:ae0fe1adad01 | 171 | strPos=4; |
joliu | 0:ae0fe1adad01 | 172 | } else { |
joliu | 0:ae0fe1adad01 | 173 | strPos--; |
joliu | 0:ae0fe1adad01 | 174 | } |
joliu | 0:ae0fe1adad01 | 175 | |
joliu | 0:ae0fe1adad01 | 176 | clearLCD(); |
joliu | 0:ae0fe1adad01 | 177 | setLCDpos(1,1); |
joliu | 0:ae0fe1adad01 | 178 | lcd.printf(strs[strPos]); |
joliu | 0:ae0fe1adad01 | 179 | setLCDpos(2,1); |
joliu | 0:ae0fe1adad01 | 180 | if (strPos<=2) { |
joliu | 0:ae0fe1adad01 | 181 | lcd.printf("%f",times[strPos]); |
joliu | 0:ae0fe1adad01 | 182 | } else if(strPos==3) { |
joliu | 0:ae0fe1adad01 | 183 | lcd.printf("%d",target); |
joliu | 0:ae0fe1adad01 | 184 | } else if(strPos==4) { |
joliu | 0:ae0fe1adad01 | 185 | lcd.printf("Y/N?"); |
joliu | 0:ae0fe1adad01 | 186 | } |
joliu | 0:ae0fe1adad01 | 187 | timer1.reset(); |
joliu | 0:ae0fe1adad01 | 188 | } else if(pushTwo && timer1.read_ms()>100) { |
joliu | 0:ae0fe1adad01 | 189 | //Increment |
joliu | 0:ae0fe1adad01 | 190 | clearLCD(); |
joliu | 0:ae0fe1adad01 | 191 | setLCDpos(1,1); |
joliu | 0:ae0fe1adad01 | 192 | lcd.printf(strs[strPos]); |
joliu | 0:ae0fe1adad01 | 193 | setLCDpos(2,1); |
joliu | 0:ae0fe1adad01 | 194 | if(strPos<=2) { |
joliu | 0:ae0fe1adad01 | 195 | times[strPos]=times[strPos]+0.1; |
joliu | 0:ae0fe1adad01 | 196 | lcd.printf("%f",times[strPos]); |
joliu | 0:ae0fe1adad01 | 197 | } else if(strPos==3) { |
joliu | 0:ae0fe1adad01 | 198 | target=target+100; |
joliu | 0:ae0fe1adad01 | 199 | lcd.printf("%d",target); |
joliu | 0:ae0fe1adad01 | 200 | } else if(strPos==4) { |
joliu | 0:ae0fe1adad01 | 201 | setLCDpos(2,1); |
joliu | 0:ae0fe1adad01 | 202 | lcd.printf("YES"); |
joliu | 0:ae0fe1adad01 | 203 | count=0; |
joliu | 0:ae0fe1adad01 | 204 | } |
joliu | 0:ae0fe1adad01 | 205 | timer1.reset(); |
joliu | 0:ae0fe1adad01 | 206 | } else if(pushThree && timer1.read_ms()>100) { |
joliu | 0:ae0fe1adad01 | 207 | //Decrement |
joliu | 0:ae0fe1adad01 | 208 | clearLCD(); |
joliu | 0:ae0fe1adad01 | 209 | setLCDpos(1,1); |
joliu | 0:ae0fe1adad01 | 210 | lcd.printf(strs[strPos]); |
joliu | 0:ae0fe1adad01 | 211 | setLCDpos(2,1); |
joliu | 0:ae0fe1adad01 | 212 | if(strPos<=2) { |
joliu | 0:ae0fe1adad01 | 213 | times[strPos]=times[strPos]- 0.1; |
joliu | 0:ae0fe1adad01 | 214 | lcd.printf("%f",times[strPos]); |
joliu | 0:ae0fe1adad01 | 215 | } else if(strPos==3) { |
joliu | 0:ae0fe1adad01 | 216 | target=target-100; |
joliu | 0:ae0fe1adad01 | 217 | lcd.printf("%d",target); |
joliu | 0:ae0fe1adad01 | 218 | } else if(strPos==4) { |
joliu | 0:ae0fe1adad01 | 219 | lcd.printf("NO"); |
joliu | 0:ae0fe1adad01 | 220 | } |
joliu | 0:ae0fe1adad01 | 221 | timer1.reset(); |
joliu | 0:ae0fe1adad01 | 222 | } else if(pushFour && timer1.read_ms()>150) { |
joliu | 0:ae0fe1adad01 | 223 | if(strPos>3) { |
joliu | 0:ae0fe1adad01 | 224 | strPos=0; |
joliu | 0:ae0fe1adad01 | 225 | } else { |
joliu | 0:ae0fe1adad01 | 226 | strPos++; |
joliu | 0:ae0fe1adad01 | 227 | } |
joliu | 0:ae0fe1adad01 | 228 | |
joliu | 0:ae0fe1adad01 | 229 | clearLCD(); |
joliu | 0:ae0fe1adad01 | 230 | setLCDpos(1,1); |
joliu | 0:ae0fe1adad01 | 231 | lcd.printf(strs[strPos]); |
joliu | 0:ae0fe1adad01 | 232 | setLCDpos(2,1); |
joliu | 0:ae0fe1adad01 | 233 | if (strPos<=2) { |
joliu | 0:ae0fe1adad01 | 234 | lcd.printf("%f",times[strPos]); |
joliu | 0:ae0fe1adad01 | 235 | } else if(strPos==3) { |
joliu | 0:ae0fe1adad01 | 236 | lcd.printf("%d",target); |
joliu | 0:ae0fe1adad01 | 237 | } else if(strPos==4) { |
joliu | 0:ae0fe1adad01 | 238 | lcd.printf("Y/N?"); |
joliu | 0:ae0fe1adad01 | 239 | } |
joliu | 0:ae0fe1adad01 | 240 | timer1.reset(); |
joliu | 0:ae0fe1adad01 | 241 | } |
joliu | 0:ae0fe1adad01 | 242 | } |
joliu | 0:ae0fe1adad01 | 243 | } else if (runIn && count<=target) { |
joliu | 0:ae0fe1adad01 | 244 | route1=0; |
joliu | 0:ae0fe1adad01 | 245 | upTime = times[0]; |
joliu | 0:ae0fe1adad01 | 246 | downTime = times[1]; |
joliu | 0:ae0fe1adad01 | 247 | waitTime = times[2]; |
joliu | 1:2ee833c4b76f | 248 | |
joliu | 1:2ee833c4b76f | 249 | setValve(UP, 1); |
joliu | 0:ae0fe1adad01 | 250 | wait(upTime); |
joliu | 1:2ee833c4b76f | 251 | setValve(UP, 0); |
joliu | 0:ae0fe1adad01 | 252 | wait(waitTime); |
joliu | 1:2ee833c4b76f | 253 | setValve(DOWN, 1); |
joliu | 0:ae0fe1adad01 | 254 | wait(downTime); |
joliu | 1:2ee833c4b76f | 255 | setValve(DOWN,0); |
joliu | 0:ae0fe1adad01 | 256 | wait(waitTime); |
joliu | 1:2ee833c4b76f | 257 | |
joliu | 1:2ee833c4b76f | 258 | count++; |
joliu | 1:2ee833c4b76f | 259 | printCount(count); |
joliu | 0:ae0fe1adad01 | 260 | } else if (count>target) { |
joliu | 0:ae0fe1adad01 | 261 | clearLCD(); |
joliu | 0:ae0fe1adad01 | 262 | setLCDpos(1,1); |
joliu | 0:ae0fe1adad01 | 263 | lcd.printf("TARGET REACHED"); |
joliu | 0:ae0fe1adad01 | 264 | setLCDpos(2,1); |
joliu | 0:ae0fe1adad01 | 265 | lcd.printf("%d",target); |
joliu | 0:ae0fe1adad01 | 266 | wait(0.1); |
joliu | 0:ae0fe1adad01 | 267 | } |
joliu | 0:ae0fe1adad01 | 268 | } |
joliu | 0:ae0fe1adad01 | 269 | } |
joliu | 0:ae0fe1adad01 | 270 |