Skip to content
This repository was archived by the owner on Jul 25, 2022. It is now read-only.

Commit 0ce2217

Browse files
committed
Added backward direction for position calculation.
1 parent 3882c8f commit 0ce2217

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

robot-control-src/Drives.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ static constexpr float stepsPerRad = stepsPerDeg / ((2*pi)/360);
1313

1414
static enum class Action
1515
{
16-
STRAIGHTFORWARD, TURN_LEFT, TURN_RIGHT
16+
FORWARD, BACKWARD, TURN_LEFT, TURN_RIGHT
1717
} lastAction;
1818

1919
Position lastKnownPosition = {0,0};
@@ -67,7 +67,7 @@ void rotateCounter(const Counter steps, const Amplitude amplitude, bool const cl
6767

6868
void driveCounter(const Counter steps, const Amplitude amplitude, const bool backwards)
6969
{
70-
lastAction = Action::STRAIGHTFORWARD;
70+
lastAction = backwards ? Action::BACKWARD : Action::FORWARD;
7171
LeftDrive::drive(steps, amplitude, backwards);
7272
RightDrive::drive(steps, calcRightSpeed(amplitude), backwards);
7373
}
@@ -88,10 +88,12 @@ void drive(const float distance, const Amplitude amplitude, const bool backwards
8888
Position flushCurrentPosition()
8989
{
9090
switch (lastAction) {
91-
case Action::STRAIGHTFORWARD:
91+
case Action::FORWARD:
92+
case Action::BACKWARD:
9293
{
93-
lastKnownPosition.x += LeftDrive::counter * board::odoIntervalLength * std::cos(orientation);
94-
lastKnownPosition.y += LeftDrive::counter * board::odoIntervalLength * std::sin(orientation);
94+
const std::int8_t reversed = (lastAction == Action::BACKWARD) ? -1 : 1;
95+
lastKnownPosition.x += LeftDrive::counter * board::odoIntervalLength * std::cos(orientation) * reversed;
96+
lastKnownPosition.y += LeftDrive::counter * board::odoIntervalLength * std::sin(orientation) * reversed;
9597
break;
9698
}
9799
case Action::TURN_LEFT:

0 commit comments

Comments
 (0)