orientation_controller.cpp
Go to the documentation of this file.
1 //
2 // Created by bruce on 2021/5/19.
3 //
4 
7 #include <rm_common/ori_tool.h>
9 
11 {
13 {
14  std::string name;
15  if (!controller_nh.getParam("name", name) || !controller_nh.getParam("frame_source", frame_source_) ||
16  !controller_nh.getParam("frame_target", frame_target_))
17  {
18  ROS_ERROR("Some params doesn't given (namespace: %s)", controller_nh.getNamespace().c_str());
19  return false;
20  }
22  robot_state_ = robot_hw->get<rm_control::RobotStateInterface>()->getHandle("robot_state");
23 
24  tf_broadcaster_.init(root_nh);
25  imu_data_sub_ = root_nh.subscribe<sensor_msgs::Imu>("data", 1, &Controller::imuDataCallback, this);
26  source2target_msg_.header.frame_id = frame_source_;
27  source2target_msg_.child_frame_id = frame_target_;
28  source2target_msg_.transform.rotation.w = 1.0;
29  return true;
30 }
31 
32 void Controller::update(const ros::Time& time, const ros::Duration& period)
33 {
35  {
37  geometry_msgs::TransformStamped source2target;
38  source2target.header.stamp = time;
39  source2target.header.stamp.nsec += 1; // Avoid redundant timestamp
40  source2target_msg_.header.stamp = time;
41  source2target_msg_.header.stamp.nsec += 1;
43  getTransform(ros::Time(0), source2target, imu_sensor_.getOrientation()[0], imu_sensor_.getOrientation()[1],
44  imu_sensor_.getOrientation()[2], imu_sensor_.getOrientation()[3]) ?
45  source2target :
47  robot_state_.setTransform(source2target_msg_, "rm_orientation_controller");
48  if (!receive_imu_msg_)
50  }
51 }
52 
53 bool Controller::getTransform(const ros::Time& time, geometry_msgs::TransformStamped& source2target, const double x,
54  const double y, const double z, const double w)
55 {
56  source2target.header.frame_id = frame_source_;
57  source2target.child_frame_id = frame_target_;
58  source2target.transform.rotation.w = 1.0;
59  tf2::Transform source2odom, odom2fixed, fixed2target;
60  try
61  {
62  geometry_msgs::TransformStamped tf_msg;
63  tf_msg = robot_state_.lookupTransform(frame_source_, "odom", time);
64  tf2::fromMsg(tf_msg.transform, source2odom);
65  tf_msg = robot_state_.lookupTransform("odom", imu_sensor_.getFrameId(), time);
66  tf2::fromMsg(tf_msg.transform, odom2fixed);
67  tf_msg = robot_state_.lookupTransform(imu_sensor_.getFrameId(), frame_target_, time);
68  tf2::fromMsg(tf_msg.transform, fixed2target);
69  }
70  catch (tf2::TransformException& ex)
71  {
72  ROS_WARN("%s", ex.what());
73  return false;
74  }
75  tf2::Quaternion odom2fixed_quat;
76  odom2fixed_quat.setValue(x, y, z, w);
77  odom2fixed.setRotation(odom2fixed_quat);
78  source2target.transform = tf2::toMsg(source2odom * odom2fixed * fixed2target);
79  return true;
80 }
81 
82 void Controller::imuDataCallback(const sensor_msgs::Imu::ConstPtr& msg)
83 {
84  if (!receive_imu_msg_)
85  receive_imu_msg_ = true;
86  geometry_msgs::TransformStamped source2target;
87  source2target.header.stamp = msg->header.stamp;
88  getTransform(ros::Time(0), source2target, msg->orientation.x, msg->orientation.y, msg->orientation.z,
89  msg->orientation.w);
90  tf_broadcaster_.sendTransform(source2target);
91 }
92 
93 } // namespace rm_orientation_controller
94 
rm_control::RobotStateHandle::lookupTransform
geometry_msgs::TransformStamped lookupTransform(const std::string &target_frame, const std::string &source_frame, const ros::Time &time)
rm_orientation_controller::Controller::imu_sensor_
rm_control::RmImuSensorHandle imu_sensor_
Definition: orientation_controller.h:29
orientation_controller.h
rm_orientation_controller::Controller::last_imu_update_time_
ros::Time last_imu_update_time_
Definition: orientation_controller.h:32
tf2::fromMsg
void fromMsg(const A &, B &b)
hardware_interface::InterfaceManager::get
T * get()
rm_orientation_controller::Controller::getTransform
bool getTransform(const ros::Time &time, geometry_msgs::TransformStamped &source2target, const double x, const double y, const double z, const double w)
Definition: orientation_controller.cpp:53
ros::NodeHandle::getParam
bool getParam(const std::string &key, bool &b) const
rm_orientation_controller::Controller::imu_data_sub_
ros::Subscriber imu_data_sub_
Definition: orientation_controller.h:40
rm_orientation_controller::Controller::update
void update(const ros::Time &time, const ros::Duration &period) override
Definition: orientation_controller.cpp:32
tf2::Transform::setRotation
TF2SIMD_FORCE_INLINE void setRotation(const Quaternion &q)
rm_common::TfRtBroadcaster::sendTransform
virtual void sendTransform(const geometry_msgs::TransformStamped &transform)
rm_control::RobotStateHandle::setTransform
bool setTransform(const geometry_msgs::TransformStamped &transform, const std::string &authority, bool is_static=false) const
rm_orientation_controller::Controller::imuDataCallback
void imuDataCallback(const sensor_msgs::Imu::ConstPtr &msg)
Definition: orientation_controller.cpp:82
rm_control::RobotStateInterface
getHandle
ROSCONSOLE_CONSOLE_IMPL_DECL void * getHandle(const std::string &name)
rm_common::TfRtBroadcaster::init
virtual void init(ros::NodeHandle &root_nh)
rm_orientation_controller::Controller::init
bool init(hardware_interface::RobotHW *robot_hw, ros::NodeHandle &root_nh, ros::NodeHandle &controller_nh) override
Definition: orientation_controller.cpp:12
controller_interface::ControllerBase
PLUGINLIB_EXPORT_CLASS
#define PLUGINLIB_EXPORT_CLASS(class_type, base_class_type)
hardware_interface::RobotHW
rm_orientation_controller::Controller::robot_state_
rm_control::RobotStateHandle robot_state_
Definition: orientation_controller.h:30
rm_orientation_controller
Definition: orientation_controller.h:14
tf2::Transform
rm_control::RmImuSensorInterface
ROS_WARN
#define ROS_WARN(...)
ros::NodeHandle::subscribe
Subscriber subscribe(const std::string &topic, uint32_t queue_size, const boost::function< void(C)> &callback, const VoidConstPtr &tracked_object=VoidConstPtr(), const TransportHints &transport_hints=TransportHints())
TimeBase< Time, Duration >::nsec
uint32_t nsec
rm_orientation_controller::Controller::receive_imu_msg_
bool receive_imu_msg_
Definition: orientation_controller.h:41
ros::Time
rm_orientation_controller::Controller::frame_target_
std::string frame_target_
Definition: orientation_controller.h:38
ROS_ERROR
#define ROS_ERROR(...)
class_list_macros.hpp
ros_utilities.h
tf2::Quaternion
tf2::toMsg
B toMsg(const A &a)
ros::NodeHandle::getNamespace
const std::string & getNamespace() const
rm_control::RmImuSensorHandle::getTimeStamp
ros::Time getTimeStamp()
tf2::TransformException
rm_orientation_controller::Controller::source2target_msg_
geometry_msgs::TransformStamped source2target_msg_
Definition: orientation_controller.h:35
ros::Duration
rm_orientation_controller::Controller::tf_broadcaster_
rm_common::TfRtBroadcaster tf_broadcaster_
Definition: orientation_controller.h:34
rm_orientation_controller::Controller
Definition: orientation_controller.h:16
rm_orientation_controller::Controller::frame_source_
std::string frame_source_
Definition: orientation_controller.h:37
ros::NodeHandle
ori_tool.h


rm_orientation_controller
Author(s): Qiayuan Liao
autogenerated on Sun Apr 13 2025 02:56:32