Segment.cpp
Go to the documentation of this file.
1 #include "Segment.h"
2 
3 #include <sstream>
4 
5 #include <sys/inotify.h>
6 
7 #include <Eigen/Dense>
8 #include <Eigen/Geometry>
9 
10 #include <SimoxUtility/algorithm/get_map_keys_values.h>
11 #include <SimoxUtility/algorithm/string.h>
12 #include <SimoxUtility/json.h>
13 #include <SimoxUtility/math/pose/pose.h>
14 #include <SimoxUtility/math/regression/linear.h>
15 
24 
29 #include <RobotAPI/libraries/ArmarXObjects/aron/ObjectPose.aron.generated.h>
40 #include <RobotAPI/libraries/armem_objects/aron/FamiliarObjectInstance.aron.generated.h>
41 #include <RobotAPI/libraries/armem_objects/aron/ObjectClass.aron.generated.h>
42 #include <RobotAPI/libraries/armem_objects/aron/ObjectInstance.aron.generated.h>
50 
52 {
53 
54  void
56  {
57  defs->optional(robotName,
58  prefix + "robotName",
59  "Name of robot whose note can be calibrated.\n"
60  "If not given, the 'fallbackName' is used.");
61  defs->optional(robotNode, prefix + "robotNode", "Robot node which can be calibrated.");
62  defs->optional(offset, prefix + "offset", "Offset for the node to be calibrated.");
63  }
64 
66  SpecializedCoreSegment(memoryToIceAdapter,
67  objects::familiarObjectInstaceSegmentID.coreSegmentName,
68  arondto::FamiliarObjectInstance::ToAronType(),
69  64)
70  {
71  }
72 
74  {
75  }
76 
77  void
79  {
80  SpecializedCoreSegment::defineProperties(defs, prefix);
81 
82 
83  defs->optional(robots.fallbackName,
84  prefix + "robots.FallbackName",
85  "Robot name to use as fallback if the robot name is not specified "
86  "in a provided object pose.");
87 
88 
89  decay.defineProperties(defs, prefix + "decay.");
90  }
91 
92  void
94  {
95  SpecializedCoreSegment::init();
96 
98  }
99 
100  void
102  {
103  (void)arviz;
104  // ARMARX_INFO << "ArticulatedObjectVisu";
105  // this->visu = std::make_unique<ArticulatedObjectVisu>(arviz, *this);
106  // visu->init();
107  }
108 
109  void
111  const std::vector<armarx::armem::arondto::FamiliarObjectInstance>& familiarObjectInstances,
112  const std::string& providerName)
113  {
114  ARMARX_CHECK_NOT_EMPTY(providerName);
115 
116  Time now = Time::Now();
117 
119  const MemoryID coreSegmentID = segmentPtr->id();
120 
121  Commit commit;
122  for (const armarx::armem::arondto::FamiliarObjectInstance& familiarObjectInstance :
123  familiarObjectInstances)
124  {
125  EntityUpdate& update = commit.updates.emplace_back();
126 
127  const MemoryID providerID = coreSegmentID.withProviderSegmentName(providerName);
128 
129  armarx::ObjectID objectID;
130  fromAron(familiarObjectInstance.objectID, objectID);
131 
132  update.entityID = providerID.withEntityName(objectID.str());
133  update.arrivedTime = now;
134  update.referencedTime = familiarObjectInstance.timestamp;
135  update.confidence = familiarObjectInstance.confidence;
136 
137  update.instancesData.push_back(familiarObjectInstance.toAron());
138  }
139  // Commit non-locking.
140  ARMARX_INFO << "Committing " << commit.updates.size()
141  << " familiar object instances from provider " << providerName;
143  }
144 
145  std::vector<armarx::armem::arondto::FamiliarObjectInstance>
147  {
148  std::vector<armarx::armem::arondto::FamiliarObjectInstance> familiarObjects;
149 
151  [&familiarObjects, &now](const wm::Entity& entity)
152  {
153  entity.getLatestSnapshot().forEachInstance(
154  [&familiarObjects, &now](const wm::EntityInstance& instance)
155  {
156  armarx::armem::arondto::FamiliarObjectInstance dto;
157  dto.fromAron(instance.data());
158 
159  if (dto.timestamp <= now)
160  {
161  familiarObjects.push_back(dto);
162  }
163  });
164  });
165 
166  return familiarObjects;
167  }
168 
169  std::map<std::string, std::vector<armarx::armem::arondto::FamiliarObjectInstance>>
171  {
172  std::map<std::string, std::vector<armarx::armem::arondto::FamiliarObjectInstance>>
173  familiarObjectsByProvider;
174 
176 
177  [&now, &familiarObjectsByProvider](const wm::ProviderSegment& ps)
178  {
179  auto& familiarObjects = familiarObjectsByProvider[ps.name()];
180  ps.forEachEntity(
181  [&familiarObjects, &now](const wm::Entity& entity)
182  {
183  entity.getLatestSnapshot().forEachInstance(
184  [&familiarObjects, &now](const wm::EntityInstance& instance)
185  {
186  armarx::armem::arondto::FamiliarObjectInstance dto;
187  dto.fromAron(instance.data());
188 
189  if (dto.timestamp <= now)
190  {
191  familiarObjects.push_back(dto);
192  }
193  });
194  });
195  }
196 
197  );
198 
199  return familiarObjectsByProvider;
200  }
201 
203  Segment::RobotsCache::get(const std::string& _robotName, const std::string& providerName)
204  {
205  std::string robotName = _robotName;
206 
207  if (robotName.empty())
208  {
209  auto antiSpam = deactivateSpam(10 * 60); // 10 minutes.
210 
211  std::stringstream ss;
212  if (providerName.empty())
213  {
214  ss << "The provided object pose";
215  }
216  else
217  {
218  ss << "The object pose provided by '" << providerName << "'";
219  }
220  ss << " does not specify a robot name";
221 
222  if (fallbackName.empty())
223  {
224  ss << ", and no fallback robot name was configured (e.g. via the properties).\n"
225  << "For these object poses, the object instance segment is not able "
226  << "to provide transformed object poses (global and in robot root frame).";
227  ARMARX_INFO << antiSpam << ss.str();
228 
229  return nullptr;
230  }
231  else
232  {
233  ss << ". The object instance segment is falling back to '" << fallbackName << "'.";
234  ARMARX_INFO << antiSpam << ss.str();
235 
236  robotName = fallbackName;
237  }
238  }
239  ARMARX_CHECK_NOT_EMPTY(robotName);
240 
241  // Lookup in cache.
242  if (auto it = loaded.find(robotName); it != loaded.end())
243  {
244  ARMARX_CHECK_NOT_NULL(it->second);
245  return it->second;
246  }
247  else
248  {
249  // Try to fetch the robot.
251  VirtualRobot::RobotPtr robot = reader->getRobot(
252  robotName, Clock::Now(), VirtualRobot::RobotIO::RobotDescription::eStructure);
253 
254  if (robot)
255  {
256  bool synchronized = reader->synchronizeRobot(*robot, Clock::Now());
257  if (not synchronized)
258  {
259  ARMARX_INFO << "The robot '" << robotName << "' could be loaded, but not"
260  << " synchronized successfully (e.g., the global localization "
261  "could be missing). "
262  << "Make sure to synchronize it before use if necessary.";
263  }
264  // Store robot if valid.
265  loaded.emplace(robotName, robot);
266  }
267  return robot; // valid or nullptr
268  }
269  }
270 
271 
272 } // namespace armarx::armem::server::obj::familiar_object_instance
memory_ids.h
armarx::armem::server::obj::familiar_object_instance::Segment::getFamiliarObjects
std::vector< armarx::armem::arondto::FamiliarObjectInstance > getFamiliarObjects(const DateTime &now)
Definition: Segment.cpp:146
RemoteRobot.h
armarx::navigation::graph::coreSegmentID
const armem::MemoryID coreSegmentID
Definition: constants.h:30
armarx::armem::server::obj::familiar_object_instance::Segment::getFamiliarObjectsByProvider
std::map< std::string, std::vector< armarx::armem::arondto::FamiliarObjectInstance > > getFamiliarObjectsByProvider(const DateTime &now)
Definition: Segment.cpp:170
armarx::armem::server::obj::familiar_object_instance::Segment::init
void init() override
Definition: Segment.cpp:93
armarx::armem::server::obj::familiar_object_instance::Segment::Segment
Segment(server::MemoryToIceAdapter &iceMemory)
Definition: Segment.cpp:65
ice_conversions.h
armarx::ObjectID
A known object ID of the form "Dataset/ClassName" or "Dataset/ClassName/InstanceName".
Definition: ObjectID.h:10
armarx::armem::objects::familiarObjectInstaceSegmentID
const MemoryID familiarObjectInstaceSegmentID
Definition: memory_ids.cpp:33
Writer.h
armarx::armem::Commit
A bundle of updates to be sent to the memory.
Definition: Commit.h:84
armarx::armem::wm::EntityInstance
Client-side working entity instance.
Definition: memory_definitions.h:32
armarx::armem::server::MemoryToIceAdapter
Helps connecting a Memory server to the Ice interface.
Definition: MemoryToIceAdapter.h:19
armarx::armem::base::detail::GetLatestSnapshotMixin::getLatestSnapshot
auto & getLatestSnapshot(int snapshotIndex=0)
Retrieve the latest entity snapshot.
Definition: lookup_mixins.h:199
armarx::core::time::DateTime::Now
static DateTime Now()
Definition: DateTime.cpp:51
armarx::armem::server::segment::SpecializedCoreSegment::SpecializedCoreSegment
SpecializedCoreSegment(MemoryToIceAdapter &iceMemory, const std::string &defaultCoreSegmentName="", aron::type::ObjectPtr coreSegmentAronType=nullptr, int defaultMaxHistorySize=10, const std::vector< PredictionEngine > &predictionEngines={})
Definition: SpecializedCoreSegment.cpp:12
Segment.h
DateTime.h
ARMARX_CHECK_NOT_NULL
#define ARMARX_CHECK_NOT_NULL(ptr)
This macro evaluates whether ptr is not null and if it turns out to be false it will throw an Express...
Definition: ExpressionException.h:206
armarx::armem::server::obj::familiar_object_instance::Segment::defineProperties
void defineProperties(armarx::PropertyDefinitionsPtr defs, const std::string &prefix="") override
Definition: Segment.cpp:78
armarx::armem::server::obj::familiar_object_instance::Segment::connect
void connect(viz::Client arviz)
Definition: Segment.cpp:101
armarx::armem::base::ProviderSegmentBase::name
std::string & name()
Definition: ProviderSegmentBase.h:95
query_fns.h
ARMARX_CHECK_NOT_EMPTY
#define ARMARX_CHECK_NOT_EMPTY(c)
Definition: ExpressionException.h:224
ice_conversions.h
Scene.h
armarx::armem::server::wm::Entity
Definition: memory_definitions.h:27
armarx::armem::server::obj::familiar_object_instance::Segment::~Segment
virtual ~Segment() override
Definition: Segment.cpp:73
armarx::armem::base::detail::ForEachEntityMixin::forEachEntity
bool forEachEntity(FunctionT &&func)
Definition: iteration_mixins.h:259
Clock.h
memory_definitions.h
armarx::armem::server::segment::detail::SegmentBase< server::wm::CoreSegment >::segmentPtr
server::wm::CoreSegment * segmentPtr
Definition: SpecializedSegment.h:53
armarx::armem::MemoryID::withProviderSegmentName
MemoryID withProviderSegmentName(const std::string &name) const
Definition: MemoryID.cpp:417
armarx::armem::server::obj::familiar_object_instance::Segment::RobotsCache::reader
std::experimental::observer_ptr< robot_state::VirtualRobotReader > reader
Definition: Segment.h:87
armarx::armem::MemoryID
A memory ID.
Definition: MemoryID.h:47
json_conversions.h
if
if(!yyvaluep)
Definition: Grammar.cpp:645
FramedPose.h
armarx::armem::server::obj::familiar_object_instance::Segment::Calibration::robotName
std::string robotName
Definition: Segment.h:49
ObjectID.h
armarx::armem::base::CoreSegmentBase::forEachProviderSegment
bool forEachProviderSegment(ProviderSegmentFunctionT &&func)
Definition: CoreSegmentBase.h:217
error.h
armarx::armem::EntityUpdate
An update of an entity for a specific point in time.
Definition: Commit.h:25
armarx::armem::server::obj::familiar_object_instance::Segment::RobotsCache::loaded
std::map< std::string, VirtualRobot::RobotPtr > loaded
Definition: Segment.h:90
aron_conversions.h
armarx::armem::server::obj::familiar_object_instance::Segment::Calibration::offset
float offset
Definition: Segment.h:51
armarx::armem::server::obj::familiar_object_instance::Segment::Calibration::defineProperties
void defineProperties(armarx::PropertyDefinitionsPtr defs, const std::string &prefix="calibration.")
Definition: Segment.cpp:55
armarx::armem::server::obj::familiar_object_instance::Segment::robots
RobotsCache robots
Definition: Segment.h:96
TaskUtil.h
armarx::armem::base::detail::MemoryItem::id
MemoryID & id()
Definition: MemoryItem.h:25
armarx::armem::server::wm::ProviderSegment
Definition: memory_definitions.h:52
armarx::armem::server::ltm::util::mongodb::detail::update
bool update(mongocxx::collection &coll, const nlohmann::json &query, const nlohmann::json &update)
Definition: mongodb.cpp:68
aron_conversions.h
MemoryToIceAdapter.h
aron_conversions.h
ExpressionException.h
armarx::core::time::DateTime
Represents a point in time.
Definition: DateTime.h:24
armarx::armem::fromAron
void fromAron(const arondto::MemoryID &dto, MemoryID &bo)
Definition: aron_conversions.cpp:8
CMakePackageFinder.h
aron_conversions.h
armarx::armem::MemoryID::withEntityName
MemoryID withEntityName(const std::string &name) const
Definition: MemoryID.cpp:425
armarx::armem::server::segment::detail::SegmentBase< server::wm::CoreSegment >::iceMemory
MemoryToIceAdapter & iceMemory
Definition: SpecializedSegment.h:58
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:181
armarx::armem::base::ProviderSegmentBase::forEachEntity
bool forEachEntity(EntityFunctionT &&func)
Definition: ProviderSegmentBase.h:189
IceUtil::Handle< class PropertyDefinitionContainer >
Builder.h
armarx::Logging::tag
LogTag tag
Definition: Logging.h:278
armarx::armem::server::obj::familiar_object_instance::Segment::decay
Decay decay
Decay model.
Definition: Segment.h:105
armarx::core::time::Clock::Now
static DateTime Now()
Current time on the virtual clock.
Definition: Clock.cpp:93
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:99
ice_conversions_templates.h
util.h
armarx::Logging::setTag
void setTag(const LogTag &tag)
Definition: Logging.cpp:54
Logging.h
armarx::armem::server::obj::familiar_object_instance
Definition: Decay.cpp:8
aron_conversions.h
armarx::armem::server::obj::familiar_object_instance::Segment::commit
void commit(const std::vector< armarx::armem::arondto::FamiliarObjectInstance > &familiarObjectInstances, const std::string &providerName)
Definition: Segment.cpp:110
armarx::armem::base::EntityInstanceBase::data
const DataT & data() const
Definition: EntityInstanceBase.h:129
armarx::viz::Client
Definition: Client.h:117
aron_conversions.h
armarx::armem::server::obj::familiar_object_instance::Segment::RobotsCache::fallbackName
std::string fallbackName
Definition: Segment.h:88
armarx::armem::server::obj::familiar_object_instance::Segment::Calibration::robotNode
std::string robotNode
Definition: Segment.h:50
armarx::armem::server::MemoryToIceAdapter::commit
data::CommitResult commit(const data::Commit &commitIce, Time timeArrived)
Definition: MemoryToIceAdapter.cpp:115
armarx::ObjectID::str
std::string str() const
Return "dataset/className" or "dataset/className/instanceName".
Definition: ObjectID.cpp:60
VirtualRobot::RobotPtr
std::shared_ptr< class Robot > RobotPtr
Definition: Bus.h:19
ObjectFinder.h
armarx::armem::server::obj::familiar_object_instance::Segment::RobotsCache::get
VirtualRobot::RobotPtr get(const std::string &robotName, const std::string &providerName="")
Definition: Segment.cpp:203
armarx::armem::server::obj::familiar_object_instance::Decay::defineProperties
void defineProperties(armarx::PropertyDefinitionsPtr defs, const std::string &prefix="decay.")
Definition: Decay.cpp:12