|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
|
|
Thread Tools | Rate Thread | Display Modes |
|
|
|
#1
|
|||
|
|||
|
Passing in variables into a command via command group
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?
|
|
#2
|
|||||
|
|||||
|
Re: Passing in variables into a command via command group
Quote:
|
|
#3
|
|||
|
|||
|
Re: Passing in variables into a command via command group
Code:
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));
}
}
Code:
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() {
}
}
Last edited by VaneRaklan : 22-02-2015 at 15:42. |
|
#4
|
|||||
|
|||||
|
Re: Passing in variables into a command via command group
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. |
|
#5
|
|||
|
|||
|
Re: Passing in variables into a command via command group
Done.
|
|
#6
|
|||||
|
|||||
|
Re: Passing in variables into a command via command group
Am I correct in assuming this is an issue of it just not working, not an issue of a compilation error?
|
|
#7
|
|||
|
|||
|
Re: Passing in variables into a command via command group
Yes.
|
|
#8
|
|||||
|
|||||
|
Re: Passing in variables into a command via command group
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. Code:
private double power;
public AutoDrive(double x) {
...
Code:
private double power;
public AutoDrive(double x) {
power = x;
}
Your execute() should look like this: Code:
protected void execute() {
Robot.driveWithJoystick.mecanumDrive_Cartesian123(0,0,0,0);
}
Code:
protected boolean isFinished() {
return RobotMap.frontLeftEnc.get() > 1500 && RobotMap.frontRightEnc.get() > 1500;
|
|
#9
|
|||
|
|||
|
Re: Passing in variables into a command via command group
Thank you very much! I guess I had a placement problem with my variable and where i was instantiating something. But anyways, thank you
![]() |
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|