CartographerAdapter.h
Go to the documentation of this file.
1/*
2 * This file is part of ArmarX.
3 *
4 * ArmarX is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * ArmarX is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * @author Fabian Reister ( fabian dot reister at kit dot edu )
17 * @date 2021
18 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
19 * GNU General Public License
20 */
21
22#pragma once
23
24#include <cstdint>
25#include <filesystem>
26#include <memory>
27#include <mutex>
28#include <optional>
29#include <vector>
30
31#include <Eigen/Core>
32#include <Eigen/Geometry>
33
34#include <opencv2/core/mat.hpp>
35
37#include <ArmarXCore/interface/observers/ObserverInterface.h>
40
41#include <RobotAPI/interface/units/LaserScannerUnit.h>
42
45
46#include <cartographer/mapping/trajectory_builder_interface.h>
47
49{
50 class MapBuilderInterface;
51} // namespace cartographer::mapping
52
54{
55 struct PoseStamped;
56}
57
59{
61 struct LaserMessage;
62
63 class CartographerAdapter : virtual public MessageCallee
64 {
65 public:
66 struct Config
67 {
68 bool useOdometry{false};
69 bool useLaserScanner{true};
70 bool useImu{false};
71
72 float frequency{-1.F};
73
75
76 std::set<std::string> laserScanners; // "LaserScannerFront", "LaserScannerBack";
77
78 bool enableDebugTiming{false};
79
81 float min_score = 0.55F;
82 };
83
85 const std::filesystem::path& mapPath,
86 const std::filesystem::path& configPath,
87 SlamDataCallable& slamDataCallable,
88 const Config& config,
89 const std::optional<std::filesystem::path>& mapToLoad = std::nullopt,
90 const std::optional<Eigen::Isometry3f>& map_T_robot_prior = std::nullopt,
91 const DebugObserverInterfacePrx& debugObserver = nullptr);
92
93 virtual ~CartographerAdapter();
94
96
97 void onTimedDataAvailable(const TimedData& data) override;
98
99 struct OdomData
100 {
101 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
102 Eigen::Vector3f position;
104
105 //! timestamp in [ms]
106 int64_t timestamp;
107 };
108
109 void processOdometryPose(OdomData odomData);
110
111 using SensorId = ::cartographer::mapping::TrajectoryBuilderInterface::SensorId;
112
114 {
115 public:
117
118 [[nodiscard]] auto
119 origin() const -> Eigen::Vector3f
120 {
121 return pose.translation();
122 }
123
124 explicit LaserScannerSensor(const SensorId& sensorId, Eigen::Isometry3f pose) :
125 LaserScannerSensorBase(sensorId.id, pose), id(sensorId)
126 {
127 }
128 };
129
131
132 bool createMap(const armarx::PackagePath& mapOutputPath);
133 void setSensorPose(const std::string& sensorId, const Eigen::Isometry3f& pose);
134 bool hasReceivedSensorData() const noexcept;
135
136 std::vector<std::string> laserSensorIds() const;
137
138 // void triggerCallbacks() const;
139
140 ::cartographer::mapping::MapBuilderInterface& getMapBuilder();
141
142 unsigned int numConstraints() const;
143
144 private:
145 void pushInOdomData(
147 void pushInLaserData(const std::vector<LaserMessage>& laserMessages);
148
149 // laser scanners will be merged
150 const SensorId sensorIdLaser{SensorId::SensorType::RANGE, "laser"};
151 const SensorId sensorIdIMU{SensorId::SensorType::IMU, "imu"};
152 const SensorId sensorIdOdom{SensorId::SensorType::ODOMETRY, "odom"};
153
154 const LaserScannerSensor& lookupSensor(const std::string& device) const;
155
156 void getLocalSlamResultCallback(
157 const int /*trajectory_id*/,
158 const ::cartographer::common::Time /*time*/,
159 const ::cartographer::transform::Rigid3d localPose,
160 ::cartographer::sensor::RangeData rangeDataInLocal,
161 const std::unique_ptr<
162 const ::cartographer::mapping::TrajectoryBuilderInterface::InsertionResult>
163 insertionResult);
164
165 std::unique_ptr<::cartographer::mapping::MapBuilderInterface> mapBuilder;
166
167 std::map<std::string, int64_t> lastSensorDataTimestamp;
168 std::optional<std::int64_t> lastOdomTimestamp;
169
170 std::optional<std::int64_t> lastLaserScannerInsertCartoTimestamp;
171
172 const Config config;
173
174 int trajectoryId{0};
175
176 std::filesystem::path mapPath;
177
178 std::set<SensorId> sensorSet;
179
180 SlamDataCallable& slamDataCallable;
181
182 ::cartographer::mapping::proto::TrajectoryBuilderOptions trajectoryBuilderOptions;
183
184 mutable std::mutex cartoInsertMtx;
185
186 std::unique_ptr<ApproximateTimeQueue> approxTimeQueue;
187
188 std::unordered_map<std::string, LaserScannerSensor> laserSensorMap;
189
190 std::optional<FrequencyReporter> cartographerInputDataReporterLaserScanner;
191 std::optional<FrequencyReporter> cartographerInputDataReporterOdometry;
192
193 mutable std::optional<DebugObserverHelper> debugObserverHelper;
194 mutable std::mutex debugObserverHelperMtx;
195
196 struct DebugObserverScopedTimer
197 {
198 DebugObserverScopedTimer(const CartographerAdapter* parent, const std::string& name);
199
200 ~DebugObserverScopedTimer();
201
202 const CartographerAdapter* parent;
203 std::string name;
204 armarx::DateTime start;
205 };
206 };
207} // namespace armarx::localization_and_mapping::cartographer_adapter
Represents a point in time.
Definition DateTime.h:25
CartographerAdapter(const std::filesystem::path &mapPath, const std::filesystem::path &configPath, SlamDataCallable &slamDataCallable, const Config &config, const std::optional< std::filesystem::path > &mapToLoad=std::nullopt, const std::optional< Eigen::Isometry3f > &map_T_robot_prior=std::nullopt, const DebugObserverInterfacePrx &debugObserver=nullptr)
void setSensorPose(const std::string &sensorId, const Eigen::Isometry3f &pose)
::cartographer::mapping::TrajectoryBuilderInterface::SensorId SensorId
LaserScannerSensorBase(const std::string &frame, const Eigen::Isometry3f &pose)
Definition types.h:141
Quaternion< float, 0 > Quaternionf
This file offers overloads of toIce() and fromIce() functions for STL container types.
::IceInternal::ProxyHandle<::IceProxy::armarx::DebugObserverInterface > DebugObserverInterfacePrx
This file is part of ArmarX.