首页 > 解决方案 > 我团队的机器人会随机旋转一段时间,我们不知道为什么

问题描述

我们正在使用现代机器人陀螺仪。我们用java编程。我们使用 FTC 为 First Tech Challenge 提供的基本代码。这是我们的陀螺仪代码以及转动代码,包括应该停止机器人的代码。

gyro = hardwareMap.get(ModernRoboticsI2cGyro.class, "gyro");

ModernRoboticsI2cGyro 陀螺仪;

私有静态最终双 GYRO_TOLERANCE = 4.00; // 度数 private static final long GYRO_DELAY = 10; //小姐

public void turn(double angle, double power)
{

    Hw.frontLeftDrive.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
    Hw.frontRightDrive.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
    Hw.backLeftDrive.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
    Hw.backRightDrive.setMode(DcMotor.RunMode.RUN_USING_ENCODER);

    // Sets angle above 0 and below 360 to prevent errors when plugging values in.
    angle = angle % 360;
    if(angle == 0 || power == 0) // if someone plugs in 0 for power or the angle, it just stops the statement and continues
        return;
    power = Math.abs(power);
    if(power > 0.5) power = 0.5; // and this sets power to half power if anyone plugs in a higher value than half

    if(Math.abs(angle) > 180.0) power = -power; //sets power sign (+ or -) to sign of angle

    //gets current angle and adds desired change to it
    float currentAngle = Hw.gyro.getHeading();
    float targetAngle = (float) (currentAngle + angle);


    // give robot some boundaries when checking gyro to prevent infinite checking of accuracy
    double tolerance = GYRO_TOLERANCE * Math.abs(power);
    long compensatedDelay = (long)(GYRO_DELAY / Math.abs(power));

    //make sure angle is over 0 or under 361
    if (targetAngle > 360)
        targetAngle -= 360;
    else if (targetAngle < 0)
        targetAngle += 360;

    double maxLimit = targetAngle + tolerance;
    double minLimit = targetAngle - tolerance;

    //set power states of motors
    Hw.frontLeftDrive.setPower(power);
    Hw.frontRightDrive.setPower(-power);
    Hw.backLeftDrive.setPower(power);
    Hw.backRightDrive.setPower(-power);

    // repeatedly check position for best accuracy until it falls into the set boundaries
    double current = Hw.gyro.getHeading();
    **while(current > maxLimit || current < minLimit){
        sleep(compensatedDelay);
        current = Hw.gyro.getHeading();
    }**
    //stops motors
    Hw.frontLeftDrive.setPower(0);
    Hw.frontRightDrive.setPower(0);
    Hw.backLeftDrive.setPower(0);
    Hw.backRightDrive.setPower(0);
}

标签: javamethodsrobotics

解决方案


推荐阅读