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