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
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@ compatibility.

## Unreleased

- A Scene can carry the covariance of the ego position and the pose a route
starts from. `EgoSequence.position_covariances_3d` is `[T,3,3]` in Scene axes
and m², and `T4Scene.start_poses_3d` is `[T,4,4]` like `goal_poses_3d`.
Both are optional, come last in their records, follow `slice_time()`, `frame_at()` and `reanchor()` in
Python and C++, and use new Protobuf field numbers, so existing Scenes read
unchanged. `start` is a new consumer requirement name. The ROS2
generator fills both from `nav_msgs/Odometry.pose.covariance` and
`LaneletRoute.start_pose`; see
[ROS2 inputs](ros2/t4scene_generation/configs/README.md#ros2-inputs).
- `T4Scene.from_proto(message, rows=(start, stop))` decodes one window of a
stored Scene without decoding or validating its other rows; `slice_model_data` cuts a
nonempty `model_data` payload to the window. Decoding also
Expand Down
17 changes: 15 additions & 2 deletions ros2/t4scene_generation/configs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,27 @@ acceleration in addition to the six inputs in the online preset.

| Input key | ROS message | Scene data |
| --- | --- | --- |
| `odometry` | `nav_msgs/msg/Odometry` | 3D pose and linear and angular velocities |
| `odometry` | `nav_msgs/msg/Odometry` | 3D pose, linear and angular velocities, and the covariance of the position in `position_covariances_3d` |
| `steering` | `autoware_vehicle_msgs/msg/SteeringReport` | Road-wheel angle from `steering_tire_angle` in radians |
| `indicator` | `autoware_vehicle_msgs/msg/TurnIndicatorsReport` | Turn-indicator state |
| `annotations` | `autoware_perception_msgs/msg/TrackedObjects` | Timestamped box, cylinder, and polygon batches |
| `traffic_lights` | `autoware_perception_msgs/msg/TrafficLightGroupArray` | Signal group IDs and element observations |
| `route` | `autoware_planning_msgs/msg/LaneletRoute` | Lane alternatives and preferred lanes in `routes`, and goal-to-Scene transforms in `goal_poses_3d` |
| `route` | `autoware_planning_msgs/msg/LaneletRoute` | Lane alternatives and preferred lanes in `routes`, goal-to-Scene transforms in `goal_poses_3d`, and start-to-Scene transforms in `start_poses_3d` |
| `acceleration` | `geometry_msgs/msg/AccelWithCovarianceStamped` | Linear acceleration, rotated into Scene axes |

The position covariance is the upper-left 3×3 block of the odometry message's
pose covariance, which is expressed in the Scene frame. A row interpolated
between two messages takes the same weighted mean of their covariances as of
their positions. The covariance of a message is unknown when the block is all
zero or fails the SDK's rule for a stored covariance (finite, symmetric and
positive semidefinite within its tolerance, see
[the wire format](../../../t4scene/proto/README.md)); the pose of that message
is still used, and every covariance the generator keeps can be exported. The
symmetric part of the block is stored. A source that never reports one leaves the column out.

The start pose of a route is unknown when its orientation is not a unit
quaternion; the route and its goal are still used.

For bag input, the configured odometry topic must exist. An optional topic may
be absent. Any bound topic present in the bag must use the listed message type
and CDR serialization.
Expand Down
54 changes: 51 additions & 3 deletions ros2/t4scene_generation/cpp/src/generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
#include "observations.hpp"
#include <Eigen/Geometry>
#include <algorithm>
#include <array>
#include <chrono>
#include <cmath>
#include <iterator>
#include <limits>
#include <mutex>
#include <optional>
#include <rclcpp/logging.hpp>
#include <t4_scene/scene.hpp>
#include <t4scene_generation/generator.hpp>
Expand All @@ -19,6 +21,8 @@ struct Motion {
Vec position;
Eigen::Quaterniond rotation;
Vec velocity, angular;
// Covariance of the position in Scene axes; unset when the source gives none.
std::optional<Eigen::Matrix3d> position_covariance{};
};
double blend(double a, double b, double t) {
const double delta = b - a;
Expand All @@ -29,8 +33,14 @@ Vec blend(const Vec &a, const Vec &b, double t) {
return a.binaryExpr(b, [t](double x, double y) { return blend(x, y, t); });
}
Motion blend(const Motion &a, const Motion &b, double t) {
return {blend(a.position, b.position, t), a.rotation.slerp(t, b.rotation),
blend(a.velocity, b.velocity, t), blend(a.angular, b.angular, t)};
Motion value{
blend(a.position, b.position, t), a.rotation.slerp(t, b.rotation),
blend(a.velocity, b.velocity, t), blend(a.angular, b.angular, t)};
// A convex combination of two covariances is a covariance.
if (a.position_covariance && b.position_covariance)
value.position_covariance =
(1 - t) * *a.position_covariance + t * *b.position_covariance;
return value;
}
struct Accel {
Vec vector;
Expand Down Expand Up @@ -165,6 +175,21 @@ Grid window_grid(const Config &c, int64_t anchor) {
return {grid_time(anchor, -history, step), step, size_t(history + 1 + future),
size_t(history)};
}
// The position block of a row-major 6x6 pose covariance, in the parent frame.
// Unknown when it is all zero (the usual way to publish "not estimated") or
// fails the SDK's rule for a stored covariance (scene_detail::covariance_fault),
// so that every covariance taken here can be exported: the pose of a message
// with an unusable covariance is still a pose.
std::optional<Eigen::Matrix3d>
read_position_covariance(const std::array<double, 36> &pose_covariance) {
Eigen::Matrix<double, 3, 3, Eigen::RowMajor> c;
for (int i = 0; i < 3; ++i)
for (int j = 0; j < 3; ++j)
c(i, j) = pose_covariance[6 * i + j];
if (c.isZero(0) || scene_detail::covariance_fault(c.data()))
return std::nullopt;
return Eigen::Matrix3d(0.5 * (c + c.transpose())); // its symmetric part
}
Motion read_motion(const nav_msgs::msg::Odometry &msg, const Config &config) {
if (frame_name(msg.header.frame_id) != config.scene_frame ||
frame_name(msg.child_frame_id) != config.body_frame)
Expand All @@ -183,6 +208,7 @@ Motion read_motion(const nav_msgs::msg::Odometry &msg, const Config &config) {
if (!value.position.allFinite() || !value.velocity.allFinite() ||
!value.angular.allFinite())
throw std::invalid_argument("invalid odometry pose or twist");
value.position_covariance = read_position_covariance(msg.pose.covariance);
return value;
}
void write_motion(Array<double> &poses, Array<double> &vel, Array<double> &ang,
Expand All @@ -197,6 +223,11 @@ void write_motion(Array<double> &poses, Array<double> &vel, Array<double> &ang,
ang.set(i * 3 + j, value.angular[j]);
}
}
void write_covariance(Array<double> &covariances, size_t i,
const Eigen::Matrix3d &value) {
for (size_t j = 0; j < 9; ++j)
covariances.set(i * 9 + j, value(j / 3, j % 3));
}
T4Scene observation(int64_t measured) {
T4Scene result;
result.scene_metadata = {"observation"};
Expand Down Expand Up @@ -452,6 +483,8 @@ struct SceneBuilder::Impl {
auto &odom_time =
ego.odometry_timestamps_ns.emplace(std::vector<uint64_t>{n});
std::vector<uint8_t> missing_motion(n, 1), missing_steer(n, 1);
Array<double> covariance(std::vector<uint64_t>{n, 3, 3});
std::vector<uint8_t> missing_covariance(n, 1);
if (config.steering) {
ego.tire_steering_angles.emplace(std::vector<uint64_t>{n});
ego.tire_steering_timestamps_ns.emplace(std::vector<uint64_t>{n});
Expand All @@ -465,6 +498,10 @@ struct SceneBuilder::Impl {
missing_motion[i] = 0;
odom_time.set(i, s->measured);
write_motion(poses, vel, ang, i, s->value);
if (const auto &c = s->value.position_covariance) {
missing_covariance[i] = 0;
write_covariance(covariance, i, *c);
}
}
if (config.steering) {
if (auto s = select(future ? future_steer : past_steer, time,
Expand All @@ -482,6 +519,12 @@ struct SceneBuilder::Impl {
mask_row(vel, missing_motion, 3);
mask_row(ang, missing_motion, 3);
mask_row(odom_time, missing_motion, 1);
// A source that never reports a covariance leaves the column out.
if (std::find(missing_covariance.begin(), missing_covariance.end(), 0) !=
missing_covariance.end()) {
mask_row(covariance, missing_covariance, 9);
ego.position_covariances_3d = std::move(covariance);
}
if (config.steering) {
mask_row(*ego.tire_steering_angles, missing_steer, 1);
mask_row(*ego.tire_steering_timestamps_ns, missing_steer, 1);
Expand Down Expand Up @@ -688,7 +731,12 @@ T4Scene SceneBuilder::convert(const nav_msgs::msg::Odometry &msg) const {
auto &p = ego.poses_3d.emplace(std::vector<uint64_t>{1, 4, 4});
auto &v = ego.velocities_3d.emplace(std::vector<uint64_t>{1, 3});
auto &w = ego.angular_velocities_3d.emplace(std::vector<uint64_t>{1, 3});
write_motion(p, v, w, 0, read_motion(msg, impl_->config));
const auto motion = read_motion(msg, impl_->config);
write_motion(p, v, w, 0, motion);
if (motion.position_covariance)
write_covariance(
ego.position_covariances_3d.emplace(std::vector<uint64_t>{1, 3, 3}), 0,
*motion.position_covariance);
ego.odometry_timestamps_ns = scene.timestamps_ns;
return scene;
}
Expand Down
26 changes: 25 additions & 1 deletion ros2/t4scene_generation/cpp/src/observations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -632,6 +632,18 @@ read_route(const autoware_planning_msgs::msg::LaneletRoute &msg) {
Eigen::Vector3d(position.x, position.y, position.z);
for (size_t k = 0; k < 16; ++k)
result.goal[k] = goal(k / 4, k % 4);
// A planner can leave the start pose unset; that must not reject the route.
const auto &origin = msg.start_pose.position;
const auto heading = quaternion(msg.start_pose.orientation);
if (finite_xyz(origin) && valid_orientation(heading)) {
auto start = Eigen::Matrix4d::Identity().eval();
start.topLeftCorner<3, 3>() = heading.normalized().toRotationMatrix();
start.topRightCorner<3, 1>() =
Eigen::Vector3d(origin.x, origin.y, origin.z);
auto &pose = result.start.emplace();
for (size_t k = 0; k < 16; ++k)
pose[k] = start(k / 4, k % 4);
}
return result;
}

Expand All @@ -640,11 +652,19 @@ void pack(T4Scene &scene, const std::vector<const RouteObservation *> &rows) {
routes.indices = Array<int64_t>({rows.size()});
auto &goals =
scene.goal_poses_3d.emplace(std::vector<uint64_t>{rows.size(), 4, 4});
Array<double> starts(std::vector<uint64_t>{rows.size(), 4, 4});
std::map<const RouteObservation *, int64_t> indices;
std::vector<uint8_t> missing;
std::vector<uint8_t> missing, missing_start;
bool any_start = false;
for (size_t i = 0; i < rows.size(); ++i) {
const auto *observation = rows[i];
missing.insert(missing.end(), 16, !observation);
const bool has_start = observation && observation->start;
missing_start.insert(missing_start.end(), 16, !has_start);
any_start |= has_start;
if (has_start)
for (size_t k = 0; k < 16; ++k)
starts.set(i * 16 + k, (*observation->start)[k]);
if (!observation) {
routes.indices.set(i, -1);
continue;
Expand All @@ -657,5 +677,9 @@ void pack(T4Scene &scene, const std::vector<const RouteObservation *> &rows) {
goals.set(i * 16 + k, observation->goal[k]);
}
goals.set_mask(std::move(missing));
if (any_start) {
starts.set_mask(std::move(missing_start));
scene.start_poses_3d = std::move(starts);
}
}
} // namespace t4scene::ros2::generation
3 changes: 3 additions & 0 deletions ros2/t4scene_generation/cpp/src/observations.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#pragma once
#include <optional>
#include <t4scene_generation/generator.hpp>
namespace t4scene::ros2::generation {
int64_t measurement_time(const builtin_interfaces::msg::Time &);
Expand All @@ -12,6 +13,8 @@ void check_observation(
struct RouteObservation {
RouteData route;
Transform3D goal;
// Unset when the message carries no usable start pose.
std::optional<Transform3D> start;
autoware_planning_msgs::msg::LaneletRoute::ConstSharedPtr source;
};
RouteObservation read_route(const autoware_planning_msgs::msg::LaneletRoute &);
Expand Down
123 changes: 123 additions & 0 deletions ros2/t4scene_generation/cpp/tests/generator_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,128 @@ void check_route(const SceneBuilder &builder, int64_t cutoff, int64_t lane,
"source route differs from Scene route");
}

// The position block of the odometry's row-major 6x6 pose covariance.
nav_msgs::msg::Odometry located(int64_t time, double x, double variance_x) {
auto msg = odom(time, x);
msg.pose.covariance[0] = variance_x;
msg.pose.covariance[1] = msg.pose.covariance[6] = 0.02;
msg.pose.covariance[7] = 0.04;
msg.pose.covariance[14] = 0.25;
return msg;
}

void check_position_covariance_and_start_pose(const fs::path &configs) {
auto close = [](double a, double b) { return std::abs(a - b) < 1e-12; };
SceneBuilder builder(configs / "range.yaml");
check(!builder.convert(odom(0, 0)).ego->position_covariances_3d,
"an all-zero covariance is not an estimate");
const auto single = builder.convert(located(0, 0, 0.09));
check(single.ego->position_covariances_3d &&
single.ego->position_covariances_3d->at(0) == 0.09 &&
single.ego->position_covariances_3d->at(4) == 0.04,
"a converted odometry keeps its position covariance");

{
// Close to singular, inside the SDK's tolerance: what the generator takes,
// the default export must accept, also blended between two messages.
SceneBuilder edge(configs / "range.yaml");
auto nearly_singular = [](int64_t time, double x) {
auto msg = odom(time, x);
for (int i = 0; i < 3; ++i)
for (int j = 0; j < 3; ++j)
msg.pose.covariance[6 * i + j] = i == j ? 1 : -0.5000000004;
return msg;
};
check(edge.ingest(nearly_singular(0, 0), 0), "odometry rejected");
check(edge.ingest(located(150000000, 1.5, 0.18), 150000000),
"odometry rejected");
check(edge.ingest(nearly_singular(300000000, 3), 300000000),
"odometry rejected");
check(edge.ingest(route(0, 7), 0), "route rejected");
const auto exported = edge.build_range({"covariance-edge"});
exported.validate();
check(!exported.ego->position_covariances_3d->masked(0),
"a covariance within the tolerance is kept");
// Not positive semidefinite by 1e-5: no covariance, and still a pose.
auto indefinite = odom(0, 0);
indefinite.pose.covariance[1] = indefinite.pose.covariance[6] = 1e-5;
indefinite.pose.covariance[14] = 1;
const auto converted = builder.convert(indefinite);
check(!converted.ego->position_covariances_3d &&
!converted.ego->poses_3d->masked(3),
"an indefinite covariance is dropped and its pose kept");
}
{
SceneBuilder invalid(configs / "range.yaml");
auto msg = odom(0, 4);
msg.pose.covariance[0] = -0.5;
msg.pose.covariance[7] = 0.04;
msg.pose.covariance[14] = 1e6;
check(invalid.ingest(msg, 0), "odometry rejected");
const auto range = invalid.build_range({"negative-variance"});
check(!range.ego->position_covariances_3d &&
range.ego->poses_3d->get(3) == 4,
"a large Z variance cannot make a negative X variance usable");
}

// Rows at 0, 100, 200 and 300 ms from samples at 0, 150 and 300 ms.
check(builder.ingest(located(0, 0, 0.09), 0), "odometry rejected");
check(builder.ingest(located(150000000, 1.5, 0.18), 150000000),
"odometry rejected");
auto asymmetric = located(300000000, 3, 0.09);
asymmetric.pose.covariance[1] = 0.5;
check(builder.ingest(asymmetric, 300000000),
"a pose with an unusable covariance is still a pose");
auto with_start = route(0, 7);
with_start.start_pose.orientation.w = 1;
with_start.start_pose.position.x = 4;
with_start.start_pose.position.y = -2;
check(builder.ingest(with_start, 0), "route rejected");
const auto scene = builder.build_range({"covariance"});
check(scene.size() == 4, "range grid");
const auto &covariances = scene.ego->position_covariances_3d.value();
check(covariances.shape() == std::vector<uint64_t>{4, 3, 3},
"covariance column shape");
check(covariances.at(0) == 0.09 && covariances.at(1) == 0.02 &&
covariances.at(3) == 0.02 && covariances.at(4) == 0.04 &&
covariances.at(8) == 0.25,
"a row on a sample keeps that sample's covariance");
// Two thirds of the way from 0.09 to 0.18; equal entries stay equal.
check(close(*covariances.get(9), 0.15) && close(*covariances.get(13), 0.04),
"an interpolated row blends the covariances of its two samples");
for (size_t i = 18; i < 36; ++i)
check(covariances.masked(i),
"a row that needs a sample without a covariance has none");
check(!scene.ego->poses_3d->masked(3 * 16 + 3), "the pose itself is kept");

const auto &starts = scene.start_poses_3d.value();
const auto last = (scene.size() - 1) * 16;
check(starts.at(last + 3) == 4 && starts.at(last + 7) == -2 &&
starts.at(last) == 1,
"the route's start pose follows the route");
for (size_t i = 0; i < scene.size() * 16; ++i)
check(starts.masked(i) == scene.goal_poses_3d->masked(i),
"a row has a start pose exactly when it has a route");
check(scene.frame_at(scene.size() - 1).start_pose_3d->at(3) == 4,
"a frame carries its route's start pose");

// A start pose without a valid orientation is unknown, not a reason to drop
// the route.
SceneBuilder unset(configs / "range.yaml");
check(unset.ingest(odom(0, 0), 0) &&
unset.ingest(odom(100000000, 1), 100000000),
"odometry rejected");
auto without_start = route(0, 7);
without_start.start_pose.orientation.w = 0;
check(unset.ingest(without_start, 0),
"a route without a start pose is still a route");
const auto plain = unset.build_range({"plain"});
check(plain.routes && plain.goal_poses_3d && !plain.start_poses_3d,
"no start column without a start pose");
check(!plain.ego->position_covariances_3d,
"no covariance column from a source that never reports one");
}

void check_polygon_footprints(const fs::path &configs) {
autoware_perception_msgs::msg::TrackedObjects msg;
msg.header.frame_id = "map";
Expand Down Expand Up @@ -287,6 +409,7 @@ int main(int argc, char **argv) {
throw std::runtime_error("cannot create test directory");
fs::path work = pattern, bag = work / "test_bag";
check_polygon_footprints(configs);
check_position_covariance_and_start_pose(configs);
check_annotation_reuse(configs);
check_window_retention(configs, work);
SceneBuilder live(configs / "online.yaml");
Expand Down
Loading