WPILibC++ 2023.4.3-108-ge5452e3
SimpleMotorFeedforward.h
Go to the documentation of this file.
1// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5#pragma once
6
7#include <wpi/MathExtras.h>
8
9#include "frc/EigenCore.h"
12#include "units/time.h"
13#include "units/voltage.h"
14
15namespace frc {
16/**
17 * A helper class that computes feedforward voltages for a simple
18 * permanent-magnet DC motor.
19 */
20template <class Distance>
22 public:
23 using Velocity =
28 using ka_unit =
30
31 constexpr SimpleMotorFeedforward() = default;
32
33 /**
34 * Creates a new SimpleMotorFeedforward with the specified gains.
35 *
36 * @param kS The static gain, in volts.
37 * @param kV The velocity gain, in volt seconds per distance.
38 * @param kA The acceleration gain, in volt secondsĀ² per distance.
39 */
41 units::volt_t kS, units::unit_t<kv_unit> kV,
43 : kS(kS), kV(kV), kA(kA) {}
44
45 /**
46 * Calculates the feedforward from the gains and setpoints.
47 *
48 * @param velocity The velocity setpoint, in distance per second.
49 * @param acceleration The acceleration setpoint, in distance per secondĀ².
50 * @return The computed feedforward, in volts.
51 */
52 constexpr units::volt_t Calculate(units::unit_t<Velocity> velocity,
53 units::unit_t<Acceleration> acceleration =
55 return kS * wpi::sgn(velocity) + kV * velocity + kA * acceleration;
56 }
57
58 /**
59 * Calculates the feedforward from the gains and setpoints.
60 *
61 * @param currentVelocity The current velocity setpoint, in distance per
62 * second.
63 * @param nextVelocity The next velocity setpoint, in distance per second.
64 * @param dt Time between velocity setpoints in seconds.
65 * @return The computed feedforward, in volts.
66 */
67 units::volt_t Calculate(units::unit_t<Velocity> currentVelocity,
68 units::unit_t<Velocity> nextVelocity,
69 units::second_t dt) const {
70 auto plant = LinearSystemId::IdentifyVelocitySystem<Distance>(kV, kA);
71 LinearPlantInversionFeedforward<1, 1> feedforward{plant, dt};
72
73 Vectord<1> r{currentVelocity.value()};
74 Vectord<1> nextR{nextVelocity.value()};
75
76 return kS * wpi::sgn(currentVelocity.value()) +
77 units::volt_t{feedforward.Calculate(r, nextR)(0)};
78 }
79
80 // Rearranging the main equation from the calculate() method yields the
81 // formulas for the methods below:
82
83 /**
84 * Calculates the maximum achievable velocity given a maximum voltage supply
85 * and an acceleration. Useful for ensuring that velocity and
86 * acceleration constraints for a trapezoidal profile are simultaneously
87 * achievable - enter the acceleration constraint, and this will give you
88 * a simultaneously-achievable velocity constraint.
89 *
90 * @param maxVoltage The maximum voltage that can be supplied to the motor.
91 * @param acceleration The acceleration of the motor.
92 * @return The maximum possible velocity at the given acceleration.
93 */
95 units::volt_t maxVoltage,
96 units::unit_t<Acceleration> acceleration) const {
97 // Assume max velocity is positive
98 return (maxVoltage - kS - kA * acceleration) / kV;
99 }
100
101 /**
102 * Calculates the minimum achievable velocity given a maximum voltage supply
103 * and an acceleration. Useful for ensuring that velocity and
104 * acceleration constraints for a trapezoidal profile are simultaneously
105 * achievable - enter the acceleration constraint, and this will give you
106 * a simultaneously-achievable velocity constraint.
107 *
108 * @param maxVoltage The maximum voltage that can be supplied to the motor.
109 * @param acceleration The acceleration of the motor.
110 * @return The minimum possible velocity at the given acceleration.
111 */
113 units::volt_t maxVoltage,
114 units::unit_t<Acceleration> acceleration) const {
115 // Assume min velocity is positive, ks flips sign
116 return (-maxVoltage + kS - kA * acceleration) / kV;
117 }
118
119 /**
120 * Calculates the maximum achievable acceleration given a maximum voltage
121 * supply and a velocity. Useful for ensuring that velocity and
122 * acceleration constraints for a trapezoidal profile are simultaneously
123 * achievable - enter the velocity constraint, and this will give you
124 * a simultaneously-achievable acceleration constraint.
125 *
126 * @param maxVoltage The maximum voltage that can be supplied to the motor.
127 * @param velocity The velocity of the motor.
128 * @return The maximum possible acceleration at the given velocity.
129 */
131 units::volt_t maxVoltage, units::unit_t<Velocity> velocity) const {
132 return (maxVoltage - kS * wpi::sgn(velocity) - kV * velocity) / kA;
133 }
134
135 /**
136 * Calculates the minimum achievable acceleration given a maximum voltage
137 * supply and a velocity. Useful for ensuring that velocity and
138 * acceleration constraints for a trapezoidal profile are simultaneously
139 * achievable - enter the velocity constraint, and this will give you
140 * a simultaneously-achievable acceleration constraint.
141 *
142 * @param maxVoltage The maximum voltage that can be supplied to the motor.
143 * @param velocity The velocity of the motor.
144 * @return The minimum possible acceleration at the given velocity.
145 */
147 units::volt_t maxVoltage, units::unit_t<Velocity> velocity) const {
148 return MaxAchievableAcceleration(-maxVoltage, velocity);
149 }
150
151 units::volt_t kS{0};
154};
155} // namespace frc
Constructs a plant inversion model-based feedforward from a LinearSystem.
Definition: LinearPlantInversionFeedforward.h:32
A helper class that computes feedforward voltages for a simple permanent-magnet DC motor.
Definition: SimpleMotorFeedforward.h:21
constexpr units::unit_t< Velocity > MinAchievableVelocity(units::volt_t maxVoltage, units::unit_t< Acceleration > acceleration) const
Calculates the minimum achievable velocity given a maximum voltage supply and an acceleration.
Definition: SimpleMotorFeedforward.h:112
units::volt_t kS
Definition: SimpleMotorFeedforward.h:151
units::volt_t Calculate(units::unit_t< Velocity > currentVelocity, units::unit_t< Velocity > nextVelocity, units::second_t dt) const
Calculates the feedforward from the gains and setpoints.
Definition: SimpleMotorFeedforward.h:67
units::compound_unit< Distance, units::inverse< units::seconds > > Velocity
Definition: SimpleMotorFeedforward.h:24
units::compound_unit< units::volts, units::inverse< Acceleration > > ka_unit
Definition: SimpleMotorFeedforward.h:29
units::unit_t< kv_unit > kV
Definition: SimpleMotorFeedforward.h:152
units::unit_t< ka_unit > kA
Definition: SimpleMotorFeedforward.h:153
constexpr units::volt_t Calculate(units::unit_t< Velocity > velocity, units::unit_t< Acceleration > acceleration=units::unit_t< Acceleration >(0)) const
Calculates the feedforward from the gains and setpoints.
Definition: SimpleMotorFeedforward.h:52
constexpr units::unit_t< Acceleration > MaxAchievableAcceleration(units::volt_t maxVoltage, units::unit_t< Velocity > velocity) const
Calculates the maximum achievable acceleration given a maximum voltage supply and a velocity.
Definition: SimpleMotorFeedforward.h:130
units::compound_unit< units::volts, units::inverse< Velocity > > kv_unit
Definition: SimpleMotorFeedforward.h:27
units::compound_unit< Velocity, units::inverse< units::seconds > > Acceleration
Definition: SimpleMotorFeedforward.h:26
constexpr units::unit_t< Acceleration > MinAchievableAcceleration(units::volt_t maxVoltage, units::unit_t< Velocity > velocity) const
Calculates the minimum achievable acceleration given a maximum voltage supply and a velocity.
Definition: SimpleMotorFeedforward.h:146
constexpr SimpleMotorFeedforward(units::volt_t kS, units::unit_t< kv_unit > kV, units::unit_t< ka_unit > kA=units::unit_t< ka_unit >(0))
Creates a new SimpleMotorFeedforward with the specified gains.
Definition: SimpleMotorFeedforward.h:40
constexpr units::unit_t< Velocity > MaxAchievableVelocity(units::volt_t maxVoltage, units::unit_t< Acceleration > acceleration) const
Calculates the maximum achievable velocity given a maximum voltage supply and an acceleration.
Definition: SimpleMotorFeedforward.h:94
constexpr SimpleMotorFeedforward()=default
constexpr underlying_type value() const noexcept
unit value
Definition: base.h:2118
typename units::detail::compound_impl< U, Us... >::type compound_unit
Represents a unit type made up from other units.
Definition: base.h:1445
Definition: AprilTagPoseEstimator.h:15
Eigen::Vector< double, Size > Vectord
Definition: EigenCore.h:12
constexpr int sgn(T val)
Definition: MathExtras.h:936