From f4373cdc590416170902193aac92b3428dedf972 Mon Sep 17 00:00:00 2001 From: Andy Killorin <37423245+Speedy6451@users.noreply.github.com> Date: Wed, 4 Feb 2026 14:50:41 -0500 Subject: [PATCH 1/2] impl CheckReachedDestination --- src/robot-nav.cpp | 6 ++++-- src/robot.h | 3 +++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/robot-nav.cpp b/src/robot-nav.cpp index 51405ba..e975cea 100644 --- a/src/robot-nav.cpp +++ b/src/robot-nav.cpp @@ -44,12 +44,14 @@ void Robot::SetDestination(const Pose& dest) bool Robot::CheckReachedDestination(void) { - bool retVal = false; /** * TODO: Add code to check if you've reached destination here. */ - return retVal; + float sq_dist = pow((destPose.x - currPose.x),2) + pow((destPose.y-currPose.y),2); + float rad_error = abs(destPose.theta - currPose.theta); + + return sq_dist < TARGET_SQ_DIST && rad_error < TARGET_RAD_ERR; } void Robot::DriveToPoint(void) diff --git a/src/robot.h b/src/robot.h index 4193edc..0c043aa 100644 --- a/src/robot.h +++ b/src/robot.h @@ -2,6 +2,9 @@ #include "chassis.h" +const float TARGET_SQ_DIST = pow(3.0, 2); // distance from setpoint that is ok (cm) +const float TARGET_RAD_ERR = 0.25; // angle from setpoint that is ok (rad) + class Robot { protected: From 718a551d72342d6f5c5c77813b5639567dc8e7fc Mon Sep 17 00:00:00 2001 From: Andy Killorin <37423245+Speedy6451@users.noreply.github.com> Date: Wed, 4 Feb 2026 14:57:41 -0500 Subject: [PATCH 2/2] calculate heading error --- src/robot-nav.cpp | 19 +++++++++++++++++-- src/robot.h | 3 +++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/robot-nav.cpp b/src/robot-nav.cpp index e975cea..7146f21 100644 --- a/src/robot-nav.cpp +++ b/src/robot-nav.cpp @@ -42,22 +42,37 @@ void Robot::SetDestination(const Pose& dest) robotState = ROBOT_DRIVE_TO_POINT; } +float Robot::SquareDistance(void) { + return pow((destPose.x - currPose.x),2) + pow((destPose.y-currPose.y),2); +} + bool Robot::CheckReachedDestination(void) { /** * TODO: Add code to check if you've reached destination here. */ - float sq_dist = pow((destPose.x - currPose.x),2) + pow((destPose.y-currPose.y),2); float rad_error = abs(destPose.theta - currPose.theta); - return sq_dist < TARGET_SQ_DIST && rad_error < TARGET_RAD_ERR; + return SquareDistance() < TARGET_SQ_DIST && rad_error < TARGET_RAD_ERR; } void Robot::DriveToPoint(void) { if(robotState == ROBOT_DRIVE_TO_POINT) { + float target_heading; + + if (SquareDistance() < TARGET_SQ_DIST) { + target_heading = destPose.theta; + } else { + target_heading = atan2(destPose.x - currPose.x, destPose.y - currPose.y); + } + + float heading_err = destPose.theta - target_heading; + float forward_effort = cos(heading_err); + + /** * TODO: Add your IK algorithm here. */ diff --git a/src/robot.h b/src/robot.h index 0c043aa..7d8bd5e 100644 --- a/src/robot.h +++ b/src/robot.h @@ -52,4 +52,7 @@ protected: void CalibDrive(void); bool CheckReachedDestination(void); void HandleDestination(void); + + // Square distance between current and target pose + float SquareDistance(void); };