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

Revision:
32:9a4046224b11
Parent:
30:c0bc92d009fe
diff -r 16e056b9f9e0 -r 9a4046224b11 globals.cpp
--- a/globals.cpp	Thu Dec 03 15:50:14 2015 +0000
+++ b/globals.cpp	Thu Dec 17 17:23:24 2015 +0100
@@ -1,3 +1,7 @@
+//
+// globals.cpp
+// Created by Chandan Siyag on 13/11/2015.
+
 #include "globals.h"
 #include "Block.h"
 #include "fpga.h"
@@ -6,18 +10,20 @@
 #include "WattBob_TextLCD.h"
 
 int kDefaultBaudRate = 19200;
-//TODO: Not let it be constant.
+
 int ColourSensorError = 0.5;
-//SerialBase gParity = SerialBase::None;
+//SerialBase gParity = SerialBase::None, 2 = Even, 3 = Odd
 int gStopBits = 1;
 float gIntegrationTime = 2.5;
 int gToggleServoNumber = 0;
 float currentMinError[3] = {0,0,0};
 float currentMaxError[3] = {0,0,0};
 
+// Intialise gloabl variables
 Block defaultHazBlock = Block(Block::Small, Block::Red);
 Block _HazBlock = Block(defaultHazBlock);
 
+// Default gloabl variables values
 bool connectedToPC = false;
 bool runServoTest = false;
 bool runBreakBeamTest = false;
@@ -32,6 +38,7 @@
 PCModes currentMode = None;
 Controls currentState = Pause;
 
+/// Sets up the default hazardous block
 void DefaultHazBlock(){
 	
 	for (int i = 0; i < 3; i++){ 
@@ -48,24 +55,20 @@
 		_averageRedBlock.components[i] = kAverageRedBlock[i];
 	}
 	
-//	_HazBlock.minColour = Colour(_minRedBlock);
-//	_HazBlock.maxColour = Colour(_maxRedBlock);
-//	_HazBlock.averageColour = Colour(_averageRedBlock);
-//	_HazBlock.size = Block::Small;
-	
 	defaultHazBlock.minColour = Colour(_minRedBlock);
 	defaultHazBlock.maxColour = Colour(_maxRedBlock);
 	defaultHazBlock.averageColour = Colour(_averageRedBlock);
 	defaultHazBlock.size = Block::Small;
 	
 	_HazBlock = Block(defaultHazBlock);
-//	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]);
 }
 
+/// Sends the supplied colours description to the PC
 void printColourDescription(Colour colour){
 	pc.printf("Red: %.3f, Green: %.3f, Blue: %.3f, Clear: %.3f\n", colour.components[0], colour.components[1], colour.components[2], colour.components[3]);
 }
 
+/// Sends the block infomation to the PC.
 void printBlockDescription(Block block){
 	pc.printf("VALUE:Size: %i\n", block.size);
 	printColourDescription(block.minColour);
@@ -75,6 +78,8 @@
 	pc.printf(":VALUE");
 }
 
+/// Reads the switches on the MBED pressed.
+/// Finishes on the Key-Up
 int readSwitches()
 {
     if(i2cport->read_bit(8)) {
@@ -95,12 +100,14 @@
 
 }
 
+/// Sets connecteToPC property to true when PC asks MBED to connect to itself.
 void connectToPC(CommandTypeRaw typeRaw)
 {
     connectedToPC = true;
     pc.printf("INFO:PC connected to MBED.\n");
 }
 
+/// Called when PC tells MBED to disconnect.
 void disconnectToPC(CommandTypeRaw typeRaw)
 {
     pc.printf("INFO:PC disconnected to MBED.\n");
@@ -108,6 +115,8 @@
     currentMode = None;
 }
 
+/// Sends all the information about the hazardous block to the PC.
+/// Or sets the new hazardous block depending on the command type i.e. Set or Query
 void hazBlock(CommandTypeRaw typeRaw)
 {
     if (typeRaw == Set) {
@@ -119,11 +128,14 @@
     }
 }
 
+/// Placeholder function, not used anymore since it's done with hazBlock(!)
+/// Is supposed to send information about next block.
 void getCurrentBlock(CommandTypeRaw typeRaw)
 {
     pc.printf("DEBUG: Getting current block readings\n");
 }
 
+/// Sets colour sensor Integration time set by the PC.
 void setIntegrationTimeTo(float integrationTime)
 {
     gIntegrationTime = integrationTime;
@@ -131,11 +143,14 @@
     pc.printf("DEBUG: Setting integration-time to %.2f.\n", gIntegrationTime);
 }
 
+/// Placeholder function. Done with colour sensor test mode.
+/// Is supposed to send current colour sensor information at regular intervals.
 void previewOnPC(bool on)
 {
     pc.printf("setting preview on pc to %i.\n", on);
 }
 
+/// Start/stop colour sensor test mode.
 void testColourSensor(Controls state){
 	if (state == Start){
 		pc.printf("INFO: Running colour test.\n");
@@ -146,14 +161,16 @@
 	}
 }
 
+/// Sends current colour sensor values.
 void readColourSensor()
 {
     int colourValue[4];
     rgbSensor.getAllColors(colourValue);
-    pc.printf(	"VALEU:Colour Reading:%i,%i,%i,%i\n:VALUE", colourValue[0], colourValue[1], colourValue[2], colourValue[3]);
+    pc.printf(	"VALUE:Colour Reading:%i,%i,%i,%i\n:VALUE", colourValue[0], colourValue[1], colourValue[2], colourValue[3]);
 
 }
 
+/// Enter/exit servo test mode.
 void testServos(Controls state)
 {
     if (state == Start) {
@@ -165,16 +182,19 @@
     }
 }
 
+/// Placeholder function. Servos now are always set to the default position i.e Non Hazardous (upright).
 void resetServos()
 {
     pc.printf("resetting servos.\n");
 }
 
+/// Placeholder functions. The port info is sent every time port is opened, and updated every time anything changes.
 void getPortInfo()
 {
     pc.printf("getting port info.\n");
 }
 
+/// Set the port baud rate to what PC told it to.
 void setPortBaudRate(int baudRate)
 {
     pc.baud(baudRate);
@@ -182,6 +202,7 @@
     pc.printf("DEBUG: Setting port Baud Rate to: %i.\n", baudRate);
 }
 
+/// Sets port parity according to the PC.
 void setPortParity(int parity)
 {
     SerialBase::Parity _parity = static_cast<SerialBase::Parity>(parity);
@@ -190,6 +211,7 @@
     pc.printf("DEBUG: Setting port parity to: %i.\n", parity);
 }
 
+/// Start/pause break beam test mode.
 void testBreakBeams(Controls state){
 	if (state == Start){
 		pc.printf("INFO: Running break beam test.\n");