The time has come to program our robot to respond to our gamepad inputs so we can drive it around! To start we will be focusing on controlling our drivetrain.
Think back to the very start of Part 2 for a moment. We will be programming our robot using the Arcade Style of TeleOp. This means our forward and back movements will be controlled using the y-axis of the joystick while turning will be controlled by the x-axis.
This section will introduce the use of variables as we create our program. This will allow us to set up calculations in our program that will determine what power the motors should receive based on our gamepad's input. How much power each motor receives changes how the robot will move, so it is important to also review this relationship will establishing our code!
Below is a sneak peek at our final program:
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_TeleOpextendsLinearOpMode {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");rightmotor.setDirection(DcMotorSimple.Direction.REVERSE);double x;double y;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()) {//Drivetrain Control x =gamepad1.right_stick_x; y =-gamepad1.right_stick_y;rightmotor.setPower(y-x);leftmotor.setPower(y+x);telemetry.addData("Status","Running");telemetry.update(); } }}