SmartDashboard Lesson

SmartDashboard: A Window Into the Robot's Brain

The SmartDashboard is a program on your driver station that provides a live, customizable display of data from your robot. It's one of the most critical tools for debugging, tuning, and operating your robot.

What is SmartDashboard?

Think of the dashboard in a car: it shows you vital information like speed, fuel, and engine temperature. The SmartDashboard is the exact same concept for your robot. Instead of printing values to a console, you can send them to the dashboard to create a clean, visual, and interactive display.

Why is it Essential?

  • Real-Time Sensor Feedback: Watch sensor values like encoder distance or gyro angle live as the robot moves. This is invaluable for diagnosing problems.
  • On-the-Fly Tuning: Create editable fields to change values in your code (like motor speeds or PID constants) without redeploying code, saving huge amounts of time.
  • Interactive Controls: Add buttons to the dashboard that can trigger commands on your robot, perfect for testing mechanisms or autonomous routines.

Sending Data to the Dashboard

Sending data is incredibly simple using the `SmartDashboard` class. You give each piece of data a unique name (a "key") and the value you want to send. This code is typically placed in a subsystem's `periodic()` method to be updated continuously.

// At the top of your subsystem file:
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;

// Inside the periodic() method of an ArmSubsystem:
@Override
public void periodic() {
    // Send a number (e.g., from an encoder)
    SmartDashboard.putNumber("Arm Angle", getAngle());

    // Send a boolean (e.g., from a limit switch)
    SmartDashboard.putBoolean("Is Arm at Bottom", isAtBottomLimit());

    // Send a string (e.g., the current state of a state machine)
    SmartDashboard.putString("Arm State", m_currentState.toString());
}
    

Getting Data from the Dashboard (Tuning)

This is the most powerful feature of SmartDashboard. You can use the `get...` methods to read values from the dashboard, allowing you to create tunable constants. The key is to provide a default value. If the key doesn't exist on the dashboard yet, the robot will safely use the default you provided.

// In your Constants.java file or in a subsystem's constructor:
private double INTAKE_SPEED;

// In the subsystem's constructor, read the value from SmartDashboard.
public IntakeSubsystem() {
    // Put the value on the dashboard so it's editable
    SmartDashboard.putNumber("Intake Speed", 0.7); 
}

// In the subsystem's periodic() method, read it back every loop.
@Override
public void periodic() {
    // Get the value. If it's not on the dashboard, default to 0.7.
    INTAKE_SPEED = SmartDashboard.getNumber("Intake Speed", 0.7);
}

// In your command:
public void execute() {
    m_intake.setSpeed(Constants.INTAKE_SPEED); // Uses the tunable value!
}
    

Now, you can double-click the "Intake Speed" field on the SmartDashboard, change its value from 0.7 to 0.5, and the robot will instantly start using the new speed without a code deploy!

Test Your Knowledge

Question: When using `SmartDashboard.getNumber("My Value", 0.5);` to create a tunable constant, what is the purpose of the second parameter, `0.5`?