Go to Post FIRST should come with a warning label. - Tim Sharp [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 27-01-2015, 13:44
Ether's Avatar
Ether Ether is offline
systems engineer (retired)
no team
 
Join Date: Nov 2009
Rookie Year: 1969
Location: US
Posts: 8,043
Ether has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond repute
Re: Command Based Programming (Threads?)


So this codeA:
Code:
public FiringCommandGroup extends CommandGroup {

     public FiringCommandGroup() {
          addSequential(new FireCommand1());
          addSeqiential(new waitCommand(1));
          addSequential(new FireCommand2());
     }
}
...is quite different than this codeB:
Code:
public void execute() {
     if (timeSinceInitialized() < sometime) {
          do something;
     } else if(timeSinceInitialized() < some bigger time) {
          do nothing;
     } else {
          do something2;
     }
}
...because codeA executes FireCommand1() once, then waits, then executes FireCommand2() once, and exits.

whereas codeB executes "something" repeatedly, then waits, then executes "something2" repeatedly.

Yes?


Reply With Quote
  #2   Spotlight this post!  
Unread 27-01-2015, 13:47
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,715
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: Command Based Programming (Threads?)

Quote:
Originally Posted by Ether View Post

So this codeA:
Code:
public FiringCommandGroup extends CommandGroup {

     public FiringCommandGroup() {
          addSequential(new FireCommand1());
          addSeqiential(new waitCommand(1));
          addSequential(new FireCommand2());
     }
}
...is quite different than this codeB:
Code:
public void execute() {
     if (timeSinceInitialized() < sometime) {
          do something;
     } else if(timeSinceInitialized() < some bigger time) {
          do nothing;
     } else {
          do something2;
     }
}
...because codeA executes FireCommand1() once, then waits, then executes FireCommand2() once, and exits.

whereas codeB executes "something" repeatedly, then waits, then executes "something2" repeatedly.

Yes?


Correct. I can write up something that would be similar to code A in the second style if you'd like.
Reply With Quote
  #3   Spotlight this post!  
Unread 27-01-2015, 14:18
Ether's Avatar
Ether Ether is offline
systems engineer (retired)
no team
 
Join Date: Nov 2009
Rookie Year: 1969
Location: US
Posts: 8,043
Ether has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond repute
Re: Command Based Programming (Threads?)

Quote:
Originally Posted by notmattlythgoe View Post
Correct. I can write up something that would be similar to code A in the second style if you'd like.
Yes, thank you, that is what I was asking originally.


Reply With Quote
  #4   Spotlight this post!  
Unread 27-01-2015, 14:52
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,715
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: Command Based Programming (Threads?)

Quote:
Originally Posted by Ether View Post
Yes, thank you, that is what I was asking originally.


Apologies, here you go.

Code:
public class ShootCommand extends Command {

	public void initialize() {
		logic for fire1;
	}

	public void execute() {
		
	}
	
	public boolean isFinished() {
		return timeSinceInitialized() > 1;
	}
	
	public void end() {
		logic for fire 2;
	}
}
1. Run fire1 logic when command starts.
2. Do nothing for 1 second.
3. After 1 second return true in isFinished().
4. When isFinished() returns true the end() method is called.
5. Run logic for fire2 in end.

Keep in mind, the logic for both fire1 and fire2 will need to execute quickly. Meaning no Timer.delays or long running loops.

Last edited by notmattlythgoe : 27-01-2015 at 15:19.
Reply With Quote
  #5   Spotlight this post!  
Unread 27-01-2015, 17:49
Ether's Avatar
Ether Ether is offline
systems engineer (retired)
no team
 
Join Date: Nov 2009
Rookie Year: 1969
Location: US
Posts: 8,043
Ether has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond repute
Re: Command Based Programming (Threads?)

Quote:
Originally Posted by notmattlythgoe View Post
Code:
public class ShootCommand extends Command {

	public void initialize() {
		logic for fire1;
	}

	public void execute() {
		
	}
	
	public boolean isFinished() {
		return timeSinceInitialized() > 1;
	}
	
	public void end() {
		logic for fire 2;
	}
}
1. Run fire1 logic when command starts.
2. Do nothing for 1 second.
3. After 1 second return true in isFinished().
4. When isFinished() returns true the end() method is called.
5. Run logic for fire2 in end.
OK thanks that's pretty clear. Is there any performance difference between the approach shown above and the code in post#15? Which approach do you prefer, and why (or does it depend on context)?


Reply With Quote
  #6   Spotlight this post!  
Unread 27-01-2015, 20:49
Arhowk's Avatar
Arhowk Arhowk is offline
FiM CSA
AKA: Jake Niman
FRC #1684 (The Chimeras) (5460 Mentor)
 
Join Date: Jan 2013
Rookie Year: 2013
Location: Lapeer
Posts: 542
Arhowk is a splendid one to beholdArhowk is a splendid one to beholdArhowk is a splendid one to beholdArhowk is a splendid one to beholdArhowk is a splendid one to beholdArhowk is a splendid one to behold
Re: Command Based Programming (Threads?)

Quote:
Originally Posted by Ether View Post
OK thanks that's pretty clear. Is there any performance difference between the approach shown above and the code in post#15? Which approach do you prefer, and why (or does it depend on context)?


Performance-wise the latter would be better, since the post in number #15 has three elements in the list whereas the post that you quoted only as one element in the list. Every 20ms, every element in the list is enumerated upon and conditions for execution are checked. This performance boost is negligible unless command groups are in excess of hundreds of commands, which is extremely uncommon (the best I got was 75, though I re-made the entire command based framework from scratch so some of the commands were syntaxical (wait, if, else, etc.))

However, the former approach (#15) is the preferred option for modularity sake. Integrating the wait with both executions turn the command, not into a modular command, but rather a defined sequence. If the sequence is regular (e.g. load hopper, wait, shoot) than it would be preferential to use a CommandGroup, for readability sake. If the sequence is irregular (e.g. turn 15 degrees, wait, shoot at low velocity) than the command will only be used once and more commands of similar conditions (e.g. turn 30 degrees, wait, shoot at high velocity) would have to be developed. In this case, it would be easier to make a turn command, a wait command, and a shoot command and plug them in in a similar fashion to post #15.

Last edited by Arhowk : 27-01-2015 at 20:52.
Reply With Quote
  #7   Spotlight this post!  
Unread 28-01-2015, 06:51
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,715
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: Command Based Programming (Threads?)

Quote:
Originally Posted by Ether View Post
OK thanks that's pretty clear. Is there any performance difference between the approach shown above and the code in post#15? Which approach do you prefer, and why (or does it depend on context)?


Honestly, the performance differences would between the two would be so minuscule they probably aren't measurable.

My team and I prefer the method I provided in post #15. It's easier to read, easier to modify, and more modular. Here is an example from our competition code from last season. Notice how easy it is to read exactly what the ShootSeries command does.

Code:
public class ShootSeries extends CommandGroup {
    
    public ShootSeries() {
        addParallel(new PickUpDeploy(PickUp.DEPLOY, 0, PickUp.CLOSE));
        addSequential(new WaitCommand(0.25));
        addSequential(new SetShooterPosition(Shooter.FIRE));
        addSequential(new WaitCommand(0.2));
        addSequential(new SetShooterPosition(Shooter.PRIME));
        addSequential(new WaitCommand(0.1));
        addParallel(new ResetArmCommand());    
    }
}
Code:
public class PickUpDeploy extends CommandBase {
    
    private boolean deploy;
    private double rollerSpeed;
    private boolean openWings;
    
    public PickUpDeploy(boolean deploy, double rollerSpeed) {
        this(deploy, rollerSpeed, PickUp.CLOSE);
    }
    
    public PickUpDeploy(boolean deploy, double rollerSpeed, boolean openWings) {
        requires(pickUp);
        this.deploy = deploy;
        this.rollerSpeed = rollerSpeed;
        this.openWings = openWings;
    }

    protected void initialize() {
        pickUp.deployArm(deploy);
        pickUp.deployCatch(openWings);
    }

    protected void execute() {
         if(oi.isRollerOn()){
            pickUp.setRollerSpeed(rollerSpeed);
        }else{
            pickUp.setRollerSpeed(0);
        }
    }

    protected boolean isFinished() {
        return false;
    }

    protected void end() { }

    protected void interrupted() { }
}
Code:
public class SetShooterPosition extends CommandBase {
    
    public boolean shooterPosition;
    
    public SetShooterPosition(boolean shooterPosition) {
        requires(shooter);
        this.shooterPosition = shooterPosition;
    }

    protected void initialize() { }
    
    protected void execute() {
        shooter.primeShooter(shooterPosition);
    }

    protected boolean isFinished() {
        return true;
    }

    protected void end() { }

    protected void interrupted() { }
}
Code:
public class ResetArmCommand extends CommandBase {
    
    public ResetArmCommand() {
        requires(pickUp);
    }
    
    protected void initialize() { }

    protected void execute() { }

    protected boolean isFinished() {
        return true;
    }

    protected void end() { }

    protected void interrupted() { }
}
Code:
public class PickUp extends Subsystem {
    
    public static final boolean DEPLOY = true;
    public static final boolean RETRACT = false;
    public static final boolean OPEN = true;
    public static final boolean CLOSE = false;
    
    private Solenoid pickUpSolenoid1 = new Solenoid(PropertyReader.getProperty("PICKUP_SOLENOID_MODULE", 2), PropertyReader.getProperty("PICKUP_SOLENOID_CHANNEL_A", RobotMap.pickUpSolenoid1));
    private Solenoid pickUpSolenoid2 = new Solenoid(PropertyReader.getProperty("PICKUP_SOLENOID_MODULE", 2), PropertyReader.getProperty("PICKUP_SOLENOID_CHANNEL_B", RobotMap.pickUpSolenoid2));
    
    private Solenoid wingSolenoid = new Solenoid(PropertyReader.getProperty("WING_SOLENOID_MODULE", 2), PropertyReader.getProperty("WING_SOLENOID_CHANNEL", RobotMap.wingSolenoidChannel));
    
    private SpeedController upperPickUp = new Talon(PropertyReader.getProperty("PICKUP_TALON_CHANNEL", RobotMap.upperPickUpRoller));
    
    private double powerLevel;
    
    private RollingAverager pickUpAverager = new RollingAverager(10, 0);
    
    public void initDefaultCommand() {
        setDefaultCommand(new PickUpDeploy(RETRACT, 0, CLOSE));
    }
    
    public void setRollerSpeed(double power) {
        powerLevel = power;
        pickUpAverager.addValue(powerLevel);
        upperPickUp.set(pickUpAverager.getAverage());
    }
    
    public void deployArm(boolean deploy) {
       pickUpSolenoid1.set(!deploy);
       pickUpSolenoid2.set(deploy);
    }
    public void deployCatch(boolean deploy) {
        wingSolenoid.set(deploy);
    }
}
Code:
public class Shooter extends Subsystem {
    public String currentHot;
    
    public Solenoid blockerPole1 = new Solenoid(PropertyReader.getProperty("BLOCKER_SOLENOID_MODULE", 1), PropertyReader.getProperty("BLOCKER_POLE_CHANNEL_A", RobotMap.blockerPolePort1));
    public Solenoid blockerPole2 = new Solenoid(PropertyReader.getProperty("BLOCKER_SOLENOID_MODULE", 1), PropertyReader.getProperty("BLOCKER_POLE_CHANNEL_B", RobotMap.blockerPolePort2));
    
    public static final boolean FIRE = true;
    public static final boolean PRIME = false;
    
    private Solenoid shooterSolenoid1 = new Solenoid(PropertyReader.getProperty("SHOOTER_SOLENOID_MODULE", 1), PropertyReader.getProperty("SHOOTER_SOLENOID_CHANNEL_A", RobotMap.shooterSolenoid1Port));
    private Solenoid shooterSolenoid2 = new Solenoid(PropertyReader.getProperty("SHOOTER_SOLENOID_MODULE", 1), PropertyReader.getProperty("SHOOTER_SOLENOID_CHANNEL_B", RobotMap.shooterSolenoid2Port));

    public void initDefaultCommand() {
        setDefaultCommand(new SetShooterPosition(Shooter.PRIME));
    }
    
    public void primeShooter(boolean prime) {
        //System.out.println("IN PRIME SHOOTER " + prime);
        shooterSolenoid1.set(prime);
        shooterSolenoid2.set(prime);
    }
}
If interested, here is the entirety of our code from last season, with some offseason additions.

Also, here is our code from this season so far.

Last edited by notmattlythgoe : 28-01-2015 at 07:08.
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 11:10.

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