When simulating a mechanism, software should not merely advance a position variable at a fixed rate. Real mechanisms accelerate according to Newton’s second law (, ), slow down as back-EMF opposes applied voltage, and sag under gravitational torque.
WPILib provides dedicated numerical physics models that integrate the differential equations of standard FRC mechanisms every simulation cycle.
1. What to Learn First
Section titled “1. What to Learn First”Before writing simulation models:
- Verify CAD mass properties for your mechanism. Open Onshape or your CAD package, select the moving assembly (carriage, arm tube, intake rollers), and inspect the calculated mass and moment of inertia.
- Determine your mechanical gear reduction (
G = N_motor / N_mechanism) and effective spool drum radius or arm length.
2. Parameterizing DCMotor
Section titled “2. Parameterizing DCMotor”WPILib’s DCMotor class encapsulates standard motor electrical characteristics:
- Free speed (
omega_freein rad/s) - Free current (
I_freein A) - Stall torque (
tau_stallin N·m) - Stall current (
I_stallin A) - Motor internal resistance (
R = V_nom / I_stall) - Motor torque constant (
K_t = tau_stall / I_stall) - Motor back-EMF constant (
K_v = omega_free / (V_nom - I_free * R))
Standard WPILib Brushless Motors
Section titled “Standard WPILib Brushless Motors”import edu.wpi.first.math.system.plant.DCMotor;
// 1 Kraken X60 motor:DCMotor armMotor = DCMotor.getKrakenX60(1);
// 2 NEO motors powering an elevator:DCMotor elevatorGearbox = DCMotor.getNEO(2);
// Modern compact motors:DCMotor wristMotor = DCMotor.getMinion(1);DCMotor intakeMotor = DCMotor.getX44(1);3. Modeling an Articulated Arm (SingleJointedArmSim)
Section titled “3. Modeling an Articulated Arm (SingleJointedArmSim)”A single-jointed arm rotates around a pivot and experiences variable gravitational torque proportional to :
tau_net = tau_motor * G - m * g * r_com * cos(theta) - b * theta_dotIn Java, instantiate SingleJointedArmSim:
package frc.robot.sim;
import edu.wpi.first.math.system.plant.DCMotor;import edu.wpi.first.math.util.Units;import edu.wpi.first.wpilibj.simulation.SingleJointedArmSim;
public class SimulatedArm { // Physical plant parameters from CAD: private static final DCMotor MOTOR_PLANT = DCMotor.getKrakenX60(1); private static final double GEAR_RATIO = 65.0; // 65:1 overall reduction private static final double MOMENT_OF_INERTIA = 0.42; // kg * m² private static final double ARM_LENGTH_METERS = 0.72; // 0.72m pivot-to-tip length private static final double MIN_ANGLE_RADS = Units.degreesToRadians(-20.0); private static final double MAX_ANGLE_RADS = Units.degreesToRadians(105.0); private static final boolean SIMULATE_GRAVITY = true;
private final SingleJointedArmSim m_armSim = new SingleJointedArmSim( MOTOR_PLANT, GEAR_RATIO, MOMENT_OF_INERTIA, ARM_LENGTH_METERS, MIN_ANGLE_RADS, MAX_ANGLE_RADS, SIMULATE_GRAVITY, Units.degreesToRadians(0.0) // Starting position );
public SingleJointedArmSim getSim() { return m_armSim; }}4. Modeling a Cascading Elevator (ElevatorSim)
Section titled “4. Modeling a Cascading Elevator (ElevatorSim)”A vertical elevator experiences constant downward gravitational force () regardless of height:
package frc.robot.sim;
import edu.wpi.first.math.system.plant.DCMotor;import edu.wpi.first.wpilibj.simulation.ElevatorSim;
public class SimulatedElevator { private static final DCMotor MOTOR_PLANT = DCMotor.getKrakenX60(2); // Dual Kraken drive private static final double GEAR_RATIO = 8.5; // 8.5:1 reduction private static final double CARRIAGE_MASS_KG = 7.5; // 7.5 kg moving mass private static final double DRUM_RADIUS_METERS = 0.022; // 44mm pitch diameter spool private static final double MIN_HEIGHT_METERS = 0.0; private static final double MAX_HEIGHT_METERS = 1.65; // 1.65m maximum extension private static final boolean SIMULATE_GRAVITY = true;
private final ElevatorSim m_elevatorSim = new ElevatorSim( MOTOR_PLANT, GEAR_RATIO, CARRIAGE_MASS_KG, DRUM_RADIUS_METERS, MIN_HEIGHT_METERS, MAX_HEIGHT_METERS, SIMULATE_GRAVITY, 0.0 // Starting height );
public ElevatorSim getSim() { return m_elevatorSim; }}5. Modeling a High-Inertia Flywheel (FlywheelSim)
Section titled “5. Modeling a High-Inertia Flywheel (FlywheelSim)”Flywheels have no travel stops or gravity, but high rotational kinetic energy (E_k = 0.5 * J * omega²):
package frc.robot.sim;
import edu.wpi.first.math.system.plant.DCMotor;import edu.wpi.first.wpilibj.simulation.FlywheelSim;
public class SimulatedFlywheel { private static final DCMotor MOTOR_PLANT = DCMotor.getKrakenX60(2); private static final double GEAR_RATIO = 1.0; // 1:1 direct belt drive private static final double MOMENT_OF_INERTIA = 0.0035; // kg * m² for dual aluminum wheels
private final FlywheelSim m_flywheelSim = new FlywheelSim( MOTOR_PLANT, GEAR_RATIO, MOMENT_OF_INERTIA );
public FlywheelSim getSim() { return m_flywheelSim; }}How Success is Checked
Section titled “How Success is Checked”- Physical Convergence:
- Apply 0.0 Volts to the simulated arm when initialized at 45 degrees.
- The arm must drop downward due to simulated gravity until it hits the minimum hardstop at -20 degrees.
- Current Draw Consistency:
- Check
m_armSim.getCurrentDrawAmps(). Holding the arm horizontal against gravity should draw between 8 A and 15 A, matching physical benchmark measurements.
- Check
- Free Speed Limit:
- Apply 12.0 Volts to the flywheel simulation.
- The flywheel speed must asymptote to the motor’s theoretical free speed (≈ 6000 RPM for Kraken X60) without exceeding physical limits.
Where to Go Next
Section titled “Where to Go Next”Now that your physics models accurately represent mechanism dynamics, proceed to Simulated Hardware & AdvantageScope to connect simulated physics to production robot subsystems.
Verified Sources & Standards
- WPILib Physics Simulation Classesdocs.wpilib.orgWPILib Standard
- WPILib DCMotor Plant Modelsgithub.comWPILib Standard