From 90acef4110e611bf845ea1b9a8a52d68a50f3bf0 Mon Sep 17 00:00:00 2001 From: Andy Killorin <37423245+Speedy6451@users.noreply.github.com> Date: Thu, 5 Feb 2026 08:31:00 -0500 Subject: [PATCH 1/5] impl point tracking switched longitudinal error equation to not care about lateral offsets --- lib/Chassis/src/chassis.h | 1 + lib/Chassis/src/utils.cpp | 2 +- src/robot-nav.cpp | 20 ++++++++++++++++---- src/robot.cpp | 5 +++++ src/robot.h | 2 +- 5 files changed, 24 insertions(+), 6 deletions(-) diff --git a/lib/Chassis/src/chassis.h b/lib/Chassis/src/chassis.h index 8b58196..7487ce5 100644 --- a/lib/Chassis/src/chassis.h +++ b/lib/Chassis/src/chassis.h @@ -37,6 +37,7 @@ public: static void Timer4OverflowISRHandler(void); public: + Romi32U4ButtonA buttonA; Romi32U4ButtonB buttonB; Romi32U4ButtonC buttonC; diff --git a/lib/Chassis/src/utils.cpp b/lib/Chassis/src/utils.cpp index 58efd54..5c1b161 100644 --- a/lib/Chassis/src/utils.cpp +++ b/lib/Chassis/src/utils.cpp @@ -2,7 +2,7 @@ void TeleplotPrint(const char* var, float value) { - Serial.print('>'); + //Serial.print('>'); Serial.print(var); Serial.print(':'); Serial.print(value); diff --git a/src/robot-nav.cpp b/src/robot-nav.cpp index bb8e158..63860fb 100644 --- a/src/robot-nav.cpp +++ b/src/robot-nav.cpp @@ -18,8 +18,15 @@ void Robot::UpdatePose(const Pose& delta) currPose.theta += delta.theta; #ifdef __NAV_DEBUG__ - TeleplotPrint("x", currPose.x); - TeleplotPrint("y", currPose.y); + Serial.print("pos"); + Serial.print(':'); + Serial.print(currPose.x); + Serial.print(':'); + Serial.print(currPose.y); + Serial.print('|xy\n'); + + //TeleplotPrint("x", currPose.x); + //TeleplotPrint("y", currPose.y); TeleplotPrint("theta", currPose.theta * 180.0 / PI); #endif } @@ -75,17 +82,22 @@ void Robot::DriveToPoint(void) if (SquareDistance() < TARGET_SQ_DIST) { target_heading = destPose.theta; } else { - target_heading = atan2(destPose.x - currPose.x, destPose.y - currPose.y); + target_heading = atan2(destPose.y - currPose.y, destPose.x - currPose.x); } float heading_err = RadError(currPose.theta, target_heading); - float forward_err = cos(heading_err) * sqrt(SquareDistance()); + float forward_err = cos(currPose.theta) * (destPose.x - currPose.x) + + sin(currPose.theta) * (destPose.y - currPose.y); float turn_effort = heading_err * LATERAL_kP; float forward_effort = forward_err * LONGITUDINAL_kP; #ifdef __NAV_DEBUG__ // Print useful stuff here. + + TeleplotPrint("heading_err", heading_err * RAD_TO_DEG); + TeleplotPrint("linear_err", forward_err); + TeleplotPrint("linear_dist", SquareDistance()); #endif chassis.SetMotorEfforts(forward_effort - turn_effort, forward_effort + turn_effort); diff --git a/src/robot.cpp b/src/robot.cpp index 27f8895..6c13b1a 100644 --- a/src/robot.cpp +++ b/src/robot.cpp @@ -1,4 +1,5 @@ #include "robot.h" +#include "utils.h" void Robot::InitializeRobot(void) { @@ -76,6 +77,10 @@ void Robot::RobotLoop(void) UpdatePose(delta); //chassis.SetMotorEfforts(220,-220); + if (chassis.buttonA.isPressed()) { + Pose dest(0.0, 10.0, 0.0); + SetDestination(dest); + } if (chassis.buttonB.isPressed()) { EnterCalibTurn(); } diff --git a/src/robot.h b/src/robot.h index 4f8d975..df61f58 100644 --- a/src/robot.h +++ b/src/robot.h @@ -6,7 +6,7 @@ const float TARGET_SQ_DIST = pow(3.0, 2); // distance from setpoint that is ok ( const float TARGET_RAD_ERR = 0.25; // angle from setpoint that is ok (rad) const float LONGITUDINAL_kP = 3.0; -const float LATERAL_kP = 3.0 * DEG_TO_RAD; +const float LATERAL_kP = 3.0 * RAD_TO_DEG; class Robot { From ab507bb68a3d91003421f2eeb81db7c865bc0764 Mon Sep 17 00:00:00 2001 From: Andy Killorin <37423245+Speedy6451@users.noreply.github.com> Date: Thu, 5 Feb 2026 08:35:43 -0500 Subject: [PATCH 2/5] add static friction feedforward --- src/robot-nav.cpp | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/robot-nav.cpp b/src/robot-nav.cpp index 63860fb..35857fa 100644 --- a/src/robot-nav.cpp +++ b/src/robot-nav.cpp @@ -73,6 +73,18 @@ bool Robot::CheckReachedDestination(void) && RadError(currPose.theta, destPose.theta) < TARGET_RAD_ERR; } +float boost(float input, float boost) { + if (input == 0.0) { + return input; + } + if (input > 0.0) { + input += boost; + } else { + input -= boost; + } + return input; +} + void Robot::DriveToPoint(void) { if(robotState == ROBOT_DRIVE_TO_POINT) @@ -100,7 +112,9 @@ void Robot::DriveToPoint(void) TeleplotPrint("linear_dist", SquareDistance()); #endif - chassis.SetMotorEfforts(forward_effort - turn_effort, forward_effort + turn_effort); + //chassis.SetMotorEfforts(20, 20); + float boost_f = 15.0; + chassis.SetMotorEfforts(boost(forward_effort - turn_effort,boost_f), boost(forward_effort + turn_effort, boost_f)); } } From 890cc88143f479eff57b4414311219a706503dd5 Mon Sep 17 00:00:00 2001 From: Andy Killorin <37423245+Speedy6451@users.noreply.github.com> Date: Thu, 5 Feb 2026 08:46:51 -0500 Subject: [PATCH 3/5] path sequencing --- src/robot-nav.cpp | 25 ++++++++++++++++++++++--- src/robot.cpp | 1 + src/robot.h | 3 +++ 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/src/robot-nav.cpp b/src/robot-nav.cpp index 35857fa..c78f811 100644 --- a/src/robot-nav.cpp +++ b/src/robot-nav.cpp @@ -3,6 +3,7 @@ */ #include "robot.h" +#include "utils.h" void Robot::UpdatePose(const Pose& delta) { @@ -23,7 +24,7 @@ void Robot::UpdatePose(const Pose& delta) Serial.print(currPose.x); Serial.print(':'); Serial.print(currPose.y); - Serial.print('|xy\n'); + Serial.print("|xy\n"); //TeleplotPrint("x", currPose.x); //TeleplotPrint("y", currPose.y); @@ -85,6 +86,17 @@ float boost(float input, float boost) { return input; } +Pose PATH[2] = { + Pose(43.0,0,0), + Pose(0,0,0), +}; + +void Robot::DrivePath(void) { + currPose = Pose(); // clear pose + index = 0; + SetDestination(PATH[index]); +} + void Robot::DriveToPoint(void) { if(robotState == ROBOT_DRIVE_TO_POINT) @@ -112,16 +124,23 @@ void Robot::DriveToPoint(void) TeleplotPrint("linear_dist", SquareDistance()); #endif - //chassis.SetMotorEfforts(20, 20); float boost_f = 15.0; chassis.SetMotorEfforts(boost(forward_effort - turn_effort,boost_f), boost(forward_effort + turn_effort, boost_f)); } } +#define len(x) sizeof(x) / sizeof(x[0]) void Robot::HandleDestination(void) { /** * TODO: Stop and change state. Turn off LED. */ - EnterIdleState(); // TODO: sequence + index += 1; + if (index == len(PATH)) { + EnterIdleState(); + } + + TeleplotPrint("path_index", index); + + SetDestination(PATH[index]); } diff --git a/src/robot.cpp b/src/robot.cpp index 6c13b1a..82c48d7 100644 --- a/src/robot.cpp +++ b/src/robot.cpp @@ -79,6 +79,7 @@ void Robot::RobotLoop(void) if (chassis.buttonA.isPressed()) { Pose dest(0.0, 10.0, 0.0); + index = 0; SetDestination(dest); } if (chassis.buttonB.isPressed()) { diff --git a/src/robot.h b/src/robot.h index df61f58..4f79bdf 100644 --- a/src/robot.h +++ b/src/robot.h @@ -36,6 +36,8 @@ protected: Pose currPose; Pose destPose; + int index = 0; + public: Robot(void) {keyString.reserve(10);} void InitializeRobot(void); @@ -51,6 +53,7 @@ protected: void UpdatePose(const Pose& u); void SetDestination(const Pose& destination); void DriveToPoint(void); + void DrivePath(void); void CalibTurn(void); void CalibDrive(void); bool CheckReachedDestination(void); From b2d0b01232f59a1030b6f563e5fa93e59e56c2a4 Mon Sep 17 00:00:00 2001 From: Andy Killorin <37423245+Speedy6451@users.noreply.github.com> Date: Thu, 5 Feb 2026 09:26:22 -0500 Subject: [PATCH 4/5] tuned path sequencing PATH is in lab coordinates, then transformed --- src/robot-nav.cpp | 40 +++++++++++++++++++++++++++++++++++----- src/robot.cpp | 6 +++--- src/robot.h | 6 ++++-- 3 files changed, 42 insertions(+), 10 deletions(-) diff --git a/src/robot-nav.cpp b/src/robot-nav.cpp index c78f811..a9e8a86 100644 --- a/src/robot-nav.cpp +++ b/src/robot-nav.cpp @@ -46,7 +46,21 @@ void Robot::SetDestination(const Pose& dest) Serial.print(dest.y); Serial.print('\n'); - destPose = dest; + // transform to board space + + Pose transformed; + + // rotate clockwise + transformed.x = dest.y; + transformed.y = -dest.x; + transformed.theta = dest.theta; + + // add offset + transformed.x += offsetPose.x; + transformed.y += offsetPose.y; + transformed.theta += offsetPose.theta; + + destPose = transformed; robotState = ROBOT_DRIVE_TO_POINT; } @@ -86,13 +100,21 @@ float boost(float input, float boost) { return input; } -Pose PATH[2] = { - Pose(43.0,0,0), - Pose(0,0,0), +// x forward +// y left +// +// goal y forward x right +const int PATH_LEN=4; +Pose PATH[PATH_LEN] = { + Pose(0.0, 43.0, 0.0), + Pose(43.0, 0.0, 0.0), + Pose(43.0, 43.0, 0.0), + Pose(0.0,0.0,0.0), }; + void Robot::DrivePath(void) { - currPose = Pose(); // clear pose + currPose = offsetPose; // clear pose index = 0; SetDestination(PATH[index]); } @@ -113,9 +135,16 @@ void Robot::DriveToPoint(void) float forward_err = cos(currPose.theta) * (destPose.x - currPose.x) + sin(currPose.theta) * (destPose.y - currPose.y); + + if (heading_err > 45.0) { + forward_err = 0.0; + } + float turn_effort = heading_err * LATERAL_kP; float forward_effort = forward_err * LONGITUDINAL_kP; + turn_effort = max(min(turn_effort, 100.0), -100.0); // constrain turn_effort + #ifdef __NAV_DEBUG__ // Print useful stuff here. @@ -138,6 +167,7 @@ void Robot::HandleDestination(void) index += 1; if (index == len(PATH)) { EnterIdleState(); + return; } TeleplotPrint("path_index", index); diff --git a/src/robot.cpp b/src/robot.cpp index 82c48d7..c6f26fa 100644 --- a/src/robot.cpp +++ b/src/robot.cpp @@ -78,9 +78,9 @@ void Robot::RobotLoop(void) //chassis.SetMotorEfforts(220,-220); if (chassis.buttonA.isPressed()) { - Pose dest(0.0, 10.0, 0.0); - index = 0; - SetDestination(dest); + //Pose dest = Pose(0.0,43.0, 0.0); + //SetDestination(dest); + DrivePath(); } if (chassis.buttonB.isPressed()) { EnterCalibTurn(); diff --git a/src/robot.h b/src/robot.h index 4f79bdf..01ff4da 100644 --- a/src/robot.h +++ b/src/robot.h @@ -5,8 +5,10 @@ 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) -const float LONGITUDINAL_kP = 3.0; -const float LATERAL_kP = 3.0 * RAD_TO_DEG; +const float LONGITUDINAL_kP = 4.0; +const float LATERAL_kP = 1.5 * RAD_TO_DEG; + +const Pose offsetPose = Pose(100.0, 100.0, 0.0); // hack to avoid negatives class Robot { From bb330560710dcf37311341b49343c422c2a858c6 Mon Sep 17 00:00:00 2001 From: Andy Killorin <37423245+Speedy6451@users.noreply.github.com> Date: Thu, 5 Feb 2026 09:46:03 -0500 Subject: [PATCH 5/5] follow sa path --- src/robot-nav.cpp | 14 +++++++------- src/robot.cpp | 1 + src/robot.h | 2 +- 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/robot-nav.cpp b/src/robot-nav.cpp index a9e8a86..3e1bc55 100644 --- a/src/robot-nav.cpp +++ b/src/robot-nav.cpp @@ -50,7 +50,7 @@ void Robot::SetDestination(const Pose& dest) Pose transformed; - // rotate clockwise + //// rotate clockwise transformed.x = dest.y; transformed.y = -dest.x; transformed.theta = dest.theta; @@ -104,12 +104,12 @@ float boost(float input, float boost) { // y left // // goal y forward x right -const int PATH_LEN=4; +const int PATH_LEN=3; Pose PATH[PATH_LEN] = { - Pose(0.0, 43.0, 0.0), - Pose(43.0, 0.0, 0.0), - Pose(43.0, 43.0, 0.0), - Pose(0.0,0.0,0.0), + Pose(0.0, 81.0, 0.0), + Pose(40.0, 40.0, M_PI/ 2.0), + Pose(0.0, 0.0, 0.0), + }; @@ -136,7 +136,7 @@ void Robot::DriveToPoint(void) + sin(currPose.theta) * (destPose.y - currPose.y); - if (heading_err > 45.0) { + if (heading_err > 15.0) { forward_err = 0.0; } diff --git a/src/robot.cpp b/src/robot.cpp index c6f26fa..9991a11 100644 --- a/src/robot.cpp +++ b/src/robot.cpp @@ -80,6 +80,7 @@ void Robot::RobotLoop(void) if (chassis.buttonA.isPressed()) { //Pose dest = Pose(0.0,43.0, 0.0); //SetDestination(dest); + delay(1000); DrivePath(); } if (chassis.buttonB.isPressed()) { diff --git a/src/robot.h b/src/robot.h index 01ff4da..36dfa53 100644 --- a/src/robot.h +++ b/src/robot.h @@ -6,7 +6,7 @@ const float TARGET_SQ_DIST = pow(3.0, 2); // distance from setpoint that is ok ( const float TARGET_RAD_ERR = 0.25; // angle from setpoint that is ok (rad) const float LONGITUDINAL_kP = 4.0; -const float LATERAL_kP = 1.5 * RAD_TO_DEG; +const float LATERAL_kP = 1.7 * RAD_TO_DEG; const Pose offsetPose = Pose(100.0, 100.0, 0.0); // hack to avoid negatives