-
Notifications
You must be signed in to change notification settings - Fork 0
/
my_measurement.sh
441 lines (355 loc) · 10.3 KB
/
my_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
440
441
#!/bin/bash
#VARIABLE TO CONFIGURE
HOME="/home/pi"
source "$HOME/catkin_ws/devel/setup.bash"
LOGFILE="$HOME/measurement.log"
DATADIR="$HOME/data"
ROSTOPICFILE="$HOME/rostmp.log"
LEDLOG="$HOME/led.log"
INSRECORDING="/tmp/insrecording"
# 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
#========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=1
INSERR=1
online=1
logger "START DATALOGGER"
rm $INSRECORDING
if [ ! -p "$INSRECORDING" ]; then
touch $INSRECORDING
fi
function led_on {
if [ "${LED[$1]}" -eq 3 ] && [ LEDGPIO[] -ne 0];then
sudo kill -USR1 ${LEDPID[$1]}
fi
if [ "${LED[$1]}" -eq 2 ] || [ "${LED[$1]}" - eq 0 ] || [ "${LED[$1]}}" -eq 4] && [ "${LED[$1]}" -ne 0 ]; then
echo "OFF" | sudo tee /tmp/ledpip"$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 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 controller
for 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 4];then
sudo python $HOME/gaitmaven/led-linuxgpio.sh $i ${LEDGPIO[$i]} >> $LEDLOG 2>1 &
fi
fi
SUDOPID[$i]=$!
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
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
check3G
# enough timeout for 3g
nc -w 30 -z 8.8.8.8 53 &> /dev/null
online=$?
if [ -f "online"]; then
online="$(cat online)"
fi
# ONLINE MODE
if [ "$online" -eq 0 ] then
# TIME IS NOT CORRECTED
if [ "$TIMECORRECTED" -ne 0 ]; then
sudo date +%Y%m%d -s "20230101"
logger "TIME TO THE PAST..."
fi
# TODO: Test this comment
if [[ "$TIMECORRECTED" -ne 0 ]] && sudo ntpdate -b time1.mike.fi time2.mikes.fi; then
TIMECORRECTED=0
logger "TIME CORRECTED"
led_blink_f 0
(sleep 5 && led_off 0) &
fi
# Stop IMU, GPS and recording
quitScreens
if [ "$STOPPED" -ne 0 ]; then
echo "OFF" > $INSRECORDING
#Uploading the data
logger "Waiting for everything to shut down"
COUNTER=0
while screen -list | grep "ins\|imu\|mtw\|pozyx\|gps\|bag\|log"; do
sleep 2
((COUNTER++))
if [ "$COUNTER" -gt 10 ]; then
quitScreens
COUNTER=0
fi
done
STOPPED=0
fi
if [ "$UPLOADED" -ne 0 ]; then
if [ "$STARTUPLOADED" -ne 0 ] && ! ps -p $GRIVEID > /dev/null 2>&1; then
STARTUPLOADED=0
logger "UPLOADING THE DATA..."
led_blink 1
DATE="$(date)"
(cd $DATADIR || exit; bash autoupdate.sh "${DATE}") >> $LOGFILE &
logger "CALLED AUTOUPDATE BASH FILE"
GRIVEID=$!
elif ps -p $GRIVEID > /dev/null 2>&1; then
led_blink 1
sleep 2
else
UPLOADED=0
STARTUPLOADED=1
led_on 1
logger "DONE UPLOADING"
fi
fi
else
# 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 [ "$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
# 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
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
if ! (screen -list | grep -q "log") && [[ $IMUERR -eq 0 ]] && [[ $INS -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
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
fi
# Sleeping a while
sleep 2
# USBFOLDER="/media/pi/USB_DISK"
# if [ -e $USBFOLDER ]; then
# led_blink_f 0
# ( sleep 5 && led_off 0)&
# quitScreens
# # STOP MEASURING FO
# if [ "$STOPPED" -ne 0 ]; then
# logger "STOP MEASURING FOR UPLOADING DATA"
# echo "OFF" > $INSRECORDING
# # UPLOADING THE DATA
# logger "Waitting for everything to shutdown"
# COUNTER=0
# while screen -list | grep "ins\|bag\|log"; do
# sleep 2
# ((COUNTER++))
# if [ "$COUNTER" -gt 10 ]; then
# quitScreens
# COUNTER=0
# fi
# done
# STOPPED=0
# fi
# # NOT YET UPLOAD (1), NOW START UPLOADING
# if [ "$UPLOADED" -ne 0 ]; then
# if [ "$STARTUPLOADED" -ne 0 ] && ! ps -p $GRIVEID > /dev/null 2>&1; then
# STARTUPLOADED=0
# logger "UPLOADING DATA ..."
# led_blink 1
# DATE="($date)"
# # CALL UPLOAD SCRIPT HERE
# # TODO: Check this file
# bash autoupdate.sh
# fi
# fi
done