Shared swerve drive 3d model paired with code

I have students working to improve their swerve drive coding and control skills. We are planning on using all CTRE components for the drive base so that we can use CTRE’s swerve drive generator and make extensive use of their libraries. We are try to avoid components from other manufacturers to keep the code simple.

The motors are on backorder, so it will be a month or so before we can actual get started putting the swerve drive base together.

I have heard that we can upload code and a 3D model to a simulator and get some good practice with that. We do not, however, have a full CTRE code or a 3D model to go with it. I did try to create the program for a full CTRE drive base, but got stuck while using the CTRE swerve drive generator, as it asks me to connect to my swerve drive base, which, of course, I don’t yet have.

In any case, I am wondering if anyone has CTRE swerve drive base and the code to go along with it that they might share? Jave would be O.K. If, however, there is anyone who happens to have code in Kotlin and has a 3d model they can share, that would be exceptional.

1 Like

In FRC, “simulation” essentially just means running the robot program on desktop rather than the RIO. The robot code is running a physics simulation for each subsystem, and updating its internal state based on that approximation of real hardware. There are several architectures for achieving that objective, but the good news is that CTRE’s swerve API already supports this kind of simulation. You might want to start by using the SwerveWithPathPlanner example (written in Java), which is similar to the code you would get out of Tuner X though not identical.

Visualization is handled separately, and for 3D visualization you can use AdvantageScope. It has several built-in robot models you can use for testing, or upload your own from CAD. Just connect to the simulator and use the 3D field tab to visualize the 2D pose published by the robot code (I believe it’s under “Pose/robotPose”).

Note that while this simulation architecture is useful for testing code logic and auto routines, it’s generally less useful for driver practice (beyond the very basics of teaching what each button does). Most physics simulations for FRC only deal with individual mechanisms, and don’t account for field collision or game piece interaction. Even accurately simulating the physics of a swerve drive is difficult, and most simulations use a very simplified model. It’s is a very useful tool, but there’s nothing that can replace driver practice on a real robot.


Jonah, thanks for this. Unfortunately, we don’t have the robot code. When I attempted to create the code this is what happened.

  1. I made a list of components, which was a swerve drive base with a RobRio, PDP, 4 Swerve drive specialty Mk4i modules, each with two Kraken motors with the integrated talon motor controllers and CTRE Cancoders. (There might have been a few other items)

  2. Downloaded Intellij IDEA and the FRC plug in.

  3. Created an FRC project

  4. Went to CTRE’s swerve drive generator.

At this point, CTRE’s swerve drive generator could not generate the swerve drive code until I “connected” to the RobRio on the swerve drive. (Which we don’t yet have).

Thus, I am hoping to find someone who will share their CTRE swerve drive code so we can play around with it in the simulation until we can get our hands on the Krakens. :slight_smile:

I linked to CTRE’s SwerveWithPathPlanner example, which is a complete robot project similar to the one you would get from the project generator. You should be able to download that project from GitHub and open it in your editor of choice (VSCode is recommended for WPILib, but IntelliJ should work as well). Once you get the robot, you can regenerate the constants file using Tuner X. Until then, the example project seems to include sensible default settings for a generic swerve drive (though they unfortunately don’t document what hardware it is based on). Launching that example project in simulation will allow you to connect AdvantageScope and experiment with the code.