Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change 300ms time delay to a while loop #341

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions src/TunePID.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -137,8 +139,19 @@ 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<steady_clock> start = steady_clock::now();
constexpr milliseconds timeout = 300ms;
DataPoint<int32_t> starting_angle_data_point = can::motor::getMotorPosition(serial);
while (!starting_angle_data_point.isValid() && steady_clock::now() - start < timeout) {
starting_angle_data_point = can::motor::getMotorPosition(serial);
}

if (!starting_angle_data_point.isValid()) {
LOG_F(ERROR, "STARTING ANGLE DATA NOT FOUND");
exit(1);
}
int32_t starting_angle = starting_angle_data_point.getData();

int32_t angle_target = starting_angle;
double acc_error = 0.0;
int total_steps = 0;
Expand All @@ -159,7 +172,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);

Expand Down