-
Notifications
You must be signed in to change notification settings - Fork 1
Motor Calibration #105
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
joshuasrjc
wants to merge
8
commits into
dev
Choose a base branch
from
joshua_motor_calibration
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Motor Calibration #105
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
0e58821
Add Recorder class
blhough 3001c9f
add multiple variable tracking
blhough d25f197
Fixed PID Bug
joshuasrjc d1aaebe
Added Data Logging for PID
joshuasrjc a6538cb
Progress
joshuasrjc 05aa4bc
Progress 2
joshuasrjc 818cf86
Merge remote-tracking branch 'refs/remotes/origin/dev' into joshua_mo…
joshuasrjc c11c8e8
Motor Calibration
joshuasrjc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| { | ||
|
|
@@ -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"; | ||
|
|
@@ -42,29 +123,51 @@ namespace Micromouse | |
| { | ||
| digitalWrite(fwdPin, HIGH); | ||
| digitalWrite(bwdPin, LOW); | ||
| analogWrite(pwmPin, (int)(255 * speed)); | ||
| setVoltage((int)(255 * speed), true); | ||
| } | ||
| 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) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| { | ||
| 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 | ||
| } | ||
|
|
||
|
|
||
|
|
@@ -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 | ||
| } | ||
|
|
||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.

There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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