博主连接:
https://www.cnblogs.com/yhlx125/p/8137885.html
上文提到特别注意map_builder_bridge_.AddTrajectory(x,x),查看其中的代码。两点:
首先是map_builder_.AddTrajectoryBuilder(…),调用了map_builder_对象的方法。其次是sensor_bridges_键值对的赋值。
int MapBuilderBridge::AddTrajectory(const std::unordered_set<std::string>& expected_sensor_ids, const TrajectoryOptions& trajectory_options)
{
const int trajectory_id = map_builder_.AddTrajectoryBuilder(expected_sensor_ids, trajectory_options.trajectory_builder_options,
::std::bind(&MapBuilderBridge::OnLocalSlamResult, this,
::std::placeholders::_1, ::std::placeholders::_2,
::std::placeholders::_3, ::std::placeholders::_4,
::std::placeholders::_5));
LOG(INFO) << "Added trajectory with ID '" << trajectory_id << "'.";
// Make sure there is no trajectory with 'trajectory_id' yet.
CHECK_EQ(sensor_bridges_.count(trajectory_id), 0);
sensor_bridges_[trajectory_id] = cartographer::common::make_unique<SensorBridge>(
trajectory_options.num_subdivisions_per_laser_scan,
trajectory_options.tracking_frame,
node_options_.lookup_transform_timeout_sec, tf_buffer_,
map_builder_.GetTrajectoryBuilder(trajectory_id));
auto emplace_result = trajectory_options_.emplace(trajectory_id, trajectory_options);
CHECK(emplace_result.second == true);
return trajectory_id;
}
其中map_builder_.AddTrajectoryBuilder(…)是Cartographer项目中的代码了。
int MapBuilder::AddTrajectoryBuilder( const std::unordered_set<std::string>& expected_sensor_ids, const proto::TrajectoryBuilderOptions& trajectory_options,
LocalSlamResultCallback local_slam_result_callback)
{
const int trajectory_id = trajectory_builders_.size();//生成trajectory_id
if (options_.use_trajectory_builder_3d())
{
CHECK(trajectory_options.has_trajectory_builder_3d_options());
trajectory_builders_.push_back(common::make_unique<CollatedTrajectoryBuilder>(
&sensor_collator_, trajectory_id, expected_sensor_ids,
common::make_unique<mapping::GlobalTrajectoryBuilder<
mapping_3d::LocalTrajectoryBuilder,
mapping_3d::proto::LocalTrajectoryBuilderOptions,
mapping_3d::PoseGraph>>(
trajectory_options.trajectory_builder_3d_options(),
trajectory_id, pose_graph_3d_.get(),
local_slam_result_callback)));//注意此处的push_back()方法
}
else
{
CHECK(trajectory_options.has_trajectory_builder_2d_options());
trajectory_builders_.push_back(common::make_unique<CollatedTrajectoryBuilder>(
&sensor_collator_, trajectory_id, expected_sensor_ids,
common::make_unique<mapping::GlobalTrajectoryBuilder<
mapping_2d::LocalTrajectoryBuilder,
mapping_2d::proto::LocalTrajectoryBuilderOptions,
mapping_2d::PoseGraph>>(
trajectory_options.trajectory_builder_2d_options(),
trajectory_id, pose_graph_2d_.get(),
local_slam_result_callback)));//注意此处的push_back()方法
}
if (trajectory_options.pure_localization())
{
constexpr int kSubmapsToKeep = 3;
pose_graph_->AddTrimmer(common::make_unique<PureLocalizationTrimmer>(trajectory_id, kSubmapsToKeep));
}
if (trajectory_options.has_initial_trajectory_pose())
{
const auto& initial_trajectory_pose = trajectory_options.initial_trajectory_pose();
pose_graph_->SetInitialTrajectoryPose(trajectory_id, initial_trajectory_pose.to_trajectory_id(),
transform::ToRigid3(initial_trajectory_pose.relative_pose()), common::FromUniversal(initial_trajectory_pose.timestamp()));
}
return trajectory_id;
}
注意,trajectory_builders_是根据trajectory_id添加的。以后调用的时候根据trajectory_id调用。
**2D/3D区分:**同时可以看到,这里对2D和3D情况作了区分,根据options_.use_trajectory_builder_3d()确定使用的类型。
在ROS的主循环运行过程中,会不断处理传感器传入的数据。
以IMU数据为例,auto sensor_bridge_ptr = map_builder_bridge_.sensor_bridge(trajectory_id),根据trajectory_id获取sensor_bridge_ptr。注意这里因为是订阅的其它ROS主题(Topic),所以sensor_id参数是从其他主题传入的。(即当前程序内部有一套主题名称的字符串,订阅了外部主题也有一套名称字符串表示。这样两者通过同样的名称字符串建立了关系)
void Node::HandleImuMessage(const int trajectory_id, const std::string& sensor_id, const sensor_msgs::Imu::ConstPtr& msg)
{
carto::common::MutexLocker lock(&mutex_);
if (!sensor_samplers_.at(trajectory_id).imu_sampler.Pulse())
{
return;
}
auto sensor_bridge_ptr = map_builder_bridge_.sensor_bridge(trajectory_id);
auto imu_data_ptr = sensor_bridge_ptr->ToImuData(msg);
if (imu_data_ptr != nullptr)
{
extrapolators_.at(trajectory_id).AddImuData(*imu_data_ptr);
}
sensor_bridge_ptr->HandleImuMessage(sensor_id, msg);
}
最后调用了sensor_bridge_ptr->HandleImuMessage(sensor_id, msg);的代码。这里又通过trajectory_builder_调用了AddSensorData方法,由于之前做为参数传入的是CollatedTrajectoryBuilder,所以实际调用的是CollatedTrajectoryBuilder的AddSensorData方法。
void SensorBridge::HandleImuMessage(const std::string& sensor_id, const sensor_msgs::Imu::ConstPtr& msg)
{
std::unique_ptr<::cartographer::sensor::ImuData> imu_data = ToImuData(msg);
if (imu_data != nullptr)
{
trajectory_builder_->AddSensorData( sensor_id, cartographer::sensor::ImuData{imu_data->time, imu_data->linear_acceleration, imu_data->angular_velocity});
}
}
SensorBridge类实现代码,消息转换函数查看msg_conversion.cc文件:
SensorBridge::SensorBridge(
const int num_subdivisions_per_laser_scan,
const std::string& tracking_frame,
const double lookup_transform_timeout_sec, tf2_ros::Buffer* const tf_buffer,
carto::mapping::TrajectoryBuilderInterface* const trajectory_builder)
: num_subdivisions_per_laser_scan_(num_subdivisions_per_laser_scan),
tf_bridge_(tracking_frame, lookup_transform_timeout_sec, tf_buffer),
trajectory_builder_(trajectory_builder) {}
std::unique_ptr<::cartographer::sensor::OdometryData>
SensorBridge::ToOdometryData(const nav_msgs::Odometry::ConstPtr& msg) {
const carto::common::Time time = FromRos(msg->header.stamp);
const auto sensor_to_tracking = tf_bridge_.LookupToTracking(
time, CheckNoLeadingSlash(msg->child_frame_id));
if (sensor_to_tracking == nullptr) {
return nullptr;
}
return ::cartographer::common::make_unique<
::cartographer::sensor::OdometryData>(
::cartographer::sensor::OdometryData{
time, ToRigid3d(msg->pose.pose) * sensor_to_tracking->inverse()});
}
void SensorBridge::HandleOdometryMessage(
const std::string& sensor_id, const nav_msgs::Odometry::ConstPtr& msg) {
std::unique_ptr<::cartographer::sensor::OdometryData> odometry_data =
ToOdometryData(msg);
if (odometry_data != nullptr) {
trajectory_builder_->AddSensorData(
sensor_id, cartographer::sensor::OdometryData{odometry_data->time,
odometry_data->pose});
}
}
std::unique_ptr<::cartographer::sensor::ImuData> SensorBridge::ToImuData(
const sensor_msgs::Imu::ConstPtr& msg) {
CHECK_NE(msg->linear_acceleration_covariance[0], -1)
<< "Your IMU data claims to not contain linear acceleration measurements "
"by setting linear_acceleration_covariance[0] to -1. Cartographer "
"requires this data to work. See "
"http://docs.ros.org/api/sensor_msgs/html/msg/Imu.html.";
CHECK_NE(msg->angular_velocity_covariance[0], -1)
<< "Your IMU data claims to not contain angular velocity measurements "
"by setting angular_velocity_covariance[0] to -1. Cartographer "
"requires this data to work. See "
"http://docs.ros.org/api/sensor_msgs/html/msg/Imu.html.";
const carto::common::Time time = FromRos(msg->header.stamp);
const auto sensor_to_tracking = tf_bridge_.LookupToTracking(
time, CheckNoLeadingSlash(msg->header.frame_id));
if (sensor_to_tracking == nullptr) {
return nullptr;
}
CHECK(sensor_to_tracking->translation().norm() < 1e-5)
<< "The IMU frame must be colocated with the tracking frame. "
"Transforming linear acceleration into the tracking frame will "
"otherwise be imprecise.";
return ::cartographer::common::make_unique<::cartographer::sensor::ImuData>(
::cartographer::sensor::ImuData{
time,
sensor_to_tracking->rotation() * ToEigen(msg->linear_acceleration),
sensor_to_tracking->rotation() * ToEigen(msg->angular_velocity)});
}
void SensorBridge::HandleImuMessage(const std::string& sensor_id,
const sensor_msgs::Imu::ConstPtr& msg) {
std::unique_ptr<::cartographer::sensor::ImuData> imu_data = ToImuData(msg);
if (imu_data != nullptr) {
trajectory_builder_->AddSensorData(
sensor_id, cartographer::sensor::ImuData{imu_data->time,
imu_data->linear_acceleration,
imu_data->angular_velocity});
}
}
void SensorBridge::HandleLaserScanMessage(
const std::string& sensor_id, const sensor_msgs::LaserScan::ConstPtr& msg) {
::cartographer::sensor::PointCloudWithIntensities point_cloud;
::cartographer::common::Time time;
std::tie(point_cloud, time) = ToPointCloudWithIntensities(*msg);
HandleLaserScan(sensor_id, time, msg->header.frame_id, point_cloud);
}
void SensorBridge::HandleMultiEchoLaserScanMessage(
const std::string& sensor_id,
const sensor_msgs::MultiEchoLaserScan::ConstPtr& msg) {
::cartographer::sensor::PointCloudWithIntensities point_cloud;
::cartographer::common::Time time;
std::tie(point_cloud, time) = ToPointCloudWithIntensities(*msg);
HandleLaserScan(sensor_id, time, msg->header.frame_id, point_cloud);
}
void SensorBridge::HandlePointCloud2Message(
const std::string& sensor_id,
const sensor_msgs::PointCloud2::ConstPtr& msg) {
pcl::PointCloud<pcl::PointXYZ> pcl_point_cloud;
pcl::fromROSMsg(*msg, pcl_point_cloud);
carto::sensor::TimedPointCloud point_cloud;
for (const auto& point : pcl_point_cloud) {
point_cloud.emplace_back(point.x, point.y, point.z, 0.f);
}
HandleRangefinder(sensor_id, FromRos(msg->header.stamp), msg->header.frame_id,
point_cloud);
}
const TfBridge& SensorBridge::tf_bridge() const { return tf_bridge_; }
void SensorBridge::HandleLaserScan(
const std::string& sensor_id, const carto::common::Time time,
const std::string& frame_id,
const carto::sensor::PointCloudWithIntensities& points) {
CHECK_LE(points.points.back()[3], 0);
// TODO(gaschler): Use per-point time instead of subdivisions.
for (int i = 0; i != num_subdivisions_per_laser_scan_; ++i) {
const size_t start_index =
points.points.size() * i / num_subdivisions_per_laser_scan_;
const size_t end_index =
points.points.size() * (i + 1) / num_subdivisions_per_laser_scan_;
carto::sensor::TimedPointCloud subdivision(
points.points.begin() + start_index, points.points.begin() + end_index);
if (start_index == end_index) {
continue;
}
const double time_to_subdivision_end = subdivision.back()[3];
// `subdivision_time` is the end of the measurement so sensor::Collator will
// send all other sensor data first.
const carto::common::Time subdivision_time =
time + carto::common::FromSeconds(time_to_subdivision_end);
for (auto& point : subdivision) {
point[3] -= time_to_subdivision_end;
}
CHECK_EQ(subdivision.back()[3], 0);
HandleRangefinder(sensor_id, subdivision_time, frame_id, subdivision);
}
}
void SensorBridge::HandleRangefinder(
const std::string& sensor_id, const carto::common::Time time,
const std::string& frame_id, const carto::sensor::TimedPointCloud& ranges) {
const auto sensor_to_tracking =
tf_bridge_.LookupToTracking(time, CheckNoLeadingSlash(frame_id));
if (sensor_to_tracking != nullptr) {
trajectory_builder_->AddSensorData(
sensor_id, cartographer::sensor::TimedPointCloudData{
time, sensor_to_tracking->translation().cast<float>(),
carto::sensor::TransformTimedPointCloud(
ranges, sensor_to_tracking->cast<float>())});
}
}
SensorBridge
ToPointCloudWithIntensities(const sensor_msgs::PointCloud2& message) {
PointCloudWithIntensities point_cloud;
// We check for intensity field here to avoid run-time warnings if we pass in
// a PointCloud2 without intensity.
if (PointCloud2HasField(message, "intensity")) {
pcl::PointCloud<pcl::PointXYZI> pcl_point_cloud;
pcl::fromROSMsg(message, pcl_point_cloud);
for (const auto& point : pcl_point_cloud) {
point_cloud.points.emplace_back(point.x, point.y, point.z, 0.f);
point_cloud.intensities.push_back(point.intensity);
}
} else {
pcl::PointCloud<pcl::PointXYZ> pcl_point_cloud;
pcl::fromROSMsg(message, pcl_point_cloud);
// If we don't have an intensity field, just copy XYZ and fill in
// 1.0.
for (const auto& point : pcl_point_cloud) {
point_cloud.points.emplace_back(point.x, point.y, point.z, 0.f);
point_cloud.intensities.push_back(1.0);
}
}
return std::make_tuple(point_cloud, FromRos(message.header.stamp));
}
msg_conversion.cc
查看CollatedTrajectoryBuilder的AddSensorData方法,在CollatedTrajectoryBuilder的头文件中,包括4个覆写的AddSensorData(x,x)方法,方法中通过sensor::MakeDispatchable转换为Dispatchable<DataType>类型。
void AddSensorData(const std::string& sensor_id, const sensor::TimedPointCloudData& timed_point_cloud_data) override
{
AddSensorData(sensor::MakeDispatchable(sensor_id, timed_point_cloud_data));
}
void AddSensorData(const std::string& sensor_id, const sensor::ImuData& imu_data) override
{
AddSensorData(sensor::MakeDispatchable(sensor_id, imu_data));
}
void AddSensorData(const std::string& sensor_id, const sensor::OdometryData& odometry_data) override
{
AddSensorData(sensor::MakeDispatchable(sensor_id, odometry_data));
}
void AddSensorData(const std::string& sensor_id, const sensor::FixedFramePoseData& fixed_frame_pose_data) override
{
AddSensorData(sensor::MakeDispatchable(sensor_id, fixed_frame_pose_data));
}
最终定位到了sensor_collator_对象的方法。
void CollatedTrajectoryBuilder::AddSensorData( std::unique_ptr<sensor::Data> data)
{
sensor_collator_->AddSensorData(trajectory_id_, std::move(data));
}
查看几个类CollatedTrajectoryBuildermapping::GlobalTrajectoryBuilder
CollatedTrajectoryBuilder::CollatedTrajectoryBuilder(sensor::Collator* const sensor_collator, const int trajectory_id,
const std::unordered_set<std::string>& expected_sensor_ids, std::unique_ptr<TrajectoryBuilderInterface> wrapped_trajectory_builder)
: sensor_collator_(sensor_collator),
trajectory_id_(trajectory_id),
wrapped_trajectory_builder_(std::move(wrapped_trajectory_builder)),
last_logging_time_(std::chrono::steady_clock::now())
{
sensor_collator_->AddTrajectory(trajectory_id, expected_sensor_ids, [this](const std::string& sensor_id, std::unique_ptr<sensor::Data> data)
{HandleCollatedSensorData(sensor_id, std::move(data));});
}
mapping::GlobalTrajectoryBuilder构造函数
GlobalTrajectoryBuilder(const LocalTrajectoryBuilderOptions& options, const int trajectory_id,
PoseGraph* const pose_graph, const LocalSlamResultCallback& local_slam_result_callback)
: trajectory_id_(trajectory_id), pose_graph_(pose_graph),
local_trajectory_builder_(options), local_slam_result_callback_(local_slam_result_callback)
{}
注意这里的继承关系:
class CollatedTrajectoryBuilder : public TrajectoryBuilderInterface
class GlobalTrajectoryBuilder : public mapping::TrajectoryBuilderInterface
在mapping_2d和mapping_3d两个命名空间下分别存在2个local_trajectory_builder_类,实现了局部的扫描匹配和子图构建。代码在cartographer\cartographer\internal文件夹下。
另外一个重要的Node类变量是extrapolators_,该对象在Node类的处理Odometry和IMU数据时都有用到,作用是位姿推算。在文一种Node::AddTrajectory方法中调用了AddExtrapolator(trajectory_id, options);
std::map<int, ::cartographer::mapping::PoseExtrapolator> extrapolators_;
void Node::AddExtrapolator(const int trajectory_id, const TrajectoryOptions& options)
{
constexpr double kExtrapolationEstimationTimeSec = 0.001; // 1 ms
CHECK(extrapolators_.count(trajectory_id) == 0);
const double gravity_time_constant =
node_options_.map_builder_options.use_trajectory_builder_3d()
? options.trajectory_builder_options.trajectory_builder_3d_options()
.imu_gravity_time_constant()
: options.trajectory_builder_options.trajectory_builder_2d_options()
.imu_gravity_time_constant();
extrapolators_.emplace(
std::piecewise_construct, std::forward_as_tuple(trajectory_id),
std::forward_as_tuple(
::cartographer::common::FromSeconds(kExtrapolationEstimationTimeSec),
gravity_time_constant));
}
map的emplace方法,高效插入。http://en.cppreference.com/w/cpp/container/map/emplace