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!