Use this file to discover all available pages before exploring further.
The WPILib Simulation API enables testing robot code on a desktop computer without physical hardware, accelerating development and enabling continuous integration testing.
import edu.wpi.first.wpilibj.simulation.PWMSim;import edu.wpi.first.wpilibj.motorcontrol.PWMSparkMax;public class DriveSubsystem extends SubsystemBase { private final PWMSparkMax leftMotor = new PWMSparkMax(0); private final PWMSparkMax rightMotor = new PWMSparkMax(1); // Simulation support private PWMSim leftMotorSim; private PWMSim rightMotorSim; public DriveSubsystem() { // Only create sims when running in simulation if (RobotBase.isSimulation()) { leftMotorSim = new PWMSim(leftMotor); rightMotorSim = new PWMSim(rightMotor); } } @Override public void simulationPeriodic() { // Get motor outputs for physics simulation double leftSpeed = leftMotorSim.getSpeed(); double rightSpeed = rightMotorSim.getSpeed(); // Use in physics simulation... }}
import edu.wpi.first.wpilibj.ADXRS450_Gyro;import edu.wpi.first.wpilibj.simulation.ADXRS450_GyroSim;private final ADXRS450_Gyro gyro = new ADXRS450_Gyro();private ADXRS450_GyroSim gyroSim;public DriveSubsystem() { if (RobotBase.isSimulation()) { gyroSim = new ADXRS450_GyroSim(gyro); }}@Overridepublic void simulationPeriodic() { // Update gyro angle based on rotation gyroSim.setAngle(simulatedAngle); // Or set rate gyroSim.setRate(angularVelocity);}
import edu.wpi.first.wpilibj.smartdashboard.Mechanism2d;import edu.wpi.first.wpilibj.smartdashboard.MechanismLigament2d;import edu.wpi.first.wpilibj.smartdashboard.MechanismRoot2d;import edu.wpi.first.wpilibj.util.Color8Bit;import edu.wpi.first.wpilibj.util.Color;public class ArmSubsystem extends SubsystemBase { // Create mechanism canvas (width, height in inches) private final Mechanism2d mech = new Mechanism2d(60, 60); // Create root at bottom center private final MechanismRoot2d root = mech.getRoot("arm", 30, 0); // Create arm ligament private final MechanismLigament2d arm = root.append( new MechanismLigament2d( "arm", 30, // Length (inches) 0, // Initial angle (degrees) 6, // Line width new Color8Bit(Color.kBlue) ) ); // Create claw at end of arm private final MechanismLigament2d claw = arm.append( new MechanismLigament2d( "claw", 5, 90, 4, new Color8Bit(Color.kGreen) ) ); public ArmSubsystem() { SmartDashboard.putData("Arm Mechanism", mech); } @Override public void periodic() { // Update arm angle arm.setAngle(getArmAngleDegrees()); // Update claw state claw.setColor(isClawOpen() ? new Color8Bit(Color.kGreen) : new Color8Bit(Color.kRed)); }}
public class RobotMechanism { private final Mechanism2d mech = new Mechanism2d(100, 100); // Drivetrain base private final MechanismRoot2d driveBase = mech.getRoot("drive", 50, 10); private final MechanismLigament2d chassis = driveBase.append( new MechanismLigament2d("chassis", 30, 90, 10, new Color8Bit(Color.kGray))); // Elevator private final MechanismLigament2d elevator = chassis.append( new MechanismLigament2d("elevator", 0, 90, 8, new Color8Bit(Color.kYellow))); // Arm on elevator private final MechanismLigament2d arm = elevator.append( new MechanismLigament2d("arm", 20, 0, 6, new Color8Bit(Color.kBlue))); // Intake/claw private final MechanismLigament2d intake = arm.append( new MechanismLigament2d("intake", 8, 0, 4, new Color8Bit(Color.kGreen))); public void update(double elevatorHeight, double armAngle, boolean intakeDeployed) { elevator.setLength(elevatorHeight); arm.setAngle(armAngle); intake.setAngle(intakeDeployed ? 45 : 0); }}
import edu.wpi.first.wpilibj.RobotBase;public class Robot extends TimedRobot { @Override public void robotInit() { if (RobotBase.isSimulation()) { // Simulation-only initialization setupSimulation(); } else { // Real robot initialization setupRealHardware(); } } @Override public void simulationInit() { // Called once when simulation starts } @Override public void simulationPeriodic() { // Called every 20ms in simulation // Update physics simulations here }}