Skip to content

Commit

Permalink
Merge branch 'cg_calcs' into temp_cg_calcs_merge
Browse files Browse the repository at this point in the history
  • Loading branch information
VyaasBaskar authored Jan 16, 2025
2 parents 66d26f6 + bb17e13 commit 40f081a
Show file tree
Hide file tree
Showing 15 changed files with 431 additions and 15 deletions.
8 changes: 8 additions & 0 deletions networktables.json
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,14 @@
"persistent": true
}
},
{
"name": "/Preferences/Robot/Robot/permissible_current_draw (A)",
"type": "double",
"value": 300.0,
"properties": {
"persistent": true
}
},
{
"name": "/Preferences/SwerveDrivetrain/steer_gains/_kP",
"type": "double",
Expand Down
80 changes: 80 additions & 0 deletions networktables.json.bck
Original file line number Diff line number Diff line change
Expand Up @@ -182,5 +182,85 @@
"properties": {
"persistent": true
}
},
{
"name": "/Preferences/Robot/Robot/permissible_current_draw (A)",
"type": "double",
"value": 300.0,
"properties": {
"persistent": true
}
},
{
"name": "/Preferences/SwerveDrivetrain/steer_gains/_kP",
"type": "double",
"value": 2.0,
"properties": {
"persistent": true
}
},
{
"name": "/Preferences/SwerveDrivetrain/steer_gains/_kI",
"type": "double",
"value": 0.0,
"properties": {
"persistent": true
}
},
{
"name": "/Preferences/SwerveDrivetrain/steer_gains/_kD",
"type": "double",
"value": 0.0,
"properties": {
"persistent": true
}
},
{
"name": "/Preferences/SwerveDrivetrain/steer_gains/_kF",
"type": "double",
"value": 0.0,
"properties": {
"persistent": true
}
},
{
"name": "/Preferences/SwerveDrivetrain/odom_fudge_factor",
"type": "double",
"value": 0.875,
"properties": {
"persistent": true
}
},
{
"name": "/Preferences/MotorMonkey/MotorMonkey/voltage_min (V)",
"type": "double",
"value": 7.5,
"properties": {
"persistent": true
}
},
{
"name": "/Preferences/MotorMonkey/MotorMonkey/recal_voltage_thresh (V)",
"type": "double",
"value": 9.5,
"properties": {
"persistent": true
}
},
{
"name": "/Preferences/MotorMonkey/MotorMonkey/default_max_draw (A)",
"type": "double",
"value": 150.0,
"properties": {
"persistent": true
}
},
{
"name": "/Preferences/MotorMonkey/MotorMonkey/battery_cc (A)",
"type": "double",
"value": 600.0,
"properties": {
"persistent": true
}
}
]
19 changes: 19 additions & 0 deletions simgui.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,25 @@
"/SmartDashboard/zero_bearing": "Command"
}
},
"NetworkTables": {
"transitory": {
"Shuffleboard": {
".metadata": {
"open": true
},
"open": true
},
"SmartDashboard": {
"open": true,
"verify_hardware": {
"open": true
},
"zero_bearing": {
"open": true
}
}
}
},
"NetworkTables Info": {
"Connections": {
"open": true
Expand Down
37 changes: 37 additions & 0 deletions src/frc846/cpp/frc846/math/RampRateLimiter.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include "frc846/math/RampRateLimiter.h"

namespace frc846::math {

RampRateLimiter::RampRateLimiter() {
m_lastTime = std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::system_clock::now().time_since_epoch());
}

double RampRateLimiter::limit(double value, double rateLimit) {
auto currentTime = std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::system_clock::now().time_since_epoch());
auto elapsedTime = currentTime - m_lastTime;
m_lastTime = currentTime;

double maxChange = rateLimit * elapsedTime.count() / 1000.0;
double change = value - m_lastValue;
if (change > maxChange) {
value = m_lastValue + maxChange;
} else if (change < -maxChange) {
value = m_lastValue - maxChange;
}

m_lastValue = value;
return value;
}

double RampRateLimiter::findRate(double value) {
auto currentTime = std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::system_clock::now().time_since_epoch());
auto elapsedTime = currentTime - m_lastTime;
m_lastTime = currentTime;

return (value - m_lastValue) / (elapsedTime.count() / 1000.0);
}

} // namespace frc846::math
4 changes: 2 additions & 2 deletions src/frc846/cpp/frc846/robot/xbox.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ XboxReadings::XboxReadings(frc::XboxController& xbox, double trigger_threshold)
right_stick_y(-xbox.GetRightY()), // negated so + is up
left_trigger(xbox.GetLeftTriggerAxis() >= trigger_threshold),
right_trigger(xbox.GetRightTriggerAxis() >= trigger_threshold),
left_bumper(xbox.GetLeftBumper()),
right_bumper(xbox.GetRightBumper()),
left_bumper(xbox.GetLeftBumperButton()),
right_bumper(xbox.GetRightBumperButton()),
back_button(xbox.GetBackButton()),
start_button(xbox.GetStartButton()),
rsb(xbox.GetRightStickButton()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class IntermediateController {

virtual ControllerErrorCodes GetLastErrorCode() = 0;

virtual bool VerifyConnected() = 0; // changed
virtual bool VerifyConnected() = 0;
};

} // namespace frc846::control::hardware
25 changes: 25 additions & 0 deletions src/frc846/include/frc846/math/RampRateLimiter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#pragma once

#include <chrono>

namespace frc846::math {

/*
RampRateLimiter
A class that limits the rate of change of a value.
*/
class RampRateLimiter {
public:
RampRateLimiter();

double limit(double value, double rateLimit);

double findRate(double value);

private:
double m_lastValue = 0.0;
std::chrono::milliseconds m_lastTime;
};

}
28 changes: 19 additions & 9 deletions src/frc846/include/frc846/math/vectors.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,18 +147,22 @@ template <typename UT, size_t N> class VectorND {
data[0] * units::math::sin(angle) + data[1] * units::math::cos(angle)};
}

// Returns the dot product of this vector and another
T dot(const VectorND<UT, N>& other) const {
T result = T{};
// Returns the dot product of this vector and another with the units of the
// other vector
template <typename UT2>
units::unit_t<UT2> dot(const VectorND<UT2, N>& other) const {
units::unit_t<UT2> result{};
for (size_t i = 0; i < N; ++i) {
result += data[i] * other[i].template to<double>();
result += data[i].template to<double>() * other[i];
}
return result;
}

// Returns the cross product of this vector and another
// Cross product is only defined for 3D vectors
VectorND<UT, N> cross(const VectorND<UT, N>& other) const {
template <typename UT2>
VectorND<units::compound_unit<UT, UT2>, N> cross(
const VectorND<UT2, N>& other) const {
static_assert(N == 3, "Cross product is only defined for 3D vectors.");
return {data[1] * other[2] - data[2] * other[1],
data[2] * other[0] - data[0] * other[2],
Expand All @@ -180,13 +184,18 @@ template <typename UT, size_t N> class VectorND {
}

// Projects this vector onto another and returns
VectorND<UT, N> projectOntoAnother(const VectorND<UT, N>& other) const {
template <typename UT2>
VectorND<UT, N> projectOntoAnother(const VectorND<UT2, N>& other) const {
return other.projectOntoThis(*this);
}

// Projects another vector onto this and returns
VectorND<UT, N> projectOntoThis(const VectorND<UT, N>& other) const {
return unit() * dot(other).template to<double>();
template <typename UT2>
VectorND<UT2, N> projectOntoThis(const VectorND<UT2, N>& other) const {
assert(N == 2 && "Projection is only defined for 2D vectors.");
double unit_x = unit()[0].template to<double>();
double unit_y = unit()[1].template to<double>();
return {unit_x * dot(other), unit_y * dot(other)};
}

// Returns the angle of this vector
Expand All @@ -201,8 +210,9 @@ template <typename UT, size_t N> class VectorND {
}

// Returns the angle between this vector and another
template <typename UT2>
units::degree_t angleTo(
const VectorND<UT, N>& other, bool angleIsBearing = false) const {
const VectorND<UT2, N>& other, bool angleIsBearing = false) const {
return other.angle(angleIsBearing) - angle(angleIsBearing);
}

Expand Down
3 changes: 2 additions & 1 deletion src/frc846/include/frc846/robot/GenericCommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ class GenericCommandGroup
private:
frc2::InstantCommand end_command_addition{[&] {
Log("Command group ending. Took {} ms to complete.",
(frc846::wpilib::CurrentFPGATime() - command_start_time_).to<double>());
(frc846::wpilib::CurrentFPGATime() - command_start_time_)
.template to<double>());
}};

frc2::InstantCommand start_command_addition{[&] {
Expand Down
111 changes: 111 additions & 0 deletions src/y2025/cpp/calculators/AntiTippingCalculator.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
#include "calculators/AntiTippingCalculator.h"

#include <units/force.h>
#include <units/torque.h>

#include "frc846/math/constants.h"
#include "subsystems/robot_constants.h"

frc846::math::Vector3D AntiTippingCalculator::elev_cg_position_{
robot_constants::elevator::pos_x, robot_constants::elevator::pos_y,
robot_constants::base::height};
frc846::math::Vector3D AntiTippingCalculator::tele_cg_position_{
robot_constants::telescope::pos_x, robot_constants::telescope::pos_y,
robot_constants::base::height};

void AntiTippingCalculator::SetElevatorHeight(units::inch_t height) {
if (height > robot_constants::elevator::min_height_off_base) {
elev_cg_position_[2] = height / 2.0;
} else {
elev_cg_position_[2] =
((robot_constants::elevator::elevator_weight *
robot_constants::elevator::min_height_off_base / 2.0) +
(robot_constants::elevator::end_effector_weight * height)) /
(robot_constants::elevator::elevator_weight +
robot_constants::elevator::end_effector_weight);
}
}

void AntiTippingCalculator::SetTelescopeHeight(units::inch_t height) {
tele_cg_position_[2] = height / 2.0;
}

frc846::math::Vector3D AntiTippingCalculator::CalculateRobotCG() {
double total_weight_scalar = robot_constants::total_weight.to<double>();

frc846::math::Vector3D weighted_sum =
(elev_cg_position_ *
robot_constants::elevator::total_weight.to<double>() +
tele_cg_position_ *
robot_constants::telescope::total_weight.to<double>() +
frc846::math::Vector3D{0_in, 0_in, robot_constants::base::height} *
robot_constants::base::weight.to<double>());

return weighted_sum / total_weight_scalar;
}

frc846::math::VectorND<units::feet_per_second_squared, 2>
AntiTippingCalculator::LimitAcceleration(
frc846::math::VectorND<units::feet_per_second_squared, 2> accel,
units::degree_t bearing) {
frc846::math::VectorND<units::feet_per_second_squared, 2> accel_dir =
accel.unit();

frc846::math::Vector3D robot_cg = CalculateRobotCG();

std::pair<double, double> kModuleLocationSigns[4] = {
{+0.5, +0.5}, // FR
{-0.5, +0.5}, // FL
{-0.5, -0.5}, // BL
{+0.5, -0.5}, // BR
};

frc846::math::Vector2D wheel_vecs[4];

for (size_t i = 0; i < 4; i++) {
wheel_vecs[i] =
frc846::math::Vector2D{
kModuleLocationSigns[i].first * robot_constants::base::wheelbase_x,
kModuleLocationSigns[i].second * robot_constants::base::wheelbase_y}
.rotate(bearing, true);
}

size_t closest_wheel_vec = 0U;
units::degree_t closest_angle = 360_deg;

for (size_t i = 0; i < 4; i++) {
units::degree_t angle = units::math::abs(accel_dir.angleTo(wheel_vecs[i]));
if (angle < closest_angle) {
closest_angle = angle;
closest_wheel_vec = i;
}
}

frc846::math::Vector2D closest_wheel = wheel_vecs[closest_wheel_vec];

frc846::math::Vector2D effective_wheel_vec =
accel_dir.projectOntoThis<units::inch>(closest_wheel);
frc846::math::Vector3D effective_wheel_vec_3d{
effective_wheel_vec[0], effective_wheel_vec[1], 0_in};

frc846::math::Vector3D r_vec = robot_cg - effective_wheel_vec_3d;
frc846::math::VectorND<units::newtons, 3> grav_vec{0_N, 0_N,
-frc846::math::constants::physics::g * robot_constants::total_weight};

units::newton_meter_t torque_grav = r_vec.cross(grav_vec).magnitude();

// TODO: Check all signs

units::newton_meter_t torque_accel =
r_vec
.cross(frc846::math::VectorND<units::newtons, 3>{
accel[0] * robot_constants::total_weight,
accel[1] * robot_constants::total_weight, 0_N})
.magnitude();

if (torque_accel < torque_grav || torque_accel == 0.0_Nm) { return accel; }

double rescale_factor = torque_grav / torque_accel;

return accel * rescale_factor;
}
Loading

0 comments on commit 40f081a

Please sign in to comment.