|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
| Thread Tools | Rate Thread | Display Modes |
|
#16
|
||||
|
||||
|
Re: Command Based Programming (Threads?)
Thanks for the response. I hope you don't mind some follow-up questions: Quote:
Quote:
|
|
#17
|
|||||
|
|||||
|
Re: Command Based Programming (Threads?)
Quote:
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;
}
}
Last edited by notmattlythgoe : 01-27-2015 at 10:20 AM. |
|
#18
|
||||
|
||||
|
Re: Command Based Programming (Threads?)
Quote:
- 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. |
|
#19
|
|||||
|
|||||
|
Re: Command Based Programming (Threads?)
Quote:
|
|
#20
|
||||
|
||||
|
Re: Command Based Programming (Threads?)
Quote:
- "something" will be executed every 20ms as long as "timeSinceInitialized()" is less than "sometime" |
|
#21
|
|||||
|
|||||
|
Re: Command Based Programming (Threads?)
Quote:
|
|
#22
|
||||
|
||||
|
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());
}
}
Code:
public void execute() {
if (timeSinceInitialized() < sometime) {
do something;
} else if(timeSinceInitialized() < some bigger time) {
do nothing;
} else {
do something2;
}
}
whereas codeB executes "something" repeatedly, then waits, then executes "something2" repeatedly. Yes? |
|
#23
|
|||||
|
|||||
|
Re: Command Based Programming (Threads?)
Quote:
|
|
#24
|
||||
|
||||
|
Re: Command Based Programming (Threads?)
Quote:
|
|
#25
|
|||||
|
|||||
|
Re: Command Based Programming (Threads?)
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;
}
}
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 : 01-27-2015 at 03:19 PM. |
|
#26
|
||||
|
||||
|
Re: Command Based Programming (Threads?)
Quote:
|
|
#27
|
||||
|
||||
|
Re: Command Based Programming (Threads?)
Quote:
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 : 01-27-2015 at 08:52 PM. |
|
#28
|
|||||
|
|||||
|
Re: Command Based Programming (Threads?)
Quote:
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);
}
}
Also, here is our code from this season so far. Last edited by notmattlythgoe : 01-28-2015 at 07:08 AM. |
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|