Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions MicroMouse-2016.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@
<ClCompile Include="micromouse\VirtualMaze.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="micromouse\ArduinoDummy.h" />
<ClInclude Include="micromouse\ButtonFlag.h" />
<ClInclude Include="micromouse\Controller.h" />
<ClInclude Include="micromouse\FlagMatrix.h" />
Expand All @@ -177,6 +178,7 @@
<ClInclude Include="micromouse\Node.h" />
<ClInclude Include="micromouse\Path.h" />
<ClInclude Include="micromouse\PIDController.h" />
<ClInclude Include="micromouse\Recorder.h" />
<ClInclude Include="micromouse\RobotIO.h" />
<ClInclude Include="micromouse\IRSensor.h" />
<ClInclude Include="micromouse\Timer.h" />
Expand Down
6 changes: 6 additions & 0 deletions MicroMouse-2016.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -110,5 +110,11 @@
<ClInclude Include="micromouse\ButtonFlag.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="micromouse\Recorder.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="micromouse\ArduinoDummy.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
</Project>
31 changes: 31 additions & 0 deletions micromouse/ArduinoDummy.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#pragma once

namespace Micromouse
{
#ifndef __MK20DX256__ // If NOT on Teensy

enum PinMode
{
INPUT, OUTPUT
};

enum PinValue
{
LOW, HIGH
};

void pinMode(int pin, PinMode mode) { }
void digitalWrite(int pin, PinValue value) { }
void analogWrite(int pin, int value) { }


class SERIAL
{
public:
void println(float x) {}
};

SERIAL Serial = SERIAL();

#endif
}
8 changes: 4 additions & 4 deletions micromouse/Controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ namespace Micromouse
{
// DEBUG CODE GOES IN HERE!

mouse.mapMaze();
//mouse.mapMaze();
//mouse.runMaze();
//mouse.testIR();
//mouse.testMotors();
mouse.testMotors();
//mouse.testRotate();

// DEBUG CODE GOES IN HERE!
Expand Down Expand Up @@ -95,7 +95,7 @@ namespace Micromouse
"1: map maze\n"
"2: run maze\n"
"3: cycle speed\n"
"4: nothing\n"
"4: debug\n"
"5: calibrate sensors\n"
"6: calibrate motor\n"
"7: reset Map\n";
Expand Down Expand Up @@ -209,7 +209,7 @@ namespace Micromouse

blinkLED(CAL_MOTOR);
state = NONE;
//TODO decide if this is even something we need
mouse.robotIO.calibrateMotors();
break;


Expand Down
17 changes: 16 additions & 1 deletion micromouse/Memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,23 @@

namespace Micromouse {

//0-99 unreserved
/*
0 - 31: Motor Calibration Data Block
*/

//32-99 unreserved


/*

100 - 179: IRSensor Calibration Data Block
relative to the starting address
0: calibrationSize
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
Expand All @@ -32,6 +38,15 @@ namespace Micromouse {
class Memory
{
public:
static const int RIGHT_FWD_VOLTAGE = 0;
static const int RIGHT_BWD_VOLTAGE = 4;
static const int RIGHT_FWD_SPEED = 8;
static const int RIGHT_BWD_SPEED = 12;
static const int LEFT_FWD_VOLTAGE = 16;
static const int LEFT_BWD_VOLTAGE = 20;
static const int LEFT_FWD_SPEED = 24;
static const int LEFT_BWD_SPEED = 28;

static int read(int address);
static void write(int address, int val);

Expand Down
123 changes: 111 additions & 12 deletions micromouse/Motor.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#include "Motor.h"
#include "Logger.h"
#include "Timer.h"
#include "ArduinoDummy.h"
#include "Memory.h"
#include "RobotIO.h"

namespace Micromouse
{
Expand All @@ -11,25 +15,102 @@ namespace Micromouse
, encoder(Encoder(fwdEncoderPin, bwdEncoderPin))
#endif
{
loadCalibration();
initPins();
}



void Motor::loadCalibration()
{
if (pwmPin == MOTOR_LEFT_PWM_PIN)
{
minFwdVoltage = Memory::read(Memory::LEFT_FWD_VOLTAGE);
minBwdVoltage = Memory::read(Memory::LEFT_BWD_VOLTAGE);
}
else if (pwmPin == MOTOR_RIGHT_PWM_PIN)
{
minFwdVoltage = Memory::read(Memory::RIGHT_FWD_VOLTAGE);
minBwdVoltage = Memory::read(Memory::RIGHT_BWD_VOLTAGE);
}
}



void Motor::saveCalibration()
{
if (pwmPin == MOTOR_LEFT_PWM_PIN)
{
Memory::write(Memory::LEFT_FWD_VOLTAGE, minFwdVoltage);
Memory::write(Memory::LEFT_BWD_VOLTAGE, minBwdVoltage);
}
else if (pwmPin == MOTOR_RIGHT_PWM_PIN)
{
Memory::write(Memory::RIGHT_FWD_VOLTAGE, minFwdVoltage);
Memory::write(Memory::RIGHT_BWD_VOLTAGE, minBwdVoltage);
}
}



void Motor::initPins()
{
#ifdef __MK20DX256__ // Teensy Compile
pinMode(fwdPin, OUTPUT);
pinMode(bwdPin, OUTPUT);
pinMode(pwmPin, OUTPUT);
#endif
}



void Motor::calibrate()
{
digitalWrite(fwdPin, HIGH);
digitalWrite(bwdPin, LOW);
minFwdVoltage = calibrateMinVoltage() + 3;

digitalWrite(fwdPin, LOW);
digitalWrite(bwdPin, HIGH);
minBwdVoltage = calibrateMinVoltage() + 3;

digitalWrite(fwdPin, LOW);
digitalWrite(bwdPin, LOW);

saveCalibration();
}



int Motor::calibrateMinVoltage()
{
Timer timer = Timer();
int voltage = 64;
bool timedOut = false;
while (voltage > 0 && !timedOut)
{
voltage--;
analogWrite(pwmPin, voltage);

float waitTime = 0;
timer.start();
resetCounts();
while (getCounts() < 10 && getCounts() > -10 && !timedOut)
{
waitTime += timer.getDeltaTime();
Serial.println(waitTime);
if (waitTime >= CALIBRATION_TIMEOUT_SEC)
{
timedOut = true;
}
}
}

return voltage;
}



void Motor::setMovement(float speed)
{
#ifdef __MK20DX256__ // Teensy Compile
if (speed > 1 || speed < -1)
{
logC(WARN) << "In setMovement, speed was not between -1 and 1";
Expand All @@ -42,29 +123,51 @@ namespace Micromouse
{
digitalWrite(fwdPin, HIGH);
digitalWrite(bwdPin, LOW);
analogWrite(pwmPin, (int)(255 * speed));
setVoltage((int)(255 * speed), true);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not pass the speed to setVoltage and do the calculations in the function

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I decided that speed would refer to a float between -1 and 1, where voltage refers to an int between 0 and 255. That's why setVoltage() takes an int. It doesn't really matter either way though.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right so setVoltage takes in a speed and sets the voltage, it still follows the convention of -1 to 1 for speed and 0 to 255 for voltage. Then that way the code stays DRY

}
else
{
digitalWrite(fwdPin, LOW);
digitalWrite(bwdPin, HIGH);
analogWrite(pwmPin, (int)(255 * (-speed)));
setVoltage((int)(255 * (-speed)), false);
}
#endif
}



void Motor::setVoltage(int voltage, bool fwd)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

So if I understand this correctly, the speed maps to voltage like depicted on the left

do you think a voltage mapping like on the right be better?

{
if (voltage > 0)
{
if (fwd)
{
if (voltage < minFwdVoltage)
{
voltage = minFwdVoltage;
}
}
else
{
if (voltage < minBwdVoltage)
{
voltage = minBwdVoltage;
}
}
}

analogWrite(pwmPin, voltage);
}



void Motor::setMaxSpeed(float maxSpeed)
{
#ifdef __MK20DX256__ // Teensy Compile
if (maxSpeed < 0 || maxSpeed > 1)
{
logC(WARN) << "Motor was given a maxSpeed that is not between 0 and 1.";
maxSpeed = maxSpeed < 0 ? 0 : 1;
}
this->maxSpeed = maxSpeed;
#endif
}


Expand All @@ -78,22 +181,18 @@ namespace Micromouse

void Motor::brake()
{
#ifdef __MK20DX256__ // Teensy Compile
digitalWrite(fwdPin, HIGH);
digitalWrite(bwdPin, HIGH);
analogWrite(pwmPin, 0);
#endif
}



void Motor::coast()
{
#ifdef __MK20DX256__ // Teensy Compile
digitalWrite(fwdPin, LOW);
digitalWrite(bwdPin, LOW);
analogWrite(pwmPin, 0);
#endif
}


Expand Down
24 changes: 24 additions & 0 deletions micromouse/Motor.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once

//#include <string>

#ifdef __MK20DX256__ // Teensy Compile
#include <Encoder.h>
#endif
Expand All @@ -11,8 +13,20 @@ namespace Micromouse
class Motor
{
public:
const float CALIBRATION_TIMEOUT_SEC = 0.5f;

Motor(int fwdPin, int bwdPin, int pwmPin, int fwdEncoderPin, int bwdEncoderPin);

//Loads calibration data from memory.
void loadCalibration();

//Saves calibration data to memory.
void saveCalibration();

//Spins the motor forward and backward, calibrating minFwdVoltage and minBwdVoltage,
//which are each set to the lowest speed that still turns the motor.
void calibrate();

//speed should be a value between -1 and 1.
//Positive speeds move the motor forward. Negative speeds move it backward.
//The actual speed of the motor will be the product of speed and maxSpeed ( see setMaxSpeed()).
Expand All @@ -38,11 +52,21 @@ namespace Micromouse
//Returns the number of encoder counts since resetCounts() was last called.
//Then resets the encoder count.
int resetCounts();

private:
void initPins();

int calibrateMinVoltage();

void setVoltage(int voltage, bool fwd);

float maxSpeed = 1.0f;

//The minimum speed that still turns the motor (forward)
int minFwdVoltage = 0;
//The minimum speed that still turns the motor (backward)
int minBwdVoltage = 0;

int fwdPin; //Forward pin
int bwdPin; //Backward pin
int pwmPin; //Pulse-width modulation pin (for speed control)
Expand Down
3 changes: 2 additions & 1 deletion micromouse/MouseBot.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ namespace Micromouse
class MouseBot
{
public:
RobotIO robotIO;

MouseBot(int x = 0, int y = 0); // Sets the position to (x,y)
~MouseBot();

Expand Down Expand Up @@ -92,7 +94,6 @@ namespace Micromouse
VirtualMaze* virtualMaze;
#endif
int saveAddress = 512;
RobotIO robotIO;
std::stack<direction> movementHistory;

// Mouse position in the maze
Expand Down
Loading