Arcade Drive

Now that we have the robot driving, let’s see if we can make it do something more useful.  Our goal in this next section is to write code that will allow us to use a joystick to drive the robot.  In Eclipse, load the DriveSubsystem.java file.

Please note that the Driver Station will only check for the presence of a joystick when it is started, so you need to plug in the joystick before launching the Driver Station. Also, if you do not have a joystick available, you can check the Enable Virtual Joystick checkbox and you will be presented with a ‘joystick’ that you can control by clicking and dragging with the mouse:

robotjavavirtualjoystick

The first thing we need to do is give the drive subsystem access to the joystick.  We declare a variable to handle the joystick by adding the following line to the variable declarations (right after the declaration of m_leftMotor).

As before, we find that doing so introduces an error.  Clicking on the error icon, we find that the solution is to import the Joystick class which we do.

Now we need to initialize the joystick by adding the following line to the Init() function

Note that the Joystick class takes a single integer parameter which specifies which joystick.  This version of the pi software currently supports only one joystick, but for compatibility with the FRC classes, we must pass a 0 to the constructor.

Finally, we need to add the following function which will allow us set the power on the drive motors based on the position of the joystick:

This function first reads the current joystick x and y positions.  Note that the getX and getY joystick functions return a value between -1 to +1.

The next two lines essentially square the return and y positions (maintain the sign).  Doing this will give us better control over the speed at the low end.

Finally we set the power based on the values of x and y.  The value of y controls the forward and backward motion of the robot so we want to set both the left and right motors based on that value.  To turn, we need to set the left and right motors in opposite directions, and the turn power needs to be controlled by the x value from the joystick.

At this point, our DriveSubsystem.java file should look like:

Now we need to call this new ArcadeDrive function during the Teleop mode of our main Robot class.  Load the Robot.java file and add the following line to the teleopPeriodic() function:

Also, since we no longer need it, remove the SetPower call we previously added to the teleopInit() function.

Your Robot.java file should now look like:

If we now run our program, we should be able to control the robot with the joystick.

We are now close to the solution that we need, but we are not quite there yet.  We really don’t want to call functions from the DriveSubsystem directly from our main program.  The reason is that there may be other actions we want the robot to take which require the use of the drive.  If we are always setting the motor power based on the joystick values, then this would interfere with those other operations.  To make it work, we would need to disable the joystick operation while those other operations were executing.

Fortunately, the built in Command structure allows this to happen automatically if we use it correctly.

The first thing we need to do is to create an ArcadeDriveCommand which will handle the arcade drive mode.

Right click on the commands folder in the Package Explorer and create a new class called ArcadeDriveCommand:

RobotMinibotArcadeDriveCommand

Once again, we could create this class from scratch, but it will be easier to copy the sample ExampleCommand and use that as a starting point.  Open the ExampleCommand.java file and copy all of the text and past it into the ArcadeDriveCommand.java file, replacing the existing text. Then as before do a search and replace changing all instances of ExampleCommand to ArcadeDriveCommand.  The resulting file should look like:

Now let’s make the changed needed to make this work.

In the constructor ArcadeDriveCommand(), we have an opportunity to specify which of the robot’s subsystems, if any, that this command requires.  For this command, we will require the DriveSubsystem.  Therefore change the requires call to:

Note that specifying all of the required subsystems is very important.  This is how the system will tell who needs what subsystems.  When one command is running that uses a particular subsystem, and a second command is started which also requires that subsystem, then the first command will be interrupted while the second command runs.  This ensures that two commands don’t ‘fight’ over the same subsystem.

We don’t need to do anything special for the initialize() function, so we will leave that alone.

The execute() function is where the action happens.  While this command is running, this function will be called repeatedly.  This is where we want to add our code to control the drive motors from the joystick.  We do this by adding the following line to the execute() function.

Next we need to modify the isFinished() function.  This function should return true when the command has finished whatever it is doing, and false if it still has more stuff to do.  In this case, the command will never end (unless it is interrupted), so we always want to return false.

Finally, the interrupted() function will be called if this command is interrupted (i.e. another command which requires the drive subsystem is about to run).  When this happens, we probably want to turn the motors off so that they aren’t simply left at their last values set by the joystick.  Add the following line to the interrupted() function:

At this point, your ArcadeDriveCommand.java file should look like:

Next, we want to set our DriveSubsystem to run this command whenever it has nothing else to do.  Open the DriveSubsystem.java file, and add the following line to the initDefaultCommand() function.  Note that you will also need to add the import for the ArcadeDriveCommand class.

Your DriveSubsystem.java file should now look like:

Finally we need to remove the direct call to m_driveSubsystem.ArcadeDrive() that we added to the teleopPeriodic() command of our Robot class in the last chapter.

The Robot.java file should now look like:

Now run deploy and run your program and you should find that you can still control it with the joystick.  Only now, the DriveSubsystem can be used by other commands without interference from the joystick.

Next: Spinner Subsystem

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.