frames_tf_broadcaster.cpp
Go to the documentation of this file.
1 #include <mutex>
2 
4 #include <ram_utils/GetTrajectoryFrame.h>
5 #include <ram_utils/GetStartPose.h>
6 #include <ram_utils/GetTool.h>
7 #include <ros/ros.h>
9 
12 
13 std::mutex start_pose_mutex;
15 
16 std::mutex tool_mutex;
18 
19 bool getStartPose(ram_utils::GetStartPose::Request &,
20  ram_utils::GetStartPose::Response &res)
21 {
22  std::lock_guard<std::mutex> lock(start_pose_mutex);
23  res.pose = start_pose;
24  return true;
25 }
26 
27 bool getTrajectoryFrame(ram_utils::GetTrajectoryFrame::Request &,
28  ram_utils::GetTrajectoryFrame::Response &res)
29 {
30  std::lock_guard<std::mutex> lock(trajectory_frame_mutex);
31  res.pose = trajectory_frame;
32  return true;
33 }
34 
35 bool getTool(ram_utils::GetTool::Request &,
36  ram_utils::GetTool::Response &res)
37 {
38  std::lock_guard<std::mutex> lock(tool_mutex);
39  res.pose = tool;
40  return true;
41 }
42 
43 void updateTrajectoryFrameTFCallback(const geometry_msgs::Pose::ConstPtr& msg)
44 {
45  std::lock_guard<std::mutex> lock(trajectory_frame_mutex);
46  trajectory_frame = *msg;
47 }
48 
49 void updateStartPoseTFCallback(const geometry_msgs::Pose::ConstPtr& msg)
50 {
51  std::lock_guard<std::mutex> lock(start_pose_mutex);
52  start_pose = *msg;
53 }
54 
55 void updateToolTFCallback(const geometry_msgs::Pose::ConstPtr& msg)
56 {
57  std::lock_guard<std::mutex> lock(tool_mutex);
58  tool = *msg;
59 }
60 
61 int main(int argc,
62  char** argv)
63 {
64  ros::init(argc, argv, "frames_tf_broadcaster");
66 
67  trajectory_frame.position.x = 0;
68  trajectory_frame.position.y = 0;
69  trajectory_frame.position.z = 0;
70  trajectory_frame.orientation.x = 0;
71  trajectory_frame.orientation.y = 0;
72  trajectory_frame.orientation.z = 0;
73  trajectory_frame.orientation.w = 1;
74 
75  start_pose.position.x = 0;
76  start_pose.position.y = 0;
77  start_pose.position.z = 0;
78  start_pose.orientation.x = 0;
79  start_pose.orientation.y = 0;
80  start_pose.orientation.z = 0;
81  start_pose.orientation.w = 1;
82 
83  tool.position.x = 0;
84  tool.position.y = 0;
85  tool.position.z = 0;
86  tool.orientation.x = 0;
87  tool.orientation.y = 0;
88  tool.orientation.z = 0;
89  tool.orientation.w = 1;
90 
92  geometry_msgs::TransformStamped transform_stamped;
93 
94  ros::Subscriber sub_1 = nh.subscribe("ram/trajectory_frame", 10, updateTrajectoryFrameTFCallback);
95  ros::Subscriber sub_2 = nh.subscribe("ram/start_pose", 10, updateStartPoseTFCallback);
96  ros::Subscriber sub_3 = nh.subscribe("ram/tool", 10, updateToolTFCallback);
97 
98  ros::ServiceServer server_1 = nh.advertiseService("ram/get_start_pose", getStartPose);
99  ros::ServiceServer server_2 = nh.advertiseService("ram/get_trajectory_frame", getTrajectoryFrame);
100  ros::ServiceServer server_3 = nh.advertiseService("ram/get_tool", getTool);
101 
102  ros::Rate rate(100);
103  while (nh.ok())
104  {
105  // Trajectory frame
106  {
107  std::lock_guard<std::mutex> lock(trajectory_frame_mutex);
108  transform_stamped.header.frame_id = "base";
109  transform_stamped.child_frame_id = "trajectory_frame";
110  transform_stamped.transform.translation.x = trajectory_frame.position.x;
111  transform_stamped.transform.translation.y = trajectory_frame.position.y;
112  transform_stamped.transform.translation.z = trajectory_frame.position.z;
113  transform_stamped.transform.rotation = trajectory_frame.orientation;
114  }
115  transform_stamped.header.stamp = ros::Time::now();
116  br.sendTransform(transform_stamped);
117  ros::spinOnce();
118 
119  // Start pose
120  {
121  std::lock_guard<std::mutex> lock(start_pose_mutex);
122  transform_stamped.header.frame_id = "trajectory_frame";
123  transform_stamped.child_frame_id = "start_pose";
124  transform_stamped.transform.translation.x = start_pose.position.x;
125  transform_stamped.transform.translation.y = start_pose.position.y;
126  transform_stamped.transform.translation.z = start_pose.position.z;
127  transform_stamped.transform.rotation = start_pose.orientation;
128  }
129  transform_stamped.header.stamp = ros::Time::now();
130  br.sendTransform(transform_stamped);
131  ros::spinOnce();
132 
133  // Tool orientation
134  {
135  std::lock_guard<std::mutex> lock(start_pose_mutex);
136  transform_stamped.header.frame_id = "start_pose";
137  transform_stamped.child_frame_id = "tool_orientation";
138  transform_stamped.transform.translation.x = tool.position.x;
139  transform_stamped.transform.translation.y = tool.position.y;
140  transform_stamped.transform.translation.z = tool.position.z;
141  transform_stamped.transform.rotation = tool.orientation;
142  }
143  transform_stamped.header.stamp = ros::Time::now();
144  br.sendTransform(transform_stamped);
145  ros::spinOnce();
146 
147  rate.sleep();
148  }
149  return 0;
150 }
bool getStartPose(ram_utils::GetStartPose::Request &, ram_utils::GetStartPose::Response &res)
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
geometry_msgs::Pose tool
Eigen::Isometry3d Pose
std::unique_ptr< ros::NodeHandle > nh
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
geometry_msgs::Pose start_pose
ServiceServer advertiseService(const std::string &service, bool(T::*srv_func)(MReq &, MRes &), T *obj)
geometry_msgs::Pose trajectory_frame
bool getTool(ram_utils::GetTool::Request &, ram_utils::GetTool::Response &res)
std::mutex trajectory_frame_mutex
int main(int argc, char **argv)
void sendTransform(const geometry_msgs::TransformStamped &transform)
void updateStartPoseTFCallback(const geometry_msgs::Pose::ConstPtr &msg)
bool sleep()
bool getTrajectoryFrame(ram_utils::GetTrajectoryFrame::Request &, ram_utils::GetTrajectoryFrame::Response &res)
void updateToolTFCallback(const geometry_msgs::Pose::ConstPtr &msg)
static Time now()
std::mutex start_pose_mutex
bool ok() const
void updateTrajectoryFrameTFCallback(const geometry_msgs::Pose::ConstPtr &msg)
ROSCPP_DECL void spinOnce()
std::mutex tool_mutex


ram_utils
Author(s): Victor Lamoine - Institut Maupertuis
autogenerated on Mon Jun 10 2019 14:49:54