diff --git a/MicroMouse-2016/MicroMouse-2016.xcodeproj/project.pbxproj b/MicroMouse-2016/MicroMouse-2016.xcodeproj/project.pbxproj index 6b2eec9..ac7f52c 100644 --- a/MicroMouse-2016/MicroMouse-2016.xcodeproj/project.pbxproj +++ b/MicroMouse-2016/MicroMouse-2016.xcodeproj/project.pbxproj @@ -37,6 +37,7 @@ /* End PBXCopyFilesBuildPhase section */ /* Begin PBXFileReference section */ + E119488C1D052D4B00D5794C /* AssertionHandler.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = AssertionHandler.h; path = ../../micromouse/AssertionHandler.h; sourceTree = ""; }; E1D9B6F61CD7CB2900F1C492 /* MicroMouse-2016 */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = "MicroMouse-2016"; sourceTree = BUILT_PRODUCTS_DIR; }; E1D9B7001CD7CB4800F1C492 /* Main.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = Main.cpp; path = ../../Main.cpp; sourceTree = ""; }; E1D9B7031CD7CB7E00F1C492 /* ButtonFlag.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = ButtonFlag.h; path = ../../micromouse/ButtonFlag.h; sourceTree = ""; }; @@ -113,6 +114,7 @@ isa = PBXGroup; children = ( E1D9B7031CD7CB7E00F1C492 /* ButtonFlag.h */, + E119488C1D052D4B00D5794C /* AssertionHandler.h */, E1D9B7041CD7CB7E00F1C492 /* callibration_temp */, E1D9B7051CD7CB7E00F1C492 /* Controller.cpp */, E1D9B7061CD7CB7E00F1C492 /* Controller.h */, @@ -339,6 +341,7 @@ E1D9B6FF1CD7CB2900F1C492 /* Release */, ); defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; }; /* End XCConfigurationList section */ }; diff --git a/micromouse/AssertionHandler.h b/micromouse/AssertionHandler.h new file mode 100644 index 0000000..13543c1 --- /dev/null +++ b/micromouse/AssertionHandler.h @@ -0,0 +1,55 @@ +// +// AssertionHandler.h +// micromouse-xcode +// +// Created by Erick Sanchez on 4/20/16. +// Copyright © 2016 Erick Sanchez. All rights reserved. +// + +#ifndef AssertionHandler_h +#define AssertionHandler_h + +#include "Logger.h" +#include "Memory.h" +#include + +#ifdef __MK20DX256__ // Teensy Compile + +inline static void logError( int code ) { + //Read and increment to the next error state, as well as validate to loop back to 1 if incremented to 21 + int intNextIndex = Micromouse::Memory::read(Micromouse::ERROR_MEMORY) +1; + intNextIndex = intNextIndex > 20 ? 1 : intNextIndex; + + //Find next state address and save code as well as the most recent error state in ERROR_MEMORY + int intNextStateAddress = Micromouse::ERROR_MEMORY +intNextIndex * 4; + Micromouse::Memory::write( intNextStateAddress, code); + Micromouse::Memory::write( Micromouse::ERROR_MEMORY, intNextIndex); + +} + +inline static void printErrorCodes() { + for (int error = 0; error <= 11; error += 1) { + int address = Micromouse::ERROR_MEMORY; + log(INFO) << Micromouse::Memory::read((error * 4) +address); + + } + +} + +//Prints to concole using Logger(..) << code and metadata of the assertion +#define assertion(condition,code) \ +( \ +(__builtin_expect(!(condition), 0) ? (void)(logError(code)) : (void)0) \ +) + +#else + +//Prints to concole using the definition of assert(e) declared in assert.h +#define assertion(condition,code) \ +( \ +(__builtin_expect(!(condition), 0) ? __assert_rtn(__func__, __FILE__, __LINE__, #condition) : (void)0) \ +) + +#endif + +#endif /* AssertionHandler_h */ diff --git a/micromouse/IRSensor.cpp b/micromouse/IRSensor.cpp index 694e385..e137afa 100644 --- a/micromouse/IRSensor.cpp +++ b/micromouse/IRSensor.cpp @@ -1,7 +1,7 @@ #include "IRSensor.h" #include "Logger.h" #include "RobotIO.h" -#include +#include "AssertionHandler.h" #ifdef __MK20DX256__ // Teensy Compile #include "WProgram.h" @@ -33,12 +33,12 @@ namespace Micromouse { bool IRSensor::calibrate( int calibrationStart, int calibrationInterval ) { - assert(calibrationStart >= MIN_RANGE); - assert(calibrationInterval > 0); + assertion(calibrationStart >= MIN_RANGE, 1); + assertion(calibrationInterval > 0, 2); this->calibrationStart = calibrationStart; this->calibrationInterval = calibrationInterval; calibrationSize = ( MAX_RANGE - calibrationStart ) / calibrationInterval + 1; - assert(calibrationSize <= 17); + assertion(calibrationSize <= 17, 3); delete[] calibrationData; calibrationData = new int[calibrationSize]; diff --git a/micromouse/Maze.cpp b/micromouse/Maze.cpp index adc1603..1a5e7c9 100644 --- a/micromouse/Maze.cpp +++ b/micromouse/Maze.cpp @@ -2,7 +2,7 @@ #include #include #include "Maze.h" -#include +#include "AssertionHandler.h" namespace Micromouse { @@ -137,7 +137,7 @@ namespace Micromouse // adds a new node to the maze at the given position void Maze::addNode( PositionVector pos ) { - assert(isInsideMaze(pos)); + assertion(isInsideMaze(pos), 4); maze[pos.x()][pos.y()] = new Node(pos); } diff --git a/micromouse/Memory.h b/micromouse/Memory.h index e645721..df13c11 100644 --- a/micromouse/Memory.h +++ b/micromouse/Memory.h @@ -16,16 +16,15 @@ namespace Micromouse { 1: calibrationStart 2: calibrationInterval 3-19: CalibrationData[] - */ - const int IR_FRONT_LEFT_MEMORY = 100;//-119 - const int IR_FRONT_RIGHT_MEMORY = 200;//-139 - const int IR_LEFT_MEMORY = 300;//-159 - const int IR_RIGHT_MEMORY = 400;//-179 - + */ + const int IR_FRONT_LEFT_MEMORY = 100;//to 199 + const int IR_FRONT_RIGHT_MEMORY = 200;//to 299 + const int IR_LEFT_MEMORY = 300;//to 399 + const int IR_RIGHT_MEMORY = 400;//to 499 //180-511 unreserved - - //512-2047 map + + const int ERROR_MEMORY = 512; //to 599 carrying 20 error states at a time plus index slot at 512 diff --git a/micromouse/MouseBot.cpp b/micromouse/MouseBot.cpp index a51f5d0..0712584 100644 --- a/micromouse/MouseBot.cpp +++ b/micromouse/MouseBot.cpp @@ -10,7 +10,7 @@ Author GitHub: joshuasrjc #include "MouseBot.h" #include "Logger.h" #include "ButtonFlag.h" - +#include "AssertionHandler.h" namespace Micromouse @@ -466,7 +466,7 @@ namespace Micromouse void MouseBot::setSpeed( int spd ) { speed = spd; - assert(speed > 0 && speed <= MAX_SPEED); + assertion(speed > 0 && speed <= MAX_SPEED, 5); } diff --git a/micromouse/PIDController.cpp b/micromouse/PIDController.cpp index d2232ee..80503b7 100644 --- a/micromouse/PIDController.cpp +++ b/micromouse/PIDController.cpp @@ -10,7 +10,7 @@ Author GitHub: joshuasrjc \*********************************/ #include "PIDController.h" -#include +#include "AssertionHandler.h" #include "Logger.h" #ifdef __MK20DX256__ // Teensy compile @@ -35,7 +35,7 @@ namespace Micromouse float PIDController::getCorrection(float currentError) { - assert(started); + assertion(started, 8); float deltaTime = getDeltaTime(); diff --git a/micromouse/Path.cpp b/micromouse/Path.cpp index 848dea5..7fe2125 100644 --- a/micromouse/Path.cpp +++ b/micromouse/Path.cpp @@ -1,6 +1,5 @@ #include "Path.h" -#include - +#include "AssertionHandler.h" @@ -26,7 +25,7 @@ namespace Micromouse DirectionVector Path::popStep() { - assert( !path.empty() ); + assertion(!path.empty(), 6); DirectionVector step = path.top(); path.pop(); @@ -38,7 +37,7 @@ namespace Micromouse DirectionVector Path::peekStep() { - assert( !path.empty() ); + assertion(!path.empty(), 7); return path.top(); } diff --git a/micromouse/RobotIO.cpp b/micromouse/RobotIO.cpp index 91bda74..4eb739e 100644 --- a/micromouse/RobotIO.cpp +++ b/micromouse/RobotIO.cpp @@ -4,7 +4,7 @@ #include "Logger.h" #include "Timer.h" #include "ButtonFlag.h" - +#include "AssertionHandler.h" #ifdef __MK20DX256__ // Teensy Compile @@ -81,7 +81,7 @@ namespace Micromouse bool RobotIO::isWallinDirection( direction dir ) { // ensure valid data - assert( dir == W || dir == N || dir == E || dir == NW || dir == NE); + assertion(dir == W || dir == N || dir == E || dir == NW || dir == NE, 9); switch( dir ) { @@ -95,7 +95,7 @@ namespace Micromouse { int dist = (int)IRSensors[FRONT_LEFT]->getDistance(); - return (dist < 120 && abs(dist - IRSensors[FRONT_RIGHT]->getDistance()) < 30); + return (dist < 120 && abs(dist - IRSensors[FRONT_RIGHT]->getDistance()) < 30); } default: diff --git a/micromouse/Vector.cpp b/micromouse/Vector.cpp index b57116b..17bd4e9 100644 --- a/micromouse/Vector.cpp +++ b/micromouse/Vector.cpp @@ -1,5 +1,5 @@ #include "Vector.h" -#include +#include "AssertionHandler.h" namespace Micromouse { @@ -201,9 +201,9 @@ namespace Micromouse void DirectionVector::validateSelf() { - assert( _dir != NONE ); - assert( _mag >= 0 ); - - assert( _mag < NUM_NODES_W || _mag < NUM_NODES_H ); + assertion(_dir != NONE, 10); + assertion(_mag >= 0, 11); + assertion(_mag < NUM_NODES_W || _mag < NUM_NODES_H, 12); + } } diff --git a/micromouse/VirtualMaze.cpp b/micromouse/VirtualMaze.cpp index 9f11897..7654de6 100644 --- a/micromouse/VirtualMaze.cpp +++ b/micromouse/VirtualMaze.cpp @@ -6,7 +6,7 @@ Author GitHub: joshuasrjc \*********************************/ #include "VirtualMaze.h" -#include +#include "AssertionHandler.h" #ifdef __MK20DX256__ // Teensy Compile #include //random @@ -23,7 +23,7 @@ namespace Micromouse width(width), height(height) { - assert(width % 4 == 3 && height % 4 == 3); + assertion(width % 4 == 3 && height % 4 == 3, 13); }