Chief Delphi

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

Coach Seb 03-02-2017 18:49

Java Command Based Command
 
New to Command Based... We are about to start coding the commands for each subsystem and before we get too far, we need the following info on how to handle multiple commands...

For example, we have a feeder motor... we want a command to FeederOn(clockwise), FeederOnRev(counterclockwise) and FeederOff...

We created a Feeder_Command class...

Code:

public class TeleopFeederOn_Command extends Command{
        @Override
        protected void execute(){
                while(Robot.oi.getOperatorJoystick().getRawButton(4) ==true){
                        Robot.fuelFeederSubsystem.FeederOn();
                }
                Robot.fuelFeederSubsystem.FeederOff();       
        }
        @Override
        protected boolean isFinished() {
                // TODO Auto-generated method stub
                return false;
        }
        @Override
        protected void end() {
                Robot.fuelFeederSubsystem.FeederOff();
        }
        @Override
        protected void interrupted(){
                end();
        }       
}

That should take care of our FeederOn command using button 4. Now should we build a new command class for FeederOnRev (button5) or do we want to add the command to this class?

otherguy 03-02-2017 22:27

Re: Java Command Based Command
 
If you want a button to execute a command you should be doing this in the OI class.
See here for an example: https://wpilib.screenstepslive.com/s...joystick-input

Basically:
  • create a command for each of your actions (or one that takes in a parameter for the "speed" to pass on to the motors).
  • In OI, you link the command to execute to a Joystick action on a button (e.g. WhenPressed, WhileHeld, etc.).
  • the execute method of your command is just going to call in to the subsystem it requires and call the right setter method that ultimately calls someMotorName.set(speed).

To specifically address your code example, get rid of everything except "Robot.fuelFeederSubsystem.FeederOn();" in the execute method. Then add something like the following to the OI class:
Code:

Joystick myJoystick = new Joystick(0);
Button aButton = new JoystickButton(myJoystick, 5);

...

aButton.whileHeld(new TeleopFeederOn_Command());

Check the Javadoc for Button to see understand what the different methods for button presses (whileHeld, whenPressed...) do.

The guides for command based programming on screensteps live are pretty good. You could also use RobotBuilder to generate some example code to see what the "right" way to do it is. And there's also lots of example code out there to pull from.


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

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