Go to Post Note to self: Before turning 30, remember to remove the birthdate year from Delphi. - Jessica Boucher [more]
Home
Go Back   Chief Delphi > Technical > Programming > Java
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Reply
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 22-02-2015, 15:08
VaneRaklan VaneRaklan is offline
Registered User
FRC #2557 (The SOTABots)
Team Role: Programmer
 
Join Date: Feb 2015
Rookie Year: 2014
Location: Tacoma, WA
Posts: 18
VaneRaklan is an unknown quantity at this point
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?
Reply With Quote
  #2   Spotlight this post!  
Unread 22-02-2015, 15:14
notmattlythgoe's Avatar
notmattlythgoe notmattlythgoe is offline
Flywheel Police
AKA: Matthew Lythgoe
FRC #2363 (Triple Helix)
Team Role: Mentor
 
Join Date: Feb 2010
Rookie Year: 2009
Location: Newport News, VA
Posts: 1,725
notmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond repute
Re: Passing in variables into a command via command group

Quote:
Originally Posted by VaneRaklan View Post
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?
Reply With Quote
  #3   Spotlight this post!  
Unread 22-02-2015, 15:29
VaneRaklan VaneRaklan is offline
Registered User
FRC #2557 (The SOTABots)
Team Role: Programmer
 
Join Date: Feb 2015
Rookie Year: 2014
Location: Tacoma, WA
Posts: 18
VaneRaklan is an unknown quantity at this point
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));
    }
}
^^^^Mainly focusing on the second and third sequentials (AutoDrive)^^^^


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() {
    }

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

Last edited by VaneRaklan : 22-02-2015 at 15:42.
Reply With Quote
  #4   Spotlight this post!  
Unread 22-02-2015, 15:31
notmattlythgoe's Avatar
notmattlythgoe notmattlythgoe is offline
Flywheel Police
AKA: Matthew Lythgoe
FRC #2363 (Triple Helix)
Team Role: Mentor
 
Join Date: Feb 2010
Rookie Year: 2009
Location: Newport News, VA
Posts: 1,725
notmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond repute
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.
Reply With Quote
  #5   Spotlight this post!  
Unread 22-02-2015, 15:56
defied defied is offline
Registered User
FRC #2557
 
Join Date: Jan 2014
Location: Tacoma, WA
Posts: 23
defied is an unknown quantity at this point
Re: Passing in variables into a command via command group

Done.
Reply With Quote
  #6   Spotlight this post!  
Unread 22-02-2015, 16:02
notmattlythgoe's Avatar
notmattlythgoe notmattlythgoe is offline
Flywheel Police
AKA: Matthew Lythgoe
FRC #2363 (Triple Helix)
Team Role: Mentor
 
Join Date: Feb 2010
Rookie Year: 2009
Location: Newport News, VA
Posts: 1,725
notmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond repute
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?
Reply With Quote
  #7   Spotlight this post!  
Unread 22-02-2015, 17:05
defied defied is offline
Registered User
FRC #2557
 
Join Date: Jan 2014
Location: Tacoma, WA
Posts: 23
defied is an unknown quantity at this point
Re: Passing in variables into a command via command group

Yes.
Reply With Quote
  #8   Spotlight this post!  
Unread 22-02-2015, 17:20
notmattlythgoe's Avatar
notmattlythgoe notmattlythgoe is offline
Flywheel Police
AKA: Matthew Lythgoe
FRC #2363 (Triple Helix)
Team Role: Mentor
 
Join Date: Feb 2010
Rookie Year: 2009
Location: Newport News, VA
Posts: 1,725
notmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond reputenotmattlythgoe has a reputation beyond repute
Re: Passing in variables into a command via command group

Quote:
Originally Posted by defied View Post
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.

Code:
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.

Code:
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:

Code:
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:

Code:
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.
Reply With Quote
  #9   Spotlight this post!  
Unread 22-02-2015, 19:21
VaneRaklan VaneRaklan is offline
Registered User
FRC #2557 (The SOTABots)
Team Role: Programmer
 
Join Date: Feb 2015
Rookie Year: 2014
Location: Tacoma, WA
Posts: 18
VaneRaklan is an unknown quantity at this point
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
Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -5. The time now is 18:07.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi