-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathrosrepub-gen.sh
executable file
·114 lines (98 loc) · 4.37 KB
/
rosrepub-gen.sh
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/usr/bin/env bash
print_help() {
if [ $# -gt 0 ] ; then
echo -e "ERROR: $*\n"
fi
echo "Usage: $0 [options] configfile"
echo "Generate C++ republisher code for LCM to ROS or ROS to LCM messaging."
echo "Options:"
echo " -h Print this help and exit"
echo ""
echo "This code uses an input config file. Each (non # commented) line "
echo "defines the topic name, lcm message package, message type, and "
echo "republish direction (ros2lcm or lcm2ros) as a comma-separated list."
echo "Thus a sample file (including the first header line) looks like: "
echo ""
echo " # Topic name, Message package, Message type, Direction "
echo " example_topic, exlcm, example_t, lcm2ros"
echo ""
echo "Using this configuration would generate the C++ code for a republisher"
echo "that reads messages of type 'exlcm::example_t' from LCM channel"
echo "'example_topic', and publishes the equivalent message onto the ROS "
echo "topic 'example_topic'."
exit 2
}
# Process option flags
while getopts ':h' flag; do
case "${flag}" in
h) print_help ;;
\?) print_help "Invalid option: -$OPTARG" ;;
esac
done
# Remove accepted options (now config file argument should be $1)
shift $((OPTIND-1))
if [ $# -eq 0 ] ; then
print_help "Must specify input config file"
fi
mkdir -p launch autosrc
touch -a autosrc/CMakeLists.txt
# Create (empty) launch file
LAUNCH_FILE=${1##*/} # Remove leading directories
LAUNCH_FILE=${LAUNCH_FILE%.*} # Remove trailing extension
LAUNCH_FILE="launch/${LAUNCH_FILE}.launch"
echo "Adding publishers to launch file: ${LAUNCH_FILE} (will be overwritten)" >&2
# if [ ! -f $LAUNCH_FILE ] ; then
echo "<launch>" > $LAUNCH_FILE
echo -e "\t<master auto=\"start\" />" >> $LAUNCH_FILE
echo -e "\t<group ns=\"lcm_to_ros\">" >> $LAUNCH_FILE
echo -e "\t</group>" >> $LAUNCH_FILE
echo "</launch>" >> $LAUNCH_FILE
# fi
# Generate republishers
while IFS=", " read TOPIC_NAME PACKAGE_NAME MESSAGE_TYPE DIRECTION JUNK ; do
if [ -z "$TOPIC_NAME" ] || [ $( echo ${TOPIC_NAME} | head -c 1 ) = "#" ] ; then
continue
fi
echo "Processing - Topic: ${TOPIC_NAME}, Message: ${PACKAGE_NAME}/${MESSAGE_TYPE}, Direction: ${DIRECTION}" >&2
# Check which direction specified
if [ "$DIRECTION" = lcm2ros ] ; then
RFILE=src/lcm2ros_default_republisher.cpp.in
elif [ "$DIRECTION" = ros2lcm ] ; then
RFILE=src/ros2lcm_default_republisher.cpp.in
else
echo -e "\tERROR: Direction ${DIRECTION} not recognised, must be 'ros2lcm' or 'lcm2ros'" >&2
continue
fi
# Check if specified message exists
if [ ! -f "${PACKAGE_NAME}/${MESSAGE_TYPE}.hpp" ] ; then
echo -e "\tERROR: Message type "${PACKAGE_NAME}/${MESSAGE_TYPE}.hpp" not found." >&2
continue
fi
# Generate republisher code
OUTFILE="autosrc/${TOPIC_NAME}_republisher.cpp"
echo -n -e "\tCreating CPP file $OUTFILE..." >&2
echo $(printf '/%.0s' {1..71}) > $OUTFILE
echo "// This source was automatically generated by the lcm_to_ros package" >>$OUTFILE
echo "// https://github.com/nrjl/lcm_to_ros, [email protected]" >>$OUTFILE
echo -e "$(printf '/%.0s' {1..71})\n//" >>$OUTFILE
echo "// Source message: $MESSAGE_TYPE.msg" >> $OUTFILE
echo "// Creation: $(date '+%c')" >> $OUTFILE
echo -e "//\n$(printf '/%.0s' {1..71})" >>$OUTFILE
cat $RFILE | sed "s/@MESSAGE_TYPE@/$MESSAGE_TYPE/g" | \
sed "s/@TOPIC_NAME@/$TOPIC_NAME/g; s/@PACKAGE_NAME@/$PACKAGE_NAME/g; " >> $OUTFILE
echo " done." >& 2
# If not already present, add CMakeLists.txt entry
if ! grep -q "add_executable(\s*${TOPIC_NAME}_republisher" autosrc/CMakeLists.txt ; then
echo -n -e "\tAdding entry to autosrc/CMakeLists.txt ..." >&2
cat src/default_CMakeLists.txt.in | sed "s/@TOPIC_NAME@/$TOPIC_NAME/g" >> autosrc/CMakeLists.txt
echo " done." >&2
fi
if ! grep -q "type=\"${TOPIC_NAME}_republisher" $LAUNCH_FILE ; then
echo -n -e "\tAdding entry to $LAUNCH_FILE ..." >&2
head -n-2 $LAUNCH_FILE > tmp
echo -e "\t\t<node pkg=\"lcm_to_ros\" type=\"${TOPIC_NAME}_republisher\" respawn=\"false\" name=\"${TOPIC_NAME}_republisher\" output=\"screen\"/>" >> tmp
tail -n2 $LAUNCH_FILE >> tmp
mv tmp $LAUNCH_FILE
echo " done." >&2
fi
done < "$1"