Skip to content

Instantly share code, notes, and snippets.

@gjcourt
Last active August 7, 2016 16:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gjcourt/9af5534bfc55fff0fc0cadcabe6ec906 to your computer and use it in GitHub Desktop.
Save gjcourt/9af5534bfc55fff0fc0cadcabe6ec906 to your computer and use it in GitHub Desktop.
#!/bin/sh
# Copyright (C) 2016 George Courtsunis, MIT license
#
# Script accepts -e -f -c -s -S -C arguments for calculating the spoke length
# based on the effective rim diameter, flange diameter, center to flange, spoke
# diameter, spoke number, and cross number. All measurements are expected in
# mm, however any unit as long as they are all consistent will work.
#
# --erd Effective rim diameter
# --fd Flange diameter
# --sd Spoke diameter
# --ctf Center to flange
# -s Number of spoke holes per hub (ie: 24, 32, 36, etc)
# -c Cross number of lacing pattern (ie: 2, 3, etc)
#
# Usage
# ~$ ./spoke_length --erd 566 --fd 56.9 --sd 2 --ctf 21.8 -s 32 -c 3
# ~$ 273.246980
while getopts ":s:c:-:" opt; do
case $opt in
-)
case $OPTARG in
erd )
# The value is the index of the next option
EFFECTIVE_RIM_DIAMETER=${!OPTIND}
# Skip over the value of this option
OPTIND=$(($OPTIND+1))
;;
erd=*)
EFFECTIVE_RIM_DIAMETER=${OPTARG#erd=}
;;
erd*)
EFFECTIVE_RIM_DIAMETER=${OPTARG#erd}
;;
fd )
# The value is the index of the next option
FLANGE_DIAMETER=${!OPTIND}
# Skip over the value of this option
OPTIND=$(($OPTIND+1))
;;
fd=*)
FLANGE_DIAMETER=${OPTARG#fd=}
;;
fd*)
FLANGE_DIAMETER=${OPTARG#fd}
;;
sd )
# The value is the index of the next option
SPOKE_DIAMETER=${!OPTIND}
# Skip over the value of this option
OPTIND=$(($OPTIND+1))
;;
sd=*)
SPOKE_DIAMETER=${OPTARG#sd=}
;;
sd*)
SPOKE_DIAMETER=${OPTARG#sd}
;;
ctf )
# The value is the index of the next option
CENTER_TO_FLANGE=${!OPTIND}
# Skip over the value of this option
OPTIND=$(($OPTIND+1))
;;
ctf=*)
CENTER_TO_FLANGE=${OPTARG#ctf=}
;;
ctf*)
CENTER_TO_FLANGE=${OPTARG#ctf}
;;
*)
echo "HERE"
;;
esac
;;
s)
SPOKES=$OPTARG
;;
c)
CROSS=$OPTARG
;;
\?)
echo "Invalid option: -$OPTARG" >&2
exit 1
;;
:)
echo "Option -$OPTARG requires an argument." >&2
exit 1
;;
esac
done
# Ensure we have correct number of arguments
if [ -z "$EFFECTIVE_RIM_DIAMETER" ]; then
echo "Must enter effective rim diameter w/ --erd"
exit 1
fi
if [ -z "$FLANGE_DIAMETER" ]; then
echo "Must enter flange diameter w/ --fd"
exit 1
fi
if [ -z "$SPOKE_DIAMETER" ]; then
echo "Must enter spoke diameter w/ --sd"
exit 1
fi
if [ -z "$CENTER_TO_FLANGE" ]; then
echo "Must enter center to flange w/ --ctf"
exit 1
fi
if [ -z "$SPOKES" ]; then
echo "Must enter spoke number w/ -s"
exit 1
fi
if [ -z "$CROSS" ]; then
echo "Must enter cross number w/ -c"
exit 1
fi
spoke_length() {
echo "
pi = 4*a(1)
/* Theta from cross number */
theta = 2*pi*$CROSS / ($SPOKES / 2)
a = $EFFECTIVE_RIM_DIAMETER / 2
b = $FLANGE_DIAMETER / 2
/* Law of cosines */
cs = a^2 + b^2 - 2*a*b*c(theta)
/* Normalize for ctf distance */
l = sqrt(cs + $CENTER_TO_FLANGE^2) - $SPOKE_DIAMETER / 2
print l
" | bc -l
}
echo `spoke_length`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment