Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   Java (http://www.chiefdelphi.com/forums/forumdisplay.php?f=184)
-   -   Command Based Programming (Threads?) (http://www.chiefdelphi.com/forums/showthread.php?t=133549)

Ether 27-01-2015 10:04

Re: Command Based Programming (Threads?)
 

Thanks for the response. I hope you don't mind some follow-up questions:

Quote:

Originally Posted by notmattlythgoe (Post 1434596)
I would suggest using a WaitCommand in a CommandGroup. Below is an example that will fire, then wait 1 second before firing again and then finishing the CommandGroup.

Code:

public FiringCommandGroup extends CommandGroup {

    public FiringCommandGroup() {
          addSequential(new FireCommand());
          addSeqiential(new waitCommand(1));
          addSequential(new FireCommand());
    }
}


What does the waitCommand() do, "under the hood". i.e., how is it implemented?

Quote:

Another way to do it would be to check the time passed in the execute method of a command to see if the period of time you want to wait has passed.
Is there a "recommended" or "best practice" way to implement that approach?



notmattlythgoe 27-01-2015 10:09

Re: Command Based Programming (Threads?)
 
Quote:

Originally Posted by Ether (Post 1434601)

Thanks for the response. I hope you don't mind some follow-up questions:



What does the waitCommand() do, "under the hood". i.e., how is it implemented?



Is there a "recommended" or "best practice" way to implement that approach?



I haven't looked at the wait command under the hood, but this is how I believe it works. I can look under the hood tonight at the source code and see if I'm correct.

Code:

public class WaitCommand extends Command {

    private double waitTime;

    public WaitCommand(double waitTime) {
          this.waitTime = waitTime;
    }

    public boolean isFinished() {
          return timeSinceInitialized >= waitTme;
    }
}


As to the second question. Something like this would work for a single command doing multiple things based on time.

Code:

public void execute() {
    if (timeSinceInitialized() < sometime) {
          do something;
    } else if(timeSinceInitialized() < some bigger time) {
          do something 2;
    } else {
          do something 3;
    }
}


Ether 27-01-2015 13:04

Re: Command Based Programming (Threads?)
 
Quote:

Originally Posted by notmattlythgoe (Post 1434604)
I haven't looked at the wait command under the hood, but this is how I believe it works. I can look under the hood tonight at the source code and see if I'm correct.

Code:

public class WaitCommand extends Command {

    private double waitTime;

    public WaitCommand(double waitTime) {
          this.waitTime = waitTime;
    }

    public boolean isFinished() {
          return timeSinceInitialized >= waitTme;
    }
}


I don't read much Java code. Is this the correct interpretation of what that code is supposed to do?
- the first time it is executed it notes the system time so that it can compute the value of "timeSinceInitialized" each time it is executed.

- it gets executed every 20ms as long as it returns "isFinished()" as false

- it returns "isFinished()" as false as long as "timeSinceInitialized" is less than the desired "waitTime" that was passed as a parameter

- as long as it is returning "isFinished()" as false, any commands that are waiting for it to finish will not execute.

- when "timeSinceInitialized" is greater than or equal to the desired "waitTime", it returns "isFinished()" as true, which results in it being removed from the list of commands to be executed every 20ms. Any commands that were waiting for it to finish will now execute.


notmattlythgoe 27-01-2015 13:13

Re: Command Based Programming (Threads?)
 
Quote:

Originally Posted by Ether (Post 1434716)
I don't read much Java code. Is this the correct interpretation of what that code is supposed to do?
- the first time it is executed it notes the system time so that it can compute the value of "timeSinceInitialized" each time it is executed.

- it gets executed every 20ms as long as it returns "isFinished()" as false

- it returns "isFinished()" as false as long as "timeSinceInitialized" is less than the desired "waitTime" that was passed as a parameter

- as long as it is returning "isFinished()" as false, any commands that are waiting for it to finish will not execute.

- when "timeSinceInitialized" is greater than or equal to the desired "waitTime", it returns "isFinished()" as true, which results in it being removed from the list of commands to be executed every 20ms. Any commands that were waiting for it to finish will now execute.


Yes, that would be the correct process from my interpretation.

Ether 27-01-2015 13:19

Re: Command Based Programming (Threads?)
 
Quote:

Originally Posted by notmattlythgoe (Post 1434604)
As to the second question. Something like this would work for a single command doing multiple things based on time.

Code:

public void execute() {
    if (timeSinceInitialized() < sometime) {
          do something;
    } else if(timeSinceInitialized() < some bigger time) {
          do something 2;
    } else {
          do something 3;
    }
}


Again, is this the correct interpretation of the above code?
- "something" will be executed every 20ms as long as "timeSinceInitialized()" is less than "sometime"

- "something 2" will be executed every 20ms as long as "timeSinceInitialized()" is greater than or equal to "sometime" and less than "sometime bigger time"

- "something 3" will be executed every 20ms as long as "timeSinceInitialized()" is greater than or equal to "some bigger time"

notmattlythgoe 27-01-2015 13:22

Re: Command Based Programming (Threads?)
 
Quote:

Originally Posted by Ether (Post 1434726)
Again, is this the correct interpretation of the above code?
- "something" will be executed every 20ms as long as "timeSinceInitialized()" is less than "sometime"

- "something 2" will be executed every 20ms as long as "timeSinceInitialized()" is greater than or equal to "sometime" and less than "sometime bigger time"

- "something 3" will be executed every 20ms as long as "timeSinceInitialized()" is greater than or equal to "some bigger time"

Perfect.

Ether 27-01-2015 13:44

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?



notmattlythgoe 27-01-2015 13:47

Re: Command Based Programming (Threads?)
 
Quote:

Originally Posted by Ether (Post 1434742)

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.

Ether 27-01-2015 14:18

Re: Command Based Programming (Threads?)
 
Quote:

Originally Posted by notmattlythgoe (Post 1434743)
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.



notmattlythgoe 27-01-2015 14:52

Re: Command Based Programming (Threads?)
 
Quote:

Originally Posted by Ether (Post 1434765)
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.

Ether 27-01-2015 17:49

Re: Command Based Programming (Threads?)
 
Quote:

Originally Posted by notmattlythgoe (Post 1434782)
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)?



Arhowk 27-01-2015 20:49

Re: Command Based Programming (Threads?)
 
Quote:

Originally Posted by Ether (Post 1434863)
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.

notmattlythgoe 28-01-2015 06:51

Re: Command Based Programming (Threads?)
 
Quote:

Originally Posted by Ether (Post 1434863)
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.


All times are GMT -5. The time now is 11:10.

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