Defining encoders that are connected to talonSRX

Hi my team uses mag encoders that are connected to the talonSRX for the drivetrain , I want to get the value for the encode but it give me an error when I define the encoder heres my code:

public class DriveTrain extends Subsystem {

  // Put methods for controlling this subsystem

  // here. Call these from Commands.

  public WPI_TalonSRX leftfront = new WPI_TalonSRX(22);

  public WPI_TalonSRX leftback = new WPI_TalonSRX(10);

  public WPI_TalonSRX rightfront = new WPI_TalonSRX(7);

  public WPI_TalonSRX rightback = new WPI_TalonSRX(5);

  leftfront.configSelectedFeedbackSensor(FeedbackDevice.CTRE_MagEncoder_Relative);

  SpeedControllerGroup leftmotors = new SpeedControllerGroup(leftfront, leftback);

  SpeedControllerGroup rightmotors = new SpeedControllerGroup(rightfront, rightback);

  DifferentialDrive DriveTrain = new DifferentialDrive(leftmotors, rightmotors);

  public void arcadeDrive(double move , double rotate){

    DriveTrain.arcadeDrive(move, rotate);

  } 

  public void tankDrive(double left , double right){

    DriveTrain.tankDrive(left, right);

  }

  @Override

  public void initDefaultCommand() {

    // Set the default command for a subsystem here.

    setDefaultCommand(new DriveArcade());

  }

}

what did i do Wrong?

What is the error?

it dosnt recognize this:
configSelectedFeedbackSensor

Are you getting an error here? Does the code refuse to compile, or does it just not do what it is expected to on your robot?

Also a little tip: Try to organize the CAN IDs of your motor controllers, and make them constant variables. It can help a lot when you organize your code. For example;

public WPI_TalonSRX frontLeft = new WPI_TalonSRX(10);
public WPI_TalonSRX backLeft = new WPI_TalonSRX(11);
public WPI_TalonSRX frontRight = new WPI_TalonSRX(12);
public WPI_TalonSRX backRight = new WPI_TalonSRX(13);

According to the docs, it looks like you need to give it the PID slot ID and the timeout first.

So for example:

leftFront.configSelectedFeedbackSensor(FeedbackDevice.CTRE_MagEncoder_Relative, 0, 100); //CTRE Relative Mag encoder, with PID slot 0, 100 ms timeout.

Edit:
Oh, and as OroArmor said, you can’t call a method outside of the methods or constructor of a class.

you cant call a method in the body of a class, you have to call it in a method/constructor

1 Like

can you explain I am new to java and we dont have a mentor that knows programming

This will cause an error because the class is not initialized

public class Example{
    hi();

    public void hi(){
         System.out.println("hi");
    }
}

This will fix that error

public class Example{
    public Example(){ // This is the constructor
        hi();
    }

    public void hi(){
         System.out.println("hi");
    }
}

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