Passing parameter to commands

We are struggling to pass parameters to a command. We have a PIDController in a Command, and we are trying to program a couple of buttons to call the same Command with different setpoints.

new JoystickButton(driverController, XboxController.Button.kRightBumper.value).onTrue(new armExtendToValue(m_armsystem, 0));
new JoystickButton(driverController, XboxController.Button.kLeftBumper.value).onTrue(new armExtendToValue(m_armsystem, 5000));

The issue is that it will keep only the setpoint of the 2nd command created. So it always try to reach 5000. It does not matter which button you are using…

Any insight would be greatly appreciated!

The problem is likely in your arm subsystem or in the armExtendToValue command.

Can you link to the rest of your code?

I’m willing to bet the 2nd argument youre passing to your command (the 5000 value) is being saved to a static member of that class

package frc.robot.commands;
import edu.wpi.first.math.controller.PIDController;
import edu.wpi.first.wpilibj2.command.CommandBase;
import frc.robot.Constants;
import frc.robot.subsystems.ArmSystem;
public class armExtendTo0 extends CommandBase {
private static ArmSystem m_armsystem;
private static PIDController m_pidController;
/** Creates a new armExtendToValue. */
public armExtendTo0(ArmSystem sub1) {
// Use addRequirements() here to declare subsystem dependencies.
m_armsystem = sub1;
System.out.println(“Initialization of destination”+0);
addRequirements(m_armsystem);
m_pidController = new PIDController(Constants.PID_ARM_P, Constants.PID_ARM_I, Constants.PID_ARM_D);
m_pidController.setSetpoint(0);
m_pidController.setTolerance(Constants.kTurnToleranceDeg, Constants.kTurnRateToleranceDegPerS);
}
// Called when the command is initially scheduled.
@Override
public void initialize() {
}
// Called every time the scheduler runs while the command is scheduled.
@Override
public void execute() {
double output = m_pidController.calculate(m_armsystem.getPosition(), 0);
m_armsystem.extendArm(output);
}
// Called once the command ends or is interrupted.
@Override
public void end(boolean interrupted) {
m_armsystem.stopArmMotor();
}
// Returns true when the command should end.
@Override
public boolean isFinished() {
return m_pidController.atSetpoint();
}
// Commands to check error and output
public static double getError() {
return m_pidController.getPositionError();
}
public static double getOutput() {
return m_pidController.calculate(m_armsystem.getPosition());
}
}

exactly what was stated above, these should not be static. Static makes the values shared across all instances of your class so when you change them, it overwrites the ones created previously.

It won’t matter for the subsystem object since there’s only ever one copy of that around anyway, but the PIDController should definitely not be static

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