YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
AudioRecorder_nws_ros2.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2025 Istituto Italiano di Tecnologia (IIT)
3 * SPDX-License-Identifier: BSD-3-Clause
4 */
5
6
9#include <yarp/os/LogStream.h>
10#include <yarp/os/Stamp.h>
11#include <cmath>
12#include <Ros2Utils.h>
13
14YARP_LOG_COMPONENT(AUDIORECORDER_NWS_ROS2, "yarp.devices.AudioRecorder_nws_ros2")
15
19
21{
22 m_audioRecorder_interface = nullptr;
23}
24
25
27{
28
29 if (driver->isValid())
30 {
31 driver->view(m_audioRecorder_interface);
32 } else {
33 yCError(AUDIORECORDER_NWS_ROS2) << "not valid driver";
34 }
35
36 if (m_audioRecorder_interface == nullptr)
37 {
38 yCError(AUDIORECORDER_NWS_ROS2, "Subdevice passed to attach method is invalid");
39 return false;
40 }
41
42 yCInfo(AUDIORECORDER_NWS_ROS2, "Attach complete");
43 PeriodicThread::setPeriod(m_period);
44 return PeriodicThread::start();
45}
46
47
49{
50 if (PeriodicThread::isRunning())
51 {
52 PeriodicThread::stop();
53 }
54 m_audioRecorder_interface = nullptr;
55 return true;
56}
57
59{
60 return true;
61}
62
64{
65 parseParams(config);
66 if (m_node_name[0] == '/') {
67 yCError(AUDIORECORDER_NWS_ROS2) << "node_name parameter cannot begin with '/'";
68 return false;
69 }
70 if (m_topic_name[0] != '/') {
71 yCError(AUDIORECORDER_NWS_ROS2) << "missing initial / in topic_name parameter";
72 return false;
73 }
74
75 rclcpp::NodeOptions node_options;
76 node_options.allow_undeclared_parameters(true);
77 node_options.automatically_declare_parameters_from_overrides(true);
78 if(m_namespace.empty()) {
79 m_node = NodeCreator::createNode(m_node_name, node_options);
80 } else {
81 m_node = NodeCreator::createNode(m_node_name, m_namespace, node_options);
82 }
83 if (m_node == nullptr) {
84 yCError(AUDIORECORDER_NWS_ROS2) << " opening " << m_node_name << " Node, check your yarp-ROS2 network configuration\n";
85 return false;
86 }
87 m_ros2Publisher_status = m_node->create_publisher<std_msgs::msg::Int16MultiArray>(m_topic_name, 10);
88
89 yCInfo(AUDIORECORDER_NWS_ROS2) << "Waiting for device to attach";
90 return true;
91}
92
96
98{
99
100 if (m_audioRecorder_interface!=nullptr && m_ros2Publisher_status)
101 {
102 if(m_ros2Publisher_status->get_subscription_count()>0)
103 {
104 double synchronized_timestamp = 0;
105
106 yarp::sig::AudioBufferSize device_buffer_current_size;
107 yarp::sig::AudioBufferSize device_buffer_max_size;
108 bool isRecording=0;
109 m_audioRecorder_interface->getRecordingAudioBufferCurrentSize(device_buffer_current_size);
110 m_audioRecorder_interface->getRecordingAudioBufferMaxSize(device_buffer_max_size);
111 m_audioRecorder_interface->isRecording(isRecording);
112
113 if (std::isnan(synchronized_timestamp) == false)
114 {
115 m_timeStamp.update(synchronized_timestamp);
116 }
117 else
118 {
119 m_timeStamp.update(yarp::os::Time::now());
120 }
121
122 std_msgs::msg::Int16MultiArray statusMsg;
123 statusMsg.data.resize(3);
124 statusMsg.data[0] = static_cast<int>(isRecording);
125 statusMsg.data[1] = static_cast<int>(device_buffer_current_size.getBufferElements());
126 statusMsg.data[2] = static_cast<int>(device_buffer_max_size.getBufferElements());
127 m_ros2Publisher_status->publish(statusMsg);
128 }
129
130 } else{
131 yCError(AUDIORECORDER_NWS_ROS2) << "the interface is not valid";
132 }
133}
134
136{
138 if (PeriodicThread::isRunning())
139 {
140 PeriodicThread::stop();
141 }
142
143 detach();
144 return true;
145}
const yarp::os::LogComponent & AUDIORECORDER_NWS_ROS2()
#define DEFAULT_THREAD_PERIOD
bool parseParams(const yarp::os::Searchable &config) override
Parse the DeviceDriver parameters.
bool attach(yarp::dev::PolyDriver *driver) override
Attach to another object.
bool detach() override
Detach the object (you must have first called attach).
void run() override
Loop function.
bool open(yarp::os::Searchable &params) override
Open the DeviceDriver.
void threadRelease() override
Release method.
bool threadInit() override
Initialization method.
bool close() override
Close the DeviceDriver.
static rclcpp::Node::SharedPtr createNode(std::string name)
Definition Ros2Utils.cpp:9
bool view(T *&x)
Get an interface to the device driver.
virtual yarp::dev::ReturnValue getRecordingAudioBufferCurrentSize(yarp::sig::AudioBufferSize &size)=0
virtual yarp::dev::ReturnValue isRecording(bool &recording_enabled)=0
Check if the recording has been enabled (e.g.
virtual yarp::dev::ReturnValue getRecordingAudioBufferMaxSize(yarp::sig::AudioBufferSize &size)=0
A container for a device driver.
Definition PolyDriver.h:23
bool isValid() const
Check if device is valid.
A base class for nested structures that can be searched.
Definition Searchable.h:31
void update()
Set the timestamp to the current time, and increment the sequence number (wrapping to 0 if the sequen...
Definition Stamp.cpp:124
#define yCInfo(component,...)
#define yCError(component,...)
#define yCTrace(component,...)
#define YARP_LOG_COMPONENT(name,...)
double now()
Return the current time in seconds, relative to an arbitrary starting point.
Definition Time.cpp:121
The main, catch-all namespace for YARP.
Definition dirs.h:16