Issues with java using YAGSL

Hi, it is our teams first time using java for our swerve drive, we are having some issues and were wondering if anyone could give us some pointers of how to fix it.

For some context we are using YAGSL-Example (GitHub - BroncBotz3481/YAGSL-Example: Yet Another General Swerve Library Example Project) for our swerve drive template. Here is our code Github(GitHub - Ryu-123-dev/Swerve-Drive: FRC 4741s swerve project, using YAGSL.), all of the hardware we are using is in the readme of the github code.

Our main issue is that when we enable the robot the setpoints keep changing from something like -45 to -135. Which just causes the wheels to move back and forth. However if we simulate our robot, it works perfectly fine.

Another issue we are having is in the module folder there is a setting called “absoluteEncoderOffset” and we are struggling to find any information on what it is and or what we need to set it to.

Any help is appreciated.

The absoluteEncoderOffset is a very important value that must be set. The JSON documentation describes it in the swerve module configuration as:

Absolute encoder offset from 0 in degrees. May need to be a negative number.

You use it to indicate the position of the magnet in your swerve module encoder. When you first set it up, when the wheels are pointed straight forward (0°) the encoder will not read 0°. You need to read the value of the encoder with the wheel in this position and configure the offset.

For example, if you point the left-front wheel straight forward and the encoder reads 46°, you need to configure an offset of -46° for that module (46° + -46° = 0°).

Get the offsets set up first, it might also solve your main problem. That could be a problem where the code is trying to “optimize” the rotation of the wheel, but it’s confused by the offset.

2 Likes

I have added this to the code, and I am still getting the same issue. I now have a Video of it now. Also would I need to change the Offset value every time I start the robot or just for the first time?

the setpoints keep changing from something like -45 to -135.

I checked while it was running the setpoints keep changing from -45 to 135.

You are using a slightly out of date YAGSL library. I don’t know that updating alone would fix the issue but you can try adding swervedrive.pushOffsetsToControllers() it could fix this issue, but it’s only in the newest version of YAGSL

The offsets are a physical characteristic of the robot, so you should only need to set it once unless your module changes. But by asking you are indicating you don’t understand this setting.

You should make sure to set the offsets correctly. You need to have the encoder offset set to zero, align the wheels forward, and read the value. After configuring the offset, you need to confirm that each wheel is reading 0° when it is pointed forward.

Not related to your issue, but I see in your swervedrive.json you have configured your IMU as navx_usb, but in your README you linked the NavX MXP. It’s more likely you have an MXP, you might want to check that.

As a follow up, make sure your magnets are glued correctly :sweat_smile:

3 Likes

For magnets we have made sure they are glued correctly. We have been setting the offset value so that is good. And we also updated the YAGSL to the newest version yet the problem is still happening.

Some of the wheels get close to the setpoint of 45 but they don’t stay.

Have you tried changing the inversion state of your angle motor or absolut encoder from the module JSON?

We have with the angle motor before so we just tried the absolute, and the setpoints just keep changing

Can you display it using the FRC Web Components App? and send a video of that.

Im a bit short on time so I cant do it with the Web Components, however our SmartDashboard has the values on them here is a video.

The wheels just kind of “dance” like that even if they are on the ground or blocks.

If you could please try this function https://github.com/Ryu-123-dev/Swerve-Drive/blob/59889753f5c29e66d9f29794e7bb301d65616a0f/src/main/java/swervelib/SwerveDrive.java#L1079

I see the function but how should/can I use it.

I’ll have to make a change to the YAGSL example, but you should make a function in the swerve subsystem. Then call the function in robot init

Or just call the function in the subsystem constructor

1 Like

Watching the video, I’m guessing your motor and encoder are not “in phase”. This means the motor and encoder’s inversion are not configured the same.

Here’s a way to troubleshoot:

  1. Put the motor’s encoder on SmartDashboard as well as the CANCoder’s value. If you do this in your subsystem period() method, you can get readings with the robot disabled, so you can safely move the wheels by hand.
  2. Choose a wheel and turn it in the counter-clockwise (left) direction, when viewed from the top.
  3. Both the motor encoder’s value and the CANCoder’s value should get larger. Invert the motor and/or the encoder so the value gets larger when you rotate counter-clockwise.

Here’s more detail on why you would see this behavior when something is out of phase. There is code built in to optimize the direction of the wheel so it never has to rotate more than 90-degrees. For example, consider a scenario where the robot is driving straight forward and you want to switch to backward. A non-optimized way to do this would be to keep the wheel driving in the same direction and spin the steering around 180° to point backwards. An optimized way to do this is to keep the steering pointed forward and drive the wheel in reverse.

This optimization is always looking at where the wheel is now, and where you’re asking it to go, and finding the best option. When the wheel rotates the wrong direction, this optimization keeps flipping between 45° (pointing forward and to the right) and -135°, which are 180° apart. It’s deciding it’s quicker to go the other way and reverse the drive instead of rotating more than 90°.

So I have the encoders so where when I turn them left their value increases, now as for the Motors I am currently struggling to figure out how to add their values to the dashbaord.

You’re using Neo motors with Spark Max controllers, right? A shortcut you could take would be to connect the REV Hardware client and just look at the position value for that motor. For the CANCoder and/or Talon FX controllers, you could use Pheonix Tuner X.

To get it onto the dashboard, you’ll have to go into SparkMaxSwerve and expose the encoder value, maybe with a method like this:

public double getSteerMotorPosition() {
  return encoder.getPosition();
}

Then, call this method for each module and put the value on the dashboard in your SwerveSubsystem.periodic() method.

1 Like

How could I view the position value in the REV Hardware Client, I have looked around for a bit now and I see somethings that might be it but when I try I am not getting anything for values.

  1. Connect a USB-C cable between the SPARK MAX and your computer.
  2. Select the SPARK MAX you want to look at.
  3. Use the telemetry tab to graph the position.
1 Like