Swerve Python (MK4I, Neos, SparkMAXES, CANCoders)

My team is currently creating swerve for the off-season and I’m been attempting to program it. We are using the MK4i swerve modules and I code in Python. I have run into a wall attempting to code this, we are using NEO Motors, SparkMAXes, and CANcoders for our encoders. I am able to make the motors run and only turn occasionally, none of which is in sync for what swerve drive should be. I will link the code I have made:
My Swerve Code
I would appreciate any help on this issue!

Work methodically! The problem space is too large for guess-and-check to be effective.

First, be able to measure what your code is doing. print() is a good way to start this. SmartDashboard.putNumber() is even better, because you can plot how values change over time.

Next, look at all your sensor inputs. Go one-by-one through each sensor - make sure its value read in to the robot matches your expectations. Run experiments where you twist and turn each module (with the robot disabled!) and make sure readings come back in as you expect. Confirm both the magnitude and the sign of the number matches what you’re doing physically in the real world.

Then, look at each motor. Each motor should have a velocity or a position it is controlling. For that thing under control, you should have desired value it’s trying to get to, and a measured value for where it’s actually at. Plot the two over the top of each other.

For different joystick inputs, do the desired values go where you expect them to? And does the actual/measured value converge to the desired value?

There’s something I’m not understanding in your robot.py file. I see you importing the Drivetrain.py file, but that file looks like it is for a tank drive not swerve. Where are you calling the swervemodule.py file?

Inside swervemodule.py, I see this line:

from wpilib import PWMTalonSRX

I assume you are using a CAN bus and was curious why you have that?

We are also migrating to Python, and I’ll get the swerve code that we currently have (which has been tested and works). We’re using Krakens for the drive motors and Falcon 500s for the turning motors, but it should hopefully get you started in the right direction.

Our 2023 swerve code is at GitHub - aesatchien/FRC2429_2023: Repo for FRC team 2429's 2023 code; we used neos on our swerve as well.

1 Like

I accidentally added the wrong file (they were named too similarly), I added the correct one as the file “Swerve Copy”. This one has the correct drivetrain file. We are using CAN, the PWMTalonSRX import is an accident left from a previous test, it isn’t called anywhere else.
Correct Swerve Code

We ran this setup (Python swerve, Mk4i, NEOs, CANCoder) in 2024. If you want another repo to poke through, ours is available here. Swerve items are mostly contained to subsystems/drivesubsystem.py, helpers/pose_estimator.py and subsystems/swervemodule.py. Should also be mentioned that we use PathPlanner for autonomous mode and teleoperated pathplanning.

1 Like