REVPhysicsSim still doesnt update CANSparkMax velocity and other values

Like the title says. I followed everything that REV robotics told me to put in the code although it still doesn’t seem to simulate correctly.

The motor variables are defined above, which access the drivetrain subsystem to get the sparkmax instances. (Sparkmax is defined in drivetrain subsystem > Drivetrain initialized in robot.java > robot.java accesses drivetrain sparkmax variables > Put into physicsSim)

How would you guys simulate CANSparkMax?

We’re using the simulation and do see those values update. Have you confirmed that the physics sim is actually being called periodically? Is your full code available to take a look at?

How are you viewing the simulation outputs? We’re seeing ours on the driver station sim screen under “Other Devices”.

While we are able to see the velocity and position update, we do see errors in the values especially if we pass in negative setpoints. For example, if we pass in a velocity of -4 m/s, we see an insanely high pre-conversion velocity (makes for some interesting odometry :slight_smile: )

1 Like

Hello, I am viewing the simulation outputs through SIM Gui “Other Devices”. I have confirmed that the physics sim is being called to run periodically.

Here’s the full code:

Please let me know if im doing any bad practices!

I’m having trouble building and running your code. There were a couple of build errors which I worked through, but I run into an exception in compressor.java since you already created a Compressor with ID 0 in solenoid.java. Is the the actual code you are trying to test? Or do you have to push an update to Github?

I also notice that you are creating two compressors in solenoid.java. Does your robot really have two compressors on board?

Hello! I think you’re on the wrong branch. I deleted most of the code and am rebuilding it from scratch since I’ve gained a lot of Java knowledge since then, the link above should go to the current branch im working on (CANYouUpdate Branch).

I’ve also confirmed that PWMSparkmax code works in the simulation. So all im attempting to do now is just seeing if I can get CANSparkmax to simulate.

It does look like the simulation has some issues. When using the “set” function (which is what the differential drive class uses), no applied output value is set. When I use the setVoltage interface, it does appear to set an output (and the simulation updates) but the value is used as a percentage rather than a voltage (i.e., 1.0 has the simulation going at full speed, 0.5 has it going at half speed, etc.). I suggest you reach out to REV support and let them know about this problem. I have contacted them on other simulation issues and they let me know they are working on a new version for the 2024 season. Perhaps more pressure from the community will make them move a little faster. Simulation is a really important tool and they need to get it right. I’ll let you know if I think of any work arounds to the problem.

I appreciate the help! Im sure that the code actually works on the robot.

You also asked for any suggestions regarding bad practices. Right now you have a very tight coupling between Robot and driveTrain. You have public declarations of motor controllers in driveTrain which are referenced by Robot, and a declaration of DifferentialDrive which is referenced by Robot. Rather than structure things that way you should have the motors in driveTrain private and referenced only from within driveTrain, and you should move the DifferentialDrive variable inside the driveTrain class as well. Add an interface to driveTrain which accepts x,y, and theta values for controlling it and call it from the DriveTeleop command. You should also add a simulationInit function to driveTrain which can be used to tell the Rev physisc sim instance about the motors. If you want an example, create a new project based on the MecanumControllerCommand example. Although its mecanum, its a good example structure and you can replace DriveSubsystem with one that implements DifferentialDrive. Good luck!

1 Like

I appreciate it! I didn’t know you could add simulationinit to subsystems.

Scratch that, I copy and pasted the robot simulationInit code to the drivetrain subsystem although it gives the error “The method simulationInit() of type drivetrain must override or implement a supertype method”

How would you fix this?

I just had to get rid of override lol

1 Like

Subsystems don’t have a simulationInit method to override. That’s why you had the compile error. That means you need to ensure your simulationInit method is called at the appropriate time (ie in the Robot simulationInit method, or in the subystem constructor).

Does this mean you’ve successfully used the REVPhysicsSim class to do simulations?