-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMovement.java
More file actions
44 lines (41 loc) · 1.26 KB
/
Movement.java
File metadata and controls
44 lines (41 loc) · 1.26 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
import rxtxrobot.*;
public class Movement {
private RXTXRobot r;
public Movement(RXTXRobot r) {
this.r = r;
}
public void align() {
int left = r.getPing(12);
int right = r.getPing(13);
int difference = left - right;
System.out.println("Difference: " + difference + " cm");
while(!(difference > -1 && difference < 1)) {
if(left < right) {
r.runEncodedMotor(RXTXRobot.MOTOR1, 100, 5, RXTXRobot.MOTOR2, -100, 5); //left forward
}
else {
r.runEncodedMotor(RXTXRobot.MOTOR1, -100, 5, RXTXRobot.MOTOR2, 100, 5); //right forward
}
left = r.getPing(12);
right = r.getPing(13);
difference = left - right;
System.out.println("Difference: " + difference + " cm");
if(difference == 2) {
r.runEncodedMotor(RXTXRobot.MOTOR1, -100, 5, RXTXRobot.MOTOR2, 100, 5);
}
if(difference == -2) {
r.runEncodedMotor(RXTXRobot.MOTOR1, 100, 5, RXTXRobot.MOTOR2, -100, 5);
}
}
System.out.println("Aligned!");
}
public void turn90right() {
r.runEncodedMotor(RXTXRobot.MOTOR1, 150, 95, RXTXRobot.MOTOR2, -170, 95);
}
public void turn90left() {
r.runEncodedMotor(RXTXRobot.MOTOR1, -150, 84, RXTXRobot.MOTOR2, 150, 84);
}
public void turn180() {
r.runEncodedMotor(RXTXRobot.MOTOR1, -200, 185, RXTXRobot.MOTOR2, 200, 185);
}
}