Skip to content

Commit

Permalink
Replace Makefile with shell scripts for canadarm2 demo
Browse files Browse the repository at this point in the history
Related to space-ros/space-ros#178
 - Revert the purpose of makefiles
 - Add a new shell script for building and running the demos
 - Update README.md for shell script usage
  • Loading branch information
franklinselva committed Aug 28, 2024
1 parent f64c755 commit bd5e472
Show file tree
Hide file tree
Showing 4 changed files with 184 additions and 66 deletions.
60 changes: 0 additions & 60 deletions canadarm2/Makefile
Original file line number Diff line number Diff line change
@@ -1,60 +0,0 @@
# Define variables
DOCKER_IMAGE = osrf/space-ros:canadarm_demo
WORKSPACE = ${HOME}/canadarm_ws
LOCAL_WORKSPACE = $(shell pwd)
SHELL := /bin/bash
XTERM_CONFIG = -bg black -fg white -fa 'Monospace' -fs 11

# Help target to describe each target
.PHONY: help
help:
@echo "CanadArm2 Makefile"
@echo " make build - Build the Docker image and the Gazebo workspace locally"
@echo " make build-docker - Build the Docker image"
@echo " make build-gazebo - Build the Gazebo workspace locally"
@echo " make run-gazebo - Run the CanadArm2 Gazebo simulation locally"
@echo " make run-demo - Run the CanadArm2 demo within Docker"
@echo " make clean - Clean the local workspace"
@echo " make all - Build and run everything"

# Build all
.PHONY: build
build: build-docker build-gazebo

# Build the Docker image
.PHONY: build-docker
build-docker:
docker build -t $(DOCKER_IMAGE) .

# Run the CanadArm Gazebo simulation locally
.PHONY: run-gazebo
run-gazebo:
xterm $(XTERM_CONFIG) -T 'CanadArm2 Gazebo' -e 'source /opt/ros/${ROS_DISTRO}/setup.bash \
&& source $(LOCAL_WORKSPACE)/install/setup.bash \
&& ros2 launch canadarm_gazebo canadarm.launch.py' &

# Build the Gazebo workspace locally
.PHONY: build-gazebo
build-gazebo:
@source /opt/ros/${ROS_DISTRO}/setup.bash && \
rosdep install --from-paths canadarm_gazebo --ignore-src -r -y && \
rosdep install --from-paths canadarm_description --ignore-src -r -y && \
colcon build --symlink-install --base-paths $(LOCAL_WORKSPACE) --install-base $(LOCAL_WORKSPACE)/install \
--cmake-args -DCMAKE_BUILD_TYPE=Release -DCMAKE_EXPORT_COMPILE_COMMANDS=ON \
--packages-select canadarm_description canadarm_gazebo

# Run the CanadArm demo within Docker
.PHONY: run-docker
run-docker:
xterm $(XTERM_CONFIG) -T 'CanadArm2 Demo' -e "docker run -it --rm \
-e RMW_IMPLEMENTATION=rmw_cyclonedds_cpp \
$(DOCKER_IMAGE) \
bash -c 'source ~/.bashrc && ros2 launch canadarm_demo canadarm.launch.py'" &

.PHONY: clean
clean:
rm -rf $(LOCAL_WORKSPACE)/install $(LOCAL_WORKSPACE)/log $(LOCAL_WORKSPACE)/build

# Build and run everything
.PHONY: run
run: build run-gazebo run-docker
28 changes: 24 additions & 4 deletions canadarm2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,33 @@ To start the demo, there are few dependencies that need to be installed. The fol
```bash
cd canadarm2
```
3. To build and run the demo, we use `Makefile`. Run the following command to build the docker image and run the container.
3. To build and run the demo, we use `./build.sh`. There are different subcommands to build and run the demo. You can see the list of available commands using the following command.
```bash
# To see the list of available commands
make help
./build.sh help
```
4. To list the avaiable demos for the canadarm2, you can use the following command.
```bash
# To list the available demos
./build.sh list
# To build and run the demo
make run
# Available demos:
# - control-demo
# Available subcommands:
# - build, clean, run
```
5. To build the demo, you can use the following command.
```bash
# To build all the demos for the canadarm2
./build.sh build
# To build a selected demo, for example control-demo
./build.sh build control-demo
```
6. To run the demo, you can use the following command.
```bash
# To run the demo
./build.sh run control-demo
```

This will start the demo in one terminal and gazebo in another terminal. To control the canadarm2, we provide ros2 services for the demo. You can control the rover using the following services.
Expand Down
160 changes: 160 additions & 0 deletions canadarm2/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
#!/bin/bash

set -e

# Define variables
DOCKER_IMAGE="osrf/space-ros:canadarm_demo"
LOCAL_WORKSPACE=$(pwd)
SHELL="/bin/bash"
# shellcheck disable=SC2089
XTERM_CONFIG="-bg black -fg white -fa 'Monospace' -fs 11"

# Define packages
SIM_PACKAGES="canadarm_description canadarm_gazebo"
CONTROL_PACKAGE="canadarm_moveit_config canadarm_demo"
DEMO_PACKAGES="$CONTROL_PACKAGE"
AVAIABLE_DEMOS="control-demo"

####################################################################################################
# High-level functions
####################################################################################################
# Help function to describe each target
help() {
echo "CanadArm2 Bash Script"
echo " ./build.sh list - List all available demos"
echo " ./build.sh build - Build all the demo components. This will include all supported demos and the Docker image"
echo " ./build.sh clean - Clean the local workspace"
echo " ./build.sh build control-demo - Build the control demo"
echo " ./build.sh run control-demo - Build and run the CanadArm2 demo"
exit 0
}

# Build all
build() {
build-control-demo
}

# Clean the local workspace
clean() {
rm -rf "$LOCAL_WORKSPACE"/install "$LOCAL_WORKSPACE"/log "$LOCAL_WORKSPACE"/build
}

check_docker() {
if ! command -v docker &>/dev/null; then
echo "Docker is not installed. Please install Docker to continue."
exit 1
fi
}

check_xterm() {
if ! command -v xterm &>/dev/null; then
echo "xterm is not installed. Please install xterm to continue."
echo "On Ubuntu, you can install xterm with 'sudo apt install xterm'."
exit 1
fi
}

check_ros2() {
if ! command -v ros2 &>/dev/null; then
echo "ROS 2 is not installed. Please install ROS 2 to continue."
exit 1
fi
}

####################################################################################################
# Low-level functions
####################################################################################################
# Build the Docker image
build-docker() {
docker build -t "$DOCKER_IMAGE" .
}

# Run the CanadArm Gazebo simulation locally
run-gazebo() {
# shellcheck disable=SC2090
# shellcheck disable=SC2086
xterm $XTERM_CONFIG -T 'CanadArm2 Gazebo' -e "source /opt/ros/\${ROS_DISTRO}/setup.bash \
&& source $LOCAL_WORKSPACE/install/setup.bash \
&& ros2 launch canadarm_gazebo canadarm.launch.py" &
}

# Build the Gazebo workspace locally
build-gazebo() {
# shellcheck source=/opt/ros/${ROS_DISTRO}/setup.bash
# shellcheck disable=SC1091
source /opt/ros/"${ROS_DISTRO}"/setup.bash &&
rosdep install --from-paths . -r -y --skip-keys "$DEMO_PACKAGES" &&
colcon build --symlink-install --base-paths "$LOCAL_WORKSPACE" --install-base "$LOCAL_WORKSPACE"/install \
--cmake-args -DCMAKE_BUILD_TYPE=Release -DCMAKE_EXPORT_COMPILE_COMMANDS=ON \
--packages-select $SIM_PACKAGES
}

####################################################################################################
# Control demo
####################################################################################################
build-control-demo() {
build-docker
build-gazebo
}

run-control-demo() {
# shellcheck disable=SC2090
# shellcheck disable=SC2086
xterm $XTERM_CONFIG -T 'CanadArm2 Demo' -e "docker run -it --rm \
-e RMW_IMPLEMENTATION=rmw_cyclonedds_cpp \
$DOCKER_IMAGE \
bash -c 'source ~/.bashrc && ros2 launch canadarm_demo canadarm.launch.py'" &
}

####################################################################################################
# Main script logic to call the appropriate function
####################################################################################################
case "$1" in
help)
help
;;
list)
echo "Available demos:"
for demo in $AVAIABLE_DEMOS; do
echo " - $demo"
done
echo "Available subcommands:"
echo " - build, clean, run"
exit 0
;;
clean)
clean
;;
build)
check_ros2
check_docker
case "$2" in
control-demo)
build-control-demo
;;
*)
build
;;
esac
;;
run)
check_xterm
check_ros2
check_docker

run-gazebo
case "$2" in
control-demo)
run-control-demo
;;
*)
echo "Invalid target. 'run" "$2" "' is not a valid demo. Try './build.sh list' to see available demos."
exit 1
;;
esac
;;
*)
echo "Invalid target. Use './build.sh help' to see available subcommands."
exit 1
;;
esac
2 changes: 0 additions & 2 deletions canadarm2/canadarm_moveit_config/package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,13 @@
<exec_depend>moveit_configs_utils</exec_depend>
<exec_depend>moveit_ros_move_group</exec_depend>
<exec_depend>moveit_ros_visualization</exec_depend>
<exec_depend>moveit_ros_warehouse</exec_depend>
<exec_depend>moveit_setup_assistant</exec_depend>
<exec_depend>robot_state_publisher</exec_depend>
<exec_depend>rviz2</exec_depend>
<exec_depend>rviz_common</exec_depend>
<exec_depend>rviz_default_plugins</exec_depend>
<exec_depend>simulation</exec_depend>
<exec_depend>tf2_ros</exec_depend>
<exec_depend>warehouse_ros_mongo</exec_depend>
<exec_depend>xacro</exec_depend>


Expand Down

0 comments on commit bd5e472

Please sign in to comment.