This repository has been archived by the owner on Nov 7, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathREV.java
165 lines (158 loc) · 6.3 KB
/
REV.java
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package org.firstinspires.ftc.teamcode;
import com.qualcomm.hardware.bosch.BNO055IMU;
import com.qualcomm.robotcore.eventloop.opmode.Autonomous;
import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode;
import com.qualcomm.robotcore.hardware.DcMotor;
import com.qualcomm.robotcore.hardware.DcMotorSimple;
import com.qualcomm.robotcore.util.ElapsedTime;
import org.firstinspires.ftc.robotcore.external.navigation.AngleUnit;
import org.firstinspires.ftc.robotcore.external.navigation.AxesOrder;
import org.firstinspires.ftc.robotcore.external.navigation.AxesReference;
@Autonomous(name = "REV (Blocks to Java)")
public class REV extends LinearOpMode {
private DcMotor front_left;
private DcMotor front_right;
private DcMotor back_left;
private DcMotor back_right;
private BNO055IMU imu;
/**
* This function is executed when this Op Mode is selected from the Driver Station.
*/
@Override
public void runOpMode() {
BNO055IMU.Parameters IMU_Parameters;
ElapsedTime ElapsedTime2;
double front_left_power;
double front_right_power;
double back_left_power;
double back_right_power;
float Yaw_Angle;
DcMotor motorFrontLeft = hardwareMap.dcMotor.get("front_left");
DcMotor motorBackLeft = hardwareMap.dcMotor.get("back_left");
DcMotor motorFrontRight = hardwareMap.dcMotor.get("front_right");
DcMotor motorBackRight = hardwareMap.dcMotor.get("back_right");
imu = hardwareMap.get(BNO055IMU.class, "imu");
// This op mode uses the REV Hub's built-in gyro to
// to allow a robot to move straight forward and then
// make a right turn.
// The op mode assume you have
// (1) Connected two motors to the expansion
// hub.
// (2) Created a config file that
// (a) names the motors "left-motor" and
// "right-motor"
// (b) configures the imu on I2C bus 0 port 0
// as a REV Expansion Hub IMU
// with the name "imu".
// Setup so motors will brake the wheels
// when motor power is set to zero.
motorFrontLeft.setZeroPowerBehavior(DcMotor.ZeroPowerBehavior.BRAKE);
motorFrontRight.setZeroPowerBehavior(DcMotor.ZeroPowerBehavior.BRAKE);
motorBackLeft.setZeroPowerBehavior(DcMotor.ZeroPowerBehavior.BRAKE);
motorBackRight.setZeroPowerBehavior(DcMotor.ZeroPowerBehavior.BRAKE);
// Reverse direction of one motor so robot moves
// forward rather than spinning in place.
motorFrontLeft.setDirection(DcMotor.Direction.REVERSE);
motorBackLeft.setDirection(DcMotor.Direction.REVERSE);
// Create an IMU parameters object.
IMU_Parameters = new BNO055IMU.Parameters();
// Set the IMU sensor mode to IMU. This mode uses
// the IMU gyroscope and accelerometer to
// calculate the relative orientation of hub and
// therefore the robot.
IMU_Parameters.mode = BNO055IMU.SensorMode.IMU;
// Intialize the IMU using parameters object.
imu.initialize(IMU_Parameters);
// Report the initialization to the Driver Station.
telemetry.addData("Status", "IMU initialized, calibration started.");
telemetry.update();
// Wait one second to ensure the IMU is ready.
sleep(1000);
// Loop until IMU has been calibrated.
while (!IMU_Calibrated()) {
telemetry.addData("If calibration ", "doesn't complete after 3 seconds, move through 90 degree pitch, roll and yaw motions until calibration complete ");
telemetry.update();
// Wait one second before checking calibration
// status again.
sleep(1000);
}
// Report calibration complete to Driver Station.
telemetry.addData("Status", "Calibration Complete");
telemetry.addData("Action needed:", "Please press the start triangle");
telemetry.update();
// Wait for Start to be pressed on Driver Station.
waitForStart();
// Create a timer object with millisecond
// resolution and save in ElapsedTime variable.
ElapsedTime2 = new ElapsedTime(ElapsedTime.Resolution.MILLISECONDS);
// Initialize motor power variables to 30%.
// Left_Power = 0.3;
// Right_Power = 0.3;
// Set motor powers to the variable values.
// leftmotor.setPower(Left_Power);
// rightmotor.setPower(Right_Power);
// Move robot forward for 2 seconds or until stop
// is pressed on Driver Station.
while (!(ElapsedTime2.milliseconds() >= 2000 || isStopRequested())) {
// Save gyro's yaw angle
Yaw_Angle = imu.getAngularOrientation(AxesReference.INTRINSIC, AxesOrder.XYZ, AngleUnit.DEGREES).thirdAngle;
// Report yaw orientation to Driver Station.
telemetry.addData("Yaw angle", Yaw_Angle);
// If the robot is moving straight ahead the
// yaw value will be close to zero. If it's not, we
// need to adjust the motor powers to adjust heading.
// If robot yaws right or left by 5 or more,
// adjust motor power variables to compensation.
if (Yaw_Angle < -5) {
// Turn left
// Left_Power = 0.25;
// Right_Power = 0.35;
} else if (Yaw_Angle > 5) {
// Turn right.
// Left_Power = 0.35;
// Right_Power = 0.25;
} else {
// Continue straight
// Left_Power = 0.3;
// Right_Power = 0.3;
}
// Report the new power levels to the Driver Station.
// telemetry.addData("Left Motor Power", Left_Power);
// telemetry.addData("Right Motor Power", Right_Power);
// Update the motors to the new power levels.
// leftmotor.setPower(Left_Power);
// rightmotor.setPower(Right_Power);
telemetry.update();
// Wait 1/5 second before checking again.
sleep(200);
}
// Now let's execute a right turn using power
// levels that will cause a turn in place.
// leftmotor.setPower(0.2);
// rightmotor.setPower(-0.2);
// Continue until robot yaws right by 90 degrees
// or stop is pressed on Driver Station.
while (!(Yaw_Angle <= -90 || isStopRequested())) {
// Update Yaw-Angle variable with current yaw.
Yaw_Angle = imu.getAngularOrientation(AxesReference.INTRINSIC, AxesOrder.XYZ, AngleUnit.DEGREES).thirdAngle;
// Report yaw orientation to Driver Station.
telemetry.addData("Yaw value", Yaw_Angle);
telemetry.update();
}
// We're done. Turn off motors
// leftmotor.setPower(0);
// rightmotor.setPower(0);
// Pause so final telemetry is displayed.
sleep(1000);
}
/**
* Function that becomes true when gyro is calibrated and
* reports calibration status to Driver Station in the meantime.
*/
private boolean IMU_Calibrated() {
telemetry.addData("IMU Calibration Status", imu.getCalibrationStatus());
telemetry.addData("Gyro Calibrated", imu.isGyroCalibrated() ? "True" : "False");
telemetry.addData("System Status", imu.getSystemStatus().toString());
return imu.isGyroCalibrated();
}
}