pass a value to a command.

I have a command that spins the robot x degrees from current gyro heading. I can use a smartdashboard.getnumber when I’m in teleop. I would like to use this command not referencing the smartdashboard in autonomous passing unique x degrees at different steps. When I add a double to one of the the addsequential(new command(90)) it asked me to add a parameter to the command. I said yes and use this not the smartdashboard.get. I then add a value to each …(new command(#)). After hours of test and debugging I found the each of the commands reacts as the last …(new command(#)).

I hope the rambling makes sense. I can try and post some sample code when I’m back to the development pc.

Inside of the command you will probably see a constructor at the top (a constructor is a method with the same name as the command). The constructor should have 1 int parameter. Create a new global variable, and in the constructor assign the parameter to the global variable. Then replace SmartDashboard.get with the global variable.

Is what I had:


public class  DriveSpinInPlace extends Command {
	
    private static LinkedList<Double> fifo = null;
    private static double averagegyro = 0;
    private static boolean sample_gyro = false;
    private static double spin = 0;                         //Declared my "spin" Var 
    private static double spinTarget = 0;
    
    public DriveSpinInPlace(double s) {
        
        requires(Robot.driveSystem);
        spin = s;                                         //Set the spin = passed par
    }

    protected void initialize() {
    	
    	sample_gyro = false;
    	fifo = new LinkedList<Double>();
    	fifo.clear();	
    }

   
    protected void execute() {
        //Do my code
    }
 protected boolean isFinished() {
        return sample_gyro && averagegyro > spinTarget-10 && averagegyro < spinTarget+10;
    }

   
    protected void end() {
    	Robot.driveSystem.driveStraight(0,0);
    }

    // Called when another command which requires one or more of the same
    // subsystems is scheduled to run
    protected void interrupted() {
    	end();
    }
}


But all ways reacted to last addSequential(new DriveSpinInPlace(90));
If I have:


addSequential(new DriveSpinInPlace(90))
addSequential(new DriveSpinInPlace(-180))
addSequential(new DriveSpinInPlace(90))
addSequential(new DriveSpinInPlace(-95))
addSequential(new DriveSpinInPlace(-45))

Every all 5 scheduled commands will do -45.

Am I declaring the correct type of variable?

Yes. Get rid of “static” in front of every variable. That should fix your problem.

What static does is that it basically means the variable will have the same value for every command that you create. And because of the way that CommandGroups work (basically, it will call the constructor of every single command the moment it is created), the last value that you set will be the value that stays in your spin variable for the entire execution.

Thank you I’ll give it a go. The Static was added automatically.

That worked thanks. But you knew that already didn’t you?