Skip to content

Commit 6a5d778

Browse files
committed
feat: add CAN setup scripts
1 parent 6a38c02 commit 6a5d778

3 files changed

Lines changed: 154 additions & 1 deletion

File tree

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ Each time you start a **new** docker container, run `container_setup.sh`. This w
102102

103103
TODO: Populate
104104

105-
## How to?
105+
## How to? / Knowledge Base
106106

107107
### Add new hardware setup
108108

@@ -156,3 +156,6 @@ Sometimes after connecting quest, the device is not discoverable. Do the followi
156156
2. Go to Quest notification menu and click on USB detected notification to allow connection
157157
3. Run `adb devices`, if device ID is shown then it should work. If restricted, then quest should have a pop up requestion USB connection again (different UI).
158158
4. If nothing shows, do `adb kill-server` then repeat step 3.
159+
160+
### CAN Bus Settings
161+
`sjw` is some bit timing parameter of CAN bus. It defaults to different value on different systems, so the `scripts/configure_socketcan.sh` set it to `21`. This seems to be okay from testing. If you need tighter or looser clock timing (affect message errors), then adjust as needed.

scripts/configure_socketcan.sh

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
#!/bin/bash
2+
#
3+
# Copyright 2025 Enactic, Inc.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http:#www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
set -eu
18+
19+
# Simple CAN Interface Setup Script
20+
# Usage: script/configure_socketcan.sh <interface> [options]
21+
22+
# Default values
23+
BITRATE=1000000
24+
DBITRATE=5000000
25+
FD_MODE=false
26+
SJW=21
27+
28+
# Show usage
29+
usage() {
30+
echo "Usage: $0 <can_interface> [options]"
31+
echo ""
32+
echo "Options:"
33+
echo " -fd Enable CAN FD mode (default: CAN 2.0)"
34+
echo " -b <bitrate> Set bitrate (default: 1000000)"
35+
echo " -d <dbitrate> Set CAN FD data bitrate (default: 5000000)"
36+
echo " -h Show help"
37+
echo ""
38+
echo "Examples:"
39+
echo " $0 can0 # CAN 2.0 at 1Mbps"
40+
echo " $0 can0 -fd # CAN FD at 1Mbps/5Mbps"
41+
echo " $0 can0 -b 500000 # CAN 2.0 at 500kbps"
42+
echo " $0 can0 -fd -d 8000000 # CAN FD with 8Mbps data rate"
43+
}
44+
45+
# Check if interface provided
46+
if [ -z "$1" ]; then
47+
echo "Error: Specify CAN interface (e.g., can0)"
48+
usage
49+
exit 1
50+
fi
51+
52+
CAN_IF=$1
53+
shift
54+
55+
# Parse options
56+
while [[ $# -gt 0 ]]; do
57+
case $1 in
58+
-fd)
59+
FD_MODE=true
60+
shift
61+
;;
62+
-b)
63+
BITRATE="$2"
64+
shift 2
65+
;;
66+
-d)
67+
DBITRATE="$2"
68+
shift 2
69+
;;
70+
-h)
71+
usage
72+
exit 0
73+
;;
74+
-s)
75+
SJW="$2"
76+
shift 2
77+
;;
78+
*)
79+
echo "Error: Unknown option '$1'"
80+
usage
81+
exit 1
82+
;;
83+
esac
84+
done
85+
86+
# Check if interface exists
87+
if ! ip link show "$CAN_IF" &>/dev/null; then
88+
echo "Error: CAN interface '$CAN_IF' not found"
89+
echo "Available interfaces:"
90+
ip link show | grep -E "can[0-9]" | cut -d: -f2 | tr -d ' ' || echo " No CAN interfaces found"
91+
exit 1
92+
fi
93+
94+
# Configure CAN interface
95+
echo "Configuring $CAN_IF..."
96+
97+
if ! sudo ip link set "$CAN_IF" down; then
98+
echo "Error: Failed to bring down $CAN_IF"
99+
exit 1
100+
fi
101+
102+
if [ "$FD_MODE" = true ]; then
103+
if ! sudo ip link set "$CAN_IF" type can bitrate "$BITRATE" dbitrate "$DBITRATE" fd on sjw "$SJW"; then
104+
echo "Error: Failed to configure CAN FD mode"
105+
exit 1
106+
fi
107+
echo "$CAN_IF is now set to CAN FD mode (${BITRATE} bps / ${DBITRATE} bps) sjw $SJW"
108+
else
109+
if ! sudo ip link set "$CAN_IF" type can bitrate "$BITRATE" sjw "$SJW"; then
110+
echo "Error: Failed to configure CAN 2.0 mode"
111+
exit 1
112+
fi
113+
echo "$CAN_IF is now set to CAN 2.0 mode (${BITRATE} bps) sjw $SJW"
114+
fi
115+
116+
if ! sudo ip link set "$CAN_IF" up; then
117+
echo "Error: Failed to bring up $CAN_IF"
118+
exit 1
119+
fi
120+
121+
echo "$CAN_IF is active"

scripts/setup_can.sh

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/bin/bash
2+
3+
SCRIPT_DIR=$(dirname "${BASH_SOURCE[0]}")
4+
5+
check_and_configure_socketcan() {
6+
local interface="${1:-}"
7+
8+
if [[ -z "$interface" ]]; then
9+
return "Usage: check_and_configure_socketcan <interface>"
10+
fi
11+
12+
if ip link show "$interface" &>/dev/null; then
13+
echo "[OK] Interface '$interface' found"
14+
else
15+
echo "[ERROR] Interface '$interface' not found"
16+
return 1
17+
fi
18+
19+
echo "[INFO] Configuring SocketCAN"
20+
if ! "$SCRIPT_DIR/configure_socketcan.sh" "$interface"; then
21+
echo "[ERROR] Failed to configure $interface"
22+
return 1
23+
fi
24+
25+
echo "[DONE] Configured $interface"
26+
}
27+
28+
check_and_configure_socketcan can_left
29+
check_and_configure_socketcan can_right

0 commit comments

Comments
 (0)