-
Notifications
You must be signed in to change notification settings - Fork 0
/
final_maze.nxc
143 lines (117 loc) · 2.16 KB
/
final_maze.nxc
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#define SENSOR_1 Sensor(S1)
#define setpoint 22
float right,straight;
float t1,time;
/* function for scanning sensor values */
float scanning()
{
RotateMotor(OUT_A,20,90);
Wait(100);
right = SensorUS(IN_4);
return right;
}
float scanning1()
{
RotateMotor(OUT_A,20,-180);
Wait(100);
straight = SensorUS(IN_4);
return straight;
}
task main()
{
SetSensorLowspeed(IN_4);
SetSensorType (IN_1,IN_TYPE_LIGHT_ACTIVE);
SetSensorLight(IN_1,IN_MODE_PCTFULLSCALE);
ResetSensor (IN_1);
float error;
float kp=1;
float ki=0.02;
float kd=0.1;
float turn;
float offset = 48;
float Tp = 30;
float integral = 0;
float lasterror = 0;
float derivative = 0;
int count;
while(true)
{
while (SensorUS(IN_4)>=21)
{
int count =0;
label:
error = Sensor(S1)-offset;
integral = integral+error;
derivative = error-lasterror;
turn = (kp*error)+(ki*integral)+(kd*derivative);
/*turn = turn/100; */
OnFwd(OUT_B,Tp+turn);
OnFwd(OUT_C,Tp-turn);
lasterror = error;
}
Off(OUT_BC);
scanning();
scanning1();
RotateMotor(OUT_A,20,90);
while (right >=26 && straight>=26)
{
OnFwd(OUT_B,30);
Wait(300);
OnFwd(OUT_B,30);
until(Sensor(S1)<=40);
OnFwd(OUT_BC,30);
Wait(300);
Off(OUT_BC);
goto label;
}
while(right <=26 && straight>=26)
{
OnFwd(OUT_B,30);
Wait(300);
OnFwd(OUT_B,30);
until(Sensor(S1)<=40);
OnFwd(OUT_BC,30);
Wait(300);
Off(OUT_BC);
goto label;
}
while(right <=26 && straight<=26)
{
OnFwdSync(OUT_BC,30,90);
Wait(1000);
OnFwdSync(OUT_BC,30,90);
until(Sensor(S1)<=40);
Off(OUT_BC);
goto label;
}
while(right >=26 && straight<=26)
{
OnFwd(OUT_C,30);
Wait(600);
OnFwd(OUT_C,35);
until(Sensor(S1)<=40);
OnFwd(OUT_BC,30);
Wait(300);
Off(OUT_BC);
goto label;
}
}
}
/*void tilt()
{
if (Sensor(S1) >=40)
{
t1 = CurrentTick();
OnRev (OUT_C , 25);
OnFwd (OUT_B , 25);
Wait(600);
time = (CurrentTick() - t1);
if (SENSOR_1 >52 && time > 600);
{
OnRev (OUT_B , 30);
OnFwd (OUT_C , 30);
until (SENSOR_1 <= 40);
Off (OUT_BC);
}
}
} */