Limelight code not working for our robot

Here is our code that is not working. We don’t understand why it is not working:

package frc.robot;
import edu.wpi.first.wpilibj.Timer;
import com.ctre.phoenix.motorcontrol.*;
import com.ctre.phoenix.motorcontrol.can.*;
import edu.wpi.first.wpilibj.Joystick;
import edu.wpi.first.wpilibj.TimedRobot;
import edu.wpi.first.networktables.NetworkTable;
import edu.wpi.first.networktables.NetworkTableEntry;
import edu.wpi.first.networktables.NetworkTableInstance;

import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;

public class Robot extends TimedRobot {
    // Motor controllers
    TalonSRX _talon0 = new TalonSRX(1);
    TalonSRX _talon1 = new TalonSRX(3);
    TalonSRX _talon2 = new TalonSRX(5);
    TalonSRX _talon3 = new TalonSRX(2);
    TalonSRX _talon4 = new TalonSRX(4);
    TalonSRX _talon5 = new TalonSRX(6);

    // Joystick (for teleop)
    Joystick _joystick = new Joystick(0);

    // Autonomous variables
    private Timer autoTimer = new Timer();
    private final double AUTO_TIME = 15.0; // Autonomous period in seconds
    private final double TARGET_DISTANCE = 5.0; // Target distance in feet
    private boolean reachedTarget = false;

    // Network Tables for Limelight
    NetworkTable limelightTable;

    @Override
    public void robotInit() {
        // Initialize Network Tables
        limelightTable = NetworkTableInstance.getDefault().getTable("limelight");
        

        // Robot initialization code here (if any)
    }

    @Override
    public void autonomousInit() {
        // Reset and start the autonomous timer
        autoTimer.reset();
        autoTimer.start();
        reachedTarget = false;

        // Initial motor configuration for autonomous
        _talon0.setInverted(false);
        _talon2.setInverted(false);
        _talon4.setInverted(false);
        _talon1.setInverted(true);
        _talon3.setInverted(true);
        _talon5.setInverted(true);
    }

    @Override
    public void autonomousPeriodic() {
        // Check if the autonomous period is still active and the target hasn't been reached
        // if (autoTimer.get() < AUTO_TIME && !reachedTarget) {
            // Get Limelight data
            NetworkTableEntry tv = limelightTable.getEntry("tv"); // Whether the limelight has any valid targets (0 or 1)
            NetworkTableEntry tx = limelightTable.getEntry("tx"); // Horizontal Offset From Crosshair To Target (-27 degrees to 27 degrees)
            NetworkTableEntry ty = limelightTable.getEntry("ty"); // Vertical Offset From Crosshair To Target (-20.5 degrees to 20.5 degrees)
            double targetVisible = tv.getDouble(0);
            double targetX = tx.getDouble(0);Preformatted text
            double targetY = ty.getDouble(0);
            SmartDashboard.putNumber("TX", targetX);
            SmartDashboard.putNumber("TV", targetVisible);
            // if (targetVisible) {
             
                double steer = targetX * 0.3;
                double drive = -0.02 * targetY;
                drive = Math.max(drive, 0.4);

                double leftCommand = drive + steer;
                double rightCommand = drive - steer;

                _talon0.set(ControlMode.PercentOutput, leftCommand);
                _talon1.set(ControlMode.PercentOutput, leftCommand);
                _talon2.set(ControlMode.PercentOutput, leftCommand);
                _talon3.set(ControlMode.PercentOutput, rightCommand);
                _talon4.set(ControlMode.PercentOutput, rightCommand);
                _talon5.set(ControlMode.PercentOutput, rightCommand);
            // } else {
                // Stop or search for target
                // stopMotors();
            // }

        // } else {
            // Stop the robot
            // stopMotors();
        // }
    }

    void stopMotors() {
      _talon0.set(ControlMode.PercentOutput, 0);
      _talon1.set(ControlMode.PercentOutput, 0);
      _talon2.set(ControlMode.PercentOutput, 0);
      _talon3.set(ControlMode.PercentOutput, 0);
      _talon4.set(ControlMode.PercentOutput, 0);
      _talon5.set(ControlMode.PercentOutput, 0);
    }

    @Override
    public void teleopPeriodic() {
    }
  }

What type of errors are you getting? Are the limelight values being outputted to your smartdashboard?

1 Like

Hi, thank you for your response. When we output the limelight values to the SmartDashboard, the “tx” value is 0, and the “tv” value is 0 as well.

The first thing I would try checking is that you are getting real values from the limelight web interface and that it is set up properly. Documentation and set-up steps

If you are getting the correct values in the web interface you could try making a separate Limelight subsystem and checking if the issue is with the way Network Tables are being checked. I haven’t been able to test this code so please let me know if it works for you. Github Code!