Extracting Encoder data (Java)

Our team is having difficulty getting data from our encoder. We are using a VersaPlanetary Integrated Encoder that connects directly to a TalonSRX motor. Constructing an Encoder object requires two channels, however we don’t know where to find the corresponding channel numbers. We looked at the documentation, but we couldn’t find anything that specifically addressed our problem. Could anybody advise on how to extract encoder data given our situation?

For a TalonSRX encoder you get the data directly from the TalonSRX. You can find the docs for this in the Talon SRX software reference manual on this page.

The WPI Encoder class is for a Quadrature encoder that plugs into the digital input pins.

Here is my example for you:

package org.usfirst.frc.team6510.robot.subsystems;

import org.usfirst.frc.team6510.robot.RobotMap;
import org.usfirst.frc.team6510.robot.commands.DriveRobotWithjoystick;
import com.ctre.phoenix.motorcontrol.can.WPI_TalonSRX;
import com.ctre.phoenix.motorcontrol.can.WPI_VictorSPX;
import edu.wpi.first.wpilibj.ADXRS450_Gyro;
import edu.wpi.first.wpilibj.SpeedControllerGroup;
import edu.wpi.first.wpilibj.XboxController;
import edu.wpi.first.wpilibj.command.Subsystem;
import edu.wpi.first.wpilibj.drive.DifferentialDrive;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;

public class DriveTrain extends Subsystem {
	
	private WPI_TalonSRX left_F = new WPI_TalonSRX(1); //right_F
	private WPI_VictorSPX  left_R= new WPI_VictorSPX(1);
	
	private WPI_TalonSRX  right_F= new WPI_TalonSRX(2); //
	private WPI_VictorSPX right_R = new WPI_VictorSPX(3);
	
	private ADXRS450_Gyro gyro = new ADXRS450_Gyro();
	
	private SpeedControllerGroup m_left = new SpeedControllerGroup(left_F, left_R);
	private SpeedControllerGroup m_right = new SpeedControllerGroup(right_F, right_R);

	public DifferentialDrive drive = new DifferentialDrive(m_left, m_right);

	public DriveTrain() {
		
		left_R.follow(left_F);
		right_R.follow(right_F);
		
		m_left.setInverted(true);
		m_right.setInverted(true);

	}

	@Override
	protected void initDefaultCommand() {
		setDefaultCommand(new DriveRobotWithjoystick());

	}

	public void drive(double speed, double rotation) {
		drive.arcadeDrive(speed, rotation);

	}

	public void drive(XboxController stick) {
		drive(stick.getRawAxis(1), -stick.getRawAxis(4) * 0.8);
	}

	public double getRightDistance() {
		double rightdist = right_F.getSelectedSensorPosition(0) * RobotMap.distPulse;
		return rightdist;
		// double rightdist = right_F.getSensorCollection().getQuadraturePosition() *
				// RobotMap.distPulse;
	}

	public double getLeftDistance() {
		double leftdist = left_F.getSelectedSensorPosition(0) * RobotMap.distPulse;
		// double leftdist = left_F.getSensorCollection().getQuadraturePosition() *
		// RobotMap.distPulse;
		return leftdist;
	}

	public double getAngle() {

		double angle = gyro.getAngle();
		return angle;
	}

	public void stop() {
		drive(0, 0);
	}

	public void resetDriveTrain() {

		// left_F.getSensorCollection().setQuadraturePosition(0, 10);
		// right_F.getSensorCollection().setQuadraturePosition(0, 10);
		gyro.reset();
		// right_F.hasResetOccurred();
	}

	public void resetEncoders() {
		right_F.setSelectedSensorPosition(0, 0, 10);
		left_F.setSelectedSensorPosition(0, 0, 10);
	}

	public boolean hasResetOccurred() {
		boolean status = false;
		if (right_F.getSelectedSensorPosition(0) == 0 && left_F.getSelectedSensorPosition(0) == 0)
			status = true;
		return status;
	}

	public void log() {
		SmartDashboard.putNumber("Right distance", getRightDistance());
		SmartDashboard.putNumber("Left distance", getLeftDistance());
		SmartDashboard.putNumber("Left Encoder", left_F.getSelectedSensorPosition(0));
		SmartDashboard.putNumber("Right Encoder", right_F.getSelectedSensorPosition(0));
		SmartDashboard.putNumber("Angle", getAngle());
	}
	/////////////////////////////////////////////////////////////////////////////////
}

Hope it helps.

Do you have a link to a github with the other files within that project or did you only write up the file for this scenario? I am trying to switch to command based programming but I wanted a template with exactly what this program was used for

Sounds like in the OP your trying to control an arm or elevator. With the SRX if your connecting the encoder to the SRX your really going to want to use the position mode of the SRX or motion magic.

If that’s the case go open one of the CTRE examples and run through their documentation on that mode.

After your system is working using the sample iterative robot program use a tool like Robotbuilder to build out the robots subsystems, robot map, OI,… you should be able to differentiate the parts of the CTRE code and paste them into the appropriate containers. The sub system will have methods to _talonSRX.setposition(pos) that the command will drive in either the initiation() or execute() methods. The isFinished() can monitor the currentposition.

I’m on my phone so getting deep into the code is difficult.