From 1bfee77e342bfd9212f2ab57ee4ae157440e743d Mon Sep 17 00:00:00 2001 From: Abhinav Goyal Date: Mon, 4 Nov 2024 17:27:50 -0800 Subject: [PATCH 1/7] Change 300ms time delay to a while loop --- src/TunePID.cpp | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/TunePID.cpp b/src/TunePID.cpp index 9f6675d3..204996e0 100644 --- a/src/TunePID.cpp +++ b/src/TunePID.cpp @@ -137,8 +137,20 @@ int main(int argc, char** argv) { double period = 8.0; - std::this_thread::sleep_for(300ms); // wait for encoder position data to arrive - int32_t starting_angle = can::motor::getMotorPosition(serial); + time_point start = steady_clock::now(); + int timeout = 300; // in milliseconds + DataPoint starting_angle_data_point = can::motor::getMotorPosition(serial); + while(!starting_angle_data_point.isValid() && steady_clock::now() - start < milliseconds(timeout)) { + starting_angle_data_point = can::motor::getMotorPosition(serial); + } + + int32_t starting_angle; + if(starting_angle_data_point.isValid()) { + starting_angle = starting_angle_data_point.getData(); + } else { + LOG_F(WARNING, "STARTING ANGLE DATA NOT FOUND"); + } + int32_t angle_target = starting_angle; double acc_error = 0.0; int total_steps = 0; From 123380540722c6bcc007a43a8052fecbca619748 Mon Sep 17 00:00:00 2001 From: Abhinav Goyal Date: Mon, 4 Nov 2024 18:04:14 -0800 Subject: [PATCH 2/7] Fix formatting issue --- src/TunePID.cpp | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/TunePID.cpp b/src/TunePID.cpp index 204996e0..96b0fbde 100644 --- a/src/TunePID.cpp +++ b/src/TunePID.cpp @@ -17,8 +17,10 @@ extern "C" { #include "HindsightCAN/CANCommon.h" } -enum class targetmode_t { - step, sinusoidal +enum class targetmode_t +{ + step, + sinusoidal }; using namespace robot::types; @@ -140,12 +142,13 @@ int main(int argc, char** argv) { time_point start = steady_clock::now(); int timeout = 300; // in milliseconds DataPoint starting_angle_data_point = can::motor::getMotorPosition(serial); - while(!starting_angle_data_point.isValid() && steady_clock::now() - start < milliseconds(timeout)) { + while (!starting_angle_data_point.isValid() && + steady_clock::now() - start < milliseconds(timeout)) { starting_angle_data_point = can::motor::getMotorPosition(serial); } int32_t starting_angle; - if(starting_angle_data_point.isValid()) { + if (starting_angle_data_point.isValid()) { starting_angle = starting_angle_data_point.getData(); } else { LOG_F(WARNING, "STARTING ANGLE DATA NOT FOUND"); @@ -171,7 +174,7 @@ int main(int argc, char** argv) { if (mode == targetmode_t::step) { prescaled_target = round(prescaled_target); } - angle_target = (int32_t) round(amplitude * prescaled_target) + starting_angle; + angle_target = (int32_t)round(amplitude * prescaled_target) + starting_angle; can::motor::setMotorPIDTarget(serial, angle_target); From 2a1ba7cf8a2640ad0f964611e9c2a24507911584 Mon Sep 17 00:00:00 2001 From: Abhinav Goyal Date: Mon, 4 Nov 2024 18:12:44 -0800 Subject: [PATCH 3/7] Fix formatting issues in TunePID --- src/TunePID.cpp | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/TunePID.cpp b/src/TunePID.cpp index 96b0fbde..3ab79024 100644 --- a/src/TunePID.cpp +++ b/src/TunePID.cpp @@ -17,10 +17,8 @@ extern "C" { #include "HindsightCAN/CANCommon.h" } -enum class targetmode_t -{ - step, - sinusoidal +enum class targetmode_t { + step, sinusoidal }; using namespace robot::types; From 28239470ef81224cfc97e48094bc55ea1537abb4 Mon Sep 17 00:00:00 2001 From: Abhinav Goyal Date: Sat, 9 Nov 2024 11:46:11 -0800 Subject: [PATCH 4/7] Make TunePID throw an error if starting angle is not found --- src/TunePID.cpp | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/TunePID.cpp b/src/TunePID.cpp index 3ab79024..671bcbe7 100644 --- a/src/TunePID.cpp +++ b/src/TunePID.cpp @@ -138,19 +138,18 @@ int main(int argc, char** argv) { double period = 8.0; time_point start = steady_clock::now(); - int timeout = 300; // in milliseconds + constexpr int timeout = 300ms; DataPoint starting_angle_data_point = can::motor::getMotorPosition(serial); while (!starting_angle_data_point.isValid() && steady_clock::now() - start < milliseconds(timeout)) { starting_angle_data_point = can::motor::getMotorPosition(serial); } - int32_t starting_angle; - if (starting_angle_data_point.isValid()) { - starting_angle = starting_angle_data_point.getData(); - } else { + if (!starting_angle_data_point.isValid()) { LOG_F(WARNING, "STARTING ANGLE DATA NOT FOUND"); } + // throws bad_datapoint_access if the data point is not valid + int32_t starting_angle = starting_angle_data_point.getData(); int32_t angle_target = starting_angle; double acc_error = 0.0; From 1e31620a9295efc7f4e1bbcba5c2458015b586d7 Mon Sep 17 00:00:00 2001 From: Abhinav Goyal Date: Sat, 9 Nov 2024 12:04:40 -0800 Subject: [PATCH 5/7] Change datatype of timeout from int to milliseconds --- src/TunePID.cpp | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/TunePID.cpp b/src/TunePID.cpp index 671bcbe7..d6651aef 100644 --- a/src/TunePID.cpp +++ b/src/TunePID.cpp @@ -17,8 +17,10 @@ extern "C" { #include "HindsightCAN/CANCommon.h" } -enum class targetmode_t { - step, sinusoidal +enum class targetmode_t +{ + step, + sinusoidal }; using namespace robot::types; @@ -138,10 +140,9 @@ int main(int argc, char** argv) { double period = 8.0; time_point start = steady_clock::now(); - constexpr int timeout = 300ms; + constexpr milliseconds timeout = 300ms; DataPoint starting_angle_data_point = can::motor::getMotorPosition(serial); - while (!starting_angle_data_point.isValid() && - steady_clock::now() - start < milliseconds(timeout)) { + while (!starting_angle_data_point.isValid() && steady_clock::now() - start < timeout) { starting_angle_data_point = can::motor::getMotorPosition(serial); } From 69dce121f2c58292c0669283100be8680a6ea2c2 Mon Sep 17 00:00:00 2001 From: Abhinav Goyal Date: Sat, 16 Nov 2024 11:59:25 -0800 Subject: [PATCH 6/7] Make code exit if starting angle not found --- src/TunePID.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/TunePID.cpp b/src/TunePID.cpp index d6651aef..6791bd9a 100644 --- a/src/TunePID.cpp +++ b/src/TunePID.cpp @@ -148,8 +148,8 @@ int main(int argc, char** argv) { if (!starting_angle_data_point.isValid()) { LOG_F(WARNING, "STARTING ANGLE DATA NOT FOUND"); + exit(1); } - // throws bad_datapoint_access if the data point is not valid int32_t starting_angle = starting_angle_data_point.getData(); int32_t angle_target = starting_angle; From 08fda94c6921b61d30cae7780fbbbe8811629d45 Mon Sep 17 00:00:00 2001 From: Abhinav Goyal Date: Mon, 18 Nov 2024 17:13:15 -0800 Subject: [PATCH 7/7] Make log message Error instead of Warning if TunePID can't find starting angle --- src/TunePID.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/TunePID.cpp b/src/TunePID.cpp index 6791bd9a..6edb16dc 100644 --- a/src/TunePID.cpp +++ b/src/TunePID.cpp @@ -147,7 +147,7 @@ int main(int argc, char** argv) { } if (!starting_angle_data_point.isValid()) { - LOG_F(WARNING, "STARTING ANGLE DATA NOT FOUND"); + LOG_F(ERROR, "STARTING ANGLE DATA NOT FOUND"); exit(1); } int32_t starting_angle = starting_angle_data_point.getData();