Hello I am a coding mentor for my team and we are currently working on coding swerve. We have the mk4i swerve modules with neo motors for both rotation and driving. I watched this video on how to code swerve because I was confused on where to start. I decided to use the code in the video almost entirely with some modifications. However, when I deployed the code and ran it none of the modules turned on at first. Later I did get the back right module to drive when I moved the right stick which should have made it rotate instead of drive.
I have the code linked and I hoping someone can help my team and I figure out why it won’t run.
You’ll notice there is no absolute encoder and that is because the my team needs to get different ones than the ones we currently had so I commented it out temporarily.
1 Like
Have you considered YAGSL instead of spinning your own code?
1 Like
What makes your swerve modules move and your robot drive are these two lines -
(113, 114)
So, you need to investigate what values you’re supplying to these methods, and whether what you sent them is actually happening.
We use very similar code. While it was originally adopted from some templates, it’s home-grown in some way, and we have MANY test commands to actually test individual swerve modules.
Ultimately you need to test the setDesiredState method, because that’s what tells your individual modules what they should be doing next. Your PIDController, for instance, has VERY low KP (unless you’re using it wrong?). That is not necessarily an issue as the final angle move will be determined by the angle encoder data (some encoders do not give you ticks, but rather some units that you can program)
Finally, I can point you to our repo, which, as I mentioned, has very similar code.
1 Like
I looked into YAGSL but I didn’t understand using JSON files for the configs. Is there a way to use YAGSL without using JSON files because if there is I’ll try it if I can’t get mine to work. Or if it is not possible if you could point me to a in detail resource about how the JSON files work that would be cool.
I have ALOT of documentation on how to use YAGSL.
For specifics about the JSON files they are described in this section and all subpages.
and there is a config generator if you know exactly what you want as well.
Can YAGSL be written from the ground up and made how I want just using its library? Or is it meant specifically to be copy and pasted from the example code and configure it around my bot. The YAGSL example code is a bit complicated to use from the start and I just want to make the robot drive before getting into path planner. So if I can write it from the ground up that would be preferred.
YAGSL can be implemented however you want, the example code just provides an EXAMPLE of how you can use it.
You can stick the SwerveDrive
object anywhere you see fit and use it just like you would DifferentialDrive
ideally, except heavily modified for Swerve.
In short, yes it can be included from the ground up.
Alright I’ll check it when I can. Do you have any idea why the right stick was causing a module to drive instead of rotate. And why it was only the back right module.
Edit: I just remembered it was driving and trying to rotate very slowly
The drive instead of rotate usually means you supply either the wrong axis of the controller, or you put them in the wrong order here:
() -> driver.getRawAxis(drivetrainSpeedX),
() -> driver.getRawAxis(drivetrainSpeedY),
() -> driver.getRawAxis(drivetrainRotation),
() -> driver.getRawButton(fieldRelativeButton)
For instance, in our code we use
private double getDriverXAxis() {
return -xboxController.getLeftStickY();
}
where
case XBOX:
rawY = this.getRawAxis(1);
So, you can check the raw axis in the driver station and just use whichever one you need for each of the suppliers.
I realized that I coded the right stick to rotate the whole bot not just the module wheels. The right stick was doing as expected. I changed the kp values and now the left and right stick move the modules as they should. However still only the back right module is moving. If you have any ideas as to why that might be it would be greatly appreciated
That’s an easy one -
you have
public static CANSparkMax driveMotor;
public static CANSparkMax angleMotor;
in Swerve24/src/main/java/frc/robot/subsystems/SwerveModule.java at 2ef7d94ed137f339f7ee7daddec6db595aa76b28 · team5863/Swerve24 · GitHub
In Java static variable means - you only have ONE variable for ALL instances of the class. You need FOUR instances of the SwerveModule, with different motors. And yet that definition means all four modules share the same motor
Suggestion - do not have ANY static variables in your code unless you have a full understanding why is it static.
Revise your code accordingly.
In fact the only places you should have static variables is Constants. And the only reason for that - this class is not instantiated. Anything else should NOT have static variables, and pass them as parameters if needed.
Just rules of good Java programming.
Omg you’re so right. I can’t believe I didn’t realize that. I had just done a personal project with a ton of static variables and I went into auto pilot and made everything static. Thank you so much
2 Likes