-
Notifications
You must be signed in to change notification settings - Fork 0
/
smr_code_live.txt
executable file
·261 lines (202 loc) · 6.33 KB
/
smr_code_live.txt
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
%--------------------------------------------------------------------
% Main part
%--------------------------------------------------------------------
eval $batteryvoltage
log "$odox" "$odoy"
laser "scanpush cmd='zoneobst'"
goto "measureBoxDist"
label "returnMeasureBoxDist"
goto "moveBoxObstacle"
label "returnMoveBoxObstacle"
goto "gateObstacle"
label "returnGateObstacle"
goto "wallObstacle"
label "returnWallObstacle"
goto "whiteLineObstacle"
label "returnWhiteLineObstacle"
goto "goalObstacle"
label "returnGoalObstacle"
% End the program
goto "stop"
%--------------------------------------------------------------------
% Measure distance to box
%--------------------------------------------------------------------
label "measureBoxDist"
% Follow the first right branch to the crossing black line before the box.
followline "bm" @v 0.3 :($drivendist>0.3)
ignoreobstacles
followline "br" @v 0.3 :($crossingblackline == 1)
stop
wait 1
eval $l4+1.935
% Initialize variables for computations
%sumFrontSensors=0
%sumFrontLeft = 0
%sumFrontMiddle = 0
%sumFrontRight = 0
%counter = 0
%maxObservations = 50 % Maximum number of observation to compute the mean from
%distanceSensorToOdoCenter = 0.235 % Should be checked
%numberOfSensors = 3
%yDistanceToMark = 1.815
% Begin the measurement loop
label "measurementLoop"
% Calculate sums
%sumFrontLeft = sumFrontLeft + $irdistfrontleft
%sumFrontMiddle = sumFrontMiddle + $irdistfrontmiddle
%sumFrontRight = sumFrontRight + $irdistfrontright
%sumFrontSensors = sumFrontLeft + sumFrontMiddle + sumFrontRight
%counter = counter + 1
% Wait 0.01 seconds because the sensor has a frequency of 100 Hz
%wait 0.01
%if( counter < maxObservations) "measurementLoop" % Check if the maximum number if iterations is reached.
%averageMeasuredDistance = sumFrontSensors / ( numberOfSensors * maxObservations )
%distanceToBox = -$odoy + distanceSensorToOdoCenter + averageMeasuredDistance
% TODO: No correct in sim, but ought to be better with real world measurement:
% distanceToBox = yDistanceToMark + distanceSensorToOdoCenter + averageMeasuredDistance
% Print the result to the terminal
%eval distanceToBox
goto "returnMeasureBoxDist"
%--------------------------------------------------------------------
% Move box obstacle
%--------------------------------------------------------------------
label "moveBoxObstacle"
% Drive the SMR to the obstacle
turn 90
drive :($blacklinefound == 1)
drive :($drivendist > 0.1)
drive :($blacklinefound == 1) %Drive up to the second black line
drive :($drivendist > 0.1)
turn -90
ignoreobstacles
followline "bm" :($drivendist > 0.1)
ignoreobstacles
followline "bm" :($crossingblackline == 1)
ignoreobstacles
drive :($cmdtime > 0.5)
% Back out from the obstacle
ignoreobstacles
fwd -0.2
ignoreobstacles
followline "bm" @v -0.5 :($drivendist > 0.8)
% Drive to the other side of the obstacle
turn -90 @v 0.3
ignoreobstacles
drive :($blacklinefound == 1)
ignoreobstacles
drive :($drivendist > 0.1)
ignoreobstacles
turn 90
followline "bm" :($crossingblackline == 1)
drive :($drivendist > 0.1)
turn 90
% Drive thorugh the obstacle
followline "bm" @v 0.2 :($crossingblackline == 1)
followline "bm" :($drivendist > 0.1)
followline "br" :($crossingblackline == 1)
% Return to the main part
goto "returnMoveBoxObstacle"
%--------------------------------------------------------------------
% Drive through gate
%--------------------------------------------------------------------
label "gateObstacle"
% Follow the black line until the first gate pole is detected
followline "bm" @v 0.1 :($l0<0.6 & $l0 > 0.01)
% Drive through the gate
followline "bm" @v 0.3 :($drivendist > 0.65)
turn 90
ignoreobstacles
fwd 0.5
ignoreobstacles
% Drive close to the wall so there is enough space to turn the SMR
drive @v 0.2 :($irdistfrontleft < 0.35 | $irdistfrontmiddle < 0.35 | $irdistfrontright < 0.35)
turn -90 @v 0.3
followwall "l" 0.3 :($irdistleft > 0.5)
drive :($blacklinefound == 1)
fwd 0.25
turn -90
followline "bm" :($drivendist > 0.2)
turn 180
goto "returnGateObstacle"
%--------------------------------------------------------------------
% Drive around wall
%--------------------------------------------------------------------
label "wallObstacle"
% Drive to the wall
followline "bm" :($crossingblackline == 1)
fwd 0.6
turn 90
fwd 0.1
ignoreobstacles
followwall "l" 0.25 :($irdistleft > 0.5)
% Move a little, turn and drive thorugh the gate
fwd 0.35
turn 90
fwd 0.8
turn 90
fwd 0.3
% Follow the wall on the other side until nothing is detected to the left side
followwall "l" 0.3 :($irdistleft > 0.5)
% Drive to the balck line and orient the SMR correctly so that it is ready for the
% next white line obstacle
drive :($blacklinefound == 1)
fwd 0.27
turn -90
followline "bm" :($drivendist > 0.3)
turn 180
followline "bm" :($drivendist > 1)
goto "returnWallObstacle"
%--------------------------------------------------------------------
% Follow the white line
%--------------------------------------------------------------------
label "whiteLineObstacle"
% Drive to the white line
followline "bm" :($crossingblackline == 1)
fwd 0.45
turn 90
ignoreobstacles
followline "wm" :($drivendist > 1)
ignoreobstacles
followline "wm" :($crossingblackline == 1)
fwd 0.1
drive :($crossingblackline == 1)
fwd 0.2
turn -90
goto "returnWhiteLineObstacle"
%--------------------------------------------------------------------
% Drive to goal obstacle
%--------------------------------------------------------------------
label "goalObstacle"
%
% TODO: Move the gate before entering the goal!!!
%
ignoreobstacles
followline "bm" @v 0.1 :($irdistfrontmiddle < 0.15)
ignoreobstacles
turn 90 @v 0.3
ignoreobstacles
fwd 0.60
ignoreobstacles
turn -90
fwd 0.25
%Smack the gate
ignoreobstacles
turn -180 @v 0.5
ignoreobstacles
fwd 0.25 @v 0.3
ignoreobstacles
turn 90
ignoreobstacles
drive @v 0.2 :($blacklinefound == 1)
ignoreobstacles
fwd 0.3
ignoreobstacles
turn 90
ignoreobstacles
followline "bm" :($irdistfrontmiddle < 0.2)
goto "returnGoalObstacle"
%--------------------------------------------------------------------
% Program end
%--------------------------------------------------------------------
label "stop"
stop