-
Notifications
You must be signed in to change notification settings - Fork 0
/
LineFollower.java
65 lines (46 loc) · 1.15 KB
/
LineFollower.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
import lejos.nxt.*;
public class LineFollower {
/**
*
* @author Octavio Arriaga and Ali Varfan
*
* The LineFollower class solve the second Exercise
*
*/
public static void main(String[] args) {
lineFollower();
}
/* this method use PID controller for following a black line*/
public static void lineFollower() {
float kp=15;
float ki=0;
float kd=0;
float offset=45;
float tp=25;
float integral=0;
float lastError=0;
float derivative=0;
float error = 0;
float turn = 0;
/* defining the robot parts*/
Motor.A.setSpeed(10);
Motor.C.setSpeed(10);
LightSensor ls1 = new LightSensor(SensorPort.S1);
Motor.A.forward();
Motor.B.forward();
/* PID controller */
while (true)
{
float lightValue = (float)ls1.readValue();
error = lightValue - offset;
integral = integral + error;
derivative = error - lastError;
turn = (kp*error)+(ki*integral)+(kd*derivative);
Motor.A.setSpeed(tp + turn);
Motor.B.setSpeed(tp - turn);
Motor.A.forward();
Motor.B.forward();
lastError = error;
}
}
}