How to get the x and y speed of swerve

l use WPILib to control my swerve and l want to get the x and y speed of my swerve in auto, how can l do that. In the teleop, it seems that l can get these two values from chassisspeed, but in auto, which uses setModulestates() to control. So, can l get the x and y speed from swervemodulestates. Or , is there any method to convert the swervemodulestate to the chassisspeed again so that the x and y value can ge got?

You can create module states using the swerve encoders and do the transition from module states to chassis speeds and then use the gyro to convert to field-oriented values.

2 Likes

Is this field-relative? How to get the field-relative one?

1 Like

The function generates robot-relative speeds, you will need to use trigonometry to convert the speeds.
I would start with combining the x and y speed to create your robot-relative speed vector, then rotate to be field-centric using the gyro angle and finally split it again to field-relative x and y speeds.

2 Likes


l think these two lines of code may solve the problem.

There is also ChassisSpeeds.fromFieldRelativeSpeeds that does this for you (by passing the reported gyro angle)

1 Like