Updated Creating and Running an Op Mode (Android Studio) (markdown)

FTC Engineering
2019-07-10 13:03:34 -04:00
parent 1b6519d424
commit 87ff50bac2

@ -1,7 +1,7 @@
### TeamCode Module ### TeamCode Module
If you successfully imported the FTC Android Studio project folder, you will see on the project browser an Android module named "TeamCode". The FTC Android Studio project folder will be used to build a version of the FTC Robot Controller app that includes the custom op modes that you will write to control your competition robot. If you successfully imported the FTC Android Studio project folder, you will see on the project browser an Android module named "TeamCode". The FTC Android Studio project folder will be used to build a version of the FTC Robot Controller app that includes the custom op modes that you will write to control your competition robot.
<p align="center"><img src="https://github.com/FIRST-Tech-Challenge/WikiSupport/blob/master/ftc_app/images/AndroidStudio/teamcodeFolder.jpg" width="300"><p> <p align="center">[[/images/Creating-and-Running-an-Op-Mode-(Android-Studio)/teamcodeFolder.jpg]]<p>
When you create your classes and op modes, you will to create them in the org.firstinspires.ftc.teamcode package that resides in the TeamCode module. This package is reserved for your use within the FTC Android Studio project folder. When you create your classes and op modes, you will to create them in the org.firstinspires.ftc.teamcode package that resides in the TeamCode module. This package is reserved for your use within the FTC Android Studio project folder.
@ -9,19 +9,19 @@ When you create your classes and op modes, you will to create them in the org.fi
The Javadoc reference documentation for the FTC SDK is available online. Visit the following URL to view the FTC SDK documentation: The Javadoc reference documentation for the FTC SDK is available online. Visit the following URL to view the FTC SDK documentation:
http://ftctechnh.github.io/ftc_app/doc/javadoc/index.html &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;http://ftctechnh.github.io/ftc_app/doc/javadoc/index.html
### Enabling Auto Import ### Enabling Auto Import
The auto import feature of Android Studio is a convenient function that helps save time as you write your op mode. If you would like to enable this feature, select the Editor->General->Auto Import item from the Android Studio Settings screen. This will display the editor's auto import settings. The auto import feature of Android Studio is a convenient function that helps save time as you write your op mode. If you would like to enable this feature, select the Editor->General->Auto Import item from the Android Studio Settings screen. This will display the editor's auto import settings.
Check the "Add unambiguous imports on the fly" so that Android Studio will automatically add the required import statements for classes that you would like to use in your op mode. Check the "Add unambiguous imports on the fly" so that Android Studio will automatically add the required import statements for classes that you would like to use in your op mode.
<p align="center"><img src="https://github.com/FIRST-Tech-Challenge/WikiSupport/blob/master/ftc_app/images/AndroidStudio/AutoImport.jpg" width="500"><p> <p align="center">[[/images/Creating-and-Running-an-Op-Mode-(Android-Studio)/AutoImport.jpg]]<p>
### Sample Op Modes ### Sample Op Modes
A great way to learn how to program a robot is to examine the sample op modes that are included with the FTC Android Studio project folder. You can locate these files in the FtcRobotController module in the package "org.firstinspires.ftc.robotcontroller.external.samples". A great way to learn how to program a robot is to examine the sample op modes that are included with the FTC Android Studio project folder. You can locate these files in the FtcRobotController module in the package "org.firstinspires.ftc.robotcontroller.external.samples".
<p align="center"><img src="https://github.com/FIRST-Tech-Challenge/WikiSupport/blob/master/ftc_app/images/AndroidStudio/externalSamples.jpg" width="300"><p> <p align="center">[[/images/Creating-and-Running-an-Op-Mode-(Android-Studio)/externalSamples.jpg]]<p>
If you would like to use a sample op mode, copy it from the "org.firstinspires.ftc.robotcontroller.external.samples" package and move it to the "org.firstinspires.ftc.teamcode". If you would like to use a sample op mode, copy it from the "org.firstinspires.ftc.robotcontroller.external.samples" package and move it to the "org.firstinspires.ftc.teamcode".
@ -38,11 +38,11 @@ and comment out this line to enable the op mode and allow it to be run on the Ro
### Creating Your FIRST Op Mode ### Creating Your FIRST Op Mode
Right mouse click on the "org.firstinspires.ftc.teamcode" package and select New->Java Class from the pop-up menu. The Create New Class dialog box appear. Specify the name of the new class as "MyFIRSTJavaOpMode" and specify as its superclass the class LinearOpMode which is in the package "com.qualcomm.robotcore.eventloop.opmode". Right mouse click on the "org.firstinspires.ftc.teamcode" package and select New->Java Class from the pop-up menu. The Create New Class dialog box appear. Specify the name of the new class as "MyFIRSTJavaOpMode" and specify as its superclass the class LinearOpMode which is in the package "com.qualcomm.robotcore.eventloop.opmode".
<p align="center"><img src="https://github.com/FIRST-Tech-Challenge/WikiSupport/blob/master/ftc_app/images/AndroidStudio/CreateLinearOpMode.jpg" width="500"><p> <p align="center">[[/images/Creating-and-Running-an-Op-Mode-(Android-Studio)/CreateLinearOpMode.jpg]]<p>
Press the OK button to create the new class. The source code for the new class should appear in the editing pane of the Android Studio user interface. Press the OK button to create the new class. The source code for the new class should appear in the editing pane of the Android Studio user interface.
<p align="center"><img src="https://github.com/FIRST-Tech-Challenge/WikiSupport/blob/master/ftc_app/images/AndroidStudio/NewOpMode.jpg" width="700"><p> <p align="center">[[/images/Creating-and-Running-an-Op-Mode-(Android-Studio)/NewOpMode.jpg]]<p>
Modify the main portion of your op mode so that it looks like the following code (note that the package definition and some import statements have been omitted in the following source code): Modify the main portion of your op mode so that it looks like the following code (note that the package definition and some import statements have been omitted in the following source code):
@ -87,12 +87,45 @@ Congratulations! You've written an op mode. It does not do much, but we will m
### Examining the Structure of Your Op Mode ### Examining the Structure of Your Op Mode
It can be helpful to think of an op mode as a list of tasks for the Robot Controller to perform. For a linear op mode, the Robot Controller will process this list of tasks sequentially. Users can also use control loops (such as a while loop) to have the Robot Controller repeat (or iterate) certain tasks within a linear op mode. It can be helpful to think of an op mode as a list of tasks for the Robot Controller to perform. For a linear op mode, the Robot Controller will process this list of tasks sequentially. Users can also use control loops (such as a while loop) to have the Robot Controller repeat (or iterate) certain tasks within a linear op mode.
<p align="center"><img src="https://github.com/FIRST-Tech-Challenge/WikiSupport/blob/master/ftc_app/images/OnBotJava/ExaminingStructurePic2.jpg" width="300"></p> <p align="center">[[/images/Creating-and-Running-an-Op-Mode-(OnBot-Java)/ExaminingStructurePic2.jpg]]</p>
If you think about an op mode as a list of instructions for the robot, this set of instructions that you created will be executed by the robot whenever a team member selects the op mode called “MyFIRSTJavaOpMode” from the list of available op modes for this Robot Controller. If you think about an op mode as a list of instructions for the robot, this set of instructions that you created will be executed by the robot whenever a team member selects the op mode called “MyFIRSTJavaOpMode” from the list of available op modes for this Robot Controller.
Lets look at the structure of your newly created op mode Lets look at the structure of your newly created op mode. Heres a copy of the op mode text (minus some comments, the package definition, and some import package statements):
```
@TeleOp
public class MyFIRSTJavaOpMode extends LinearOpMode {
private Gyroscope imu;
private DcMotor motorTest;
private DigitalChannel digitalTouch;
private DistanceSensor sensorColorRange;
private Servo servoTest;
@Override
public void runOpMode() {
imu = hardwareMap.get(Gyroscope.class, "imu");
motorTest = hardwareMap.get(DcMotor.class, "motorTest");
digitalTouch = hardwareMap.get(DigitalChannel.class, "digitalTouch");
sensorColorRange = hardwareMap.get(DistanceSensor.class, "sensorColorRange");
servoTest = hardwareMap.get(Servo.class, "servoTest");
telemetry.addData("Status", "Initialized");
telemetry.update();
// Wait for the game to start (driver presses PLAY)
waitForStart();
// run until the end of the match (driver presses STOP)
while (opModeIsActive()) {
telemetry.addData("Status", "Running");
telemetry.update();
}
}
}
```
At the start of the op mode there is an annotation that occurs before the class definition. This annotation states that this is a tele-operated (i.e., driver controlled) op mode: At the start of the op mode there is an annotation that occurs before the class definition. This annotation states that this is a tele-operated (i.e., driver controlled) op mode:
``` ```
@ -107,7 +140,7 @@ You can see from the sample code that an op mode is defined as a Java class. In
public class MyFIRSTJavaOpMode extends LinearOpMode { public class MyFIRSTJavaOpMode extends LinearOpMode {
``` ```
You can also see that the sample op mode includes five private member variables for this op mode. These variables will hold references to the five devices that you configured earlier for the configuration file of your Robot Controller. You can also see that the OnBot Java editor created five private member variables for this op mode. These variables will hold references to the five configured devices that the OnBot Java editor detected in the configuration file of your Robot Controller.
``` ```
private Gyroscope imu; private Gyroscope imu;
@ -163,44 +196,55 @@ After a start command has been received, the op mode enters a while loop and kee
As the op mode iterates in the while loop, it will continue to send telemetry messages with the index of “Status” and the message of “Running” to be displayed on the Driver Station. As the op mode iterates in the while loop, it will continue to send telemetry messages with the index of “Status” and the message of “Running” to be displayed on the Driver Station.
### Building and Installing Your Op Mode ### Building and Installing Your Op Mode
Verify that the Robot Controller phone is connected to your laptop and that the laptop has USB debugging permission for the phone. Look towards the top of the Android Studio user interface and find the little green Play or Run button (which is represented by a green triangle) next to the words “Team Code”. Press this green button to build the Robot Controller app and to install it onto your phone. Verify that the Robot Controller phone is connected to your laptop and that the laptop has USB debugging permission for the phone.
<p align="center"><img src="https://github.com/FIRST-Tech-Challenge/WikiSupport/blob/master/ftc_app/images/AndroidStudio/RunTeamCode.jpg" width="300"></p> <p align="center">[[/images/Creating-and-Running-an-Op-Mode-(Android-Studio)/phoneUSBConnected.jpg]]</p>
Or, if you are using a Control Hub, verify that the Control Hub is connected to your laptop through its USB Type C port. Note that the Control Hub should automatically have USB debugging permission enabled.
<p align="center">[[/images/Creating-and-Running-an-Op-Mode-(Android-Studio)/controlHubUSBConnected.jpg]]</p>
When using the Control Hub, please make sure you use the Type C port (and not the USB Mini port) to connect the Control Hub to your development laptop.
<p align="center">[[/images/Creating-and-Running-an-Op-Mode-(Android-Studio)/typeC.jpg]]</p>
Look towards the top of the Android Studio user interface and find the little green Play or Run button (which is represented by a green triangle) next to the words “Team Code”. Press this green button to build the Robot Controller app and to install it onto your phone.
<p align="center">[[/images/Creating-and-Running-an-Op-Mode-(Android-Studio)/RunTeamCode.jpg]]</p>
Android Studio should prompt you to select a target device to install the Robot Controller app. Your screen might look something like the image shown below. Android Studio should prompt you to select a target device to install the Robot Controller app. Your screen might look something like the image shown below.
<p align="center"><img src="https://github.com/FIRST-Tech-Challenge/WikiSupport/blob/master/ftc_app/images/AndroidStudio/SelectDeploymentTarget.jpg" width="300"></p> <p align="center">[[/images/Creating-and-Running-an-Op-Mode-(Android-Studio)/SelectDeploymentTarget.jpg]]</p>
Make sure that you select the correct target device. In the figure above the Motorola phone is selected as the target device. Hit OK to build the APK file and install it on the target device. Make sure that you select the correct target device. In the figure above the Motorola phone is selected as the target device. Hit OK to build the APK file and install it on the target device.
Note that if you previously installed a copy of the FTC Robot Controller app from the Google Play store, the installation of your newly built app will fail the first time you attempt to install it. This is because Android Studio detects that the app that you just build has a different digital signature than the official version of the FTC Robot Controller app that was installed from Google Play. Note that if you previously installed a copy of the FTC Robot Controller app from the Google Play store, the installation of your newly built app will fail the first time you attempt to install it. This is because Android Studio detects that the app that you just build has a different digital signature than the official version of the FTC Robot Controller app that was installed from Google Play.
<p align="center"><img src="https://github.com/FIRST-Tech-Challenge/WikiSupport/blob/master/ftc_app/images/AndroidStudio/ApplicationInstallFailed.jpg" width="400"></p> <p align="center">[[/images/Creating-and-Running-an-Op-Mode-(Android-Studio)/ApplicationInstallFailed.jpg]]</p>
If this happens, Android Studio will prompt you if it's OK to uninstall the previous (official) version of the app from your device and replace it with the updated version of the app. Select "OK" to uninstall the previous version and to replace it with your newly created Robot Controller App (see image above). If this happens, Android Studio will prompt you if it's OK to uninstall the previous (official) version of the app from your device and replace it with the updated version of the app. Select "OK" to uninstall the previous version and to replace it with your newly created Robot Controller App (see image above).
<p align="center"><img src="https://github.com/FIRST-Tech-Challenge/WikiSupport/blob/master/ftc_app/images/AndroidStudio/RCLaunched.jpg" width="175"></p> <p align="center">[[/images/Creating-and-Running-an-Op-Mode-(Android-Studio)/RCLaunched.jpg]]</p>
If the installation was successful, the Robot Controller app should be launched on the target Android device. If the installation was successful, the Robot Controller app should be launched on the target Android device.
### Running Your Op Mode ### Running Your Op Mode
If you successfully built and installed your updated Android app with your new op mode, then you are ready to run the op mode. Verify that the Driver Station is still connected to the Robot Controller. Since you designated that your example op mode is a tele-operated op mode, it will be listed as a “TeleOp” op mode. If you successfully built and installed your updated Android app with your new op mode, then you are ready to run the op mode. Verify that the Driver Station is still connected to the Robot Controller. Since you designated that your example op mode is a tele-operated op mode, it will be listed as a “TeleOp” op mode.
On the Driver Station, use the “TeleOp” dropdown list control to display the list of available op modes. Select your op mode (“MyFIRSTJavaOpMode”) from the list. On the Driver Station, use the “TeleOp” dropdown list control to display the list of available op modes. Select your op mode (“MyFIRSTJavaOpMode”) from the list.
<p align="center"><img src="https://github.com/FIRST-Tech-Challenge/WikiSupport/blob/master/ftc_app/images/OnBotJava/OnBotDoc_SelectMyFIRSTOpMode.jpg" width="175"></p> <p align="center">[[/images/Creating-and-Running-an-Op-Mode-(OnBot-Java)/OnBotDoc_SelectMyFIRSTOpMode.jpg]]</p>
Press the INIT button to initialize the op mode. Press the INIT button to initialize the op mode.
<p align="center"><img src="https://github.com/FIRST-Tech-Challenge/WikiSupport/blob/master/ftc_app/images/OnBotJava/OnBotDoc_MyFIRSTPushInit.jpg" width="175"></p> <p align="center">[[/images/Creating-and-Running-an-Op-Mode-(OnBot-Java)/OnBotDoc_MyFIRSTPushInit.jpg]]</p>
The op mode will execute the statements in the runOpMode method up to the waitForStart statement. It will then wait until you press the start button (which is represented by the triangular shaped symbol) to continue. The op mode will execute the statements in the runOpMode method up to the waitForStart statement. It will then wait until you press the start button (which is represented by the triangular shaped symbol) to continue.
<p align="center"><img src="https://github.com/FIRST-Tech-Challenge/WikiSupport/blob/master/ftc_app/images/OnBotJava/OnBotDoc_PressStart.jpg" width="175"></p> <p align="center">[[/images/Creating-and-Running-an-Op-Mode-(OnBot-Java)/OnBotDoc_PressStart.jpg]]</p>
Once you press the start button, the op mode will continue to iterate and send the “Status: Running” message to the Driver Station. To stop the op mode, press the square-shaped stop button. Once you press the start button, the op mode will continue to iterate and send the “Status: Running” message to the Driver Station. To stop the op mode, press the square-shaped stop button.
<p align="center"><img src="https://github.com/FIRST-Tech-Challenge/WikiSupport/blob/master/ftc_app/images/OnBotJava/OnBotDoc_PressStop.jpg" width="175"></p> <p align="center">[[/images/Creating-and-Running-an-Op-Mode-(OnBot-Java)/OnBotDoc_PressStop.jpg]]</p>
Congratulations! You ran your first java op mode! Congratulations! You ran your first java op mode!
@ -236,7 +280,7 @@ tgtPower = -this.gamepad1.left_stick_y;
The object gamepad1 is available for you to access in the runOpMode method. It represents the state of gamepad #1 on your Driver Station. Note that for the F310 gamepads that are used during the competition, the Y value of a joystick ranges from -1, when a joystick is in its topmost position, to +1, when a joystick is in its bottommost position. In the example code above, you negate the left_stick_y value so that pushing the left joystick forward will result in a positive power being applied to the motor. Note that in this example, the notion of forwards and backwards for the motor is arbitrary. However, the concept of negating the joystick y value can be very useful in practice. The object gamepad1 is available for you to access in the runOpMode method. It represents the state of gamepad #1 on your Driver Station. Note that for the F310 gamepads that are used during the competition, the Y value of a joystick ranges from -1, when a joystick is in its topmost position, to +1, when a joystick is in its bottommost position. In the example code above, you negate the left_stick_y value so that pushing the left joystick forward will result in a positive power being applied to the motor. Note that in this example, the notion of forwards and backwards for the motor is arbitrary. However, the concept of negating the joystick y value can be very useful in practice.
<p align="center"><img src="https://github.com/FIRST-Tech-Challenge/WikiSupport/blob/master/ftc_app/images/OnBotJava/OnBotDoc_left_stick_y.jpg" width="300"></p> <p align="center">[[/images/Creating-and-Running-an-Op-Mode-(OnBot-Java)/OnBotDoc_left_stick_y.jpg]]</p>
The next set of statements sets the power of motorTest to the value represented by the variable tgtPower. The values for target power and actual motor power are then added to the set of data that will be sent via the telemetry mechanism to the Driver Station. The next set of statements sets the power of motorTest to the value represented by the variable tgtPower. The values for target power and actual motor power are then added to the set of data that will be sent via the telemetry mechanism to the Driver Station.
@ -247,22 +291,22 @@ The next set of statements sets the power of motorTest to the value represented
telemetry.addData("Motor Power", motorTest.getPower()); telemetry.addData("Motor Power", motorTest.getPower());
``` ```
After you have modified your op mode to include these new statements, build and install the updated app onto your Robot Controller Android device. After you have modified your op mode to include these new statements, press the build button and verify that the op mode was built successfully.
### Running Your Op Mode with a Gamepad Connected ### Running Your Op Mode with a Gamepad Connected
Your op mode takes input from a gamepad and uses this input to control a DC motor. To run your op mode, you will need to connect a Logitech F310 gamepad to the Driver Station. Your op mode takes input from a gamepad and uses this input to control a DC motor. To run your op mode, you will need to connect a Logitech F310 gamepad to the Driver Station.
Before you connect your gamepad to the phone, verify that the switch on the bottom of the gamepad is set to the “X” position. Before you connect your gamepad to the phone, verify that the switch on the bottom of the gamepad is set to the “X” position.
<p align="center"><img src="https://github.com/FIRST-Tech-Challenge/WikiSupport/blob/master/ftc_app/images/OnBotJava/OnBotDoc_SwitchXMode.jpg" width="300"></p> <p align="center">[[/images/Running-Your-Op-Mode/RunningOpModeStep1.jpg]]</p>
Connect the gamepad to the Driver Station using the Micro USB OTG adapter cable. Connect the gamepad to the Driver Station using the Micro USB OTG adapter cable.
<p align="center"><img src="https://github.com/FIRST-Tech-Challenge/WikiSupport/blob/master/ftc_app/images/OnBotJava/OnBotDoc_USBOTGAdapter.jpg" width="300"></p> <p align="center">[[/images/Running-Your-Op-Mode/RunningOpModeStep2.jpg]]</p>
Your example op mode is looking for input from the gamepad designated as the user or driver #1. Press the Start button and the A button simultaneously on the Logictech F310 controller to designate your gamepad as user #1. Note that pushing the Start button and the B button simultaneously would designate the gamepad as user #2. Your example op mode is looking for input from the gamepad designated as the user or driver #1. Press the Start button and the A button simultaneously on the Logictech F310 controller to designate your gamepad as user #1. Note that pushing the Start button and the B button simultaneously would designate the gamepad as user #2.
<p align="center"><img src="https://github.com/FIRST-Tech-Challenge/WikiSupport/blob/master/ftc_app/images/OnBotJava/OnBotDoc_PressStartAndA.jpg" width="300"></p> <p align="center">[[/images/Running-Your-Op-Mode/RunningOpModeStep3.jpg]]</p>
If you successfully designated the gamepad to be user #1, you should see a little gamepad icon above the text “User 1” in the upper right hand corner of the Driver Station Screen. Whenever there is activity on gamepad #1, the little icon should be highlighted in green. If the icon is missing or if it does not highlight in green when you use your gamepad, then there is a problem with the connection to the gamepad. If you successfully designated the gamepad to be user #1, you should see a little gamepad icon above the text “User 1” in the upper right hand corner of the Driver Station Screen. Whenever there is activity on gamepad #1, the little icon should be highlighted in green. If the icon is missing or if it does not highlight in green when you use your gamepad, then there is a problem with the connection to the gamepad.
@ -270,4 +314,4 @@ Select, initialize and run your “MyFIRSTJavaOpMode” op mode. It is importan
If you configured your gamepad properly, then the left joystick should control the motion of the motor. As you run your op mode, be careful and make sure you do not get anything caught in the turning motor. Note that the User #1 gamepad icon should highlight green each time you move the joystick. Also note that the target power and actual motor power values should be displayed in the telemetry area on the Driver Station. If you configured your gamepad properly, then the left joystick should control the motion of the motor. As you run your op mode, be careful and make sure you do not get anything caught in the turning motor. Note that the User #1 gamepad icon should highlight green each time you move the joystick. Also note that the target power and actual motor power values should be displayed in the telemetry area on the Driver Station.
<p align="center"><img src="https://github.com/FIRST-Tech-Challenge/WikiSupport/blob/master/ftc_app/images/OnBotJava/OnBotDoc_RunOpModeDCMotor.jpg" width="175"></p> <p align="center">[[/images/Creating-and-Running-an-Op-Mode-(OnBot-Java)/OnBotDoc_RunOpModeDCMotor.jpg]]</p>