forked from whitelightning450/spec
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathcommon_functions.sh
More file actions
307 lines (253 loc) · 10.2 KB
/
Copy pathcommon_functions.sh
File metadata and controls
307 lines (253 loc) · 10.2 KB
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
#!/bin/bash
#
# Script Name: common_functions.sh
# Description: This scripts has the common functions Gstreamer and cleanup for run_PIV.sh and test_PIV.sh.
#
# Usage:
# Script is runs when called by run_PIV.sh or test_PIV.sh
#
# Credits:
# -Funding for this project is provided through the USGS Next Generation Water Observing System (NGWOS) Research and Development program.
# -Engineered by: Deep Analytics LLC http://www.deepvt.com/
# -----------------------------------------------------
# Function to clean up the IMU process
cleanup() {
echo "Stopping IMU process..."
# Kill the IMU process
sudo pkill -f "run_imu.py"
echo "IMU process stopped."
}
# Launch gstreamer for a finite time based on duration parameter, retrying if needed
run_gst_launch() {
local attempt=1
local success=false
local expected_frames=$((duration * framerate))
local pid
local frame_count
local final_count
local tolerance=8 # Define tolerance for frame count capture
while [ "$success" = false ]; do
echo "Attempt $attempt to capture $expected_frames frames (±$tolerance frames acceptable)..."
# Clear any existing frames before starting
rm -f ${PARENT_DIR}/raw_frames/*
# Clear any existing video output
rm -f "${PARENT_DIR}/save_data/video_output.avi"
# Start gst-launch in background
gst-launch-1.0 -e -m -f \
v4l2src device=/dev/video3 ! image/jpeg, width=${width}, height=${height}, framerate=30/1 ! tee name=t \
\
t. ! queue ! avimux ! filesink location=${PARENT_DIR}/save_data/video_output.avi sync=false \
\
t. ! queue ! jpegdec ! videorate ! video/x-raw,format=I420,framerate=${framerate}/1 ! jpegenc ! multifilesink location=${PARENT_DIR}/raw_frames/frame%05d.jpg &
pid=$!
echo "gst-launch started with PID: $pid"
# Monitor frame count
local pipeline_failed=false
while true; do
# Check if gst-launch is still running
if ! kill -0 $pid 2>/dev/null; then
echo "gst-launch terminated unexpectedly"
pipeline_failed=true
break
fi
# Count current frames
frame_count=$(ls ${PARENT_DIR}/raw_frames/*.jpg 2>/dev/null | wc -l)
echo -ne "Current frames: $frame_count/$expected_frames\r"
if [ "$frame_count" -ge "$expected_frames" ]; then
echo -e "\nDesired frame count reached"
# Brief moment for final writes
sleep 0.1
# Terminate gst-launch gracefully first
kill -SIGINT $pid
sleep 2
# If still running, force terminate
if kill -0 $pid 2>/dev/null; then
echo "Forcing termination of gst-launch"
kill -9 $pid
fi
# Wait for any children to finish
wait $pid 2>/dev/null
break
fi
sleep 0.1
done
# Verify final frame count
final_count=$(ls ${PARENT_DIR}/raw_frames/*.jpg 2>/dev/null | wc -l)
echo "Final frame count: $final_count/$expected_frames"
# Check if count is within tolerance
if [ "$final_count" -ge "$((expected_frames - tolerance))" ] && \
[ "$final_count" -le "$((expected_frames + tolerance))" ]; then
echo "Frame capture completed successfully on attempt $attempt"
echo "Got $final_count frames (within ±$tolerance of target $expected_frames)"
# Verify AVI file exists and has size greater than 0
if [ -f "${PARENT_DIR}/save_data/video_output.avi" ] && \
[ -s "${PARENT_DIR}/save_data/video_output.avi" ]; then
# If we have extra frames, remove them
if [ "$final_count" -gt "$expected_frames" ]; then
echo "Trimming excess frames..."
ls -v ${PARENT_DIR}/raw_frames/*.jpg | \
tail -n $((final_count - expected_frames)) | \
xargs rm -f
# Verify the trim operation
final_count=$(ls ${PARENT_DIR}/raw_frames/*.jpg 2>/dev/null | wc -l)
echo "After trimming: $final_count frames"
if [ "$final_count" -eq "$expected_frames" ]; then
echo "Successfully trimmed to exact frame count"
success=true
else
echo "Error in trimming process, retrying capture..."
((attempt++))
continue
fi
else
success=true
fi
else
echo "AVI file is missing or empty, retrying..."
((attempt++))
fi
else
echo "Frame count outside acceptable range on attempt $attempt"
echo "Got $final_count frames, expected $expected_frames (±$tolerance)"
echo "Waiting 5 seconds before retry..."
sleep 5
((attempt++))
if [ $attempt -gt 10 ]; then
echo "Maximum retry attempts reached. Please check the system."
return 1
fi
fi
done
# Final verification
final_count=$(ls ${PARENT_DIR}/raw_frames/*.jpg 2>/dev/null | wc -l)
echo "Final verification: $final_count frames"
if [ "$final_count" -eq "$expected_frames" ]; then
echo "Frame capture completed successfully"
return 0
else
echo "Unexpected final frame count: $final_count"
return 1
fi
}
check_readable() {
if [ ! -r "$1" ]; then
echo "Error: Cannot read $1"
return 1
fi
return 0
}
is_numeric() {
[[ "$1" =~ ^[0-9]+$ ]]
}
enable_rtc_charging() {
local BATTERY_VOLTAGE_PATH="/sys/devices/platform/soc/soc:rpi_rtc/rtc/rtc0/battery_voltage"
local CHARGING_VOLTAGE_PATH="/sys/devices/platform/soc/soc:rpi_rtc/rtc/rtc0/charging_voltage"
local CONFIG_PATH="/boot/firmware/config.txt"
local CHARGING_PARAM="dtparam=rtc_bbat_vchg=3000000"
# Check if files exist and are readable
for file in "$BATTERY_VOLTAGE_PATH" "$CHARGING_VOLTAGE_PATH" "$CONFIG_PATH"; do
if ! check_readable "$file"; then
return 1
fi
done
# Read battery and charging voltages
local battery_voltage=$(cat "$BATTERY_VOLTAGE_PATH")
local charging_voltage=$(cat "$CHARGING_VOLTAGE_PATH")
# Check if values are numeric
if ! is_numeric "$battery_voltage" || ! is_numeric "$charging_voltage"; then
echo "Error: Invalid voltage values"
return 1
fi
echo "Battery voltage: $battery_voltage"
echo "Charging voltage: $charging_voltage"
# Check if battery is present (voltage > 0) and charging is not enabled
if [ "$battery_voltage" -gt 0 ] && [ "$charging_voltage" -eq 0 ]; then
echo "Battery detected and charging is not enabled"
# Check if charging parameter already exists in config.txt
if ! grep -q "rtc_bbat_vchg" "$CONFIG_PATH"; then
echo "Adding charging parameter to config.txt"
# Backup config file
sudo cp "$CONFIG_PATH" "${CONFIG_PATH}.backup"
# Add charging parameter
echo "$CHARGING_PARAM" | sudo tee -a "$CONFIG_PATH"
echo "Charging parameter added. A reboot is required for changes to take effect."
return 2 # Return code 2 indicates reboot needed
else
echo "Charging parameter already exists in config.txt"
return 0
fi
else
if [ "$battery_voltage" -eq 0 ]; then
echo "No battery detected"
fi
if [ "$charging_voltage" -gt 0 ]; then
echo "Charging is already enabled"
fi
return 0
fi
}
check_darkness() {
threshold=25
total_frames=5
required_dark=4
dark_count=0
echo "Checking $total_frames consecutive frames for darkness..."
for i in $(seq 1 $total_frames); do
tmp_img="/tmp/dark_check_$$_$i.jpg"
# Capture single frame using working camera caps
gst-launch-1.0 -q -v \
v4l2src device=/dev/video3 num-buffers=1 ! \
image/jpeg, width=${width}, height=${height}, framerate=30/1 ! \
jpegdec ! videoconvert ! jpegenc ! filesink location=$tmp_img
# If capture failed, treat as not dark (safe)
if [ ! -f "$tmp_img" ]; then
echo "Frame $i capture failed — assuming light present."
return 1
fi
# Calculate brightness (still using convert, optional replacement later)
brightness=$(convert "$tmp_img" -colorspace Gray -format "%[fx:mean*255]" info:)
brightness_int=$(printf "%.0f" "$brightness")
echo "Frame $i brightness: $brightness_int"
# Delete immediately
rm -f "$tmp_img"
if [ "$brightness_int" -lt "$threshold" ]; then
dark_count=$((dark_count + 1))
fi
sleep 0.5
done
echo "Dark frames: $dark_count / $total_frames"
if [ "$dark_count" -ge "$required_dark" ]; then
echo "Darkness confirmed (>= $required_dark frames)."
return 0 # DARK
else
echo "Light detected."
return 1 # NOT DARK
fi
}
check_piv_output_data() {
if ! command -v jq &> /dev/null; then
echo "[WARN] jq not installed. Cannot check PIV data."
return
fi
if [ ! -f "$SAVE_JSON" ]; then
echo "[WARN] save.json not found."
return
fi
CURRENT_DATA_DIR=$(jq -r '.current_data_directory' "$SAVE_JSON")
if [ -z "$CURRENT_DATA_DIR" ] || [ "$CURRENT_DATA_DIR" = "null" ] || [ ! -d "$CURRENT_DATA_DIR" ]; then
echo "[WARN] current_data_directory invalid or does not exist: $CURRENT_DATA_DIR"
return
fi
# Find folder containing PIV_output in its name
PIV_FOLDER=$(find "$CURRENT_DATA_DIR" -maxdepth 1 -type d -name "*PIV_output*" | head -n 1)
if [ -z "$PIV_FOLDER" ]; then
echo "[WARN] NO DATA in $CURRENT_DATA_DIR (No PIV_output folder found)"
return
fi
CSV_COUNT=$(find "$PIV_FOLDER" -maxdepth 1 -type f -name "*.csv" | wc -l)
if [ "$CSV_COUNT" -eq 6 ]; then
echo "[INFO] PIV data verified in $PIV_FOLDER (6 CSV files present)"
else
echo "[WARN] NO DATA in $CURRENT_DATA_DIR (Found $CSV_COUNT CSV files)"
fi
}