Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[wpilib] Adding torque input classes to PhysicsSim classes (Fly, DC, Elev, Arm, DriveTrain) #7157

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
96 commits
Select commit Hold shift + click to select a range
1dff657
Initial file setup with FlywheelSim
narmstro2020 Oct 3, 2024
ed867eb
added LinearSysId createFlywheel for torque
narmstro2020 Oct 3, 2024
eb97efa
doc fix to identifyVelocitySystem
narmstro2020 Oct 3, 2024
de59203
added getVoltage method for Torque Sim
narmstro2020 Oct 3, 2024
8f17d17
added clamping comment
narmstro2020 Oct 3, 2024
1750120
rough first full draft of LinearSystemId
narmstro2020 Oct 3, 2024
9886cef
name reversion
narmstro2020 Oct 4, 2024
af6fbe6
equation and derivation fixes.
narmstro2020 Oct 4, 2024
234c166
cleanup on DCMotorSim classes.
narmstro2020 Oct 4, 2024
49f4ab0
elevatorSim base class first draft.
narmstro2020 Oct 4, 2024
b964be7
integrated Units class into FlywheelSims
narmstro2020 Oct 4, 2024
4a59ada
integrated units library into DCMotorSim
narmstro2020 Oct 4, 2024
f2b6051
ElevatorSim Base class units library integration
narmstro2020 Oct 4, 2024
1663bde
first draft of elevatorVoltageSim
narmstro2020 Oct 4, 2024
8c035da
formatting fixes
narmstro2020 Oct 4, 2024
b35ab9c
start elevatorTorqueSim. More descriptive g constants.
narmstro2020 Oct 4, 2024
607e57c
first draft ElevatorTorqueSim
narmstro2020 Oct 4, 2024
c1e49a9
renaming Base, Sim, SimTorque
narmstro2020 Oct 4, 2024
b5e46a0
Integrated Units library to LinearSystemId
narmstro2020 Oct 4, 2024
37982ee
Merge branch 'wpilibsuite:main' into PhysicsSimTorqueCurrent
narmstro2020 Oct 4, 2024
defb6d7
formatting wpiformat
narmstro2020 Oct 4, 2024
102f13a
general cleanup of existing files
narmstro2020 Oct 5, 2024
538c01a
more formatting fixes
narmstro2020 Oct 5, 2024
38bed76
visibility modifers fixes
narmstro2020 Oct 5, 2024
86362b4
visibility fixes
narmstro2020 Oct 5, 2024
337f67c
replaced comments with JavaDocs
narmstro2020 Oct 5, 2024
0be0df1
more JavaDoc fixes
narmstro2020 Oct 5, 2024
a5eb954
doc fixes
narmstro2020 Oct 5, 2024
40129c6
doc fixes
narmstro2020 Oct 5, 2024
521e7ec
formatting fixes
narmstro2020 Oct 5, 2024
b1a3441
added WheelSim
narmstro2020 Oct 5, 2024
bf986c2
added more units class support to ElevatorSim
narmstro2020 Oct 5, 2024
3981ca2
formatting fixes
narmstro2020 Oct 5, 2024
e4cd729
more units class support for LinearSystemId
narmstro2020 Oct 5, 2024
ce0f364
Merge branch 'wpilibsuite:main' into PhysicsSimTorqueCurrent
narmstro2020 Oct 5, 2024
c620a16
create but not integrate new DCMotorConstants class
narmstro2020 Oct 5, 2024
c2eeceb
Merge branch 'PhysicsSimTorqueCurrent' of https://github.com/narmstro…
narmstro2020 Oct 5, 2024
e1fbb94
Gearbox class created.
narmstro2020 Oct 6, 2024
b550255
formatting fixes
narmstro2020 Oct 6, 2024
22e4e10
replace record with POJO
narmstro2020 Oct 6, 2024
2b675da
formatting fixes
narmstro2020 Oct 6, 2024
4f10e64
formatting fixes
narmstro2020 Oct 6, 2024
2e2dd08
formatting fixes
narmstro2020 Oct 6, 2024
0793bff
replace DCMotor with Gearbox in Flywheel
narmstro2020 Oct 6, 2024
bb9153c
formatting fixes
narmstro2020 Oct 6, 2024
f4ba66c
doc fixes
narmstro2020 Oct 6, 2024
8d6b32d
formatting fixes
narmstro2020 Oct 6, 2024
2e2fd5a
formatting fixes
narmstro2020 Oct 6, 2024
f044a44
replaced DCMotor with Gearbox in DCMotorSim
narmstro2020 Oct 6, 2024
0b4ea76
doc fixes
narmstro2020 Oct 6, 2024
c66e8ec
formatting fixes
narmstro2020 Oct 6, 2024
7373c5c
replaced DCMotor with Wheel in WheelSims
narmstro2020 Oct 6, 2024
84e8043
replace DCMotor with Wheel as drum in ElevatorSims
narmstro2020 Oct 7, 2024
5ec6322
formatting fixes
narmstro2020 Oct 7, 2024
69ca1e6
formatting fixes
narmstro2020 Oct 7, 2024
8eb8e2b
formatting fixes
narmstro2020 Oct 7, 2024
0e83319
doc fixes
narmstro2020 Oct 7, 2024
986e0f9
more doc fixes
narmstro2020 Oct 7, 2024
50257c0
formatting fixes
narmstro2020 Oct 7, 2024
bb9f114
more formatting fixes.
narmstro2020 Oct 7, 2024
293f272
numWheels fix
narmstro2020 Oct 7, 2024
1f4df29
formatting fix
narmstro2020 Oct 7, 2024
9b4e904
more formatting fixes
narmstro2020 Oct 7, 2024
198a51a
formatting fixes
narmstro2020 Oct 7, 2024
59d12c0
formatting fixes
narmstro2020 Oct 7, 2024
9e63930
missing param
narmstro2020 Oct 7, 2024
79ba433
line length fix
narmstro2020 Oct 7, 2024
46c3d2b
SingleJointedArmSimBase created
narmstro2020 Oct 7, 2024
a3d3117
doc and formatting fixes
narmstro2020 Oct 7, 2024
2343880
formatting fixes
narmstro2020 Oct 7, 2024
12c09d7
SingleJointedArm voltage and torque created
narmstro2020 Oct 8, 2024
7915c56
formatting fixes
narmstro2020 Oct 8, 2024
52ada56
formatting fixes
narmstro2020 Oct 8, 2024
82271f5
diffdrivetrain draft
narmstro2020 Oct 10, 2024
f03018c
revert DrivetrainSim
narmstro2020 Oct 10, 2024
fe8441a
formatting fixes
narmstro2020 Oct 10, 2024
2ec0101
formatting fixes
narmstro2020 Oct 10, 2024
3ef6746
initial diffdrivetrain setup
narmstro2020 Oct 11, 2024
e5e9262
formatting fixes
narmstro2020 Oct 11, 2024
a0b791b
formatting fixes
narmstro2020 Oct 11, 2024
af36c13
more formatting fixes.
narmstro2020 Oct 11, 2024
8b76b71
LinearSystemId work on drivetrain
narmstro2020 Oct 11, 2024
bc7db91
replace DCMotorType with DCMotor
narmstro2020 Oct 11, 2024
9ba0ceb
formatting fixes
narmstro2020 Oct 11, 2024
c32206e
more formatting fixes
narmstro2020 Oct 11, 2024
d561338
remove diffdrivesim constructor
narmstro2020 Oct 11, 2024
7fdd630
replace measurementStd devs with array
narmstro2020 Oct 11, 2024
47a8aab
Merge branch 'main' into PhysicsSimTorqueCurrent
narmstro2020 Oct 11, 2024
1073b71
fix missing import
narmstro2020 Oct 11, 2024
1caafae
Merge branch 'wpilibsuite:main' into PhysicsSimTorqueCurrent
narmstro2020 Oct 11, 2024
eff75a7
DCMotor, Gearbox, Flywheel review drafts
narmstro2020 Oct 11, 2024
7888aaa
doc fixes
narmstro2020 Oct 11, 2024
61ecceb
doc fixes
narmstro2020 Oct 11, 2024
edea2ab
DCMotorSim clasees review draft
narmstro2020 Oct 11, 2024
a3a2590
wheel clean for review draft
narmstro2020 Oct 12, 2024
f36d6d8
removed wheelSims
narmstro2020 Oct 12, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,252 +4,90 @@

package edu.wpi.first.wpilibj.simulation;

import static edu.wpi.first.units.Units.Radians;
import static edu.wpi.first.units.Units.RadiansPerSecond;
import static edu.wpi.first.units.Units.RadiansPerSecondPerSecond;
import static edu.wpi.first.units.Units.Amps;
import static edu.wpi.first.units.Units.KilogramSquareMeters;
import static edu.wpi.first.units.Units.Volts;

import edu.wpi.first.math.VecBuilder;
import edu.wpi.first.math.numbers.N1;
import edu.wpi.first.math.numbers.N2;
import edu.wpi.first.math.system.LinearSystem;
import edu.wpi.first.math.system.plant.DCMotor;
import edu.wpi.first.math.util.Units;
import edu.wpi.first.units.measure.Angle;
import edu.wpi.first.units.measure.AngularAcceleration;
import edu.wpi.first.units.measure.AngularVelocity;
import edu.wpi.first.units.measure.MutAngle;
import edu.wpi.first.units.measure.MutAngularAcceleration;
import edu.wpi.first.units.measure.MutAngularVelocity;
import edu.wpi.first.math.system.plant.Gearbox;
import edu.wpi.first.math.system.plant.LinearSystemId;
import edu.wpi.first.units.measure.MomentOfInertia;
import edu.wpi.first.units.measure.Voltage;
import edu.wpi.first.wpilibj.RobotController;

/** Represents a simulated DC motor mechanism. */
public class DCMotorSim extends LinearSystemSim<N2, N1, N2> {
// Gearbox for the DC motor.
private final DCMotor m_gearbox;

// The gearing from the motors to the output.
private final double m_gearing;

// The moment of inertia for the DC motor mechanism.
private final double m_jKgMetersSquared;

// The angle of the system.
private final MutAngle m_angle = Radians.mutable(0.0);

// The angular velocity of the system.
private final MutAngularVelocity m_angularVelocity = RadiansPerSecond.mutable(0.0);

// The angular acceleration of the system.
private final MutAngularAcceleration m_angularAcceleration =
RadiansPerSecondPerSecond.mutable(0.0);

/** Represents a simulated DC motor mechanism controlled by voltage input. */
public class DCMotorSim extends DCMotorSimBase {
/**
* Creates a simulated DC motor mechanism.
* Creates a simulated DC motor mechanism controlled by voltage input.
*
* @param plant The linear system representing the DC motor. This system can be created with
* {@link edu.wpi.first.math.system.plant.LinearSystemId#createDCMotorSystem(DCMotor, double,
* double)} or {@link
* edu.wpi.first.math.system.plant.LinearSystemId#createDCMotorSystem(double, double)}. If
* {@link edu.wpi.first.math.system.plant.LinearSystemId#createDCMotorSystem(double, double)}
* is used, the distance unit must be radians.
* @param gearbox The type of and number of motors in the DC motor gearbox.
* <p>If using physical constants create the plant using either {@link
* LinearSystemId#createDCMotorSystem(Gearbox, double)} or {@link
* LinearSystemId#createDCMotorSystem(Gearbox, MomentOfInertia)}.
*
* <p>If using system characterization create the plant using either {@link
* LinearSystemId#identifyPositionSystem(double, double)} or the units class overload. The
* distance unit must be radians. The input unit must be volts.
*
* @param plant The linear system that represents the simulated DC motor mechanism.
* @param gearbox The gearbox of the simulated DC motor mechanism.
* @param measurementStdDevs The standard deviations of the measurements. Can be omitted if no
* noise is desired. If present must have 2 elements. The first element is for position. The
* second element is for velocity.
* second element is for velocity. The units should be radians for position and radians per
* second for velocity.
*/
public DCMotorSim(LinearSystem<N2, N1, N2> plant, DCMotor gearbox, double... measurementStdDevs) {
super(plant, measurementStdDevs);
m_gearbox = gearbox;

// By theorem 6.10.1 of https://file.tavsys.net/control/controls-engineering-in-frc.pdf,
// the DC motor state-space model is:
public DCMotorSim(LinearSystem<N2, N1, N2> plant, Gearbox gearbox, double... measurementStdDevs) {
// By theorem 6.10.1 of
// https://file.tavsys.net/control/controls-engineering-in-frc.pdf,
// the DC motor mechanism state-space model with voltage as input is:
//
// dx/dt = -G²Kₜ/(KᵥRJ)x + (GKₜ)/(RJ)u
// A = -G²Kₜ/(KᵥRJ)
// B = GKₜ/(RJ)
//
// Solve for G.
//
// A/B = -G/Kᵥ
// G = -KᵥA/B
// dx/dt = -G²Kₜ/(KᵥRJ)x + (GKₜ)/(RJ)u
// A = -G²Kₜ/(KᵥRJ)
// B = GKₜ/(RJ)
//
// Solve for J.
//
// B = GKₜ/(RJ)
// J = GKₜ/(RB)
m_gearing = -gearbox.KvRadPerSecPerVolt * plant.getA(1, 1) / plant.getB(1, 0);
m_jKgMetersSquared = m_gearing * gearbox.KtNMPerAmp / (gearbox.rOhms * plant.getB(1, 0));
}

/**
* Sets the state of the DC motor.
*
* @param angularPositionRad The new position in radians.
* @param angularVelocityRadPerSec The new velocity in radians per second.
*/
public void setState(double angularPositionRad, double angularVelocityRadPerSec) {
setState(VecBuilder.fill(angularPositionRad, angularVelocityRadPerSec));
}

/**
* Sets the DC motor's angular position.
*
* @param angularPositionRad The new position in radians.
*/
public void setAngle(double angularPositionRad) {
setState(angularPositionRad, getAngularVelocityRadPerSec());
}

/**
* Sets the DC motor's angular velocity.
*
* @param angularVelocityRadPerSec The new velocity in radians per second.
*/
public void setAngularVelocity(double angularVelocityRadPerSec) {
setState(getAngularPositionRad(), angularVelocityRadPerSec);
}

/**
* Returns the gear ratio of the DC motor.
*
* @return the DC motor's gear ratio.
*/
public double getGearing() {
return m_gearing;
}

/**
* Returns the moment of inertia of the DC motor.
*
* @return The DC motor's moment of inertia.
*/
public double getJKgMetersSquared() {
return m_jKgMetersSquared;
// B = GKₜ/(RJ)
// J = GKₜ/(RB)
super(
plant,
gearbox,
KilogramSquareMeters.of(
gearbox.numMotors
* gearbox.reduction
* gearbox.motorType.KtNMPerAmp
/ gearbox.motorType.rOhms
/ plant.getB(1, 0)),
measurementStdDevs);
}

/**
* Returns the gearbox for the DC motor.
* Sets the input voltage of the simulated DC motor mechanism.
*
* @return The DC motor's gearbox.
* @param volts The input voltage in volts.
*/
public DCMotor getGearbox() {
return m_gearbox;
}

/**
* Returns the DC motor's position.
*
* @return The DC motor's position.
*/
public double getAngularPositionRad() {
return getOutput(0);
}

/**
* Returns the DC motor's position in rotations.
*
* @return The DC motor's position in rotations.
*/
public double getAngularPositionRotations() {
return Units.radiansToRotations(getAngularPositionRad());
}

/**
* Returns the DC motor's position.
*
* @return The DC motor's position
*/
public Angle getAngularPosition() {
m_angle.mut_setMagnitude(getAngularPositionRad());
return m_angle;
}

/**
* Returns the DC motor's velocity.
*
* @return The DC motor's velocity.
*/
public double getAngularVelocityRadPerSec() {
return getOutput(1);
}

/**
* Returns the DC motor's velocity in RPM.
*
* @return The DC motor's velocity in RPM.
*/
public double getAngularVelocityRPM() {
return Units.radiansPerSecondToRotationsPerMinute(getAngularVelocityRadPerSec());
}

/**
* Returns the DC motor's velocity.
*
* @return The DC motor's velocity
*/
public AngularVelocity getAngularVelocity() {
m_angularVelocity.mut_setMagnitude(getAngularVelocityRadPerSec());
return m_angularVelocity;
}

/**
* Returns the DC motor's acceleration in Radians Per Second Squared.
*
* @return The DC motor's acceleration in Radians Per Second Squared.
*/
public double getAngularAccelerationRadPerSecSq() {
var acceleration = (m_plant.getA().times(m_x)).plus(m_plant.getB().times(m_u));
return acceleration.get(0, 0);
}

/**
* Returns the DC motor's acceleration.
*
* @return The DC motor's acceleration.
*/
public AngularAcceleration getAngularAcceleration() {
m_angularAcceleration.mut_setMagnitude(getAngularAccelerationRadPerSecSq());
return m_angularAcceleration;
}

/**
* Returns the DC motor's torque in Newton-Meters.
*
* @return The DC motor's torque in Newton-Meters.
*/
public double getTorqueNewtonMeters() {
return getAngularAccelerationRadPerSecSq() * m_jKgMetersSquared;
}

/**
* Returns the DC motor's current draw.
*
* @return The DC motor's current draw.
*/
public double getCurrentDrawAmps() {
// I = V / R - omega / (Kv * R)
// Reductions are output over input, so a reduction of 2:1 means the motor is spinning
// 2x faster than the output
return m_gearbox.getCurrent(m_x.get(1, 0) * m_gearing, m_u.get(0, 0))
* Math.signum(m_u.get(0, 0));
public void setInputVoltage(double volts) {
setInput(volts);
clampInput(RobotController.getBatteryVoltage());
m_voltage.mut_replace(m_u.get(0, 0), Volts);
}

/**
* Gets the input voltage for the DC motor.
* Sets the input voltage of the simulated DC motor mechanism.
*
* @return The DC motor's input voltage.
* @param voltage The input voltage.
*/
public double getInputVoltage() {
return getInput(0);
public void setInputVoltage(Voltage voltage) {
setInputVoltage(voltage.in(Volts));
}

/**
* Sets the input voltage for the DC motor.
*
* @param volts The input voltage.
*/
public void setInputVoltage(double volts) {
setInput(volts);
clampInput(RobotController.getBatteryVoltage());
@Override
public void update(double dtSeconds) {
super.update(dtSeconds);
m_currentDraw.mut_replace(
m_gearbox.currentAmps(m_x.get(1, 0), getInput(0)) * Math.signum(m_u.get(0, 0)), Amps);
m_voltage.mut_replace(getInput(0), Volts);
m_torque.mut_replace(m_gearbox.torque(m_currentDraw));
}
}
Loading
Loading