Log in

View Full Version : Passing in variables into a command via command group


VaneRaklan
22-02-2015, 15:08
Hello people of the world! Its me again, with another question pertaining to programming. So I am having trouble figuring out how to pass an argument into a command in a Command Group. I don't know if it a formatting thing within the command itself or if I am doing something else wrong. What I mean by passing the argument is say I have addSequential(new AutoDrive()); and I want to pass in a speed for the different times that I want to call it. So in turn i would say addSequential(new AutoDrive(.5)); or AutoDrive(1) or something like that. Anyone know how to do this?

notmattlythgoe
22-02-2015, 15:14
Hello people of the world! Its me again, with another question pertaining to programming. So I am having trouble figuring out how to pass an argument into a command in a Command Group. I don't know if it a formatting thing within the command itself or if I am doing something else wrong. What I mean by passing the argument is say I have addSequential(new AutoDrive()); and I want to pass in a speed for the different times that I want to call it. So in turn i would say addSequential(new AutoDrive(.5)); or AutoDrive(1) or something like that. Anyone know how to do this?

What you have written should be correct. Can you post the Command Group in question?

VaneRaklan
22-02-2015, 15:29
package org.usfirst.frc2557.SOTABots2015.commands;

import edu.wpi.first.wpilibj.command.CommandGroup;

/**
*
*/
public class Autonomous extends CommandGroup {

public Autonomous() {
// Add Commands here:
// e.g. addSequential(new Command1());
// addSequential(new Command2());
// these will run in order.

// To run multiple commands at the same time,
// use addParallel()
// e.g. addParallel(new Command1());
// addSequential(new Command2());
// Command1 and Command2 will run in parallel.

// A command group will require all of the subsystems that each member
// would require.
// e.g. if Command1 requires chassis, and Command2 requires arm,
// a CommandGroup containing them would require both the chassis and the
// arm.
// addSequential(new BackHook());
addSequential(new AutoDrive(.2)); //has to change
addSequential(new AutoDrive(-.2));
}
}
^^^^Mainly focusing on the second and third sequentials (AutoDrive)^^^^


package org.usfirst.frc2557.SOTABots2015.commands;

import org.usfirst.frc2557.SOTABots2015.Robot;
import org.usfirst.frc2557.SOTABots2015.RobotMap;

import edu.wpi.first.wpilibj.command.Command;

/**
*
*/
public class AutoDrive extends Command {

public AutoDrive(double x) {
// Use requires() here to declare subsystem dependencies
// eg. requires(chassis);
}

// Called just before this Command runs the first time
protected void initialize() {
RobotMap.frontLeftEnc.reset();
RobotMap.frontRightEnc.reset();
RobotMap.rearLeftEnc.reset();
RobotMap.rearRightEnc.reset();

}

// Called repeatedly when this Command is scheduled to run
protected void execute() {
while(RobotMap.frontLeftEnc.get() < 1500 & RobotMap.frontRightEnc.get() < 1500){
Robot.driveWithJoystick.mecanumDrive_Cartesian123( 0,0,0,0);
}
}

// Make this return true when this Command no longer needs to run execute()
protected boolean isFinished() {
return true;
}

// Called once after isFinished returns true
protected void end() {
Robot.driveWithJoystick.mecanumDrive_Cartesian123( 0,0,0,0);
}

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


}
^^^This is the command that I am trying to call.^^^

notmattlythgoe
22-02-2015, 15:31
Could you do me a favor and paste that code into code tags? To do this place a [/code] after the code and a [code] before it.

Thanks.

defied
22-02-2015, 15:56
Done.

notmattlythgoe
22-02-2015, 16:02
Am I correct in assuming this is an issue of it just not working, not an issue of a compilation error?

defied
22-02-2015, 17:05
Yes.

notmattlythgoe
22-02-2015, 17:20
Yes.

Great, there are a couple of things going on that are easy to fix.

First, you have done a good job of passing the value into the AutoDrive command. Now that it is in the command you need to actually do something with it. First off, we need a global place to store it so it is accessible out side of the constructor. I would also suggest naming the parameter something meaningful like power, or drivePower. It will make your code more readable in the future when you go back to it.


private double power;

public AutoDrive(double x) {
...


Now that you have a place to store it you need to store the passed in value in the new variable.


private double power;

public AutoDrive(double x) {
power = x;
}


You now have access to this value to use in execute(). But, your execute method is going to cause you problems. In the Command Base structure you want to avoid using long executing loops of any kind. Instead, you want to use isFinished() to determine with the command should finish.

Your execute() should look like this:


protected void execute() {
Robot.driveWithJoystick.mecanumDrive_Cartesian123( 0,0,0,0);
}


Then your isFinished() should look like this, this will stop the command once both encoders are reporting values greater than 1500:


protected boolean isFinished() {
return RobotMap.frontLeftEnc.get() > 1500 && RobotMap.frontRightEnc.get() > 1500;


This should fix most of your problems. Make those changes and post your updated code. Let me know if you have any questions.

VaneRaklan
22-02-2015, 19:21
Thank you very much! I guess I had a placement problem with my variable and where i was instantiating something. But anyways, thank you :)