diff --git a/include/drivetrain.hpp b/include/drivetrain.hpp index 1f0722d..66dbff5 100644 --- a/include/drivetrain.hpp +++ b/include/drivetrain.hpp @@ -345,7 +345,7 @@ class drivetrain { * * @param[in] distance Distance to travel in meters (positive = forward, negative = backward) */ - void linear_mp(const float distance); + void linear_mp(const float distance, bool log = false); /** * @brief Executes an angular motion profile to rotate a specified angle @@ -357,7 +357,7 @@ class drivetrain { * * @param[in] angle Angle to rotate in radians (positive = counterclockwise, negative = clockwise) */ - void angular_mp(const float angle); + void angular_mp(const float angle, bool log = false); /** * @brief Moves to a target pose using motion profiles diff --git a/include/logger.hpp b/include/logger.hpp new file mode 100644 index 0000000..4f69d00 --- /dev/null +++ b/include/logger.hpp @@ -0,0 +1,3 @@ +#include "api.h" + +void print_vector(const std::vector& vector, const char* name); diff --git a/src/drivetrain.cpp b/src/drivetrain.cpp index 7373af9..b8c04ba 100644 --- a/src/drivetrain.cpp +++ b/src/drivetrain.cpp @@ -9,6 +9,7 @@ #include "drivetrain.hpp" #include "helper-functions.hpp" #include "structs.hpp" +#include "logger.hpp" drivetrain::drivetrain(const wheels>& motors_, const float wheelbase_length_, @@ -201,41 +202,60 @@ void drivetrain::move_differential_robot_vels_ramsete(std::vector> linear_velocities; + if (log) { + linear_velocities.emplace(); + linear_velocities->reserve(500); + } + while(!LinearMP.profileFinished(time) || start) { float linear_velocity = LinearMP.velocity(time, distance); + if (linear_velocities) { + linear_velocities->push_back(linear_velocity); + } std::vector desired_differential_vel = {{linear_velocity, 0.f}}; move_differential_robot_vels(desired_differential_vel); pros::delay(static_cast(timestep * 1000.f)); time += timestep; start = false; } - motor_brakes(); -} -void print_vector(const std::vector& vector, const char* name) { - printf("%s = [", name); - for (size_t i = 0; i < vector.size(); ++i) { - printf("%.4f", vector[i]); - if (i + 1 < vector.size()) printf(","); - } - printf("]\n"); + if (linear_velocities) { + print_vector(*linear_velocities, "L"); + } + + motor_brakes(); } -void drivetrain::angular_mp(const float angle) { +void drivetrain::angular_mp(const float angle, bool log) { float time = 0.f; bool start = true; + std::optional> angular_velocities; + if (log) { + angular_velocities.emplace(); + angular_velocities->reserve(500); + } + while(!AngularMP.profileFinished(time) || start) { float angular_velocity = AngularMP.velocity(time, angle); + if (angular_velocities) { + angular_velocities->push_back(angular_velocity); + } std::vector desired_differential_vel = {{0.f, angular_velocity}}; move_differential_robot_vels(desired_differential_vel); pros::delay(timestep * 1000.f); time += timestep; start = false; } - motor_brakes(); + + if (angular_velocities) { + print_vector(*angular_velocities, "L"); + } + + motor_brakes(); } void drivetrain::calculate_and_print_motor_constants() { diff --git a/src/logger.cpp b/src/logger.cpp new file mode 100644 index 0000000..d5e135c --- /dev/null +++ b/src/logger.cpp @@ -0,0 +1,10 @@ +#include "logger.hpp" + +void print_vector(const std::vector& vector, const char* name) { + printf("%s = [", name); + for (size_t i = 0; i < vector.size(); ++i) { + printf("%.4f", vector[i]); + if (i + 1 < vector.size()) printf(","); + } + printf("]\n"); +}