URGENT I need help with my swerve code

Hi guys, last year my swerve code was working well, however, this year after the Kick-Off our code is broken (Video of what is happening below). I have a MAX SWERVE 3i Module, with a Neo Motor for driving and a Neo 550 for turning. My code is based on the Max Swerve Template from REV.

The biggest difference was in the Pigeon, the field orientation was not working, and when I tried to turn the robot while moving it did not correspond well.

GITHUB LINK: EndrewBortoli/SwerveCodeMAX]GitHub - EndrewBortoli/SwerveCodeMAX

Video link:

Please, I do need help

Do you have the max speed turned way down? If so the modules can only go so fast. Requesting a turn and a movement could be going outside the max speed causing the modules to not be able to reach the needed speed to spin and drive at the same time…

Did you make sure that the Pigeon was updated to 2024 firmware? Also check to make sure that you are getting the correct units from the pigeon, as I believe at least some unit options were removed for some Ctre products this year.

1 Like

The Angular motor speed is 1 because we are just testing. But the problem is not just turning while moving. The other problem that I also show in the video is that when I turn the robot the front changes as well, even though the turning does not reset to 0.

Yeah, I double-checked the Pigeon 2 firmware, and it is up to date

I’ve seen this type of behavior before.
it usually is a result of your gyro being inverted from what you want, you turn right, it thinks you turned left. invert your gyro input and it should be fixed

2 Likes

Just in case - remember that some IMUs (namely NavX) count positive angles in clockwise direction, and others (namely, Pigeon) count positive angles in counterclockwise direction.

Make sure your code accounts for that.

I tried inverted and not inverted but did not change anything.

My main problem involves the rotation of the robot. The wheels seem to respond to commands; when I push the left analog stick forward, the wheels turn forward, and when I pull it backward, the wheels turn backward. Similarly, when I move the right analog stick to the right, the robot rotates right, and when I move the left analog stick to the left, the robot rotates left.

The problem arises when I turn the robot. For instance, when I turn the robot 90 degrees, theoretically, the front of the robot should remain at 0 degrees. However, the front of the robot changes. Consequently, when I instruct the robot to move forward, it goes where its new front is, but this front doesn’t update to reflect the direction of the turn. In the example I provided, it should be 90 degrees, but the front becomes a random location.

This sounds like Field Centric almost but not quite. where the robots “North” will always be the top of the playing field. If you press forward while the robot is facing north it goes normal, as soon as you turn a little and press forward again it would still go north even though the robot looks crooked.

This is normal behavior for Field Centric driving. This is someone asking how to add it into their swerve max code and it turns outs it’s already there by default if one of the arguments is set to true. The 4th argument in their example. The post below the solution is what I would reference. In your case I would try setting this to false and see if the behavior goes away

Looking at your code, it appears your gyro is inverted. WPILib expects values to get larger as your robot rotates counter-clockwise (CCW+). However, the Pigeon 2’s getAngle() method returns the yaw with CW+.

You need to either invert the values, or use the Pigeon’s getRotation2d() or getYaw() method everywhere that you’re reading the yaw, which returns CCW+ like WPILib expects.

3 Likes

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