-
Notifications
You must be signed in to change notification settings - Fork 0
/
tf_publisher.cpp
134 lines (101 loc) · 3.19 KB
/
tf_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
133
#include "ros/ros.h"
#include <math.h>
#include <time.h>
#include "std_msgs/String.h"
//#include "first_project/Odom.h"
#include "nav_msgs/Odometry.h"
#include <tf/transform_broadcaster.h>
#include "second_project/Reset.h"
#include <sstream>
class odomNode{
private:
ros::NodeHandle n;
ros::Subscriber sub;
ros::Publisher pub;
ros::Publisher customPub;
tf::TransformBroadcaster br;
ros::ServiceServer resetter;
double L = 0.6;
double B = 0.4;
double x;
double y;
double z;
geometry_msgs::Quaternion th;
//ros::Time last_time;
public:
odomNode(){
//current_time = ros::Time::now();
sub = n.subscribe("/t265/odom", 1000, &odomNode::subscriber, this);
pub = n.advertise<nav_msgs::Odometry>("/tf_publisher", 1000);
//customPub = n.advertise<first_project::Odom>("/custom_odometry", 1000);
resetter = n.advertiseService("reset_odom", &odomNode::reset, this);
}
//Subscriber from bags
void subscriber(const nav_msgs::Odometry& msg){
ros::Time current_time = ros::Time::now();
x = msg.pose.pose.position.x;
y = msg.pose.pose.position.y;
z = msg.pose.pose.position.z;
th = msg.pose.pose.orientation;
// Create a transform message
geometry_msgs::TransformStamped transform;
transform.header = msg.header;
transform.child_frame_id = "base_footprint"; // Replace with your desired frame ID
transform.transform.translation.x = x;
transform.transform.translation.y = y;
transform.transform.translation.z = z;
transform.transform.rotation = th;
// tf::Transform transform2;
// tf2::fromMsg(transform, transform2);
// Extract position and orientation from the Odometry message
geometry_msgs::Pose pose = msg.pose.pose;
geometry_msgs::Point position = pose.position;
geometry_msgs::Quaternion orientation = pose.orientation;
// Create a tf::Vector3 for position and tf::Quaternion for orientation
tf::Vector3 tf_position(position.x, position.y, position.z);
tf::Quaternion tf_orientation(orientation.x, orientation.y, orientation.z, orientation.w);
// Create a tf::Transform from the position and orientation
tf::Transform transform2(tf_orientation, tf_position);
// Broadcast the transform
//static tf::TransformBroadcaster br;
// Broadcast the transform
br.sendTransform(tf::StampedTransform(transform2, ros::Time::now(), "odom", "base_footprint"));
ROS_INFO("tf published");
}
/*
void publishTf(nav_msgs::Odometry& msg){
ros::Time current_time = ros::Time::now();
x = msg.pose.pose.position.x;
y = msg.pose.pose.position.y;
z = msg.pose.pose.position.z;
th = msg.pose.pose.orientation;
transform.setOrigin( tf::Vector3(x, y, 0) );
tf::Quaternion q;
q.setRPY(0, 0, th);
transform.setRotation(q);
br.sendTransform(tf::StampedTransform(transform, ros::Time::now(), "t265", "base_footprint"));
ROS_INFO("tf published");
}
*/
// Compute the reset
bool reset(second_project::Reset::Request &req, second_project::Reset::Response &res){
x = 0.0;
y = 0.0;
z = 0.0;
th.x= 0.0;
th.y= 0.0;
th.z= 0.0;
th.w= 0.0;
res.resetted =true;
ROS_INFO("Odometry resetted");
return true;
}
};
int main(int argc, char **argv)
{
ros::init(argc, argv, "tf_publisher");
ROS_INFO("node started");
odomNode odomNode;
ros::spin();
return 0;
}