-
Notifications
You must be signed in to change notification settings - Fork 1
/
run_log
executable file
·328 lines (276 loc) · 8.15 KB
/
run_log
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
#!/bin/bash
################################################################################
##
## Script that logs the synchronization status in WR-LEN and check that
## the servo state is "TRACK_PHASE"
##
## Authors:
## - Felipe Torres González (torresfelipex1<AT>gmail.com)
## - Daniel Melgarejo García
##
## System requirements: Etherbone
##
## GNU Lesser General Public License Usage
## This file may be used under the terms of the GNU Lesser
## General Public License version 2.1 as published by the Free Software
## Foundation and appearing in the file LICENSE.LGPL included in the
## packaging of this file. Please review the following information to
## ensure the GNU Lesser General Public License version 2.1 requirements
## will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
################################################################################
# Enable verbose message output from the script
VERBOSE_MSG=0
# WRSTAT log (save calls to the wrc)
WRSTAT="x"
# Etherbone variables
ETHBONE_DISCOVER=./bin/eb-discover
ETHBONE_DISCOVER_FLAGS=udp/
# Virtual UART Tool
VUART=./py7slib/shell.py
# Flag to enable sync check
SYNC="OFF"
# Temperature interval
TEMP="OFF"
# BUS used for external sensors
BUS="w1"
BUS_PATH=/sys/bus/$BUS/devices/
# DS18B20+ OneWire sensors have an unique address starting by 28
SENSOR_HADDR="28"
SENSORS=0
# Enable oven Switchoff
# This variable allows to connect a relay to the Host to switchoff the
# equipment if the maximum temperature is reached.
OVENSWITCH="OFF"
GPIOVEN=17
# Remote device
REMOTE_NAME="WR LEN"
REMOTE_ADDR=
# Default timeout
INTERVAL=0
# File where the result of each check will be stored
HEADEREDOUT=0
# Remote username (in the WRZEN is 'root' & pass 'root')
REMOTE_USER='root'
REMOTE_PASS='root'
# Trap Ctrl-C and call ctrl_c() to end the test
trap ctrl_c INT
# -------------------------------------------
# Function to check if a device is alive
check_alive()
{
if [ $VERBOSE_MSG != 0 ]; then
echo -e "\033[1mChecking connection with $REMOTE_NAME ($REMOTE_ADDR)...\033[0m"
fi
ping -c 1 $1 &> /dev/null
if [ $? -ne 0 ]; then
echo -e "\e[31mCould not connect with $REMOTE_NAME($REMOTE_ADDR)\033[0m"
exit 1
fi
}
# -------------------------------------------
# Function to scan a bus and search for valid temperature sensors
search_tsensors()
{
sensors=$(ls $BUS_PATH)
for entry in $sensors
do
haddr=$(echo $entry | cut -d'-' -f1)
if [ $haddr == $SENSOR_HADDR ]
then
if [ $VERBOSE_MSG -eq 1 ]; then
echo "Temperature sensor detected: $entry"
fi
SENSORS_ARRAY[$SENSORS]=$entry
SENSORS=$(($SENSORS+1))
fi
done
}
# -------------------------------------------
# Function to read temperature sensors
read_tsensors()
{
for s in ${SENSORS_ARRAY[@]}
do
temp=$(cat $BUS_PATH/$s/w1_slave)
temp=$(echo $temp | grep -oh "t=[0-9]\{5\}" | cut -d'=' -f2)
temp=$(echo "scale=3;$temp/1000" | bc)
echo -e "$s:$temp \c"
done
}
# -------------------------------------------
# Function to get temperature from external sensors (1W) and
# from the WRC (stat)
temp_check()
{
if [[ $VERBOSE_MSG -eq 1 ]]; then
echo -e "Checking temperatures..."
fi
search_tsensors
cur_temp=$(read_tsensors)
if [[ $WRSTAT == "x" ]]; then
echo "stat" > temp
$VUART -i temp -o temp2 $REMOTE_ADDR
WRSTAT=`cat ./temp2`
#WRSTAT=$($VUART -i temp $REMOTE_ADDR | tail -6)
rm temp temp2
fi
wrc_temp=$(echo $WRSTAT | grep -oh "temp: [0-9.]\{5\}.")
echo "$cur_temp$wrc_temp"
}
# -------------------------------------------
temp_range_check()
{
wrs_temps=$1
if [[ $SENSOR_TAG == "temp" ]]; then
temp_sensor=$(echo $wrs_temps | grep -oh "$SENSOR_TAG: [0-9.]\{5\}." | cut -d' ' -f2)
temp_sensor=${temp_sensor%.*}
else
temp_sensor=$(echo $wrs_temps | grep -oh "$SENSOR_TAG:[0-9.]\{4\}." | cut -d':' -f2)
temp_sensor=${temp_sensor%.*}
fi
if [ $OVENSWITCH == "ON" ]; then
if [[ $temp_sensor -gt $MAX || $temp_sensor -lt $MIN ]]; then
echo -e "\nTemperature exceeded ($temp_sensorºC)"
echo "Turning off..."
python gpio.py $GPIOVEN off
exit 1
fi
fi
}
# -------------------------------------------
check_secureoff()
{
if [[ $1 == "" ]]; then
echo -e "\e[31m-x parameter needs a temperature sensor's name (Sensor_Tag)\033[0m"
exit 1
fi
}
wrstat()
{
echo "stat" > temp
$VUART -i temp -o temp2 $REMOTE_ADDR
WRSTAT=`cat ./temp2`
rm temp temp2
echo $WRSTAT
}
# -------------------------------------------
# Check the arguments to sync flag
check_synclevel()
{
if [[ $1 -ne 1 && $1 -ne 2 ]]; then
echo -e "\e[31mNot a valid sync level introduced. Exiting...\033[0m"
exit 1
fi
}
# -------------------------------------------
# Check the synchronization status of the WRC
sync_check()
{
if [[ $VERBOSE_MSG -eq 1 ]]; then
echo -e "Checking synchronization..."
fi
WRSTAT=$1
if [[ $VERBOSE_MSG -eq 1 ]]; then
echo -e "\tLink Status: $link_status"
fi
# Save linkup and servo state
wrs_servo=$(echo $WRSTAT | grep -oh "lock:[0-1]" | head -n 1)
wrs_TF=$(echo $WRSTAT | grep -oh "ss:.[A-Z_]\{11\}." | cut -d':' -f2)
echo -e "$wrs_servo sync:$wrs_TF\c"
if [[ $SYNC_LEVEL -ge 2 ]]; then
# Save Round Trip Time
rtt=$(echo $WRSTAT | grep -oh "mu:[0-9]*" | cut -d':' -f2)
# Save bitslide values
drxm=$(echo $WRSTAT | grep -oh "drxm:[0-9]*" | cut -d':' -f2)
drxs=$(echo $WRSTAT | grep -oh "drxs:[0-9]*" | cut -d':' -f2)
echo -e " rtt:$rtt drxm:$drxm drxs:$drxs"
fi
}
# -------------------------------------------
# Trap for the ctrl-c key combination
ctrl_c()
{
# Called from ctrl-c
echo -e "\033[1mBye!\033[0m"
exit 0
}
# ------------------------------------------
help()
{
cat << EOF
Usage: $(basename $0) [options]
Options:
-h|--help Prints this help
-v|--verbose Enables verbose mode
-r|--remote Remote address (IP)
-i|--interval Checking rate
-d|--header Write a description header of the log values
-s|--sync Enable synchronization checking
Levels:
1 -> Servo state
2 -> 1 + RTT and Bitslides
-t|--temp min,max Enable temperature checking
-x|--secureoff Switchoff oven when temp max is reached.
User must specify the lower address of the temp sensor
EOF
exit 0
}
# -------------------------------------------
# Main test loop
test()
{
# First check that the device is still alive
check_alive $REMOTE_ADDR
WRSTAT=$(wrstat)
# Get temperature values from the sensors
temps=$(temp_check "$WRSTAT")
# Save synchronization status
if [ $SYNC == "ON" ]; then
syncs=$(sync_check "$WRSTAT")
fi
# Control of the temperature range
if [ $TEMP == "ON" ]; then
temp_range_check "$temps"
if [[ $? -gt 0 ]]; then exit $?; fi
fi
echo -e "$(date +%s) $temps $syncs"
}
# -------------------------------------------
if [ $# -eq 0 ]; then help; exit 1; fi
while [ $# -gt 0 ]; do # Until you run out of parameters . . .
case "$1" in
-h|--help) help;;
-v|--verbose) VERBOSE="-v";;
-r|--remote) REMOTE_ADDR=$2;shift;;
-i|--interval) INTERVAL=$2;shift;;
-d|--header) HEADEREDOUT=1;shift;;
-t|--temp)
TEMP="ON";
MIN=$(echo $2 | cut -d"," -f1);
MAX=$(echo $2 | cut -d"," -f2);
shift;;
-s|--sync)
SYNC="ON";
SYNC_LEVEL=$2;
check_synclevel $SYNC_LEVEL;
shift;;
-x|--secureoff)
OVENSWITCH="ON";
SENSOR_TAG=$2;
check_secureoff $SENSOR_TAG;
shift;;
*) echo "Unknown arg: $1"; help;;
esac
shift # Check next set of parameters.
done
# Main script -------------------------------
echo -e "\033[1mRunning test...\033[0m"
echo -e "\tConnecting to WR LEN ($REMOTE_ADDR)"
if [ $HEADEREDOUT == 1 ]; then
echo "# Epoch seconds $tempsensors $devtemp $synclabels"
fi
while true
do
test
sleep $INTERVAL
done