|
| 1 | +/* Copyright (c) 2022 FIRST. All rights reserved. |
| 2 | + * |
| 3 | + * Redistribution and use in source and binary forms, with or without modification, |
| 4 | + * are permitted (subject to the limitations in the disclaimer below) provided that |
| 5 | + * the following conditions are met: |
| 6 | + * |
| 7 | + * Redistributions of source code must retain the above copyright notice, this list |
| 8 | + * of conditions and the following disclaimer. |
| 9 | + * |
| 10 | + * Redistributions in binary form must reproduce the above copyright notice, this |
| 11 | + * list of conditions and the following disclaimer in the documentation and/or |
| 12 | + * other materials provided with the distribution. |
| 13 | + * |
| 14 | + * Neither the name of FIRST nor the names of its contributors may be used to endorse or |
| 15 | + * promote products derived from this software without specific prior written permission. |
| 16 | + * |
| 17 | + * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS |
| 18 | + * LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 | + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 20 | + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 21 | + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE |
| 22 | + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 23 | + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 24 | + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 25 | + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 26 | + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 27 | + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | + */ |
| 29 | + |
| 30 | +package org.firstinspires.ftc.robotcontroller.external.samples; |
| 31 | + |
| 32 | +import com.qualcomm.robotcore.eventloop.opmode.Disabled; |
| 33 | +import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode; |
| 34 | +import com.qualcomm.robotcore.eventloop.opmode.TeleOp; |
| 35 | +import com.qualcomm.robotcore.util.Range; |
| 36 | + |
| 37 | +/** |
| 38 | + * This OpMode Sample illustrates how to use an external "hardware" class to modularize all the robot's sensors and actuators. |
| 39 | + * This approach is very efficient because the same hardware class can be used by all of your teleop and autonomous OpModes |
| 40 | + * without requiring many copy & paste operations. Once you have defined and tested the hardware class with one OpMode, |
| 41 | + * it is instantly available to other OpModes. |
| 42 | + * |
| 43 | + * The real benefit of this approach is that as you tweak your robot hardware, you only need to make changes in ONE place (the Hardware Class). |
| 44 | + * So, to be effective you should put as much or your hardware setup and access code as possible in the hardware class. |
| 45 | + * Essentially anything you do with hardware in BOTH Teleop and Auto should likely go in the hardware class. |
| 46 | + * |
| 47 | + * The Hardware Class is created in a separate file, and then an "instance" of this class is created in each OpMode. |
| 48 | + * In order for the class to do typical OpMode things (like send telemetry data) it must be passed a reference to the |
| 49 | + * OpMode object when it's created, so it can access all core OpMode functions. This is illustrated below. |
| 50 | + * |
| 51 | + * In this concept sample, the hardware class file is called RobotHardware.java and it must accompany this sample OpMode. |
| 52 | + * So, if you copy ConceptExternalHardwareClass.java into TeamCode (using Android Studio or OnBotJava) then RobotHardware.java |
| 53 | + * must also be copied to the same location (maintaining its name). |
| 54 | + * |
| 55 | + * For comparison purposes, this sample and its accompanying hardware class duplicates the functionality of the |
| 56 | + * RobotTelopPOV_Linear opmode. It assumes three motors (left_drive, right_drive and arm) and two servos (left_hand and right_hand) |
| 57 | + * |
| 58 | + * View the RobotHardware.java class file for more details |
| 59 | + * |
| 60 | + * Use Android Studio to Copy this Class, and Paste it into your team's code folder with a new name. |
| 61 | + * Remove or comment out the @Disabled line to add this opmode to the Driver Station OpMode list |
| 62 | + * |
| 63 | + * In OnBot Java, add a new OpMode, drawing from this Sample; select TeleOp. |
| 64 | + * Also add another new file named RobotHardware.java, drawing from the Sample with that name; select Not an OpMode. |
| 65 | + */ |
| 66 | + |
| 67 | +@TeleOp(name="Concept: Robot Hardware Class", group="Robot") |
| 68 | +@Disabled |
| 69 | +public class ConceptExternalHardwareClass extends LinearOpMode { |
| 70 | + |
| 71 | + // Create a RobotHardware object to be used to access robot hardware. |
| 72 | + // Prefix any hardware functions with "robot." to access this class. |
| 73 | + RobotHardware robot = new RobotHardware(this); |
| 74 | + |
| 75 | + @Override |
| 76 | + public void runOpMode() { |
| 77 | + double drive = 0; |
| 78 | + double turn = 0; |
| 79 | + double arm = 0; |
| 80 | + double handOffset = 0; |
| 81 | + |
| 82 | + // initialize all the hardware, using the hardware class. See how clean and simple this is? |
| 83 | + robot.init(); |
| 84 | + |
| 85 | + // Send telemetry message to signify robot waiting; |
| 86 | + // Wait for the game to start (driver presses PLAY) |
| 87 | + waitForStart(); |
| 88 | + |
| 89 | + // run until the end of the match (driver presses STOP) |
| 90 | + while (opModeIsActive()) { |
| 91 | + |
| 92 | + // Run wheels in POV mode (note: The joystick goes negative when pushed forward, so negate it) |
| 93 | + // In this mode the Left stick moves the robot fwd and back, the Right stick turns left and right. |
| 94 | + // This way it's also easy to just drive straight, or just turn. |
| 95 | + drive = -gamepad1.left_stick_y; |
| 96 | + turn = gamepad1.right_stick_x; |
| 97 | + |
| 98 | + // Combine drive and turn for blended motion. Use RobotHardware class |
| 99 | + robot.driveRobot(drive, turn); |
| 100 | + |
| 101 | + // Use gamepad left & right Bumpers to open and close the claw |
| 102 | + // Use the SERVO constants defined in RobotHardware class. |
| 103 | + // Each time around the loop, the servos will move by a small amount. |
| 104 | + // Limit the total offset to half of the full travel range |
| 105 | + if (gamepad1.right_bumper) |
| 106 | + handOffset += robot.HAND_SPEED; |
| 107 | + else if (gamepad1.left_bumper) |
| 108 | + handOffset -= robot.HAND_SPEED; |
| 109 | + handOffset = Range.clip(handOffset, -0.5, 0.5); |
| 110 | + |
| 111 | + // Move both servos to new position. Use RobotHardware class |
| 112 | + robot.setHandPositions(handOffset); |
| 113 | + |
| 114 | + // Use gamepad buttons to move arm up (Y) and down (A) |
| 115 | + // Use the MOTOR constants defined in RobotHardware class. |
| 116 | + if (gamepad1.y) |
| 117 | + arm = robot.ARM_UP_POWER; |
| 118 | + else if (gamepad1.a) |
| 119 | + arm = robot.ARM_DOWN_POWER; |
| 120 | + else |
| 121 | + arm = 0; |
| 122 | + |
| 123 | + robot.setArmPower(arm); |
| 124 | + |
| 125 | + // Send telemetry messages to explain controls and show robot status |
| 126 | + telemetry.addData("Drive", "Left Stick"); |
| 127 | + telemetry.addData("Turn", "Right Stick"); |
| 128 | + telemetry.addData("Arm Up/Down", "Y & A Buttons"); |
| 129 | + telemetry.addData("Hand Open/Closed", "Left and Right Bumpers"); |
| 130 | + telemetry.addData("-", "-------"); |
| 131 | + |
| 132 | + telemetry.addData("Drive Power", "%.2f", drive); |
| 133 | + telemetry.addData("Turn Power", "%.2f", turn); |
| 134 | + telemetry.addData("Arm Power", "%.2f", arm); |
| 135 | + telemetry.addData("Hand Position", "Offset = %.2f", handOffset); |
| 136 | + telemetry.update(); |
| 137 | + |
| 138 | + // Pace this loop so hands move at a reasonable speed. |
| 139 | + sleep(50); |
| 140 | + } |
| 141 | + } |
| 142 | +} |
0 commit comments