WPILibC++ 2023.4.3-108-ge5452e3
LinearSystemId.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 <concepts>
8#include <stdexcept>
9
10#include <wpi/SymbolExports.h>
11
14#include "units/acceleration.h"
17#include "units/length.h"
19#include "units/velocity.h"
20#include "units/voltage.h"
21
22namespace frc {
24 public:
25 template <typename Distance>
28
29 template <typename Distance>
33
34 /**
35 * Create a state-space model of the elevator system. The states of the system
36 * are [position, velocity], inputs are [voltage], and outputs are [position].
37 *
38 * @param motor The motor (or gearbox) attached to the carriage.
39 * @param mass The mass of the elevator carriage, in kilograms.
40 * @param radius The radius of the elevator's driving drum, in meters.
41 * @param G Gear ratio from motor to carriage.
42 * @throws std::domain_error if mass <= 0, radius <= 0, or G <= 0.
43 */
45 units::kilogram_t mass,
46 units::meter_t radius, double G);
47
48 /**
49 * Create a state-space model of a single-jointed arm system.The states of the
50 * system are [angle, angular velocity], inputs are [voltage], and outputs are
51 * [angle].
52 *
53 * @param motor The motor (or gearbox) attached to the arm.
54 * @param J The moment of inertia J of the arm.
55 * @param G Gear ratio from motor to arm.
56 * @throws std::domain_error if J <= 0 or G <= 0.
57 */
59 DCMotor motor, units::kilogram_square_meter_t J, double G);
60
61 /**
62 * Create a state-space model for a 1 DOF velocity system from its kV
63 * (volts/(unit/sec)) and kA (volts/(unit/sec²)). These constants can be
64 * found using SysId. The states of the system are [velocity], inputs are
65 * [voltage], and outputs are [velocity].
66 *
67 * You MUST use an SI unit (i.e. meters or radians) for the Distance template
68 * argument. You may still use non-SI units (such as feet or inches) for the
69 * actual method arguments; they will automatically be converted to SI
70 * internally.
71 *
72 * The parameters provided by the user are from this feedforward model:
73 *
74 * u = K_v v + K_a a
75 *
76 * @param kV The velocity gain, in volts/(unit/sec).
77 * @param kA The acceleration gain, in volts/(unit/sec²).
78 * @throws std::domain_error if kV <= 0 or kA <= 0.
79 */
80 template <typename Distance>
81 requires std::same_as<units::meter, Distance> ||
82 std::same_as<units::radian, Distance>
84 decltype(1_V / Velocity_t<Distance>(1)) kV,
85 decltype(1_V / Acceleration_t<Distance>(1)) kA) {
86 if (kV <= decltype(kV){0}) {
87 throw std::domain_error("Kv must be greater than zero.");
88 }
89 if (kA <= decltype(kA){0}) {
90 throw std::domain_error("Ka must be greater than zero.");
91 }
92
93 Matrixd<1, 1> A{-kV.value() / kA.value()};
94 Matrixd<1, 1> B{1.0 / kA.value()};
95 Matrixd<1, 1> C{1.0};
96 Matrixd<1, 1> D{0.0};
97
98 return LinearSystem<1, 1, 1>(A, B, C, D);
99 }
100
101 /**
102 * Create a state-space model for a 1 DOF position system from its kV
103 * (volts/(unit/sec)) and kA (volts/(unit/sec²)). These constants can be
104 * found using SysId. the states of the system are [position, velocity],
105 * inputs are [voltage], and outputs are [position].
106 *
107 * You MUST use an SI unit (i.e. meters or radians) for the Distance template
108 * argument. You may still use non-SI units (such as feet or inches) for the
109 * actual method arguments; they will automatically be converted to SI
110 * internally.
111 *
112 * The parameters provided by the user are from this feedforward model:
113 *
114 * u = K_v v + K_a a
115 *
116 * @param kV The velocity gain, in volts/(unit/sec).
117 * @param kA The acceleration gain, in volts/(unit/sec²).
118 *
119 * @throws std::domain_error if kV <= 0 or kA <= 0.
120 */
121 template <typename Distance>
122 requires std::same_as<units::meter, Distance> ||
123 std::same_as<units::radian, Distance>
125 decltype(1_V / Velocity_t<Distance>(1)) kV,
126 decltype(1_V / Acceleration_t<Distance>(1)) kA) {
127 if (kV <= decltype(kV){0}) {
128 throw std::domain_error("Kv must be greater than zero.");
129 }
130 if (kA <= decltype(kA){0}) {
131 throw std::domain_error("Ka must be greater than zero.");
132 }
133
134 Matrixd<2, 2> A{{0.0, 1.0}, {0.0, -kV.value() / kA.value()}};
135 Matrixd<2, 1> B{0.0, 1.0 / kA.value()};
136 Matrixd<1, 2> C{1.0, 0.0};
137 Matrixd<1, 1> D{0.0};
138
139 return LinearSystem<2, 1, 1>(A, B, C, D);
140 }
141
142 /**
143 * Identify a differential drive drivetrain given the drivetrain's kV and kA
144 * in both linear {(volts/(meter/sec), (volts/(meter/sec²))} and angular
145 * {(volts/(radian/sec), (volts/(radian/sec²))} cases. These constants can be
146 * found using SysId.
147 *
148 * States: [[left velocity], [right velocity]]<br>
149 * Inputs: [[left voltage], [right voltage]]<br>
150 * Outputs: [[left velocity], [right velocity]]
151 *
152 * @param kVLinear The linear velocity gain in volts per (meters per second).
153 * @param kALinear The linear acceleration gain in volts per (meters per
154 * second squared).
155 * @param kVAngular The angular velocity gain in volts per (meters per
156 * second).
157 * @param kAAngular The angular acceleration gain in volts per (meters per
158 * second squared).
159 * @throws domain_error if kVLinear <= 0, kALinear <= 0, kVAngular <= 0,
160 * or kAAngular <= 0.
161 */
163 decltype(1_V / 1_mps) kVLinear, decltype(1_V / 1_mps_sq) kALinear,
164 decltype(1_V / 1_mps) kVAngular, decltype(1_V / 1_mps_sq) kAAngular);
165
166 /**
167 * Identify a differential drive drivetrain given the drivetrain's kV and kA
168 * in both linear {(volts/(meter/sec)), (volts/(meter/sec²))} and angular
169 * {(volts/(radian/sec)), (volts/(radian/sec²))} cases. This can be found
170 * using SysId.
171 *
172 * States: [[left velocity], [right velocity]]<br>
173 * Inputs: [[left voltage], [right voltage]]<br>
174 * Outputs: [[left velocity], [right velocity]]
175 *
176 * @param kVLinear The linear velocity gain in volts per (meters per
177 * second).
178 * @param kALinear The linear acceleration gain in volts per (meters per
179 * second squared).
180 * @param kVAngular The angular velocity gain in volts per (radians per
181 * second).
182 * @param kAAngular The angular acceleration gain in volts per (radians per
183 * second squared).
184 * @param trackwidth The distance between the differential drive's left and
185 * right wheels, in meters.
186 * @throws domain_error if kVLinear <= 0, kALinear <= 0, kVAngular <= 0,
187 * kAAngular <= 0, or trackwidth <= 0.
188 */
190 decltype(1_V / 1_mps) kVLinear, decltype(1_V / 1_mps_sq) kALinear,
191 decltype(1_V / 1_rad_per_s) kVAngular,
192 decltype(1_V / 1_rad_per_s_sq) kAAngular, units::meter_t trackwidth);
193
194 /**
195 * Create a state-space model of a flywheel system, the states of the system
196 * are [angular velocity], inputs are [voltage], and outputs are [angular
197 * velocity].
198 *
199 * @param motor The motor (or gearbox) attached to the flywheel.
200 * @param J The moment of inertia J of the flywheel.
201 * @param G Gear ratio from motor to flywheel.
202 * @throws std::domain_error if J <= 0 or G <= 0.
203 */
205 units::kilogram_square_meter_t J,
206 double G);
207
208 /**
209 * Create a state-space model of a DC motor system. The states of the system
210 * are [angular position, angular velocity], inputs are [voltage], and outputs
211 * are [angular position, angular velocity].
212 *
213 * @param motor The motor (or gearbox) attached to the system.
214 * @param J the moment of inertia J of the DC motor.
215 * @param G Gear ratio from motor to output.
216 * @throws std::domain_error if J <= 0 or G <= 0.
217 */
219 units::kilogram_square_meter_t J,
220 double G);
221 /**
222 * Create a state-space model of differential drive drivetrain. In this model,
223 * the states are [left velocity, right velocity], the inputs are [left
224 * voltage, right voltage], and the outputs are [left velocity, right
225 * velocity]
226 *
227 * @param motor The motor (or gearbox) driving the drivetrain.
228 * @param mass The mass of the robot in kilograms.
229 * @param r The radius of the wheels in meters.
230 * @param rb The radius of the base (half of the track width), in meters.
231 * @param J The moment of inertia of the robot.
232 * @param G Gear ratio from motor to wheel.
233 * @throws std::domain_error if mass <= 0, r <= 0, rb <= 0, J <= 0, or
234 * G <= 0.
235 */
237 const DCMotor& motor, units::kilogram_t mass, units::meter_t r,
238 units::meter_t rb, units::kilogram_square_meter_t J, double G);
239};
240
241} // namespace frc
#define WPILIB_DLLEXPORT
Definition: SymbolExports.h:36
The matrix class, also used for vectors and row-vectors.
Definition: Matrix.h:180
Holds the constants for a DC motor.
Definition: DCMotor.h:20
A plant defined using state-space notation.
Definition: LinearSystem.h:31
Definition: LinearSystemId.h:23
static LinearSystem< 2, 1, 1 > ElevatorSystem(DCMotor motor, units::kilogram_t mass, units::meter_t radius, double G)
Create a state-space model of the elevator system.
static LinearSystem< 2, 1, 2 > DCMotorSystem(DCMotor motor, units::kilogram_square_meter_t J, double G)
Create a state-space model of a DC motor system.
static LinearSystem< 2, 1, 1 > IdentifyPositionSystem(decltype(1_V/Velocity_t< Distance >(1)) kV, decltype(1_V/Acceleration_t< Distance >(1)) kA)
Create a state-space model for a 1 DOF position system from its kV (volts/(unit/sec)) and kA (volts/(...
Definition: LinearSystemId.h:124
static LinearSystem< 2, 1, 1 > SingleJointedArmSystem(DCMotor motor, units::kilogram_square_meter_t J, double G)
Create a state-space model of a single-jointed arm system.The states of the system are [angle,...
static LinearSystem< 1, 1, 1 > IdentifyVelocitySystem(decltype(1_V/Velocity_t< Distance >(1)) kV, decltype(1_V/Acceleration_t< Distance >(1)) kA)
Create a state-space model for a 1 DOF velocity system from its kV (volts/(unit/sec)) and kA (volts/(...
Definition: LinearSystemId.h:83
static LinearSystem< 2, 2, 2 > IdentifyDrivetrainSystem(decltype(1_V/1_mps) kVLinear, decltype(1_V/1_mps_sq) kALinear, decltype(1_V/1_rad_per_s) kVAngular, decltype(1_V/1_rad_per_s_sq) kAAngular, units::meter_t trackwidth)
Identify a differential drive drivetrain given the drivetrain's kV and kA in both linear {(volts/(met...
static LinearSystem< 2, 2, 2 > DrivetrainVelocitySystem(const DCMotor &motor, units::kilogram_t mass, units::meter_t r, units::meter_t rb, units::kilogram_square_meter_t J, double G)
Create a state-space model of differential drive drivetrain.
static LinearSystem< 1, 1, 1 > FlywheelSystem(DCMotor motor, units::kilogram_square_meter_t J, double G)
Create a state-space model of a flywheel system, the states of the system are [angular velocity],...
static LinearSystem< 2, 2, 2 > IdentifyDrivetrainSystem(decltype(1_V/1_mps) kVLinear, decltype(1_V/1_mps_sq) kALinear, decltype(1_V/1_mps) kVAngular, decltype(1_V/1_mps_sq) kAAngular)
Identify a differential drive drivetrain given the drivetrain's kV and kA in both linear {(volts/(met...
Container for values which represent quantities of a given unit.
Definition: base.h:1937
typename units::detail::inverse_impl< U >::type inverse
represents the inverse unit type of class U.
Definition: base.h:1145
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
G
Definition: magnetic_field_strength.h:52