Giter Club home page Giter Club logo

ros2_yaml_parser's Introduction

ros2_yaml_parser

ROS2 example on how to parse complex yaml files to be used as parameters for a Node based on python.

Dependencies

This using this package depends on pyyaml and anytree . You can easily install them both using the following command:

pip install pyyaml
pip install anytree

ALERT

The code parses the contents of the yaml file as a dictionary.

Alert: โš ๏ธ The first item in the yaml file has to be ros__parameters: for the code to work.

The first item in the yaml file is parsed as an object, not as a dictionary by default when using the anytree importer. I solved it by forcing the user to use ros__parameters: as the first variable and then ignoring it.

FAST DEPLOYMENT SNIPPETS

Launch

import os
from ament_index_python.packages import get_package_share_directory
from launch.actions import DeclareLaunchArgument
from launch.substitutions import LaunchConfiguration

DeclareLaunchArgument(name = "parameters_file_path",
                        description ="--params-file (name of the spot to identify)",
                        default_value = os.path.join(get_package_share_directory('ros2_yaml_parser'), 'config', 'spots.yaml')),
arguments=[
            '--params-file', LaunchConfiguration("parameters_file_path"),
            '--spot', LaunchConfiguration("spot_to_find")],

Script

import argparse
import os
from ament_index_python.packages import get_package_share_directory
from anytree.importer import DictImporter
import yaml

# PARSE ARGUMENTS
parser = argparse.ArgumentParser()
parser.add_argument('--params-file', type=str, default=os.path.join(get_package_share_directory('ros2_yaml_parser'), 'config', 'spots.yaml'), help='Size argument')
args, _ = parser.parse_known_args()
parsed_dictionary = yaml_to_dictionary(args.params_file) 

def yaml_to_dictionary(file_path):
    with open(file_path, 'r') as yaml_file:
        yaml_data = yaml.safe_load(yaml_file)
    parsed_data = DictImporter().import_(yaml_data)
    return parsed_data.ros__parameters

Code Testing

You can easily download this package and test it by downloading it into your src

You can easily test the package using the following command to execute the launchfile:

ros2 launch ros2_yaml_parser yaml_test.launch.py

Or check the code for other spots by using another spot label as an input argument:

ros2 launch ros2_yaml_parser yaml_test.launch.py spot:="corner2"

ros2_yaml_parser's People

Contributors

pep248 avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.