Repository navigation
Expand file tree
/
Copy pathHighPriorityRunnerSigma2016.java
More file actions
136 lines (105 loc) · 4.85 KB
/
Copy pathHighPriorityRunnerSigma2016.java
File metadata and controls
136 lines (105 loc) · 4.85 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
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
package org.firstinspires.ftc.teamcode;
import com.qualcomm.robotcore.util.Range;
import static org.firstinspires.ftc.teamcode.HardwareSigma2016.POWER_ADJ_STEP;
import static org.firstinspires.ftc.teamcode.HardwareSigma2016.PUSHER_STOP;
public class HighPriorityRunnerSigma2016 extends Thread {
// class variables
HardwareSigma2016 robot = null;
boolean bLineDetection = false;
public boolean go = false;
long lastOpModeLiveCheckTime = 0;
// speed calculation variables
int lastLeftEncoderPosition = 0;
int lastRightEncoderPosition = 0;
double lastAngle = 0.0;
// methods
HighPriorityRunnerSigma2016(HardwareSigma2016 ourHardware) {
robot = ourHardware;
this.setPriority(this.MAX_PRIORITY);
this.go = true;
}
public int GetSpeed(long timeDelta) {
int leftEncoderPosition = 0;
int rightEncoderPosition = 0;
double lastSpeed = 0.0;
double angle = 0;
double leftSpeed, rightSpeed;
leftEncoderPosition = robot.LeftMotor.getCurrentPosition();
rightEncoderPosition = robot.RightMotor.getCurrentPosition();
leftSpeed = Math.abs(leftEncoderPosition - lastLeftEncoderPosition) / (double) timeDelta;
rightSpeed = Math.abs(rightEncoderPosition - lastRightEncoderPosition) / (double) timeDelta;
// calculate current speed -- unit inch per second
robot.currentSpeed = Math.max(leftSpeed, rightSpeed) * 1000.0 / robot.COUNTS_PER_INCH; // inch per second
// System.out.println(leftEncoderPosition + "-" + lastLeftEncoderPosition + "=" + leftSpeed);
// System.out.println(rightEncoderPosition + "-" + lastRightEncoderPosition + "=" + rightSpeed);
// System.out.println("timeDelta=" + timeDelta);
lastLeftEncoderPosition = leftEncoderPosition;
lastRightEncoderPosition = rightEncoderPosition;
angle = robot.gyro.getIntegratedZValue();
robot.currentAngleSpeed = Math.abs(angle - lastAngle) * 1000 / timeDelta; // degree per second
lastAngle = angle;
return (0);
}
public void run() {
int lightlevelR, lightlevelG, lightlevelB;
long curTime, timeInterval;
long previousRunTime = 0, lastSpeedCheckTime = 0;
int lightlevel = 0;
// from Hitecnic color sensor web page
// The Color Number calculated by the sensor is refreshed approximately 100 times per second.
int expectedRunInterval = 10; // millisecond
while (this.go) {
// check if task runs on time
curTime = System.currentTimeMillis();
timeInterval = curTime - previousRunTime;
if (timeInterval > expectedRunInterval * 2) {
System.out.println("Sigma2016 -- high priority runner did not run for " + timeInterval + "ms");
}
// Get current speed information
timeInterval = curTime - lastSpeedCheckTime;
if (timeInterval > robot.SPEED_CHECK_INTERVAL) {
GetSpeed(timeInterval);
lastSpeedCheckTime = curTime;
}
previousRunTime = curTime;
// line detection
if (bLineDetection) {
lightlevelB = robot.lineLightSensor.blue();
lightlevelR = robot.lineLightSensor.red();
lightlevelG = robot.lineLightSensor.green();
lightlevel = lightlevelR + lightlevelG + lightlevelB;
if (lightlevel > robot.maxLineBrightnessReading)
{
robot.maxLineBrightnessReading = lightlevel;
System.out.println("Ground Brightness:: " + robot.groundbrightnessAVG
+ " Light Level:: " + robot.maxLineBrightnessReading);
}
}
if (robot.pusherRetractionStartTime > 0) {
if (curTime - robot.pusherRetractionStartTime > 1600) {
robot.pusherR.setPosition(robot.PUSHER_STOP);
robot.pusherL.setPosition(robot.PUSHER_STOP);
robot.pusherRetractionStartTime = 0;
}
}
// check if OP mode is still running. If OP mode is stopped, stop the high priority runner.
if (robot.opModeLiveCheck == 1)
{
robot.opModeLiveCheck = 0;
lastOpModeLiveCheckTime = curTime;
}
else
{
if (curTime - lastOpModeLiveCheckTime > 10000) // 10 seconds
{
go = false;
}
}
try {
sleep(expectedRunInterval);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}