FTC – Java Programming

Transcription

Kettering FTC WorkshopFTC – JAVA PROGRAMMINGWorkshop 2015Eric WeberFRC: 1322, FTC: 5954 & 7032EW - 2015

Kettering FTC WorkshopJava History First appeared in 1995 Sun Microsystems creates and maintains the corelanguage Community involvement is very high in thedevelopment Appears in many small devices Want college credit in Computer Science?– Java is the standard language for AP CS courses Most importantly, it is currently the gateway intoother languages– Know java? You know C, C , C#, Python, Ruby, Pascaland many others with minimal understandingEW - 2015

Kettering FTC WorkshopWhat you will need? Android Studio– http://developer.android.com/sdk/index.html#top FTC App– https://github.com/ftctechnh/ftc app/archive/master.zip Tutorial for setting up phones– https://github.com/ftctechnh/ftc app/blob/master/doc/tutorial/FTCTraining Manual.pdfEW - 2015

Kettering FTC WorkshopIf you haven’t followed the instructionsonhttp://paws.kettering.edu/ webe3546/Start DownloadingThe process will take a long time tocomplete4EW - 2015

Kettering FTC WorkshopIntroducing the UIProject TabCompilerSource Code EditorEW - 2015

Kettering FTC WorkshopImportant Definitions IDE (Integrated Development Environment):– Android Studio itself is an IDE. It contains a sourcecode editor, compiler, and a debugger all in one. OP Modes: define how our robots behave– Teleop and Autonomous modes are now called OPModes Keywords: Reserved words that Java requires, andcannot be used as an unique nameEW - 2015

Kettering FTC WorkshopTELEOP EXAMPLE CODEWill work with the robot being built during this workshop.EW - 2015

Kettering FTC WorkshopCode Objective:Don’t write this down yet.We will cover this line byline.EW - 2015

Kettering FTC WorkshopTeleop Mode Example: This code is strictly for a simple tele OP mode Not optimal for programming a robot with anautonomous mode Equivalent to a ‘Hello World’ for the robot that isbeing built in the class rooms upstairsEW - 2015

Kettering FTC WorkshopCreating an OP Mode:1) In the Project Tree Navigate:- FTC APP - FtcRobotController - src - main - com.qualcomm.ftcrobotcontroller - opmodes3) Right Click on the Folder4) Go to “New” - “Java Class”5) Give it a name6) Click “OK”EW - 2015

Kettering FTC WorkshopEdit Class Definition: Once we have created our OP Mode, we need toedit a line immediately. Please add “extends OpMode” between the nameof class and the “{“EW - 2015

Kettering FTC WorkshopAnyone Notice This? Then you may auto complete with theselected word below Press the “Tab” key to allow completion. A benefit of using an IDE allows easierfunctionalityEW - 2015

Kettering FTC WorkshopImportant Notes: First we are defining a public class– Classes defines data formatting and procedures– Public defines how it may be accessed In this case, anywhere Second we have a unique name for these classes– Must be unique and not be a keyword Third we are extending a parent class (Inheritance)– We are directly adding onto a class already made– We also gain the functionality of this classEW - 2015

Kettering FTC WorkshopDefine Properties Next we will enter in the following below These are what are called properties or fields– From here on out, we will refer to them as propertiesEW - 2015

Kettering FTC WorkshopImportant Notes: Properties allow us to define data to be used In this case we are defining:– 1 DcMotorController (a class soon to be an object)– 2 DcMotor (another class soon to be an object) Later we will be able to effect the values of the DCMotors In non-OOP languages, these are also known asvariables (RobotC) If you want more, you have to define moreEW - 2015

Kettering FTC WorkshopOur first Method Methods allow us to perform tasks Please enter the next lines after our properties:EW - 2015

Kettering FTC WorkshopImportant Notes: First off, @Override allows us to over write aprevious method from OpMode.– This is one of two methods that MUST be overridden.– This will always be the first method called once theARM button is pressed on the robot controller. Second, we are assigning actual objects to theproperties we already have definedEW - 2015

Kettering FTC WorkshopImportant Notes: Third, what is hardwareMap?– It is an object that contains all the hardware mappingas defined by the configuration files on your RobotController app– Everything stated by your robot configuration file willbe here. This makes setting up your configurationcorrectly and translate it EXACTLY into your javacode.EW - 2015

Kettering FTC WorkshopOur Second Method The second method we must override is the loop()method. Please enter the next lines after our previousmethod.EW - 2015

Kettering FTC WorkshopImportant Notes: This method is called every time the robot cycles(approx. 20ms give or take) Not where to apply a loop Since a part of OpMode, this will be consistentwith autonomous OpModes as wellEW - 2015

Kettering FTC WorkshopFinal step: Register your OpMode We need to finalize the app by registering our OpMode with therest of the program. Navigate through the project tab to: ftc app-master - FtcRobotController - src - main - java - com - qualcomm - ftcrobotcontroller - opmodes - FtcOpModeRegister Under the register method, type: manager.register(“Tutorial”,Tutorial.class);EW - 2015

Kettering FTC WorkshopImportant Notes: To be able to select your OpMode, it needs to beadded to a list. I have already trimmed down the OpModes thatwere used as tutorials. NullOp will do nothing.– Good to keep due to any issues that arise.EW - 2015

Kettering FTC WorkshopImportant Definitions: Class: Defines data format and proceduresProperties: Variables defined by the classMethods: Procedures that work on inputs or propertiesInheritance: The ability to extend a class to include morefunctionality (methods) or data (properties) Overriding: The ability to take a method and change it togive different functionality Constructor: As an object is created, a special method isalways called immediately.EW - 2015

Kettering FTC WorkshopSTRUCTURAL SUGGESTIONSIdeas to extend your code from Teleop to Autonomous modes24EW - 2015

Kettering FTC WorkshopHierarchy Object Oriented Programming’s Greatestasset is reusability and extensibility. Better to define a robot by its actions, thencontrol it through those actions.CoreFunctionsTeleopModeTestZoneAutoModesEW - 2015

Kettering FTC WorkshopKey Ideas on Inheritance Inheritance Properties:– Extending a base class forces the base class to exist,giving us those methods as well Think drive systems, arms, sensors, or timers required But know about Private, Public, and Protected– We only need to override the operation modes wewant.– For instance, leave initialization for the base class, onlywork with loop() in the actual OpModes.EW - 2015

Kettering FTC WorkshopImportant Keys to Note: Methods with inheritanceOverriding (Virtual Methods)OpModesEncapsulationEW - 2015

Kettering FTC WorkshopNOW AN OPEN DISCUSSION.Questions and Suggestions?What would you like to see28EW - 2015

Kettering FTC WorkshopAUTONOMOUS MODEA method to accomplish tasks in Autonomous mode29EW - 2015

Kettering FTC WorkshopState Machines For this style of programming, State Machines arethe suggested method. Review of State Machines:– Idea of states: Set of instructions unique to a phase ofa program– States define what the robot is to do– Redefine outputs– Read inputs to trigger next stateEW - 2015

Kettering FTC WorkshopState Machines Requirements of a state machine:––––A state variable (usually an enumeration)A state selector (always a case-switch operator)State triggers (sensors or timers)An initial stateInitializeDriveForwardCheckEncodersStopEW - 2015

Kettering FTC WorkshopEnumerations: Enumerations are unique names with valuesdefined behind them Common examples include compass directions(values of NORTH, SOUTH, EAST, and WEST) Place above the actual OpModeEW - 2015

Kettering FTC WorkshopSwitch and Case Structure: Allows for multiple cases or states to make differentoperations Selector can take Enumeration’s, Integer’s commonlySwitch Structure – Refers to allcasesSelector – Selects whatever valueis enteredCase – Different states, as selectedby the selectorBreak- Escapes out of structureDefault- If no valid case exists,default will always be usedEW - 2015

Kettering FTC WorkshopTriggers: Usually done by an if statement– If statements are like case’s, but can easier to definewith logical statements Causes a change in our state variableEW - 2015

Kettering FTC WorkshopPutting it Together: We get the following OpMode:EW - 2015

Kettering FTC WorkshopFINAL REMARKS &QUESTIONSEW - 2015

Kettering FTC WorkshopThank YouNow get out their and program!EW - 2015

Kettering FTC Workshop EW - 2015 FTC –JAVA PROGRAMMING Workshop 2015 Eric Weber FRC: 1322, FTC: 5954 & 7032File Size: 738KB