15#include "units/acceleration.h"
16#include "units/angular_acceleration.h"
17#include "units/angular_velocity.h"
18#include "units/length.h"
19#include "units/moment_of_inertia.h"
20#include "units/velocity.h"
21#include "units/voltage.h"
29 template <
typename Distance>
31 units::compound_unit<Distance, units::inverse<units::seconds>>>;
33 template <
typename Distance>
35 units::compound_unit<Distance, units::inverse<units::seconds>>,
36 units::inverse<units::seconds>>>;
50 units::kilogram_t mass,
51 units::meter_t radius,
54 throw std::domain_error(
"mass must be greater than zero.");
57 throw std::domain_error(
"radius must be greater than zero.");
60 throw std::domain_error(
"gearing must be greater than zero.");
66 (motor.
R * units::math::pow<2>(radius) * mass * motor.
Kv))
69 {(gearing * motor.
Kt / (motor.
R * radius * mass)).value()}};
87 DCMotor motor, units::kilogram_square_meter_t J,
double gearing) {
89 throw std::domain_error(
"J must be greater than zero.");
92 throw std::domain_error(
"gearing must be greater than zero.");
97 {0.0, (-
gcem::pow(gearing, 2) * motor.
Kt / (motor.
Kv * motor.
R * J))
127 template <
typename Distance>
128 requires std::same_as<units::meter, Distance> ||
129 std::same_as<units::radian, Distance>
133 if (kV <
decltype(kV){0}) {
134 throw std::domain_error(
"Kv must be greater than or equal to zero.");
136 if (kA <=
decltype(kA){0}) {
137 throw std::domain_error(
"Ka must be greater than zero.");
170 template <
typename Distance>
171 requires std::same_as<units::meter, Distance> ||
172 std::same_as<units::radian, Distance>
176 if (kV <
decltype(kV){0}) {
177 throw std::domain_error(
"Kv must be greater than or equal to zero.");
179 if (kA <=
decltype(kA){0}) {
180 throw std::domain_error(
"Ka must be greater than zero.");
183 Matrixd<2, 2> A{{0.0, 1.0}, {0.0, -kV.value() / kA.value()}};
214 decltype(1_V / 1_mps) kVLinear,
decltype(1_V / 1_mps_sq) kALinear,
215 decltype(1_V / 1_mps) kVAngular,
decltype(1_V / 1_mps_sq) kAAngular) {
216 if (kVLinear <=
decltype(kVLinear){0}) {
217 throw std::domain_error(
"Kv,linear must be greater than zero.");
219 if (kALinear <=
decltype(kALinear){0}) {
220 throw std::domain_error(
"Ka,linear must be greater than zero.");
222 if (kVAngular <=
decltype(kVAngular){0}) {
223 throw std::domain_error(
"Kv,angular must be greater than zero.");
225 if (kAAngular <=
decltype(kAAngular){0}) {
226 throw std::domain_error(
"Ka,angular must be greater than zero.");
229 double A1 = -(kVLinear.value() / kALinear.value() +
230 kVAngular.value() / kAAngular.value());
231 double A2 = -(kVLinear.value() / kALinear.value() -
232 kVAngular.value() / kAAngular.value());
233 double B1 = 1.0 / kALinear.value() + 1.0 / kAAngular.value();
234 double B2 = 1.0 / kALinear.value() - 1.0 / kAAngular.value();
275 decltype(1_V / 1_mps) kVLinear,
decltype(1_V / 1_mps_sq) kALinear,
276 decltype(1_V / 1_rad_per_s) kVAngular,
277 decltype(1_V / 1_rad_per_s_sq) kAAngular, units::meter_t trackwidth) {
278 if (kVLinear <=
decltype(kVLinear){0}) {
279 throw std::domain_error(
"Kv,linear must be greater than zero.");
281 if (kALinear <=
decltype(kALinear){0}) {
282 throw std::domain_error(
"Ka,linear must be greater than zero.");
284 if (kVAngular <=
decltype(kVAngular){0}) {
285 throw std::domain_error(
"Kv,angular must be greater than zero.");
287 if (kAAngular <=
decltype(kAAngular){0}) {
288 throw std::domain_error(
"Ka,angular must be greater than zero.");
290 if (trackwidth <= 0_m) {
291 throw std::domain_error(
"r must be greater than zero.");
305 return IdentifyDrivetrainSystem(kVLinear, kALinear,
306 kVAngular * 2.0 / trackwidth * 1_rad,
307 kAAngular * 2.0 / trackwidth * 1_rad);
321 DCMotor motor, units::kilogram_square_meter_t J,
double gearing) {
322 if (J <= 0_kg_sq_m) {
323 throw std::domain_error(
"J must be greater than zero.");
325 if (gearing <= 0.0) {
326 throw std::domain_error(
"gearing must be greater than zero.");
352 DCMotor motor, units::kilogram_square_meter_t J,
double gearing) {
353 if (J <= 0_kg_sq_m) {
354 throw std::domain_error(
"J must be greater than zero.");
356 if (gearing <= 0.0) {
357 throw std::domain_error(
"gearing must be greater than zero.");
362 {0.0, (-
gcem::pow(gearing, 2) * motor.
Kt / (motor.
Kv * motor.
R * J))
392 template <
typename Distance>
393 requires std::same_as<units::meter, Distance> ||
394 std::same_as<units::radian, Distance>
398 if (kV <
decltype(kV){0}) {
399 throw std::domain_error(
"Kv must be greater than or equal to zero.");
401 if (kA <=
decltype(kA){0}) {
402 throw std::domain_error(
"Ka must be greater than zero.");
405 Matrixd<2, 2> A{{0.0, 1.0}, {0.0, -kV.value() / kA.value()}};
429 const DCMotor& motor, units::kilogram_t mass, units::meter_t r,
430 units::meter_t rb, units::kilogram_square_meter_t J,
double gearing) {
432 throw std::domain_error(
"mass must be greater than zero.");
435 throw std::domain_error(
"r must be greater than zero.");
438 throw std::domain_error(
"rb must be greater than zero.");
440 if (J <= 0_kg_sq_m) {
441 throw std::domain_error(
"J must be greater than zero.");
443 if (gearing <= 0.0) {
444 throw std::domain_error(
"gearing must be greater than zero.");
448 (motor.
Kv * motor.
R * units::math::pow<2>(r));
449 auto C2 = gearing * motor.
Kt / (motor.
R * r);
451 Matrixd<2, 2> A{{((1 / mass + units::math::pow<2>(rb) / J) * C1).value(),
452 ((1 / mass - units::math::pow<2>(rb) / J) * C1).value()},
453 {((1 / mass - units::math::pow<2>(rb) / J) * C1).value(),
454 ((1 / mass + units::math::pow<2>(rb) / J) * C1).value()}};
455 Matrixd<2, 2> B{{((1 / mass + units::math::pow<2>(rb) / J) * C2).value(),
456 ((1 / mass - units::math::pow<2>(rb) / J) * C2).value()},
457 {((1 / mass - units::math::pow<2>(rb) / J) * C2).value(),
458 ((1 / mass + units::math::pow<2>(rb) / J) * C2).value()}};
#define WPILIB_DLLEXPORT
Definition SymbolExports.h:36
Holds the constants for a DC motor.
Definition DCMotor.h:20
radians_per_second_per_volt_t Kv
Motor velocity constant.
Definition DCMotor.h:48
units::ohm_t R
Motor internal resistance.
Definition DCMotor.h:45
newton_meters_per_ampere_t Kt
Motor torque constant.
Definition DCMotor.h:51
A plant defined using state-space notation.
Definition LinearSystem.h:35
Linear system ID utility functions.
Definition LinearSystemId.h:27
static constexpr LinearSystem< 2, 1, 2 > DCMotorSystem(decltype(1_V/Velocity_t< Distance >(1)) kV, decltype(1_V/Acceleration_t< Distance >(1)) kA)
Create a state-space model of a DC motor system from its kV (volts/(unit/sec)) and kA (volts/(unit/se...
Definition LinearSystemId.h:395
static constexpr LinearSystem< 2, 1, 2 > SingleJointedArmSystem(DCMotor motor, units::kilogram_square_meter_t J, double gearing)
Create a state-space model of a single-jointed arm system.The states of the system are [angle,...
Definition LinearSystemId.h:86
static constexpr LinearSystem< 2, 1, 2 > 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:173
static constexpr LinearSystem< 2, 1, 2 > ElevatorSystem(DCMotor motor, units::kilogram_t mass, units::meter_t radius, double gearing)
Create a state-space model of the elevator system.
Definition LinearSystemId.h:49
static constexpr LinearSystem< 1, 1, 1 > FlywheelSystem(DCMotor motor, units::kilogram_square_meter_t J, double gearing)
Create a state-space model of a flywheel system, the states of the system are [angular velocity],...
Definition LinearSystemId.h:320
static constexpr 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 gearing)
Create a state-space model of differential drive drivetrain.
Definition LinearSystemId.h:428
static constexpr 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...
Definition LinearSystemId.h:213
static constexpr 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:130
static constexpr LinearSystem< 2, 1, 2 > DCMotorSystem(DCMotor motor, units::kilogram_square_meter_t J, double gearing)
Create a state-space model of a DC motor system.
Definition LinearSystemId.h:351
units::unit_t< units::compound_unit< units::compound_unit< Distance, units::inverse< units::seconds > >, units::inverse< units::seconds > > > Acceleration_t
Definition LinearSystemId.h:34
units::unit_t< units::compound_unit< Distance, units::inverse< units::seconds > > > Velocity_t
Definition LinearSystemId.h:30
static constexpr 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...
Definition LinearSystemId.h:274
Definition SystemServer.h:9
Eigen::Matrix< double, Rows, Cols, Options, MaxRows, MaxCols > Matrixd
Definition EigenCore.h:21
constexpr common_t< T1, T2 > pow(const T1 base, const T2 exp_term) noexcept
Compile-time power function.
Definition pow.hpp:82