110 lines
5.6 KiB
Bash
110 lines
5.6 KiB
Bash
|
|
#!/usr/bin/env bash
|
||
|
|
set -euo pipefail
|
||
|
|
ROOT="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." && pwd)"
|
||
|
|
CAN=1; EC=1; CHECK=0; RECONFIGURE=0
|
||
|
|
BITRATE="${CAN_BITRATE:-1000000}"
|
||
|
|
IFACE=""; CAN_IFACES=()
|
||
|
|
IGH_ROOT="${IGH_ROOT:-$ROOT/dependency/x86/third_party/ethercat/v1.7.0}"
|
||
|
|
CONF="${ETHERCAT_CONF:-$IGH_ROOT/etc/ethercat.conf}"
|
||
|
|
usage(){ cat <<'EOF'
|
||
|
|
Usage: scripts/start_hardware.sh --ethercat-iface IFACE [--can-iface can0 ...]
|
||
|
|
[--bitrate 1000000] [--can-only|--ethercat-only] [--check] [--reconfigure-can]
|
||
|
|
scripts/start_hardware.sh --can-only [--can-iface can0 ...] [--check]
|
||
|
|
|
||
|
|
--check only checks prerequisites and prints the plan; no sudo or changes.
|
||
|
|
Without --can-iface, select existing CAN interfaces. Default bitrate: 1000000.
|
||
|
|
EtherCAT requires --ethercat-iface on every invocation, including --check.
|
||
|
|
No interface is selected from configuration or environment. Normal execution needs sudo.
|
||
|
|
No motor enable or motion commands are issued.
|
||
|
|
EOF
|
||
|
|
}
|
||
|
|
while (( $# )); do
|
||
|
|
case "$1" in
|
||
|
|
--ethercat-iface) [[ $# -ge 2 ]] || exit 2; IFACE="$2"; shift 2;;
|
||
|
|
--can-iface) [[ $# -ge 2 ]] || exit 2; CAN_IFACES+=("$2"); shift 2;;
|
||
|
|
--bitrate) [[ $# -ge 2 ]] || exit 2; BITRATE="$2"; shift 2;;
|
||
|
|
--can-only) EC=0; shift;;
|
||
|
|
--ethercat-only) CAN=0; shift;;
|
||
|
|
--check) CHECK=1; shift;;
|
||
|
|
--reconfigure-can) RECONFIGURE=1; shift;;
|
||
|
|
--help|-h) usage; exit 0;;
|
||
|
|
*) usage >&2; exit 2;;
|
||
|
|
esac
|
||
|
|
done
|
||
|
|
(( CAN || EC )) || { echo 'Select at least one bus.' >&2; exit 2; }
|
||
|
|
if (( EC )) && [[ -z "$IFACE" ]]; then
|
||
|
|
echo 'EtherCAT: --ethercat-iface IFACE is required (for example --ethercat-iface enp2s0).' >&2
|
||
|
|
exit 2
|
||
|
|
fi
|
||
|
|
[[ "$BITRATE" =~ ^[0-9]{1,8}$ ]] && (( 10#$BITRATE > 0 )) || { echo 'Invalid CAN bitrate' >&2; exit 2; }
|
||
|
|
for command in ip python3 modinfo; do command -v "$command" >/dev/null || { echo "Missing command: $command" >&2; exit 1; }; done
|
||
|
|
if (( CAN && ${#CAN_IFACES[@]} == 0 )); then
|
||
|
|
for path in /sys/class/net/*; do
|
||
|
|
if [[ "$(cat "$path/type")" == 280 ]]; then CAN_IFACES+=("${path##*/}"); fi
|
||
|
|
done
|
||
|
|
fi
|
||
|
|
failed=0
|
||
|
|
if (( CAN )); then
|
||
|
|
if (( ${#CAN_IFACES[@]} == 0 )); then
|
||
|
|
echo 'CAN: no CAN interface detected. Check the adapter and driver; no interface was created or marked ready.' >&2
|
||
|
|
failed=1
|
||
|
|
fi
|
||
|
|
for interface in "${CAN_IFACES[@]}"; do
|
||
|
|
if [[ ! -f "/sys/class/net/$interface/type" ]] || [[ "$(cat "/sys/class/net/$interface/type")" != 280 ]]; then
|
||
|
|
echo "CAN: not a CAN interface: $interface" >&2; failed=1
|
||
|
|
else echo "CAN plan: $interface, bitrate=$BITRATE"; fi
|
||
|
|
done
|
||
|
|
fi
|
||
|
|
if (( EC )); then
|
||
|
|
if [[ -z "$IFACE" || ! "$IFACE" =~ ^[[:alnum:]_.:-]+$ || ! -d "/sys/class/net/$IFACE" ]]; then
|
||
|
|
echo 'EtherCAT: specify an existing dedicated Ethernet port using --ethercat-iface.' >&2; failed=1
|
||
|
|
elif [[ "$IFACE" == lo || -d "/sys/class/net/$IFACE/wireless" ]] || ip route show default | grep -qE " dev $IFACE( |$)" || [[ -n "$(ip -o addr show dev "$IFACE" scope global)" ]]; then
|
||
|
|
echo "EtherCAT: $IFACE carries normal networking; refusing to disconnect or flush it." >&2; failed=1
|
||
|
|
else
|
||
|
|
echo "EtherCAT plan: $IFACE, MAC=$(cat "/sys/class/net/$IFACE/address"), existing config=$CONF"
|
||
|
|
if [[ "$(cat "/sys/class/net/$IFACE/carrier" 2>/dev/null || echo 0)" != 1 ]]; then
|
||
|
|
echo "EtherCAT: $IFACE has no link; a connected slave cannot be verified." >&2
|
||
|
|
fi
|
||
|
|
fi
|
||
|
|
for module in ec_master ec_igc; do
|
||
|
|
modinfo "$module" >/dev/null 2>&1 || { echo "EtherCAT: missing kernel module $module" >&2; failed=1; }
|
||
|
|
done
|
||
|
|
[[ -x "$IGH_ROOT/sbin/ethercatctl" && -x "$IGH_ROOT/bin/ethercat" ]] || { echo "EtherCAT: incomplete IgH tools in $IGH_ROOT" >&2; failed=1; }
|
||
|
|
fi
|
||
|
|
(( failed == 0 )) || exit 1
|
||
|
|
(( CHECK == 0 )) || { echo 'Prerequisite checks passed; no changes made.'; exit 0; }
|
||
|
|
if (( EUID != 0 )); then
|
||
|
|
args=(); (( CAN )) || args+=(--ethercat-only); (( EC )) || args+=(--can-only)
|
||
|
|
[[ -z "$IFACE" ]] || args+=(--ethercat-iface "$IFACE")
|
||
|
|
for interface in "${CAN_IFACES[@]}"; do args+=(--can-iface "$interface"); done
|
||
|
|
(( RECONFIGURE == 0 )) || args+=(--reconfigure-can)
|
||
|
|
exec sudo env "IGH_ROOT=$IGH_ROOT" "ETHERCAT_CONF=$CONF" bash "$ROOT/scripts/start_hardware.sh" "${args[@]}" --bitrate "$BITRATE"
|
||
|
|
fi
|
||
|
|
# Do not reinitialize a bus while the application may be using it.
|
||
|
|
if pgrep -x cmvr_es >/dev/null; then echo 'Stop cmvr_es through its manager before initializing hardware.' >&2; exit 1; fi
|
||
|
|
if (( CAN )); then
|
||
|
|
modprobe can; modprobe can_raw
|
||
|
|
for interface in "${CAN_IFACES[@]}"; do
|
||
|
|
read -r up current_rate < <(ip -details -json link show dev "$interface" | python3 -c 'import sys,json; d=json.load(sys.stdin)[0]; print(int("UP" in d.get("flags",[])),d.get("linkinfo",{}).get("info_data",{}).get("bitrate",0))')
|
||
|
|
if (( up )); then
|
||
|
|
if [[ "$current_rate" == "$BITRATE" ]]; then echo "CAN: $interface already up at $BITRATE; kept running."; continue; fi
|
||
|
|
(( RECONFIGURE )) || { echo "CAN: $interface already up at $current_rate; use --reconfigure-can to change bitrate." >&2; exit 1; }
|
||
|
|
ip link set dev "$interface" down
|
||
|
|
fi
|
||
|
|
ip link set dev "$interface" type can bitrate "$BITRATE"
|
||
|
|
ip link set dev "$interface" up
|
||
|
|
echo "CAN: $interface started, bitrate=$BITRATE"
|
||
|
|
done
|
||
|
|
fi
|
||
|
|
if (( EC )); then
|
||
|
|
mac="$(cat "/sys/class/net/$IFACE/address")"
|
||
|
|
if [[ -e /dev/EtherCAT0 ]] && "$IGH_ROOT/bin/ethercat" master 2>/dev/null | grep -qi "$mac"; then
|
||
|
|
echo "EtherCAT: master already uses $IFACE; kept running."
|
||
|
|
else
|
||
|
|
IGH_ROOT="$IGH_ROOT" ETHERCAT_CONF="$CONF" DEVICE_MODULES=igc bash "$ROOT/scripts/ethercat/start_ethercat.sh" "$IFACE"
|
||
|
|
fi
|
||
|
|
[[ -e /dev/EtherCAT0 ]] || { echo 'EtherCAT: /dev/EtherCAT0 was not created.' >&2; exit 1; }
|
||
|
|
"$IGH_ROOT/bin/ethercat" master
|
||
|
|
"$IGH_ROOT/bin/ethercat" slaves
|
||
|
|
fi
|