Help with using Talon FX integrated encoders

Hello, I am trying to use DifferentialDriveWheelSpeeds class to set the speed of my wheels. I know you have to use encoders to do so and I wish to use the integrated ones in the Talon FX motors. Problem is, I dont know how to do that. Am I supposed to type in .getVelocity( ) or something after my motor? Here is a pic of my current code, any help at all is appreciated.

DifferentialDriveWheelSpeeds just represents the speed of the right and left wheels, it doesn’t set them or calculate anything.

What are you trying to achieve? If you’re trying to follow the Trajectory Tutorial you would use getSelectedSensorVelocity() to get the velocity, and convert it to the proper units.

The curly brace on line 62 is suspicious. Are you defining your motor variables inside a method, or is that a stray curly brace?

  1. Yes, I am following the trajectory tutorial and thank you for clarifying what i have to use
  2. It doesnt fit in the ss but the curly brace is the latter half of the one for my public drivetrain

Its not directly related to your original question, but you are shadowing your motor and controller group variables in the constructor. You are going to get a NullPointerException on line 69 because rightMotor1 is null.

Instead of:

public Drivetrain {
    WPI_TalonFX rightMotor1 = new WPI_TalonFX(Constants.RIGHT_MOTOR_1);
    ...
}

You need to remove the type so you are not defining a new variable:

public Drivetrain {
    rightMotor1 = new WPI_TalonFX(Constants.RIGHT_MOTOR_1);
    ...
}

Or you could move the assignment of the variables outside of the constructor, like in the example code.

Thats interesting, how come rightmotor1 is null? In any case, i removed the type. Is this better?

Take a look at the link I shared about shadowing. Inside the constructor you were defining a local variable with the same name as an instance variable. You set a value to the local variable, but the instance variable remains null because it was never set.

You still have the issue with your rightMotors and leftMotors variables. You might never notice it if you don’t reference the instance variables anywhere, but they will be null.

You can use our simulator project to reference some working code using Falcons and the integrated encoders to follow the trajectory tutorial.

Things to pay attention to:
You’ll need to know how to convert the TalonFX API return values to units that make sense in the context of the trajectory. CTRE does that weird thing where the velocity is returning a value in units per 100 milliseconds, rather than something more sane like RPM or RPS.

You’ll need to make sure your gear ratios match up when doing the math.

Then just follow the code very closely in the trajectory tutorial, and try not to deviate too much from the example code.

Ah, I see. Now that I think about it I dont even know why I defined it twice.

1 Like

First, thank you for the reference code. Second, in the case that CTRE returns the value in per 100 ms then what would I have to change this math to? (This was put under the assumption that velocity would be returned in rpm)

The example repository already shows you how to do the math, and it functions with the trajectory tutorial from wpilib.

Just take a look at the periodic() method in the link I gave you and you can see what the math needs to be.

Also, the math in that example is taken directly from the CTRE example code available on github as well.

Alright, this has cleared up a lot of misunderstanding for me. Thank you

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