DPG2013
05-02-2013, 16:33
I am kind of new to Java programming and I was looking through the WPI cookbookhere (http://604robotics.com/Files/WPI%20CookBook.pdf) and under the command robot tutorial I saw this:
public class Chassis extends Subsystem {
public static final int LEFT_MOTOR_PORT = 2;
public static final int RIGHT_MOTOR_PORT = 1;
public static final double TURN_AMOUNT = 1.0;
public static final double FORWARD_SPEED = 1.0;
private static Chassis instance = null;
private RobotDrive drive;
public static Chassis getInstance() {
if(instance == null) {
instance = new Chassis();
instance.setDefaultCommand(new DriveWithJoystick());
}
return instance;
}
private Chassis() {
drive = new RobotDrive(LEFT_MOTOR_PORT, RIGHT_MOTOR_PORT);
drive.setSafetyEnabled(false); // to simplify explanation (not good practice)
}
public void straight() {
drive.arcadeDrive(-FORWARD_SPEED, 0.0); // start driving forward
}
public void turnLeft() {
drive.arcadeDrive(0.0, TURN_AMOUNT); // start turning
}
public void driveWithJoystick() {
drive.arcadeDrive(OI.getInstance().getJoystick());
}
I pretty much understand every thing except the instance part. What is a instance and how is it being used here.
public class Chassis extends Subsystem {
public static final int LEFT_MOTOR_PORT = 2;
public static final int RIGHT_MOTOR_PORT = 1;
public static final double TURN_AMOUNT = 1.0;
public static final double FORWARD_SPEED = 1.0;
private static Chassis instance = null;
private RobotDrive drive;
public static Chassis getInstance() {
if(instance == null) {
instance = new Chassis();
instance.setDefaultCommand(new DriveWithJoystick());
}
return instance;
}
private Chassis() {
drive = new RobotDrive(LEFT_MOTOR_PORT, RIGHT_MOTOR_PORT);
drive.setSafetyEnabled(false); // to simplify explanation (not good practice)
}
public void straight() {
drive.arcadeDrive(-FORWARD_SPEED, 0.0); // start driving forward
}
public void turnLeft() {
drive.arcadeDrive(0.0, TURN_AMOUNT); // start turning
}
public void driveWithJoystick() {
drive.arcadeDrive(OI.getInstance().getJoystick());
}
I pretty much understand every thing except the instance part. What is a instance and how is it being used here.