REVPhysicsSim Spark Max position control simulation

Will REV fix this issue? Its kinda a pain, and makes simulation more difficult.
Using ControlMode.kPosition doesn’t update in simulation, however ControlMode.kVelocity works just fine in simulation.

Has anyone successfully used REVPhysicsSim to simulate mechanisms that use position control, like a swerve module, or arm?

What did REV support say when you contacted them?

No response on the GitHub issue so far.

Recently we have been working on simulating a full-neo (SparkMAX) swerve drive while trying to use as much of rev’s simulation support as possible

The swerve simulation does indeed work but we had to completely bypass RevPhysicsSim (we found it quite clunky to work with) and write our own SparkMAX simulation wrapper - not sure how much use it is to you but I can outline the general structure we have in our code

  • A RevSparkMaxSim class that uses the DCMotor and DCMotorSim classes from wpilib to effectively replace the original rev simulation code
  • We update the integrated relative encoder using the same math that rev uses in their SparkMaxSimProfile, but, for remote feedback sensors, we use the data that we get back from the DCMotorSim - the 2 do indeed match up fairly well, though
  • To run closed-loop control on position in simulation, we run a PID directly in our code (on a real robot, this would be the same as running it on the roborio), feeding it our measured and desired positions, tuning the controller to output a voltage so we can control the SparkMAX using something like
    sparkMax.getPIDController().setReference(pid.calculate(measured, setpoint), CANSparkMax.ControlMode.kVoltage)
    

relevant code for the SwerveModule and RevSparkMaxSim

1 Like

Running PID controllers in software just for simulation is pretty rough…

I’ve worked around it for now by taking advantage of AdvantageKit, and setting the encoder values based on the calculated swerve state. So when we do getPosition() on each module to update the pose estimator, it can calculate the robot’s position in simulation just as it would in reality.

Unfortunately that seems to be the only way to keep code consistent everywhere else, hopefully rev will consider improved simulation support in the near future. FWIW, rev specifically mentions that the only control modes that are supported in simulation are kVelocity and kVoltage, so supporting kPosition is more of a feature request than a bug.

@ Documentation

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.