prediction_helpers.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 phesch ( ulila at student dot kit dot edu )
17  * @date 2022
18  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
19  * GNU General Public License
20  */
21 
22 #pragma once
23 
24 #include <functional>
25 #include <vector>
26 
27 #include <ArmarXCore/core/time.h>
28 
32 
33 namespace armarx::armem
34 {
35  /**
36  * Holds info on snapshot data extracted from a time range.
37  * Used for performing predictions.
38  */
39  template <typename DataType, typename LatestType>
41  {
42  bool success = false;
43  std::string errorMessage = "";
44 
45  std::vector<double> timestampsSec = {};
46  std::vector<DataType> values = {};
47  LatestType latestValue;
48  };
49 
50  /*!
51  * @brief Get data points for the snapshots of an entity in a given time range.
52  *
53  * The resulting `SnapshotRangeInfo` is useful for performing predictions.
54  * Data from the individual snapshots is given to the conversion functions
55  * before being stored in the result.
56  * Timestamps in the `timestampsSec` vector are relative to the endTime parameter
57  * to this function.
58  * Data from a snapshot is always retrieved from the instance with index 0.
59  * If some data cannot be retrieved, `success` is set to `false`
60  * and the `errorMessage` in the return value is set accordingly.
61  *
62  * @param segment the segment to get the snapshot data from
63  * @param entityID the entity to get the data for
64  * @param startTime the beginning of the time range
65  * @param endTime the end of the time range. Timestamps are relative to this value
66  * @param dictToData the conversion function from `DictPtr` to `DataType`
67  * @param dictToLatest the conversion function from `DictPtr` to `LatestType`
68  * @return the corresponding `LatestSnapshotInfo`
69  */
70  template <typename SegmentType, typename DataType, typename LatestType>
72  getSnapshotsInRange(const SegmentType* segment,
73  const MemoryID& entityID,
74  const DateTime& startTime,
75  const DateTime& endTime,
76  std::function<DataType(const aron::data::DictPtr&)> dictToData,
77  std::function<LatestType(const aron::data::DictPtr&)> dictToLatest)
78  {
80  result.success = false;
81 
82  const server::wm::Entity* entity = segment->findEntity(entityID);
83  if (entity == nullptr)
84  {
85  std::stringstream sstream;
86  sstream << "Could not find entity with ID " << entityID << ".";
87  result.errorMessage = sstream.str();
88  return result;
89  }
90 
91  const int instanceIndex = 0;
92  bool querySuccess = true;
94  startTime,
95  endTime,
96  [&](const wm::EntitySnapshot& snapshot)
97  {
98  const auto* instance = snapshot.findInstance(instanceIndex);
99  if (instance)
100  {
101  result.timestampsSec.push_back(
102  (instance->id().timestamp - endTime).toSecondsDouble());
103  result.values.emplace_back(dictToData(instance->data()));
104  }
105  else
106  {
107  std::stringstream sstream;
108  sstream << "Could not find instance with index " << instanceIndex
109  << " in snapshot " << snapshot.id() << ".";
110  result.errorMessage = sstream.str();
111  querySuccess = false;
112  }
113  });
114 
115  if (querySuccess)
116  {
117  aron::data::DictPtr latest = entity->findLatestInstanceData(instanceIndex);
118  if (latest)
119  {
120  result.success = true;
121  result.latestValue = dictToLatest(latest);
122  }
123  else
124  {
125  std::stringstream sstream;
126  sstream << "Could not find instance with index " << instanceIndex << " for entity "
127  << entity->id() << ".";
128  result.errorMessage = sstream.str();
129  }
130  }
131 
132  return result;
133  }
134 
135 } // namespace armarx::armem
armarx::armem::SnapshotRangeInfo::timestampsSec
std::vector< double > timestampsSec
Definition: prediction_helpers.h:45
MemoryID.h
armarx::armem
Definition: LegacyRobotStateMemoryAdapter.cpp:31
armarx::armem::server::wm::Entity
Definition: memory_definitions.h:30
Dict.h
armarx::armem::wm::detail::FindInstanceDataMixinForEntity::findLatestInstanceData
aron::data::DictPtr findLatestInstanceData(int instanceIndex=0) const
Definition: data_lookup_mixins.h:62
armarx::armem::SnapshotRangeInfo::success
bool success
Definition: prediction_helpers.h:42
memory_definitions.h
armarx::armem::MemoryID
A memory ID.
Definition: MemoryID.h:47
armarx::armem::SnapshotRangeInfo::latestValue
LatestType latestValue
Definition: prediction_helpers.h:47
armarx::armem::getSnapshotsInRange
SnapshotRangeInfo< DataType, LatestType > getSnapshotsInRange(const SegmentType *segment, const MemoryID &entityID, const DateTime &startTime, const DateTime &endTime, std::function< DataType(const aron::data::DictPtr &)> dictToData, std::function< LatestType(const aron::data::DictPtr &)> dictToLatest)
Get data points for the snapshots of an entity in a given time range.
Definition: prediction_helpers.h:72
armarx::armem::wm::EntitySnapshot
Client-side working memory entity snapshot.
Definition: memory_definitions.h:80
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::client::query_fns::latest
std::function< void(query::SnapshotSelector &)> latest()
Definition: query_fns.h:109
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
armarx::armem::base::EntityBase::forEachSnapshotInTimeRange
void forEachSnapshotInTimeRange(const Time &min, const Time &max, FunctionT &&func) const
Return all snapshots between, including, min and max.
Definition: EntityBase.h:477
armarx::armem::SnapshotRangeInfo::errorMessage
std::string errorMessage
Definition: prediction_helpers.h:43
time.h
armarx::armem::SnapshotRangeInfo
Holds info on snapshot data extracted from a time range.
Definition: prediction_helpers.h:40
armarx::armem::base::EntitySnapshotBase::findInstance
EntityInstanceT * findInstance(int index)
Definition: EntitySnapshotBase.h:93