![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
3rd year group project. Electronic and Electrical Engineering. Heriot-Watt University. This is the code for the mbed for the Automatic Little Object Organiser (ALOO).
Dependencies: MCP23017 TCS3472_I2C WattBob_TextLCD mbed
globals.cpp@24:02c61793f90b, 2015-11-29 (annotated)
- Committer:
- dreamselec
- Date:
- Sun Nov 29 01:05:42 2015 +0000
- Revision:
- 24:02c61793f90b
- Parent:
- 23:db91aaa43a9e
- Child:
- 25:792540d69c49
Both break-beam and servo test modes working.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dreamselec | 2:7a55cb10259f | 1 | #include "globals.h" |
dreamselec | 9:dc8f155b71c8 | 2 | #include "Block.h" |
dreamselec | 9:dc8f155b71c8 | 3 | #include "fpga.h" |
dreamselec | 10:16ba52f8e025 | 4 | #include "TCS3472_I2C.h" |
dreamselec | 10:16ba52f8e025 | 5 | #include "MCP23017.h" |
dreamselec | 10:16ba52f8e025 | 6 | #include "WattBob_TextLCD.h" |
dreamselec | 2:7a55cb10259f | 7 | |
dreamselec | 7:b6e31bfdb2af | 8 | int kDefaultBaudRate = 19200; |
dreamselec | 20:4e0f0944f28f | 9 | //TODO: Not let it be constant. |
dreamselec | 20:4e0f0944f28f | 10 | int ColourSensorError = 0.5; |
dreamselec | 2:7a55cb10259f | 11 | //SerialBase gParity = SerialBase::None; |
dreamselec | 2:7a55cb10259f | 12 | int gStopBits = 1; |
dreamselec | 3:843b830ee8bd | 13 | |
dreamselec | 20:4e0f0944f28f | 14 | |
dreamselec | 20:4e0f0944f28f | 15 | Block _HazBlock = Block(); |
dreamselec |
8:e1da2ae62885 | 16 | |
dreamselec |
8:e1da2ae62885 | 17 | bool connectedToPC = false; |
dreamselec | 10:16ba52f8e025 | 18 | bool runServoTest = false; |
dreamselec |
23:db91aaa43a9e | 19 | bool runBreakBeamTest = false; |
dreamselec | 17:af373246bf80 | 20 | PCModes currentMode = None; |
dreamselec |
18:44a1c1a30166 | 21 | Controls currentState = Pause; |
dreamselec |
8:e1da2ae62885 | 22 | |
dreamselec | 20:4e0f0944f28f | 23 | void DefaultHazBlock(){ |
dreamselec | 20:4e0f0944f28f | 24 | Colour _minRedBlock = Colour(); |
dreamselec | 20:4e0f0944f28f | 25 | Colour _maxRedBlock = Colour(); |
dreamselec | 20:4e0f0944f28f | 26 | Colour _averageRedBlock = Colour(); |
dreamselec | 20:4e0f0944f28f | 27 | |
dreamselec | 20:4e0f0944f28f | 28 | for (int i = 0; i < 4; i++){ |
dreamselec | 20:4e0f0944f28f | 29 | _minRedBlock.components[i] = kMinRedBlock[i]; |
dreamselec | 20:4e0f0944f28f | 30 | _maxRedBlock.components[i] = kMaxRedBlock[i]; |
dreamselec | 20:4e0f0944f28f | 31 | _averageRedBlock.components[i] = kAverageRedBlock[i]; |
dreamselec | 20:4e0f0944f28f | 32 | } |
dreamselec | 20:4e0f0944f28f | 33 | |
dreamselec | 20:4e0f0944f28f | 34 | _HazBlock.minColour = Colour(_minRedBlock); |
dreamselec | 20:4e0f0944f28f | 35 | _HazBlock.maxColour = Colour(_maxRedBlock); |
dreamselec | 20:4e0f0944f28f | 36 | _HazBlock.averageColour = Colour(_averageRedBlock); |
dreamselec | 20:4e0f0944f28f | 37 | _HazBlock.size = Block::Small; |
dreamselec | 20:4e0f0944f28f | 38 | |
dreamselec | 20:4e0f0944f28f | 39 | // defaultHazBlock.minColour = Colour(_minRedBlock); |
dreamselec | 20:4e0f0944f28f | 40 | // defaultHazBlock.maxColour = Colour(_maxRedBlock); |
dreamselec | 20:4e0f0944f28f | 41 | // defaultHazBlock.averageColour = Colour(_averageRedBlock); |
dreamselec | 20:4e0f0944f28f | 42 | |
dreamselec | 20:4e0f0944f28f | 43 | // Block _HazBlock = Block(defaultHazBlock); |
dreamselec | 20:4e0f0944f28f | 44 | } |
dreamselec | 20:4e0f0944f28f | 45 | |
dreamselec | 10:16ba52f8e025 | 46 | int readSwitches() |
dreamselec | 10:16ba52f8e025 | 47 | { |
dreamselec | 20:4e0f0944f28f | 48 | if(i2cport->read_bit(8)) { |
dreamselec | 20:4e0f0944f28f | 49 | while (i2cport->read_bit(8)) { } |
dreamselec | 20:4e0f0944f28f | 50 | return 1; |
dreamselec | 20:4e0f0944f28f | 51 | } else if (i2cport->read_bit(9)) { |
dreamselec | 20:4e0f0944f28f | 52 | while (i2cport->read_bit(9)) { } |
dreamselec | 20:4e0f0944f28f | 53 | return 2; |
dreamselec | 20:4e0f0944f28f | 54 | } else if (i2cport->read_bit(10)) { |
dreamselec | 20:4e0f0944f28f | 55 | while (i2cport->read_bit(10)) { } |
dreamselec | 20:4e0f0944f28f | 56 | return 3; |
dreamselec | 20:4e0f0944f28f | 57 | } else if (i2cport->read_bit(11)) { |
dreamselec | 20:4e0f0944f28f | 58 | while (i2cport->read_bit(11)) { } |
dreamselec | 20:4e0f0944f28f | 59 | return 4; |
dreamselec | 20:4e0f0944f28f | 60 | } else { |
dreamselec | 20:4e0f0944f28f | 61 | return 0; |
dreamselec | 20:4e0f0944f28f | 62 | } |
dreamselec | 10:16ba52f8e025 | 63 | |
dreamselec |
8:e1da2ae62885 | 64 | } |
dreamselec |
8:e1da2ae62885 | 65 | |
dreamselec | 10:16ba52f8e025 | 66 | void connectToPC(CommandTypeRaw typeRaw) |
dreamselec | 10:16ba52f8e025 | 67 | { |
dreamselec | 20:4e0f0944f28f | 68 | connectedToPC = true; |
dreamselec | 24:02c61793f90b | 69 | pc.printf("INFO:PC connected to MBED.\n"); |
dreamselec | 10:16ba52f8e025 | 70 | } |
dreamselec | 10:16ba52f8e025 | 71 | |
dreamselec | 10:16ba52f8e025 | 72 | void disconnectToPC(CommandTypeRaw typeRaw) |
dreamselec | 10:16ba52f8e025 | 73 | { |
dreamselec | 24:02c61793f90b | 74 | pc.printf("INFO:PC disconnected to MBED.\n"); |
dreamselec | 20:4e0f0944f28f | 75 | connectedToPC = false; |
dreamselec | 20:4e0f0944f28f | 76 | currentMode = None; |
dreamselec |
8:e1da2ae62885 | 77 | } |
dreamselec |
8:e1da2ae62885 | 78 | |
dreamselec | 10:16ba52f8e025 | 79 | void hazBlock(CommandTypeRaw typeRaw) |
dreamselec | 10:16ba52f8e025 | 80 | { |
dreamselec | 20:4e0f0944f28f | 81 | if (typeRaw == Set) { |
dreamselec | 20:4e0f0944f28f | 82 | pc.printf("INFO:Setting new haz block.\n"); |
dreamselec | 20:4e0f0944f28f | 83 | int lowerBeam = 0; |
dreamselec | 20:4e0f0944f28f | 84 | int higherBeam = 0; |
dreamselec | 20:4e0f0944f28f | 85 | char readyChar = '\0'; |
dreamselec | 20:4e0f0944f28f | 86 | |
dreamselec | 20:4e0f0944f28f | 87 | lcd->cls(); |
dreamselec | 20:4e0f0944f28f | 88 | lcd->locate(0,0); |
dreamselec | 20:4e0f0944f28f | 89 | lcd->printf("New haz block"); |
dreamselec |
18:44a1c1a30166 | 90 | |
dreamselec | 20:4e0f0944f28f | 91 | do { |
dreamselec | 20:4e0f0944f28f | 92 | higherBeam = fpga->getBeamValue(Top); |
dreamselec | 20:4e0f0944f28f | 93 | if (readSwitches() == 4) { |
dreamselec | 20:4e0f0944f28f | 94 | if (displayAbortDialog()) { |
dreamselec | 20:4e0f0944f28f | 95 | pc.printf("INFO: Operation aborted form MBED.\n"); |
dreamselec | 20:4e0f0944f28f | 96 | displayPCStatus(); |
dreamselec | 20:4e0f0944f28f | 97 | return; |
dreamselec | 20:4e0f0944f28f | 98 | } else { |
dreamselec | 20:4e0f0944f28f | 99 | lcd->cls(); |
dreamselec | 20:4e0f0944f28f | 100 | lcd->locate(0,0); |
dreamselec | 20:4e0f0944f28f | 101 | lcd->printf("New haz block"); |
dreamselec | 20:4e0f0944f28f | 102 | } |
dreamselec |
18:44a1c1a30166 | 103 | |
dreamselec | 20:4e0f0944f28f | 104 | } |
dreamselec | 20:4e0f0944f28f | 105 | } while (higherBeam != 1); |
dreamselec | 10:16ba52f8e025 | 106 | |
dreamselec | 20:4e0f0944f28f | 107 | int colourValue[4]; |
dreamselec | 20:4e0f0944f28f | 108 | rgbSensor.getAllColors(colourValue); |
dreamselec | 20:4e0f0944f28f | 109 | |
dreamselec | 20:4e0f0944f28f | 110 | do { |
dreamselec | 20:4e0f0944f28f | 111 | lowerBeam = fpga->checkForBlock(); |
dreamselec | 20:4e0f0944f28f | 112 | } while (lowerBeam != 1); |
dreamselec | 20:4e0f0944f28f | 113 | higherBeam = fpga->checkForSize(); |
dreamselec |
18:44a1c1a30166 | 114 | |
dreamselec | 20:4e0f0944f28f | 115 | _HazBlock = Block(); |
dreamselec | 20:4e0f0944f28f | 116 | if (higherBeam == 1) |
dreamselec | 20:4e0f0944f28f | 117 | _HazBlock.size = Block::Big; |
dreamselec | 20:4e0f0944f28f | 118 | else if (higherBeam == 0) |
dreamselec | 20:4e0f0944f28f | 119 | _HazBlock.size = Block::Small; |
dreamselec | 20:4e0f0944f28f | 120 | for (int i = 0; i < 3; i++) { |
dreamselec | 20:4e0f0944f28f | 121 | _HazBlock.minColour.components[i] = colourValue[i]/colourValue[3] - ColourSensorError; |
dreamselec | 20:4e0f0944f28f | 122 | _HazBlock.maxColour.components[i] = colourValue[i]/colourValue[3] + ColourSensorError; |
dreamselec | 20:4e0f0944f28f | 123 | } |
dreamselec | 20:4e0f0944f28f | 124 | fpga->moveSortingServo(Haz); |
dreamselec | 20:4e0f0944f28f | 125 | fpga->moveStoppingServo(Go); |
dreamselec | 20:4e0f0944f28f | 126 | while (fpga->checkForBlock()) {} |
dreamselec | 20:4e0f0944f28f | 127 | fpga->moveStoppingServo(Stop); |
dreamselec | 20:4e0f0944f28f | 128 | fpga->moveSortingServo(NonHaz); |
dreamselec |
18:44a1c1a30166 | 129 | |
dreamselec | 22:993821a4c396 | 130 | pc.printf( "VALUE:HazBlock:\n \t Size:%i\n \tMin Colour:%i,%i,%i,%i\n \t Max Colour:%i,%i,%i,%i:VALUE", _HazBlock.size, colourValue[0], colourValue[1], colourValue[2], colourValue[3], _HazBlock.minColour.components[Colour::Red], _HazBlock.minColour.components[Colour::Blue], _HazBlock.minColour.components[Colour::Green], _HazBlock.minColour.components[Colour::Alpha], _HazBlock.maxColour.components[Colour::Red], _HazBlock.maxColour.components[Colour::Blue], _HazBlock.maxColour.components[Colour::Green], _HazBlock.maxColour.components[Colour::Alpha]); |
dreamselec | 20:4e0f0944f28f | 131 | } else if (typeRaw == Query) { |
dreamselec | 20:4e0f0944f28f | 132 | pc.printf( "VALUE:HazBlock:\n \t Size:%i\n \t Min Colour:%i,%i,%i,%i\n \t Max Colour:%i,%i,%i,%i:VALUE", _HazBlock.size, _HazBlock.minColour.components[Colour::Red], _HazBlock.minColour.components[Colour::Blue], _HazBlock.minColour.components[Colour::Green], _HazBlock.minColour.components[Colour::Alpha], _HazBlock.maxColour.components[Colour::Red], _HazBlock.maxColour.components[Colour::Blue], _HazBlock.maxColour.components[Colour::Green], _HazBlock.maxColour.components[Colour::Alpha]); |
dreamselec | 20:4e0f0944f28f | 133 | } |
dreamselec |
8:e1da2ae62885 | 134 | } |
dreamselec |
8:e1da2ae62885 | 135 | |
dreamselec | 10:16ba52f8e025 | 136 | void getCurrentBlock(CommandTypeRaw typeRaw) |
dreamselec | 10:16ba52f8e025 | 137 | { |
dreamselec | 20:4e0f0944f28f | 138 | pc.printf("DEBUG: Getting current block readings\n"); |
dreamselec |
8:e1da2ae62885 | 139 | } |
dreamselec |
8:e1da2ae62885 | 140 | |
dreamselec | 15:777390eb5afd | 141 | void setIntegrationTimeTo(float integrationTime) |
dreamselec | 10:16ba52f8e025 | 142 | { |
dreamselec | 20:4e0f0944f28f | 143 | rgbSensor.setIntegrationTime(integrationTime); |
dreamselec | 20:4e0f0944f28f | 144 | pc.printf("DEBUG: Setting integration-time to %i.\n", integrationTime); |
dreamselec |
8:e1da2ae62885 | 145 | } |
dreamselec |
8:e1da2ae62885 | 146 | |
dreamselec | 10:16ba52f8e025 | 147 | void previewOnPC(bool on) |
dreamselec | 10:16ba52f8e025 | 148 | { |
dreamselec | 20:4e0f0944f28f | 149 | pc.printf("setting preview on pc to %i.\n", on); |
dreamselec |
8:e1da2ae62885 | 150 | } |
dreamselec |
8:e1da2ae62885 | 151 | |
dreamselec | 20:4e0f0944f28f | 152 | void readColourSensor() |
dreamselec | 20:4e0f0944f28f | 153 | { |
dreamselec | 20:4e0f0944f28f | 154 | int colourValue[4]; |
dreamselec | 20:4e0f0944f28f | 155 | rgbSensor.getAllColors(colourValue); |
dreamselec | 20:4e0f0944f28f | 156 | pc.printf( "VALEU:Colour Reading:%i,%i,%i,%i\n:VALUE", colourValue[0], colourValue[1], colourValue[2], colourValue[3]); |
dreamselec |
18:44a1c1a30166 | 157 | |
dreamselec |
8:e1da2ae62885 | 158 | } |
dreamselec |
8:e1da2ae62885 | 159 | |
dreamselec | 17:af373246bf80 | 160 | void testServos(Controls state) |
dreamselec | 10:16ba52f8e025 | 161 | { |
dreamselec | 20:4e0f0944f28f | 162 | if (state == Start) { |
dreamselec | 20:4e0f0944f28f | 163 | pc.printf("INFO: Running servo test.\n"); |
dreamselec | 20:4e0f0944f28f | 164 | runServoTest = true; |
dreamselec | 20:4e0f0944f28f | 165 | } else if (state == Pause) { |
dreamselec | 20:4e0f0944f28f | 166 | pc.printf("INFO: Finished running servo test.\n"); |
dreamselec | 20:4e0f0944f28f | 167 | runServoTest = false; |
dreamselec | 20:4e0f0944f28f | 168 | } |
dreamselec |
8:e1da2ae62885 | 169 | } |
dreamselec |
8:e1da2ae62885 | 170 | |
dreamselec | 10:16ba52f8e025 | 171 | void resetServos() |
dreamselec | 10:16ba52f8e025 | 172 | { |
dreamselec | 20:4e0f0944f28f | 173 | pc.printf("resetting servos.\n"); |
dreamselec |
8:e1da2ae62885 | 174 | } |
dreamselec |
8:e1da2ae62885 | 175 | |
dreamselec | 10:16ba52f8e025 | 176 | void getPortInfo() |
dreamselec | 10:16ba52f8e025 | 177 | { |
dreamselec | 20:4e0f0944f28f | 178 | pc.printf("getting port info.\n"); |
dreamselec |
8:e1da2ae62885 | 179 | } |
dreamselec |
8:e1da2ae62885 | 180 | |
dreamselec | 17:af373246bf80 | 181 | void setPortBaudRate(int baudRate) |
dreamselec | 10:16ba52f8e025 | 182 | { |
dreamselec | 20:4e0f0944f28f | 183 | pc.baud(baudRate); |
dreamselec | 20:4e0f0944f28f | 184 | wait(0.1); |
dreamselec | 20:4e0f0944f28f | 185 | pc.printf("DEBUG: Setting port Baud Rate to: %i.\n", baudRate); |
dreamselec | 10:16ba52f8e025 | 186 | } |
dreamselec | 10:16ba52f8e025 | 187 | |
dreamselec | 20:4e0f0944f28f | 188 | void setPortParity(int parity) |
dreamselec | 20:4e0f0944f28f | 189 | { |
dreamselec | 20:4e0f0944f28f | 190 | SerialBase::Parity _parity = static_cast<SerialBase::Parity>(parity); |
dreamselec | 20:4e0f0944f28f | 191 | pc.format(8, _parity, 1); |
dreamselec | 20:4e0f0944f28f | 192 | wait(0.1); |
dreamselec | 20:4e0f0944f28f | 193 | pc.printf("DEBUG: Setting port parity to: %i.\n", parity); |
dreamselec |
14:cf2f255b5560 | 194 | } |
dreamselec |
23:db91aaa43a9e | 195 | |
dreamselec |
23:db91aaa43a9e | 196 | void testBreakBeams(Controls state){ |
dreamselec |
23:db91aaa43a9e | 197 | if (state == Start){ |
dreamselec |
23:db91aaa43a9e | 198 | pc.printf("INFO: Running break beam test.\n"); |
dreamselec |
23:db91aaa43a9e | 199 | runBreakBeamTest = true; |
dreamselec |
23:db91aaa43a9e | 200 | }else if (state == Pause){ |
dreamselec |
23:db91aaa43a9e | 201 | pc.printf("INFO: Exiting break beam test.\n"); |
dreamselec |
23:db91aaa43a9e | 202 | runBreakBeamTest = false; |
dreamselec |
23:db91aaa43a9e | 203 | } |
dreamselec |
23:db91aaa43a9e | 204 | } |