Advice on Robotpy and SparkMAX simulation

Does anyone have a good example of using the SparkMAX in the robotpy sim?
Previously we had been adding dummy PWMs that we updated from the drive subsystem if we were in sim mode. Basically, we mirrored the SparkMAX output to a PWM because the sim can easily read the PWMs. I suspect it is probably a lot cleaner now - I’m hoping the SparkMAX has a readable output.

Snippet from our old way - just using a “left motor” as an example

import wpilib.simulation as simlib
# in PhysicsEngine __init__
self.l_motor = simlib.PWMSim(1)  # in drivetrain code, mirror output from sparkmax to PWM
# Motor simulation definitions. Each correlates to a motor defined in the drivetrain subsystem.
self.l_spark = simlib.SimDeviceSim('SPARK MAX [1]')  # SparkMAX sim device
self.l_spark_position = self.l_spark.getDouble('Position')  # SparkMAX encoder distance
self.l_spark_velocity = self.l_spark.getDouble('Velocity')  # SparkMAX encoder rate
self.l_spark_output = self.l_spark.getDouble('Applied Output')  # SparkMAX controller output
# in update_sim()
l_motor = self.l_motor.getSpeed()
self.l_spark_output.set(l_motor)  # update the SparkMAX motor output so you can plot
self.l_spark_position.set(self.drivesim.getLeftPosition())  # update the SparkMAX encoder output
self.l_spark_velocity.set(self.drivesim.getLeftVelocity())  # update the SparkMAX encoder rate

Thanks!

I don’t believe REV has updated their simulation support for C++ this year, so nothing has changed since last year. @NoahAndrews might be able to say differently.

This is what my team did last year:

… which is similar to what you did. I’m open to including such shims in robotpy-rev, but I’d rather the vendor fix it instead, though I’m not optimistic about that.

2 Likes

Thanks, it’s good to know I’m not too far behind the times (at least in that one respect). I’m also learning a lot of other tricks just by looking at your code.

Yeah, there’s a lot of neat stuff in wpilib that isn’t very documented. Maybe someday.