rm_controllers
Loading...
Searching...
No Matches
robot_state_controller.h
Go to the documentation of this file.
1/*******************************************************************************
2 * BSD 3-Clause License
3 *
4 * Copyright (c) 2021, Qiayuan Liao
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 *
10 * * Redistributions of source code must retain the above copyright notice, this
11 * list of conditions and the following disclaimer.
12 *
13 * * Redistributions in binary form must reproduce the above copyright notice,
14 * this list of conditions and the following disclaimer in the documentation
15 * and/or other materials provided with the distribution.
16 *
17 * * Neither the name of the copyright holder nor the names of its
18 * contributors may be used to endorse or promote products derived from
19 * this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE
25 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 *******************************************************************************/
33
34//
35// Created by qiayuan on 1/3/21.
36//
37
38#pragma once
39
40#include <rm_common/hardware_interface/robot_state_interface.h>
41#include <rm_common/tf_rt_broadcaster.h>
42
43#include <controller_interface/multi_interface_controller.h>
44#include <hardware_interface/joint_state_interface.h>
45#include <pluginlib/class_list_macros.hpp>
46#include <tf2_ros/static_transform_broadcaster.h>
47#include <tf2_ros/transform_broadcaster.h>
48#include <tf2_ros/buffer.h>
49#include <urdf/model.h>
50#include <kdl/tree.hpp>
51#include <realtime_tools/realtime_buffer.h>
52
54{
56{
57public:
58 SegmentPair(const KDL::Segment& p_segment, std::string p_root, std::string p_tip)
59 : segment(p_segment), root(std::move(p_root)), tip(std::move(p_tip))
60 {
61 }
62
63 KDL::Segment segment{};
64 std::string root, tip;
65};
66
68 : public controller_interface::MultiInterfaceController<hardware_interface::JointStateInterface,
69 rm_control::RobotStateInterface>
70{
71public:
73 bool init(hardware_interface::RobotHW* robot_hw, ros::NodeHandle& root_nh, ros::NodeHandle& controller_nh) override;
74 void update(const ros::Time& time, const ros::Duration& /*period*/) override;
75
76private:
77 void addChildren(KDL::SegmentMap::const_iterator segment);
78 void tfSubCallback(const tf2_msgs::TFMessageConstPtr& msg);
79 void staticSubCallback(const tf2_msgs::TFMessageConstPtr& msg);
80
81 urdf::Model model_{};
82 std::map<std::string, urdf::JointMimicSharedPtr>* mimic_{};
83 unsigned int num_hw_joints_{};
84 bool use_tf_static_{};
85 bool ignore_timestamp_{};
86 double publish_rate_{};
87 ros::Time last_update_;
88 ros::Time last_publish_time_;
89
90 std::map<std::string, hardware_interface::JointStateHandle> jnt_states_;
91 std::map<std::string, SegmentPair> segments_, segments_fixed_;
92
93 tf2_ros::Buffer* tf_buffer_{};
94 rm_common::TfRtBroadcaster tf_broadcaster_;
95 rm_common::StaticTfRtBroadcaster static_tf_broadcaster_;
96 // Do not use tf2_ros::TransformListener because it will lead to setTransform calling twice when publishing the transform
97 ros::Subscriber tf_sub_;
98 ros::Subscriber tf_static_sub_;
99 realtime_tools::RealtimeBuffer<tf2_msgs::TFMessage> tf_msg_;
100 realtime_tools::RealtimeBuffer<tf2_msgs::TFMessage> tf_static_msg_;
101};
102
103} // namespace robot_state_controller
Definition robot_state_controller.h:70
bool init(hardware_interface::RobotHW *robot_hw, ros::NodeHandle &root_nh, ros::NodeHandle &controller_nh) override
Definition robot_state_controller.cpp:44
void update(const ros::Time &time, const ros::Duration &) override
Definition robot_state_controller.cpp:103
Definition robot_state_controller.h:56
KDL::Segment segment
Definition robot_state_controller.h:63
SegmentPair(const KDL::Segment &p_segment, std::string p_root, std::string p_tip)
Definition robot_state_controller.h:58
std::string root
Definition robot_state_controller.h:64
std::string tip
Definition robot_state_controller.h:64
Definition robot_state_controller.h:54