Skip to content
Snippets Groups Projects
Commit e3df9705 authored by Fabian Reister's avatar Fabian Reister
Browse files

fix: catching exception in TransformHelper

parent 315f51de
No related branches found
No related tags found
No related merge requests found
#include "TransformHelper.h"
#include <optional>
#include <string>
#include <SimoxUtility/algorithm/get_map_keys_values.h>
......@@ -48,12 +49,23 @@ namespace armarx::armem::common::robot_state::localization
const std::vector<Eigen::Affine3f> transforms = _obtainTransforms(
localizationCoreSegment, tfChain, query.header.agent, query.header.timestamp);
const armem::Time sanitizedTimestamp = _obtainTimestamp(localizationCoreSegment, query.header.timestamp);
const std::optional<armem::Time> sanitizedTimestamp = _obtainTimestamp(localizationCoreSegment, query.header.timestamp);
if(not sanitizedTimestamp.has_value())
{
return {.transform = {.header = query.header},
.status = TransformResult::Status::Error,
.errorMessage = "Error: Issue with timestamp"};
}
auto header = query.header;
ARMARX_CHECK(sanitizedTimestamp.has_value());
// ARMARX_INFO << header.timestamp << "vs" << sanitizedTimestamp;
header.timestamp = sanitizedTimestamp;
header.timestamp = sanitizedTimestamp.value();
if (transforms.empty())
{
......@@ -178,27 +190,43 @@ namespace armarx::armem::common::robot_state::localization
}
template <class ...Args>
armarx::core::time::DateTime
std::optional<armarx::core::time::DateTime>
TransformHelper::_obtainTimestamp(const armem::base::CoreSegmentBase<Args...>& localizationCoreSegment, const armem::Time& timestamp)
{
// first we check which the newest timestamp is
int64_t timeSinceEpochUs = 0;
std::optional<int64_t> timeSinceEpochUs = std::nullopt;
localizationCoreSegment.forEachEntity([&timeSinceEpochUs, &timestamp](const auto& entity){
auto snapshot = entity.findLatestSnapshotBeforeOrAt(timestamp);
if(snapshot == nullptr)
{
return;
}
if(not snapshot->hasInstance(0))
{
return;
}
const armem::wm::EntityInstance& item = snapshot->getInstance(0);
const auto tf = _convertEntityToTransform(item);
const auto& dataTs = tf.header.timestamp;
timeSinceEpochUs = std::max(timeSinceEpochUs, dataTs.toMicroSecondsSinceEpoch());
timeSinceEpochUs = std::max(timeSinceEpochUs.value_or(0), dataTs.toMicroSecondsSinceEpoch());
});
if(not timeSinceEpochUs.has_value())
{
return std::nullopt;
}
// then we ensure that the timestamp is not more recent than the query timestamp
timeSinceEpochUs = std::min(timeSinceEpochUs, timestamp.toMicroSecondsSinceEpoch());
timeSinceEpochUs = std::min(timeSinceEpochUs.value(), timestamp.toMicroSecondsSinceEpoch());
return armarx::core::time::DateTime(armarx::core::time::Duration::MicroSeconds(timeSinceEpochUs));
return armarx::core::time::DateTime(armarx::core::time::Duration::MicroSeconds(timeSinceEpochUs.value()));
}
......
......@@ -113,7 +113,7 @@ namespace armarx::armem::common::robot_state::localization
template <class ...Args>
static
armarx::core::time::DateTime
std::optional<armarx::core::time::DateTime>
_obtainTimestamp(const armem::base::CoreSegmentBase<Args...>& localizationCoreSegment, const armem::Time& timestamp);
static
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment