-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathSampleTeleop.java
More file actions
105 lines (88 loc) · 4.8 KB
/
SampleTeleop.java
File metadata and controls
105 lines (88 loc) · 4.8 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
102
103
104
105
/* Created by Phil Malone. 2023.
This class illustrates my simplified Odometry Strategy.
It implements basic straight line motions but with heading and drift controls to limit drift.
See the readme for a link to a video tutorial explaining the operation and limitations of the code.
*/
package org.firstinspires.ftc.teamcode;
import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode;
import com.qualcomm.robotcore.eventloop.opmode.TeleOp;
import com.qualcomm.robotcore.util.ElapsedTime;
/*
* This OpMode illustrates a teleop OpMode for an Omni robot.
* An external "Robot" class is used to manage all motor/sensor interfaces, and to assist driving functions.
* The IMU gyro is used to stabilize the heading when the operator is not requesting a turn.
*/
@TeleOp(name="Sample Teleop", group = "Mr. Phil")
public class SampleTeleop extends LinearOpMode
{
final double SAFE_DRIVE_SPEED = 0.8 ; // Adjust this to your robot and your driver. Slower usually means more accuracy. Max value = 1.0
final double SAFE_STRAFE_SPEED = 0.8 ; // Adjust this to your robot and your driver. Slower usually means more accuracy. Max value = 1.0
final double SAFE_YAW_SPEED = 0.5 ; // Adjust this to your robot and your driver. Slower usually means more accuracy. Max value = 1.0
final double HEADING_HOLD_TIME = 10.0 ; // How long to hold heading once all driver input stops. (This Avoids effects of Gyro Drift)
// local parameters
ElapsedTime stopTime = new ElapsedTime(); // Use for timeouts.
boolean autoHeading = false; // used to indicate when heading should be locked.
// get an instance of the "Robot" class.
SimplifiedOdometryRobot robot = new SimplifiedOdometryRobot(this);
@Override public void runOpMode()
{
// Initialize the drive hardware & Turn on telemetry
robot.initialize(true);
// Wait for driver to press start
while(opModeInInit()) {
telemetry.addData(">", "Touch Play to drive");
// Read and display sensor data
robot.readSensors();
telemetry.update();
};
while (opModeIsActive())
{
robot.readSensors();
// Allow the driver to reset the gyro by pressing both small gamepad buttons
if(gamepad1.options && gamepad1.share){
robot.resetHeading();
robot.resetOdometry();
}
// read joystick values and scale according to limits set at top of this file
double drive = -gamepad1.left_stick_y * SAFE_DRIVE_SPEED; // Fwd/back on left stick
double strafe = -gamepad1.left_stick_x * SAFE_STRAFE_SPEED; // Left/Right on left stick
double yaw = -gamepad1.right_stick_x * SAFE_YAW_SPEED; // Rotate on right stick
// OR... For special conditions, Use the DPAD to make pure orthoginal motions
if (gamepad1.dpad_left) {
strafe = SAFE_DRIVE_SPEED / 2.0;
} else if (gamepad1.dpad_right) {
strafe = -SAFE_DRIVE_SPEED / 2.0;
} else if (gamepad1.dpad_up) {
drive = SAFE_DRIVE_SPEED / 2.0;
} else if (gamepad1.dpad_down) {
drive = -SAFE_STRAFE_SPEED / 2.0;
}
// This is where we heep the robot heading locked so it doesn't turn while driving or strafing in a straight line.
// Is the driver turning the robot, or should it hold its heading?
if (Math.abs(yaw) > 0.05) {
// driver is commanding robot to turn, so turn off auto heading.
autoHeading = false;
} else {
// If we are not already locked, wait for robot to stop rotating (<2 deg per second) and then lock-in the current heading.
if (!autoHeading && Math.abs(robot.getTurnRate()) < 2.0) {
robot.yawController.reset(robot.getHeading()); // Lock in the current heading
autoHeading = true;
}
}
// If auto heading is on, override manual yaw with the value generated by the heading controller.
if (autoHeading) {
yaw = robot.yawController.getOutput(robot.getHeading());
}
// Drive the wheels based on the desired axis motions
robot.moveRobot(drive, strafe, yaw);
// If the robot has just been sitting here for a while, make heading setpoint track any gyro drift to prevent rotating.
if ((drive == 0) && (strafe == 0) && (yaw == 0)) {
if (stopTime.time() > HEADING_HOLD_TIME) {
robot.yawController.reset(robot.getHeading()); // just keep tracking the current heading
}
} else {
stopTime.reset();
}
}
}
}