imu_proxy_node.cpp
Go to the documentation of this file.
1 #include <ros/ros.h>
2 #include "rosabridge_msgs/Imu.h"
3 #include "rosabridge_msgs/ImuCovariances.h"
4 #include "rosabridge_msgs/RequestImuCovariances.h"
5 #include <sensor_msgs/Imu.h>
6 #include <signal.h>
7 #include <string>
8 
9 // CBA Define following to enable service for obtaining covariances
10 // (not currently supported by rosserial on Arduino)
11 //#define _IMU_COVAR
12 
13 void mySigintHandler(int sig)
14 {
15  ROS_INFO("Received SIGINT signal, shutting down...");
16  ros::shutdown();
17 }
18 
20 {
21 
22 public:
23  ImuProxyNode();
24 
25 public:
26  void imu_callback(const rosabridge_msgs::Imu::ConstPtr& imuMsg);
27  int run();
28 
29 protected:
33  sensor_msgs::Imu imu_msg;
34 
35 #ifdef _IMU_COVAR
36  ros::ServiceClient imu_client;
37 #endif
38 
39  std::string imu_frame;
40  std::string imu_topic;
41 
42 };
43 
45 {
46 
47  // CBA Read local params (from launch file)
48  ros::NodeHandle nhLocal("~");
49  nhLocal.param<std::string>("imu_frame", imu_frame, "");
50  ROS_INFO_STREAM("imu_frame: " << imu_frame);
51  nhLocal.param<std::string>("imu_topic", imu_topic, "imu/data");
52  ROS_INFO_STREAM("imu_topic: " << imu_topic);
53 
54  // CBA Initially zero out covariances
55  imu_msg.orientation_covariance.assign(0);
56  imu_msg.angular_velocity_covariance.assign(0);
57  imu_msg.linear_acceleration_covariance.assign(0);
58 
59 }
60 
61 void ImuProxyNode::imu_callback(const rosabridge_msgs::Imu::ConstPtr& imuMsg)
62 {
63  imu_msg.header.stamp = imuMsg->header.stamp;
64  if ( !imu_frame.empty() )
65  imu_msg.header.frame_id = imu_frame;
66  else
67  imu_msg.header.frame_id = imuMsg->header.frame_id;
68  imu_msg.orientation.x = imuMsg->orientation.x;
69  imu_msg.orientation.y = imuMsg->orientation.y;
70  imu_msg.orientation.z = imuMsg->orientation.z;
71  imu_msg.orientation.w = imuMsg->orientation.w;
72  imu_msg.linear_acceleration.x = imuMsg->linear_acceleration.x;
73  imu_msg.linear_acceleration.y = imuMsg->linear_acceleration.y;
74  imu_msg.linear_acceleration.z = imuMsg->linear_acceleration.z;
75  imu_msg.angular_velocity.x = imuMsg->angular_velocity.x;
76  imu_msg.angular_velocity.y = imuMsg->angular_velocity.y;
77  imu_msg.angular_velocity.z = imuMsg->angular_velocity.z;
78  imu_msg.orientation_covariance[0] = imuMsg->orientation_covariance[0];
79  imu_msg.orientation_covariance[4] = imuMsg->orientation_covariance[1];
80  imu_msg.orientation_covariance[8] = imuMsg->orientation_covariance[2];
81  imu_msg.linear_acceleration_covariance[0] = imuMsg->linear_acceleration_covariance[0];
82  imu_msg.linear_acceleration_covariance[4] = imuMsg->linear_acceleration_covariance[1];
83  imu_msg.linear_acceleration_covariance[8] = imuMsg->linear_acceleration_covariance[2];
84  imu_msg.angular_velocity_covariance[0] = imuMsg->angular_velocity_covariance[0];
85  imu_msg.angular_velocity_covariance[4] = imuMsg->angular_velocity_covariance[1];
86  imu_msg.angular_velocity_covariance[8] = imuMsg->angular_velocity_covariance[2];
88 }
89 
91 {
92 
93 #ifdef _IMU_COVAR
94  ROS_INFO("Requesting Imu Covariances");
95  imu_client = nh.serviceClient<rosabridge_msgs::RequestImuCovariances>("rosabridge/imu_covar_srv");
96  rosabridge_msgs::RequestImuCovariances imuCovarSrv;
97  if (imu_client.call(imuCovarSrv))
98  {
99  imu_msg.orientation_covariance[0] = imuCovarSrv.response.orientation_covariance[0];
100  imu_msg.orientation_covariance[1] = imuCovarSrv.response.orientation_covariance[1];
101  imu_msg.orientation_covariance[2] = imuCovarSrv.response.orientation_covariance[2];
102  imu_msg.orientation_covariance[3] = imuCovarSrv.response.orientation_covariance[3];
103  imu_msg.orientation_covariance[4] = imuCovarSrv.response.orientation_covariance[4];
104  imu_msg.orientation_covariance[5] = imuCovarSrv.response.orientation_covariance[5];
105  imu_msg.orientation_covariance[6] = imuCovarSrv.response.orientation_covariance[6];
106  imu_msg.orientation_covariance[7] = imuCovarSrv.response.orientation_covariance[7];
107  imu_msg.orientation_covariance[8] = imuCovarSrv.response.orientation_covariance[8];
108  imu_msg.linear_acceleration_covariance[0] = imuCovarSrv.response.linear_acceleration_covariance[0];
109  imu_msg.linear_acceleration_covariance[1] = imuCovarSrv.response.linear_acceleration_covariance[1];
110  imu_msg.linear_acceleration_covariance[2] = imuCovarSrv.response.linear_acceleration_covariance[2];
111  imu_msg.linear_acceleration_covariance[3] = imuCovarSrv.response.linear_acceleration_covariance[3];
112  imu_msg.linear_acceleration_covariance[4] = imuCovarSrv.response.linear_acceleration_covariance[4];
113  imu_msg.linear_acceleration_covariance[5] = imuCovarSrv.response.linear_acceleration_covariance[5];
114  imu_msg.linear_acceleration_covariance[6] = imuCovarSrv.response.linear_acceleration_covariance[6];
115  imu_msg.linear_acceleration_covariance[7] = imuCovarSrv.response.linear_acceleration_covariance[7];
116  imu_msg.linear_acceleration_covariance[8] = imuCovarSrv.response.linear_acceleration_covariance[8];
117  imu_msg.angular_velocity_covariance[0] = imuCovarSrv.response.angular_velocity_covariance[0];
118  imu_msg.angular_velocity_covariance[1] = imuCovarSrv.response.angular_velocity_covariance[1];
119  imu_msg.angular_velocity_covariance[2] = imuCovarSrv.response.angular_velocity_covariance[2];
120  imu_msg.angular_velocity_covariance[3] = imuCovarSrv.response.angular_velocity_covariance[3];
121  imu_msg.angular_velocity_covariance[4] = imuCovarSrv.response.angular_velocity_covariance[4];
122  imu_msg.angular_velocity_covariance[5] = imuCovarSrv.response.angular_velocity_covariance[5];
123  imu_msg.angular_velocity_covariance[6] = imuCovarSrv.response.angular_velocity_covariance[6];
124  imu_msg.angular_velocity_covariance[7] = imuCovarSrv.response.angular_velocity_covariance[7];
125  imu_msg.angular_velocity_covariance[8] = imuCovarSrv.response.angular_velocity_covariance[8];
126  }
127  else
128  {
129  ROS_WARN("Failed to retrieve Imu Covariances");
130  }
131 #endif
132 
133  ROS_INFO_STREAM("Publishing to topic " << imu_topic);
134  imu_pub = nh.advertise<sensor_msgs::Imu>(imu_topic, 1000);
135  ROS_INFO("Subscribing to topic rosabridge/imu");
136  imu_sub = nh.subscribe("rosabridge/imu", 1000, &ImuProxyNode::imu_callback, this);
137 
138  ROS_INFO("Relaying between topics...");
139  ros::spin();
140 
141  ROS_INFO("Exiting");
142 
143  return 0;
144 }
145 
146 int main(int argc, char **argv)
147 {
148 
149  ros::init(argc, argv, "imu_proxy_node");
150 
151  ImuProxyNode node;
152 
153  // Override the default ros sigint handler.
154  // This must be set after the first NodeHandle is created.
155  signal(SIGINT, mySigintHandler);
156 
157  return node.run();
158 }
159 
ServiceClient serviceClient(const std::string &service_name, bool persistent=false, const M_string &header_values=M_string())
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())
ros::NodeHandle nh
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
sensor_msgs::Imu imu_msg
#define ROS_WARN(...)
ROSCPP_DECL void spin(Spinner &spinner)
std::string imu_frame
#define ROS_INFO(...)
bool param(const std::string &param_name, T &param_val, const T &default_val) const
std::string imu_topic
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
ros::Publisher imu_pub
int main(int argc, char **argv)
#define ROS_INFO_STREAM(args)
ros::Subscriber imu_sub
ROSCPP_DECL void shutdown()
void imu_callback(const rosabridge_msgs::Imu::ConstPtr &imuMsg)
void mySigintHandler(int sig)


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