How do you guys simulate swerve drive

I have been looking for methods of simulating swerve drive and was curious to what other teams are using/doing. How do you go about swerve simulation without offical support?

YAGSL has swerve simulation support

Hey Dragonblade!

Making swerve work in simulation was actually one of my biggest goals last summer, and I’m pretty happy with the results.

I used 6328’s 2023 code as an example and was able to get really solid results. They utilize two FlyWheelSim objects to represent each module; one flywheel is the drive motor and the other is the turn motor.

The way it works is that you can natively get velocity data from the sims as well as keep track of positional data by multiplying the current velocity by 0.02, the simulation update time, to get the displacement for each loop period. Once you have position and velocity data for both the drive and turn motor of each module, you should then just be able to re-use all your swerve code that makes them work on your usual motor type. The gyro/robot heading is simulated by converting the states of the modules back into chassis speeds with SwerveDriveKinematics.toChassisSpeeds, getting the rotational velocity of the robot, and then doing the same trick as with the flywheels to keep track of the rotational position. I did the same thing and can find our current 2024 code here. However, be warned that this is a development branch and so some of it might be a little messey. However, if you stay in the general and sim folder, you should be good.

As a side note, super useful for making sure this all works has been 6328’s Advantage Scope visualization tool. It’s actually now built into 2024 WPILib, so I’d highly recommend giving it a try if you have not already. You can see your modules represented as vectors in the swerve tab so long as you are sending their data to networktables in the form of double arrays. Check out the Advantage Scope docs for more info on that.

If you have any questions feel free to ask, I hope this helped!
Noah Simon
Team Optix 3749 President and programmer

3 Likes

3061-lib has supported swerve module simulation in a generic fashion in the past. Here’s the best example.

CTRE’s swerve library (new for 2024) also supports simulation, but you have to be using all CTRE devices for your drivetrain.

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