-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_cws_build_ros_model.sh
135 lines (113 loc) · 3.22 KB
/
create_cws_build_ros_model.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/bin/bash
#
# Copyright 2014-2016 The MathWorks, Inc.
ARCHIVE="$1"
CATKIN_WS="$2"
catkinWorkspaceHelp() {
echo ""
echo "You can create a Catkin workspace as follows:"
echo " mkdir -p ~/catkin_ws/src"
echo " cd ~/catkin_ws/src"
echo " catkin_init_workspace"
}
commandUsage() {
echo "Usage: $(basename $0) ARCHIVE_NAME... CATKIN_WS..."
echo "Extract and build a C++ ROS node generated from a Simulink model."
echo "ARCHIVE_NAME is the name of the TGZ file generated from the Simulink model."
echo "CATKIN_WS is the full path to your ROS Catkin workspace."
echo ""
echo "Example:"
echo " ./$(basename $0) simulinkmodel.tgz ~/catkin_ws"
}
fullUsage() {
commandUsage
catkinWorkspaceHelp
exit
}
toLowerCase() {
echo $1 | tr '[A-Z]' '[a-z]'
}
if [ -z "$1" ] || ([ ! -z "$1" ] && [ "$1" = "-h" ] || [ "$1" = "--help" ]) ; then
fullUsage
exit 0
fi
if [ ! $# -eq 2 ] ; then
echo "Expected two input arguments. Got $#."
fullUsage
exit 1
fi
# Create catkin workspace
CURR_DIR=`pwd`
mkdir -p "$CATKIN_WS"
rm -fr "$CATKIN_WS"
mkdir -p "$CATKIN_WS"/src
cd "$CATKIN_WS"/src
catkin_init_workspace
cd "$CURR_DIR"
# Check Catkin workspace
if [ ! -d "$CATKIN_WS" ] ; then
echo "The catkin workspace directory, "$CATKIN_WS", does not exist."
echo "Enter a valid catkin workspace directory."
catkinWorkspaceHelp
exit 1
fi
# Sanity check for CATKIN workspace
if [ ! -f "$CATKIN_WS"/src/CMakeLists.txt ] ; then
echo "The Catkin workspace directory, "$CATKIN_WS", is not a valid Catkin workspace."
echo "Enter a valid Catkin workspace directory."
catkinWorkspaceHelp
exit 1
fi
# Check Simulink archive
if [ ! -f "$ARCHIVE" ] ; then
echo "The archive, "$ARCHIVE", does not exist."
echo "Enter a valid Simulink model archive (.tgz file)."
echo ""
commandUsage
exit 1
fi
# Enforce that $ARCHIVE ends with .tgz, since the model
# name is derived by stripping off the .tgz extension
if [ ${ARCHIVE: -4} != ".tgz" ] ; then
echo "The archive, "$ARCHIVE", does not have a .tgz extension."
echo "Enter a valid Simulink model archive (.tgz file)."
echo ""
commandUsage
exit 1
fi
# Check if $ARCHIVE is a valid zip file
gzip -t "$ARCHIVE" 2> /dev/null
VALID_ZIP=$?
if [ $VALID_ZIP -ne 0 ] ; then
echo "The archive, "$ARCHIVE", is not a valid .tgz (tar zip) file."
echo ""
commandUsage
exit 1
fi
# Check for one of the standard files generated from Simulink
# (ert_main.cpp)
tar ztf "$ARCHIVE" | grep -q ert_main.cpp 2> /dev/null
VALID_SIMULINK_ARCHIVE=$?
if [ $VALID_SIMULINK_ARCHIVE -ne 0 ] ; then
echo "The archive, "$ARCHIVE", is not a valid Simulink model archive (.tgz file)."
echo ""
commandUsage
exit 1
fi
# $ARCHIVE appears to be valid.
# Extract and build it
MODEL_NAME=$(toLowerCase $(basename "$ARCHIVE" .tgz))
PROJECT_DIR="$CATKIN_WS/src/$MODEL_NAME"
echo "Catkin project directory: $PROJECT_DIR"
# Extract files to catkin project directory
mkdir -p "$PROJECT_DIR"
rm -fr "$PROJECT_DIR"/*
tar -C "$PROJECT_DIR" -xf "$ARCHIVE"
# Ensure that catkin_make will rebuild the executable
touch "$PROJECT_DIR"/*.cpp
# Build the Simulink model as a catkin project
CURR_DIR=`pwd`
cd "$CATKIN_WS"
catkin_make "$MODEL_NAME"_node
cd "$CURR_DIR"
exit 0