-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPController.java
More file actions
53 lines (44 loc) · 1.55 KB
/
PController.java
File metadata and controls
53 lines (44 loc) · 1.55 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
package wallFollower;
import lejos.hardware.motor.EV3LargeRegulatedMotor;
public class PController implements UltrasonicController {
private final int bandCenter, bandwidth;
private final int motorStraight = 200, FILTER_OUT = 20;
private EV3LargeRegulatedMotor leftMotor, rightMotor;
private int distance;
private int filterControl;
public PController(EV3LargeRegulatedMotor leftMotor, EV3LargeRegulatedMotor rightMotor,
int bandCenter, int bandwidth) {
//Default Constructor
this.bandCenter = bandCenter;
this.bandwidth = bandwidth;
this.leftMotor = leftMotor;
this.rightMotor = rightMotor;
leftMotor.setSpeed(motorStraight); // Initalize motor rolling forward
rightMotor.setSpeed(motorStraight);
leftMotor.forward();
rightMotor.forward();
filterControl = 0;
}
@Override
public void processUSData(int distance) {
// rudimentary filter - toss out invalid samples corresponding to null signal.
// (n.b. this was not included in the Bang-bang controller, but easily could have).
//
if (distance == 255 && filterControl < FILTER_OUT) {
// bad value, do not set the distance var, however do increment the filter value
filterControl ++;
} else if (distance == 255){
// true 255, therefore set distance to 255
this.distance = distance;
} else {
// distance went below 255, therefore reset everything.
filterControl = 0;
this.distance = distance;
}
// TODO: process a movement based on the us distance passed in (P style)
}
@Override
public int readUSDistance() {
return this.distance;
}
}