YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
Localization2D_nws_ros2.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#ifndef _USE_MATH_DEFINES
7#define _USE_MATH_DEFINES
8#endif
9
11
13#include <yarp/os/LogStream.h>
14#include <Ros2Utils.h>
15
16#include <cmath>
17
18using namespace std::chrono_literals;
19using namespace yarp::os;
20using namespace yarp::dev;
21using namespace yarp::dev::Nav2D;
22
23
25
26
32
34{
35 if (poly->isValid())
36 {
37 poly->view(m_iLoc);
38 }
39
40 //attach the hardware device
41 if (nullptr == m_iLoc)
42 {
43 yCError(LOCALIZATION2D_NWS_ROS2, "Subdevice passed to attach method is invalid");
44 return false;
45 }
46
47 return true;
48}
49
51{
53 {
55 }
56 m_iLoc = nullptr;
57
58 return true;
59}
60
62{
64 if (m_stats_time_curr - m_stats_time_last > 5.0)
65 {
66 yCInfo(LOCALIZATION2D_NWS_ROS2) << "Running";
67 m_stats_time_last = yarp::os::Time::now();
68 }
69
70 if (m_iLoc!=nullptr)
71 {
72 bool ret = m_iLoc->getLocalizationStatus(m_current_status);
73 if (!ret)
74 {
75 yCError(LOCALIZATION2D_NWS_ROS2) << "getLocalizationStatus() failed";
76 }
77
78 if (m_current_status == LocalizationStatusEnum::localization_status_localized_ok)
79 {
80 bool ret2 = m_iLoc->getCurrentPosition(m_current_position);
81 if (!ret2)
82 {
83 yCError(LOCALIZATION2D_NWS_ROS2) << "getCurrentPosition() failed";
84 }
85 else
86 {
87 m_loc_stamp.update();
88 }
89 bool ret3 = m_iLoc->getEstimatedOdometry(m_current_odometry);
90 if (!ret3)
91 {
92 //yCError(LOCALIZATION2D_NWS_ROS2) << "getEstimatedOdometry() failed";
93 }
94 else
95 {
96 m_odom_stamp.update();
97 }
98 }
99 else
100 {
101 yCWarning(LOCALIZATION2D_NWS_ROS2, "The system is not properly localized!");
102 }
103
104 if (1) publish_odometry_on_ROS_topic();
105 if (1) publish_odometry_on_TF_topic();
106 }
107}
108
110{
111 if(m_node_name[0] == '/'){
112 yCError(LOCALIZATION2D_NWS_ROS2) << "node_name cannot begin with an initial /";
113 return false;
114 }
115
116 parseParams(config);
117
118 //create the topics
119 const std::string m_odom_topic ="/odom";
120 const std::string m_tf_topic ="/tf";
122
123 m_publisher_odom = m_node->create_publisher<nav_msgs::msg::Odometry>(m_odom_topic, 10);
124 m_publisher_tf = m_node->create_publisher<tf2_msgs::msg::TFMessage>(m_tf_topic, 10);
125 yCInfo(LOCALIZATION2D_NWS_ROS2, "Opened topics: %s, %s", m_odom_topic.c_str(), m_tf_topic.c_str());
126
127 yCInfo(LOCALIZATION2D_NWS_ROS2) << "Waiting for device to attach";
128
129 //start the publishig thread
131 start();
132 return true;
133}
134
136{
139 {
141 }
142
143 detach();
144 return true;
145}
146
147void Localization2D_nws_ros2::publish_odometry_on_TF_topic()
148{
149 tf2_msgs::msg::TFMessage rosData;
150
151 geometry_msgs::msg::TransformStamped tsData;
152 tsData.child_frame_id = m_ROS_child_frame_id;
153 tsData.header.frame_id = m_ROS_parent_frame_id;
154 tsData.header.stamp = m_node->get_clock()->now(); //@@@@@@@@@@@ CHECK HERE: simulation time?
155 double halfYaw = m_current_odometry.odom_theta / 180.0 * M_PI * 0.5;
156 double cosYaw = cos(halfYaw);
157 double sinYaw = sin(halfYaw);
158 tsData.transform.rotation.x = 0;
159 tsData.transform.rotation.y = 0;
160 tsData.transform.rotation.z = sinYaw;
161 tsData.transform.rotation.w = cosYaw;
162 tsData.transform.translation.x = m_current_odometry.odom_x;
163 tsData.transform.translation.y = m_current_odometry.odom_y;
164 tsData.transform.translation.z = 0;
165
166 if (rosData.transforms.size() == 0)
167 {
168 rosData.transforms.push_back(tsData);
169 }
170 else
171 {
172 rosData.transforms[0] = tsData;
173 }
174
175 m_publisher_tf->publish(rosData);
176}
177
178void Localization2D_nws_ros2::publish_odometry_on_ROS_topic()
179{
180 nav_msgs::msg::Odometry rosData;
181
182 rosData.header.frame_id = m_fixed_frame;
183 rosData.header.stamp = m_node->get_clock()->now(); //@@@@@@@@@@@ CHECK HERE: simulation time?
184 rosData.child_frame_id = m_robot_frame;
185
186 rosData.pose.pose.position.x = m_current_odometry.odom_x;
187 rosData.pose.pose.position.y = m_current_odometry.odom_y;
188 rosData.pose.pose.position.z = 0;
190 vecrpy[0] = 0;
191 vecrpy[1] = 0;
192 vecrpy[2] = m_current_odometry.odom_theta;
194 yarp::math::Quaternion q; q.fromRotationMatrix(matrix);
195 rosData.pose.pose.orientation.x = q.x();
196 rosData.pose.pose.orientation.y = q.y();
197 rosData.pose.pose.orientation.z = q.z();
198 rosData.pose.pose.orientation.w = q.w();
199 //rosData.pose.covariance = 0;
200
201 rosData.twist.twist.linear.x = m_current_odometry.base_vel_x;
202 rosData.twist.twist.linear.y = m_current_odometry.base_vel_y;
203 rosData.twist.twist.linear.z = 0;
204 rosData.twist.twist.angular.x = 0;
205 rosData.twist.twist.angular.y = 0;
206 rosData.twist.twist.angular.z = m_current_odometry.base_vel_theta;
207 //rosData.twist.covariance = 0;
208
209 m_publisher_odom->publish(rosData);
210}
#define M_PI
bool ret
const yarp::os::LogComponent & LOCALIZATION2D_NWS_ROS2()
bool parseParams(const yarp::os::Searchable &config) override
Parse the DeviceDriver parameters.
bool open(yarp::os::Searchable &config) override
Open the DeviceDriver.
bool attach(yarp::dev::PolyDriver *poly) override
Attach to another object.
bool detach() override
Detach the object (you must have first called attach).
bool close() override
Close the DeviceDriver.
void run() override
Loop function.
static rclcpp::Node::SharedPtr createNode(std::string name)
Definition Ros2Utils.cpp:9
bool view(T *&x)
Get an interface to the device driver.
virtual bool getEstimatedOdometry(yarp::dev::OdometryData &odom)=0
Gets the estimated odometry the robot, including its velocity expressed in the world and in the local...
virtual bool getCurrentPosition(yarp::dev::Nav2D::Map2DLocation &loc)=0
Gets the current position of the robot w.r.t world reference frame.
virtual bool getLocalizationStatus(LocalizationStatusEnum &status)=0
Gets the current status of the localization task.
double base_vel_x
velocity of the robot [m/s] expressed in the robot reference frame
double base_vel_theta
angular velocity of the robot [deg/s] expressed in the robot reference frame
double base_vel_y
velocity of the robot [m/s] expressed in the robot reference frame
double odom_x
position of the robot [m], expressed in the world reference frame
double odom_y
position of the robot [m], expressed in the world reference frame
double odom_theta
orientation the robot [deg], expressed in the world reference frame
A container for a device driver.
Definition PolyDriver.h:23
bool isValid() const
Check if device is valid.
A mini-server for performing network communication in the background.
@ TraceType
Definition Log.h:92
An abstraction for a periodic thread.
bool setPeriod(double period)
Set the (new) period of the thread.
bool isRunning() const
Returns true when the thread is started, false otherwise.
bool start()
Call this to start the thread.
void stop()
Call this to stop the thread, this call blocks until the thread is terminated (and releaseThread() ca...
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
A class for a Matrix.
Definition Matrix.h:39
#define yCInfo(component,...)
#define yCError(component,...)
#define yCTrace(component,...)
#define yCWarning(component,...)
#define YARP_LOG_COMPONENT(name,...)
For streams capable of holding different kinds of content, check what they actually have.
Definition jointData.cpp:13
yarp::sig::Matrix rpy2dcm(const yarp::sig::Vector &rpy)
Converts roll-pitch-yaw angles in the corresponding dcm (direction cosine matrix) rotation matrix (de...
Definition math.cpp:847
double now()
Return the current time in seconds, relative to an arbitrary starting point.
Definition Time.cpp:121
An interface to the operating system, including Port based communication.
The main, catch-all namespace for YARP.
Definition dirs.h:16