Java Workshop. Quick java review Goal: We want the application to tell us what the nth fibonacci...

Post on 01-Apr-2015

214 views 1 download

Transcript of Java Workshop. Quick java review Goal: We want the application to tell us what the nth fibonacci...

Java Workshop

Quick java review• Goal: We want the application to tell us

what the nth fibonacci number is.

• Components needed to achieve the goal:

• A Scanner to listen to inputs

• A function to calculate the fibonacci numbers

How to instantiate an object

• School lowellHighSchool = new School(697);

Type of object

Name of object

MAGIC WORD TO MAKE NEW

THINGS!!

Constructor with argument

• Start a new java application in Netbeans

• Name it fibonacciNumberCalculator

• Java has a scanner class part of the utility package, so we can just take it for granted.

• Instantiate a new scanner and write some code to listen to the input and spit it back out.

• Java has a scanner class part of the utility package, so we can just take it for granted.

• Instantiate a new scanner and write some code to listen to the input and spit it back out.

• But we don't want the program to just quit after putting in one number, we want it to keep on listening to our inputs.

• So let’s put it in a while-true loop

• Well, how do we deal with the less intelligent people that can’t seem to enter whole numbers??

• Let’s use a try-catch

• Now what? Stuck in a unwanted feature! (infinite loop)

• So we need to make our own parsing logic

• First we need to check if there’s a NEW input

• Then we take the input and try to parse it as integer

• Components needed to achieve the goal:

• A Scanner to listen to inputs

• A function to calculate the fibonacci numbers

• Now we can move on to the second component of the application

• What is fibonacci number?

• It’s actually really easy to make a recursive function in computer language! So why don’t we calculate it with a recursive function!

• Complete this function

• Now let’s modify the program so we can interface the user input with our calculating function

GREAT ARE WE DONE YET?!?!?

• Not quite. As you can see, recursive functions are one of the slowest functions in computer science. There are tricks to make it significantly faster by using more memory to store those values, but how can we make it faster when we don’t have a massive memory bank and very limited computing power like on a robot??

• Thanks to this cool guy

• Jacques Philippe Marie Binet

• He found this!

• Well let’s not put his hard work up for display!

• Now that we have 2 different methods that can calculate the fibonacci sequence, which one SHOULD we use?

• Why don’t we use both?

• Write a function that takes in the user input as argument, and automatically chooses which method to use.

FRC Java• Difference:

• Written by WPI

• Slimmed down JVM

• Essentially Java ME (SE 1.2/3 ish)

• No generics, autoboxing

• FIRST provided us with 3 basic styles to control the robot

• Simple Robot

• Iterative Robot

• Command Based Robot

Command Based• Very very detailed control of every

subsystem

• Requires A LOT of testing/fine tuning.

• Idea for teams with a lot of time at their hands.

Iterative Robot• Much more automation and less

programmer control than command base robot.

• Still allows for modularization in different modes

• Good for teams that is trying to optimize resource consumption on the cRIO.

Simple Robot• Used by most first year teams without a

strong programming team or is trying to switch over to Java

• Easy to use, best for testing prototypes!!!

• 4 Modes

• Autonomous, Teleop, Disabled, Testing

Our version• It’s a 3 way hybrid that we wrote on our

own.

• It’s written from ground up, with modularization in mind.

• It’s automated like simple and iterative robot, but also allows for command based controls and it’s been multi-threaded to optimize for performance

Simple Robot• Let’s start with simple robot

• To control anything, you must first register it with the system.

• For example, lets walkthrough how to setup a drivetrain.

• Physically we have:

Motor Speed Contoller PWM Signal

You Joystick cRIO Sidecar

How to instantiate an object

• School lowellHighSchool = new School(697);

Type of object

Name of object

MAGIC WORD TO MAKE NEW

THINGS!!

Constructor with argument

How to access class methods

• The Dot Operator!!!

• lowellHighSchool.getTeachersNames();

• access the function written in School class that will return the names of the teachers of lowellHighSchool

• lowellHighSchool.enrollStudent(“John Smith”);

• enrolls the student named John Smith into lowellHighSchool

• We are really just interfacing with the speed controllers via a joystick

• So we must register those two things with the program

• To do so, simply import the needed library and create an instant of the desired object

• Now that we have the physical connections setup, lets tell the computer what to do with those connections

• We want to use the RobotDrive class and tell that class about these two connections we created

• Our connection has been setup

• We told the cRIO where the two speed controllers are (port 1&2) and what we would like to use them for (RobotDrive)

• We told the robot that we have a joystick plugged into virtual usb hub 1

• Now let’s do something with the system in operatorControl

• Since it’s simple robot, we just need to dump code into the operatorControl function and the cRIO will run whatever it’s inside.

• We are accessing the arcadeDrive function written for RobotDrive class.

• If you don’t know what to put inside an argument, like

• PIDController drivePID = new PIDController(?????????);

• That takes you to the programming bible (API)

Find the name of the class you have

questions about

This is what we’re

interested in

• Now we know what to put into the question marks:

• PIDController drivePID = new PIDController(?????????);

• You have to pick one of the overloaded constructors then instantiate your drivePID system

• You can also find information about the class functions, that’s why it’s called the bible!

• If you have time, try writing this

• Use a gyro to drive straight in autonomous mode for 10 seconds.

• You need to create an instance of a gyroscope, and use the reading from the gyroscope to correct the heading

• hint: use arcadeDrive as driving method instead.