![](/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@30:c0bc92d009fe, 2015-12-01 (annotated)
- Committer:
- dreamselec
- Date:
- Tue Dec 01 23:50:29 2015 +0000
- Revision:
- 30:c0bc92d009fe
- Parent:
- 29:9c0339e3c593
- Child:
- 32:9a4046224b11
All functions appear to be ... functioning. Need to test set new haz block mode with hardware.
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 | 28:7e4d29977d72 | 13 | float gIntegrationTime = 2.5; |
dreamselec | 25:792540d69c49 | 14 | int gToggleServoNumber = 0; |
dreamselec | 26:bbcc25418ffa | 15 | float currentMinError[3] = {0,0,0}; |
dreamselec | 26:bbcc25418ffa | 16 | float currentMaxError[3] = {0,0,0}; |
dreamselec | 20:4e0f0944f28f | 17 | |
dreamselec |
29:9c0339e3c593 | 18 | Block defaultHazBlock = Block(Block::Small, Block::Red); |
dreamselec |
29:9c0339e3c593 | 19 | Block _HazBlock = Block(defaultHazBlock); |
dreamselec |
8:e1da2ae62885 | 20 | |
dreamselec |
8:e1da2ae62885 | 21 | bool connectedToPC = false; |
dreamselec | 10:16ba52f8e025 | 22 | bool runServoTest = false; |
dreamselec |
23:db91aaa43a9e | 23 | bool runBreakBeamTest = false; |
dreamselec | 25:792540d69c49 | 24 | bool runColourSensorTest = false; |
dreamselec | 25:792540d69c49 | 25 | bool getColourSensorValue = false; |
dreamselec | 25:792540d69c49 | 26 | bool getBlockColourValue = false; |
dreamselec |
29:9c0339e3c593 | 27 | bool setNewHazBlock = false; |
dreamselec | 28:7e4d29977d72 | 28 | bool pcModeChanged = false; |
dreamselec | 28:7e4d29977d72 | 29 | int errorMultiplier = 1; |
dreamselec | 27:2cb1bdb7ae3d | 30 | int hazReadingCount = 1; |
dreamselec | 26:bbcc25418ffa | 31 | |
dreamselec | 17:af373246bf80 | 32 | PCModes currentMode = None; |
dreamselec |
18:44a1c1a30166 | 33 | Controls currentState = Pause; |
dreamselec |
8:e1da2ae62885 | 34 | |
dreamselec | 20:4e0f0944f28f | 35 | void DefaultHazBlock(){ |
dreamselec | 26:bbcc25418ffa | 36 | |
dreamselec | 26:bbcc25418ffa | 37 | for (int i = 0; i < 3; i++){ |
dreamselec |
29:9c0339e3c593 | 38 | currentMaxError[i] = kMaxRedError[i]; |
dreamselec |
29:9c0339e3c593 | 39 | currentMinError[i] = kMinRedError[i]; |
dreamselec | 26:bbcc25418ffa | 40 | } |
dreamselec | 20:4e0f0944f28f | 41 | Colour _minRedBlock = Colour(); |
dreamselec | 20:4e0f0944f28f | 42 | Colour _maxRedBlock = Colour(); |
dreamselec | 20:4e0f0944f28f | 43 | Colour _averageRedBlock = Colour(); |
dreamselec | 20:4e0f0944f28f | 44 | |
dreamselec | 20:4e0f0944f28f | 45 | for (int i = 0; i < 4; i++){ |
dreamselec | 20:4e0f0944f28f | 46 | _minRedBlock.components[i] = kMinRedBlock[i]; |
dreamselec | 20:4e0f0944f28f | 47 | _maxRedBlock.components[i] = kMaxRedBlock[i]; |
dreamselec | 20:4e0f0944f28f | 48 | _averageRedBlock.components[i] = kAverageRedBlock[i]; |
dreamselec | 20:4e0f0944f28f | 49 | } |
dreamselec | 20:4e0f0944f28f | 50 | |
dreamselec | 26:bbcc25418ffa | 51 | // _HazBlock.minColour = Colour(_minRedBlock); |
dreamselec | 26:bbcc25418ffa | 52 | // _HazBlock.maxColour = Colour(_maxRedBlock); |
dreamselec | 26:bbcc25418ffa | 53 | // _HazBlock.averageColour = Colour(_averageRedBlock); |
dreamselec | 26:bbcc25418ffa | 54 | // _HazBlock.size = Block::Small; |
dreamselec | 26:bbcc25418ffa | 55 | |
dreamselec | 26:bbcc25418ffa | 56 | defaultHazBlock.minColour = Colour(_minRedBlock); |
dreamselec | 26:bbcc25418ffa | 57 | defaultHazBlock.maxColour = Colour(_maxRedBlock); |
dreamselec | 26:bbcc25418ffa | 58 | defaultHazBlock.averageColour = Colour(_averageRedBlock); |
dreamselec | 26:bbcc25418ffa | 59 | defaultHazBlock.size = Block::Small; |
dreamselec | 20:4e0f0944f28f | 60 | |
dreamselec | 26:bbcc25418ffa | 61 | _HazBlock = Block(defaultHazBlock); |
dreamselec | 27:2cb1bdb7ae3d | 62 | // pc.printf( "VALUE:HazBlock:\n \t Size:%i\n \t Min Colour:%f,%f,%f,%f\n \t Max Colour:%f,%f,%f,%f: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 | 26:bbcc25418ffa | 63 | } |
dreamselec | 26:bbcc25418ffa | 64 | |
dreamselec | 26:bbcc25418ffa | 65 | void printColourDescription(Colour colour){ |
dreamselec | 26:bbcc25418ffa | 66 | pc.printf("Red: %.3f, Green: %.3f, Blue: %.3f, Clear: %.3f\n", colour.components[0], colour.components[1], colour.components[2], colour.components[3]); |
dreamselec | 26:bbcc25418ffa | 67 | } |
dreamselec | 26:bbcc25418ffa | 68 | |
dreamselec | 26:bbcc25418ffa | 69 | void printBlockDescription(Block block){ |
dreamselec | 26:bbcc25418ffa | 70 | pc.printf("VALUE:Size: %i\n", block.size); |
dreamselec | 26:bbcc25418ffa | 71 | printColourDescription(block.minColour); |
dreamselec | 26:bbcc25418ffa | 72 | printColourDescription(block.averageColour); |
dreamselec | 26:bbcc25418ffa | 73 | printColourDescription(block.maxColour); |
dreamselec | 26:bbcc25418ffa | 74 | //TODO: print errors |
dreamselec | 26:bbcc25418ffa | 75 | pc.printf(":VALUE"); |
dreamselec | 20:4e0f0944f28f | 76 | } |
dreamselec | 20:4e0f0944f28f | 77 | |
dreamselec | 10:16ba52f8e025 | 78 | int readSwitches() |
dreamselec | 10:16ba52f8e025 | 79 | { |
dreamselec | 20:4e0f0944f28f | 80 | if(i2cport->read_bit(8)) { |
dreamselec | 20:4e0f0944f28f | 81 | while (i2cport->read_bit(8)) { } |
dreamselec | 20:4e0f0944f28f | 82 | return 1; |
dreamselec | 20:4e0f0944f28f | 83 | } else if (i2cport->read_bit(9)) { |
dreamselec | 20:4e0f0944f28f | 84 | while (i2cport->read_bit(9)) { } |
dreamselec | 20:4e0f0944f28f | 85 | return 2; |
dreamselec | 20:4e0f0944f28f | 86 | } else if (i2cport->read_bit(10)) { |
dreamselec | 20:4e0f0944f28f | 87 | while (i2cport->read_bit(10)) { } |
dreamselec | 20:4e0f0944f28f | 88 | return 3; |
dreamselec | 20:4e0f0944f28f | 89 | } else if (i2cport->read_bit(11)) { |
dreamselec | 20:4e0f0944f28f | 90 | while (i2cport->read_bit(11)) { } |
dreamselec | 20:4e0f0944f28f | 91 | return 4; |
dreamselec | 20:4e0f0944f28f | 92 | } else { |
dreamselec | 20:4e0f0944f28f | 93 | return 0; |
dreamselec | 20:4e0f0944f28f | 94 | } |
dreamselec | 10:16ba52f8e025 | 95 | |
dreamselec |
8:e1da2ae62885 | 96 | } |
dreamselec |
8:e1da2ae62885 | 97 | |
dreamselec | 10:16ba52f8e025 | 98 | void connectToPC(CommandTypeRaw typeRaw) |
dreamselec | 10:16ba52f8e025 | 99 | { |
dreamselec | 20:4e0f0944f28f | 100 | connectedToPC = true; |
dreamselec | 24:02c61793f90b | 101 | pc.printf("INFO:PC connected to MBED.\n"); |
dreamselec | 10:16ba52f8e025 | 102 | } |
dreamselec | 10:16ba52f8e025 | 103 | |
dreamselec | 10:16ba52f8e025 | 104 | void disconnectToPC(CommandTypeRaw typeRaw) |
dreamselec | 10:16ba52f8e025 | 105 | { |
dreamselec | 24:02c61793f90b | 106 | pc.printf("INFO:PC disconnected to MBED.\n"); |
dreamselec | 20:4e0f0944f28f | 107 | connectedToPC = false; |
dreamselec | 20:4e0f0944f28f | 108 | currentMode = None; |
dreamselec |
8:e1da2ae62885 | 109 | } |
dreamselec |
8:e1da2ae62885 | 110 | |
dreamselec | 10:16ba52f8e025 | 111 | void hazBlock(CommandTypeRaw typeRaw) |
dreamselec | 10:16ba52f8e025 | 112 | { |
dreamselec | 20:4e0f0944f28f | 113 | if (typeRaw == Set) { |
dreamselec | 30:c0bc92d009fe | 114 | setNewHazBlock = true; |
dreamselec | 30:c0bc92d009fe | 115 | pc.printf("INFO: Setting new haz block.\n"); |
dreamselec | 30:c0bc92d009fe | 116 | } else if (typeRaw == Query) { |
dreamselec | 30:c0bc92d009fe | 117 | pc.printf("VALUE:Hazardous Block:\n \tSize:%i \n \tMin Error:%.3f, %.3f, %.3f\n \t Max Error:%.3f, %.3f, %.3f\n:VALUE", _HazBlock.size, kMinError[_HazBlock.colour][1], kMinError[_HazBlock.colour][1], kMinError[_HazBlock.colour][2], kMaxError[_HazBlock.colour][0], kMaxError[_HazBlock.colour][1], kMaxError[_HazBlock.colour][2]); |
dreamselec | 30:c0bc92d009fe | 118 | pc.printf("VALUE:\tAverage Colour:%.3f, %.3f, %.3f, %.3f\n:VALUE", kAverageValues[_HazBlock.colour][0], kAverageValues[_HazBlock.colour][1], kAverageValues[_HazBlock.colour][2], kAverageValues[_HazBlock.colour][3]); |
dreamselec | 20:4e0f0944f28f | 119 | } |
dreamselec |
8:e1da2ae62885 | 120 | } |
dreamselec |
8:e1da2ae62885 | 121 | |
dreamselec | 10:16ba52f8e025 | 122 | void getCurrentBlock(CommandTypeRaw typeRaw) |
dreamselec | 10:16ba52f8e025 | 123 | { |
dreamselec | 20:4e0f0944f28f | 124 | pc.printf("DEBUG: Getting current block readings\n"); |
dreamselec |
8:e1da2ae62885 | 125 | } |
dreamselec |
8:e1da2ae62885 | 126 | |
dreamselec | 15:777390eb5afd | 127 | void setIntegrationTimeTo(float integrationTime) |
dreamselec | 10:16ba52f8e025 | 128 | { |
dreamselec | 25:792540d69c49 | 129 | gIntegrationTime = integrationTime; |
dreamselec | 25:792540d69c49 | 130 | rgbSensor.setIntegrationTime(gIntegrationTime); |
dreamselec | 30:c0bc92d009fe | 131 | pc.printf("DEBUG: Setting integration-time to %.2f.\n", gIntegrationTime); |
dreamselec |
8:e1da2ae62885 | 132 | } |
dreamselec |
8:e1da2ae62885 | 133 | |
dreamselec | 10:16ba52f8e025 | 134 | void previewOnPC(bool on) |
dreamselec | 10:16ba52f8e025 | 135 | { |
dreamselec | 20:4e0f0944f28f | 136 | pc.printf("setting preview on pc to %i.\n", on); |
dreamselec |
8:e1da2ae62885 | 137 | } |
dreamselec |
8:e1da2ae62885 | 138 | |
dreamselec | 25:792540d69c49 | 139 | void testColourSensor(Controls state){ |
dreamselec | 25:792540d69c49 | 140 | if (state == Start){ |
dreamselec | 25:792540d69c49 | 141 | pc.printf("INFO: Running colour test.\n"); |
dreamselec | 25:792540d69c49 | 142 | runColourSensorTest = true; |
dreamselec | 25:792540d69c49 | 143 | } else if (state == Pause){ |
dreamselec | 25:792540d69c49 | 144 | pc.printf("INFO: Finished colour test.\n"); |
dreamselec | 25:792540d69c49 | 145 | runColourSensorTest = false; |
dreamselec | 25:792540d69c49 | 146 | } |
dreamselec | 25:792540d69c49 | 147 | } |
dreamselec | 25:792540d69c49 | 148 | |
dreamselec | 20:4e0f0944f28f | 149 | void readColourSensor() |
dreamselec | 20:4e0f0944f28f | 150 | { |
dreamselec | 20:4e0f0944f28f | 151 | int colourValue[4]; |
dreamselec | 20:4e0f0944f28f | 152 | rgbSensor.getAllColors(colourValue); |
dreamselec | 20:4e0f0944f28f | 153 | pc.printf( "VALEU:Colour Reading:%i,%i,%i,%i\n:VALUE", colourValue[0], colourValue[1], colourValue[2], colourValue[3]); |
dreamselec |
18:44a1c1a30166 | 154 | |
dreamselec |
8:e1da2ae62885 | 155 | } |
dreamselec |
8:e1da2ae62885 | 156 | |
dreamselec | 17:af373246bf80 | 157 | void testServos(Controls state) |
dreamselec | 10:16ba52f8e025 | 158 | { |
dreamselec | 20:4e0f0944f28f | 159 | if (state == Start) { |
dreamselec | 20:4e0f0944f28f | 160 | pc.printf("INFO: Running servo test.\n"); |
dreamselec | 20:4e0f0944f28f | 161 | runServoTest = true; |
dreamselec | 20:4e0f0944f28f | 162 | } else if (state == Pause) { |
dreamselec | 20:4e0f0944f28f | 163 | pc.printf("INFO: Finished running servo test.\n"); |
dreamselec | 20:4e0f0944f28f | 164 | runServoTest = false; |
dreamselec | 20:4e0f0944f28f | 165 | } |
dreamselec |
8:e1da2ae62885 | 166 | } |
dreamselec |
8:e1da2ae62885 | 167 | |
dreamselec | 10:16ba52f8e025 | 168 | void resetServos() |
dreamselec | 10:16ba52f8e025 | 169 | { |
dreamselec | 20:4e0f0944f28f | 170 | pc.printf("resetting servos.\n"); |
dreamselec |
8:e1da2ae62885 | 171 | } |
dreamselec |
8:e1da2ae62885 | 172 | |
dreamselec | 10:16ba52f8e025 | 173 | void getPortInfo() |
dreamselec | 10:16ba52f8e025 | 174 | { |
dreamselec | 20:4e0f0944f28f | 175 | pc.printf("getting port info.\n"); |
dreamselec |
8:e1da2ae62885 | 176 | } |
dreamselec |
8:e1da2ae62885 | 177 | |
dreamselec | 17:af373246bf80 | 178 | void setPortBaudRate(int baudRate) |
dreamselec | 10:16ba52f8e025 | 179 | { |
dreamselec | 20:4e0f0944f28f | 180 | pc.baud(baudRate); |
dreamselec | 20:4e0f0944f28f | 181 | wait(0.1); |
dreamselec | 20:4e0f0944f28f | 182 | pc.printf("DEBUG: Setting port Baud Rate to: %i.\n", baudRate); |
dreamselec | 10:16ba52f8e025 | 183 | } |
dreamselec | 10:16ba52f8e025 | 184 | |
dreamselec | 20:4e0f0944f28f | 185 | void setPortParity(int parity) |
dreamselec | 20:4e0f0944f28f | 186 | { |
dreamselec | 20:4e0f0944f28f | 187 | SerialBase::Parity _parity = static_cast<SerialBase::Parity>(parity); |
dreamselec | 20:4e0f0944f28f | 188 | pc.format(8, _parity, 1); |
dreamselec | 20:4e0f0944f28f | 189 | wait(0.1); |
dreamselec | 20:4e0f0944f28f | 190 | pc.printf("DEBUG: Setting port parity to: %i.\n", parity); |
dreamselec |
14:cf2f255b5560 | 191 | } |
dreamselec |
23:db91aaa43a9e | 192 | |
dreamselec |
23:db91aaa43a9e | 193 | void testBreakBeams(Controls state){ |
dreamselec |
23:db91aaa43a9e | 194 | if (state == Start){ |
dreamselec |
23:db91aaa43a9e | 195 | pc.printf("INFO: Running break beam test.\n"); |
dreamselec |
23:db91aaa43a9e | 196 | runBreakBeamTest = true; |
dreamselec |
23:db91aaa43a9e | 197 | }else if (state == Pause){ |
dreamselec |
23:db91aaa43a9e | 198 | pc.printf("INFO: Exiting break beam test.\n"); |
dreamselec |
23:db91aaa43a9e | 199 | runBreakBeamTest = false; |
dreamselec |
23:db91aaa43a9e | 200 | } |
dreamselec |
23:db91aaa43a9e | 201 | } |