Skip to content

Commit

Permalink
Update tunepid and tuned constants (#329)
Browse files Browse the repository at this point in the history
  • Loading branch information
abhaybd authored Jul 3, 2024
1 parent 25665b0 commit 8da8ecb
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
26 changes: 20 additions & 6 deletions src/TunePID.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ extern "C" {
#include "HindsightCAN/CANCommon.h"
}

enum class targetmode_t {
step, sinusoidal
};

using namespace robot::types;
using namespace std::chrono;
using namespace std::chrono_literals;
Expand Down Expand Up @@ -101,6 +105,16 @@ int main(int argc, char** argv) {
motorid_t motor = nameToMotorMap.at(motor_name);
uint8_t serial = robot::motorSerialIDMap.at(motor);

std::cout << "Enter amplitude (deg) > ";
std::string ampl_str;
std::getline(std::cin, ampl_str);
const double amplitude = std::stoi(ampl_str) * 1000.0;

std::cout << "Enter mode (0=step, 1=sinusoidal) > ";
std::string mode_name;
std::getline(std::cin, mode_name);
targetmode_t mode = static_cast<targetmode_t>(std::stoi(mode_name));

if (!initMotor(motor)) {
return 1;
}
Expand All @@ -121,9 +135,6 @@ int main(int argc, char** argv) {
can::motor::setMotorPIDConstants(serial, p_coeff, i_coeff, d_coeff);

double period = 8.0;
double amplitude =
20 * 1000.0; // in 1000th's of degrees
// (doesn't make sense for hand motor, but that doesn't use PID)

std::this_thread::sleep_for(300ms); // wait for encoder position data to arrive
int32_t starting_angle = can::motor::getMotorPosition(serial);
Expand All @@ -134,7 +145,7 @@ int main(int argc, char** argv) {
time_point<steady_clock> tp = steady_clock::now();
time_point<steady_clock> startTime = tp;
while (steady_clock::now() - startTime < 3 * milliseconds((int)(period * 1000))) {
int32_t current_angle = can::motor::getMotorPosition(serial);
int32_t current_angle = can::motor::getMotorPosition(serial).getData();
double difference = (current_angle - angle_target) / 1000.0;
acc_error += difference * difference;
printf("Step %02d: target %05d actual %05d\n", total_steps, angle_target,
Expand All @@ -143,8 +154,11 @@ int main(int argc, char** argv) {

double time =
duration_cast<milliseconds>(steady_clock::now() - startTime).count() / 1000.0;
angle_target =
(int32_t)round(amplitude * sin(2 * M_PI * time / period)) + starting_angle;
double prescaled_target = sin(2 * M_PI * time / period);
if (mode == targetmode_t::step) {
prescaled_target = round(prescaled_target);
}
angle_target = (int32_t) round(amplitude * prescaled_target) + starting_angle;

can::motor::setMotorPIDTarget(serial, angle_target);

Expand Down
6 changes: 3 additions & 3 deletions src/world_interface/real_world_constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,10 @@ constexpr auto motorSerialIDMap = frozen::make_unordered_map<motorid_t, can::dev
constexpr auto motorPIDMap = frozen::make_unordered_map<motorid_t, pidcoef_t>(
{{motorid_t::shoulder, {70, 0, 0}},
{motorid_t::elbow, {15, 7, -2}},
{motorid_t::frontLeftSwerve, {2, 0, -1}},
{motorid_t::frontRightSwerve, {3, 0, -1}},
{motorid_t::frontLeftSwerve, {2, 0, 0}},
{motorid_t::frontRightSwerve, {2, 0, 0}},
{motorid_t::rearLeftSwerve, {0, 0, 0}},
{motorid_t::rearRightSwerve, {2, 0, -1}}});
{motorid_t::rearRightSwerve, {2, 0, 0}}});

/**
* @brief A mapping of motorids to power scale factors when commanded with positive power.
Expand Down

0 comments on commit 8da8ecb

Please sign in to comment.