-
Notifications
You must be signed in to change notification settings - Fork 0
/
short_measurement.sh
439 lines (351 loc) · 10 KB
/
short_measurement.sh
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
#!/bin/bash
#VARIABLE TO CONFIGURE
HOME="/home/pi"
source "$HOME/catkin_ws/devel/setup.bash"
GPSTEMPFILE="$HOME/gpstemp.log"
INSTEMPFILE="$HOME/instemp.log"
LOGFILE="$HOME/measurement.log"
DATADIR="$HOME/data"
ROSTOPICFILE="$HOME/rostmp.log"
ASCIILOG="$HOME/ascii.log"
LEDLOG="$HOME/led.log"
INSRECORDING="/tmp/insrecording"
BUZZLOG="$HOME/buzz.log"
BUZZRECORDING="/tmp/buzzrecording"
# LOGFILE="$HOME/logfile.log"
# GPS connected = 0, not = 1
GPS=1
# Vectornav is connected = 0, not = 1
INS=0
# LED mode connected = 0, not = 1, pwh = 2, rpigpio =3, inverser gpio = 4
LED[0]=4
#GPIO pin number
LEDGPIO[0]=24
LEDPID[0]=0
LED[1]=4
LEDGPIO[1]=25
LEDPID[1]=0
LED[2]=1
LEDGPIO[2]=10
LEDPID[2]=0
BUZZ[0]=4
BUZZGPIO[0]=21
BUZZPID[0]=0
#========END CONFIGURATION
function logger {
echo "[$(date)] $1" >> $LOGFILE
echo "[$(date)] $1"
}
function testCmd {
if ! which $1; then
logger "ERROR: Could not find $1!"
return 1
fi
return 0
}
if [ "$1" = check ]; then
testCmd rostopic
testCmd sudo
testCmd kill
testCmd echo
testCmd tee
testCmd python
testCmd bash
testCmd ps
testCmd bash
testCmd screen
testCmd lsusb
# TODO: Implement this
testCmd sakis3g
testCmd udevadm
testCmd ntpdate
testCmd grive
testCmd roslaunch
testCmd cat
testCmd rosrun
testCmd rosbag
testCmd readlink
exit 0
fi
GRIVEID=-1
STOPPED=0
UPLOAD=1
STATEUPLOAD=1
IMUERR=1
GPSERR=0
INSERR=0
online=1
logger "START DATALOGGER"
rm $INSRECORDING
if [ ! -p "$INSRECORDING" ]; then
touch $INSRECORDING
fi
function led_on {
if [ "${LED[$1]}" -eq 3 ] && [ "${LEDPID[$1]}" -ne 0 ]; then
sudo kill -USR1 ${LEDPID[$1]}
fi
if [ "${LED[$1]}" -eq 2 ] || [ "${LED[$1]}" -eq 0 ] || [ "${LED[$1]}" -eq 4 ] && [ "${LEDPID[$1]}" -ne 0 ]; then
echo "ON" | sudo tee /tmp/ledpipe"$1"
fi
}
function led_off {
if [ "${LED[$1]}" -eq 3 ] && [ "${LEDPID[$1]}" -ne 0 ]; then
sudo kill -USR2 ${LEDPID[$1]}
fi
if [ "${LED[$1]}" -eq 2 ] || [ "${LED[$1]}" -eq 0 ] || [ "${LED[$1]}" -eq 4 ] && [ "${LEDPID[$1]}" -ne 0 ]; then
echo "OFF" | sudo tee /tmp/ledpipe"$1"
fi
}
function led_blink {
if [ "${LED[$1]}" -eq 3 ] && [ "${LEDPID[$1]}" -ne 0 ]; then
for _ in {1..10}; do
led_on "$1"
sleep 0.4
led_off "$1"
sleep 0.2
done
fi
if [ "${LED[$1]}" -eq 2 ] || [ "${LED[$1]}" -eq 0 ] || [ "${LED[$1]}" -eq 4 ] && [ "${LEDPID[$1]}" -ne 0 ]; then
echo "BLINK" | sudo tee /tmp/ledpipe"$1"
fi
}
function buzz_on {
if [ "${BUZZ[$1]}" -eq 4 ] && [ "${BUZZPID[$1]}" -ne 0 ]; then
echo "ON" | sudo tee /tmp/buzzpipe"$1"
fi
}
function buzz_off {
if [ "${BUZZ[$1]}" -eq 4 ] && [ "${BUZZPID[$1]}" -ne 0 ]; then
echo "OFF" | sudo tee /tmp/buzzpipe"$1"
fi
}
function buzz_pulse {
if [ "${BUZZ[$1]}" -eq 4 ]; then
echo "PULSE" | sudo tee /tmp/buzzpipe"$1"
fi
}
function led_blink_f {
if [ "${LED[$1]}" -eq 3 ] && [ "${LEDPID[$1]}" -ne 0 ]; then
for _ in {1..30}; do
led_on "$1"
sleep 0.1
led_off "$1"
sleep 0.1
done
fi
if [ "${LED[$1]}" -eq 2 ] || [ "${LED[$1]}" -eq 0 ] || [ "${LED[$1]}" -eq 4 ] && [ "${LEDPID[$1]}" -ne 0 ]; then
echo "FASTER" | sudo tee /tmp/ledpipe"$1"
fi
}
# start led controllers
# for i in {0..2}; do
# if [ "${LED[$i]}" -eq 3 ] || [ "${LED[$i]}" -eq 2 ] || [ "${LED[$i]}" -eq 0 ] || [ "${LED[$i]}" -eq 4 ]; then
# if [ "${LED[$i]}" -eq 3 ]; then
# sudo python $HOME/openkin/led-pin.py ${LEDGPIO[$i]} >> $LEDLOG 2>&1 &
# fi
# if [ "${LED[$i]}" -eq 2 ]; then
# sudo bash $HOME/openkin/led-linuxpwm.sh $i ${LEDGPIO[$i]} >> $LEDLOG 2>&1 &
# fi
# if [ "${LED[$i]}" -eq 0 ]; then
# sudo bash $HOME/openkin/led-linuxgpio.sh $i ${LEDGPIO[$i]} >> $LEDLOG 2>&1 &
# fi
# if [ "${LED[$i]}" -eq 4 ]; then
# sudo bash $HOME/openkin/led-linuxgpio.sh $i ${LEDGPIO[$i]} 1 >> $LEDLOG 2>&1 &
# fi
# SUDOPID[$i]=$!
# echo $!
# sleep 1
# LEDPID[$i]=$(ps --ppid ${SUDOPID[$i]} -o pid=)
# echo "${LEDPID[$i]}"
# logger "Led $i connected"
# sleep 2
# led_on $i
# sleep 1
# led_off $i
# fi
# done
function quitScreens {
led_off 0
led_off 2
if screen -list | grep -q "log"; then
logger "Shutting down logger"
# Send Ctrl+C signal to terminal session named log
screen -S log -X stuff $'\003'
sleep 3
screen -S log -X quit
fi
if screen -list | grep -q "bag"; then
logger "Shutting down bag"
screen -S bag -X stuff $'\003'
sleep 3
screen -S bag -X quit
fi
if screen -list | grep -q "ins"; then
logger "Shutting down the INS"
screen -S ins -X stuff $'\003'
sleep 4
screen -S ins -X quit
fi
# Reset error
INSERR=1
}
function check3G {
logger "CHECKING 3G ..."
# No online
if [ "$online" -ne 0 ]; then
# Change number according to dongle
if lsusb | grep -q 12d1:1003; then
logger "ATTEMPT CONNECTING TO 3G"
# TODO: Check command this beforehand
sudo sakis3g connect OTHER="USBMODEM" USBMODEM="12d1:1003" APN="internet.saunalahti"
fi
fi
}
#======== START MEASURMENT
logger "STARTING MEASURING LOOP ..."
#while true; do
for ((i=0;i<2;i++))
do
# check3G
logger "======== ITERATION $i ============"
# enough timeout for 3g
nc -w 30 -z 8.8.8.8 53 &> /dev/null
# OFFLINE MDOE
# led_off 1
UPLOADED=1
GRIVEID=0
STARTEDUPLOAD=1
if [ "$GPSERR" -gt 10 ]; then
logger "GPS-errors more than 10, restarting"
GPSERR=1
quitScreens
fi
# UPDATED:
if [ "$GPS" -eq 0 ]; then
#NOTE! The gps is a launch file which will take care of starting the ros core
#If this is not used, separate core process must be launched
if ! screen -list | grep -q "gps"; then
logger "Offline: Starting GPS and ROSCORE"
screen -dmS gps
screen -r gps -X stuff $'\nsudo -E bash\nrm '$GPSTEMPFILE$'\nnice -n -10 roslaunch ublox_gps ublox_gps.launch 2> '$GPSTEMPFILE$'\n'
# should have time to error, if going to
sleep 15
# if file exists and not empty
if [ -s $GPSTEMPFILE ]; then
logger "Error on starting gps"
logger "$(cat $GPSTEMPFILE)"
screen -S gps -X quit
led_off 0
led_off 2
# start over
continue
elif [ ! -f $GPSTEMPFILE ]; then
logger "GPSlog not found"
screen -S gps -X quit
led_off 0
led_off 2
# start over
continue
fi
fi
elif [ "$INS" -ne 0 ]; then
if ! screen -list | grep -q "gps"; then
logger "Offline: Starting ROSCORE"
screen -dmS gps
screen -r gps -X stuff $'\nroscore\n'
fi
fi
logger "Screen list before launch"
logger "$(screen -list)"
logger "INS before launch? $INS"
# IF ROSCORE IS RUNNING
if [ "$INS" -eq 0 ] || screen -list | grep -q "gps"; then
if [ "$IMUERR" -gt 10 ]; then
logger "IMU-errors more than 10, restarting IMUs"
IMUERR=1
led_off 0
led_off 2
screen -S imu -X quit
fi
if [ "$INSERR" -gt 10 ]; then
logger "INS-errors more than 10, restarting vectornav"
INSERR=1
led_off 0
led_off 2
screen -S ins -X quit
fi
# INS connected
echo "INS connected? $INS"
if [ "$INS" -eq 0 ]; then
if ! screen -list | grep -q "ins"; then
logger "OFFLINE: Starting INS"
screen -dmS ins
screen -r ins -X stuff $'\n sudo -E bash\nrm '$INSTEMPFILE$'\nnice -n -10 roslaunch vectornav vn200.launch 2> '$INSTEMPFILE$'\n'
sleep 15
fi
# If INS file exists and not empty
if [ -s $INSTEMPFILE ]; then
logger "Error on starting ins"
logger "$(cat $INSTEMPFILE)"
screen -S ins -X quit
led_off 0
led_off 2
led_blink_f 1
(sleep 2 && led_off 1)
# Start over
continue
elif [ ! -f $INSTEMPFILE ]; then
logger "INSlog not found"
screen -S ins -X quit
led_off 0
led_off 2
led_blink_f 1
(sleep 2 && led_off 1)
# start over
continue
fi
fi
logger "Screen list after launch"
logger "$(screen -list)"
logger "INS after launch? $INS"
logger "Check before launching logger"
logger "IMUERR $IMUERR, INSERR $INSERR"
if ! (screen -list | grep -q "log") && [[ $INSERR -eq 0 ]]; then
logger "OFFLINE: Starting logger"
screen -dmS log
screen -r log -X stuff $'\nrosrun ascii_logger listener.py > '$ASCIILOG$'\n'
#led_blink 2
#led_blink 0
echo "ON" > $INSRECORDING
fi
else
logger "GPS/roscore wasn't running!"
fi
STOPPED=1
# Check topics we want
rostopic list > $ROSTOPICFILE 2>&1
# Print rostopic file
logger "cat ROSTOPICFILE"
logger "$(cat $ROSTOPICFILE )"
logger "[INSERR] check for topic $INSERR"
logger "[GPSERR] check for topic $GPSERR"
logger "[INS] $INS"
logger "[GPS] $GPS"
if [ "$INS" -eq 0 ] && ! (grep -q "^/vectornav/imugps$" $ROSTOPICFILE); then
logger "/vectornav/imugps not found"
((INSERR++))
else
INSERR=0
fi
if [ "$GPS" -eq 0 ] && ! (grep -q "^/gps/navpvtwh$" $ROSTOPICFILE && grep -q "^/gps/fix$" $ROSTOPICFILE); then
logger "/.gps/navpvtwh or /gps/fix not found on rostopic"
((GPSERR++))
elif [ "$GPS" -eq 1 ] && grep -q "^/rosout" $ROSTOPICFILE; then
logger "/rosout not found"
((GPSERR++))
else
GPSERR=0
fi
# Sleeping a while
sleep 2
done