review of c++ and object oriented programming
TRANSCRIPT
![Page 1: Review of C++ and Object Oriented Programming](https://reader033.vdocument.in/reader033/viewer/2022042619/586a45731a28ab52568c4111/html5/thumbnails/1.jpg)
Review of Java and Object Oriented Programming
Brad MillerAssociate Director Robotics Resource Center
[email protected], December 13, 2010
![Page 2: Review of C++ and Object Oriented Programming](https://reader033.vdocument.in/reader033/viewer/2022042619/586a45731a28ab52568c4111/html5/thumbnails/2.jpg)
What do I do?§ Associate Director Robotics Resource Center
§ Responsible for the WPILib–Used for LabVIEW, C++, and now Java
§ Involved with FIRST since 2001§ On the Game Design Software Sub-Committee
§ One of the mentors for Team 190
Monday, December 13, 2010
![Page 3: Review of C++ and Object Oriented Programming](https://reader033.vdocument.in/reader033/viewer/2022042619/586a45731a28ab52568c4111/html5/thumbnails/3.jpg)
What is WPI?§ Worcester Polytechnic Institute
–Worcester, Massachusetts§ Undergraduate and graduate university with an emphasis on technology and engineering
Monday, December 13, 2010
![Page 4: Review of C++ and Object Oriented Programming](https://reader033.vdocument.in/reader033/viewer/2022042619/586a45731a28ab52568c4111/html5/thumbnails/4.jpg)
Robotics Engineering at WPI§ 3rd largest major on campus based on incoming freshman
§ Students learn Mechanical Engineering, Software Engineering, and Electrical Engineering
§ I used to say, “It’s not about the robots”.Now it is!
Monday, December 13, 2010
![Page 6: Review of C++ and Object Oriented Programming](https://reader033.vdocument.in/reader033/viewer/2022042619/586a45731a28ab52568c4111/html5/thumbnails/6.jpg)
Agenda§ What is it?§ Java§ Look at a sample Program§ Using the Tools§ More Java§ A few sample built in classes§ Differences C++ and Java§ Documentation …§ What’s new for 2011
Monday, December 13, 2010
![Page 7: Review of C++ and Object Oriented Programming](https://reader033.vdocument.in/reader033/viewer/2022042619/586a45731a28ab52568c4111/html5/thumbnails/7.jpg)
Goals of Java for FRC§ Provide a safe text based language§ Reduce the barrier to entry§ Have a program environment that matches what students learn in school
§ Simplify robot programming even more
Monday, December 13, 2010
![Page 8: Review of C++ and Object Oriented Programming](https://reader033.vdocument.in/reader033/viewer/2022042619/586a45731a28ab52568c4111/html5/thumbnails/8.jpg)
What is it?§ Java implementation of the WPILib API for FRC Robots–We call it WPILibJ
§ Sponsored by Sun Microsystems (Oracle)§ Demonstrated to FIRST in fall 2008§ Released to FRC teams for the 2010 season§ Started by
–Derek White (Sun)–Eric Arseneau (Sun)–Brad Miller (WPI)–Students from WPI
Monday, December 13, 2010
![Page 9: Review of C++ and Object Oriented Programming](https://reader033.vdocument.in/reader033/viewer/2022042619/586a45731a28ab52568c4111/html5/thumbnails/9.jpg)
Java based on Squawk§ Java Virtual Machine for small devices
–Sun Labs Project – PI – Derek White–Java ME CLDC 1.1 IMP 1.0
§ Sun Microsystems Labs project / mobile and embedded community project–https://squawk.dev.java.net
§ Java Operating Environment for small devices§ Bootloader / Micro Kernel
Monday, December 13, 2010
![Page 10: Review of C++ and Object Oriented Programming](https://reader033.vdocument.in/reader033/viewer/2022042619/586a45731a28ab52568c4111/html5/thumbnails/10.jpg)
WPILibJ§ Equivalent functionality as C++/LabVIEW
–Teams can learn about and use object oriented programming techniques
§ Framework to support competition programs§ Runs on top of VxWorks
–Full support for Java threading features§ Parity between C/C++ and LabVIEW libraries
Monday, December 13, 2010
![Page 11: Review of C++ and Object Oriented Programming](https://reader033.vdocument.in/reader033/viewer/2022042619/586a45731a28ab52568c4111/html5/thumbnails/11.jpg)
WPILib Features§ Currently supported devices through functions and C++ Classes (more on the way)
– Access to LabVIEW camera / image processing library– High performance A/D converter– Speed controls with settable profiles– Gyros & Accelerometers– Driver Station I/O and Dashboard– Relays and solenoids– Counters, gear-tooth sensors and encoders– Timing– Driving based on robot definition– Arcade, tank, holonomic (Mecanum) steering– Compressor & pressure sensor
§ 23
Monday, December 13, 2010
![Page 12: Review of C++ and Object Oriented Programming](https://reader033.vdocument.in/reader033/viewer/2022042619/586a45731a28ab52568c4111/html5/thumbnails/12.jpg)
Everything is Open Source§ Community ownership - nothing proprietary
–Allowing community contributors make for a more acceptable standard code base
§ Nothing hidden “under the covers”–Gives teams a better understanding of how code
works–Teams can now be more self supporting without
relying on a single person/company for support§ Teams can easily extend and adapt code for their own needs
Monday, December 13, 2010
![Page 13: Review of C++ and Object Oriented Programming](https://reader033.vdocument.in/reader033/viewer/2022042619/586a45731a28ab52568c4111/html5/thumbnails/13.jpg)
Why Objects?§Objects are a good way of representing real-world “things”–Motors, sensors, driver station, robot drive train,
robot subsystems, your whole robot–Have data associated with the “thing” and code
that operates on the data (member data and methods)
–Some objects are more specific versions of other objects (subclasses)
Monday, December 13, 2010
![Page 14: Review of C++ and Object Oriented Programming](https://reader033.vdocument.in/reader033/viewer/2022042619/586a45731a28ab52568c4111/html5/thumbnails/14.jpg)
Object Oriented Programming§C++ = C + Objects + other features
–C++ programs that don’t use the extensions look like C programs
§C++ objects are similar to Java objects–Java taught in many high schools–Java used on programming AP exams
Monday, December 13, 2010
![Page 15: Review of C++ and Object Oriented Programming](https://reader033.vdocument.in/reader033/viewer/2022042619/586a45731a28ab52568c4111/html5/thumbnails/15.jpg)
Beginner’s Guide to Java
Monday, December 13, 2010
![Page 16: Review of C++ and Object Oriented Programming](https://reader033.vdocument.in/reader033/viewer/2022042619/586a45731a28ab52568c4111/html5/thumbnails/16.jpg)
Classes§ Classes represent templates of information–Like blank forms
§ Objects are filled out forms–Filling out the template (blank form)
instantiates it
Monday, December 13, 2010
![Page 17: Review of C++ and Object Oriented Programming](https://reader033.vdocument.in/reader033/viewer/2022042619/586a45731a28ab52568c4111/html5/thumbnails/17.jpg)
Comments are Important
§ Comments document what your code does
§ Should be in all programs so that you and others will remember how the
/* * This code does this. */
Monday, December 13, 2010
![Page 18: Review of C++ and Object Oriented Programming](https://reader033.vdocument.in/reader033/viewer/2022042619/586a45731a28ab52568c4111/html5/thumbnails/18.jpg)
import
§ Imports classes from the WPILib package (collection of programs)
§ The name of the package is the reverse of the Internet domain name
import edu.wpi.first.wpilib.*;
Monday, December 13, 2010
![Page 19: Review of C++ and Object Oriented Programming](https://reader033.vdocument.in/reader033/viewer/2022042619/586a45731a28ab52568c4111/html5/thumbnails/19.jpg)
Class declarations
§ “MySimpleRobot” defines a new class
public class MySimpleRobot { public void run() { move(); turnLeft(); move(); }}
Monday, December 13, 2010
![Page 20: Review of C++ and Object Oriented Programming](https://reader033.vdocument.in/reader033/viewer/2022042619/586a45731a28ab52568c4111/html5/thumbnails/20.jpg)
Adding new methods
§ Adding a capability to MySimpleRobot§ run is a new method
public class MySimpleRobot { public void run() { move(); turnLeft(); move(); }}
Monday, December 13, 2010
![Page 21: Review of C++ and Object Oriented Programming](https://reader033.vdocument.in/reader033/viewer/2022042619/586a45731a28ab52568c4111/html5/thumbnails/21.jpg)
§ The method is called run and it’s implementation (code) codes between the braces
§ public - this method can be used outside of the class§ void - this method doesn’t return a result
public class MySimpleRobot { public void run() { move(); turnLeft(); move(); }}
Monday, December 13, 2010
![Page 22: Review of C++ and Object Oriented Programming](https://reader033.vdocument.in/reader033/viewer/2022042619/586a45731a28ab52568c4111/html5/thumbnails/22.jpg)
The Method Body
§ This is the implementation of the method§ Each statement ends in a semicolon, and the statements are executed sequentially
public class MySimpleRobot { public void run() { move(); turnLeft(); move(); }}
Monday, December 13, 2010
![Page 23: Review of C++ and Object Oriented Programming](https://reader033.vdocument.in/reader033/viewer/2022042619/586a45731a28ab52568c4111/html5/thumbnails/23.jpg)
Defining New Methods
§ Adding methods to a class encapsulates the information
§ private methods are only accessible inside the class
§ private methods hide information about the implementation (information hiding)
private void turnAround() { turnLeft(); turnLeft();}
Monday, December 13, 2010
![Page 24: Review of C++ and Object Oriented Programming](https://reader033.vdocument.in/reader033/viewer/2022042619/586a45731a28ab52568c4111/html5/thumbnails/24.jpg)
Control Statements§ Conditional Statements
–if and if-else§ Iteration statements
–while - doing something until condition–for - doing something a number of times
Monday, December 13, 2010
![Page 25: Review of C++ and Object Oriented Programming](https://reader033.vdocument.in/reader033/viewer/2022042619/586a45731a28ab52568c4111/html5/thumbnails/25.jpg)
If Statements
§ Starts with the if part§ Then the statements to execute if the condition is true
§ Can choose between operations with else§ Possible conditions: frontIsClear, leftIsClear, beepersPresent, beepersInBag, etc.
if (rightIsClear()) {fillPotHole();
}
Monday, December 13, 2010
![Page 26: Review of C++ and Object Oriented Programming](https://reader033.vdocument.in/reader033/viewer/2022042619/586a45731a28ab52568c4111/html5/thumbnails/26.jpg)
for statement
§ Used when you know the number of times to do some operations
for (int i = 0; i < 4; i++) {move(10);turnRight(90);
}
Monday, December 13, 2010
![Page 27: Review of C++ and Object Oriented Programming](https://reader033.vdocument.in/reader033/viewer/2022042619/586a45731a28ab52568c4111/html5/thumbnails/27.jpg)
while Statement
§ Repeats operation while a condition
int i=0;while (i < 4) { move(10); turnRight(90); i++;}
Monday, December 13, 2010
![Page 28: Review of C++ and Object Oriented Programming](https://reader033.vdocument.in/reader033/viewer/2022042619/586a45731a28ab52568c4111/html5/thumbnails/28.jpg)
Airplane Reservation§ What data is stored in a reservation?§ What can you do with a reservation?§ How do you refer to a particular reservation?
Monday, December 13, 2010
![Page 29: Review of C++ and Object Oriented Programming](https://reader033.vdocument.in/reader033/viewer/2022042619/586a45731a28ab52568c4111/html5/thumbnails/29.jpg)
Instances and Classes
§ Use the new operator to create an instance from a class–new returns a reference
to the object–refer to the member
variables and methods using the “.” operator
Reservation bradRes = new Reservation();bradRes.setName(“Brad Miller”);
class Reservation {private String name;private double baseCost;private int seat;
public double getCost() {return baseCost * 1.12;
}
public void setName(String n) {name = n;
}}
membervariables
methods
Initialize the name
Monday, December 13, 2010
![Page 30: Review of C++ and Object Oriented Programming](https://reader033.vdocument.in/reader033/viewer/2022042619/586a45731a28ab52568c4111/html5/thumbnails/30.jpg)
Constructors§ Oops! What’s the value of the other member variables that we didn’t initialize when the class was instantiated?
§ Wouldn’t it be nice if the name got filled in when the new happened?
§ In fact, wouldn’t it be nice if all the member variables could be filled in when the class was instantiated?
Monday, December 13, 2010
![Page 31: Review of C++ and Object Oriented Programming](https://reader033.vdocument.in/reader033/viewer/2022042619/586a45731a28ab52568c4111/html5/thumbnails/31.jpg)
Constructors to the Rescue
§ A method with the same name as the class is called when the object is created
§ And it can have parameters - cool!§ Now remove setName... maybe
Reservation bradRes = new Reservation(“Brad Miller”);
class Reservation {
Reservation(String n) {name = n;baseCost = 0.0;seat = 0;
}// other stuff in class here too...
}
Look. Not only is it initializing name, but all the other member variables too!
Monday, December 13, 2010
![Page 32: Review of C++ and Object Oriented Programming](https://reader033.vdocument.in/reader033/viewer/2022042619/586a45731a28ab52568c4111/html5/thumbnails/32.jpg)
Motors§ Our programs send PWM signals to motors (actually to the motor speed controllers) to control them
§ What would an object look like that controls a motor? What sort of methods would it have?
Monday, December 13, 2010
![Page 33: Review of C++ and Object Oriented Programming](https://reader033.vdocument.in/reader033/viewer/2022042619/586a45731a28ab52568c4111/html5/thumbnails/33.jpg)
Speed Controllers
§ Let’s call our class PWM since it controls a PWM output on the controller. What should it do?–We’d like to linearize the output signal and
we know the min and max PWM values and the center
–So we can make setSpeed do math to linearize the values
class PWM {public PWM(int portNumber) {...}public void setSpeed(double speed) {...}public double getSpeed() {...}
}
Monday, December 13, 2010
![Page 34: Review of C++ and Object Oriented Programming](https://reader033.vdocument.in/reader033/viewer/2022042619/586a45731a28ab52568c4111/html5/thumbnails/34.jpg)
Using a PWM object
§ Looks pretty straightforward, nice linearized speed controllers
§ I’m happy... how about you?
// Instantiate the PWM objectsPWM left = new PWM(2);PWM right = new PWM(3);
// set the speeds for full forwardright.setSpeed(1.0);left.setSpeed(-1.0);
Monday, December 13, 2010
![Page 35: Review of C++ and Object Oriented Programming](https://reader033.vdocument.in/reader033/viewer/2022042619/586a45731a28ab52568c4111/html5/thumbnails/35.jpg)
Enter the Jaguar§ Just when we thought everything was OK, we get Jaguars
§ Different min and max values§ Different curve to linearize§ We can’t use our PWM object to control Jaguars
Monday, December 13, 2010
![Page 36: Review of C++ and Object Oriented Programming](https://reader033.vdocument.in/reader033/viewer/2022042619/586a45731a28ab52568c4111/html5/thumbnails/36.jpg)
A New Class for Jaguars
class PWM {public PWM(int portNumber) {...}public void setSpeed(double speed) {...}public double getSpeed() {...}
}
Monday, December 13, 2010
![Page 37: Review of C++ and Object Oriented Programming](https://reader033.vdocument.in/reader033/viewer/2022042619/586a45731a28ab52568c4111/html5/thumbnails/37.jpg)
A New Class for Jaguars
class PWM {public PWM(int portNumber) {...}public void setSpeed(double speed) {...}public double getSpeed() {...}
}
Monday, December 13, 2010
![Page 38: Review of C++ and Object Oriented Programming](https://reader033.vdocument.in/reader033/viewer/2022042619/586a45731a28ab52568c4111/html5/thumbnails/38.jpg)
A New Class for Jaguars
class PWM {public PWM(int portNumber) {...}public void setSpeed(double speed) {...}public double getSpeed() {...}
}
class Victor {public Victor(int portNumber) {...}public void setSpeed(double speed) {...}public double getSpeed() {...}
}
Monday, December 13, 2010
![Page 39: Review of C++ and Object Oriented Programming](https://reader033.vdocument.in/reader033/viewer/2022042619/586a45731a28ab52568c4111/html5/thumbnails/39.jpg)
A New Class for Jaguars
class PWM {public PWM(int portNumber) {...}public void setSpeed(double speed) {...}public double getSpeed() {...}
}
class Victor {public Victor(int portNumber) {...}public void setSpeed(double speed) {...}public double getSpeed() {...}
}
and add...
Monday, December 13, 2010
![Page 40: Review of C++ and Object Oriented Programming](https://reader033.vdocument.in/reader033/viewer/2022042619/586a45731a28ab52568c4111/html5/thumbnails/40.jpg)
A New Class for Jaguars
class PWM {public PWM(int portNumber) {...}public void setSpeed(double speed) {...}public double getSpeed() {...}
}
class Victor {public Victor(int portNumber) {...}public void setSpeed(double speed) {...}public double getSpeed() {...}
}
class Jaguar {public Jaguar(int portNumber) {...}public void setSpeed(double speed) {...}public double getSpeed() {...}
}
and add...
Monday, December 13, 2010
![Page 41: Review of C++ and Object Oriented Programming](https://reader033.vdocument.in/reader033/viewer/2022042619/586a45731a28ab52568c4111/html5/thumbnails/41.jpg)
A New Class for Jaguars
class PWM {public PWM(int portNumber) {...}public void setSpeed(double speed) {...}public double getSpeed() {...}
}
class Victor {public Victor(int portNumber) {...}public void setSpeed(double speed) {...}public double getSpeed() {...}
}
class Jaguar {public Jaguar(int portNumber) {...}public void setSpeed(double speed) {...}public double getSpeed() {...}
}
and add...
But all the linearization code and PWM generation code is duplicated in Victor and Jaguar :-(
Monday, December 13, 2010
![Page 42: Review of C++ and Object Oriented Programming](https://reader033.vdocument.in/reader033/viewer/2022042619/586a45731a28ab52568c4111/html5/thumbnails/42.jpg)
Back up a Few Steps§ We had PWMs§ Victors are PWMs with one type of curve§ Jaguars are PWMs with another type of curve
§ The curves can be represented by some values (min and max PWM, min and max deadband, and center)
Monday, December 13, 2010
![Page 43: Review of C++ and Object Oriented Programming](https://reader033.vdocument.in/reader033/viewer/2022042619/586a45731a28ab52568c4111/html5/thumbnails/43.jpg)
The Solution: Inheritanceclass PWM {
public PWM(int portNumber) {...}public void setSpeed(double speed) {...}public double getSpeed() {...}public void setBounds(int min, int max,
int highDB, int lowDB, int center) {...}
}
class Jaguar extends PWM {public Jaguar(int portNumber) {
super(portNumber);setBounds(...); // Jaguar values
}
class Victor extends PWM {public Victor(int portNumber) {
super(portNumber);setBounds(...); // Victor values
}
Jaguar and Victor both “inherit from”
or extend PWM
Monday, December 13, 2010
![Page 44: Review of C++ and Object Oriented Programming](https://reader033.vdocument.in/reader033/viewer/2022042619/586a45731a28ab52568c4111/html5/thumbnails/44.jpg)
Inheritance§ Some terminology
–PWM is the superclass–Jaguar and Victor are subclasses
§ Jaguar and Victor get every public (or protected) method and instance variable from PWM
§ Jaguar and Victor have their own constructor that can call the superclass (PWM) constructor
Monday, December 13, 2010
![Page 45: Review of C++ and Object Oriented Programming](https://reader033.vdocument.in/reader033/viewer/2022042619/586a45731a28ab52568c4111/html5/thumbnails/45.jpg)
RobotDrive§ Robot drive trains have a lot in common:
–Some motors (almost always 2 or 4)–Can be controlled with Joysticks–Can be controlled with code–Have some limited drive modes: tank, arcade,
meccanum, maybe some more
Monday, December 13, 2010
![Page 46: Review of C++ and Object Oriented Programming](https://reader033.vdocument.in/reader033/viewer/2022042619/586a45731a28ab52568c4111/html5/thumbnails/46.jpg)
Design a RobotDrive class
class RobotDrive {public RobotDrive(Victor leftMotor, Victor rightMotor) {...}public void drive(double speed, double direction) {...}public void tank(Joystick leftStick, Joystick rightStick) {...}public void arcade(Joystick stick) {...}
Here we go again, what about Jaguars?
Monday, December 13, 2010
![Page 47: Review of C++ and Object Oriented Programming](https://reader033.vdocument.in/reader033/viewer/2022042619/586a45731a28ab52568c4111/html5/thumbnails/47.jpg)
More RobotDrive
class RobotDrive {public RobotDrive(PWM leftMotor, PWM rightMotor) {...}public void drive(double speed, double direction) {...}public void tank(Joystick leftStick, Joystick rightStick) {...}public void arcade(Joystick stick) {...}
Monday, December 13, 2010
![Page 48: Review of C++ and Object Oriented Programming](https://reader033.vdocument.in/reader033/viewer/2022042619/586a45731a28ab52568c4111/html5/thumbnails/48.jpg)
More RobotDrive
Does this work? After all, Victors and Jaguars are both PWMs
class RobotDrive {public RobotDrive(PWM leftMotor, PWM rightMotor) {...}public void drive(double speed, double direction) {...}public void tank(Joystick leftStick, Joystick rightStick) {...}public void arcade(Joystick stick) {...}
Monday, December 13, 2010
![Page 49: Review of C++ and Object Oriented Programming](https://reader033.vdocument.in/reader033/viewer/2022042619/586a45731a28ab52568c4111/html5/thumbnails/49.jpg)
More RobotDrive
Does this work? After all, Victors and Jaguars are both PWMs
class RobotDrive {public RobotDrive(PWM leftMotor, PWM rightMotor) {...}public void drive(double speed, double direction) {...}public void tank(Joystick leftStick, Joystick rightStick) {...}public void arcade(Joystick stick) {...}
It does, but it will break soon. Why?
Monday, December 13, 2010
![Page 50: Review of C++ and Object Oriented Programming](https://reader033.vdocument.in/reader033/viewer/2022042619/586a45731a28ab52568c4111/html5/thumbnails/50.jpg)
Now There are CAN Jaguars§ CAN connected Jaguars don’t use PWM signaling - you just tell it the speed and the Jaguar does it
§ It doesn’t make sense to base the new CANJaguar class on PWM
§ So the RobotDrive class can’t operate on PWM objects, it needs something else
Monday, December 13, 2010
![Page 51: Review of C++ and Object Oriented Programming](https://reader033.vdocument.in/reader033/viewer/2022042619/586a45731a28ab52568c4111/html5/thumbnails/51.jpg)
Quick overview of Objects
Monday, December 13, 2010
![Page 52: Review of C++ and Object Oriented Programming](https://reader033.vdocument.in/reader033/viewer/2022042619/586a45731a28ab52568c4111/html5/thumbnails/52.jpg)
Objects (Classes)
Quadrature Encoder ObjectWhat can it do?getCount()getPeriod()reset()
What does it know?Port numbersReversed or not
Methods
RobotDrive ObjectWhat can it do?drive()arcadeDrive()tankDrive()
What does it know?Port numbersSpeed controller type
MemberVariables
Monday, December 13, 2010
![Page 53: Review of C++ and Object Oriented Programming](https://reader033.vdocument.in/reader033/viewer/2022042619/586a45731a28ab52568c4111/html5/thumbnails/53.jpg)
SimpleRobot class
Subclass of a class
autonomous() { print message “Autonomous” }
operatorControl() { print message “Operator control” }
Very boring!
isAutonomous() { return the current field state }
isEnabled() { return robot state }
Monday, December 13, 2010
![Page 54: Review of C++ and Object Oriented Programming](https://reader033.vdocument.in/reader033/viewer/2022042619/586a45731a28ab52568c4111/html5/thumbnails/54.jpg)
SimpleRobot class
Subclass of a class
autonomous() { print message “Autonomous” }
operatorControl() { print message “Operator control” }
Very boring!
isAutonomous() { return the current field state }
isEnabled() { return robot state }
Team190Robot class
Monday, December 13, 2010
![Page 55: Review of C++ and Object Oriented Programming](https://reader033.vdocument.in/reader033/viewer/2022042619/586a45731a28ab52568c4111/html5/thumbnails/55.jpg)
SimpleRobot class
Subclass of a class
autonomous() { print message “Autonomous” }
operatorControl() { print message “Operator control” }
Very boring!
autonomous() { Drive around the field and score point }
operatorControl() { Drive our robot using tank steering}
isAutonomous() { return the current field state }
isEnabled() { return robot state }
Team190Robot class
Monday, December 13, 2010
![Page 56: Review of C++ and Object Oriented Programming](https://reader033.vdocument.in/reader033/viewer/2022042619/586a45731a28ab52568c4111/html5/thumbnails/56.jpg)
SimpleRobot class
Subclass of a class
autonomous() { print message “Autonomous” }
operatorControl() { print message “Operator control” }
Very boring!
autonomous() { Drive around the field and score point }
operatorControl() { Drive our robot using tank steering}
Now very cool!
isAutonomous() { return the current field state }
isEnabled() { return robot state }
Team190Robot class
Monday, December 13, 2010
![Page 57: Review of C++ and Object Oriented Programming](https://reader033.vdocument.in/reader033/viewer/2022042619/586a45731a28ab52568c4111/html5/thumbnails/57.jpg)
Objects in sample program
RobotDrive Object
Joystick Object
Constructor:RobotDrive(leftmotor, rightmotor);Methods:drive(speed, curve);arcade(joystick);
Constructor:Joystick(port);Methods:getX();gety();getTwist(); …
stick
myRobot
Monday, December 13, 2010
![Page 58: Review of C++ and Object Oriented Programming](https://reader033.vdocument.in/reader033/viewer/2022042619/586a45731a28ab52568c4111/html5/thumbnails/58.jpg)
Robot Definitionpackage edu.wpi.first.wpilibj.templates;import edu.wpi.first.wpilibj.SimpleRobot;public class Team190Robot extends SimpleRobot { public void autonomous() { } public void operatorControl() { }}
Monday, December 13, 2010
![Page 59: Review of C++ and Object Oriented Programming](https://reader033.vdocument.in/reader033/viewer/2022042619/586a45731a28ab52568c4111/html5/thumbnails/59.jpg)
Declarations & InitializationRobotDrive drive;Joystick leftStick;Joystick rightStick;
// Constructorpublic Team190Robot() { drive = new RobotDrive(1, 2); leftStick = new Joystick(1); rightStick = new Joystick(2);}
Monday, December 13, 2010
![Page 60: Review of C++ and Object Oriented Programming](https://reader033.vdocument.in/reader033/viewer/2022042619/586a45731a28ab52568c4111/html5/thumbnails/60.jpg)
Autonomous part of program
public void autonomous(void){ drive.drive(0.5, 0.0); Wait(2000); drive.drive(0.0, 0.0);}
Use the RobotDrive object drive method to go 0.5 speed (forward, half speed) with no turn Then stop.
Key to the example:drive.drive(speed, curve) speed: a value from -1.0 to 1.0 where 0.0 is stopped curve: a value from -1.0 to 1.0 where 0.0 is no turn
Monday, December 13, 2010
![Page 61: Review of C++ and Object Oriented Programming](https://reader033.vdocument.in/reader033/viewer/2022042619/586a45731a28ab52568c4111/html5/thumbnails/61.jpg)
TeleOp part of program
public void operatorControl(){ while (true) { drive.tankDrive(leftStick, rightStick); Timer.delay(0.01); }}
Use the RobotBase object ArcadeDrive method to do arcade driving using our
Joystick object
Re-enable the watchdog since we aren’t waiting
any more
Monday, December 13, 2010
![Page 62: Review of C++ and Object Oriented Programming](https://reader033.vdocument.in/reader033/viewer/2022042619/586a45731a28ab52568c4111/html5/thumbnails/62.jpg)
Microedition not Desktop Java§ Microedition is Java used on phones§ No dynamic class loading§ Pre-verification step§ Finalization not implemented§ JNI not supported JNA instead§ Serialization and RMI not supported§ No User Interface APIs - Swing and AWT§ Threads are supported without thread groups§ No new features such as Generics and Autoboxing, yet…
Monday, December 13, 2010
![Page 63: Review of C++ and Object Oriented Programming](https://reader033.vdocument.in/reader033/viewer/2022042619/586a45731a28ab52568c4111/html5/thumbnails/63.jpg)
Cross platform development tools§ NetBeans & Eclipse
–Run on Windows, Linux, and Mac OS X§ Still need Windows/LabVIEW to image the cRIO
§ To install on alternate platforms...–Install Java–Install NetBeans or Eclipse–Install the plugins
Monday, December 13, 2010
![Page 64: Review of C++ and Object Oriented Programming](https://reader033.vdocument.in/reader033/viewer/2022042619/586a45731a28ab52568c4111/html5/thumbnails/64.jpg)
Development Tools§ NetBeans
–Open source IDE• Supported by Sun Microsystems and community development
–Custom FRC plugins§ Eclipse
–Open source–Available for 2011
Monday, December 13, 2010
![Page 65: Review of C++ and Object Oriented Programming](https://reader033.vdocument.in/reader033/viewer/2022042619/586a45731a28ab52568c4111/html5/thumbnails/65.jpg)
Creating a Robot Program
Monday, December 13, 2010
![Page 66: Review of C++ and Object Oriented Programming](https://reader033.vdocument.in/reader033/viewer/2022042619/586a45731a28ab52568c4111/html5/thumbnails/66.jpg)
First programpackage edu.wpi.first.wpilibj.templates;import edu.wpi.first.wpilibj.SimpleRobot;public class Team190Robot extends SimpleRobot { public void autonomous() { } public void operatorControl() { }}
§ Easy template program that you fill in–autonomous and
operatorControl methods called at the appropriate times
–You can fill in constructor with initialization code
Monday, December 13, 2010
![Page 67: Review of C++ and Object Oriented Programming](https://reader033.vdocument.in/reader033/viewer/2022042619/586a45731a28ab52568c4111/html5/thumbnails/67.jpg)
Compiling the program§ Select “Build Main Project” and that’s it!
–Designate project as main project if there is more than one
§ Program builds and errors are highlighted
Monday, December 13, 2010
![Page 68: Review of C++ and Object Oriented Programming](https://reader033.vdocument.in/reader033/viewer/2022042619/586a45731a28ab52568c4111/html5/thumbnails/68.jpg)
Running the program§ Select “Run main project” or use the arrow in the toolbar–Program will compile–Then get downloaded to the cRIO
§ Set your driver station to Autonomous or Teleop and Enabled
§ Thats it!
Monday, December 13, 2010
![Page 69: Review of C++ and Object Oriented Programming](https://reader033.vdocument.in/reader033/viewer/2022042619/586a45731a28ab52568c4111/html5/thumbnails/69.jpg)
Debugging the code
1. Place breakpoint in your code2.Click on debug button3.Wait for “Waiting for connection message4.Attach debugger
Monday, December 13, 2010
![Page 70: Review of C++ and Object Oriented Programming](https://reader033.vdocument.in/reader033/viewer/2022042619/586a45731a28ab52568c4111/html5/thumbnails/70.jpg)
How Java Programs Start§ OTA server runs at cRIO boot
–Named JavaVM.out–OTA server loads squawk.out (VM) and starts it–Squawk loads midlet class from suite file
• Robot class is derived from midlet–Robot program starts
§ NetBeans loads programs–Verifies the versions of squawk and the OTA server
and loads new ones if necessary–Loads the user program–Reboots the cRIO
Monday, December 13, 2010
![Page 71: Review of C++ and Object Oriented Programming](https://reader033.vdocument.in/reader033/viewer/2022042619/586a45731a28ab52568c4111/html5/thumbnails/71.jpg)
Threads built in to Java§ Threads part of the Java language
Monday, December 13, 2010
![Page 72: Review of C++ and Object Oriented Programming](https://reader033.vdocument.in/reader033/viewer/2022042619/586a45731a28ab52568c4111/html5/thumbnails/72.jpg)
Java brings runtime checking§ In C++ there is no runtime checking for pointers or subscripts–Everything is a Downloadable Kernel Module
§ The good news:–Java provides full runtime checking
§ The bad news:–Java provides full runtime checking
§ It will help you find bugs, but will terminate your program when it finds them
Monday, December 13, 2010
![Page 73: Review of C++ and Object Oriented Programming](https://reader033.vdocument.in/reader033/viewer/2022042619/586a45731a28ab52568c4111/html5/thumbnails/73.jpg)
Exceptions
§ An exception is an event that disrupts the normal flow of the program
§ A method checks for error condition§ Exception is “thrown” when an error occurs§ Calling program checks for an exception using a try-catch block
Monday, December 13, 2010
![Page 74: Review of C++ and Object Oriented Programming](https://reader033.vdocument.in/reader033/viewer/2022042619/586a45731a28ab52568c4111/html5/thumbnails/74.jpg)
Exceptions are passed up§ If the calling method doesn’t check, it’s passed up the call stack
§ If no method catches the exception, the runtime system does–But the program terminates
Monday, December 13, 2010
![Page 75: Review of C++ and Object Oriented Programming](https://reader033.vdocument.in/reader033/viewer/2022042619/586a45731a28ab52568c4111/html5/thumbnails/75.jpg)
Three types of exceptions§ Checked exception: an error condition a program might encounter and should be checked for
§ Error exception: something external to the application that it can’t anticipate or recover from
§ Runtime exception: subscript out of bounds or a null pointer. Best to fix the program!
Monday, December 13, 2010
![Page 76: Review of C++ and Object Oriented Programming](https://reader033.vdocument.in/reader033/viewer/2022042619/586a45731a28ab52568c4111/html5/thumbnails/76.jpg)
WPILib exception policy§ Unchecked exceptions for program errors like bad port numbers, etc.–You can optionally check for these
§ Checked exceptions for runtime errors–You’re required to check for these
§ Implication–Bad port numbers will likely kill the program–Runtime errors will be caught
Monday, December 13, 2010
![Page 77: Review of C++ and Object Oriented Programming](https://reader033.vdocument.in/reader033/viewer/2022042619/586a45731a28ab52568c4111/html5/thumbnails/77.jpg)
Error handling§ Exceptions§ Errors on the dashboard
Monday, December 13, 2010
![Page 78: Review of C++ and Object Oriented Programming](https://reader033.vdocument.in/reader033/viewer/2022042619/586a45731a28ab52568c4111/html5/thumbnails/78.jpg)
A few sample built in classes§ Camera and imaging classes§ PID control
Monday, December 13, 2010
![Page 79: Review of C++ and Object Oriented Programming](https://reader033.vdocument.in/reader033/viewer/2022042619/586a45731a28ab52568c4111/html5/thumbnails/79.jpg)
2010 Camera/Image Classes§ Based on an image class
–Image class is an abstract base class• ColorImage and MonoImage subclassed from Image• HSLImage, RGBImage from ColorImage• BinaryImage from MonoImage (for masks)
–ParticleAnalysisReport object§ AxisCamera class
–Object oriented interface to camera–Web interface settings now in class
• Brightness• Exposure control
Monday, December 13, 2010
![Page 80: Review of C++ and Object Oriented Programming](https://reader033.vdocument.in/reader033/viewer/2022042619/586a45731a28ab52568c4111/html5/thumbnails/80.jpg)
AxisCamera Class§ Camera class is singleton
–AxisCamera.getInstance()§ Several methods to set parameters
–writeBrightness–writeWhiteBalance–writeColorLevel–writeExposureControl–writeExposurePriority–writeResolution–writeCompression–writeRotation
Monday, December 13, 2010
![Page 81: Review of C++ and Object Oriented Programming](https://reader033.vdocument.in/reader033/viewer/2022042619/586a45731a28ab52568c4111/html5/thumbnails/81.jpg)
Image classes
Image
ColorImage
HSLImage RGBImage
MonoImage
BinaryImage
§ Different image types depending on the application
Monday, December 13, 2010
![Page 82: Review of C++ and Object Oriented Programming](https://reader033.vdocument.in/reader033/viewer/2022042619/586a45731a28ab52568c4111/html5/thumbnails/82.jpg)
Image classes
ColorImage (RGB & HSL) Supports plane extraction and thresholding
MonoImage Shape detectionBinaryImage Particle detectionHSLImage Returned by the Camera
Monday, December 13, 2010
![Page 83: Review of C++ and Object Oriented Programming](https://reader033.vdocument.in/reader033/viewer/2022042619/586a45731a28ab52568c4111/html5/thumbnails/83.jpg)
Image methods§ All images have:
–Height and width–Write operation to file
§ Color Images–Get/Replace color planes–Thresholding to extract masks in ranges–RGB and HSL images can load from file
§ Binary Images–Particle analysis reporting
Monday, December 13, 2010
![Page 84: Review of C++ and Object Oriented Programming](https://reader033.vdocument.in/reader033/viewer/2022042619/586a45731a28ab52568c4111/html5/thumbnails/84.jpg)
Finding Pink / Green target§ Threshold on an HSLImage for each color to generate BinaryImages
§ Apply getOrderedParticleAnalysisReport on the BinaryImages–List of particleAnalysisReports for each image–Results correspond to each pink/green object
Monday, December 13, 2010
![Page 85: Review of C++ and Object Oriented Programming](https://reader033.vdocument.in/reader033/viewer/2022042619/586a45731a28ab52568c4111/html5/thumbnails/85.jpg)
Implementation§ Image operations and images are implemented using C++ library–Better memory usage (no garbage collection)–Higher performance–Less copying of images
§ Therefore - images need to be manually freed after use
Monday, December 13, 2010
![Page 86: Review of C++ and Object Oriented Programming](https://reader033.vdocument.in/reader033/viewer/2022042619/586a45731a28ab52568c4111/html5/thumbnails/86.jpg)
PID Class§ PID = Proportional, Integral, Differential
–Integral and Differential terms are time-based§ Interfaces for source and target objects
Monday, December 13, 2010
![Page 87: Review of C++ and Object Oriented Programming](https://reader033.vdocument.in/reader033/viewer/2022042619/586a45731a28ab52568c4111/html5/thumbnails/87.jpg)
PID Interfaces§ Add these interfaces to define input and output
§ Sources are:– Accelerometer– Analog Channel– Encoder– Gyro– Ultrasonic
§ Outputs are:– Jaguar– Victor
public interface PIDOutput { public void pidWrite(double output);}
public interface PIDSource { public double pidGet();}
Monday, December 13, 2010
![Page 88: Review of C++ and Object Oriented Programming](https://reader033.vdocument.in/reader033/viewer/2022042619/586a45731a28ab52568c4111/html5/thumbnails/88.jpg)
PIDController 1
public PIDController(double Kp, double Ki, double Kd, PIDSource source, PIDOutput output, double period) { m_controlLoop = new java.util.Timer(); m_P = Kp; m_I = Ki; m_D = Kd; m_pidInput = source; m_pidOutput = output; m_period = period; m_controlLoop.schedule(new PIDTask(this), 0L, (long) (m_period * 1000));}
Monday, December 13, 2010
![Page 89: Review of C++ and Object Oriented Programming](https://reader033.vdocument.in/reader033/viewer/2022042619/586a45731a28ab52568c4111/html5/thumbnails/89.jpg)
PIDController 2 private class PIDTask extends TimerTask {
private PIDController m_controller;
public PIDTask(PIDController controller) { m_controller = controller; }
public void run() { m_controller.calculate(); } }
Monday, December 13, 2010
![Page 90: Review of C++ and Object Oriented Programming](https://reader033.vdocument.in/reader033/viewer/2022042619/586a45731a28ab52568c4111/html5/thumbnails/90.jpg)
PID Controller 3 private synchronized void calculate() {
double input = m_pidInput.pidGet();
// calculations go here
m_pidOutput.pidWrite(m_result);
}
Monday, December 13, 2010
![Page 91: Review of C++ and Object Oriented Programming](https://reader033.vdocument.in/reader033/viewer/2022042619/586a45731a28ab52568c4111/html5/thumbnails/91.jpg)
Java and C++ compared§ Java and C++ are very similar§ Can’t tell them apart from a distance
–Able to port code by editing§ Same classes, same methods (almost)§ If you can write in one language, then you can also write in the other
Monday, December 13, 2010
![Page 92: Review of C++ and Object Oriented Programming](https://reader033.vdocument.in/reader033/viewer/2022042619/586a45731a28ab52568c4111/html5/thumbnails/92.jpg)
Java and C++ compared§ Garbage collection§ Header files§ Single inheritance with interfaces§ No pointers (sort of)§ Runtime checking§ New development tools§ Performance
Monday, December 13, 2010
![Page 93: Review of C++ and Object Oriented Programming](https://reader033.vdocument.in/reader033/viewer/2022042619/586a45731a28ab52568c4111/html5/thumbnails/93.jpg)
Naming conventions§ Method names
–C++ - Uppercase first letter, then camel case–Java - Lowercase first letter, then camel case
§ Names are otherwise the same
Monday, December 13, 2010
![Page 94: Review of C++ and Object Oriented Programming](https://reader033.vdocument.in/reader033/viewer/2022042619/586a45731a28ab52568c4111/html5/thumbnails/94.jpg)
No include files§ Java source files only - no includes necessary§ Use import to resolve references to external code
§ Packages define ownership, path (and some access) for groups of modules
Monday, December 13, 2010
![Page 95: Review of C++ and Object Oriented Programming](https://reader033.vdocument.in/reader033/viewer/2022042619/586a45731a28ab52568c4111/html5/thumbnails/95.jpg)
Special Class Methods§C++ has constructors and destructors, Java only has constructors
§Constructors (method with class-name)–Called automatically when an instance is created–Used to initialize resources
Monday, December 13, 2010
![Page 96: Review of C++ and Object Oriented Programming](https://reader033.vdocument.in/reader033/viewer/2022042619/586a45731a28ab52568c4111/html5/thumbnails/96.jpg)
Documentation§ Getting Started with Java§ WPILib Users Guide§ Includes JavaDocs for all APIs§ Java Tutorial (at java.sun.com)
§ And user generated JavaDoc for your own classes
Monday, December 13, 2010
![Page 97: Review of C++ and Object Oriented Programming](https://reader033.vdocument.in/reader033/viewer/2022042619/586a45731a28ab52568c4111/html5/thumbnails/97.jpg)
Getting the source code§ Source code is included in the distribution
–Look at <user account>/sunspotfrcsdk/lib/WPILibJ/src• Open that location as a project
–Generate JavaDocs from that project• Right-click on project and select “Generate Javadocs”
§ Also on FIRSTForge server–Details will be available soon–Uses a Subversion repository
Monday, December 13, 2010
![Page 98: Review of C++ and Object Oriented Programming](https://reader033.vdocument.in/reader033/viewer/2022042619/586a45731a28ab52568c4111/html5/thumbnails/98.jpg)
Installing
§ Follow instructions on web site–http://first.wpi.edu–http://first.wpi.edu/FRC/frcjava.html
§ You can get the 2010 kit from there today§ 2011 kit will be released at Kickoff
Monday, December 13, 2010
![Page 99: Review of C++ and Object Oriented Programming](https://reader033.vdocument.in/reader033/viewer/2022042619/586a45731a28ab52568c4111/html5/thumbnails/99.jpg)
Getting started
1. Download JDK and NetBeans
2. Install software
3. Install plugins
Monday, December 13, 2010
![Page 100: Review of C++ and Object Oriented Programming](https://reader033.vdocument.in/reader033/viewer/2022042619/586a45731a28ab52568c4111/html5/thumbnails/100.jpg)
What’s new for 2011
§ Eclipse Plugin§ Dashboard§ Motor Safety
Monday, December 13, 2010
![Page 101: Review of C++ and Object Oriented Programming](https://reader033.vdocument.in/reader033/viewer/2022042619/586a45731a28ab52568c4111/html5/thumbnails/101.jpg)
Eclipse Plugin
§ Eclipse is a more widely used IDE§ Also cross platform§ Eclipse is where all new work will be done§ Work in progress
• Graphical robot model• Graphical state machine programing model• Event system
Monday, December 13, 2010
![Page 102: Review of C++ and Object Oriented Programming](https://reader033.vdocument.in/reader033/viewer/2022042619/586a45731a28ab52568c4111/html5/thumbnails/102.jpg)
Dashboard
§ Automatic dashboard generator§ Call log methods on robot§ Interface on Classmate is auto generated§ Work in progress
–Save and edit layouts
Monday, December 13, 2010
![Page 103: Review of C++ and Object Oriented Programming](https://reader033.vdocument.in/reader033/viewer/2022042619/586a45731a28ab52568c4111/html5/thumbnails/103.jpg)
Motor Safety
§ Replacement for user watchdog–Same across all languages
§ No explicit watchdog feeding needed§ Expiration timers for each motor§ Setting motor values resets timer§ Timer timeout stops motor
Monday, December 13, 2010
![Page 104: Review of C++ and Object Oriented Programming](https://reader033.vdocument.in/reader033/viewer/2022042619/586a45731a28ab52568c4111/html5/thumbnails/104.jpg)
Last but not least§ Source Control§ NetBeans & Eclipse support Subversion and Mercurial–We will be using SVN for FIRSTForge
§ You should seriously consider using source control for your teams–Can revert if (when) code breaks–Can track revisions–Multiple people can work on it at the same time
Monday, December 13, 2010
![Page 105: Review of C++ and Object Oriented Programming](https://reader033.vdocument.in/reader033/viewer/2022042619/586a45731a28ab52568c4111/html5/thumbnails/105.jpg)
The END§ Go to http://first.wpi.edu/ for more information§ This presentation will be posted
Monday, December 13, 2010