|
| 1 | + |
| 2 | +/* |
| 3 | + * Software License Agreement (BSD License) |
| 4 | + * |
| 5 | + * Copyright (c) 2016, Fraunhofer IPA. |
| 6 | + * All rights reserved. |
| 7 | + * |
| 8 | + * Redistribution and use in source and binary forms, with or without |
| 9 | + * modification, are permitted provided that the following conditions |
| 10 | + * are met: |
| 11 | + * |
| 12 | + * * Redistributions of source code must retain the above copyright |
| 13 | + * notice, this list of conditions and the following disclaimer. |
| 14 | + * * Redistributions in binary form must reproduce the above |
| 15 | + * copyright notice, this list of conditions and the following |
| 16 | + * disclaimer in the documentation and/or other materials provided |
| 17 | + * with the distribution. |
| 18 | + * * Neither the name of the copyright holder nor the names of its |
| 19 | + * contributors may be used to endorse or promote products derived |
| 20 | + * from this software without specific prior written permission. |
| 21 | + * |
| 22 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 23 | + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 24 | + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 25 | + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 26 | + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 27 | + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 28 | + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 29 | + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 30 | + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 31 | + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
| 32 | + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 33 | + * POSSIBILITY OF SUCH DAMAGE. |
| 34 | + * |
| 35 | + * |
| 36 | + */ |
| 37 | + |
| 38 | +/* |
| 39 | + * Author: Sofie Nilsson |
| 40 | + */ |
| 41 | + |
1 | 42 | #include <pointcloud_to_laserscan/frame_publisher.h>
|
2 | 43 |
|
3 | 44 | bool FramePublisher::initialize()
|
4 | 45 | {
|
5 |
| - priv_nh_ = ros::NodeHandle("~"); |
| 46 | + priv_nh_ = ros::NodeHandle("~"); |
| 47 | + |
| 48 | + priv_nh_.param<double>("update_rate", update_rate_, 0.01); // 100Hz |
| 49 | + |
| 50 | + priv_nh_.param<std::string>("base_frame", base_frame_, "base_link"); |
| 51 | + priv_nh_.param<std::string>("rotation_frame", rotation_frame_, "torso_center_link"); |
| 52 | + priv_nh_.param<std::string>("target_frame", target_frame_, "torso_rotated_base_link"); |
6 | 53 |
|
7 |
| - priv_nh_.param<double>("update_rate", update_rate_, 0.01); // 100Hz |
| 54 | + priv_nh_.param<bool>("rot_z", rot_z_, false); |
| 55 | + priv_nh_.param<bool>("rot_x", rot_x_, false); |
| 56 | + priv_nh_.param<bool>("rot_y", rot_y_, false); |
8 | 57 |
|
9 |
| - priv_nh_.param<std::string>("base_frame", base_frame_, "base_link"); |
10 |
| - priv_nh_.param<std::string>("rotation_frame", rotation_frame_, "torso_center_link"); |
11 |
| - priv_nh_.param<std::string>("target_frame", target_frame_, "torso_rotated_base_link"); |
12 |
| - |
13 |
| - priv_nh_.param<bool>("rot_z", rot_z_, false); |
14 |
| - priv_nh_.param<bool>("rot_x", rot_x_, false); |
15 |
| - priv_nh_.param<bool>("rot_y", rot_y_, false); |
16 |
| - |
17 |
| - frame_broadcast_timer_ = nh_.createTimer(ros::Duration(update_rate_), &FramePublisher::frameBroadcastCallback, this); |
| 58 | + frame_broadcast_timer_ = nh_.createTimer(ros::Duration(update_rate_), &FramePublisher::frameBroadcastCallback, this); |
18 | 59 |
|
19 |
| - ros::Duration(1.0).sleep(); //give tf_listener some time |
| 60 | + ros::Duration(1.0).sleep(); //give tf_listener some time |
20 | 61 |
|
21 |
| - return true; |
| 62 | + return true; |
22 | 63 | }
|
23 | 64 |
|
24 | 65 | /// Broadcast a frame aligned with the base frame but rotated around specified axes as rotation_frame
|
25 | 66 | void FramePublisher::frameBroadcastCallback(const ros::TimerEvent& event)
|
26 | 67 | {
|
27 |
| - tf::StampedTransform frame_transform; |
28 |
| - try |
29 |
| - { |
30 |
| - tf_listener_.waitForTransform(base_frame_, rotation_frame_, event.current_real, ros::Duration(0.1)); |
31 |
| - tf_listener_.lookupTransform(base_frame_, rotation_frame_, ros::Time(0), frame_transform); |
32 |
| - } |
33 |
| - catch (tf::TransformException& ex) |
34 |
| - { |
35 |
| - ROS_ERROR("FramePublisher::getTransform: \n%s", ex.what()); |
36 |
| - return; |
37 |
| - } |
38 |
| - |
39 |
| - double rot_frame_roll, rot_frame_pitch, rot_frame_yaw; |
40 |
| - frame_transform.getBasis().getRPY(rot_frame_roll, rot_frame_pitch, rot_frame_yaw); |
41 |
| - tf::Transform target_transform(frame_transform.getRotation()); |
42 |
| - |
43 |
| - // Use rotations according to settings |
44 |
| - double target_frame_roll = 0; |
45 |
| - double target_frame_pitch = 0; |
46 |
| - double target_frame_yaw = 0; |
47 |
| - if (rot_z_){ target_frame_yaw = rot_frame_yaw;} |
48 |
| - if (rot_x_){ target_frame_roll = rot_frame_roll;} |
49 |
| - if (rot_y_){ target_frame_pitch = rot_frame_pitch;} |
50 |
| - |
51 |
| - target_transform.getBasis().setRPY(target_frame_roll, target_frame_pitch, target_frame_yaw); |
52 |
| - |
53 |
| - // Broadcast new frame |
54 |
| - tf_broadcaster_.sendTransform(tf::StampedTransform(target_transform, frame_transform.stamp_, base_frame_, target_frame_)); |
| 68 | + tf::StampedTransform frame_transform; |
| 69 | + try |
| 70 | + { |
| 71 | + tf_listener_.waitForTransform(base_frame_, rotation_frame_, event.current_real, ros::Duration(0.1)); |
| 72 | + tf_listener_.lookupTransform(base_frame_, rotation_frame_, ros::Time(0), frame_transform); |
| 73 | + } |
| 74 | + catch (tf::TransformException& ex) |
| 75 | + { |
| 76 | + ROS_ERROR("FramePublisher::getTransform: \n%s", ex.what()); |
| 77 | + return; |
| 78 | + } |
| 79 | + |
| 80 | + double rot_frame_roll, rot_frame_pitch, rot_frame_yaw; |
| 81 | + frame_transform.getBasis().getRPY(rot_frame_roll, rot_frame_pitch, rot_frame_yaw); |
| 82 | + tf::Transform target_transform(frame_transform.getRotation()); |
| 83 | + |
| 84 | + // Use rotations according to settings |
| 85 | + double target_frame_roll = 0; |
| 86 | + double target_frame_pitch = 0; |
| 87 | + double target_frame_yaw = 0; |
| 88 | + if (rot_z_){ target_frame_yaw = rot_frame_yaw;} |
| 89 | + if (rot_x_){ target_frame_roll = rot_frame_roll;} |
| 90 | + if (rot_y_){ target_frame_pitch = rot_frame_pitch;} |
| 91 | + |
| 92 | + target_transform.getBasis().setRPY(target_frame_roll, target_frame_pitch, target_frame_yaw); |
| 93 | + |
| 94 | + // Broadcast new frame |
| 95 | + tf_broadcaster_.sendTransform(tf::StampedTransform(target_transform, frame_transform.stamp_, base_frame_, target_frame_)); |
55 | 96 | }
|
0 commit comments