{"id":3171,"date":"2020-06-30T18:47:04","date_gmt":"2020-07-01T01:47:04","guid":{"rendered":"http:\/\/firstinmichigan.us\/FTC\/?page_id=3171"},"modified":"2022-10-18T06:35:49","modified_gmt":"2022-10-18T13:35:49","slug":"ftc-java-basics","status":"publish","type":"page","link":"https:\/\/firstinmichigan.us\/FTC\/ftc-java-basics\/","title":{"rendered":"FTC Java Basics"},"content":{"rendered":"\n<script src=\"https:\/\/cdn.jsdelivr.net\/gh\/google\/code-prettify@master\/loader\/run_prettify.js\"><\/script>\n\n\n\n<h2 class=\"wp-block-heading\">Prerequisites<\/h2>\n\n\n\n<p>If you are using <strong>OnBot Java<\/strong> as your editor, this guide will start with the program that we generated in the <a href=\"\/FTC\/onbot-java-editor-basics\" target=\"_blank\" rel=\"noopener noreferrer\">last guide<\/a>.<\/p>\n\n\n\n<p>If you are using <strong>Android Studio<\/strong> as your editor, you can simply create a file called &#8220;BasicDrive.java&#8221; in your teamcode folder and copy paste the starting code in this guide.<\/p>\n\n\n\n<p>If you are new to the Java programming language, it is also recommended that you read the <a href=\"http:\/\/java-syntax-basics\/\">Java Syntax Basics<\/a> guide. Even if you don&#8217;t understand everything in that guide (it is fairly technical), it will provide good background knowledge for this guide.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The Linear OpMode Structure<\/h2>\n\n\n\n<p>In this guide, <code>...<\/code> will be used to indicate a section of code that was removed from the example. Note that the <code>...<\/code> is not actually part of the code.  <\/p>\n\n\n\n<p>Here is the code generated by the BlankLinearOpMode sample:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted prettyprint\">\/*\nCopyright 2020 FIRST Tech Challenge Team XYZW\n...\n*\/\npackage org.firstinspires.ftc.teamcode;\n\nimport com.qualcomm.robotcore.eventloop.opmode.LinearOpMode;\nimport com.qualcomm.robotcore.eventloop.opmode.TeleOp;\nimport com.qualcomm.robotcore.eventloop.opmode.Disabled;\nimport com.qualcomm.robotcore.hardware.DcMotor;\nimport com.qualcomm.robotcore.hardware.DcMotorSimple;\nimport com.qualcomm.robotcore.util.ElapsedTime;\n\n\/**\n * This file contains an minimal example of a Linear \"OpMode\". \n...\n *\/\n@TeleOp\npublic class BasicDrive extends LinearOpMode {\n\n\n    @Override\n    public void runOpMode() {\n\n        telemetry.addData(\"Status\", \"Initialized\");\n        telemetry.update();\n        \/\/ Wait for the game to start (driver presses PLAY)\n        waitForStart();\n\n        \/\/ run until the end of the match (driver presses STOP)\n        while (opModeIsActive()) {\n            telemetry.addData(\"Status\", \"Running\");\n            telemetry.update();\n\n        }\n    }\n}\n<\/pre>\n\n\n\n<p>Let&#8217;s break it down into sections to learn how it works.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">The Top of the File<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted prettyprint\">\/*\nCopyright 2020 FIRST Tech Challenge Team XYZW\n...\n*\/\npackage org.firstinspires.ftc.teamcode;\n\nimport com.qualcomm.robotcore.eventloop.opmode.LinearOpMode;\nimport com.qualcomm.robotcore.eventloop.opmode.TeleOp;\nimport com.qualcomm.robotcore.eventloop.opmode.Disabled;\nimport com.qualcomm.robotcore.hardware.DcMotor;\nimport com.qualcomm.robotcore.hardware.DcMotorSimple;\nimport com.qualcomm.robotcore.util.ElapsedTime;\n\n\/**\n * This file contains an minimal example of a Linear \"OpMode\". \n...\n *\/\n<\/pre>\n\n\n\n<p>At the top, we have some automatically generated comments (enclosed in <code>\/*<\/code> and <code>*\/<\/code>). There is also a <code>package<\/code> statement and some <code>import<\/code> statements. These statements will be automatically generated when you create the program and as you write code, therefore understanding them is not critical. Just know that the <code>package<\/code> statement defines the file&#8217;s location, and the <code>import<\/code> statements &#8220;import&#8221; FTC specific features (such as motors) into our program for us to use.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">The Class<\/h3>\n\n\n\n<p>Next we have the class definition:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted prettyprint\">...\n\n@TeleOp\npublic class BasicDrive extends LinearOpMode {\n    ...\n}\n<\/pre>\n\n\n\n<p>The <code>public class BasicDrive<\/code> indicates that we are defining a class (program) named &#8220;BasicDrive&#8221;. The <code>extends LinearOpMode<\/code> portion indicates that this class will inherit variables (values) and methods (behaviors) from a <strong>parent class<\/strong> called &#8220;LinearOpMode&#8221;, which is a built-in FTC class. In other words, <strong>this class will act like an OpMode<\/strong> rather than a regular Java program.<\/p>\n\n\n\n<p>The <code>@TeleOp<\/code> line before the class definition is an <strong>annotation<\/strong> of the class. This annotation tell&#8217;s the Driver Station phone that this is a <strong>TeleOp OpMode<\/strong>, and therefore should be listed in that section of the app. You can also use <code>@Autonomous<\/code> and <code>@Disabled<\/code> annotations.<\/p>\n\n\n\n<p>After the class is defined, the brackets (<code>{}<\/code>) will contain all of our code for the program.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">The <code>runOpMode<\/code> Method<\/h3>\n\n\n\n<p>Inside of the class&#8217; brackets, we have a method called <code>runOpMode<\/code>. The definition of the method looks like this:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted prettyprint\">...\n    @Override\n    public void runOpMode() {\n        ...\n    }\n...\n<\/pre>\n\n\n\n<p>The method is <code>public<\/code> (usable anywhere), returns <code>void<\/code> (does not output a value), is named <code>runOpMode<\/code>, and has no parameters (the parenthesis <code>()<\/code> are empty).<\/p>\n\n\n\n<p>This method is NOT unique to our class; rather, it is a method inherited from the parent class (<code>LinearOpMode<\/code>). Thus, it is important that the name of the method is <em>exactly<\/em> <code>runOpMode<\/code>. There is also an <code>@Override<\/code> annotation before the method, which indicates that this method already exists in the parent class, and we are simply <strong>overriding<\/strong> that method with our own code.<\/p>\n\n\n\n<p>The robot will start running the code within this method&#8217;s brackets when the <strong>INIT<\/strong> button is pressed on the Driver Station phone.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Sectioning the <code>runOpMode<\/code> Method for TeleOp<\/h3>\n\n\n\n<p>The default code inside of the <code>runOpMode<\/code> section is set up so that the method is divided into various TeleOp sections:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>init<\/strong> &#8211; code in the <strong>init <\/strong>section is ran <em>once<\/em> as soon as the INIT button is pressed. The init section is good for writing one-time setup code, such as mapping hardware devices (more on that later in this guide), setting the directions of motors, and resetting sensors.<\/li><li><strong>loop<\/strong> &#8211; code in the <strong>loop <\/strong>section will run continuously until the OpMode is no longer active (the STOP button is pressed). The loop will contain all of our control code that needs to constantly run, such as updating drive motor speeds based on joysticks or updating a claw&#8217;s position based on button presses.<\/li><\/ul>\n\n\n\n<p>I added comments (highlighted in yellow) to indicate where code for the init and loop sections would go:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted prettyprint\">...\n    @Override\n    public void runOpMode() {\n        <span class=\"highlight\">\/\/ Init section<\/span>\n        telemetry.addData(\"Status\", \"Initialized\");\n        telemetry.update();\n        \/\/ Wait for the game to start (driver presses PLAY)\n        waitForStart();\n\n        \/\/ run until the end of the match (driver presses STOP)\n        while (opModeIsActive()) {\n            <span class=\"highlight\">\/\/ Loop section<\/span>\n            telemetry.addData(\"Status\", \"Running\");\n            telemetry.update();\n        }\n    }\n...\n<\/pre>\n\n\n\n<p>In this code, we can see that <strong>init code<\/strong> would go at the very beginning of the method. <\/p>\n\n\n\n<p>After the init code is two <code>telemetry<\/code> statements. Telemetry statements prepare (<code>addData<\/code>) and display (<code>update<\/code>) information on the Driver Station phone. These are not necessary but they are part of the BlankLinearOpMode template. The two statements after the init section simply let the driver know that the robot has finished initializing.<\/p>\n\n\n\n<p>Next is a line with <code>waitForStart();<\/code>. As the name implies, the program will pause at this line untill the START button is pressed on the Driver Station.<\/p>\n\n\n\n<p>d <code>while (opModeIsActive()) { }<\/code>. This is a <strong>while loop<\/strong> that will continuously run the code in the brackets while the condition in the parenthesis is <code>true<\/code>. In this case, the condition is the result of invoking the method <code>opModeIsActive()<\/code>, a method which returns <code>true<\/code> if the OpMode is running and <code>false<\/code> if it isn&#8217;t. Thus, the code in the brackets will keep running until the OpMode is stopped, making that the code in the brackets is the <strong>loop code<\/strong>.<\/p>\n\n\n\n<p>Once again, there are also two <code>telemetry<\/code> statements in the loop code simply indicating to the driver that the robot is now running.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Adding Our Own Code<\/h2>\n\n\n\n<p>Now let&#8217;s try to add some of our own code to this program to run a DC motor. Let&#8217;s say that we named this motor &#8220;left&#8221; in our <a rel=\"noreferrer noopener\" href=\"https:\/\/firstinmichigan.us\/FTC\/hardware-configuration\/\" target=\"_blank\">hardware configuration<\/a>. In order to start using a device, we first have to <strong>create a variable<\/strong> for it. We will define this variable as an <strong>instance variable<\/strong> (outside of any methods) so that the entire class can use it:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted prettyprint\">...\n@TeleOp\npublic class BasicDrive extends LinearOpMode {\n    <span class=\"highlight\">private DcMotor left;<\/span>\n\n    @Override\n    public void runOpMode() {\n        \n        telemetry.addData(\"Status\", \"Initialized\");\n        telemetry.update();\n        \/\/ Wait for the game to start (driver presses PLAY)\n        waitForStart();\n\n        \/\/ run until the end of the match (driver presses STOP)\n        while (opModeIsActive()) {\n            telemetry.addData(\"Status\", \"Running\");\n            telemetry.update();\n        }\n    }\n}\n<\/pre>\n\n\n\n<p>I named this variable <code>left<\/code>. This is the same name as in the hardware configuration, but note that it doesn&#8217;t have to be; it is just generally good practice.<\/p>\n\n\n\n<p>We have now defined a variable to store our DC motor in. However, currently this variable is empty. In order to use the motor, we have to <strong>map the variable to the device in our hardware configuration<\/strong>. FTC provides the method <code>hardwareMap.dcMotor.get(deviceName)<\/code> to accomplish this, where <code>deviceName<\/code> is a <strong>string<\/strong> (text) indicating <em>exactly<\/em> what we named the device in our hardware configuration.<\/p>\n\n\n\n<p>In the <strong>init<\/strong> section of our code, let&#8217;s invoke this method and assign (set) it&#8217;s returned value to the <code>left<\/code> variable.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted prettyprint\">...\n@TeleOp\npublic class BasicDrive extends LinearOpMode {\n    <span class=\"highlight\">private DcMotor left;<\/span>\n\n    @Override\n    public void runOpMode() {\n        <span class=\"highlight\">left = hardwareMap.dcMotor.get(\"left\");<\/span>\n        telemetry.addData(\"Status\", \"Initialized\");\n        telemetry.update();\n        \/\/ Wait for the game to start (driver presses PLAY)\n        waitForStart();\n\n        \/\/ run until the end of the match (driver presses STOP)\n        while (opModeIsActive()) {\n            telemetry.addData(\"Status\", \"Running\");\n            telemetry.update();\n        }\n    }\n}\n<\/pre>\n\n\n\n<p>Notice that we surrounded the device name string in quotes (<code>\" \"<\/code>). String values must always be surrounded in quotes so that the computer can differentiate them from variables.<\/p>\n\n\n\n<p>The code we just wrote is our hardware configuration code. When you read the next guides, the hardware configuration code you will need for that guide will always be provided at the top for your convenience.<\/p>\n\n\n\n<p>Now that we have assigned our <code>left<\/code> variable, we can use it to control the corresponding motor on our robot. <code>left<\/code> is an object variable, meaning that it contains various methods that we can access using the <code>.<\/code> operator and invoke. In our <strong>loop<\/strong> code, let&#8217;s invoke the motor&#8217;s <code>setPower(power)<\/code> method with a <code>power<\/code> value of <code>1<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted prettyprint\">...\n@TeleOp\npublic class BasicDrive extends LinearOpMode {\n    <span class=\"highlight\">private DcMotor left;<\/span>\n\n    @Override\n    public void runOpMode() {\n        <span class=\"highlight\">left = hardwareMap.dcMotor.get(\"left\");<\/span>\n        telemetry.addData(\"Status\", \"Initialized\");\n        telemetry.update();\n        \/\/ Wait for the game to start (driver presses PLAY)\n        waitForStart();\n\n        \/\/ run until the end of the match (driver presses STOP)\n        while (opModeIsActive()) {\n            <span class=\"highlight\">left.setPower(1);<\/span>\n            telemetry.addData(\"Status\", \"Running\");\n            telemetry.update();\n        }\n    }\n}\n<\/pre>\n\n\n\n<p>Now we have created a simple linear TeleOp OpMode that maps and controls a device on our robot. The next guides on the <a href=\"\/FTC\/ftc-programming-resources\">FTC Programming Resources page<\/a> walk through various ways that devices can be used in order to control the robot.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Prerequisites If you are using OnBot Java as your editor, this guide will start with the program that we generated in the last guide. If you are using Android Studio as your editor, you can simply create a file called &#8220;BasicDrive.java&#8221; in your teamcode folder and copy paste the starting code in this guide. If you are new to the Java programming language, it is also recommended that you read the Java Syntax Basics guide. Even if you don&#8217;t understand everything in that guide (it is fairly technical), it will provide good background knowledge for this guide. The Linear OpMode Structure In this guide, &#8230;<a class=\"more-link\" href=\"https:\/\/firstinmichigan.us\/FTC\/ftc-java-basics\/\">Read More &rarr;<\/a><\/p>\n","protected":false},"author":7,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"nf_dc_page":"","footnotes":""},"class_list":["entry","page","publish","author-eschnabel","post-3171"],"_links":{"self":[{"href":"https:\/\/firstinmichigan.us\/FTC\/wp-json\/wp\/v2\/pages\/3171","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/firstinmichigan.us\/FTC\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/firstinmichigan.us\/FTC\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/firstinmichigan.us\/FTC\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/firstinmichigan.us\/FTC\/wp-json\/wp\/v2\/comments?post=3171"}],"version-history":[{"count":34,"href":"https:\/\/firstinmichigan.us\/FTC\/wp-json\/wp\/v2\/pages\/3171\/revisions"}],"predecessor-version":[{"id":3278,"href":"https:\/\/firstinmichigan.us\/FTC\/wp-json\/wp\/v2\/pages\/3171\/revisions\/3278"}],"wp:attachment":[{"href":"https:\/\/firstinmichigan.us\/FTC\/wp-json\/wp\/v2\/media?parent=3171"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}