Skip to content

Commit ef2c040

Browse files
authored
Merge pull request #102 from RobotGirls/issue-99-DistanceSensorTask
Issue 99 distance sensor task
2 parents ddf9477 + e09bb2a commit ef2c040

File tree

1 file changed

+210
-0
lines changed

1 file changed

+210
-0
lines changed
Lines changed: 210 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
1+
2+
/*
3+
* Copyright (c) September 2017 FTC Teams 25/5218
4+
*
5+
* All rights reserved.
6+
*
7+
* Redistribution and use in source and binary forms, with or without modification,
8+
* are permitted (subject to the limitations in the disclaimer below) provided that
9+
* the following conditions are met:
10+
*
11+
* Redistributions of source code must retain the above copyright notice, this list
12+
* of conditions and the following disclaimer.
13+
*
14+
* Redistributions in binary form must reproduce the above copyright notice, this
15+
* list of conditions and the following disclaimer in the documentation and/or
16+
* other materials provided with the distribution.
17+
*
18+
* Neither the name of FTC Teams 25/5218 nor the names of their contributors may be used to
19+
* endorse or promote products derived from this software without specific prior
20+
* written permission.
21+
*
22+
* NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS
23+
* LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24+
* AS IS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
25+
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESSFOR A PARTICULAR PURPOSE
26+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
27+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
29+
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30+
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
31+
* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32+
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33+
*/
34+
35+
package team25core;
36+
37+
import com.qualcomm.robotcore.hardware.DcMotor;
38+
import com.qualcomm.robotcore.hardware.DistanceSensor;
39+
import org.firstinspires.ftc.robotcore.external.Telemetry;
40+
import org.firstinspires.ftc.robotcore.external.navigation.DistanceUnit;
41+
42+
public class DistanceSensorTask extends RobotTask {
43+
44+
45+
private DistanceSensor rightSensor;
46+
private DistanceSensor leftSensor;
47+
48+
double rightDistance;
49+
double leftDistance;
50+
double minDistance;
51+
double maxDistance;
52+
int numLoops;
53+
54+
boolean continuousFlag;
55+
int pollingThresh;
56+
57+
private Telemetry.Item rightDistanceTlm;
58+
private Telemetry.Item leftDistanceTlm;
59+
private Telemetry.Item rightCountTlm;
60+
private Telemetry.Item leftCountTlm;
61+
private Telemetry.Item notFoundCountTlm;
62+
63+
64+
// Constructor.
65+
public DistanceSensorTask(Robot robot, DistanceSensor myRightSensor,
66+
DistanceSensor myLeftSensor, Telemetry telemetry,
67+
double myMinDistance, double myMaxDistance, int numLoops,
68+
int pollingThresh, boolean continuousFlag)
69+
{
70+
super(robot);
71+
72+
this.rightSensor = myRightSensor;
73+
this.leftSensor = myLeftSensor;
74+
this.minDistance = myMinDistance;
75+
this.maxDistance = myMaxDistance;
76+
77+
this.continuousFlag = continuousFlag;
78+
79+
this.numLoops = numLoops;
80+
this.pollingThresh = pollingThresh;
81+
82+
this.leftDistanceTlm = telemetry.addData("leftDistance", "none");
83+
this.rightDistanceTlm = telemetry.addData("rightDistance", "none");
84+
this.rightCountTlm = telemetry.addData("rightCount", "none");
85+
this.leftCountTlm = telemetry.addData("leftCount", "none");
86+
this.notFoundCountTlm = telemetry.addData("notFoundCount", "none");
87+
88+
}
89+
90+
91+
// Class: events.
92+
public class DistanceSensorEvent extends RobotEvent {
93+
public EventKind kind;
94+
public double distance;
95+
96+
97+
public DistanceSensorEvent(RobotTask task, EventKind k, double myDistance)
98+
{
99+
super(task);
100+
kind = k;
101+
distance = myDistance;
102+
103+
}
104+
}
105+
106+
// Enumeration: events.
107+
public enum EventKind {
108+
LEFT_DISTANCE,
109+
RIGHT_DISTANCE,
110+
UNKNOWN,
111+
}
112+
113+
@Override
114+
public void start()
115+
{
116+
/*
117+
* TODO: Implement with new hardware.
118+
*/
119+
}
120+
121+
@Override
122+
public void stop()
123+
{
124+
}
125+
126+
public void setMinMax(double minDistance, double maxDistance)
127+
{
128+
this.minDistance = minDistance;
129+
this.maxDistance = maxDistance;
130+
}
131+
132+
// this function will poll the data from the sensors and will determine whether the object is
133+
// on the left or right
134+
private boolean pollingAndVoting()
135+
{
136+
int leftCount = 0;
137+
int rightCount = 0;
138+
int notFound = 0;
139+
140+
for(int i=0; i< numLoops ; i++){
141+
leftDistance = leftSensor.getDistance(DistanceUnit.INCH);
142+
rightDistance = rightSensor.getDistance(DistanceUnit.INCH);
143+
rightDistanceTlm.setValue(rightDistance);
144+
leftDistanceTlm.setValue(leftDistance);
145+
146+
147+
if (leftDistance < maxDistance && leftDistance > minDistance){
148+
leftCount++;
149+
}
150+
else if (rightDistance < maxDistance && rightDistance > minDistance){
151+
rightCount++;
152+
}
153+
else {
154+
notFound++;
155+
}
156+
}
157+
leftCountTlm.setValue(leftCount);
158+
rightCountTlm.setValue(rightCount);
159+
notFoundCountTlm.setValue(notFound);
160+
// FIXME later bound the distance so you return a distance relevant to where the prop is
161+
if (leftCount > rightCount && leftCount > pollingThresh){
162+
robot.queueEvent(new DistanceSensorEvent(this, EventKind.LEFT_DISTANCE,
163+
leftDistance));
164+
}
165+
else if (rightCount > leftCount && rightCount > pollingThresh){
166+
robot.queueEvent(new DistanceSensorEvent(this, EventKind.RIGHT_DISTANCE,
167+
rightDistance));
168+
}
169+
else {
170+
robot.queueEvent(new DistanceSensorEvent(this, EventKind.UNKNOWN,
171+
leftDistance));
172+
}
173+
return true;
174+
}
175+
176+
private boolean continuousRead()
177+
{
178+
leftDistance = leftSensor.getDistance(DistanceUnit.INCH);
179+
rightDistance = rightSensor.getDistance(DistanceUnit.INCH);
180+
rightDistanceTlm.setValue(rightDistance);
181+
leftDistanceTlm.setValue(leftDistance);
182+
183+
// FIXME later bound the distance so you return a distance relevant to where the prop is
184+
if (leftDistance < maxDistance && leftDistance > minDistance){
185+
robot.queueEvent(new DistanceSensorEvent(this, EventKind.LEFT_DISTANCE,
186+
leftDistance));
187+
}
188+
if (rightDistance < maxDistance && rightDistance > minDistance){
189+
robot.queueEvent(new DistanceSensorEvent(this, EventKind.RIGHT_DISTANCE,
190+
rightDistance));
191+
}
192+
193+
// This task doesn't stop.
194+
return false;
195+
}
196+
197+
@Override
198+
public boolean timeslice() {
199+
if (continuousFlag)
200+
{
201+
return continuousRead();
202+
}
203+
else
204+
{
205+
return pollingAndVoting();
206+
}
207+
// This task doesn't stop.
208+
//return false;
209+
}
210+
}

0 commit comments

Comments
 (0)