Introduction to Autonomous Programming Team 7182 Mechanical Paradox

  • Slides: 50
Download presentation
Introduction to Autonomous Programming Team 7182, Mechanical Paradox Kieran Barvenik

Introduction to Autonomous Programming Team 7182, Mechanical Paradox Kieran Barvenik

This Class • This class is designed to be a hands-on workshop, where students

This Class • This class is designed to be a hands-on workshop, where students can have the chance not only to learn the information provided, but also to apply it • We hope that the workshop’s interactive medium will help people retain the information taught in the class more so than a normal lecture format would 2

This Class • The class will be covering FTC specific Java programming and the

This Class • The class will be covering FTC specific Java programming and the essential building blocks for writing code for your robot • We will be creating a program that commands a standard pushbot to move forwards for 2. 5 seconds, open, and close its claw 3

Java and Android Studio • The FTC hardware system operates over an Android platform,

Java and Android Studio • The FTC hardware system operates over an Android platform, which is written in Java • Android Studio is an IDE (Integrated Development Environment) specifically designed for Android programming • Interacting directly with the code is an extremely versatile way of writing programs that allows the writer to gain complete control over the program’s operation (in contrast to MIT App Developer, which is fairly limited) 4

Looking at Android Studio A First look at the IDE 5

Looking at Android Studio A First look at the IDE 5

 • The highlighted portion (in red) is the toolbar, where the user has

• The highlighted portion (in red) is the toolbar, where the user has access to the functions and shortcuts of the IDE 6

 • The highlighted portion is the file explorer, where the user has access

• The highlighted portion is the file explorer, where the user has access to the hierarchy of program files 7

 • The highlighted portion is the program window, where viewing and editing of

• The highlighted portion is the program window, where viewing and editing of code occurs 8

Android Studio Features • Android Studio includes a variety of other useful features for

Android Studio Features • Android Studio includes a variety of other useful features for programming including, but not limited to: • Intelligent code editor (advanced code completion, analysis, and refactoring) • Automatic integration with version control and project management software such as GIT • Fast and expansive emulator 9

What is an Op. Mode? • Op. Mode refers to a user generated operational

What is an Op. Mode? • Op. Mode refers to a user generated operational mode, or a program run on the robot • The Term Op. Mode encompasses both Autonomous and Teleoperated modes – Autonomous Op. Modes are robot-controlled programs that are run during the first 30 seconds of a match – Teleoperated Op. Modes are driver-controlled programs that are run during the last 2 minutes of a match 10

Structure and Run Order • The run cycle of the program is its order

Structure and Run Order • The run cycle of the program is its order of operations from the starting to the stopping of the program • Op. Modes contain multiple different prewritten methods (groups of code that perform an operation), each which plays during a different time during the run cycle of the program 11

Structure and Run Order • Methods vary between 2 different types of Op. Modes:

Structure and Run Order • Methods vary between 2 different types of Op. Modes: Normal and Linear – Normal Op. Mode: employs 4 main methods: init, start, loop, and stop – Linear Op. Mode: only uses one method, the run. Op. Mode method, that runs once the program is initialized. – We will be creating a Linear Op. Mode, because the concepts we are demonstrating work better with the Linear setup 12

Run Cycle Explanation In a Normal Op. Mode, pressing the init button will trigger

Run Cycle Explanation In a Normal Op. Mode, pressing the init button will trigger the init method ; however, in a Linear Op. Mode, the init button will trigger the only method, run. Op. Mode In a Normal Op. Mode, pressing the play button will immediately trigger the start method, followed by beginning the loop method In a Normal Op. Mode, pressing the stop button will stop the loop method, and proceed to run the stop method before exiting the program 13

Creating the Program File • The first step in creating an FTC Op. Mode

Creating the Program File • The first step in creating an FTC Op. Mode is to create a file to hold it • To do this, we need to navigate to the team code storage directory • • • Path: Team. Code/java/org. firstinspires. ftc. teamcode OR: FTCRobot. Controller/java/com. qualcomm. ftcrobotcontroller/opmodes

Creating the Program File • Right click the lowest directory in the hierarchy (for

Creating the Program File • Right click the lowest directory in the hierarchy (for non-Beta users, the opmodes folder; for Beta users, org. firstinspires. ftc. teamcode)

Creating the Program File • Select New, and Java Class • Enter the name

Creating the Program File • Select New, and Java Class • Enter the name of the program in the box (Note: although you can name the program virtually anything, it is best to name it something that relates to what it does such that you can recognize it easily)

The Program File • The resulting file that pops up should look like this:

The Program File • The resulting file that pops up should look like this: • The statement at the top that begins with the keyword package simply declares where our program is located. The curly braces after the public class Test. Op contain most of the Op. Mode’s code

Op. Mode Creation (importing) • Before we start writing code that will actually move

Op. Mode Creation (importing) • Before we start writing code that will actually move the robot, we need to import, or gain access to, the FTC SDK files. We need to add the following statements after the package statement but before public class …

Op. Mode Creation (importing) • Import statements effectively are giving our program access to

Op. Mode Creation (importing) • Import statements effectively are giving our program access to prewritten files in the package we specify in the statement • The above statement gives our program access to the Linear. Op. Mode code located in the package com. qualcomm. robotcore. eventloop. opmode

Op. Mode Creation (importing) • The program should now look like this:

Op. Mode Creation (importing) • The program should now look like this:

Op. Mode Creation (extension) • To begin creating the Op. Mode, we must first

Op. Mode Creation (extension) • To begin creating the Op. Mode, we must first add the keywords extends Linear. Op. Mode between the public class Test. Op and the opening curly brace 21

Variable Declaration [flesh out] • First, we need to define variables to hold data

Variable Declaration [flesh out] • First, we need to define variables to hold data that we will utilize and reference later in the program • We need to create 3 Dc. Motor variables and 2 Servo variables • The lines we add to declare motors and servos are added inside the curly braces. The statements look like these: 22

Variable Declaration • There are 3 motor variables to create: left. Drive, right. Drive,

Variable Declaration • There are 3 motor variables to create: left. Drive, right. Drive, and arm • There are 2 servo variables to create: left. Claw and right. Claw 23

Op. Mode Creation (run. Op. Mode) • In the body of the program (between

Op. Mode Creation (run. Op. Mode) • In the body of the program (between the curly braces and below our variable declarations), we now need to add the following block of code: This is the method in our program that will execute when the init button is pressed 24

25

25

Initializing Hardware 1 • Next, we need to bind the variables we created earlier

Initializing Hardware 1 • Next, we need to bind the variables we created earlier in the program to the robot’s hardware, such that the variables we interact with represent the motors and servos on the robot • The lines of code that we have to write to do this are placed inside the curly braces for the run. Op. Mode method 26

Initializing Hardware 2 • The line to initialize a motor looks as follows: •

Initializing Hardware 2 • The line to initialize a motor looks as follows: • The line to initialize a servo looks as follows: • The name inside the quotation marks is a name we give to the hardware device on the robot controller once the program is downloaded to the phone 27

28

28

Wait for Start • After these lines of code, we have to add a

Wait for Start • After these lines of code, we have to add a wait. For. Start() method call. Because the run. Op. Mode() method begins when the user presses the init button, the wait. For. Start() command tells the system not to continue until the play button is pressed on the phone 29

30

30

Motor Movement • After the wait. For. Start() method call, is where the movement

Motor Movement • After the wait. For. Start() method call, is where the movement procedure is written • To make a motor move, the method [motor. Name]. set. Power(0. 0) is called. The number put between the parenthesis is a decimal value that can range from -1. 0 to 1. 0, with -1. 0 being full power in reverse and 1. 0 being full power forwards. • To stop a motor’s movement, use 0 for the decimal number 31

Servo Movement • To move a servo, the line [servo. Name]. set. Position(0. 0)

Servo Movement • To move a servo, the line [servo. Name]. set. Position(0. 0) is called in the run. Op. Mode() method • The number inside the parenthesis is a decimal value that ranges from 0 to 1. 0 • The decimal represents a position from 0 degrees to 180 degrees, so the decimal equivalent to the position in degrees can be found by dividing the number of degrees by 180 32

Movement Code • To command the robot to drive forwards and open its claw,

Movement Code • To command the robot to drive forwards and open its claw, add the following code after the wait. For. Start(); : 33

34

34

Creating a Timer • To effectively write autonomous programs, a means of measuring distance

Creating a Timer • To effectively write autonomous programs, a means of measuring distance must be utilized • The simplest way to accomplish this is by creating a timer, though encoders or sensors can also be utilized • The timer included in the FTC API is called Elapsed. Time, a user controlled timer that can measure in units as small as milliseconds 35

Creating a Timer • First, we need to create an instance of Elapsed. Time

Creating a Timer • First, we need to create an instance of Elapsed. Time by placing the following line of code after our movement code: 36

Waiting • To tell our program to wait until a specific time has elapsed,

Waiting • To tell our program to wait until a specific time has elapsed, we have to set up a loop that will continually check if our desired amount of time has passed (2. 5 seconds, in our example) • To accomplish this, we add the following code after our movement code in the run. Op. Mode() method (e. Time. time() returns seconds, not milliseconds) 37

38

38

Stopping Movement • After the time has elapsed, we want to command the robot

Stopping Movement • After the time has elapsed, we want to command the robot to cease movement • To accomplish this, we will reuse the movement code we used earlier, but substituting 0 for the power value. The result is this: 39

Stopping Movement • Additionally, we are going to close the claw by implementing this

Stopping Movement • Additionally, we are going to close the claw by implementing this code: 40

41

41

Waiting at the End • In order for the servos to fully close, we

Waiting at the End • In order for the servos to fully close, we need the program to give them time to close; otherwise, the program will end and the servos will not move • We need to copy this code: And paste it at the end of the program to do that 42

43

43

Registration of Your Program (non. Beta) • Even though the FTC Op. Mode is

Registration of Your Program (non. Beta) • Even though the FTC Op. Mode is now complete, the program will not show up on the phone without registering it in the FTCOp. Mode. Register file • To do this, open the file and add a line of code like the following to the main body (with the other manager. register lines): 44

Registration Select the FTCOp. Mode. Register Program in the FTCOp. Mode. Register>com. qualcomm. ftcrobotcontroller->opmodes->FTCOp.

Registration Select the FTCOp. Mode. Register Program in the FTCOp. Mode. Register>com. qualcomm. ftcrobotcontroller->opmodes->FTCOp. Mode. Register directory 45

Registration Add the following line next to the other similar lines: 46

Registration Add the following line next to the other similar lines: 46

Registration (Beta Users) • Alternatively, if you are using the new beta version of

Registration (Beta Users) • Alternatively, if you are using the new beta version of the FTC SDK, registration of your program is far easier. Simply scroll to the top of your program file (the one the majority of code is written in) and insert the following line beneath the import statements:

Registration • This line identifies the program to the FTC Robot Controller as an

Registration • This line identifies the program to the FTC Robot Controller as an autonomous program (you can also use @Teleop for teleop programs). With the new version, the coding is all contained within one file rather than spread across multiple files, so the entire coding process is much more streamlined.

Contact Information and Website Address • The Power. Point used in this class will

Contact Information and Website Address • The Power. Point used in this class will be uploaded to our team’s website for anyone interested in going over the material again • Our team’s website is: http: //mechanicalparadox. org/ • If anyone from the class has questions about any of the topics covered, our team’s email is: mechanicalparadox 7182@gmail. com 50