001/*
002 * Copyright (C) Cross The Road Electronics.  All rights reserved.
003 * License information can be found in CTRE_LICENSE.txt
004 * For support and suggestions contact support@ctr-electronics.com or file
005 * an issue tracker at https://github.com/CrossTheRoadElec/Phoenix-Releases
006 */
007package com.ctre.phoenix6.swerve.utility;
008
009import static edu.wpi.first.units.Units.*;
010
011import com.ctre.phoenix6.swerve.SwerveDrivetrain;
012
013import edu.wpi.first.math.geometry.Rotation2d;
014import edu.wpi.first.math.geometry.Translation2d;
015import edu.wpi.first.math.kinematics.ChassisSpeeds;
016import edu.wpi.first.units.measure.*;
017
018/**
019 * Calculates wheel force feedforwards for a {@link SwerveDrivetrain}.
020 * Wheel force feedforwards can be used to improve accuracy of path
021 * following in regions of acceleration.
022 * <p>
023 * Many path planning libraries (such as PathPlanner and Choreo) already
024 * provide per-module force feedforwards, which should be preferred over
025 * this class.
026 */
027public class WheelForceCalculator {
028    /** Wheel force feedforwards to apply to a swerve drivetrain. */
029    public static class Feedforwards {
030        /** The X component of the forces in Newtons. */
031        public final double[] x_newtons;
032        /** The Y component of the forces in Newtons. */
033        public final double[] y_newtons;
034
035        /**
036         * Constructs a new set of wheel force feedforwards.
037         *
038         * @param numModules The number of swerve modules
039         */
040        public Feedforwards(int numModules) {
041            x_newtons = new double[numModules];
042            y_newtons = new double[numModules];
043        }
044    }
045
046    private final Translation2d[] moduleLocations;
047    private final double mass;
048    private final double moi;
049
050    /**
051     * Constructs a new wheel force feedforward calculator for the given
052     * drivetrain characteristics.
053     *
054     * @param moduleLocations The drivetrain swerve module locations
055     * @param mass_kg The mass of the robot in kg
056     * @param moi_kg_m_sq The moment of inertia of the robot in kg m^2
057     */
058    public WheelForceCalculator(Translation2d[] moduleLocations, double mass_kg, double moi_kg_m_sq) {
059        this.moduleLocations = moduleLocations;
060        this.mass = mass_kg;
061        this.moi = moi_kg_m_sq;
062    }
063
064    /**
065     * Constructs a new wheel force feedforward calculator for the given
066     * drivetrain characteristics.
067     *
068     * @param moduleLocations The drivetrain swerve module locations
069     * @param mass The mass of the robot
070     * @param moi The moment of inertia of the robot
071     */
072    public WheelForceCalculator(Translation2d[] moduleLocations, Mass mass, MomentOfInertia moi) {
073        this(moduleLocations, mass.in(Kilograms), moi.in(KilogramSquareMeters));
074    }
075
076    /**
077     * Calculates wheel force feedforwards for the desired robot acceleration.
078     * This can be used with either robot-centric or field-centric accelerations;
079     * the returned force feedforwards will match in being robot-/field-centric.
080     *
081     * @param ax Acceleration in the X direction (forward) in m/s^2
082     * @param ay Acceleration in the Y direction (left) in m/s^2
083     * @param alpha Angular acceleration in rad/s^2
084     * @return Wheel force feedforwards to apply
085     */
086    public final Feedforwards calculate(double ax, double ay, double alpha) {
087        return calculate(ax, ay, alpha, Translation2d.kZero);
088    }
089
090    /**
091     * Calculates wheel force feedforwards for the desired robot acceleration.
092     * This can be used with either robot-centric or field-centric accelerations;
093     * the returned force feedforwards will match in being robot-/field-centric.
094     *
095     * @param ax Acceleration in the X direction (forward) in m/s^2
096     * @param ay Acceleration in the Y direction (left) in m/s^2
097     * @param alpha Angular acceleration in rad/s^2
098     * @param centerOfRotation Center of rotation
099     * @return Wheel force feedforwards to apply
100     */
101    public final Feedforwards calculate(double ax, double ay, double alpha, Translation2d centerOfRotation) {
102        final double fx = ax * mass;
103        final double fy = ay * mass;
104        final double tau = alpha * moi;
105
106        final var feedforwards = new Feedforwards(moduleLocations.length);
107        for (int i = 0; i < moduleLocations.length; ++i) {
108            final var r = moduleLocations[i].minus(centerOfRotation);
109
110            final var f_tau = new Translation2d(tau / r.getNorm(), r.getAngle().plus(Rotation2d.kCCW_90deg));
111            feedforwards.x_newtons[i] = (fx + f_tau.getX()) / moduleLocations.length;
112            feedforwards.y_newtons[i] = (fy + f_tau.getY()) / moduleLocations.length;
113        }
114
115        return feedforwards;
116    }
117
118    /**
119     * Calculates wheel force feedforwards for the desired robot acceleration.
120     * This can be used with either robot-centric or field-centric accelerations;
121     * the returned force feedforwards will match in being robot-/field-centric.
122     *
123     * @param ax Acceleration in the X direction (forward) in m/s^2
124     * @param ay Acceleration in the Y direction (left) in m/s^2
125     * @param alpha Angular acceleration in rad/s^2
126     * @return Wheel force feedforwards to apply
127     */
128    public final Feedforwards calculate(LinearAcceleration ax, LinearAcceleration ay, AngularAcceleration alpha) {
129        return calculate(
130            ax.in(MetersPerSecondPerSecond),
131            ay.in(MetersPerSecondPerSecond),
132            alpha.in(RadiansPerSecondPerSecond)
133        );
134    }
135
136    /**
137     * Calculates wheel force feedforwards for the desired robot acceleration.
138     * This can be used with either robot-centric or field-centric accelerations;
139     * the returned force feedforwards will match in being robot-/field-centric.
140     *
141     * @param ax Acceleration in the X direction (forward) in m/s^2
142     * @param ay Acceleration in the Y direction (left) in m/s^2
143     * @param alpha Angular acceleration in rad/s^2
144     * @param centerOfRotation Center of rotation
145     * @return Wheel force feedforwards to apply
146     */
147    public final Feedforwards calculate(
148        LinearAcceleration ax,
149        LinearAcceleration ay,
150        AngularAcceleration alpha,
151        Translation2d centerOfRotation
152    ) {
153        return calculate(
154            ax.in(MetersPerSecondPerSecond),
155            ay.in(MetersPerSecondPerSecond),
156            alpha.in(RadiansPerSecondPerSecond),
157            centerOfRotation
158        );
159    }
160
161    /**
162     * Calculates wheel force feedforwards for the desired change in speeds.
163     * This can be used with either robot-centric or field-centric speeds; the
164     * returned force feedforwards will match in being robot-/field-centric.
165     *
166     * @param dt The change in time between the path setpoints
167     * @param prev The previous ChassisSpeeds setpoint of the path
168     * @param current The new ChassisSpeeds setpoint of the path
169     * @return Wheel force feedforwards to apply
170     */
171    public final Feedforwards calculate(double dt, ChassisSpeeds prev, ChassisSpeeds current) {
172        return calculate(dt, prev, current, Translation2d.kZero);
173    }
174
175    /**
176     * Calculates wheel force feedforwards for the desired change in speeds.
177     * This can be used with either robot-centric or field-centric speeds; the
178     * returned force feedforwards will match in being robot-/field-centric.
179     *
180     * @param dt The change in time between the path setpoints
181     * @param prev The previous ChassisSpeeds setpoint of the path
182     * @param current The new ChassisSpeeds setpoint of the path
183     * @param centerOfRotation Center of rotation
184     * @return Wheel force feedforwards to apply
185     */
186    public final Feedforwards calculate(double dt, ChassisSpeeds prev, ChassisSpeeds current, Translation2d centerOfRotation) {
187        final double ax = (current.vxMetersPerSecond - prev.vxMetersPerSecond) / dt;
188        final double ay = (current.vyMetersPerSecond - prev.vyMetersPerSecond) / dt;
189        final double alpha = (current.omegaRadiansPerSecond - prev.omegaRadiansPerSecond) / dt;
190        return calculate(ax, ay, alpha, centerOfRotation);
191    }
192}