CTRE Phoenix 6 C++ 26.0.0-beta-1
Loading...
Searching...
No Matches
WheelForceCalculator.hpp
Go to the documentation of this file.
1/*
2 * Copyright (C) Cross The Road Electronics.  All rights reserved.
3 * License information can be found in CTRE_LICENSE.txt
4 * For support and suggestions contact support@ctr-electronics.com or file
5 * an issue tracker at https://github.com/CrossTheRoadElec/Phoenix-Releases
6 */
7#pragma once
8
10#include "units/angular_acceleration.h"
11#include "units/force.h"
12#include "units/moment_of_inertia.h"
13#include "units/torque.h"
14#include <vector>
15
16namespace ctre {
17namespace phoenix6 {
18namespace swerve {
19
20/**
21 * Calculates wheel force feedforwards for a SwerveDrivetrain.
22 * Wheel force feedforwards can be used to improve accuracy of path
23 * following in regions of acceleration.
24 *
25 * Many path planning libraries (such as PathPlanner and Choreo) already
26 * provide per-module force feedforwards, which should be preferred over
27 * this class.
28 */
30public:
31 /** \brief Wheel force feedforwards to apply to a swerve drivetrain. */
32 struct Feedforwards {
33 /** \brief The X component of the forces in Newtons. */
34 std::vector<units::newton_t> x;
35 /** \brief The Y component of the forces in Newtons. */
36 std::vector<units::newton_t> y;
37
38 /**
39 * \brief Constructs a new set of wheel force feedforwards.
40 *
41 * \param numModules The number of swerve modules
42 */
43 Feedforwards(size_t numModules) :
44 x(numModules),
45 y(numModules)
46 {}
47 };
48
49private:
50 std::vector<Translation2d> moduleLocations;
51 units::kilogram_t mass;
52 units::kilogram_square_meter_t moi;
53
54public:
55 /**
56 * \brief Constructs a new wheel force feedforward calculator for the given
57 * drivetrain characteristics.
58 *
59 * \param moduleLocations The drivetrain swerve module locations
60 * \param mass The mass of the robot
61 * \param moi The moment of inertia of the robot
62 */
64 std::vector<Translation2d> moduleLocations,
65 units::kilogram_t mass,
66 units::kilogram_square_meter_t moi
67 ) :
68 moduleLocations{std::move(moduleLocations)},
69 mass{mass},
70 moi{moi}
71 {}
72
73 /**
74 * \brief Calculates wheel force feedforwards for the desired robot acceleration.
75 * This can be used with either robot-centric or field-centric accelerations;
76 * the returned force feedforwards will match in being robot-/field-centric.
77 *
78 * \param ax Acceleration in the X direction (forward) in m/s^2
79 * \param ay Acceleration in the Y direction (left) in m/s^2
80 * \param alpha Angular acceleration in rad/s^2
81 * \param centerOfRotation Center of rotation
82 * \returns Wheel force feedforwards to apply
83 */
85 units::meters_per_second_squared_t ax,
86 units::meters_per_second_squared_t ay,
87 units::radians_per_second_squared_t alpha,
88 Translation2d const &centerOfRotation = {}
89 ) const {
90 units::newton_t const fx = ax * mass;
91 units::newton_t const fy = ay * mass;
92 units::newton_meter_t const tau = alpha * moi / 1_rad;
93
94 Feedforwards feedforwards{moduleLocations.size()};
95 for (size_t i = 0; i < moduleLocations.size(); ++i) {
96 Translation2d const r = moduleLocations[i] - centerOfRotation;
97
98 Translation2d const f_tau{tau / r.Norm() * 1_m/1_N, r.Angle() + Rotation2d{90_deg}};
99 feedforwards.x[i] = (fx + f_tau.X() * 1_N/1_m) / moduleLocations.size();
100 feedforwards.y[i] = (fy + f_tau.Y() * 1_N/1_m) / moduleLocations.size();
101 }
102
103 return feedforwards;
104 }
105
106 /**
107 * \brief Calculates wheel force feedforwards for the desired change in speeds.
108 * This can be used with either robot-centric or field-centric speeds; the
109 * returned force feedforwards will match in being robot-/field-centric.
110 *
111 * \param dt The change in time between the path setpoints
112 * \param prev The previous ChassisSpeeds setpoint of the path
113 * \param current The new ChassisSpeeds setpoint of the path
114 * \param centerOfRotation Center of rotation
115 * \returns Wheel force feedforwards to apply
116 */
118 units::second_t dt,
119 ChassisSpeeds const &prev,
120 ChassisSpeeds const &current,
121 Translation2d const &centerOfRotation = {}
122 ) const {
123 auto const ax = (current.vx - prev.vx) / dt;
124 auto const ay = (current.vy - prev.vy) / dt;
125 auto const alpha = (current.omega - prev.omega) / dt;
126 return Calculate(ax, ay, alpha, centerOfRotation);
127 }
128};
129
130}
131}
132}
Calculates wheel force feedforwards for a SwerveDrivetrain.
Definition WheelForceCalculator.hpp:29
Feedforwards Calculate(units::second_t dt, ChassisSpeeds const &prev, ChassisSpeeds const &current, Translation2d const &centerOfRotation={}) const
Calculates wheel force feedforwards for the desired change in speeds.
Definition WheelForceCalculator.hpp:117
WheelForceCalculator(std::vector< Translation2d > moduleLocations, units::kilogram_t mass, units::kilogram_square_meter_t moi)
Constructs a new wheel force feedforward calculator for the given drivetrain characteristics.
Definition WheelForceCalculator.hpp:63
Feedforwards Calculate(units::meters_per_second_squared_t ax, units::meters_per_second_squared_t ay, units::radians_per_second_squared_t alpha, Translation2d const &centerOfRotation={}) const
Calculates wheel force feedforwards for the desired robot acceleration.
Definition WheelForceCalculator.hpp:84
Definition motor_constants.h:14
Wheel force feedforwards to apply to a swerve drivetrain.
Definition WheelForceCalculator.hpp:32
std::vector< units::newton_t > x
The X component of the forces in Newtons.
Definition WheelForceCalculator.hpp:34
std::vector< units::newton_t > y
The Y component of the forces in Newtons.
Definition WheelForceCalculator.hpp:36
Feedforwards(size_t numModules)
Constructs a new set of wheel force feedforwards.
Definition WheelForceCalculator.hpp:43