Skip to content

Commit

Permalink
[Description/Bringup] Update scripts and templates for generating *.l…
Browse files Browse the repository at this point in the history
…aunch.xml files (#160)

* XML launch generator-> robot description + bringup
* Add options for choosing the generated files

---------

Co-authored-by: Dr. Denis <[email protected]>
Co-authored-by: Manuel M <[email protected]>
  • Loading branch information
3 people authored Feb 21, 2024
1 parent b5ec9da commit 540cb5f
Show file tree
Hide file tree
Showing 6 changed files with 247 additions and 47 deletions.
76 changes: 50 additions & 26 deletions scripts/setup-robot-bringup.bash
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
usage="setup-robot-bringup ROBOT_NAME DESCRIPTION_PKG_NAME"

# Load Framework defines
script_own_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" > /dev/null && pwd )"
script_own_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null && pwd)"
source $script_own_dir/../setup.bash
check_and_set_ros_distro_and_version "${ROS_DISTRO}"

Expand All @@ -31,22 +31,45 @@ if [ -z "$DESCR_PKG_NAME" ]; then
print_and_exit "ERROR: You should provide description package name! Nothing to do 😯" "$usage"
fi

echo "Which launchfiles should be added? Choose from the following options:"
echo "1) xml"
echo "2) python"
echo "3) both"

read -p "Enter your choice:" choice

LAUNCH_FILE_TYPES=()

case $choice in
1)
LAUNCH_FILE_TYPES+=(".xml")
;;
2)
LAUNCH_FILE_TYPES+=(".py")
;;
3)
LAUNCH_FILE_TYPES+=(".xml" ".py")
;;
*)
print_and_exit "Invalid choice. Exiting."
;;
esac

if [ ! -f "package.xml" ]; then
print_and_exit "ERROR: 'package.xml' not found. You should execute this script at the top level of your package folder. Nothing to do 😯" "$usage"
fi
PKG_NAME="$(grep -Po '(?<=<name>).*?(?=</name>)' package.xml | sed -e 's/[[:space:]]//g')"


echo ""
echo -e "${TERMINAL_COLOR_USER_NOTICE}ATTENTION: Setting up bringup configuration for robot '$ROBOT_NAME' in package '$PKG_NAME' in folder '`pwd`' with robot description package '$DESCR_PKG_NAME'.${TERMINAL_COLOR_NC}"
echo -e "${TERMINAL_COLOR_USER_NOTICE}ATTENTION: Setting up bringup configuration for robot '$ROBOT_NAME' in package '$PKG_NAME' in folder '$(pwd)' with robot description package '$DESCR_PKG_NAME'.${TERMINAL_COLOR_NC}"
echo -e "${TERMINAL_COLOR_USER_CONFIRMATION}If correct press <ENTER>, otherwise <CTRL>+C and start the script again from the package folder and/or with correct robot name.${TERMINAL_COLOR_NC}"
read

# Remove include and src folders - in this package should be no source
RM_FOLDERS=("include" "src")

for FOLDER in "${RM_FOLDERS[@]}"; do
if [[ -d $FOLDER && ! "$(ls -A $FOLDER )" ]]; then
if [[ -d $FOLDER && ! "$(ls -A $FOLDER)" ]]; then
rm -r $FOLDER
fi
done
Expand All @@ -62,29 +85,33 @@ cp -n $ROS2_CONTROL_TEMPLATES/robot_controllers.yaml $ROBOT_CONTROLLERS_YAML
cp -n $ROS2_CONTROL_TEMPLATES/test_goal_publishers_config.yaml $ROBOT_FPC_PUB_YAML

# Copy launch files
ROBOT_LAUNCH="launch/${ROBOT_NAME}.launch.py"
TEST_FWD_POS_CTRL_LAUNCH="launch/test_forward_position_controller.launch.py"
TEST_JTC_LAUNCH="launch/test_joint_trajectory_controller.launch.py"
cp -n $ROS2_CONTROL_TEMPLATES/robot_ros2_control.launch.py ${ROBOT_LAUNCH}
cp -n $ROS2_CONTROL_TEMPLATES/test_forward_position_controller.launch.py $TEST_FWD_POS_CTRL_LAUNCH
cp -n $ROS2_CONTROL_TEMPLATES/test_joint_trajectory_controller.launch.py $TEST_JTC_LAUNCH


# sed all needed files
FILES_TO_SED=($ROBOT_LAUNCH $TEST_FWD_POS_CTRL_LAUNCH $TEST_JTC_LAUNCH)

for SED_FILE in "${FILES_TO_SED[@]}"; do
sed -i "s/\\\$PKG_NAME\\\$/${PKG_NAME}/g" $SED_FILE
sed -i "s/\\\$RUNTIME_CONFIG_PKG_NAME\\\$/${PKG_NAME}/g" $SED_FILE
sed -i "s/\\\$ROBOT_NAME\\\$/${ROBOT_NAME}/g" $SED_FILE
sed -i "s/\\\$DESCR_PKG_NAME\\\$/${DESCR_PKG_NAME}/g" $SED_FILE
for file_type in "${LAUNCH_FILE_TYPES[@]}"; do
# Construct the file paths
ROBOT_LAUNCH="launch/${ROBOT_NAME}.launch${file_type}"
TEST_FWD_POS_CTRL_LAUNCH="launch/test_forward_position_controller.launch${file_type}"
TEST_JTC_LAUNCH="launch/test_joint_trajectory_controller.launch${file_type}"

# Copy the templates to the destination with the specified file type
cp -n "$ROS2_CONTROL_TEMPLATES/robot_ros2_control.launch${file_type}" "${ROBOT_LAUNCH}"
cp -n "$ROS2_CONTROL_TEMPLATES/test_forward_position_controller.launch${file_type}" "${TEST_FWD_POS_CTRL_LAUNCH}"
cp -n "$ROS2_CONTROL_TEMPLATES/test_joint_trajectory_controller.launch${file_type}" "${TEST_JTC_LAUNCH}"

# sed all needed files
FILES_TO_SED=($ROBOT_LAUNCH $TEST_FWD_POS_CTRL_LAUNCH $TEST_JTC_LAUNCH)

for SED_FILE in "${FILES_TO_SED[@]}"; do
sed -i "s/\\\$PKG_NAME\\\$/${PKG_NAME}/g" $SED_FILE
sed -i "s/\\\$RUNTIME_CONFIG_PKG_NAME\\\$/${PKG_NAME}/g" $SED_FILE
sed -i "s/\\\$ROBOT_NAME\\\$/${ROBOT_NAME}/g" $SED_FILE
sed -i "s/\\\$DESCR_PKG_NAME\\\$/${DESCR_PKG_NAME}/g" $SED_FILE
done
done

# package.xml: Add dependencies if they not exist
DEP_PKGS=("xacro" "rviz2" "ros2_controllers_test_nodes" "robot_state_publisher" "joint_trajectory_controller" "joint_state_broadcaster" "forward_command_controller" "controller_manager" "$DESCR_PKG_NAME")

for DEP_PKG in "${DEP_PKGS[@]}"; do
if `grep -q $DEP_PKG package.xml`; then
if $(grep -q $DEP_PKG package.xml); then
echo "'$DEP_PKG' is already listed in package.xml"
else
append_to_string="<buildtool_depend>ament_cmake<\/buildtool_depend>"
Expand All @@ -98,19 +125,16 @@ sed -i "s/$prepend_to_string/install\(\\n DIRECTORY config launch\\n DESTINATI

# extend README with general instructions
if [ -f README.md ]; then
cat $ROS2_CONTROL_TEMPLATES/append_to_README.md >> README.md
cat $ROS2_CONTROL_TEMPLATES/append_to_README.md >>README.md
sed -i "s/\\\$PKG_NAME\\\$/${PKG_NAME}/g" README.md
sed -i "s/\\\$ROBOT_NAME\\\$/${ROBOT_NAME}/g" README.md
sed -i "s/\\\$DESCR_PKG_NAME\\\$/${DESCR_PKG_NAME}/g" $SED_FILE
fi

# TODO: Add license checks

git add .
# git commit -m "RosTeamWS: Bringup files for $ROBOT_NAME generated."

# Compile and add new package the to the path
compile_and_source_package $PKG_NAME

echo ""
echo -e "${TERMINAL_COLOR_USER_NOTICE}FINISHED: You can test the configuration by executing 'ros2 launch $PKG_NAME ${ROBOT_NAME}.launch.py'${TERMINAL_COLOR_NC}"
echo -e "${TERMINAL_COLOR_USER_NOTICE}FINISHED: You can test the configuration by executing 'ros2 launch $PKG_NAME ${ROBOT_NAME}.launch${LAUNCH_FILE_TYPES[*]}'${TERMINAL_COLOR_NC}"
65 changes: 44 additions & 21 deletions scripts/setup-robot-description.bash
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
# See the License for the specific language governing permissions and
# limitations under the License.

usage="setup-robot-description ROBOT_NAME"
usage="setup-robot-description ROBOT_NAME LAUNCH_FILE_TYPE"

# Load Framework defines
script_own_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" > /dev/null && pwd )"
script_own_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null && pwd)"
source $script_own_dir/../setup.bash
check_and_set_ros_distro_and_version ${ROS_DISTRO}

Expand All @@ -28,13 +28,37 @@ if [ -z "$ROBOT_NAME" ]; then
print_and_exit "ERROR: You should provide robot name! Nothing to do 😯" "$usage"
fi

echo "Which launchfiles should be added? Choose from the following options:"
echo "1) xml"
echo "2) python"
echo "3) both"

read -p "Enter your choice:" choice

LAUNCH_FILE_TYPES=()

case $choice in
1)
LAUNCH_FILE_TYPES+=(".xml")
;;
2)
LAUNCH_FILE_TYPES+=(".py")
;;
3)
LAUNCH_FILE_TYPES+=(".xml" ".py")
;;
*)
print_and_exit "Invalid choice. Exiting."
;;
esac

if [ ! -f "package.xml" ]; then
print_and_exit "ERROR: 'package.xml' not found. You should execute this script at the top level of your package folder. Nothing to do 😯" "$usage"
fi
PKG_NAME="$(grep -Po '(?<=<name>).*?(?=</name>)' package.xml | sed -e 's/[[:space:]]//g')"

echo ""
echo -e "${TERMINAL_COLOR_USER_NOTICE}ATTENTION: Setting up description configuration for robot '$ROBOT_NAME' in package '$PKG_NAME' in folder '`pwd`'.${TERMINAL_COLOR_NC}"
echo -e "${TERMINAL_COLOR_USER_NOTICE}ATTENTION: Setting up description configuration for robot '$ROBOT_NAME' in package '$PKG_NAME' in folder '$(pwd)'.${TERMINAL_COLOR_NC}"
echo -e "${TERMINAL_COLOR_USER_CONFIRMATION}If correct press <ENTER>, otherwise <CTRL>+C and start the script again from the package folder and/or with correct robot name.${TERMINAL_COLOR_NC}"
read

Expand All @@ -60,10 +84,20 @@ cp -n "$ROBOT_DESCRIPTION_TEMPLATES/robot.urdf.xacro" $ROBOT_URDF_XACRO
cp -n "$ROBOT_DESCRIPTION_TEMPLATES/robot_macro.xacro" $ROBOT_MACRO
cp -n "$ROBOT_DESCRIPTION_TEMPLATES/robot_macro.ros2_control.xacro" $ROBOT_MACRO_ROS2_CONTROL

# Copy launch.py file for testing the description
mkdir -p launch
VIEW_ROBOT_LAUNCH="launch/view_${ROBOT_NAME}.launch.py"
cp -n "$ROBOT_DESCRIPTION_TEMPLATES/view_robot.launch.py" $VIEW_ROBOT_LAUNCH
# Copy launch files for testing the description
for file_type in "${LAUNCH_FILE_TYPES[@]}"; do
mkdir -p launch
VIEW_ROBOT_LAUNCH="launch/view_${ROBOT_NAME}.launch${file_type}"
cp -n "$ROBOT_DESCRIPTION_TEMPLATES/view_robot.launch${file_type}" $VIEW_ROBOT_LAUNCH

# sed all needed files
FILES_TO_SED=($ROBOT_URDF_XACRO $ROBOT_MACRO $ROBOT_MACRO_ROS2_CONTROL $VIEW_ROBOT_LAUNCH)

for SED_FILE in "${FILES_TO_SED[@]}"; do
sed -i "s/\\\$PKG_NAME\\\$/${PKG_NAME}/g" $SED_FILE
sed -i "s/\\\$ROBOT_NAME\\\$/${ROBOT_NAME}/g" $SED_FILE
done
done

# Copy YAML files
mkdir -p config
Expand All @@ -74,19 +108,11 @@ mkdir -p rviz
ROBOT_RVIZ="rviz/${ROBOT_NAME}.rviz"
cp -n "$ROBOT_DESCRIPTION_TEMPLATES/robot.rviz" $ROBOT_RVIZ

# sed all needed files
FILES_TO_SED=($ROBOT_URDF_XACRO $ROBOT_MACRO $ROBOT_MACRO_ROS2_CONTROL $VIEW_ROBOT_LAUNCH)

for SED_FILE in "${FILES_TO_SED[@]}"; do
sed -i "s/\\\$PKG_NAME\\\$/${PKG_NAME}/g" $SED_FILE
sed -i "s/\\\$ROBOT_NAME\\\$/${ROBOT_NAME}/g" $SED_FILE
done

# Add dependencies if they not exist
DEP_PKGS=("xacro" "rviz2" "robot_state_publisher" "joint_state_publisher_gui")

for DEP_PKG in "${DEP_PKGS[@]}"; do
if `grep -q $DEP_PKG package.xml`; then
if $(grep -q $DEP_PKG package.xml); then
echo "'$DEP_PKG' is already listed in package.xml"
else
append_to_string="<buildtool_depend>ament_cmake<\/buildtool_depend>"
Expand All @@ -100,18 +126,15 @@ sed -i "s/$preppend_to_string/install\(\\n DIRECTORY config launch meshes rviz

# extend README with general instructions
if [ -f README.md ]; then
cat $ROBOT_DESCRIPTION_TEMPLATES/append_to_README.md >> README.md
cat $ROBOT_DESCRIPTION_TEMPLATES/append_to_README.md >>README.md
sed -i "s/\\\$PKG_NAME\\\$/${PKG_NAME}/g" README.md
sed -i "s/\\\$ROBOT_NAME\\\$/${ROBOT_NAME}/g" README.md
fi

#TODO: Set license

git add .
git commit -m "RosTeamWS: Description files for $ROBOT_NAME generated."

# Compile and add new package the to the path
compile_and_source_package $PKG_NAME

echo ""
echo -e "${TERMINAL_COLOR_USER_NOTICE}FINISHED: You can test the configuration by executing 'ros2 launch $PKG_NAME view_${ROBOT_NAME}.launch.py'${TERMINAL_COLOR_NC}"
echo -e "${TERMINAL_COLOR_USER_NOTICE}FINISHED: You can test the configuration by executing 'ros2 launch $PKG_NAME view_${ROBOT_NAME}.launch${LAUNCH_FILE_TYPES[*]}'${TERMINAL_COLOR_NC}"
20 changes: 20 additions & 0 deletions templates/robot_description/view_robot.launch.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<launch>
<arg name="description_package"
default="$PKG_NAME$"
description="Description package of the $ROBOT_NAME$. Usually the argument is not set,
it enables use of a custom description."/>
<arg name="prefix"
default=""
description="Prefix of the joint names, useful for multi-robot setup. If changed than also joint
names in the controllers' configuration have to be updated."/>

<let name="robot_description_content" value="$(command '$(find-exec xacro) $(find-pkg-share $(var description_package))/urdf/$ROBOT_NAME$.urdf.xacro prefix:=$(var prefix)')" />

<node pkg="joint_state_publisher_gui" exec="joint_state_publisher_gui"/>

<node pkg="robot_state_publisher" exec="robot_state_publisher" output="both">
<param name="robot_description" value="$(var robot_description_content)" />
</node>

<node pkg="rviz2" exec="rviz2" output="log" args="-d $(find-pkg-share $(var description_package))/rviz/$ROBOT_NAME$.rviz"/>
</launch>
71 changes: 71 additions & 0 deletions templates/ros2_control/robot_ros2_control.launch.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<!--
Copyright (c) 2024, Stogl Robotics Consulting UG (haftungsbeschränkt)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Source of this file are templates in https://github.com/StoglRobotics/ros_team_workspace repository.
Authors: Riyan Jose, Manuel Muth, Dr. Denis
-->

<launch>
<arg name="runtime_config_package"
default="$RUNTIME_CONFIG_PKG_NAME$"
description="Package with the controller's configuration in 'config' folder. Usually the argument is not set, it enables use of a custom setup."/>
<arg name="controllers_file"
default="$ROBOT_NAME$_controllers"
description="YAML file (without extension) with the controllers configuration."/>
<arg name="description_package"
default="$DESCR_PKG_NAME$"
description="Description package with robot URDF/xacro files. Usually the argument is not set, it enables use of a custom description."/>
<arg name="description_file"
default="$ROBOT_NAME$"
description="URDF/XACRO description file (without extension) with the robot."/>
<arg name="prefix"
default=""
description="Prefix of the joint names, useful for multi-robot setup. If changed than also joint names in the controllers' configuration have to be updated."/>
<arg name="use_mock_hardware"
default="true"
description="Start robot with mock hardware mirroring command to its states."/>
<arg name="mock_sensor_commands"
default="false"
description="Enable mock command interfaces for sensors used for simple simulations. Used only if 'use_mock_hardware' parameter is true."/>
<arg name="robot_controller"
default="forward_position_controller"
description="Robot controller to start. Choices are: [forward_position_controller, joint_trajectory_controller]."/>

<let name="robot_description_content" value="$(command '$(find-exec xacro) $(find-pkg-share $(var description_package))/urdf/$(var description_file).urdf.xacro prefix:=$(var prefix) use_mock_hardware:=$(var use_mock_hardware) mock_sensor_commands:=$(var mock_sensor_commands)')"/>


<!--robot_state_pub_node-->
<node pkg="robot_state_publisher" exec="robot_state_publisher" output="both">
<param name="robot_description" value="$(var robot_description_content)"/>
</node>

<!--control_node-->
<node pkg="controller_manager" exec="ros2_control_node" output="both">
<param name="robot_description" value="$(var robot_description_content)" />
<param from="$(find-pkg-share $(var runtime_config_package))/config/$(var controllers_file).yaml"/>
</node>

<!--rviz_node-->
<node pkg="rviz2" exec="rviz2" output="log" args="-d $(find-pkg-share $(var description_package))/rviz/$ROBOT_NAME$.rviz"/>

<!--joint_state_braodcaster_spawner-->
<node pkg="controller_manager" exec="spawner" args="joint_state_broadcaster"/>

<!--robot_controller_spawner-->
<node pkg="controller_manager" exec="spawner" args="$(var robot_controller)"/>

</launch>
31 changes: 31 additions & 0 deletions templates/ros2_control/test_forward_position_controller.launch.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<!--
Copyright (c) 2024, Stogl Robotics Consulting UG (haftungsbeschränkt)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Source of this file are templates in https://github.com/StoglRobotics/ros_team_workspace repository.
Authors: Riyan Jose, Dr. Denis
-->

<launch>

<node pkg="ros2_controllers_test_nodes"
exec="publisher_forward_position_controller"
name="publisher_forward_position_controller"
output="screen">
<param from="$(find-pkg-share '$PKG_NAME$')/config/test_goal_publishers_config.yaml"/>
</node>

</launch>
Loading

0 comments on commit 540cb5f

Please sign in to comment.