java - 我团队的机器人会随机旋转一段时间,我们不知道为什么
问题描述
我们正在使用现代机器人陀螺仪。我们用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);
}
解决方案
推荐阅读
- sql-server - 将数据从 Azure Databricks 加载到 Azure SQL DB 时出现错误,无法在对象中插入重复的键行
- javascript - 交换对象数组中的一些值,同时保持对其他对象的排序
- java - Hibernate IS-A 关系映射
- java - “th:text/each”不显示数据(Spring Boot/Thymeleaf)
- javascript - Datatables/JQuery/Javascript - 研究摘要和同一 TD 中的“打开”按钮,将“打开”一词添加到导出的导出中
- c# - 使用文件上的 cer 或 pfx 从 Microsoft Graph 端点获取身份验证令牌
- node.js - aws 负载均衡器给请求 5 秒一次
- caching - 新闻文章的 Redis 缓存
- deprecated - 不推荐使用 express-session req.secret;提供秘密选项 app.js:27:9
- pandas - 大熊猫数据框的内存大小与 xlsx 保存的大小