-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsend_velocities.sh
More file actions
executable file
·35 lines (28 loc) · 1.14 KB
/
send_velocities.sh
File metadata and controls
executable file
·35 lines (28 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#!/bin/bash
# Check if the correct number of arguments are provided
if [ "$#" -ne 4 ]; then
echo "Usage: $0 <velocity1> <velocity2> <velocity3> <velocity4>"
exit 1
fi
# Function to convert decimal fixed-point value to integer
# Multiply by 100 to shift two decimal places and remove decimal point
to_integer() {
printf "%.0f" "$(echo "$1 * 100" | bc)"
}
# Function to convert integer to little endian hexadecimal representation
to_little_endian_hex() {
printf "%04x" "$((($1 & 0xFF) << 8 | ($1 >> 8) & 0xFF))"
}
# Extract velocities from arguments, convert to integers, and convert to little endian hexadecimal representation
velocity1_int=$(to_integer $1)
velocity2_int=$(to_integer $2)
velocity3_int=$(to_integer $3)
velocity4_int=$(to_integer $4)
velocity1_hex=$(to_little_endian_hex $velocity1_int)
velocity2_hex=$(to_little_endian_hex $velocity2_int)
velocity3_hex=$(to_little_endian_hex $velocity3_int)
velocity4_hex=$(to_little_endian_hex $velocity4_int)
# Concatenate velocities to form CAN data
can_data="${velocity1_hex}${velocity2_hex}${velocity3_hex}${velocity4_hex}"
# Send CAN frame using cansend
cansend can0 123#${can_data}