Skip to content

Commit

Permalink
fixed the race condition between plot_graph and Klipper on slow Pi cl…
Browse files Browse the repository at this point in the history
…ones
  • Loading branch information
Frix-x committed May 2, 2023
1 parent f1cf77a commit e40edf2
Showing 1 changed file with 36 additions and 5 deletions.
41 changes: 36 additions & 5 deletions scripts/plot_graphs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
# @version: 1.6

# CHANGELOG:
# v1.6: updated the handling of shaper graph files to be able to optionnaly account for added positions in the filenames and remove them
# v1.6: - updated the handling of shaper graph files to be able to optionnaly account for added positions in the filenames and remove them
# - fixed a bug in the belt graph on slow SD card or Pi clones (Klipper was still writing in the file while we were already reading it)
# v1.5: fixed klipper unnexpected fail at the end of the execution, even if graphs were correctly generated (unicode decode error fixed)
# v1.4: added the ~/klipper dir parameter to the call of graph_vibrations.py for a better user handling (in case user is not "pi")
# v1.3: some documentation improvement regarding the line endings that needs to be LF for this file
Expand Down Expand Up @@ -46,12 +47,31 @@ STORE_RESULTS=3 # Number of results to keep (older files are automatically clean

export LC_ALL=C

function is_fopen() {
filepath=$(realpath "$1")
for pid in $(ls /proc | grep -E '^[0-9]+$'); do
if [ -d "/proc/$pid/fd" ]; then
for fd in /proc/$pid/fd/*; do
if [ -L "$fd" ] && [ "$(readlink -f "$fd")" == "$filepath" ]; then
return 0
fi
done
fi
done
return 1
}

function plot_shaper_graph {
local generator filename newfilename date axis
generator="${KLIPPER_FOLDER}/scripts/calibrate_shaper.py"

# For each file
while read filename; do
# Wait for the file handler to be released by Klipper
while is_fopen "${filename}"; do
sleep 3
done

# We remove the /tmp in front of the filename
newfilename="$(echo ${filename} | sed -e "s/\\/tmp\///")"

Expand Down Expand Up @@ -80,6 +100,11 @@ function plot_belts_graph {

# For each file
while read filename; do
# Wait for the file handler to be released by Klipper
while is_fopen "${filename}"; do
sleep 3
done

# We extract the belt tested from the filename
belt="$(basename "${filename}" | cut -d '_' -f4 | cut -d '.' -f1 | sed -e 's/\(.*\)/\U\1/')"

Expand All @@ -97,14 +122,23 @@ function plot_vibr_graph {
date_ext="$(date +%Y%m%d_%H%M%S)"
generator="${SCRIPTS_FOLDER}/graph_vibrations.py"

# For each file
while read filename; do
# Wait for the file handler to be released by Klipper
while is_fopen "${filename}"; do
sleep 3
done

# Cleanup of the filename and moving it in the result folder
newfilename="$(echo ${filename} | sed -e "s/\\/tmp\/adxl345/vibr_${date_ext}/")"
mv "${filename}" "${isf}"/vibrations/"${newfilename}"
done <<< "$(find /tmp -type f -name "adxl345-*.csv" 2>&1 | grep -v "Permission")"

sync && sleep 2

# We compute the vibration graphs using all the csv files
"${generator}" "${isf}"/vibrations/vibr_"${date_ext}"*.csv -o "${isf}"/vibrations/vibrations_"${date_ext}".png -a "$1" -k "${KLIPPER_FOLDER}"

# Finally we cleanup the folder by moving the csv files in an archive
tar cfz "${isf}"/vibrations/vibrations_"${date_ext}".tar.gz "${isf}"/vibrations/vibr_"${date_ext}"*.csv
rm "${isf}"/vibrations/vibr_"${date_ext}"*.csv
}
Expand Down Expand Up @@ -162,9 +196,6 @@ fi

isf="${RESULTS_FOLDER//\~/${HOME}}"

# Dirty fix to be sure Klipper has closed the file before doing anything
sync && sleep 5

case ${1} in
SHAPER|shaper)
plot_shaper_graph
Expand Down

0 comments on commit e40edf2

Please sign in to comment.