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

Add ROS yaml-cpp example for blogpost #168

Merged
merged 14 commits into from
Nov 28, 2024
Merged
8 changes: 7 additions & 1 deletion .github/workflows/ros-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,14 @@ jobs:
python3 -m pip install git+https://github.com/conan-io/conan.git
chmod +x ./examples/tools/ros/rosenv/workspace/test_ros.sh

- name: Run example with Conan from repo
- name: Run printer example
shell: bash
run: |
cd examples/tools/ros/rosenv/workspace
./test_ros.sh

- name: Run navigator example
shell: bash
run: |
cd examples/tools/ros/rosenv/navigation_ws
./test_ros.sh
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,6 @@ examples/libraries/libcurl/ascii_art_color/x64/Release/*

examples/tools/ros/rosenv/workspace/install/
examples/tools/ros/rosenv/workspace/log/

examples/tools/ros/rosenv/navigation_ws/install/
examples/tools/ros/rosenv/navigation_ws/log/
3 changes: 2 additions & 1 deletion examples/tools/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@

### [tools.ros](ros)

- Build [ROS packages inside their workspace](ros/rosenv/workspace/) using dependencies from Conan Center and consuming them also as transitive dependencies.
- Build [ROS packages inside their workspace](ros/rosenv/workspace) using the [fmt library from Conan Center](https://conan.io/center/recipes/fmt) and consuming them also as transitive dependencies.
- Build a [ROS navigation package](ros/rosenv/navigation_ws) that sends goals to a mobile robot from a YAML file using the [yaml-cpp library from Conan Center](https://conan.io/center/recipes/yaml-cpp).
10 changes: 10 additions & 0 deletions examples/tools/ros/rosenv/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
FROM osrf/ros:humble-desktop
RUN apt-get update && apt-get install -y \
curl \
python3-pip \
git \
ros-humble-nav2-msgs \
&& rm -rf /var/lib/apt/lists/*
RUN pip3 install --upgrade pip && pip3 install conan
RUN conan profile detect
CMD ["bash"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
cmake_minimum_required(VERSION 3.8)
project(navigation_package)

if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
add_compile_options(-Wall -Wextra -Wpedantic)
endif()

# ROS dependencies
find_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED)
find_package(rclcpp_action REQUIRED)
find_package(nav2_msgs REQUIRED)

# Conan dependencies
find_package(yaml-cpp REQUIRED)

add_executable(navigator src/navigator.cpp)

target_compile_features(navigator PUBLIC c_std_99 cxx_std_17) # Require C99 and C++17
ament_target_dependencies(navigator rclcpp rclcpp_action nav2_msgs yaml-cpp)

install(TARGETS navigator
DESTINATION lib/${PROJECT_NAME})

ament_package()
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[requires]
yaml-cpp/0.8.0

[generators]
CMakeDeps
CMakeToolchain
ROSEnv
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
locations:
- name: "Kitchen"
x: 3.5
y: 2.0
- name: "Living Room"
x: 1.0
y: -1.0
- name: "Bedroom"
x: -2.0
y: 1.5
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>navigation_package</name>
<version>0.0.0</version>
<description>Example using yaml-cpp to send navigation goals</description>
<maintainer email="[email protected]">Conan</maintainer>
<license>MIT</license>

<buildtool_depend>ament_cmake</buildtool_depend>

<depend>rclcpp</depend>
<depend>rclcpp_action</depend>
<depend>nav2_msgs</depend>

<export>
<build_type>ament_cmake</build_type>
</export>
</package>
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#include <string>
#include <vector>

#include <rclcpp/rclcpp.hpp>
#include <nav2_msgs/action/navigate_to_pose.hpp>
#include <rclcpp_action/rclcpp_action.hpp>

#include <yaml-cpp/yaml.h>

using NavigateToPose = nav2_msgs::action::NavigateToPose;


class YamlNavigationNode : public rclcpp::Node {
public:
YamlNavigationNode(const std::string &yaml_file_path) : Node("yaml_navigation_node") {
// Create action client
action_client_ = rclcpp_action::create_client<NavigateToPose>(this, "navigate_to_pose");

// Read locations from YAML file
RCLCPP_INFO(this->get_logger(), "Reading locations from YAML...");
if (!loadLocations(yaml_file_path)) {
RCLCPP_ERROR(this->get_logger(), "Failed to load locations.");
return;
}

if (locations_.empty()) {
RCLCPP_ERROR(this->get_logger(), "No locations found in the YAML file.");
return;
}

sendAllGoals();
}

private:
struct Location {
std::string name;
double x;
double y;
};

std::vector<Location> locations_;
rclcpp_action::Client<NavigateToPose>::SharedPtr action_client_;

bool loadLocations(const std::string &file_path) {
try {
YAML::Node yaml_file = YAML::LoadFile(file_path);
for (const auto &node : yaml_file["locations"]) {
Location location;
location.name = node["name"].as<std::string>();
location.x = node["x"].as<double>();
location.y = node["y"].as<double>();
locations_.emplace_back(location);
}
return true;
} catch (const std::exception &e) {
RCLCPP_ERROR(this->get_logger(), "Error parsing YAML: %s", e.what());
return false;
}
}

void sendAllGoals() {
for (const auto &location : locations_) {
RCLCPP_INFO(this->get_logger(), "Sending %s goal (%.2f, %.2f) to robot", location.name.c_str(), location.x, location.y);

auto goal_msg = NavigateToPose::Goal();
goal_msg.pose.header.frame_id = "map";
goal_msg.pose.header.stamp = this->now();
goal_msg.pose.pose.position.x = location.x;
goal_msg.pose.pose.position.y = location.y;
goal_msg.pose.pose.orientation.w = 1.0;

action_client_->async_send_goal(goal_msg);
}

RCLCPP_INFO(this->get_logger(), "All goals have been sent.");
}
};


int main(int argc, char **argv) {
rclcpp::init(argc, argv);

if (argc < 2) {
RCLCPP_ERROR(rclcpp::get_logger("yaml_navigation_node"), "Usage: yaml_navigation_node <yaml_file_path>");
return 1;
}

std::string yaml_file_path = argv[1];
std::make_shared<YamlNavigationNode>(yaml_file_path);

rclcpp::shutdown();
return 0;
}
10 changes: 10 additions & 0 deletions examples/tools/ros/rosenv/navigation_ws/test_ros.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
source /opt/ros/humble/setup.bash
rosdep init
rosdep update
rosdep install --from-paths navigation_package/
conan profile detect --force
conan install navigation_package/conanfile.txt --build=missing --output-folder install/conan
source install/conan/conanrosenv.sh
colcon build --packages-select navigation_package
source install/setup.bash
ros2 run navigation_package navigator navigation_package/locations.yaml