Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example showing usage of resource from an external ROS package in Drake #1

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions examples/external_ros_resources/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Using External ROS Resources in Drake

This example shows how to use external ROS resources, such as URDF files in Drake.
It uses the [`ament_cmake`](https://index.ros.org/doc/ros2/Tutorials/Ament-CMake-Documentation/)
build system and [`colcon`](https://colcon.readthedocs.io) command line tool,
with an installed instance of the Drake [binary packages](https://drake.mit.edu/from_binary.html).

## Instructions

To use `ament_cmake` and `colcon` from the ROS 2 Dashing package archive, install
the required packages and configure your environment as follows:
```
sudo ../../scripts/setup/linux/ubuntu/bionic/install_prereqs --ros-dashing
source /opt/ros/dashing/setup.bash
```

To build the `drake_example_pendulum` example:
```
colcon build --cmake-args "-DCMAKE_PREFIX_PATH=/path/to/drake;$CMAKE_PREFIX_PATH"
```

*If the Drake binary package is installed to `/opt/drake`, you may omit the
`--cmake-args <args>` argument.*


To run the `drake_example_pendulum` example, source the workspace and run the
executable:

```
source install/setup.bash
drake_example_pendulum
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Copyright (c) 2021, Massachusetts Institute of Technology.
# Copyright (c) 2021, Toyota Research Institute.
# All rights reserved.
#
# 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.

cmake_minimum_required(VERSION 3.10)
project(drake_example_pendulum)

set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

find_package(ament_cmake CONFIG REQUIRED)
find_package(drake CONFIG REQUIRED PATHS /opt/drake)

add_executable(${PROJECT_NAME}
src/drake_example_pendulum.cc
)

target_link_libraries(${PROJECT_NAME} drake::drake)

install(TARGETS ${PROJECT_NAME}
RUNTIME DESTINATION bin
)

ament_package()
19 changes: 19 additions & 0 deletions examples/external_ros_resources/drake_example_pendulum/package.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0"?>
<?xml-model href="http://download.ros.org/schema/package_format3.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?>
<package format="3">
<name>drake_example_pendulum</name>
<version>0.1.0</version>
<description>Example showing usage of resource from an external ROS package in Drake</description>
<author email="[email protected]">Drake Users</author>
<maintainer email="[email protected]">Drake Users</maintainer>
<license>Apache License 2.0</license>
<url type="website">https://drake.mit.edu</url>
<url type="bugtracker">https://github.com/RobotLocomotion/drake-ros/issues</url>
<url type="repository">https://github.com/RobotLocomotion/drake-ros</url>
<buildtool_depend>ament_cmake</buildtool_depend>
<depend>drake</depend>
<exec_depend>drake_example_pendulum_description</exec_depend>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mind adding <depend>drake</depend>? With that I'm able to build this together with RobotLocomotion/drake in an isolated colcon workspace.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. 5747a7c

<export>
<build_type>ament_cmake</build_type>
</export>
</package>
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*****************************************************************************
* Copyright (c) 2017-2021, Massachusetts Institute of Technology.
* Copyright (c) 2017-2021, Toyota Research Institute.
* All rights reserved.
*
* 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.
*
*****************************************************************************/

#include <iostream>

#include <drake/geometry/drake_visualizer.h>
#include <drake/multibody/parsing/parser.h>
#include <drake/multibody/plant/multibody_plant.h>
#include <drake/multibody/tree/revolute_joint.h>
#include <drake/systems/analysis/simulator.h>
#include <drake/systems/framework/diagram_builder.h>

int main(int argc, char** argv) {
drake::systems::DiagramBuilder<double> builder;
auto [pendulum_plant, scene_graph] =
drake::multibody::AddMultibodyPlantSceneGraph(&builder, 0.0);
auto parser = drake::multibody::Parser(&pendulum_plant, &scene_graph);

// Populate from AMENT_PREFIX_PATH environment variable to find URDF files and
// their resources, such as meshes.
parser.package_map().PopulateFromEnvironment("AMENT_PREFIX_PATH");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@IanTheEngineer @EricCousineau-TRI any interest in in this example?

A lot has changed since this PR opened. I think now we'd put this in a folder in drake_ros_examples. The key piece of information appears to be calling PackageMap::PopuldateFromEnvironment with AMENT_PREFIX_PATH.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that's a very reasonable approach. Let's make this a simple example populating the Drake package_map from the local ROS environment inside drake_ros_examples.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, let's revive this once #207 lands!

const std::string pendulum_desc_package =
"drake_example_pendulum_description";

if (!parser.package_map().Contains(pendulum_desc_package))
{
std::cerr << "The package: '" << pendulum_desc_package
<< "' could not be found. Have you sourced your ROS workspace?"
<< std::endl;
return -1;
}

const std::string package_path =
parser.package_map().GetPath(pendulum_desc_package);
auto model_instance = parser.AddModelFromFile(
package_path + "/urdf/drake_example_pendulum.urdf");

const auto& base_link =
pendulum_plant.GetFrameByName("base_link", model_instance);
// Weld the base_link to the world so the pendulum doesn't fall forever.
pendulum_plant.WeldFrames(pendulum_plant.world_frame(), base_link, {});

pendulum_plant.Finalize();

// The following line can be commented out if Drake visualizer is not needed.
drake::geometry::DrakeVisualizer<double>::AddToBuilder(&builder, scene_graph);

auto diagram = builder.Build();

drake::systems::Simulator<double> simulator(*diagram);
simulator.set_target_realtime_rate(1.0);

drake::systems::Context<double>& pendulum_context =
diagram->GetMutableSubsystemContext(pendulum_plant,
&simulator.get_mutable_context());

drake::VectorX<double> joint_position(1);
joint_position << 0.5;
pendulum_plant.SetPositions(&pendulum_context, joint_position);

simulator.set_monitor([&pendulum = pendulum_plant](const auto& root_context) {
auto &context = pendulum.GetMyContextFromRoot(root_context);
std::cout << fmt::format("{:0.3f}: {}", context.get_time(),
pendulum.GetPositions(context).transpose())
<< std::endl;
return drake::systems::EventStatus::Succeeded();
});

simulator.AdvanceTo(10);
return 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Copyright (c) 2021, Massachusetts Institute of Technology.
# Copyright (c) 2021, Toyota Research Institute.
# All rights reserved.
#
# 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.

cmake_minimum_required(VERSION 3.10)
project(drake_example_pendulum_description)

find_package(ament_cmake CONFIG REQUIRED)

install(DIRECTORY urdf meshes
DESTINATION share/${PROJECT_NAME})

ament_package()
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Blender MTL File: 'drake_base_link.blend'
# Material Count: 2

newmtl Material.001
Ns 323.999994
Ka 1.000000 1.000000 1.000000
Kd 0.800000 0.000000 0.025331
Ks 0.500000 0.500000 0.500000
Ke 0.000000 0.000000 0.000000
Ni 1.000000
d 1.000000
illum 2

newmtl Material.002
Ns 323.999994
Ka 1.000000 1.000000 1.000000
Kd 0.100046 0.420499 0.800000
Ks 0.500000 0.500000 0.500000
Ke 0.000000 0.000000 0.000000
Ni 1.000000
d 1.000000
illum 2
Loading