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
6 changes: 6 additions & 0 deletions .github/workflows/ros-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,9 @@ jobs:
run: |
cd examples/tools/ros/rosenv/workspace
./test_ros.sh

- name: Run example with Conan from repo 2
shell: bash
run: |
cd examples/tools/ros/rosenv/workspace2
./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/workspace2/install/
examples/tools/ros/rosenv/workspace2/log/
26 changes: 26 additions & 0 deletions examples/tools/ros/rosenv/workspace2/my_package/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
cmake_minimum_required(VERSION 3.8)
project(my_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(geometry_msgs REQUIRED)
find_package(nav2_msgs REQUIRED)

# Conan dependencies
find_package(yaml-cpp REQUIRED)

add_executable(package src/main.cpp)

target_compile_features(package PUBLIC c_std_99 cxx_std_17) # Require C99 and C++17
ament_target_dependencies(package rclcpp rclcpp_action geometry_msgs nav2_msgs yaml-cpp)
danimtb marked this conversation as resolved.
Show resolved Hide resolved

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

ament_package()
7 changes: 7 additions & 0 deletions examples/tools/ros/rosenv/workspace2/my_package/conanfile.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[requires]
yaml-cpp/0.8.0

[generators]
CMakeDeps
CMakeToolchain
ROSEnv
10 changes: 10 additions & 0 deletions examples/tools/ros/rosenv/workspace2/my_package/locations.yaml
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
19 changes: 19 additions & 0 deletions examples/tools/ros/rosenv/workspace2/my_package/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>my_package</name>
<version>0.0.0</version>
<description>Example using yaml-cpp to send navigation goals</description>
<maintainer email="[email protected]">danimtb</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>
93 changes: 93 additions & 0 deletions examples/tools/ros/rosenv/workspace2/my_package/src/main.cpp
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 goal to %s: (%.2f, %.2f)", 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/workspace2/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 my_package/
conan profile detect --force
conan install my_package/conanfile.txt --build=missing --output-folder install/conan
source install/conan/conanrosenv.sh
colcon build --packages-select my_package
source install/setup.bash
ros2 run my_package package my_package/locations.yaml