Drive Encoder
A localizer that uses the drive motor encoders
Prerequisites
Ensure that you have all four drive motors encoders connected to their respective ports on a REV hub.
Setup
In Constants.java
, add an instance of TwoWheelConstants
. Make sure to
replace the hardware map names with the actual names. You must also set the
IMU orientation to match the orientation of your Control Hub.
public static DriveEncoderConstants localizerConstants = new DriveEncoderConstants()
.forwardTicksToInches(1)
.strafeTicksToInches(1)
.turnTicksToInches(1)
.robotWidth(1)
.robotLength(1)
.leftFrontEncoderDirection(Encoder.FORWARD)
.leftRearEncoderDirection(Encoder.FORWARD)
.rightFrontEncoderDirection(Encoder.FORWARD)
.rightRearEncoderDirection(Encoder.FORWARD);
Then, add .driveEncoderLocalizer
to createFollower
:
return new FollowerBuilder(followerConstants, hardwareMap)
.driveEncoderLocalizer(localizerConstants)
/* other builder steps */
.build();
Tuning
Encoder Directions
We will now determine the encoder directions. First, select and run localization test under the localization folder in the tuning OpMode. Then, move the robot forward. All the encoders should tick up when the robot moves forward.
To reverse an encoder, add one of the following to DriveEncoderConstants
:
.leftFrontEncoderDirection(Encoder.REVERSE)
.leftRearEncoderDirection(Encoder.REVERSE)
.rightFrontEncoderDirection(Encoder.REVERSE)
.rightRearEncoderDirection(Encoder.REVERSE);
Robot Width and Length
Measure your robot's wheelbase in inches, the length - the distance from the
forward and back wheels - and width - the distance between the left and right wheels.
Set in your DriveEncoderConstants
both .robotWidth()
and .robotLength()
to the
values measured above.
Forward Tuner
We will now adjust multipliers that convert encoder ticks into real-world measurements: inches. This ensures your localizer's readings are accurate.
Tip
It is recommended that you run these tests multiple times and average the results, as it can result in more accurate localization.
In the tuning OpMode, under localization, select and start the forward tuner. Then, push the robot forward 48 inches (exactly 2 field tiles). This distance is configurable if needed. Once you push the robot forward, two numbers will be displayed on telemetry:
- The distance the robot thinks it has traveled
- The multiplier; this is the number you want.
Add the multiplier to your DriveEncoderConstants
by adding the following.
.forwardTicksToInches(multiplier)
Lateral Tuner
The lateral tuner is very similar to the forward tuner, except it is sideways. In the tuning OpMode, under localization, select and start the lateral tuner. Push the robot left 48 inches (exactly 2 field tiles). As with the forward tuner, this distance is configurable.
Lastly, add the multiplier to DriveEncoderConstants
by adding the following line.
.strafeTicksToInches(multiplier)
Turn Tuner
The turn tuner is again, similar to both the forward tuner and lateral tuner, except it is rotational. Place the robot so it aligns to a fixed reference point (eg. edge of a field tile). In the tuning OpMode, under localization, select and start the lateral tuner. Rotate the robot counterclockwise 48 inches (exactly 2 field tiles). As with the previous tuner, this distance is configurable. Note that the distance is in radians rather than degrees.
Lastly, add the multiplier to DriveEncoderConstants
by adding the following line.
.turnTicksToInches(multiplier)
Testing the localizer
Once you have completed the tuning steps, you can test your localizer as described on the localization page.
Congratulations on successfully tuning your localizer!
Troubleshooting
If you have any problems, see the (troubleshooting page)[/docs/pathing/tuning/troubleshooting].
Last updated on