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

Simulate subsystems #59

Open
amorygalili opened this issue Mar 2, 2025 · 1 comment
Open

Simulate subsystems #59

amorygalili opened this issue Mar 2, 2025 · 1 comment

Comments

@amorygalili
Copy link
Contributor

amorygalili commented Mar 2, 2025

Each subsystem should have a simulationPeriodic method:

@Override
public void simulationPeriodic() {
    // sim code goes here
}

Docs on simulating Spark MAX and Flex devices: https://docs.revrobotics.com/revlib/spark/sim/simulation-getting-started#simulating-spark-max-and-flex-devices

Elevator sim example: https://docs.wpilib.org/en/stable/docs/software/wpilib-tools/robot-simulation/physics-sim.html#usage-in-user-code

Example:

// create the normal Spark MAX object
SparkMax max = new SparkMAX(10, MotorType.kBrushless);
// create the Spark MAX sim object
SparkMaxSim maxSim = new SparkMaxSim(max, maxGearbox);

 ElevatorSim m_elevatorSim =
      new ElevatorSim(
          m_elevatorGearbox,
          Constants.kElevatorGearing,
          Constants.kCarriageMass,
          Constants.kElevatorDrumRadius,
          Constants.kMinElevatorHeightMeters,
          Constants.kMaxElevatorHeightMeters,
          true,
          0,
          0.01,
          0.0);

public void simulationPeriodic() {
    // In this method, we update our simulation of what our elevator is doing
    // First, we set our "inputs" (voltages)
    m_elevatorSim.setInput(maxSim.getAppliedOutput() * RobotController.getBatteryVoltage());

    // Next, we update it. The standard loop time is 20ms.
    m_elevatorSim.update(0.020);

    // Now, we update the Spark Flex
    flexSim.iterate(
        Units.radiansPerSecondToRotationsPerMinute( // motor velocity, in RPM
            m_elevatorSim.getVelocityRadPerSec()),
        RobotController.getBatteryVoltage(), // Simulated battery voltage, in Volts
        0.02); // Time interval, in Seconds
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Backlog
Development

No branches or pull requests

1 participant