Start by creating a new OpMode called HelloRobot_ElapsedTime using the BlankLinearOpMode sample similar to what we used in Part 1.
Selecting the features discussed above will allow you to start with the following code. Remember that if "Setup Code for Configured Hardware" is selected the OpMode will try to generate a hardwareMap based on the active configuration. This example uses the same Hello Robot config file we originally created!
packageorg.firstinspires.ftc.teamcode;importcom.qualcomm.robotcore.eventloop.opmode.LinearOpMode;importcom.qualcomm.robotcore.hardware.Blinker;importcom.qualcomm.robotcore.hardware.Servo;importcom.qualcomm.robotcore.hardware.TouchSensor;importcom.qualcomm.robotcore.eventloop.opmode.TeleOp;importcom.qualcomm.robotcore.eventloop.opmode.Disabled;importcom.qualcomm.robotcore.hardware.DcMotor;importcom.qualcomm.robotcore.hardware.DcMotorSimple;importcom.qualcomm.robotcore.util.ElapsedTime;@TeleOppublicclassHelloRobot_ElapsedTimeextendsLinearOpMode {privateBlinker control_Hub;privateDcMotor arm;privateDcMotor leftmotor;privateDcMotor rightmotor;privateDcMotor test_motor;privateServo test_servo;privateTouchSensor test_touch; @OverridepublicvoidrunOpMode() { control_Hub =hardwareMap.get(Blinker.class,"Control Hub"); arm =hardwareMap.get(DcMotor.class,"arm"); leftmotor =hardwareMap.get(DcMotor.class,"leftmotor"); rightmotor =hardwareMap.get(DcMotor.class,"rightmotor"); test_motor =hardwareMap.get(DcMotor.class,"test_motor"); test_servo =hardwareMap.get(Servo.class,"test_servo"); test_touch =hardwareMap.get(TouchSensor.class,"test_touch");telemetry.addData("Status","Initialized");telemetry.update();// Wait for the game to start (driver presses PLAY)waitForStart();// run until the end of the match (driver presses STOP)while (opModeIsActive()) {telemetry.addData("Status","Running");telemetry.update(); } }}
Setting up the Basics
To prepare to use ElapsedTime, a variable and an instance of ElapsedTime needs to be created. To do this the following line is needed:
privateElapsedTime runtime =newElapsedTime();
In this case we are named our variable runtime.
The above line performs two actions:
A ElapsedTime variable called runtime is created. Once it is created and defined as an ElapsedTime variable, it can hold the relevant time information and data.
The other part of the line, runtime = new ElapsedTime();, creates an instance of the ElapsedTime timer object and assigns it to our new runtime variable.
Add this line to the OpMode with the other private variables:
In OnBot Java the private keyword is an access modifier that, in this situation, means this variable can only be used within the class it was defined. Our class for this project is our "public class HelloRobot_ElapsedTime".
Next we can go ahead and add the basic movement for our motors. For this example, we will set both motors to a power of 1:
@TeleOppublicclassHelloRobot_ElapsedTimeextendsLinearOpMode {privateBlinker control_Hub;privateDcMotor arm;privateDcMotor leftmotor;privateDcMotor rightmotor;privateDcMotor test_motor;privateServo test_servo;privateTouchSensor test_touch;privateElapsedTime runtime =newElapsedTime(); @OverridepublicvoidrunOpMode() { control_Hub =hardwareMap.get(Blinker.class,"Control Hub"); arm =hardwareMap.get(DcMotor.class,"arm"); leftmotor =hardwareMap.get(DcMotor.class,"leftmotor"); rightmotor =hardwareMap.get(DcMotor.class,"rightmotor"); test_motor =hardwareMap.get(DcMotor.class,"test_motor"); test_servo =hardwareMap.get(Servo.class,"test_servo"); test_touch =hardwareMap.get(TouchSensor.class,"test_touch");telemetry.addData("Status","Initialized");telemetry.update();// Wait for the game to start (driver presses PLAY)waitForStart();// run until the end of the match (driver presses STOP)while (opModeIsActive()) {leftmotor.setPower(1);rightmotor.setPower(1);telemetry.addData("Status","Running");telemetry.update(); } }}
Lastly, we need to make sure our right motor's direction is reversed during initialization:
@OverridepublicvoidrunOpMode() { control_Hub =hardwareMap.get(Blinker.class,"Control Hub"); arm =hardwareMap.get(DcMotor.class,"arm"); leftmotor =hardwareMap.get(DcMotor.class,"leftmotor"); rightmotor =hardwareMap.get(DcMotor.class,"rightmotor"); test_motor =hardwareMap.get(DcMotor.class,"test_motor"); test_servo =hardwareMap.get(Servo.class,"test_servo"); test_touch =hardwareMap.get(TouchSensor.class,"test_touch");rightmotor.setDirection(DcMotorSimple.Direction.REVERSE);telemetry.addData("Status","Initialized");telemetry.update();// Wait for the game to start (driver presses PLAY)waitForStart();