Segment.cpp
Go to the documentation of this file.
1 #include "Segment.h"
2 
3 // STL
4 #include <iterator>
5 
6 #include <manif/SE3.h>
7 
8 #include <SimoxUtility/math/pose/pose.h>
9 #include <SimoxUtility/math/regression/linear.h>
10 
12 
15 
21 
22 #include <RobotAPI/libraries/armem_robot_state/aron/Robot.aron.generated.h>
24 
31 
32 
34 {
35 
37  Base(memoryToIceAdapter, armem::robot_state::localizationSegmentID.coreSegmentName, arondto::Transform::ToAronType(), 1024)
38  {
39  }
40 
41 
43  {
44  }
45 
46  void Segment::defineProperties(armarx::PropertyDefinitionsPtr defs, const std::string& prefix)
47  {
48  Base::defineProperties(defs, prefix);
49 
50  defs->optional(properties.predictionTimeWindow,
51  "prediction.TimeWindow",
52  "Duration of time window into the past to use for predictions"
53  " when requested via the PredictingMemoryInterface (in seconds).");
54  }
55 
57  {
58  Base::init();
59 
61  [this](const PredictionRequest& request){ return this->predictLinear(request); });
62  }
63 
64 
66  {
67  }
68 
69 
72  {
73  return this->doLocked([this, &timestamp]()
74  {
75  return getRobotFramePoses(timestamp);
76  });
77  }
78 
79 
81  Segment::getRobotFramePoses(const armem::Time& timestamp) const
82  {
83  using ::armarx::armem::robot_state::localization::TransformHelper;
84  using ::armarx::armem::robot_state::localization::TransformQuery;
85 
86  RobotFramePoseMap frames;
87  for (const std::string& robotName : segmentPtr->getProviderSegmentNames())
88  {
89  TransformQuery query
90  {
91  .header = {
92  .parentFrame = armarx::GlobalFrame,
94  .agent = robotName,
95  .timestamp = timestamp
96  }
97  };
98 
99  const auto result = TransformHelper::lookupTransformChain(*segmentPtr, query);
100  if (not result)
101  {
102  // TODO
103  continue;
104  }
105 
106  frames.emplace(robotName, result.transforms);
107  }
108 
110  << "Number of known robot pose chains: " << frames.size();
111 
112  return frames;
113  }
114 
115 
118  {
119  return this->doLocked([this, &timestamp]()
120  {
121  return getRobotGlobalPoses(timestamp);
122  });
123  }
124 
125 
128  {
129  using ::armarx::armem::robot_state::localization::TransformHelper;
130  using ::armarx::armem::robot_state::localization::TransformQuery;
131 
132  RobotPoseMap robotGlobalPoses;
133  for (const std::string& robotName : segmentPtr->getProviderSegmentNames())
134  {
135  TransformQuery query
136  {
137  .header =
138  {
139  .parentFrame = GlobalFrame,
141  .agent = robotName,
142  .timestamp = timestamp
143  }
144  };
145 
146  if (const auto result = TransformHelper::lookupTransform(*segmentPtr, query))
147  {
148  robotGlobalPoses.emplace(robotName, result.transform.transform);
149  }
150  else
151  {
152  // TODO
153  }
154  }
155 
157  << "Number of known robot poses: " << robotGlobalPoses.size();
158 
159  return robotGlobalPoses;
160  }
161 
162 
164  {
165  Commit commit;
166  commit.add(makeUpdate(transform));
167 
168  const armem::CommitResult result = iceMemory.commit(commit);
169  return result.allSuccess();
170  }
171 
172 
174  {
175  Commit commit;
176  commit.add(makeUpdate(transform));
177 
178  const armem::CommitResult result = iceMemory.commitLocking(commit);
179  return result.allSuccess();
180  }
181 
182 
184  {
185  const armem::Time& timestamp = transform.header.timestamp;
186  const MemoryID providerID = segmentPtr->id().withProviderSegmentName(transform.header.agent);
187 
189  update.entityID = providerID.withEntityName(transform.header.parentFrame + "," + transform.header.frame);
190  update.arrivedTime = update.referencedTime = update.sentTime = timestamp;
191 
192  arondto::Transform aronTransform;
193  toAron(aronTransform, transform);
194  update.instancesData = {aronTransform.toAron()};
195 
196  return update;
197  }
198 
199 
200  PredictionResult Segment::predictLinear(const PredictionRequest& request)
201  {
202  PredictionResult result;
203  result.snapshotID = request.snapshotID;
204  if (request.predictionSettings.predictionEngineID != "Linear")
205  {
206  result.success = false;
207  result.errorMessage = "Prediction engine " +
209  " is not supported in Proprioception.";
210  return result;
211  }
212 
213  static const int tangentDims = 6;
214  using Vector6d = Eigen::Matrix<double, tangentDims, 1>;
215 
216  const DateTime timeOrigin = DateTime::Now();
217  const armarx::Duration timeWindow =
218  Duration::SecondsDouble(properties.predictionTimeWindow);
220 
221  doLocked(
222  [&, this]()
223  {
224  info = getSnapshotsInRange<server::wm::CoreSegment, Vector6d, arondto::Transform>(
225  segmentPtr,
226  request.snapshotID,
227  timeOrigin - timeWindow,
228  timeOrigin,
229  [](const aron::data::DictPtr& data)
230  {
231  Eigen::Matrix4d mat =
232  arondto::Transform::FromAron(data).transform.cast<double>();
233  manif::SE3d se3(simox::math::position(mat),
234  Eigen::Quaterniond(simox::math::orientation(mat)));
235  return se3.log().coeffs();
236  },
237  [](const aron::data::DictPtr& data)
238  { return arondto::Transform::FromAron(data); });
239  });
240 
241  if (info.success)
242  {
243  Eigen::Matrix4f prediction;
244  if (info.timestampsSec.size() <= 1)
245  {
246  prediction = info.latestValue.transform;
247  }
248  else
249  {
250  using simox::math::LinearRegression;
251  const bool inputOffset = false;
252  const LinearRegression<tangentDims> model = LinearRegression<tangentDims>::Fit(
253  info.timestampsSec, info.values, inputOffset);
254  const auto predictionTime = request.snapshotID.timestamp;
255  Vector6d linearPred =
256  model.predict((predictionTime - timeOrigin).toSecondsDouble());
257  prediction = manif::SE3Tangentd(linearPred).exp().transform().cast<float>();
258  }
259 
260  info.latestValue.transform = prediction;
261  result.success = true;
262  result.prediction = info.latestValue.toAron();
263  }
264  else
265  {
266  result.success = false;
267  result.errorMessage = info.errorMessage;
268  }
269 
270  return result;
271  }
272 
273 } // namespace armarx::armem::server::robot_state::localization
armarx::armem::MemoryID::timestamp
Time timestamp
Definition: MemoryID.h:54
ARMARX_VERBOSE
#define ARMARX_VERBOSE
Definition: Logging.h:180
armarx::armem::PredictionRequest
Definition: Prediction.h:49
armarx::armem::PredictionResult::snapshotID
armem::MemoryID snapshotID
Definition: Prediction.h:63
armarx::armem::PredictionEngine::engineID
std::string engineID
Definition: Prediction.h:35
armarx::armem::SnapshotRangeInfo::timestampsSec
std::vector< double > timestampsSec
Definition: prediction_helpers.h:45
armarx::armem::robot_state::localizationSegmentID
const MemoryID localizationSegmentID
Definition: memory_ids.cpp:33
prediction_helpers.h
armarx::armem::Commit
A bundle of updates to be sent to the memory.
Definition: Commit.h:89
armarx::armem::server::robot_state::localization::Segment::~Segment
virtual ~Segment() override
Definition: Segment.cpp:42
armarx::armem::server::MemoryToIceAdapter
Helps connecting a Memory server to the Ice interface.
Definition: MemoryToIceAdapter.h:19
armarx::core::time::DateTime::Now
static DateTime Now()
Definition: DateTime.cpp:55
MemoryID.h
armarx::GlobalFrame
const std::string GlobalFrame
Definition: FramedPose.h:62
armarx::armem::toAron
void toAron(arondto::MemoryID &dto, const MemoryID &bo)
Definition: aron_conversions.cpp:19
armarx::armem::PredictionRequest::predictionSettings
PredictionSettings predictionSettings
Definition: Prediction.h:52
armarx::armem
Definition: LegacyRobotStateMemoryAdapter.cpp:31
constants.h
types.h
armarx::armem::server::robot_state::localization::Segment::Segment
Segment(server::MemoryToIceAdapter &iceMemory)
Definition: Segment.cpp:36
armarx::armem::robot_state::localization::Transform
Definition: types.h:154
armarx::armem::server::robot_state::localization
Definition: forward_declarations.h:71
armarx::armem::robot_state::constants::robotRootNodeName
const std::string robotRootNodeName
Definition: constants.h:36
armarx::armem::PredictionResult
Definition: Prediction.h:58
armarx::armem::server::robot_state::localization::Segment::defineProperties
void defineProperties(armarx::PropertyDefinitionsPtr defs, const std::string &prefix="") override
Definition: Segment.cpp:46
armarx::armem::SnapshotRangeInfo::success
bool success
Definition: prediction_helpers.h:42
armarx::armem::base::CoreSegmentBase::getProviderSegmentNames
std::vector< std::string > getProviderSegmentNames() const
Definition: CoreSegmentBase.h:271
TransformHelper.h
armarx::armem::server::segment::detail::SegmentBase< server::wm::CoreSegment >::segmentPtr
server::wm::CoreSegment * segmentPtr
Definition: SpecializedSegment.h:54
armarx::armem::MemoryID::withProviderSegmentName
MemoryID withProviderSegmentName(const std::string &name) const
Definition: MemoryID.cpp:412
armarx::armem::MemoryID
A memory ID.
Definition: MemoryID.h:47
armarx::armem::PredictionResult::prediction
aron::data::DictPtr prediction
Definition: Prediction.h:64
FramedPose.h
armarx::armem::server::robot_state::localization::Segment::onConnect
void onConnect()
Definition: Segment.cpp:65
armarx::armem::server::segment::detail::SegmentBase< server::wm::CoreSegment >
armarx::armem::server::segment::SpecializedCoreSegment::init
virtual void init() override
Definition: SpecializedCoreSegment.cpp:49
data
uint8_t data[1]
Definition: EtherCATFrame.h:68
armarx::armem::server::segment::SpecializedCoreSegment::doLocked
auto doLocked(FunctionT &&function) const
Definition: SpecializedCoreSegment.h:39
armarx::armem::server::robot_state::localization::Segment::getRobotGlobalPosesLocking
RobotPoseMap getRobotGlobalPosesLocking(const armem::Time &timestamp) const
Definition: Segment.cpp:117
Segment.h
armarx::armem::server::robot_state::localization::Segment::getRobotGlobalPoses
RobotPoseMap getRobotGlobalPoses(const armem::Time &timestamp) const
Definition: Segment.cpp:127
armarx::armem::EntityUpdate
An update of an entity for a specific point in time.
Definition: Commit.h:27
armarx::armem::CommitResult
Result of a Commit.
Definition: Commit.h:110
armarx::core::time::Duration::SecondsDouble
static Duration SecondsDouble(double seconds)
Constructs a duration in seconds.
Definition: Duration.cpp:90
aron_conversions.h
armarx::armem::SnapshotRangeInfo::latestValue
LatestType latestValue
Definition: prediction_helpers.h:47
armarx::armem::server::robot_state::localization::Segment::commitTransformLocking
bool commitTransformLocking(const armem::robot_state::localization::Transform &transform)
Definition: Segment.cpp:173
types.h
armarx::armem::SnapshotRangeInfo::values
std::vector< DataType > values
Definition: prediction_helpers.h:46
armarx::armem::base::detail::MemoryItem::id
MemoryID & id()
Definition: MemoryItem.h:27
armarx::armem::server::wm::detail::Prediction::addPredictor
void addPredictor(const PredictionEngine &engine, Predictor &&predictor)
Definition: Prediction.h:68
armarx::armem::server::ltm::util::mongodb::detail::update
bool update(mongocxx::collection &coll, const nlohmann::json &query, const nlohmann::json &update)
Definition: mongodb.cpp:67
aron_conversions.h
MemoryToIceAdapter.h
armarx::armem::server::robot_state::localization::Segment::commitTransform
bool commitTransform(const armem::robot_state::localization::Transform &transform)
Definition: Segment.cpp:163
armarx::armem::PredictionEngine
Definition: Prediction.h:33
aron_conversions.h
armarx::core::time::DateTime
Represents a point in time.
Definition: DateTime.h:24
armarx::aron::data::DictPtr
std::shared_ptr< Dict > DictPtr
Definition: Dict.h:41
GfxTL::Matrix4f
MatrixXX< 4, 4, float > Matrix4f
Definition: MatrixXX.h:601
armarx::armem::MemoryID::withEntityName
MemoryID withEntityName(const std::string &name) const
Definition: MemoryID.cpp:420
armarx::armem::server::robot_state::localization::RobotPoseMap
std::unordered_map< std::string, Eigen::Isometry3f > RobotPoseMap
Definition: forward_declarations.h:73
armarx::armem::Commit::add
EntityUpdate & add()
Definition: Commit.cpp:81
armarx::armem::server::segment::detail::SegmentBase< server::wm::CoreSegment >::iceMemory
MemoryToIceAdapter & iceMemory
Definition: SpecializedSegment.h:60
armarx::armem::PredictionResult::errorMessage
std::string errorMessage
Definition: Prediction.h:61
armarx::armem::server::robot_state::localization::Segment::getRobotFramePosesLocking
RobotFramePoseMap getRobotFramePosesLocking(const armem::Time &timestamp) const
Definition: Segment.cpp:71
IceUtil::Handle< class PropertyDefinitionContainer >
armarx::transform
auto transform(const Container< InputT, Alloc > &in, OutputT(*func)(InputT const &)) -> Container< OutputT, typename std::allocator_traits< Alloc >::template rebind_alloc< OutputT > >
Convenience function (with less typing) to transform a container of type InputT into the same contain...
Definition: algorithm.h:315
armarx::core::time::Duration
Represents a duration.
Definition: Duration.h:17
Time.h
armarx::armem::SnapshotRangeInfo::errorMessage
std::string errorMessage
Definition: prediction_helpers.h:43
armarx::armem::SnapshotRangeInfo
Holds info on snapshot data extracted from a time range.
Definition: prediction_helpers.h:40
armarx::Logging::deactivateSpam
SpamFilterDataPtr deactivateSpam(float deactivationDurationSec=10.0f, const std::string &identifier="", bool deactivate=true) const
disables the logging for the current line for the given amount of seconds.
Definition: Logging.cpp:92
Eigen::Matrix
Definition: EigenForwardDeclarations.h:27
armarx::armem::server::robot_state::localization::Segment::init
void init() override
Definition: Segment.cpp:56
armarx::armem::server::robot_state::localization::RobotFramePoseMap
std::unordered_map< std::string, std::vector< Eigen::Isometry3f > > RobotFramePoseMap
Definition: forward_declarations.h:74
Logging.h
robot_conversions.h
armarx::armem::PredictionResult::success
bool success
Definition: Prediction.h:60
armarx::armem::PredictionRequest::snapshotID
armem::MemoryID snapshotID
Definition: Prediction.h:51
armarx::armem::CommitResult::allSuccess
bool allSuccess() const
Definition: Commit.cpp:64
armarx::armem::server::segment::SpecializedCoreSegment::defineProperties
virtual void defineProperties(armarx::PropertyDefinitionsPtr defs, const std::string &prefix="") override
Definition: SpecializedCoreSegment.cpp:33
armarx::armem::server::MemoryToIceAdapter::commitLocking
data::CommitResult commitLocking(const data::Commit &commitIce, Time timeArrived)
Definition: MemoryToIceAdapter.cpp:166
memory_ids.h
armarx::armem::server::MemoryToIceAdapter::commit
data::CommitResult commit(const data::Commit &commitIce, Time timeArrived)
Definition: MemoryToIceAdapter.cpp:115
armarx::armem::server::robot_state::localization::Segment::getRobotFramePoses
RobotFramePoseMap getRobotFramePoses(const armem::Time &timestamp) const
Definition: Segment.cpp:81
armarx::armem::PredictionSettings::predictionEngineID
std::string predictionEngineID
Definition: Prediction.h:43