So I’ve been working on PID control and I found the PIDSubsystem class. I’ve been doing some research onto how this would integrate into a drive train and I think I have programmed a subsystem. My only issue is that I don’t understand what the double output
is used for in useOutput()
method. The WPIlib example uses it for a feedforward but it’s part of the default method so I can’t change it. Does that output need to be used in a specific way?
I’m also a little confused about the PIDCommand class. Do you have to use a PIDCommand with a PIDSubsystem or can you use a regular command? If I do use PIDCommand, how can I make it work with my PIDSubsystem?
here’s my subsystem
package frc.robot.subsystems;
import com.revrobotics.CANSparkMax;
import com.revrobotics.CANSparkMaxLowLevel;
import edu.wpi.first.wpilibj.CounterBase.EncodingType;
import edu.wpi.first.wpilibj.controller.PIDController;
import edu.wpi.first.wpilibj.Encoder;
import edu.wpi.first.wpilibj2.command.PIDSubsystem;
import frc.robot.Constants;
public class AutoDriveTrain extends PIDSubsystem {
CANSparkMax leftLeader = new CANSparkMax(1, CANSparkMaxLowLevel.MotorType.kBrushless);
CANSparkMax rightLeader = new CANSparkMax(3, CANSparkMaxLowLevel.MotorType.kBrushless);
CANSparkMax leftFollower = new CANSparkMax(2, CANSparkMaxLowLevel.MotorType.kBrushless);
CANSparkMax rightFollower = new CANSparkMax(4, CANSparkMaxLowLevel.MotorType.kBrushless);
Encoder driveEncoder = new Encoder(0, 1, false, EncodingType.k4X);
public AutoDriveTrain() {
super(new PIDController(Constants.kP, Constants.kI, Constants.kD));
leftFollower.follow(leftLeader, false);
rightFollower.follow(rightLeader, false);
driveEncoder.reset();
}
@Override
public void periodic() {
}
@Override
public void useOutput(double output, double setpoint) {
leftLeader.set(output + getController().calculate(getMeasurement(), setpoint));
rightLeader.set(output + getController().calculate(getMeasurement(), setpoint));
}
@Override
public double getMeasurement() {
return driveEncoder.get();
}
}
Thanks!!