29 #ifndef HECTOR_QUADROTOR_INTERFACE_HELPERS_H 30 #define HECTOR_QUADROTOR_INTERFACE_HELPERS_H 32 #include <geometry_msgs/PoseStamped.h> 33 #include <sensor_msgs/Imu.h> 34 #include <nav_msgs/Odometry.h> 35 #include <geometry_msgs/Pose.h> 36 #include <geometry_msgs/Twist.h> 37 #include <geometry_msgs/Accel.h> 38 #include <geometry_msgs/TransformStamped.h> 40 #include <hector_uav_msgs/AttitudeCommand.h> 41 #include <hector_uav_msgs/YawrateCommand.h> 42 #include <hector_uav_msgs/ThrustCommand.h> 43 #include <std_msgs/Header.h> 46 #include <boost/thread/mutex.hpp> 47 #include <boost/circular_buffer.hpp> 86 geometry_msgs::Accel &acceleration, std_msgs::Header &header)
87 : pose_(pose), twist_(twist), acceleration_(acceleration), header_(header)
89 odom_sub_ = nh.
subscribe<nav_msgs::Odometry>(topic, 1,
109 if (!header_.stamp.isZero() && !odom->header.stamp.isZero())
111 const double acceleration_time_constant = 0.1;
112 double dt((odom->header.stamp - header_.stamp).toSec());
115 acceleration_.linear.x =
116 ((odom->twist.twist.linear.x - twist_.linear.x) + acceleration_time_constant * acceleration_.linear.x) /
117 (dt + acceleration_time_constant);
118 acceleration_.linear.y =
119 ((odom->twist.twist.linear.y - twist_.linear.y) + acceleration_time_constant * acceleration_.linear.y) /
120 (dt + acceleration_time_constant);
121 acceleration_.linear.z =
122 ((odom->twist.twist.linear.z - twist_.linear.z) + acceleration_time_constant * acceleration_.linear.z) /
123 (dt + acceleration_time_constant);
124 acceleration_.angular.x = ((odom->twist.twist.angular.x - twist_.angular.x) +
125 acceleration_time_constant * acceleration_.angular.x) /
126 (dt + acceleration_time_constant);
127 acceleration_.angular.y = ((odom->twist.twist.angular.y - twist_.angular.y) +
128 acceleration_time_constant * acceleration_.angular.y) /
129 (dt + acceleration_time_constant);
130 acceleration_.angular.z = ((odom->twist.twist.angular.z - twist_.angular.z) +
131 acceleration_time_constant * acceleration_.angular.z) /
132 (dt + acceleration_time_constant);
136 header_ = odom->header;
137 pose_ = odom->pose.pose;
138 twist_ = odom->twist.twist;
148 pose_sub_ = nh.
subscribe<geometry_msgs::PoseStamped>(topic, 1,
155 pose_sub_.shutdown();
158 geometry_msgs::PoseStampedConstPtr
get()
const {
return pose_; }
163 geometry_msgs::PoseStampedConstPtr
pose_;
176 geometry_msgs::Accel &accel)
182 double dt = (time - last_time_).toSec();
183 double roll, pitch, yaw, last_roll, last_pitch, last_yaw;
192 twist.linear.x = (pose.position.x - last_pose_->position.x) / dt;
193 twist.linear.y = (pose.position.y - last_pose_->position.y) / dt;
194 twist.linear.z = (pose.position.z - last_pose_->position.z) / dt;
195 twist.angular.x = differenceWithWraparound(roll, last_roll) / dt;
196 twist.angular.y = differenceWithWraparound(pitch, last_pitch) / dt;
197 twist.angular.z = differenceWithWraparound(yaw, last_yaw) / dt;
201 accel.linear.x = (twist.linear.x - last_twist_->linear.x) / dt;
202 accel.linear.y = (twist.linear.y - last_twist_->linear.y) / dt;
203 accel.linear.z = (twist.linear.z - last_twist_->linear.z) / dt;
205 accel.angular.x = (twist.angular.x - last_twist_->angular.x) / dt;
206 accel.angular.y = (twist.angular.y - last_twist_->angular.y) / dt;
207 accel.angular.z = (twist.angular.z - last_twist_->angular.z) / dt;
208 *last_twist_ = twist;
212 last_twist_ = boost::make_shared<geometry_msgs::Twist>(twist);
219 last_pose_ = boost::make_shared<geometry_msgs::Pose>(pose);
233 double diff = angle - last_angle;
236 return diff - 2 * M_PI;
238 else if (diff < -M_PI)
240 return diff + 2 * M_PI;
255 geometry_msgs::Twist &twist, geometry_msgs::Accel &accel, std_msgs::Header &header)
256 : pose_(pose), twist_(twist), accel_(accel), header_(header)
259 tf_sub_ = nh.
subscribe<geometry_msgs::TransformStamped>(topic, 1,
284 void tfCb(
const geometry_msgs::TransformStampedConstPtr &transform)
286 header_ = transform->header;
287 pose_.position.x = transform->transform.translation.x;
288 pose_.position.y = transform->transform.translation.y;
289 pose_.position.z = transform->transform.translation.z;
290 pose_.orientation = transform->transform.rotation;
332 hector_uav_msgs::AttitudeCommand &attitude_command,
333 hector_uav_msgs::YawrateCommand &yawrate_command,
334 hector_uav_msgs::ThrustCommand &thrust_command)
335 : command_mutex_(command_mutex), attitude_command_(attitude_command), yawrate_command_(yawrate_command),
336 thrust_command_(thrust_command)
338 attitude_subscriber_ = nh.
subscribe<hector_uav_msgs::AttitudeCommand>(
"attitude", 1, boost::bind(
340 yawrate_subscriber_ = nh.
subscribe<hector_uav_msgs::YawrateCommand>(
"yawrate", 1, boost::bind(
342 thrust_subscriber_ = nh.
subscribe<hector_uav_msgs::ThrustCommand>(
"thrust", 1, boost::bind(
348 attitude_subscriber_.shutdown();
349 yawrate_subscriber_.shutdown();
350 thrust_subscriber_.shutdown();
362 boost::mutex::scoped_lock lock(command_mutex_);
363 attitude_command_ = *command;
364 if (attitude_command_.header.stamp.isZero())
370 boost::mutex::scoped_lock lock(command_mutex_);
371 yawrate_command_ = *command;
372 if (yawrate_command_.header.stamp.isZero())
378 boost::mutex::scoped_lock lock(command_mutex_);
379 thrust_command_ = *command;
380 if (thrust_command_.header.stamp.isZero())
388 bool poseWithinTolerance(
const geometry_msgs::Pose &pose_current,
const geometry_msgs::Pose &pose_target,
389 const double dist_tolerance,
const double yaw_tolerance);
393 #endif // HECTOR_QUADROTOR_INTERFACE_HELPERS_H
void getRPY(tf2Scalar &roll, tf2Scalar &pitch, tf2Scalar &yaw, unsigned int solution_number=1) const
hector_uav_msgs::AttitudeCommand & attitude_command_
void thrustCommandCb(const hector_uav_msgs::ThrustCommandConstPtr &command)
void attitudeCommandCb(const hector_uav_msgs::AttitudeCommandConstPtr &command)
void yawrateCommandCb(const hector_uav_msgs::YawrateCommandConstPtr &command)
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
PoseDifferentiatorHelper diff_
boost::mutex & command_mutex_
geometry_msgs::Pose & pose_
geometry_msgs::Accel & accel_
geometry_msgs::Accel & acceleration_
hector_uav_msgs::YawrateCommand & yawrate_command_
bool poseWithinTolerance(const geometry_msgs::Pose &pose_current, const geometry_msgs::Pose &pose_target, const double dist_tolerance, const double yaw_tolerance)
IMETHOD Vector diff(const Vector &p_w_a, const Vector &p_w_b, double dt=1)
hector_uav_msgs::ThrustCommand & thrust_command_
~AttitudeSubscriberHelper()
AttitudeSubscriberHelper(ros::NodeHandle nh, boost::mutex &command_mutex, hector_uav_msgs::AttitudeCommand &attitude_command, hector_uav_msgs::YawrateCommand &yawrate_command, hector_uav_msgs::ThrustCommand &thrust_command)
double differenceWithWraparound(double angle, double last_angle)
PoseSubscriberHelper(ros::NodeHandle nh, std::string topic)
OdomSubscriberHelper(ros::NodeHandle nh, std::string topic, geometry_msgs::Pose &pose, geometry_msgs::Twist &twist, geometry_msgs::Accel &acceleration, std_msgs::Header &header)
std_msgs::Header & header_
void imuCallback(const sensor_msgs::ImuConstPtr &imu)
StateSubscriberHelper(ros::NodeHandle nh, std::string topic, geometry_msgs::Pose &pose, geometry_msgs::Twist &twist, geometry_msgs::Accel &accel, std_msgs::Header &header)
void fromMsg(const A &, B &b)
geometry_msgs::TwistPtr last_twist_
geometry_msgs::PoseStampedConstPtr pose_
geometry_msgs::Twist & twist_
ImuSubscriberHelper(ros::NodeHandle nh, std::string topic, sensor_msgs::Imu &imu)
void tfCb(const geometry_msgs::TransformStampedConstPtr &transform)
geometry_msgs::Pose & pose_
void updateAndEstimate(const ros::Time &time, const geometry_msgs::Pose &pose, geometry_msgs::Twist &twist, geometry_msgs::Accel &accel)
ros::Subscriber pose_sub_
geometry_msgs::Twist & twist_
ros::Subscriber yawrate_subscriber_
bool getMassAndInertia(const ros::NodeHandle &nh, double &mass, double inertia[3])
ros::Subscriber odom_sub_
void poseCallback(const geometry_msgs::PoseStampedConstPtr &pose)
geometry_msgs::PosePtr last_pose_
void odomCallback(const nav_msgs::OdometryConstPtr &odom)