Sample Code - Gyro Sensor

Gyro Sensor
The Gyro Sensor can provide angular velocity or angle measurements, allowing you to incorporate features like precise turns, maintaining heading, or implementing gyro-assisted navigation.

Here's a brief example of using a Gyro Sensor in an FTC OpMode:
In this example, the BNO055IMU class is used to interface with the Gyro Sensor. The initializeHardware() method initializes the sensor and waits for it to calibrate. The getHeading() method retrieves the robot's heading in degrees.

You can use the heading value to implement various features such as precise turns, maintaining a specific heading, or even gyro-assisted navigation. Adjust the code inside the loop to define actions based on the heading value.

Remember to configure the hardware map with the appropriate name for your Gyro Sensor and ensure that you have the necessary dependencies and imports set up for the Gyro Sensor functionality.

Note that the specific implementation of the Gyro Sensor may vary depending on the manufacturer or model of the sensor you are using. Refer to the documentation provided by the sensor manufacturer for any additional setup or usage instructions specific to your Gyro Sensor.

--- code ---

import com.qualcomm.hardware.bosch.BNO055IMU;
import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode;
import com.qualcomm.robotcore.hardware.HardwareMap;
import com.qualcomm.robotcore.util.ElapsedTime;
import org.firstinspires.ftc.robotcore.external.navigation.AngleUnit;
import org.firstinspires.ftc.robotcore.external.navigation.AxesOrder;
import org.firstinspires.ftc.robotcore.external.navigation.AxesReference;
import org.firstinspires.ftc.robotcore.external.navigation.Orientation;

public class GyroSensorExample extends LinearOpMode {
    private BNO055IMU imu;
    private Orientation angles;

    @Override
    public void runOpMode() throws InterruptedException {
        initializeHardware();

        waitForStart();

        while (opModeIsActive()) {
            double heading = getHeading();

            // Perform actions based on the robot's heading

            telemetry.addData("Heading (degrees)", heading);
            telemetry.update();

            idle();
        }
    }

    private void initializeHardware() {
        HardwareMap hardwareMap = hardwareMap;

        BNO055IMU.Parameters parameters = new BNO055IMU.Parameters();
        parameters.angleUnit = BNO055IMU.AngleUnit.DEGREES;
        parameters.accelUnit = BNO055IMU.AccelUnit.METERS_PERSEC_PERSEC;

        imu = hardwareMap.get(BNO055IMU.class, "gyro_sensor");
        imu.initialize(parameters);

        ElapsedTime timer = new ElapsedTime();
        timer.reset();

        while (!imu.isGyroCalibrated() && timer.seconds() < 3) {
            sleep(50);
        }

        angles = imu.getAngularOrientation(AxesReference.INTRINSIC, AxesOrder.ZYX, AngleUnit.DEGREES);
    }

    private double getHeading() {
        angles = imu.getAngularOrientation(AxesReference.INTRINSIC, AxesOrder.ZYX, AngleUnit.DEGREES);
        return AngleUnit.normalizeDegrees(angles.firstAngle);
    }
}




© Copyright CircuitBreakers - All Rights Reserved

Made with ‌

WYSIWYG HTML Editor