pose_tracker/main.cpp
Go to the documentation of this file.
1 #include <ros/ros.h>
2 #include <sensor_msgs/Imu.h>
3 #include <nav_msgs/Odometry.h>
4 #include <geometry_msgs/PoseWithCovarianceStamped.h>
5 #include <tf/transform_broadcaster.h>
6 #include <tf/transform_listener.h>
7 #include <memory>
8 #include <iostream>
9 
10 using namespace tf;
11 using namespace std;
12 
13 ros::Publisher pose_pub;
14 ros::Publisher origin_pub;
15 std::unique_ptr<TransformBroadcaster> tf_broadcaster;
16 std::unique_ptr<TransformListener> tf_listener;
17 
18 geometry_msgs::Quaternion orientation;
19 nav_msgs::Odometry gps;
20 
21 geometry_msgs::Point origin;
22 bool first = true;
23 
25 {
26  return orientation.w != 0 ||
27  orientation.x != 0 ||
28  orientation.y != 0 ||
29  orientation.z != 0;
30 }
31 
33 {
34  if(!orientationIsValid())
35  return;
36  geometry_msgs::PoseStamped pose;
37  pose.header.frame_id = "/base_footprint";
38  pose.pose.position.x = gps.pose.pose.position.x;
39  pose.pose.position.y = gps.pose.pose.position.y;
40  pose.pose.orientation = orientation;
41  pose_pub.publish(pose);
42 
43  Transform transform;
44  transform.setOrigin(Vector3(pose.pose.position.x, pose.pose.position.y, 0.0));
45  Quaternion q;
46  quaternionMsgToTF(pose.pose.orientation, q);
47  transform.setRotation(q);
48  tf_broadcaster->sendTransform(StampedTransform(transform, ros::Time::now(), "map", "base_footprint"));
49 }
50 
51 void imu_callback(const sensor_msgs::ImuConstPtr& msg)
52 {
53  geometry_msgs::QuaternionStamped q;
54  q.header.stamp = ros::Time(0);
55  q.header.frame_id = msg->header.frame_id;
56  q.quaternion = msg->orientation;
57  tf_listener->waitForTransform("base_footprint", msg->header.frame_id, ros::Time(0), ros::Duration(10.0));
58  geometry_msgs::QuaternionStamped transformed;
59  tf_listener->transformQuaternion("base_footprint", q, transformed);
60  orientation = msg->orientation;//transformed.quaternion;
61 }
62 
63 void gps_callback(const nav_msgs::OdometryConstPtr& msg)
64 {
65  gps = *msg;
66 
67  tf_listener->waitForTransform("base_footprint", msg->header.frame_id, ros::Time(0), ros::Duration(10.0));
68  geometry_msgs::PoseStamped p;
69  p.header.stamp = ros::Time(0);
70  p.header.frame_id = msg->header.frame_id;
71  p.pose = msg->pose.pose;
72  geometry_msgs::PoseStamped transformed;
73  tf_listener->transformPose("base_footprint", p, transformed);
74  gps.pose.pose = transformed.pose;
75 
76  if(first)
77  {
78  origin = gps.pose.pose.position;
79  first = false;
80  } else {
81  gps.pose.pose.position.x -= origin.x;
82  gps.pose.pose.position.y -= origin.y;
83  gps.pose.pose.position.z -= origin.z;
84  }
85 }
86 
87 int main(int argc, char** argv)
88 {
89  ros::init(argc, argv, "pose_tracker");
90 
91  tf_broadcaster = std::unique_ptr<TransformBroadcaster>(new TransformBroadcaster);
92 
93  tf_listener = std::unique_ptr<TransformListener>(new TransformListener);
94 
95  ros::NodeHandle nh;
96 
97  ros::Subscriber imu_sub = nh.subscribe("/imu", 1, imu_callback);
98 
99  ros::Subscriber gps_sub = nh.subscribe("/gps_odom", 1, gps_callback);
100 
101  pose_pub = nh.advertise<geometry_msgs::PoseStamped>("/odom_combined", 1);
102 
103  origin_pub = nh.advertise<geometry_msgs::PointStamped>("/map_origin", 1);
104 
105  // Publish initial pose
106  orientation.x = 0;
107  orientation.y = 0;
108  orientation.z = 1;
109  orientation.w = 0;
110  publish_pose();
111 
112  ros::Rate rate(20); // 20 Hz
113  while(ros::ok())
114  {
115  ros::spinOnce();
116 
117  geometry_msgs::PointStamped msg;
118  msg.header.stamp = ros::Time::now();
119  msg.header.frame_id = "/world";
120  msg.point = origin;
121  origin_pub.publish(msg);
122 
123  publish_pose();
124 
125  rate.sleep();
126  }
127 
128  return 0;
129 }
std::unique_ptr< TransformBroadcaster > tf_broadcaster
ros::Publisher pose_pub
geometry_msgs::Quaternion orientation
nav_msgs::Odometry gps
ros::Publisher origin_pub
geometry_msgs::Point origin
std::unique_ptr< TransformListener > tf_listener
void gps_callback(const nav_msgs::OdometryConstPtr &msg)
int main(int argc, char **argv)
void imu_callback(const sensor_msgs::ImuConstPtr &msg)
void publish_pose()
bool orientationIsValid()
bool first


igvc
Author(s): Matthew Barulic , Al Chaussee
autogenerated on Sun May 10 2015 16:18:45