Help Passing Parameters in Command Based

Hello,

We are trying out command based programming and we have run into an issue that need some assistance. We have a button that triggers an AutoTestCommandGroup in which we get the current angle from the gyro, add 90 and then pass it to a “Turn” command which then rotates the robot 90 degrees.

It works fine but it only works once - the very first time it is run. We would like to be able to do this every time the button is pressed.

In other words, we would like the “target angle” parameter to be passed to the “Turn” command every time the button is pressed. Is there a specific way to format the AutoTestCommandGroup so that a new parameter gets passed every time a button is pressed?

simplified code follows (“testAngle” is the parameter that we would like to get passed every time the button is pressed):

--------AutoTestCommandGroup----------------

public class AutoTestCommandGroup extends CommandGroup {

public AutoTestCommandGroup() {        
	double testAngle = RobotMap.ahrs.getAngle() + 90;
	addSequential(new Turn(testAngle));

------------ Turn command -----------------

public class Turn extends Command {

public double inputAngle;
public double inputAngle2;

public Turn (double inputAngle) {
  	this.inputAngle=inputAngle;
    requires(Robot.driveT);
}
     protected void initialize() {	
	    }
protected void execute() {
	   	inputAngle2 =RobotMap.ahrs.getAngle();
    if (inputAngle2<inputAngle){
		DriveTrainSubsystem.drive.arcadeDrive(0,-.6);
	}
protected boolean isFinished() {
	    	return true;
}

    protected void end() {
}
protected void interrupted() {
	end();

Are you creating a new AutoTestCommandGroup() each time the button is pressed? If you re-use the old command, it will still have the old values.

The problem seems to be that the Constructor for the CommandGroup isn’t going to be re-run each time you start the CommandGroup, so the passed value won’t be updated.

Personally, what I would do is setup the Turn Command so you pass how many degrees you want to turn, instead of the new target angle itself, then in the Turn Command you get the starting position in the initialize, and then just compare them to how far you wanted to turn.

The cleanest way to do this is to use lambdas; pass a supplier instead of a value.