Java FRC CAN ID

I am a new programmer for my HS robotics team. This year, our team switched from NI Lab view to FRC Java. I have the can ID’s from the Lab view code and I am trying to write the ID’s into Java. I have had a difficult time finding any information on how to do this, and I am asking if anyone else has any information on what to do.

If you have the CAN IDs, you can just input them when declaring your motor controllers:

REVLib:
https://codedocs.revrobotics.com/java/com/revrobotics/cansparkmax#<init>(int,com.revrobotics.CANSparkMaxLowLevel.MotorType)

private CANSparkMax leftDrive = new CANSparkMax({CANID}, MotorType.kBrushless);

CTRE:
https://store.ctr-electronics.com/content/api/java/html/classcom_1_1ctre_1_1phoenix_1_1motorcontrol_1_1can_1_1_talon_f_x.html#a2385514ebf8710d2bf8af59a701fc501

private TalonFX leftDrive = new TalonFX({CANID}, {CAN bus});

A good way to keep your CAN ID’s organized is by using a Constants file. Any numbers that have significant meaning and that don’t change should be placed in this file. Here is an example of our Constants file on 5406 from this year:

1 Like

Thank you, this should help me get back on track.

1 Like

I ended up having another issue. I went to the two motor controller websites and tried to get the imports needed for declaring CAN motor controllers, but when I copied the imports into my code it would say that it cannot resolve the imports. An example import for the Talon import is

import com.ctre.phoenix.motorcontrol.can.TalonSRX;

Have you ever had any issues with this?

Do you have the vendor dependencies installed?

2 Likes

That is probably the issue, I was unaware those existed. I will check that shortly.

1 Like

3rd Party Libraries — FIRST Robotics Competition documentation for reference in installing the dependencies.

1 Like

I’d recommend reading and following the CTRE documentation here: Welcome to Phoenix’s documentation! — Phoenix documentation

1 Like