The front of the robot is constantly changing in swerve drive

hello everyone; I am using swerve drive MK4. When I use Rx Axis, the front of the robot constantly changes. I need your help on how to overcome this problem.
my code is here
GitHub - wusled/gul2: da

Have you tried reversing the gyro on the Constants?

Can you help me on how to do it?

Go to the Constants.java, find the line 21, and change false to true

I tried what you said but it’s still the same. So when I rotate the robot around its environment, the front of the robot constantly changes

Can you try describing it in a different way? I don’t understand what you mean.

For instance, what is the robot doing, versus what do you expect it to do?

If possible send a video describing your problem

We had similar problems in our prototype. It turned out that 1) we were not accessing our gyro right (using the wrong make from WPILib, no compile nor runtime error just always returned 0 yaw); then 2) we had the sign on the gyro wrong, solved by inserting a single “-“ in the relevant place (gyro reference in swerve code).

Correct me if im wrong but it sounds like you are driving field centric and not expecting it to be field centric.

Field centric means if you turn on your robot for the first time and tape an arrow on the floor labeling the “front” of your robot. The arrow will tell you what direction forward is for the rest of the time until you turn the robot off or reset. Regardless of which direction the robot is actually facing.

Is this whats happening?

1 Like

Yes, you definitely understood our problem correctly. I dont want to drive the robot filed centric. What if I want to determine the front of the robot? Actually I Want to direct the robot as swerve driver,but I dont want it to behave as field centric.
what changes should I make in the codes?

Gotta follow the code. Here’s the process I used:

There’s a robotCentric object in RobotContainer. That seems promising, so let’s look at where it goes. Beginning line 49 of RobotContainer:

s_Swerve.setDefaultCommand( new TeleopSwerve(s_Swerve, () -> -xbox.getRawAxis(translationAxis), () -> -xbox.getRawAxis(strafeAxis), () -> xbox.getRawAxis(4), robotCentric)); // Configure the button bindings configureButtonBindings();

It is passed to the constructor of TeleopSwerve. If I go there, I find:

public TeleopSwerve( Swerve s_Swerve, DoubleSupplier translationSup, DoubleSupplier strafeSup, DoubleSupplier rotationSup, BooleanSupplier robotCentricSup) {

robotCentric was the fifth parameter. The fifth parameter of the TeleopSwerve constructor is robotCentricSup.

On line 36, it is assigned to this.robotCentricSup.

In TeleopSwerve’s execute method, line 47, the code calls !robotCentricSup.getAsBoolean() and passes this to the drive method (3rd argument) of the s_Swerve, an object of class Swerve. So, it’s flipping the boolean that represents whether the button is currently pressed. This means that when you aren’t holding the button, this should be true.

In Swerve, the method drive is defined on line 59 as:

drive(Translation2d translation, double rotation, boolean fieldRelative, boolean isOpenLoop)

The 3rd argument is boolean fieldRelative, which it uses to select between:

when true: (lines 63-64) ChassisSpeeds.fromFieldRelativeSpeeds(translation.getX(), translation.getY(), rotation, getYaw())

or

when false: (line 65)
new ChassisSpeeds(translation.getX(), translation.getY(), rotation)

The first condition above (when true) says in the name – it’s trying to calculate your swerve module states from field-relative. You can verify this by looking up the class documentation.

So, either you need to hold the button while driving, or, if you never want it, you can replace the robotCentric assignment in RobotContainer to never look at the button and instead, a constant. e.g.,

private final JoystickButton robotCentric = new JoystickButton(driver, 1);

becomes

private final BooleanSupplier robotCentric = () -> true;

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