-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworld_odom_publisher.cpp
132 lines (97 loc) · 3.74 KB
/
world_odom_publisher.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#ifndef _WORLD_ODOM_PUBLISHER_HH_
#define _WORLD_ODOM_PUBLISHER_HH_
#include <functional>
#include <gazebo/gazebo.hh>
#include <ignition/math/Pose3.hh>
#include <ignition/math/Vector3.hh>
#include <gazebo/physics/physics.hh>
#include <gazebo/common/common.hh>
#include <stdio.h>
#include <ros/ros.h>
#include <tf/transform_broadcaster.h>
#include <nav_msgs/Odometry.h>
using namespace gazebo;
class WorldOdomPublisher : public ModelPlugin {
private: physics::ModelPtr model;
private: event::ConnectionPtr updateConnection;
private: std::unique_ptr<ros::NodeHandle> node;
private: ros::Publisher pub;
private: tf::TransformBroadcaster tf;
private: ros::Time lastTime;
private: double publishFreq = 10.0; // publish_frequency
public: void Load(physics::ModelPtr _model, sdf::ElementPtr _sdf) {
if (_sdf->HasElement("world_odom")) {
std::cout << "Real robot odometry is used.\n";
} else {
std::cout << "Controller odometry is used.\n";
return;
}
if (_sdf->HasElement("publish_frequency")) {
publishFreq = _sdf->Get<double>("publish_frequency");
}
model = _model;
updateConnection = event::Events::ConnectWorldUpdateBegin(
std::bind(&WorldOdomPublisher::OnUpdate, this));
lastTime = ros::Time::now();
// initialize ros
std::string ns = "world_odom_publisher";
if (!ros::isInitialized()) {
int argc = 0;
char **argv = NULL;
ros::init(argc, argv, ns, ros::init_options::NoSigintHandler);
}
node.reset(new ros::NodeHandle(ns));
pub = node->advertise<nav_msgs::Odometry>("odom", 50);
}
public: void OnUpdate() {
ros::Time currentTime = ros::Time::now();
double deltaSecs = currentTime.toSec() - lastTime.toSec();
double maxSecs = 0.0;
if (publishFreq != 0.0) {
maxSecs = 1.0 / publishFreq;
}
if (deltaSecs > maxSecs && deltaSecs != 0.0) {
lastTime = currentTime;
PublishOdom(currentTime);
}
}
private: void PublishOdom(ros::Time& currentTime) {
ignition::math::Pose3d pose = model->WorldPose();
double x = pose.Pos().X();
double y = pose.Pos().Y();
double th = pose.Rot().Yaw();
ignition::math::Vector3d linearVel = model->WorldLinearVel();
double vx = linearVel.X();
double vy = linearVel.Y();
ignition::math::Vector3d angularVel = model->WorldAngularVel();
double vth = angularVel.Z();
// std::cout << "Pose: [x=" << x << ", y=" << y << ", th=" << th << "]\n";
// std::cout << "Twist: [vx=" << vx << ", vy=" << vy << ", vth=" << vth << "]\n";
geometry_msgs::Quaternion q = tf::createQuaternionMsgFromYaw(th);
// transformation frame
geometry_msgs::TransformStamped ts;
ts.header.stamp = currentTime;
ts.header.frame_id = "odom";
ts.child_frame_id = "base_link_vis";
ts.transform.translation.x = x;
ts.transform.translation.y = y;
ts.transform.translation.z = 0.0;
ts.transform.rotation = q;
tf.sendTransform(ts);
// publish odometry topic
nav_msgs::Odometry odom;
odom.header.stamp = currentTime;
odom.header.frame_id = "odom";
odom.child_frame_id = "base_link_vis";
odom.pose.pose.position.x = x;
odom.pose.pose.position.y = y;
odom.pose.pose.position.z = 0.0;
odom.pose.pose.orientation = q;
odom.twist.twist.linear.x = vx;
odom.twist.twist.linear.y = vy;
odom.twist.twist.angular.z = vth;
pub.publish(odom);
}
};
GZ_REGISTER_MODEL_PLUGIN(WorldOdomPublisher)
#endif