forked from FIRST-Tech-Challenge/FtcRobotController
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTeleOpAlt.java
More file actions
101 lines (91 loc) · 5.17 KB
/
TeleOpAlt.java
File metadata and controls
101 lines (91 loc) · 5.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package org.firstinspires.ftc.teamcode;
import com.acmerobotics.dashboard.FtcDashboard;
import com.acmerobotics.dashboard.config.Config;
import com.acmerobotics.dashboard.telemetry.MultipleTelemetry;
import com.arcrobotics.ftclib.controller.PIDController;
import com.arcrobotics.ftclib.drivebase.MecanumDrive;
import com.arcrobotics.ftclib.gamepad.GamepadEx;
import com.arcrobotics.ftclib.hardware.motors.Motor;
import com.qualcomm.hardware.rev.RevHubOrientationOnRobot;
import com.qualcomm.robotcore.eventloop.opmode.Disabled;
import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode;
import com.qualcomm.robotcore.eventloop.opmode.TeleOp;
import com.qualcomm.robotcore.hardware.DcMotor;
import com.qualcomm.robotcore.hardware.DcMotorSimple;
import com.qualcomm.robotcore.hardware.IMU;
import org.firstinspires.ftc.robotcore.external.Telemetry;
import org.firstinspires.ftc.robotcore.external.navigation.AngleUnit;
@TeleOp
@Config
@Disabled
public class TeleOpAlt extends LinearOpMode {
public static double kP = 0.05;
public static double kD = 0;
public static double kI = 0;
public static double mult = 10;
@Override
public void runOpMode() throws InterruptedException {
// Declare our motors
// Make sure your ID's match your configuration
DcMotor frontLeftMotor = hardwareMap.dcMotor.get("frontLeft");
DcMotor backLeftMotor = hardwareMap.dcMotor.get("backLeft");
DcMotor frontRightMotor = hardwareMap.dcMotor.get("frontRight");
DcMotor backRightMotor = hardwareMap.dcMotor.get("backRight");
// Reverse the right side motors. This may be wrong for your setup.
// If your robot moves backwards when commanded to go forwards,
// reverse the left side instead.
// See the note about this earlier on this page.
frontRightMotor.setDirection(DcMotorSimple.Direction.REVERSE);
backRightMotor.setDirection(DcMotorSimple.Direction.REVERSE);
RevHubOrientationOnRobot.LogoFacingDirection logoDirection = RevHubOrientationOnRobot.LogoFacingDirection.UP;
RevHubOrientationOnRobot.UsbFacingDirection usbDirection = RevHubOrientationOnRobot.UsbFacingDirection.FORWARD;
frontLeftMotor.setZeroPowerBehavior(DcMotor.ZeroPowerBehavior.FLOAT);
frontRightMotor.setZeroPowerBehavior(DcMotor.ZeroPowerBehavior.FLOAT);
backLeftMotor.setZeroPowerBehavior(DcMotor.ZeroPowerBehavior.FLOAT);
backRightMotor.setZeroPowerBehavior(DcMotor.ZeroPowerBehavior.FLOAT);
RevHubOrientationOnRobot orientationOnRobot = new RevHubOrientationOnRobot(logoDirection, usbDirection);
IMU imu = (IMU) hardwareMap.get(IMU.class, "imu");
imu.initialize(new IMU.Parameters(orientationOnRobot));
double target=180;
PIDController pid = new PIDController(kP, kI, kD);
frontRightMotor.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
backRightMotor.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
frontLeftMotor.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
backLeftMotor.setMode(DcMotor.RunMode.RUN_USING_ENCODER);
waitForStart();
boolean lastturnstopped = false;
if (isStopRequested()) return;
FtcDashboard dashboard = FtcDashboard.getInstance();
telemetry = new MultipleTelemetry(telemetry, FtcDashboard.getInstance().getTelemetry());
while (opModeIsActive()) {
pid.setPID(kP, kI, kD);
double y = -gamepad1.left_stick_y; // Remember, Y stick value is reversed
double x = gamepad1.left_stick_x * 1.1; // Counteract imperfect strafing
double rx = -pid.calculate((imu.getRobotYawPitchRollAngles().getYaw(AngleUnit.DEGREES)+180) % 360, target);
if (gamepad1.right_stick_x == 0 && (x == 0) && (y == 0) && !gamepad1.right_bumper) {
target = (imu.getRobotYawPitchRollAngles().getYaw(AngleUnit.DEGREES) +180) % 360+ gamepad1.right_stick_x*mult;
lastturnstopped=true;
} else if (gamepad1.right_stick_x != 0) {
rx = gamepad1.right_stick_x;
target = (imu.getRobotYawPitchRollAngles().getYaw(AngleUnit.DEGREES) +180) % 360+ gamepad1.right_stick_x*mult;
lastturnstopped=false;
}
target = target % 360;
// Denominator is the largest motor power (absolute value) or 1
// This ensures all the powers maintain the same ratio,
// but only if at least one is out of the range [-1, 1]
double denominator = Math.max(Math.abs(y) + Math.abs(x) + Math.abs(rx), 1);
double frontLeftPower = (y + x + rx) / denominator;
double backLeftPower = (y - x + rx) / denominator;
double frontRightPower = (y - x - rx) / denominator;
double backRightPower = (y + x - rx) / denominator;
frontLeftMotor.setPower(frontLeftPower);
backLeftMotor.setPower(backLeftPower);
frontRightMotor.setPower(frontRightPower);
backRightMotor.setPower(backRightPower);
telemetry.addData("Target", target);
telemetry.addData("Actual", (imu.getRobotYawPitchRollAngles().getYaw(AngleUnit.DEGREES)+180 % 360));
telemetry.update();
}
}
}