我团队的机器人会随机旋转一段时间,我们不知道为什么

时间:2019-03-13 21:46:12

标签: java methods robotics

我们正在使用现代机器人陀螺仪。 我们用java编程。 我们正在使用FTC为首次技术挑战赛提供的基本代码。 这是我们的陀螺代码以及转弯代码,包括应该停止机器人的代码。

gyro = hardwareMap.get(ModernRoboticsI2cGyro.class,“ gyro”);

ModernRoboticsI2cGyro陀螺仪;

私有静态最终双精度GYRO_TOLERANCE = 4.00; //度   私有静态最终long GYRO_DELAY = 10; // ms

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);
}

0 个答案:

没有答案