Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion sensing/autoware_gnss_poser/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ This node subscribes to NavSatFix to publish the pose of **base_link**. The data

The antenna is rigidly mounted on the vehicle, so this transformation is expected to be static; an antenna that moves relative to `base_link` is out of scope. Publishing it on `/tf_static` is recommended, but a setup that publishes the same fixed relation on `/tf` is supported as well: the node looks up the latest transform rather than the transform at the fix's `header.stamp`, which `/tf` cannot always provide.

If the transformation from `base_link` to the antenna cannot be obtained, it outputs the pose of the antenna position without performing coordinate transformation.
If the transformation from `base_link` to the antenna cannot be obtained, the fix is skipped: `gnss_fixed` still reports the receiver's status, but no pose is published. Depending on the startup order, the transformation may arrive after the first fixes, so the fixes are skipped silently for the first 5 seconds, counted by the `header.stamp` of the fixes from the first fix (whatever its status). After that, a fix that cannot get the transformation makes the node log an error once, naming both frames, so that a `header.frame_id` that never gets a transformation is visible instead of silently yielding no pose. When TF has the transformations but the lookup still fails, for example because `header.frame_id` is not a frame name TF accepts (empty, or starting with `/`), waiting does not help, so it is logged as an error at once. Each of the two problems is logged once, and again only after the transformation has been obtained in between. A rosbag replayed in a loop gets the 5 seconds only on its first pass.

## Inputs / Outputs

Expand Down
1 change: 1 addition & 0 deletions sensing/autoware_gnss_poser/package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
<depend>geographic_msgs</depend>
<depend>geographiclib</depend>
<depend>geometry_msgs</depend>
<depend>libexpected-dev</depend>
<depend>rclcpp</depend>
<depend>rclcpp_components</depend>
<depend>sensor_msgs</depend>
Expand Down
78 changes: 65 additions & 13 deletions sensing/autoware_gnss_poser/src/gnss_poser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ bool is_fixed(const sensor_msgs::msg::NavSatStatus & nav_sat_status_msg)
return nav_sat_status_msg.status >= sensor_msgs::msg::NavSatStatus::STATUS_FIX;
}

/// The stamp as the time since the epoch of the clock that stamped it.
std::chrono::nanoseconds to_stamp_time(const builtin_interfaces::msg::Time & stamp)
{
return std::chrono::seconds(stamp.sec) + std::chrono::nanoseconds(stamp.nanosec);
}

/// True when the receiver reports a covariance type other than UNKNOWN.
bool can_get_covariance(const sensor_msgs::msg::NavSatFix & nav_sat_fix_msg)
{
Expand Down Expand Up @@ -228,20 +234,22 @@ void GnssPoser::set_ins_orientation(

GnssPoser::Result GnssPoser::input_fix(const sensor_msgs::msg::NavSatFix & fix)
{
if (!first_fix_stamp_.has_value()) {
first_fix_stamp_ = to_stamp_time(fix.header.stamp);
}

// Return immediately if map_projector_info has not been received yet.
if (!received_map_projector_info_) {
return {Outcome::NoProjectorInfo, std::nullopt, std::nullopt, std::nullopt, std::nullopt};
return {Outcome::NoProjectorInfo};
}

if (projector_info_.projector_type == autoware_map_msgs::msg::MapProjectorInfo::LOCAL) {
return {Outcome::LocalProjector, std::nullopt, std::nullopt, std::nullopt, std::nullopt};
return {Outcome::LocalProjector};
}

// check fixed topic
if (!is_fixed(fix.status)) {
return {
Outcome::NotFixed, make_gnss_fixed(fix.header.stamp, false), std::nullopt, std::nullopt,
std::nullopt};
return {Outcome::NotFixed, make_gnss_fixed(fix.header.stamp, false)};
}

// get position
Expand All @@ -256,9 +264,7 @@ GnssPoser::Result GnssPoser::input_fix(const sensor_msgs::msg::NavSatFix & fix)
// fill position buffer
position_buffer_.push_front(position);
if (!position_buffer_.full()) {
return {
Outcome::Buffering, make_gnss_fixed(fix.header.stamp, true), std::nullopt, std::nullopt,
std::nullopt};
return {Outcome::Buffering, make_gnss_fixed(fix.header.stamp, true)};
}
// publish average pose or median pose of position buffer
gnss_antenna_pose.position = (params_.gnss_pose_pub_method == GnssPosePubMethod::Average)
Expand All @@ -281,11 +287,23 @@ GnssPoser::Result GnssPoser::input_fix(const sensor_msgs::msg::NavSatFix & fix)

gnss_antenna_pose.orientation = orientation;

// get TF from gnss_antenna to base_link. If it cannot be obtained, the antenna pose is published
// as the base_link pose, i.e. the identity transform is used (a default-constructed Transform has
// zero translation and rotation w = 1).
const geometry_msgs::msg::Transform antenna_to_base_link =
lookup_antenna_to_base_link_(fix.header.frame_id).value_or(geometry_msgs::msg::Transform{});
// get TF from gnss_antenna to base_link.
const auto transform = lookup_antenna_to_base_link_(fix.header.frame_id);
// Drop this fix if its antenna transform is not available: the fix cannot be expressed as a
// base_link pose without it. The transform is static, so this does not resolve by asking for
// another time; it resolves once the transform is published.
if (!transform.has_value()) {
const TransformLookupError error = transform.error();
Result result{Outcome::TransformLookupFailed, make_gnss_fixed(fix.header.stamp, true)};
if (should_report_transform_error(error, fix.header.stamp)) {
mark_transform_error_reported(error);
result.antenna_transform_error = error;
}
return result;
}
transform_unavailable_reported_ = false;
transform_lookup_failed_reported_ = false;
const geometry_msgs::msg::Transform antenna_to_base_link = transform.value();

std::array<double, 3> rotation_variances{};
if (params_.use_gnss_ins_orientation) {
Expand Down Expand Up @@ -316,4 +334,38 @@ GnssPoser::Result GnssPoser::input_fix(const sensor_msgs::msg::NavSatFix & fix)
make_transform_stamped(pose, params_.gnss_base_frame)};
}

bool GnssPoser::should_report_transform_error(
const TransformLookupError error, const builtin_interfaces::msg::Time & stamp) const
{
bool expected = false;
bool reported = false;
switch (error) {
case TransformLookupError::Unavailable:
// Expected within the grace period, as /tf_static may arrive after the first fixes.
expected = first_fix_stamp_.has_value() &&
to_stamp_time(stamp) - first_fix_stamp_.value() <= antenna_transform_grace_period;
reported = transform_unavailable_reported_;
break;

case TransformLookupError::Failed:
// Never expected: it does not resolve by waiting.
reported = transform_lookup_failed_reported_;
break;
}
return !expected && !reported;
}

void GnssPoser::mark_transform_error_reported(const TransformLookupError error)
{
switch (error) {
case TransformLookupError::Unavailable:
transform_unavailable_reported_ = true;
break;

case TransformLookupError::Failed:
transform_lookup_failed_reported_ = true;
break;
}
}

} // namespace autoware::gnss_poser
58 changes: 50 additions & 8 deletions sensing/autoware_gnss_poser/src/gnss_poser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
#ifndef GNSS_POSER_HPP_
#define GNSS_POSER_HPP_

#include <builtin_interfaces/msg/time.hpp>
#include <tl/expected.hpp>

#include <autoware_internal_debug_msgs/msg/bool_stamped.hpp>
#include <autoware_map_msgs/msg/map_projector_info.hpp>
#include <autoware_sensing_msgs/msg/gnss_ins_orientation.hpp>
Expand All @@ -27,6 +30,7 @@
#include <boost/circular_buffer.hpp>

#include <array>
#include <chrono>
#include <functional>
#include <optional>
#include <string>
Expand Down Expand Up @@ -61,6 +65,11 @@ constexpr std::array<double, 3> default_unknown_position_variances = {10.0, 10.0
constexpr std::array<double, 3> default_motion_rotation_variances = {0.1, 0.1, 1.0}; // [rad^2]
constexpr double default_ins_placeholder_rmse = 1.0; // [rad]

// How long after the first fix the antenna transform may be unavailable without being an issue:
// depending on the startup order, /tf_static may arrive after the first fixes. Measured by the fix
// header stamps, so that it follows the data, e.g. a rosbag replayed at another rate.
constexpr std::chrono::seconds antenna_transform_grace_period{5};

/// \brief The covariance values above, as GnssPoser takes them.
///
/// Not node parameters: GnssPoserNode constructs GnssPoser without them, so the values are the
Expand All @@ -85,16 +94,26 @@ struct GnssPoserCovarianceDefaults
class GnssPoser
{
public:
/// \brief Why a TransformLookup could not provide the transform.
enum class TransformLookupError {
/// TF is not ready for the lookup yet: it has not received the frames, or not the transforms
/// that connect them. Expected right after startup, until the transform is published.
Unavailable,
/// TF was ready for the lookup, but the lookup failed.
Failed,
};

/// \brief Resolves the transform from the antenna frame named in the fix header to base_link,
/// or std::nullopt when it cannot be resolved.
/// or the reason it cannot be resolved.
///
/// The antenna is rigidly mounted on the vehicle, so this transform does not change over time
/// and no stamp is passed; a moving antenna is out of scope. input_fix() calls the lookup only
/// for a fix that passed the gates and the buffering, right before the pose is composed.
/// Obtaining the transform (TF lookup, logging) is the caller's business; what to do when there
/// is none is decided here.
using TransformLookup =
std::function<std::optional<geometry_msgs::msg::Transform>(const std::string & antenna_frame)>;
std::function<tl::expected<geometry_msgs::msg::Transform, TransformLookupError>(
const std::string & antenna_frame)>;

/// \param lookup_antenna_to_base_link see TransformLookup; kept for the lifetime of the object.
/// \param covariance_defaults see GnssPoserCovarianceDefaults; the node leaves it at its
Expand All @@ -110,7 +129,9 @@ class GnssPoser
LocalProjector, ///< the map uses a local projector, so a GNSS fix cannot be converted
NotFixed, ///< the receiver reports no fix; its status is known but no pose is computed
Buffering, ///< the position buffer is not full yet (average and median methods)
Published, ///< a pose was computed, see Result::gnss_pose
/// TransformLookup gave a TransformLookupError, of either kind.
TransformLookupFailed,
Published, ///< a pose was computed, see Result::gnss_pose
};

/// \brief What the fix produced, as the messages to publish.
Expand All @@ -124,13 +145,17 @@ class GnssPoser
Outcome outcome;
/// The receiver's fix status. Empty for a fix that was rejected before the fixed check,
/// which says nothing about the receiver.
std::optional<autoware_internal_debug_msgs::msg::BoolStamped> gnss_fixed;
std::optional<autoware_internal_debug_msgs::msg::BoolStamped> gnss_fixed = std::nullopt;
/// The base_link pose in the map frame.
std::optional<geometry_msgs::msg::PoseStamped> gnss_pose;
std::optional<geometry_msgs::msg::PoseStamped> gnss_pose = std::nullopt;
/// The same pose with its 6x6 covariance (only the diagonal is filled).
std::optional<geometry_msgs::msg::PoseWithCovarianceStamped> gnss_pose_cov;
std::optional<geometry_msgs::msg::PoseWithCovarianceStamped> gnss_pose_cov = std::nullopt;
/// The same pose as a transform, map_frame -> gnss_base_frame, ready to broadcast.
std::optional<geometry_msgs::msg::TransformStamped> transform;
std::optional<geometry_msgs::msg::TransformStamped> transform = std::nullopt;
/// Why the antenna transform is missing, at the fix where that is to be logged: the first
/// Unavailable after the grace period from the first fix, or the first Failed. Each is set
/// once, and again only after a fix has got the transform.
std::optional<TransformLookupError> antenna_transform_error = std::nullopt;
};

void set_projector_info(const autoware_map_msgs::msg::MapProjectorInfo & projector_info);
Expand All @@ -140,10 +165,20 @@ class GnssPoser
void set_ins_orientation(const autoware_sensing_msgs::msg::GnssInsOrientation & orientation);

/// \brief Process one fix: gate, project, buffer, orient, compose with the antenna transform and
/// attach the covariance.
/// attach the covariance. A fix whose antenna transform cannot be resolved is dropped; it has
/// still been observed, so the position buffer and the motion orientation take it into account.
Result input_fix(const sensor_msgs::msg::NavSatFix & fix);

private:
// Whether a fix stamped `stamp` whose antenna transform lookup failed with `error` reports it as
// Result::antenna_transform_error: not Unavailable within the grace period from the first fix,
// and not an error of a kind already reported since the transform was last obtained.
bool should_report_transform_error(
TransformLookupError error, const builtin_interfaces::msg::Time & stamp) const;
// Record that `error` has been reported, so that its kind is not reported again until the
// transform is obtained.
void mark_transform_error_reported(TransformLookupError error);

GnssPoserParams params_;
TransformLookup lookup_antenna_to_base_link_;
GnssPoserCovarianceDefaults covariance_defaults_;
Expand All @@ -154,6 +189,13 @@ class GnssPoser
geometry_msgs::msg::Point prev_position_;
bool has_prev_position_ = false;
autoware_sensing_msgs::msg::GnssInsOrientation ins_orientation_;
// The header stamp of the first fix, whatever its status, from which the grace period runs. Set
// once, so a rosbag replayed in a loop gets the grace period only on its first pass.
std::optional<std::chrono::nanoseconds> first_fix_stamp_;
// Whether Result::antenna_transform_error has been set to Unavailable, and to Failed, since a
// fix last got the transform: each kind is reported once.
bool transform_unavailable_reported_ = false;
bool transform_lookup_failed_reported_ = false;
};

} // namespace autoware::gnss_poser
Expand Down
48 changes: 39 additions & 9 deletions sensing/autoware_gnss_poser/src/gnss_poser_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@

#include "gnss_poser_node.hpp"

#include <tf2/exceptions.hpp>
#include <tf2/time.hpp>

#include <autoware_sensing_msgs/msg/gnss_ins_orientation_stamped.hpp>

#include <memory>
#include <optional>
#include <stdexcept>
#include <string>
#include <utility>
Expand Down Expand Up @@ -127,9 +127,16 @@ void GnssPoserNode::callback_nav_sat_fix(
"Buffering Position. Output Skipped.");
break;

// A missing antenna transform is logged below, when the result says it is to be reported;
// every fix without it would be too many.
case GnssPoser::Outcome::TransformLookupFailed:
case GnssPoser::Outcome::Published:
break;
}
if (result.antenna_transform_error.has_value()) {
log_antenna_transform_error(
nav_sat_fix_msg_ptr->header.frame_id, result.antenna_transform_error.value());
}

publish_data(result);
}
Expand All @@ -152,14 +159,35 @@ void GnssPoserNode::publish_data(const GnssPoser::Result & result)
}
}

void GnssPoserNode::log_antenna_transform_error(
const std::string & antenna_frame, const GnssPoser::TransformLookupError error)
{
switch (error) {
case GnssPoser::TransformLookupError::Unavailable:
RCLCPP_ERROR_STREAM(
this->get_logger(), "No transform from "
<< antenna_frame << " to " << base_frame_
<< " is available; please publish it on TF. Fixes are skipped.");
break;

case GnssPoser::TransformLookupError::Failed:
RCLCPP_ERROR_STREAM(
this->get_logger(), "Failed to look up the transform from \"" << antenna_frame << "\" to \""
<< base_frame_
<< "\". Fixes are skipped.");
break;
}
}

void GnssPoserNode::callback_gnss_ins_orientation_stamped(
const AUTOWARE_MESSAGE_CONST_SHARED_PTR(autoware_sensing_msgs::msg::GnssInsOrientationStamped) &
msg)
{
gnss_poser_.set_ins_orientation(msg->orientation);
}

std::optional<geometry_msgs::msg::Transform> GnssPoserNode::get_static_transform(
tl::expected<geometry_msgs::msg::Transform, GnssPoser::TransformLookupError>
GnssPoserNode::get_static_transform(
const std::string & target_frame, const std::string & source_frame)
{
if (target_frame == source_frame) {
Expand All @@ -173,13 +201,15 @@ std::optional<geometry_msgs::msg::Transform> GnssPoserNode::get_static_transform
// than /tf_static and the fix is newer than the last /tf message, which tf2 refuses to
// extrapolate.
return tf2_buffer_.lookupTransform(target_frame, source_frame, tf2::TimePointZero).transform;
} catch (tf2::TransformException & ex) {
RCLCPP_WARN_STREAM_THROTTLE(
this->get_logger(), *this->get_clock(), std::chrono::milliseconds(1000).count(), ex.what());
RCLCPP_WARN_STREAM_THROTTLE(
this->get_logger(), *this->get_clock(), std::chrono::milliseconds(1000).count(),
"Please publish TF " << target_frame.c_str() << " to " << source_frame.c_str());
return std::nullopt;
} catch (const tf2::LookupException &) {
// A frame has not been received.
return tl::unexpected(GnssPoser::TransformLookupError::Unavailable);
} catch (const tf2::ConnectivityException &) {
// The transforms that connect the frames have not been received.
return tl::unexpected(GnssPoser::TransformLookupError::Unavailable);
} catch (const tf2::TransformException &) {
// e.g. InvalidArgumentException for a frame name that TF does not accept.
return tl::unexpected(GnssPoser::TransformLookupError::Failed);
}
}

Expand Down
13 changes: 8 additions & 5 deletions sensing/autoware_gnss_poser/src/gnss_poser_node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <autoware/agnocast_wrapper/tf2.hpp>
#include <rclcpp/rclcpp.hpp>
#include <tf2/transform_datatypes.hpp>
#include <tl/expected.hpp>

#include <autoware_internal_debug_msgs/msg/bool_stamped.hpp>
#include <autoware_map_msgs/msg/map_projector_info.hpp>
Expand All @@ -31,7 +32,6 @@
#include <sensor_msgs/msg/nav_sat_fix.hpp>
#include <tf2_geometry_msgs/tf2_geometry_msgs.hpp>

#include <optional>
#include <string>

namespace autoware::gnss_poser
Expand All @@ -54,13 +54,16 @@ class GnssPoserNode : public autoware::agnocast_wrapper::Node
const AUTOWARE_MESSAGE_CONST_SHARED_PTR(autoware_sensing_msgs::msg::GnssInsOrientationStamped) &
msg);

// The latest transform between the two frames, or std::nullopt (with a throttled warning) when TF
// cannot provide it. The antenna sits rigidly on the vehicle, so the transform is static and the
// latest one is the one that applies to every fix.
std::optional<geometry_msgs::msg::Transform> get_static_transform(
// The latest transform between the two frames, or why TF cannot provide it. The antenna sits
// rigidly on the vehicle, so the transform is static and the latest one is the one that applies
// to every fix.
tl::expected<geometry_msgs::msg::Transform, GnssPoser::TransformLookupError> get_static_transform(
const std::string & target_frame, const std::string & source_frame);
// Publish the messages the pose computation produced.
void publish_data(const GnssPoser::Result & result);
// Log why the antenna transform is missing, at the fix where GnssPoser says to.
void log_antenna_transform_error(
const std::string & antenna_frame, GnssPoser::TransformLookupError error);

// Declared before tf2_listener_, so that a missing or invalid parameter throws before the
// listener starts its spin thread. If the listener is destroyed before that thread enters
Expand Down
Loading
Loading