imu_proxy_node.cpp
Go to the documentation of this file.
00001 #include <ros/ros.h>
00002 #include "rosabridge_msgs/Imu.h"
00003 #include "rosabridge_msgs/ImuCovariances.h"
00004 #include "rosabridge_msgs/RequestImuCovariances.h"
00005 #include <sensor_msgs/Imu.h>
00006 #include <signal.h>
00007 #include <string>
00008 
00009 // CBA Define following to enable service for obtaining covariances
00010 // (not currently supported by rosserial on Arduino)
00011 //#define _IMU_COVAR
00012 
00013 void mySigintHandler(int sig)
00014 {
00015   ROS_INFO("Received SIGINT signal, shutting down...");
00016   ros::shutdown();
00017 }
00018 
00019 class ImuProxyNode
00020 {
00021 
00022 public:
00023   ImuProxyNode();
00024 
00025 public:
00026   void imu_callback(const rosabridge_msgs::Imu::ConstPtr& imuMsg);
00027   int run();
00028 
00029 protected:
00030   ros::NodeHandle nh;
00031   ros::Publisher imu_pub;
00032   ros::Subscriber imu_sub;
00033   sensor_msgs::Imu imu_msg;
00034 
00035 #ifdef _IMU_COVAR
00036   ros::ServiceClient imu_client;
00037 #endif
00038 
00039   std::string imu_frame;
00040   std::string imu_topic;
00041 
00042 };
00043 
00044 ImuProxyNode::ImuProxyNode()
00045 {
00046 
00047   // CBA Read local params (from launch file)
00048   ros::NodeHandle nhLocal("~");
00049   nhLocal.param<std::string>("imu_frame", imu_frame, "");
00050   ROS_INFO_STREAM("imu_frame: " << imu_frame);
00051   nhLocal.param<std::string>("imu_topic", imu_topic, "imu/data");
00052   ROS_INFO_STREAM("imu_topic: " << imu_topic);
00053 
00054   // CBA Initially zero out covariances
00055   imu_msg.orientation_covariance.assign(0);
00056   imu_msg.angular_velocity_covariance.assign(0);
00057   imu_msg.linear_acceleration_covariance.assign(0);
00058 
00059 }
00060 
00061 void ImuProxyNode::imu_callback(const rosabridge_msgs::Imu::ConstPtr& imuMsg)
00062 {
00063   imu_msg.header.stamp = imuMsg->header.stamp;
00064   if ( !imu_frame.empty() )
00065     imu_msg.header.frame_id = imu_frame;
00066   else
00067     imu_msg.header.frame_id = imuMsg->header.frame_id;
00068   imu_msg.orientation.x = imuMsg->orientation.x;
00069   imu_msg.orientation.y = imuMsg->orientation.y;
00070   imu_msg.orientation.z = imuMsg->orientation.z;
00071   imu_msg.orientation.w = imuMsg->orientation.w;
00072   imu_msg.linear_acceleration.x = imuMsg->linear_acceleration.x;
00073   imu_msg.linear_acceleration.y = imuMsg->linear_acceleration.y;
00074   imu_msg.linear_acceleration.z = imuMsg->linear_acceleration.z;
00075   imu_msg.angular_velocity.x = imuMsg->angular_velocity.x;
00076   imu_msg.angular_velocity.y = imuMsg->angular_velocity.y;
00077   imu_msg.angular_velocity.z = imuMsg->angular_velocity.z;
00078   imu_msg.orientation_covariance[0] = imuMsg->orientation_covariance[0];
00079   imu_msg.orientation_covariance[4] = imuMsg->orientation_covariance[1];
00080   imu_msg.orientation_covariance[8] = imuMsg->orientation_covariance[2];
00081   imu_msg.linear_acceleration_covariance[0] = imuMsg->linear_acceleration_covariance[0];
00082   imu_msg.linear_acceleration_covariance[4] = imuMsg->linear_acceleration_covariance[1];
00083   imu_msg.linear_acceleration_covariance[8] = imuMsg->linear_acceleration_covariance[2];
00084   imu_msg.angular_velocity_covariance[0] = imuMsg->angular_velocity_covariance[0];
00085   imu_msg.angular_velocity_covariance[4] = imuMsg->angular_velocity_covariance[1];
00086   imu_msg.angular_velocity_covariance[8] = imuMsg->angular_velocity_covariance[2];
00087   imu_pub.publish(imu_msg);
00088 }
00089 
00090 int ImuProxyNode::run()
00091 {
00092 
00093 #ifdef _IMU_COVAR
00094   ROS_INFO("Requesting Imu Covariances");
00095   imu_client = nh.serviceClient<rosabridge_msgs::RequestImuCovariances>("rosabridge/imu_covar_srv");
00096   rosabridge_msgs::RequestImuCovariances imuCovarSrv;
00097   if (imu_client.call(imuCovarSrv))
00098   {
00099     imu_msg.orientation_covariance[0] = imuCovarSrv.response.orientation_covariance[0];
00100     imu_msg.orientation_covariance[1] = imuCovarSrv.response.orientation_covariance[1];
00101     imu_msg.orientation_covariance[2] = imuCovarSrv.response.orientation_covariance[2];
00102     imu_msg.orientation_covariance[3] = imuCovarSrv.response.orientation_covariance[3];
00103     imu_msg.orientation_covariance[4] = imuCovarSrv.response.orientation_covariance[4];
00104     imu_msg.orientation_covariance[5] = imuCovarSrv.response.orientation_covariance[5];
00105     imu_msg.orientation_covariance[6] = imuCovarSrv.response.orientation_covariance[6];
00106     imu_msg.orientation_covariance[7] = imuCovarSrv.response.orientation_covariance[7];
00107     imu_msg.orientation_covariance[8] = imuCovarSrv.response.orientation_covariance[8];
00108     imu_msg.linear_acceleration_covariance[0] = imuCovarSrv.response.linear_acceleration_covariance[0];
00109     imu_msg.linear_acceleration_covariance[1] = imuCovarSrv.response.linear_acceleration_covariance[1];
00110     imu_msg.linear_acceleration_covariance[2] = imuCovarSrv.response.linear_acceleration_covariance[2];
00111     imu_msg.linear_acceleration_covariance[3] = imuCovarSrv.response.linear_acceleration_covariance[3];
00112     imu_msg.linear_acceleration_covariance[4] = imuCovarSrv.response.linear_acceleration_covariance[4];
00113     imu_msg.linear_acceleration_covariance[5] = imuCovarSrv.response.linear_acceleration_covariance[5];
00114     imu_msg.linear_acceleration_covariance[6] = imuCovarSrv.response.linear_acceleration_covariance[6];
00115     imu_msg.linear_acceleration_covariance[7] = imuCovarSrv.response.linear_acceleration_covariance[7];
00116     imu_msg.linear_acceleration_covariance[8] = imuCovarSrv.response.linear_acceleration_covariance[8];
00117     imu_msg.angular_velocity_covariance[0] = imuCovarSrv.response.angular_velocity_covariance[0];
00118     imu_msg.angular_velocity_covariance[1] = imuCovarSrv.response.angular_velocity_covariance[1];
00119     imu_msg.angular_velocity_covariance[2] = imuCovarSrv.response.angular_velocity_covariance[2];
00120     imu_msg.angular_velocity_covariance[3] = imuCovarSrv.response.angular_velocity_covariance[3];
00121     imu_msg.angular_velocity_covariance[4] = imuCovarSrv.response.angular_velocity_covariance[4];
00122     imu_msg.angular_velocity_covariance[5] = imuCovarSrv.response.angular_velocity_covariance[5];
00123     imu_msg.angular_velocity_covariance[6] = imuCovarSrv.response.angular_velocity_covariance[6];
00124     imu_msg.angular_velocity_covariance[7] = imuCovarSrv.response.angular_velocity_covariance[7];
00125     imu_msg.angular_velocity_covariance[8] = imuCovarSrv.response.angular_velocity_covariance[8];
00126   }
00127   else
00128   {
00129     ROS_WARN("Failed to retrieve Imu Covariances");
00130   }
00131 #endif
00132 
00133   ROS_INFO_STREAM("Publishing to topic " << imu_topic);
00134   imu_pub = nh.advertise<sensor_msgs::Imu>(imu_topic, 1000);
00135   ROS_INFO("Subscribing to topic rosabridge/imu");
00136   imu_sub = nh.subscribe("rosabridge/imu", 1000, &ImuProxyNode::imu_callback, this);
00137 
00138   ROS_INFO("Relaying between topics...");
00139   ros::spin();
00140 
00141   ROS_INFO("Exiting");
00142 
00143   return 0;
00144 }
00145 
00146 int main(int argc, char **argv)
00147 {
00148 
00149   ros::init(argc, argv, "imu_proxy_node");
00150 
00151   ImuProxyNode node;
00152 
00153   // Override the default ros sigint handler.
00154   // This must be set after the first NodeHandle is created.
00155   signal(SIGINT, mySigintHandler);
00156 
00157   return node.run();
00158 }
00159 


rosabridge_server
Author(s): Chad Attermann
autogenerated on Thu Jun 6 2019 20:29:43