Skip to content

Commit

Permalink
add scripts/create_slcan.sh
Browse files Browse the repository at this point in the history
  • Loading branch information
PonomarevDA committed Dec 4, 2024
1 parent 298c2c0 commit 3a3429c
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
!scripts/cyphal_config_slcan1.sh
!scripts/run_sim.sh
!scripts/install.sh
!scripts/tools/can/create_slcan.sh
!scripts/create_slcan.sh

!uav_dynamics/timed_roslaunch

Expand Down
126 changes: 126 additions & 0 deletions scripts/create_slcan.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
#!/bin/bash
# https://github.com/PonomarevDA/tools/blob/main/can/create_slcan.sh
# This software is distributed under the terms of the MIT License.
# Copyright (c) 2023 Dmitry Ponomarev.
# Author: Dmitry Ponomarev <[email protected]>

SCRIPT_NAME=$(basename $BASH_SOURCE)
RED='\033[0;31m'
YELLOW='\033[0;33m'
NC='\033[0m' # No Color

HELP="usage: $SCRIPT_NAME [--help] [--device <dev_path>] [--interface <interface>] [--only-find].
The utility automatically finds a CAN-sniffer and creates SLCAN.
options:
-d, --device DEV_PATH Specify device path manually instead of trying to find it
among known sniffers. Example: /dev/ttyACM0.
-i, --interface INTERFACE Specify custom interface. By default it attach it to slcan0.
-o, --only-find Do not create the interface, only look for a sniffer.
-h, --help Show this help message and exit."

INTERFACE=slcan0
DEV_PATH=""
ONLY_FIND="no"

function log_error() {
lineno=($(caller))
printf "$RED$SCRIPT_NAME ERROR on line ${lineno}: $1!$NC\n"
}

function log_warn() {
lineno=($(caller))
printf "$YELLOW$SCRIPT_NAME WARN on line ${lineno}: $1.$NC\n"
}

while [[ $# -gt 0 ]]; do
case $1 in
-d|--device)
DEV_PATH="$2"
shift
;;

-i|--interface)
INTERFACE="$2"
shift
;;

-o|--only-find)
ONLY_FIND="yes"
;;

-h|--help)
echo "$HELP"
[[ "${BASH_SOURCE[0]}" -ef "$0" ]] && exit 0 || return 0
;;

*)
log_error "Unknown option: $1"
echo "$HELP"
[[ "${BASH_SOURCE[0]}" -ef "$0" ]] && exit 0 || return 1
;;
esac
shift
done

function find_sniffer() {
KNOWN_SNIFFERS=(
"ID_MODEL_ID=374b|ID_VENDOR_ID=0483" # RaccoonLab programmer-sniffer
"ID_MODEL_ID=60c7|ID_VENDOR_ID=1d50" # Zubax Babel-Babel
)

EXPECTED_DEV_PATH="/dev/ttyACM*"
EXPECTED_SYMLINK_PATH="/dev/serial/by-id/"
for dev_path in $EXPECTED_DEV_PATH; do
[ -e "$dev_path" ] || continue
for sniffer in ${KNOWN_SNIFFERS[@]}; do
is_sniffer=$(udevadm info $dev_path | grep -E "($sniffer)" -wc || true)
if [ "$is_sniffer" == 2 ]; then
DEV_PATH=$(find -L $EXPECTED_SYMLINK_PATH -samefile $dev_path)
break
fi
done
done
}

function create_slcan() {
sudo slcand -o -c -f -s8 -t hw -S 1000000 $DEV_PATH $INTERFACE
sleep 1 # without sleep next commands may fail with 'Cannot find device "slcan0"'
sudo ip link set up $INTERFACE
sudo tc qdisc add dev $INTERFACE root handle 1: pfifo_head_drop limit 1000
}


# Step 1. Define the device path
if [ -z $DEV_PATH ]; then
find_sniffer

if [ -z "$DEV_PATH" ]; then
log_error "Can't find a sniffer"
[[ "${BASH_SOURCE[0]}" -ef "$0" ]] && exit 0 || return 1
fi

if [[ $ONLY_FIND == "yes" ]]; then
echo "$DEV_PATH"
[[ "${BASH_SOURCE[0]}" -ef "$0" ]] && exit 0 || return 0
fi
fi


# Step 2. Create SLCAN based on device path
echo "SLCAN creator settings:
- DEV_PATH=$DEV_PATH
- INTERFACE=$INTERFACE"

if [[ $(ifconfig | grep $INTERFACE) ]]; then
log_warn "specified interface already exist, skip"
[[ "${BASH_SOURCE[0]}" -ef "$0" ]] && exit 0 || return 1
fi

create_slcan

if [[ -z $(ifconfig | grep $INTERFACE) ]]; then
log_error "Interface '$INTERFACE' has not been successfully created"
[[ "${BASH_SOURCE[0]}" -ef "$0" ]] && exit 0 || return 1
fi
4 changes: 2 additions & 2 deletions scripts/docker.sh
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ setup_mavlink_sitl_config() {
setup_dronecan_hitl_config() {
setup_mavlink_sitl_config
if [ -z "$SNIFFER" ]; then
source ./tools/can/create_slcan.sh --only-find
source ./create_slcan.sh --only-find
DRONECAN_DEV_PATH_SYMLINK=$DEV_PATH
else
DRONECAN_DEV_PATH_SYMLINK=$SNIFFER
Expand All @@ -101,7 +101,7 @@ setup_dronecan_hitl_config() {
setup_cyphal_hitl_config() {
setup_mavlink_sitl_config
if [ -z "$SNIFFER" ]; then
source ./tools/can/create_slcan.sh --only-find
source ./create_slcan.sh --only-find
CYPHAL_DEV_PATH_SYMLINK=$DEV_PATH
else
CYPHAL_DEV_PATH_SYMLINK=$SNIFFER
Expand Down
8 changes: 4 additions & 4 deletions scripts/run_sim.sh
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ setup_ros() {
setup_dronecan_hitl() {
if [ ! -z $DRONECAN_DEV_PATH_SYMLINK ]; then
echo "Trying to create slcan0 for dronecan..."
$SCRIPT_DIR/tools/can/create_slcan.sh -d $DRONECAN_DEV_PATH_SYMLINK -i slcan0
$SCRIPT_DIR/create_slcan.sh -d $DRONECAN_DEV_PATH_SYMLINK -i slcan0
fi
if [[ -z $(ifconfig | grep slcan0) ]]; then
echo "HITL can't be started without CAN interface!"
Expand All @@ -55,7 +55,7 @@ setup_dronecan_hitl() {
setup_cyphal_hitl() {
if [ ! -z $CYPHAL_DEV_PATH_SYMLINK ]; then
echo "Trying to create slcan0 for cyphal/serial..."
$SCRIPT_DIR/tools/can/create_slcan.sh -d $CYPHAL_DEV_PATH_SYMLINK -i slcan0
$SCRIPT_DIR/create_slcan.sh -d $CYPHAL_DEV_PATH_SYMLINK -i slcan0
fi
if [[ -z $(ifconfig | grep slcan0) ]]; then
echo "HITL can't be started without CAN interface!"
Expand All @@ -67,11 +67,11 @@ setup_cyphal_hitl() {
setup_combined_hitl() {
if [ ! -z $DRONECAN_DEV_PATH_SYMLINK ]; then
echo "Trying to create slcan0 for dronecan..."
$SCRIPT_DIR/tools/can/create_slcan.sh -d $DRONECAN_DEV_PATH_SYMLINK -i slcan0
$SCRIPT_DIR/create_slcan.sh -d $DRONECAN_DEV_PATH_SYMLINK -i slcan0
fi
if [ ! -z $CYPHAL_DEV_PATH_SYMLINK ]; then
echo "Trying to create slcan1 for cyphal..."
$SCRIPT_DIR/tools/can/create_slcan.sh -d $CYPHAL_DEV_PATH_SYMLINK -i slcan1
$SCRIPT_DIR/create_slcan.sh -d $CYPHAL_DEV_PATH_SYMLINK -i slcan1
source $SCRIPT_DIR/cyphal_config_slcan1.sh
fi
}
Expand Down

0 comments on commit 3a3429c

Please sign in to comment.