RobotWriter.cpp
Go to the documentation of this file.
1#include "RobotWriter.h"
2
3#include <chrono>
4#include <mutex>
5#include <optional>
6#include <thread>
7
13
22#include <RobotAPI/libraries/armem_robot_state/aron/Proprioception.aron.generated.h>
23#include <RobotAPI/libraries/armem_robot_state/aron/Robot.aron.generated.h>
24#include <RobotAPI/libraries/armem_robot_state/aron/RobotState.aron.generated.h>
25#include <RobotAPI/libraries/armem_robot_state/aron/Transform.aron.generated.h>
26#include <RobotAPI/libraries/armem_robot_state/aron/TransformHeader.aron.generated.h>
30
31
32namespace fs = ::std::filesystem;
33
35{
40
41 RobotWriter::~RobotWriter() = default;
42
43 void
47
48 void
50 {
51 // Wait for the memory to become available and add it as dependency.
52 ARMARX_IMPORTANT << "RobotWriter: Waiting for memory '" << constants::memoryName << "' ...";
53 try
54 {
56 ARMARX_IMPORTANT << "RobotWriter: Connected to memory '" << constants::memoryName
57 << "'";
58 }
60 {
61 ARMARX_ERROR << e.what();
62 return;
63 }
64 }
65
66 bool
69 {
70 const auto validTimestamp = timestamp.isInvalid() ? armarx::Clock::Now() : timestamp;
71
72 const auto providerId = armem::MemoryID(
74 const auto entityID =
75 providerId.withEntityName(constants::descriptionEntityName).withTimestamp(timestamp);
76
78 update.entityID = entityID;
79
80 arondto::RobotDescription aronDescription;
81 toAron(aronDescription, description);
82
83 update.instancesData = {aronDescription.toAron()};
84 update.referencedTime = timestamp;
85
86 ARMARX_DEBUG << "Committing " << update << " at time " << timestamp;
87 armem::EntityUpdateResult updateResult = memoryWriter.commit(update);
88
89 ARMARX_DEBUG << updateResult;
90
91 if (not updateResult.success)
92 {
93 ARMARX_ERROR << updateResult.errorMessage;
94 return false;
95 }
96
97 return true;
98 }
99
100 bool
101 RobotWriter::storeLocalization(const Eigen::Matrix4f& globalRootPose,
102 const std::string& robotName,
103 const std::string& robotRootNodeName,
104 const armem::Time& timestamp)
105 {
106
107 const auto providerId =
109 const auto entityID = providerId.withEntityName(GlobalFrame + "," + robotRootNodeName)
110 .withTimestamp(timestamp);
111
112 armem::EntityUpdate update;
113 update.entityID = entityID;
114
115 arondto::Transform aronTransform;
116
117 // populate aronTransform
118 {
119 aronTransform.header.agent = robotName;
120 aronTransform.header.frame = robotRootNodeName;
121 aronTransform.header.parentFrame = GlobalFrame;
122 aronTransform.header.timestamp = timestamp;
123
124 aronTransform.transform = globalRootPose;
125 }
126
127 update.instancesData = {aronTransform.toAron()};
128 update.referencedTime = timestamp;
129
130 ARMARX_DEBUG << "Committing " << update << " at time " << timestamp;
131 armem::EntityUpdateResult updateResult = memoryWriter.commit(update);
132
133 ARMARX_DEBUG << updateResult;
134
135 if (not updateResult.success)
136 {
137 ARMARX_ERROR << updateResult.errorMessage;
138 return false;
139 }
140
141 return true;
142 }
143
144 bool
145 RobotWriter::storeProprioception(const std::map<std::string, float>& jointMap,
146 const std::string& robotTypeName,
147 const std::string& robotName,
148 const armem::Time& timestamp)
149 {
150
151 const auto providerId = armem::MemoryID(
153 const auto entityID = providerId.withEntityName(robotName).withTimestamp(timestamp);
154
155 armem::EntityUpdate update;
156 update.entityID = entityID;
157
158 arondto::Proprioception aronProprioception;
159 aronProprioception.joints.position = jointMap;
160
161 update.instancesData = {aronProprioception.toAron()};
162 update.referencedTime = timestamp;
163
164 ARMARX_DEBUG << "Committing " << update << " at time " << timestamp;
165 armem::EntityUpdateResult updateResult = memoryWriter.commit(update);
166
167 ARMARX_DEBUG << updateResult;
168
169 if (not updateResult.success)
170 {
171 ARMARX_ERROR << updateResult.errorMessage;
172 return false;
173 }
174
175 return true;
176 }
177
178 bool
180 const std::string& robotTypeName,
181 const std::string& robotName,
182 const std::string& robotRootNodeName)
183 {
184 const bool successLoc = storeLocalization(
185 state.globalPose.matrix(), robotName, robotRootNodeName, state.timestamp);
186 const bool successProp =
187 storeProprioception(state.jointMap, robotTypeName, robotName, state.timestamp);
188
189 return successLoc and successProp;
190 }
191
192
193} // namespace armarx::armem::robot_state
std::string timestamp()
static DateTime Now()
Current time on the virtual clock.
Definition Clock.cpp:93
The memory name system (MNS) client.
Writer useWriter(const MemoryID &memoryID)
Use a memory server and get a writer for it.
Indicates that a query to the Memory Name System failed.
Definition mns.h:25
void connect(armem::client::MemoryNameSystem &memoryNameSystem)
struct armarx::armem::robot_state::RobotWriter::Properties properties
bool storeProprioception(const std::map< std::string, float > &jointMap, const std::string &robotTypeName, const std::string &robotName, const armem::Time &timestamp)
bool storeLocalization(const Eigen::Matrix4f &globalRootPose, const std::string &robotName, const std::string &robotRootNodeName, const armem::Time &timestamp)
void registerPropertyDefinitions(::armarx::PropertyDefinitionsPtr &def)
bool storeDescription(const description::RobotDescription &description, const armem::Time &timestamp=armem::Time::Invalid()) override
bool storeState(const RobotState &state, const std::string &robotTypeName, const std::string &robotName, const std::string &robotRootNodeName) override
#define ARMARX_IMPORTANT
The logging level for always important information, but expected behaviour (in contrast to ARMARX_WAR...
Definition Logging.h:190
#define ARMARX_ERROR
The logging level for unexpected behaviour, that must be fixed.
Definition Logging.h:196
#define ARMARX_DEBUG
The logging level for output that is only interesting while debugging.
Definition Logging.h:184
std::string const GlobalFrame
Variable of the global coordinate system.
Definition FramedPose.h:65
const std::string descriptionCoreSegment
Definition constants.h:28
const std::string localizationCoreSegment
Definition constants.h:29
const std::string proprioceptionCoreSegment
Definition constants.h:30
const std::string descriptionEntityName
Definition constants.h:33
void toAron(arondto::ObjectInstance &dto, const RobotState &bo)
armarx::core::time::DateTime Time
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.
Result of an EntityUpdate.
Definition Commit.h:75
An update of an entity for a specific point in time.
Definition Commit.h:26