Commands & Buttons

In this next chapter we will learn how to connect a command to a button on the joystick so that command will be executed when the button is pressed. We are going to connect our previously created DriveForTimeCommand to button 1 (i.e. the trigger button) on the joystick so that when we press that button, the DriveForTimeCommand will override the normally running ArcadeDriveCommand and make the robot drive forward for a period of time.

Open the file OI.java and you will currently see:

Since this class will need to access the joystick, you can see that it is already creating an instance of the Joystick class. We also need one instance of the Button class for each button that we wish to control and, as you can see, there is already one created (m_trigger) which will monitor button 1 on the joystick, which is the trigger button.

Now to connect the button to a specific command we add a line to the OI() constructor like the one you see commented out:

This tells the command system to execute the DriveForTimeCommand(0.75, 2) whenever the trigger button is pressed which will result in the robot driving forward at three quarters power for two seconds.

You OI.java file should now look like:

Now deploy and run your program. Verify that you can still drive the robot around using the joystick, and then press the trigger button and the robot should drive forward for 2 seconds. Note that while the DriveForTimeCommand is executing, you can no longer control the robot with the joystick. This is because the DriveForTimeCommand has temporarily interrupted the ArcadeDriveCommand because it needs to use the DriveSubsystem which is also required by the ArcadeDriveCommand. Note, however, that once the DriveForTimeCommand has completed, the ArcadeDriveCommand starts running again and we can control the robot with the joystick.

If we take a look at the logging information in our Console window, we can see just how these events unfold:

I have added comments to each log line explaining what that line means.

Next: Wheel encoders

Pierre Pierre, who replica handbag 185cm tall, gucci replica handbags the "Diamond King" in his hermes replica handbags . He has been selected as one of the handbag replica most beautiful 50 people in the "People" magazine. It is also known as replica handbags most elegant and quiet in the world. Unforgettable prince.