YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
Ros2Test.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2023 Istituto Italiano di Tecnologia (IIT)
3 * SPDX-License-Identifier: BSD-3-Clause
4 */
5
6#include "Ros2Test.h"
7
8#include <chrono>
9#include <functional>
10#include <memory>
11#include <string>
12
14#include <yarp/os/LogStream.h>
15
16using namespace std::chrono_literals;
17
19
20
22{
23 rclcpp::init(/*argc*/ 0, /*argv*/ nullptr);
24 node = std::make_shared<rclcpp::Node>("yarprobotinterface_node");
25}
26
28{
29 static Ros2Init instance;
30 return instance;
31}
32
33
35 yarp::os::PeriodicThread(0.5)
36{
37}
38
40{
42 m_topic = config.check("topic", yarp::os::Value("ros2test_topic"), "Name of the ROS topic").asString();
43 yCInfo(ROS2TEST, "Ros2Test::open - %s", m_topic.c_str());
44
45 m_publisher = Ros2Init::get().node->create_publisher<std_msgs::msg::String>(m_topic, 10);
46
47 start();
48 return true;
49}
50
52{
54 yCInfo(ROS2TEST, "Ros2Test::close");
55 return true;
56}
57
59{
61 auto message = std_msgs::msg::String();
62 message.data = "Hello, " + m_topic + "! " + std::to_string(m_count++);
63 yCInfo(ROS2TEST, "Publishing: '%s'", message.data.c_str());
64 m_publisher->publish(message);
65}
const yarp::os::LogComponent & ROS2TEST()
Definition Ros2Test.cpp:18
std::shared_ptr< rclcpp::Node > node
Definition Ros2Test.h:22
static Ros2Init & get()
Definition Ros2Test.cpp:27
bool open(yarp::os::Searchable &config) override
Open the DeviceDriver.
Definition Ros2Test.cpp:39
bool close() override
Close the DeviceDriver.
Definition Ros2Test.cpp:51
void run() override
Loop function.
Definition Ros2Test.cpp:58
@ TraceType
Definition Log.h:92
bool start()
Call this to start the thread.
A base class for nested structures that can be searched.
Definition Searchable.h:31
virtual bool check(const std::string &key) const =0
Check if there exists a property of the given name.
A single value (typically within a Bottle).
Definition Value.h:43
#define yCInfo(component,...)
#define yCTrace(component,...)
#define YARP_LOG_COMPONENT(name,...)
The main, catch-all namespace for YARP.
Definition dirs.h:16