How to make encoder go to a set position

From looking at documentation for position closed loop for our grayhill optical encoder, I figured out how to obtain the position using the method getSelectedSensorPosition(); however, how do I utilize this value to get the motor to move to a certain position every time the robot needs to.

For example, like in autonomous, to get the robot to move to a certain distance using encoders.

I do have code of what I have so far on this github repo:

Right now you are ever only setting the motor to PercentOutput mode, which will not do what you want.

Now that you have the encoder cable connected to the Talon, you need to go through the bring up phases to make sure that your encoder is in phase with your motor. (Which means, the motor spin and the encoder need to be in the same direction where positive = forward, and negative = backward).

Once your sensors and motors are in phase, you can go through the close loop control examples here:

You need to research using a Position PID controller. For the PID the encoder value you want to go to is the target and you use your encoder’s value as the feedback for calculating the error. The PID then tells the motor how much power to supply to reduce the error until the values match.

1 Like

To add, specifically, if you are trying to get the robot to drive an exact distance, whether that is the drivetrain or an elevator, I would highly recommend you follow the MotionMagic instructions.

1 Like

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