How to use PIDCommand

Hello, I’m trying to create a command that moves to a specific angle(to a target provided by PhotonVision).
So I created a PIDCommand in Java, pic :

// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.

package frc.robot.commands;

import org.photonvision.targeting.PhotonPipelineResult;
import edu.wpi.first.math.controller.PIDController;
import edu.wpi.first.wpilibj.ADIS16448_IMU;
import edu.wpi.first.wpilibj2.command.PIDCommand;
import frc.robot.Constants.VisionConstants;
import frc.robot.subsystems.DriveTrain;
// NOTE:  Consider using this command inline, rather than writing a subclass.  For more
// information, see:
public class TurnPID extends PIDCommand {
  /** Creates a new TurnPID. */
  public TurnPID(ADIS16448_IMU imu, DriveTrain drivetrain, PhotonPipelineResult result) {
        // The controller that the command will use
        new PIDController(VisionConstants.kTurnP, VisionConstants.kTurnI, VisionConstants.kTurnD),
        // Thiss should return the measurement
        () -> imu.getAngle(),
        // This should return the setpoint (can also be a constant)
        () -> imu.getAngle() + result.getBestTarget().getYaw(),
        // This uses the output
        output -> {
          drivetrain.arcadeDrive(0, output, true);
    // Use addRequirements() here to declare subsystem dependencies.
    getController().enableContinuousInput(-180, 180);
    // Configure additional PID options by calling `getController` here.
    .setTolerance(VisionConstants.kTurnToleranceDeg, VisionConstants.kTurnRateToleranceDegPerS);

  // Returns true when the command should end.
  public boolean isFinished() {
    return getController().atSetpoint();

The question is, how do I use the PIDCommand, couldn’t manage to understand.

My team doesn’t really use them a lot, but looking at the documentation, it seems like all you need to do is schedule it once and it will continue sending output to the DoubleConsumer until isFinished returns true.

The code you have looks like it would work.

WPILib has step-by-step instructions on how to code a PIDCommand, along with some examples

I’m not exactly sure how to use it tho, I am generally kinda new to FRC and Robotics as a whole, so I’m handling here a sizeable learning curve.

I got this PIDCommand, and I got another command to use this PIDCommand to turn(which I heard is needed based on my local teammates), what do I schedule, this command or the other command that uses this? and if I need to use the other command that uses this, how do I use the PIDCommand in it?

I coded a PIDCommand, I’m just not exactly sure how to use it, do I use it in another command? if so how? or do I just use it as any other command?

Could you share your code? I dont see the need for puting that command inside of another command. You should just be able to schedule that command when you want to run it.

The code for the other command is not exactly ready, kinda in a need of a rewrite as soon as I determine if I actually need it or do I just schedule the PIDCommand and add the stuff I would normally put in a command in the execute() method in PIDCommand

What code would go in the execute method?

The pidcommand should automatically call whatever method you passed in, in this case your arcade drive method.

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