CAN Talon Encoder

Hey I am a programmer on 4085 and we are having a ton of struggle with the new encoders. We have an encoder hooked up directly to the pins on our Talon SR’s. We are currently getting position and I have spent 3 hours working on a dry board trying to get it to move forward for 4’ and stop. Simple right? But there’s a catch! There is no reset encoder position command. So we have no way of making it “zero” at the beginning of autonomous. Now you understand why I had 3 hours at the dry board. Our “get encoder position” command returns a number that doesn’t start a “zero”. Our “get velocity” command returns a largely changing number so we are calling it “dead” and bugged. We do have the breakout board plugged into the encoders before we plug them into the Talon. What are you guise using this year? We want to know that the pin out on the speed controllers don’t work before we just rewire it back to the analog ports. Can you help?

package org.usfirst.frc.team4085.robot;

import edu.wpi.first.wpilibj.IterativeRobot;
import edu.wpi.first.wpilibj.command.Command;
import edu.wpi.first.wpilibj.command.Scheduler;

import org.usfirst.frc.team4085.robot.commands.ExampleCommand;
import org.usfirst.frc.team4085.robot.subsystems.ExampleSubsystem;

import edu.wpi.first.wpilibj.SpeedController;
import edu.wpi.first.wpilibj.Talon;
import edu.wpi.first.wpilibj.RobotDrive;
import edu.wpi.first.wpilibj.Joystick;
import edu.wpi.first.wpilibj.CANTalon;
import edu.wpi.first.wpilibj.smartdashboard.*;
import edu.wpi.first.wpilibj.Encoder;

public class Robot extends IterativeRobot {
	CANTalon ldrive = new CANTalon(5);
	CANTalon rdrive = new CANTalon(6);
	Joystick lstick = new Joystick(1);
	Joystick rstick = new Joystick(0);
	RobotDrive moving = new RobotDrive(ldrive, rdrive);
	SmartDashboard smart = new SmartDashboard();
	int d;
	int y;
	int z;
	int b;
	Encoder lenc1 = new Encoder(2);

	public static final ExampleSubsystem exampleSubsystem = new ExampleSubsystem();
	public static OI oi;

    Command autonomousCommand;

     * This function is run when the robot is first started up and should be
     * used for any initialization code.
    public void robotInit() {
		oi = new OI();
        // instantiate the command used for the autonomous period
        autonomousCommand = new ExampleCommand();
	public void disabledPeriodic() {

    public void autonomousInit() {
        // schedule the autonomous command (example)
        if (autonomousCommand != null) autonomousCommand.start();

     * This function is called periodically during autonomous
    public void autonomousPeriodic() {

    public void teleopInit() {
		// This makes sure that the autonomous stops running when
        // teleop starts running. If you want the autonomous to 
        // continue until interrupted by another command, remove
        // this line or comment it out.
        if (autonomousCommand != null) autonomousCommand.cancel();

     * This function is called when the disabled button is hit.
     * You can use it to reset subsystems before shutting down.
    public void disabledInit(){


     * This function is called periodically during operator control
    public void teleopPeriodic() {
      //  moving1.tankDrive(rstick, lstick)
        smart.putString("ldrive Position", Integer.toString(ldrive.getEncPosition()));
        smart.putString("rdrive Position", Integer.toString(rdrive.getEncPosition()));
        smart.putString("ldrive Velocity", Integer.toString(ldrive.getEncVelocity()));
        smart.putString("rdrive Velocity", Integer.toString(rdrive.getEncVelocity()));

    public void testPeriodic() {

What ports on the breakout board are the encoders plugged into. You made it wound like they are plugged into the analog ports. The A and B channels from the encoder should be plugged into the corresponding ports on the breakout.

As for resetting the encoder, how about just saving the current value whenever you want to reset it and just add that to your setpoint.
EDIT: Actually ignore this, Neal has a better answer

It would also help to know what type of encoder you are using and to see your autonomous command.

Why can’t you use ldrive->SetPosition(0)? See 13.2 in the Talon SRX Software Reference Manual.

Good question Neal :slight_smile:

Also, are you using Analog Encoder or Quadrature Encoder?

If you’re using Analog encoder be sure to update your Talon to 1.4 and also read the workaround for Functional Limitation 21.17 in the Talon SRX Software Reference Manual.

CANTalon defaults to Quadrature Encoder for the feedbackdevice. Follows the steps in Section 7 for selecting the feedback device (Sensor) and follow 7.4 for selecting the sensor direction correctly.

Use the Self-Test (section 2.4) to see what’s going on in the Talon.

Lowering the elevator/mechanism to the starting position and calling talon.setPosition(0) sets that position as zero for the encoder. We had the same problem as you, and just solved it.

We are trying to read the encoder value by calling leftMotor.getPosition or leftMotor.getEncPosition, both returned zero while the motor is running. Is there a trick to do this? Did I miss some sort of configuration to “enable” encoder on the CANTalon? I hooked the encoder wires to +5V, A, B and GND of the Talon. We are not using the breakout board (they are out-of-stock). We spliced the ribbon cable to a 8-pin molex connector. Encoder should have +5V on pin 2, A on pin 7, B on pin 5 and GND on pin 10. I assume the red wire on the ribbon is pin 1. I checked with a volt meter that pin 2 and pin 10 is indeed 5V to GND. So it means I did not flip the connector backward. In code, aside from instantiate the CANTalon motor controller, the only call we made is to leftMotor.setFeedbackDevice(CANTalon.FeedbackDevice.QuadEncoder). I am thinking we may not even have to do that if we call getEncPosition. But we set the feedback device anyway so we can also call getPosition. But somehow both calls returned zero. Any help is appreciated.
BTW, everybody said there is new beta firmware for the Talon SRX but nobody seems to know where to download it. A link to it would be nice.

Never mind, got it all working. The encoder was mis-wired.

Same place as the documentation…

Also please read the FRC team updates…
…not only are the updates mentioned (and their links) but you don’t want to miss API updates, firmware updates, rule changes, scoring updates, etc…
You can rely on CD for some stuff, but this is the source.