|
| 1 | +/* |
| 2 | +Copyright (c) 2022 REV Robotics, FIRST |
| 3 | +
|
| 4 | +All rights reserved. |
| 5 | +
|
| 6 | +Redistribution and use in source and binary forms, with or without modification, |
| 7 | +are permitted (subject to the limitations in the disclaimer below) provided that |
| 8 | +the following conditions are met: |
| 9 | +
|
| 10 | +Redistributions of source code must retain the above copyright notice, this list |
| 11 | +of conditions and the following disclaimer. |
| 12 | +
|
| 13 | +Redistributions in binary form must reproduce the above copyright notice, this |
| 14 | +list of conditions and the following disclaimer in the documentation and/or |
| 15 | +other materials provided with the distribution. |
| 16 | +
|
| 17 | +Neither the name of REV Robotics nor the names of its contributors may be used to |
| 18 | +endorse or promote products derived from this software without specific prior |
| 19 | +written permission. |
| 20 | +
|
| 21 | +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS |
| 22 | +LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 23 | +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 24 | +THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 25 | +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE |
| 26 | +FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 27 | +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 28 | +SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 29 | +CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR |
| 30 | +TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 31 | +THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 | +*/ |
| 33 | +package org.firstinspires.ftc.robotcontroller.external.samples; |
| 34 | + |
| 35 | +import com.qualcomm.hardware.rev.RevHubOrientationOnRobot; |
| 36 | +import com.qualcomm.robotcore.eventloop.opmode.Disabled; |
| 37 | +import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode; |
| 38 | +import com.qualcomm.robotcore.eventloop.opmode.TeleOp; |
| 39 | +import com.qualcomm.robotcore.hardware.IMU; |
| 40 | + |
| 41 | +import org.firstinspires.ftc.robotcore.external.navigation.AngleUnit; |
| 42 | +import org.firstinspires.ftc.robotcore.external.navigation.AngularVelocity; |
| 43 | +import org.firstinspires.ftc.robotcore.external.navigation.YawPitchRollAngles; |
| 44 | + |
| 45 | +/** |
| 46 | + * This file demonstrates the impact of setting the IMU orientation correctly or incorrectly. This |
| 47 | + * code assumes there is an IMU configured with the name "imu". |
| 48 | + * <p> |
| 49 | + * Note: This OpMode is more of a tool than a code sample. The User Interface portion of this code |
| 50 | + * goes beyond simply showing how to interface to the IMU.<br> |
| 51 | + * For a minimal example of interfacing to an IMU, please see the SensorIMUOrthogonal or SensorIMUNonOrthogonal sample OpModes. |
| 52 | + * <p> |
| 53 | + * This sample enables you to re-specify the Hub Mounting orientation dynamically by using gamepad controls. |
| 54 | + * While doing so, the sample will display how Pitch, Roll and Yaw angles change as the hub is moved. |
| 55 | + * <p> |
| 56 | + * The gamepad controls let you change the two parameters that specify how the Control/Expansion Hub is mounted. <br> |
| 57 | + * The first parameter specifies which direction the printed logo on the Hub is pointing. <br> |
| 58 | + * The second parameter specifies which direction the USB connector on the Hub is pointing. <br> |
| 59 | + * All directions are relative to the robot, and left/right is as viewed from behind the robot. |
| 60 | + * <p> |
| 61 | + * How will you know if you have chosen the correct Orientation? With the correct orientation |
| 62 | + * parameters selected, pitch/roll/yaw should act as follows: |
| 63 | + * <p> |
| 64 | + * Pitch value should INCREASE as the robot is tipped UP at the front. (Rotation about X) <br> |
| 65 | + * Roll value should INCREASE as the robot is tipped UP at the left side. (Rotation about Y) <br> |
| 66 | + * Yaw value should INCREASE as the robot is rotated Counter Clockwise. (Rotation about Z) <br> |
| 67 | + * <p> |
| 68 | + * The Yaw can be reset (to zero) by pressing the Y button on the gamepad (Triangle on a PS4 controller) |
| 69 | + * <p> |
| 70 | + * The rotational velocities should follow the change in corresponding axes. |
| 71 | + */ |
| 72 | + |
| 73 | +@TeleOp(name="Concept: IMU Orientation", group="Concept") |
| 74 | +@Disabled |
| 75 | +public class ConceptExploringIMUOrientation extends LinearOpMode { |
| 76 | + static RevHubOrientationOnRobot.LogoFacingDirection[] logoFacingDirections |
| 77 | + = RevHubOrientationOnRobot.LogoFacingDirection.values(); |
| 78 | + static RevHubOrientationOnRobot.UsbFacingDirection[] usbFacingDirections |
| 79 | + = RevHubOrientationOnRobot.UsbFacingDirection.values(); |
| 80 | + static int LAST_DIRECTION = logoFacingDirections.length - 1; |
| 81 | + static float TRIGGER_THRESHOLD = 0.2f; |
| 82 | + |
| 83 | + IMU imu; |
| 84 | + int logoFacingDirectionPosition; |
| 85 | + int usbFacingDirectionPosition; |
| 86 | + boolean orientationIsValid = true; |
| 87 | + |
| 88 | + @Override public void runOpMode() throws InterruptedException { |
| 89 | + imu = hardwareMap.get(IMU.class, "imu"); |
| 90 | + logoFacingDirectionPosition = 0; // Up |
| 91 | + usbFacingDirectionPosition = 2; // Forward |
| 92 | + |
| 93 | + updateOrientation(); |
| 94 | + |
| 95 | + boolean justChangedLogoDirection = false; |
| 96 | + boolean justChangedUsbDirection = false; |
| 97 | + |
| 98 | + // Loop until stop requested |
| 99 | + while (!isStopRequested()) { |
| 100 | + |
| 101 | + // Check to see if Yaw reset is requested (Y button) |
| 102 | + if (gamepad1.y) { |
| 103 | + telemetry.addData("Yaw", "Resetting\n"); |
| 104 | + imu.resetYaw(); |
| 105 | + } else { |
| 106 | + telemetry.addData("Yaw", "Press Y (triangle) on Gamepad to reset.\n"); |
| 107 | + } |
| 108 | + |
| 109 | + // Check to see if new Logo Direction is requested |
| 110 | + if (gamepad1.left_bumper || gamepad1.right_bumper) { |
| 111 | + if (!justChangedLogoDirection) { |
| 112 | + justChangedLogoDirection = true; |
| 113 | + if (gamepad1.left_bumper) { |
| 114 | + logoFacingDirectionPosition--; |
| 115 | + if (logoFacingDirectionPosition < 0) { |
| 116 | + logoFacingDirectionPosition = LAST_DIRECTION; |
| 117 | + } |
| 118 | + } else { |
| 119 | + logoFacingDirectionPosition++; |
| 120 | + if (logoFacingDirectionPosition > LAST_DIRECTION) { |
| 121 | + logoFacingDirectionPosition = 0; |
| 122 | + } |
| 123 | + } |
| 124 | + updateOrientation(); |
| 125 | + } |
| 126 | + } else { |
| 127 | + justChangedLogoDirection = false; |
| 128 | + } |
| 129 | + |
| 130 | + // Check to see if new USB Direction is requested |
| 131 | + if (gamepad1.left_trigger > TRIGGER_THRESHOLD || gamepad1.right_trigger > TRIGGER_THRESHOLD) { |
| 132 | + if (!justChangedUsbDirection) { |
| 133 | + justChangedUsbDirection = true; |
| 134 | + if (gamepad1.left_trigger > TRIGGER_THRESHOLD) { |
| 135 | + usbFacingDirectionPosition--; |
| 136 | + if (usbFacingDirectionPosition < 0) { |
| 137 | + usbFacingDirectionPosition = LAST_DIRECTION; |
| 138 | + } |
| 139 | + } else { |
| 140 | + usbFacingDirectionPosition++; |
| 141 | + if (usbFacingDirectionPosition > LAST_DIRECTION) { |
| 142 | + usbFacingDirectionPosition = 0; |
| 143 | + } |
| 144 | + } |
| 145 | + updateOrientation(); |
| 146 | + } |
| 147 | + } else { |
| 148 | + justChangedUsbDirection = false; |
| 149 | + } |
| 150 | + |
| 151 | + // Display User instructions and IMU data |
| 152 | + telemetry.addData("logo Direction (set with bumpers)", logoFacingDirections[logoFacingDirectionPosition]); |
| 153 | + telemetry.addData("usb Direction (set with triggers)", usbFacingDirections[usbFacingDirectionPosition] + "\n"); |
| 154 | + |
| 155 | + if (orientationIsValid) { |
| 156 | + YawPitchRollAngles orientation = imu.getRobotYawPitchRollAngles(); |
| 157 | + AngularVelocity angularVelocity = imu.getRobotAngularVelocity(AngleUnit.DEGREES); |
| 158 | + |
| 159 | + telemetry.addData("Yaw (Z)", "%.2f Deg. (Heading)", orientation.getYaw(AngleUnit.DEGREES)); |
| 160 | + telemetry.addData("Pitch (X)", "%.2f Deg.", orientation.getPitch(AngleUnit.DEGREES)); |
| 161 | + telemetry.addData("Roll (Y)", "%.2f Deg.\n", orientation.getRoll(AngleUnit.DEGREES)); |
| 162 | + telemetry.addData("Yaw (Z) velocity", "%.2f Deg/Sec", angularVelocity.zRotationRate); |
| 163 | + telemetry.addData("Pitch (X) velocity", "%.2f Deg/Sec", angularVelocity.xRotationRate); |
| 164 | + telemetry.addData("Roll (Y) velocity", "%.2f Deg/Sec", angularVelocity.yRotationRate); |
| 165 | + } else { |
| 166 | + telemetry.addData("Error", "Selected orientation on robot is invalid"); |
| 167 | + } |
| 168 | + |
| 169 | + telemetry.update(); |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + // apply any requested orientation changes. |
| 174 | + void updateOrientation() { |
| 175 | + RevHubOrientationOnRobot.LogoFacingDirection logo = logoFacingDirections[logoFacingDirectionPosition]; |
| 176 | + RevHubOrientationOnRobot.UsbFacingDirection usb = usbFacingDirections[usbFacingDirectionPosition]; |
| 177 | + try { |
| 178 | + RevHubOrientationOnRobot orientationOnRobot = new RevHubOrientationOnRobot(logo, usb); |
| 179 | + imu.initialize(new IMU.Parameters(orientationOnRobot)); |
| 180 | + orientationIsValid = true; |
| 181 | + } catch (IllegalArgumentException e) { |
| 182 | + orientationIsValid = false; |
| 183 | + } |
| 184 | + } |
| 185 | +} |
0 commit comments