forked from Elsie4ever/DpmTeam2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Avoidance.java
111 lines (99 loc) · 3.12 KB
/
Avoidance.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
package trotty02;
import lejos.hardware.motor.EV3LargeRegulatedMotor;
/**
*
* @author Shi Yu
*
*/
public class Avoidance extends Thread{
private boolean avoid = false;
private UltrasonicPoller usPollerF;
private UltrasonicPoller usPollerS;
private int bandCenter, track;
private static final int EMERGENCY = 5;
private static final int motorStraight = 150;
private static final int motorTurn = 100;
private EV3LargeRegulatedMotor leftMotor, rightMotor;
private static final int P = 7; //proportion constant
private int horizontal = bandCenter;
private int vertical = bandCenter;
private double prevHeading;
/**
* Constructor for the avoidance class
* @param usPoller Ultrasonic Poller
* @param leftMotor
* @param rightMotor
* @param bandCenter distance between the robot and the wall
* @param track width of the robot
*/
public Avoidance(UltrasonicPoller usPollerF, UltrasonicPoller usPollerS, EV3LargeRegulatedMotor leftMotor, EV3LargeRegulatedMotor rightMotor,
int bandCenter, int track){
this.usPollerF = usPollerF;
this.usPollerS = usPollerS;
//Default Constructor
this.bandCenter = bandCenter;
this.track = track;
this.leftMotor = leftMotor;
this.rightMotor = rightMotor;
}
public void run(){
while(true){
while(avoid){
processUSData(usPollerF.getDistance(), usPollerS.getDistance());
}
}
}
/**
* Tells the code whether to run the avoidance loop inside the tread or not
* @param avoid {boolean} true to run avoidance loop, false to exit the loop
*/
public void setAvoid(boolean avoid){
this.avoid = avoid;
}
/**
* Return whether or not the avoidance loop is being run
* @return {boolean} true if running the avoidance loop, false if not
*/
public boolean getAvoid(){
return this.avoid;
}
/**
* Processes the distance from both the front and the side sensors to allow
* wheel movements - avoid the block.
* @param distFront distance seen by the front sensor
* @param distSide distance seen by the side sensor
*/
public void processUSData(double distFront, double distSide){
//TODO: Movement based on information from the front & the side
horizontal = (int) distSide;
vertical = (int) distFront;
if(horizontal < vertical){
if(horizontal < bandCenter){ // turn CCW
leftMotor.setSpeed(motorStraight);
rightMotor.setSpeed(motorStraight + P*Math.abs(horizontal - bandCenter));
leftMotor.forward();
rightMotor.forward();
}
else{ // turn CW
leftMotor.setSpeed(motorStraight + P*Math.abs(horizontal - bandCenter));
rightMotor.setSpeed(motorStraight);
leftMotor.forward();
rightMotor.forward();
}
}
else{
if(vertical < bandCenter){ // turn CCW
leftMotor.setSpeed(motorStraight);
rightMotor.setSpeed(motorStraight + P*Math.abs(vertical - bandCenter) + motorTurn);
leftMotor.forward();
rightMotor.forward();
}
else{ // turn CW
leftMotor.setSpeed(motorStraight + motorTurn);
rightMotor.setSpeed(motorStraight);
leftMotor.forward();
rightMotor.forward();
}
}
}
}