odom_proxy_node.cpp
Go to the documentation of this file.
1 #include <ros/ros.h>
2 #include "rosabridge_msgs/Odometry.h"
3 #include "rosabridge_msgs/OdometryCovariances.h"
4 #include "rosabridge_msgs/RequestOdometryCovariances.h"
6 #include <nav_msgs/Odometry.h>
7 #include <signal.h>
8 #include <string>
9 
10 // CBA Define following to enable service for obtaining covariances
11 // (not currently supported by rosserial on Arduino)
12 //#define _ODOM_COVAR
13 
14 void mySigintHandler(int sig)
15 {
16  ROS_INFO("Received SIGINT signal, shutting down...");
17  ros::shutdown();
18 }
19 
21 {
22 
23 public:
24  OdomProxyNode();
25 
26 public:
27  void odom_callback(const rosabridge_msgs::Odometry::ConstPtr& odomMsg);
28  int run();
29 
30 protected:
35  geometry_msgs::TransformStamped trans_msg;
36  nav_msgs::Odometry odom_msg;
37 
38 #ifdef _ODOM_COVAR
39  ros::ServiceClient odom_client;
40 #endif
41 
43  std::string odom_frame;
44  std::string base_frame;
45  std::string odom_topic;
46 
47 };
48 
50 {
51 
52  // CBA Read local params (from launch file)
53  ros::NodeHandle nhLocal("~");
54  nhLocal.param("pub_odom_tf", pub_odom_tf, true);
55  ROS_INFO_STREAM("pub_odom_tf: " << pub_odom_tf);
56  nhLocal.param<std::string>("odom_frame", odom_frame, "");
57  ROS_INFO_STREAM("odom_frame: " << odom_frame);
58  nhLocal.param<std::string>("base_frame", base_frame, "");
59  ROS_INFO_STREAM("base_frame: " << base_frame);
60  nhLocal.param<std::string>("odom_topic", odom_topic, "odom");
61  ROS_INFO_STREAM("odom_topic: " << odom_topic);
62 
63  // CBA Initially zero out covariances
64  odom_msg.pose.covariance.assign(0);
65  odom_msg.twist.covariance.assign(0);
66 
67 }
68 
69 void OdomProxyNode::odom_callback(const rosabridge_msgs::Odometry::ConstPtr& odomMsg)
70 {
71  if ( pub_odom_tf )
72  {
73  trans_msg.header.stamp = odomMsg->header.stamp;
74  if ( !odom_frame.empty() )
75  trans_msg.header.frame_id = odom_frame;
76  else
77  trans_msg.header.frame_id = odomMsg->header.frame_id;
78  if ( !base_frame.empty() )
79  trans_msg.child_frame_id = base_frame;
80  else
81  trans_msg.child_frame_id = odomMsg->child_frame_id;
82  trans_msg.transform.translation.x = odomMsg->pose.position.x;
83  trans_msg.transform.translation.y = odomMsg->pose.position.y;
84  trans_msg.transform.translation.z = odomMsg->pose.position.z;
85  trans_msg.transform.rotation.x = odomMsg->pose.orientation.x;
86  trans_msg.transform.rotation.y = odomMsg->pose.orientation.y;
87  trans_msg.transform.rotation.z = odomMsg->pose.orientation.z;
88  trans_msg.transform.rotation.w = odomMsg->pose.orientation.w;
90  }
91 
92  odom_msg.header.stamp = odomMsg->header.stamp;
93  if ( !odom_frame.empty() )
94  odom_msg.header.frame_id = odom_frame;
95  else
96  odom_msg.header.frame_id = odomMsg->header.frame_id;
97  if ( !base_frame.empty() )
98  odom_msg.child_frame_id = base_frame;
99  else
100  odom_msg.child_frame_id = odomMsg->child_frame_id;
101  odom_msg.pose.pose.position.x = odomMsg->pose.position.x;
102  odom_msg.pose.pose.position.y = odomMsg->pose.position.y;
103  odom_msg.pose.pose.position.z = odomMsg->pose.position.z;
104  odom_msg.pose.pose.orientation.x = odomMsg->pose.orientation.x;
105  odom_msg.pose.pose.orientation.y = odomMsg->pose.orientation.y;
106  odom_msg.pose.pose.orientation.z = odomMsg->pose.orientation.z;
107  odom_msg.pose.pose.orientation.w = odomMsg->pose.orientation.w;
108  odom_msg.twist.twist.linear.x = odomMsg->twist.linear.x;
109  odom_msg.twist.twist.linear.y = odomMsg->twist.linear.y;
110  odom_msg.twist.twist.linear.z = odomMsg->twist.linear.z;
111  odom_msg.twist.twist.angular.x = odomMsg->twist.angular.x;
112  odom_msg.twist.twist.angular.y = odomMsg->twist.angular.y;
113  odom_msg.twist.twist.angular.z = odomMsg->twist.angular.z;
114  odom_msg.pose.covariance[0] = odomMsg->pose_covariance[0];
115  odom_msg.pose.covariance[7] = odomMsg->pose_covariance[1];
116  odom_msg.pose.covariance[14] = odomMsg->pose_covariance[2];
117  odom_msg.pose.covariance[21] = odomMsg->pose_covariance[3];
118  odom_msg.pose.covariance[28] = odomMsg->pose_covariance[4];
119  odom_msg.pose.covariance[35] = odomMsg->pose_covariance[5];
120  odom_msg.twist.covariance[0] = odomMsg->twist_covariance[0];
121  odom_msg.twist.covariance[7] = odomMsg->twist_covariance[1];
122  odom_msg.twist.covariance[14] = odomMsg->twist_covariance[2];
123  odom_msg.twist.covariance[21] = odomMsg->twist_covariance[3];
124  odom_msg.twist.covariance[28] = odomMsg->twist_covariance[4];
125  odom_msg.twist.covariance[35] = odomMsg->twist_covariance[5];
127 }
128 
130 {
131 
132 #ifdef _ODOM_COVAR
133  ROS_INFO("Requesting Odometry Covariances");
134  odom_client = nh.serviceClient<rosabridge_msgs::RequestOdometryCovariances>("rosabridge/odom_covar_srv");
135  rosabridge_msgs::RequestOdometryCovariances odomCovarSrv;
136  if (odom_client.call(odomCovarSrv))
137  {
138  odom_msg.pose.covariance[0] = odomCovarSrv.response.pose_covariance[0];
139  odom_msg.pose.covariance[1] = odomCovarSrv.response.pose_covariance[1];
140  odom_msg.pose.covariance[2] = odomCovarSrv.response.pose_covariance[2];
141  odom_msg.pose.covariance[3] = odomCovarSrv.response.pose_covariance[3];
142  odom_msg.pose.covariance[4] = odomCovarSrv.response.pose_covariance[4];
143  odom_msg.pose.covariance[5] = odomCovarSrv.response.pose_covariance[5];
144  odom_msg.pose.covariance[6] = odomCovarSrv.response.pose_covariance[6];
145  odom_msg.pose.covariance[7] = odomCovarSrv.response.pose_covariance[7];
146  odom_msg.pose.covariance[8] = odomCovarSrv.response.pose_covariance[8];
147  odom_msg.pose.covariance[9] = odomCovarSrv.response.pose_covariance[9];
148  odom_msg.pose.covariance[10] = odomCovarSrv.response.pose_covariance[10];
149  odom_msg.pose.covariance[11] = odomCovarSrv.response.pose_covariance[11];
150  odom_msg.pose.covariance[12] = odomCovarSrv.response.pose_covariance[12];
151  odom_msg.pose.covariance[13] = odomCovarSrv.response.pose_covariance[13];
152  odom_msg.pose.covariance[14] = odomCovarSrv.response.pose_covariance[14];
153  odom_msg.pose.covariance[15] = odomCovarSrv.response.pose_covariance[15];
154  odom_msg.pose.covariance[16] = odomCovarSrv.response.pose_covariance[16];
155  odom_msg.pose.covariance[17] = odomCovarSrv.response.pose_covariance[17];
156  odom_msg.pose.covariance[18] = odomCovarSrv.response.pose_covariance[18];
157  odom_msg.pose.covariance[19] = odomCovarSrv.response.pose_covariance[19];
158  odom_msg.pose.covariance[20] = odomCovarSrv.response.pose_covariance[20];
159  odom_msg.pose.covariance[21] = odomCovarSrv.response.pose_covariance[21];
160  odom_msg.pose.covariance[22] = odomCovarSrv.response.pose_covariance[22];
161  odom_msg.pose.covariance[23] = odomCovarSrv.response.pose_covariance[23];
162  odom_msg.pose.covariance[24] = odomCovarSrv.response.pose_covariance[24];
163  odom_msg.pose.covariance[25] = odomCovarSrv.response.pose_covariance[25];
164  odom_msg.pose.covariance[26] = odomCovarSrv.response.pose_covariance[26];
165  odom_msg.pose.covariance[27] = odomCovarSrv.response.pose_covariance[27];
166  odom_msg.pose.covariance[28] = odomCovarSrv.response.pose_covariance[28];
167  odom_msg.pose.covariance[29] = odomCovarSrv.response.pose_covariance[29];
168  odom_msg.pose.covariance[30] = odomCovarSrv.response.pose_covariance[30];
169  odom_msg.pose.covariance[31] = odomCovarSrv.response.pose_covariance[31];
170  odom_msg.pose.covariance[32] = odomCovarSrv.response.pose_covariance[32];
171  odom_msg.pose.covariance[33] = odomCovarSrv.response.pose_covariance[33];
172  odom_msg.pose.covariance[34] = odomCovarSrv.response.pose_covariance[34];
173  odom_msg.pose.covariance[35] = odomCovarSrv.response.pose_covariance[35];
174  odom_msg.twist.covariance[0] = odomCovarSrv.response.twist_covariance[0];
175  odom_msg.twist.covariance[1] = odomCovarSrv.response.twist_covariance[1];
176  odom_msg.twist.covariance[2] = odomCovarSrv.response.twist_covariance[2];
177  odom_msg.twist.covariance[3] = odomCovarSrv.response.twist_covariance[3];
178  odom_msg.twist.covariance[4] = odomCovarSrv.response.twist_covariance[4];
179  odom_msg.twist.covariance[5] = odomCovarSrv.response.twist_covariance[5];
180  odom_msg.twist.covariance[6] = odomCovarSrv.response.twist_covariance[6];
181  odom_msg.twist.covariance[7] = odomCovarSrv.response.twist_covariance[7];
182  odom_msg.twist.covariance[8] = odomCovarSrv.response.twist_covariance[8];
183  odom_msg.twist.covariance[9] = odomCovarSrv.response.twist_covariance[9];
184  odom_msg.twist.covariance[10] = odomCovarSrv.response.twist_covariance[10];
185  odom_msg.twist.covariance[11] = odomCovarSrv.response.twist_covariance[11];
186  odom_msg.twist.covariance[12] = odomCovarSrv.response.twist_covariance[12];
187  odom_msg.twist.covariance[13] = odomCovarSrv.response.twist_covariance[13];
188  odom_msg.twist.covariance[14] = odomCovarSrv.response.twist_covariance[14];
189  odom_msg.twist.covariance[15] = odomCovarSrv.response.twist_covariance[15];
190  odom_msg.twist.covariance[16] = odomCovarSrv.response.twist_covariance[16];
191  odom_msg.twist.covariance[17] = odomCovarSrv.response.twist_covariance[17];
192  odom_msg.twist.covariance[18] = odomCovarSrv.response.twist_covariance[18];
193  odom_msg.twist.covariance[19] = odomCovarSrv.response.twist_covariance[19];
194  odom_msg.twist.covariance[20] = odomCovarSrv.response.twist_covariance[20];
195  odom_msg.twist.covariance[21] = odomCovarSrv.response.twist_covariance[21];
196  odom_msg.twist.covariance[22] = odomCovarSrv.response.twist_covariance[22];
197  odom_msg.twist.covariance[23] = odomCovarSrv.response.twist_covariance[23];
198  odom_msg.twist.covariance[24] = odomCovarSrv.response.twist_covariance[24];
199  odom_msg.twist.covariance[25] = odomCovarSrv.response.twist_covariance[25];
200  odom_msg.twist.covariance[26] = odomCovarSrv.response.twist_covariance[26];
201  odom_msg.twist.covariance[27] = odomCovarSrv.response.twist_covariance[27];
202  odom_msg.twist.covariance[28] = odomCovarSrv.response.twist_covariance[28];
203  odom_msg.twist.covariance[29] = odomCovarSrv.response.twist_covariance[29];
204  odom_msg.twist.covariance[30] = odomCovarSrv.response.twist_covariance[30];
205  odom_msg.twist.covariance[31] = odomCovarSrv.response.twist_covariance[31];
206  odom_msg.twist.covariance[32] = odomCovarSrv.response.twist_covariance[32];
207  odom_msg.twist.covariance[33] = odomCovarSrv.response.twist_covariance[33];
208  odom_msg.twist.covariance[34] = odomCovarSrv.response.twist_covariance[34];
209  odom_msg.twist.covariance[35] = odomCovarSrv.response.twist_covariance[35];
210  }
211  else
212  {
213  ROS_WARN("Failed to retrieve Odometry Covariances");
214  }
215 #endif
216 
217  ROS_INFO("Broadcasting odom tf");
218  ROS_INFO_STREAM("Publishing to topic " << odom_topic);
219  odom_pub = nh.advertise<nav_msgs::Odometry>(odom_topic, 1000);
220  ROS_INFO("Subscribing to topic rosabridge/odom");
221  odom_sub = nh.subscribe("rosabridge/odom", 1000, &OdomProxyNode::odom_callback, this);
222 
223  ROS_INFO("Relaying between topics...");
224  ros::spin();
225 
226  ROS_INFO("Exiting");
227 
228  return 0;
229 }
230 
231 int main(int argc, char **argv)
232 {
233 
234  ros::init(argc, argv, "odom_proxy_node");
235 
236  OdomProxyNode node;
237 
238  // Override the default ros sigint handler.
239  // This must be set after the first NodeHandle is created.
240  signal(SIGINT, mySigintHandler);
241 
242  return node.run();
243 }
244 
ServiceClient serviceClient(const std::string &service_name, bool persistent=false, const M_string &header_values=M_string())
tf::TransformBroadcaster odom_broadcaster
void publish(const boost::shared_ptr< M > &message) const
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
nav_msgs::Odometry odom_msg
std::string base_frame
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
ros::NodeHandle nh
std::string odom_topic
#define ROS_WARN(...)
ROSCPP_DECL void spin(Spinner &spinner)
void odom_callback(const rosabridge_msgs::Odometry::ConstPtr &odomMsg)
ros::Publisher odom_pub
int main(int argc, char **argv)
#define ROS_INFO(...)
bool param(const std::string &param_name, T &param_val, const T &default_val) const
void sendTransform(const StampedTransform &transform)
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
ros::Subscriber odom_sub
#define ROS_INFO_STREAM(args)
geometry_msgs::TransformStamped trans_msg
void mySigintHandler(int sig)
ROSCPP_DECL void shutdown()
std::string odom_frame


rosabridge_server
Author(s): Chad Attermann
autogenerated on Sat Apr 10 2021 02:37:52