Skip to content

Commit 434286f

Browse files
committed
Merge pull request CCNYRoboticsLab#52 from mintar/port_params
Port params from madgwick, change debug topic
2 parents 537d2f7 + 06e2a2a commit 434286f

File tree

2 files changed

+86
-47
lines changed

2 files changed

+86
-47
lines changed

imu_complementary_filter/include/imu_complementary_filter/complementary_filter_ros.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#define IMU_TOOLS_COMPLEMENTARY_FILTER_ROS_H
3434

3535
#include <sensor_msgs/MagneticField.h>
36+
#include <geometry_msgs/Vector3Stamped.h>
3637
#include <message_filters/subscriber.h>
3738
#include <message_filters/sync_policies/approximate_time.h>
3839
#include <message_filters/synchronizer.h>
@@ -74,15 +75,16 @@ class ComplementaryFilterROS
7475
boost::shared_ptr<MagSubscriber> mag_subscriber_;
7576

7677
ros::Publisher imu_publisher_;
77-
ros::Publisher roll_publisher_;
78-
ros::Publisher pitch_publisher_;
79-
ros::Publisher yaw_publisher_;
78+
ros::Publisher rpy_publisher_;
8079
ros::Publisher state_publisher_;
8180
tf::TransformBroadcaster tf_broadcaster_;
8281

8382
// Parameters:
8483
bool use_mag_;
84+
bool publish_tf_;
85+
bool reverse_tf_;
8586
double constant_dt_;
87+
bool publish_debug_topics_;
8688
std::string fixed_frame_;
8789

8890
// State variables:

imu_complementary_filter/src/complementary_filter_ros.cpp

Lines changed: 81 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -49,18 +49,27 @@ ComplementaryFilterROS::ComplementaryFilterROS(
4949
int queue_size = 5;
5050

5151
// Register publishers:
52-
imu_publisher_ = nh_.advertise<sensor_msgs::Imu>("imu/data", queue_size);
53-
roll_publisher_ = nh_.advertise<std_msgs::Float64>("imu/roll", queue_size);
54-
pitch_publisher_ = nh_.advertise<std_msgs::Float64>("imu/pitch", queue_size);
55-
yaw_publisher_ = nh_.advertise<std_msgs::Float64>("imu/yaw", queue_size);
56-
52+
imu_publisher_ = nh_.advertise<sensor_msgs::Imu>(ros::names::resolve("imu") + "/data", queue_size);
53+
54+
if (publish_debug_topics_)
55+
{
56+
rpy_publisher_ = nh_.advertise<geometry_msgs::Vector3Stamped>(
57+
ros::names::resolve("imu") + "/rpy/filtered", queue_size);
58+
59+
if (filter_.getDoBiasEstimation())
60+
{
61+
state_publisher_ = nh_.advertise<std_msgs::Bool>(
62+
ros::names::resolve("imu") + "/steady_state", queue_size);
63+
}
64+
}
65+
5766
// Register IMU raw data subscriber.
58-
imu_subscriber_.reset(new ImuSubscriber(nh_, "imu/data_raw", queue_size));
67+
imu_subscriber_.reset(new ImuSubscriber(nh_, ros::names::resolve("imu") + "/data_raw", queue_size));
5968

6069
// Register magnetic data subscriber.
6170
if (use_mag_)
6271
{
63-
mag_subscriber_.reset(new MagSubscriber(nh_, "imu/mag", queue_size));
72+
mag_subscriber_.reset(new MagSubscriber(nh_, ros::names::resolve("imu") + "/mag", queue_size));
6473

6574
sync_.reset(new Synchronizer(
6675
SyncPolicy(queue_size), *imu_subscriber_, *mag_subscriber_));
@@ -72,12 +81,6 @@ ComplementaryFilterROS::ComplementaryFilterROS(
7281
imu_subscriber_->registerCallback(
7382
&ComplementaryFilterROS::imuCallback, this);
7483
}
75-
76-
if (filter_.getDoBiasEstimation())
77-
{
78-
state_publisher_ = nh_.advertise<std_msgs::Bool>("imu/steady_state",
79-
queue_size);
80-
}
8184
}
8285

8386
ComplementaryFilterROS::~ComplementaryFilterROS()
@@ -97,6 +100,14 @@ void ComplementaryFilterROS::initializeParams()
97100
fixed_frame_ = "odom";
98101
if (!nh_private_.getParam ("use_mag", use_mag_))
99102
use_mag_ = false;
103+
if (!nh_private_.getParam ("publish_tf", publish_tf_))
104+
publish_tf_ = false;
105+
if (!nh_private_.getParam ("reverse_tf", reverse_tf_))
106+
reverse_tf_ = false;
107+
if (!nh_private_.getParam ("constant_dt", constant_dt_))
108+
constant_dt_ = 0.0;
109+
if (!nh_private_.getParam ("publish_debug_topics", publish_debug_topics_))
110+
publish_debug_topics_ = false;
100111
if (!nh_private_.getParam ("gain_acc", gain_acc))
101112
gain_acc = 0.01;
102113
if (!nh_private_.getParam ("gain_mag", gain_mag))
@@ -123,6 +134,15 @@ void ComplementaryFilterROS::initializeParams()
123134
if(!filter_.setBiasAlpha(bias_alpha))
124135
ROS_WARN("Invalid bias_alpha passed to ComplementaryFilter.");
125136
}
137+
138+
// check for illegal constant_dt values
139+
if (constant_dt_ < 0.0)
140+
{
141+
// if constant_dt_ is 0.0 (default), use IMU timestamp to determine dt
142+
// otherwise, it will be constant
143+
ROS_WARN("constant_dt parameter is %f, must be >= 0.0. Setting to 0.0", constant_dt_);
144+
constant_dt_ = 0.0;
145+
}
126146
}
127147

128148
void ComplementaryFilterROS::imuCallback(const ImuMsg::ConstPtr& imu_msg_raw)
@@ -139,8 +159,13 @@ void ComplementaryFilterROS::imuCallback(const ImuMsg::ConstPtr& imu_msg_raw)
139159
return;
140160
}
141161

142-
// Calculate dt.
143-
double dt = (time - time_prev_).toSec();
162+
// determine dt: either constant, or from IMU timestamp
163+
double dt;
164+
if (constant_dt_ > 0.0)
165+
dt = constant_dt_;
166+
else
167+
dt = (time - time_prev_).toSec();
168+
144169
time_prev_ = time;
145170

146171
// Update the filter.
@@ -215,38 +240,50 @@ void ComplementaryFilterROS::publish(
215240

216241
imu_publisher_.publish(imu_msg);
217242

218-
// Create and publish roll, pitch, yaw angles
219-
double roll, pitch, yaw;
220-
tf::Matrix3x3 M;
221-
M.setRotation(q);
222-
M.getRPY(roll, pitch, yaw);
223-
std_msgs::Float64 roll_msg;
224-
std_msgs::Float64 pitch_msg;
225-
std_msgs::Float64 yaw_msg;
226-
roll_msg.data = roll;
227-
pitch_msg.data = pitch;
228-
yaw_msg.data = yaw;
229-
roll_publisher_.publish(roll_msg);
230-
pitch_publisher_.publish(pitch_msg);
231-
yaw_publisher_.publish(yaw_msg);
232-
233-
// Publish whether we are in the steady state, when doing bias estimation
234-
if (filter_.getDoBiasEstimation())
243+
if (publish_debug_topics_)
235244
{
236-
std_msgs::Bool state_msg;
237-
state_msg.data = filter_.getSteadyState();
238-
state_publisher_.publish(state_msg);
245+
// Create and publish roll, pitch, yaw angles
246+
geometry_msgs::Vector3Stamped rpy;
247+
rpy.header = imu_msg_raw->header;
248+
249+
tf::Matrix3x3 M;
250+
M.setRotation(q);
251+
M.getRPY(rpy.vector.x, rpy.vector.y, rpy.vector.z);
252+
rpy_publisher_.publish(rpy);
253+
254+
// Publish whether we are in the steady state, when doing bias estimation
255+
if (filter_.getDoBiasEstimation())
256+
{
257+
std_msgs::Bool state_msg;
258+
state_msg.data = filter_.getSteadyState();
259+
state_publisher_.publish(state_msg);
260+
}
239261
}
240262

241-
// Create and publish the ROS tf.
242-
tf::Transform transform;
243-
transform.setOrigin(tf::Vector3(0.0, 0.0, 0.0));
244-
transform.setRotation(q);
245-
tf_broadcaster_.sendTransform(
246-
tf::StampedTransform(transform,
247-
imu_msg_raw->header.stamp,
248-
fixed_frame_,
249-
imu_msg_raw->header.frame_id));
263+
if (publish_tf_)
264+
{
265+
// Create and publish the ROS tf.
266+
tf::Transform transform;
267+
transform.setOrigin(tf::Vector3(0.0, 0.0, 0.0));
268+
transform.setRotation(q);
269+
270+
if (reverse_tf_)
271+
{
272+
tf_broadcaster_.sendTransform(
273+
tf::StampedTransform(transform.inverse(),
274+
imu_msg_raw->header.stamp,
275+
imu_msg_raw->header.frame_id,
276+
fixed_frame_));
277+
}
278+
else
279+
{
280+
tf_broadcaster_.sendTransform(
281+
tf::StampedTransform(transform,
282+
imu_msg_raw->header.stamp,
283+
fixed_frame_,
284+
imu_msg_raw->header.frame_id));
285+
}
286+
}
250287
}
251288

252289
} // namespace imu_tools

0 commit comments

Comments
 (0)