Skip to content

Commit e7e3cda

Browse files
committed
Added backward functionality for robot and refactored movement logic
1 parent b6ae4ac commit e7e3cda

1 file changed

Lines changed: 24 additions & 9 deletions

File tree

src/controller.hpp

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ using namespace std::chrono_literals;
2222

2323
class RobotController : public HIVE::Commons::Threading::Tickable {
2424
public:
25-
enum EBotActions { FORWARD, TURN_LEFT, TURN_RIGHT, STOP };
25+
enum EBotActions { FORWARD, BACKWARD, TURN_LEFT, TURN_RIGHT, STOP };
2626

2727
RobotController(int tps) : Tickable(tps, "Robot") { HIVE::Commons::Zumo::GPIO::setup(); }
2828

@@ -78,17 +78,28 @@ class RobotController : public HIVE::Commons::Threading::Tickable {
7878
Vec::Vec3 cross = robot_dir_flat.cross(required_dir_3D);
7979
bool turn_right = cross.getY() > 0;
8080

81+
// dot product in 3D (go forwards or backwards)
82+
float dot = robot_dir_flat.dot(required_dir_3D);
83+
8184
// log error
8285
Logger::log("Error: " + std::to_string(error), LogLevel::Level::INFO);
8386

84-
if (error < EB_ROT) { // if error is within bounds
85-
return FORWARD; // move forward
86-
} else { // else
87-
if (turn_right) { // if turn right
88-
return TURN_RIGHT; // turn right
89-
} else { // else
90-
return TURN_LEFT; // turn left
91-
}
87+
if (dot > 0) {
88+
if (error < EB_ROT) {
89+
return FORWARD; // move forward if aligned
90+
}
91+
else {
92+
// need to turn
93+
if (turn_right) {
94+
return TURN_RIGHT;
95+
}
96+
else {
97+
return TURN_LEFT;
98+
}
99+
}
100+
}
101+
else {
102+
return BACKWARD; // move backward if facing away from target
92103
}
93104
}
94105

@@ -150,6 +161,10 @@ class RobotController : public HIVE::Commons::Threading::Tickable {
150161
Logger::log("Moving Forward", LogLevel::Level::INFO);
151162
Movement::forward();
152163
break;
164+
case BACKWARD:
165+
Logger::log("Moving Backward", LogLevel::Level::INFO);
166+
Movement::backward();
167+
break;
153168
case TURN_LEFT:
154169
Logger::log("Turning Left", LogLevel::Level::INFO);
155170
Movement::turn_left();

0 commit comments

Comments
 (0)