-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathColorAuto.java0
144 lines (118 loc) · 4.67 KB
/
ColorAuto.java0
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
package org.firstinspires.ftc.teamcode;
import com.qualcomm.robotcore.eventloop.opmode.Autonomous;
import com.qualcomm.robotcore.hardware.DcMotor;
import com.qualcomm.robotcore.hardware.DistanceSensor;
import com.qualcomm.robotcore.util.Range;
import com.qualcomm.robotcore.eventloop.opmode.LinearOpMode;
import android.graphics.Color;
import com.qualcomm.robotcore.hardware.ColorSensor;
//import android.speech.tts.TextToSpeech;
@Autonomous(name = "TestAutonomous", group = "Sensor")
public class ColorAuto extends LinearOpMode {
private Testbot robot = new Testbot();
ColorSensor colorSense;
DcMotor frontLeft;
DcMotor frontRight;
DcMotor backLeft;
DcMotor backRight;
DistanceSensor distanceSense;
@Override
public void runOpMode() {
robot.init(hardwareMap);
//TextToSpeech.speak("initiating robot uprising", 0, null, "init");
telemetry.addData("Robot", "ready");
colorSense = hardwareMap.get(ColorSensor.class, "sensor_color_distance");
float hsv[] = {0F, 0F, 0F};
final float values[] = hsv;
int scale = 1;
boolean colorIsRed = false;
waitForStart();
Color.RGBToHSV((int) (colorSense.red() * scale),
(int) (colorSense.green() * scale),
(int) (colorSense.blue() * scale),
hsv);
if(hsv[0] < 100 || hsv[0] > 300) colorIsRed = true;
else colorIsRed = false;
float speed = (float) 0.6;
if(!colorIsRed) {
setAll(speed, -speed, speed, -speed);
wait(100);
setAll(0, 0, 0, 0);
wait(100);
setAll(-speed, speed, -speed, speed);
wait(100);
setAll(0, 0, 0, 0);
}
else {
setAll(-speed, speed, -speed, speed);
wait(100);
setAll(0, 0, 0, 0);
wait(100);
setAll(speed, -speed, speed, -speed);
wait(100);
setAll(0, 0, 0, 0);
}
wait(1000);
/*while (opModeIsActive()) {
Color.RGBToHSV((int) (colorSense.red() * scale),
(int) (colorSense.green() * scale),
(int) (colorSense.blue() * scale),
hsv);
if(hsv[0] < 100 || hsv[0] > 300) colorIsRed = true;
else colorIsRed = false;
telemetry.addData("Alpha", colorSense.alpha());
telemetry.addData("Red ", colorSense.red());
telemetry.addData("Green", colorSense.green());
telemetry.addData("Blue ", colorSense.blue());
telemetry.addData("Hue", hsv[0]);
telemetry.addData("colorIsRed", colorIsRed);
telemetry.update();
}*/
}
private double si(double joyVal, double divBy) {
/**
* This function scales the input.
* It makes it so that when you push the joystick forward, the robot doesn't respond in a linear way.
* When you push it a little bit, you have more precision control over speed.
* Pushing all the way overrides the math and jumps to 1.
*/
if(joyVal>=1) return 1;
else if(joyVal<=-1) return -1;
else return -Math.log(-joyVal + 1) / divBy;
}
private void timeDrive(float x, float y, int time) {
float fl = x + y;
float bl = x - y;
float fr = x - y;
float br = x + y;
setAll(fl, bl, fr, br);
wait(1000);
setAll(0, 0, 0, 0);
}
private void setAll(float fl, float fr, float bl, float br) {
robot.frontLeft.setPower(fl);
robot.frontRight.setPower(fr);
robot.backLeft.setPower(bl);
robot.backRight.setPower(br);
}
private void setAllS(float fl, float fr, float bl, float br) {
robot.frontLeft.setPower(Range.clip(si(fl, 1.5), -1, 1));
robot.frontRight.setPower(Range.clip(si(fr, 1.5), -1, 1));
robot.backLeft.setPower(Range.clip(si(bl, 1.5), -1, 1));
robot.backRight.setPower(Range.clip(si(br, 1.5), -1, 1));
}
private void wait(int time) {
try {
Thread.sleep(time);
}
catch(InterruptedException ex) {
Thread.currentThread().interrupt();
}
}
private void setBrakes() {
robot.frontLeft.setZeroPowerBehavior(DcMotor.ZeroPowerBehavior.BRAKE);
robot.frontRight.setZeroPowerBehavior(DcMotor.ZeroPowerBehavior.BRAKE);
robot.backLeft.setZeroPowerBehavior(DcMotor.ZeroPowerBehavior.BRAKE);
robot.backRight.setZeroPowerBehavior(DcMotor.ZeroPowerBehavior.BRAKE);
}
}