imu_provider.cpp
Go to the documentation of this file.
00001 #include <ros/ros.h>
00002 #include <sensor_msgs/Imu.h>
00003 #include <geometry_msgs/Vector3Stamped.h>
00004 #include <message_filters/subscriber.h>
00005 #include <message_filters/synchronizer.h>
00006 #include <message_filters/sync_policies/approximate_time.h>
00007 #include <tf/transform_listener.h>
00008 #include <string>
00009 #include <utility>
00010 
00011 // robot's index in ROS ecosystem (if multiple instances)
00012 std::string tf_prefix;
00013 
00014 // publisher in IMU for imu message
00015 ros::Publisher publisher;
00016 
00017 // listener for tf transforms from gyroscope and accelerometer
00018 tf::TransformListener *tf_listener;
00019 
00025 void imuData_cb(const geometry_msgs::Vector3Stamped::ConstPtr& accel_msg, const geometry_msgs::Vector3Stamped::ConstPtr& gyro_msg)
00026 {
00027   sensor_msgs::Imu imu_msg;
00028   geometry_msgs::Vector3Stamped transformed_vec;
00029   double c = 10e-4;
00030   // target frame for whole message
00031   std::string target_frame = tf_prefix + "/base_link";
00032 
00033   // curently we have no orientation data (missing compass)
00034   c=10e4;
00035   imu_msg.orientation_covariance={c,0,0,
00036                                   0,c,0,
00037                                   0,0,c};
00038 
00039   // data from accelerometer, uknown covarince matrix ATM
00040   try
00041   {
00042     // transform accel data
00043     tf_listener->transformVector(target_frame, *accel_msg, transformed_vec);
00044     imu_msg.linear_acceleration = transformed_vec.vector;
00045     c =10e-4;
00046     imu_msg.linear_acceleration_covariance ={c,0,0,
00047                                              0,c,0,
00048                                              0,0,c};
00049   } catch (const tf::TransformException& ex) {
00050     ROS_WARN("can't transform accelerometer message: %s", ex.what());
00051   }
00052 
00053   // data from gyroscope, uknown covarince matrix ATM
00054   try
00055   {
00056     // transform gyro data
00057     tf_listener->transformVector(target_frame, *gyro_msg, transformed_vec);
00058     imu_msg.angular_velocity = transformed_vec.vector;
00059     c =10e-4;
00060     imu_msg.angular_velocity_covariance ={c,0,0,
00061                                           0,c,0,
00062                                           0,0,c};
00063   } catch (const tf::TransformException& ex) {
00064     ROS_WARN("can't transform gyroscope message: %s", ex.what());
00065   }
00066 
00067   // header
00068   imu_msg.header.stamp = ros::Time::now(); // current time of data collection
00069   imu_msg.header.frame_id = target_frame;
00070 
00071   publisher.publish(std::move(imu_msg));
00072 }
00073 
00074 int main(int argc, char **argv)
00075 {
00076   ros::init(argc, argv, "imuProvider");
00077   ros::NodeHandle n; // we want relative namespace
00078 
00079   // init parameters
00080   std::string tf_prefix_path;
00081   if (n.searchParam("tf_prefix", tf_prefix_path))
00082   {
00083     n.getParam(tf_prefix_path, tf_prefix);
00084   }
00085 
00086   // initialize tf_listener
00087   tf::TransformListener tf_listener_;
00088   tf_listener = &tf_listener_;
00089 
00090   // subscribers to acceleromer and gyroscope
00091   message_filters::Subscriber<geometry_msgs::Vector3Stamped> accel_sub (n, "hal/accelerometer/sensor0/axis_data", 1000);
00092   message_filters::Subscriber<geometry_msgs::Vector3Stamped> gyro_sub (n, "hal/gyro/sensor0/axis_data", 1000);
00093 
00094   // sync messages using approximate alghorithm
00095   constexpr int sync_delay = 50;
00096   typedef message_filters::sync_policies::ApproximateTime<geometry_msgs::Vector3Stamped, geometry_msgs::Vector3Stamped> ImuSyncPolicy;
00097   message_filters::Synchronizer<ImuSyncPolicy> imu_processor (ImuSyncPolicy(sync_delay), accel_sub, gyro_sub);
00098   imu_processor.registerCallback(imuData_cb);
00099 
00100   // publish fused message
00101   publisher = n.advertise<sensor_msgs::Imu>("imu_data", 1000);
00102 
00103   ROS_INFO("DPL: imuProvider initialized");
00104 
00105   // runs event loop
00106   ros::spin();
00107 
00108   return 0;
00109 }


p3dx_dpl
Author(s):
autogenerated on Sat Jun 8 2019 20:22:37