LegacyRobotStateMemoryAdapter.cpp
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 * @package RobotAPI::ArmarXObjects::MemoryNameSystem
17 * @author Rainer Kartmann ( rainer dot kartmann at kit dot edu )
18 * @date 2020
19 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20 * GNU General Public License
21 */
22
24
25#include <SimoxUtility/math/convert/mat3f_to_rpy.h>
26
27#include <RobotAPI/libraries/armem_robot_state/aron/Proprioception.aron.generated.h>
28#include <RobotAPI/libraries/armem_robot_state/aron/RobotDescription.aron.generated.h>
29#include <RobotAPI/libraries/armem_robot_state/aron/Transform.aron.generated.h>
31
33
35 "LegacyRobotStateMemoryAdapter");
36
38{
39
42 {
45
46 std::string prefix = "mem.";
47 defs->optional(properties.frequency,
48 prefix + "updateFrequency",
49 "The frequency in Hz to check for updates and send them to the memory.");
50 defs->optional(properties.memoryNameSystemName,
51 prefix + "memoryNameSystemName",
52 "The name of the MemoryNameSystem.");
53 defs->optional(properties.robotStateMemoryName,
54 prefix + "memoryName",
55 "The name of the RobotStateMemory.");
56
57 prefix = "listener.";
58 defs->topic<KinematicUnitListener>("RealRobotState", prefix + "KinematicUnitName");
59 defs->topic<PlatformUnitListener>("Armar6PlatformUnit", prefix + "PlatformUnitName");
60 return defs;
61 }
62
63 std::string
65 {
66 return "LegacyRobotStateMemoryAdapter";
67 }
68
69 void
70 LegacyRobotStateMemoryAdapter::checkUpdateAndSendToMemory()
71 {
72 std::lock_guard l(updateMutex);
73 if (updateChanged)
74 {
75 // If the update is not changed it has probably been committed already.
77 << "Try to send data to robotStateMemory but data has not changed.";
78 return;
79 }
80
81 // convert the update into a commit and send to memory
82 arondto::Proprioception prop;
83 for (const auto& [k, v] : update.jointAngles)
84 {
85 prop.joints.position[k] = v;
86 }
87 for (const auto& [k, v] : update.jointAccelerations)
88 {
89 prop.joints.acceleration[k] = v;
90 }
91 for (const auto& [k, v] : update.jointCurrents)
92 {
93 prop.joints.motorCurrent[k] = v;
94 }
95 for (const auto& [k, v] : update.jointTorques)
96 {
97 prop.joints.torque[k] = v;
98 }
99 for (const auto& [k, v] : update.jointVelocities)
100 {
101 prop.joints.velocity[k] = v;
102 }
103
104 // is this corect??
105 prop.platform.acceleration = Eigen::Vector3f::Zero();
106 prop.platform.relativePosition =
107 Eigen::Vector3f(update.platformPose.x, // this should be globasl AFAIK
108 update.platformPose.y,
109 update.platformPose.rotationAroundZ);
110 prop.platform.velocity = Eigen::Vector3f(std::get<0>(update.platformVelocity),
111 std::get<1>(update.platformVelocity),
112 std::get<2>(update.platformVelocity));
113
114 armem::EntityUpdate entityUpdate;
115 entityUpdate.entityID = propEntityID;
117 _timestampUpdateFirstModifiedInUs)); // we take the oldest timestamp
118
119 entityUpdate.instancesData = {prop.toAron()};
120
121 ARMARX_DEBUG << "Committing " << entityUpdate;
122 armem::EntityUpdateResult updateResult = memoryWriter.commit(entityUpdate);
123 ARMARX_DEBUG << updateResult;
124 if (!updateResult.success)
125 {
126 ARMARX_ERROR << updateResult.errorMessage;
127 }
128
129 // store odometry pose in localization segment
130 {
131 armem::arondto::Transform transform;
132 transform.header.agent = "Armar3";
133 transform.header.parentFrame = OdometryFrame;
135 transform.header.timestamp =
136 armem::Time(armem::Duration::MicroSeconds(_timestampUpdateFirstModifiedInUs));
137
138 Eigen::Isometry3f tf = Eigen::Isometry3f::Identity();
139 tf.translation().x() = (update.platformPose.x);
140 tf.translation().y() = (update.platformPose.y);
141 tf.linear() =
142 Eigen::AngleAxisf((update.platformPose.rotationAroundZ), Eigen::Vector3f::UnitZ())
143 .toRotationMatrix();
144
145 transform.transform = tf.matrix();
146
147 armem::EntityUpdate locUpdate;
148 locUpdate.entityID = locEntityID;
149 locUpdate.referencedTime =
150 Time(Duration::MicroSeconds(_timestampUpdateFirstModifiedInUs));
151 locUpdate.instancesData = {transform.toAron()};
152
153 ARMARX_DEBUG << "Committing " << entityUpdate;
154 armem::EntityUpdateResult updateResult = memoryWriter.commit(locUpdate);
155 ARMARX_DEBUG << updateResult;
156 if (!updateResult.success)
157 {
158 ARMARX_ERROR << updateResult.errorMessage;
159 }
160 }
161
162
163 // reset update
164 updateChanged = true;
165 }
166
167 void
168 LegacyRobotStateMemoryAdapter::updateTimestamps(long ts)
169 {
170 if (updateChanged)
171 {
172 _timestampUpdateFirstModifiedInUs = IceUtil::Time::now().toMicroSeconds();
173 }
174 updateChanged = false;
175 }
176
177 void
179 Ice::Long,
180 bool,
181 const Ice::Current&)
182 {
183 }
184
185 void
187 Ice::Long ts,
188 bool,
189 const Ice::Current&)
190 {
191 ARMARX_DEBUG << "Got an update for joint angles";
192 std::lock_guard l(updateMutex);
193 update.jointAngles = m;
194 updateTimestamps(ts);
195 }
196
197 void
199 Ice::Long ts,
200 bool,
201 const Ice::Current&)
202 {
203 ARMARX_DEBUG << "Got an update for joint vels";
204 std::lock_guard l(updateMutex);
205 update.jointVelocities = m;
206 updateTimestamps(ts);
207 }
208
209 void
211 Ice::Long ts,
212 bool,
213 const Ice::Current&)
214 {
215 ARMARX_DEBUG << "Got an update for joint torques";
216 std::lock_guard l(updateMutex);
217 update.jointTorques = m;
218 updateTimestamps(ts);
219 }
220
221 void
223 Ice::Long ts,
224 bool,
225 const Ice::Current&)
226 {
227 ARMARX_DEBUG << "Got an update for joint accels";
228 std::lock_guard l(updateMutex);
229 update.jointAccelerations = m;
230 updateTimestamps(ts);
231 }
232
233 void
235 Ice::Long ts,
236 bool,
237 const Ice::Current&)
238 {
239 ARMARX_DEBUG << "Got an update for joint currents";
240 std::lock_guard l(updateMutex);
241 update.jointCurrents = m;
242 updateTimestamps(ts);
243 }
244
245 void
247 Ice::Long,
248 bool,
249 const Ice::Current&)
250 {
251 }
252
253 void
255 Ice::Long,
256 bool,
257 const Ice::Current&)
258 {
259 }
260
261 // void LegacyRobotStateMemoryAdapter::reportPlatformPose(const PlatformPose& p, const Ice::Current &)
262 // {
263 // ARMARX_DEBUG << "Got an update for platform pose";
264 // std::lock_guard l(updateMutex);
265 // update.platformPose = p;
266 // updateTimestamps(p.timestampInMicroSeconds);
267 // }
268
269 void
271 const ::armarx::TransformStamped& transformStamped,
272 const ::Ice::Current&)
273 {
274
275 ARMARX_DEBUG << "Got an update for platform pose";
276
277 const Eigen::Isometry3f global_T_robot(transformStamped.transform);
278 const float yaw = simox::math::mat3f_to_rpy(global_T_robot.linear()).z();
279
280 armarx::PlatformPose p;
281 p.x = global_T_robot.translation().x();
282 p.y = global_T_robot.translation().y();
283 p.rotationAroundZ = yaw;
284 p.timestampInMicroSeconds = transformStamped.header.timestampInMicroSeconds;
285
286 {
287 std::lock_guard l(updateMutex);
288 update.platformPose = p;
289 updateTimestamps(p.timestampInMicroSeconds);
290 }
291 }
292
293 void
295 Ice::Float f2,
296 Ice::Float f3,
297 const Ice::Current&)
298 {
299 ARMARX_DEBUG << "Got an update for platform vels";
300 auto now = IceUtil::Time::now().toMicroSeconds();
301
302 std::lock_guard l(updateMutex);
303 update.platformVelocity = {f1, f2, f3};
304 updateTimestamps(now);
305 }
306
307 void
309 Ice::Float f2,
310 Ice::Float f3,
311 const Ice::Current&)
312 {
313 ARMARX_DEBUG << "Got an update for platform odom pose";
314 auto now = IceUtil::Time::now().toMicroSeconds();
315
316 std::lock_guard l(updateMutex);
317 update.platformOdometryPose = {f1, f2, f3};
318 updateTimestamps(now);
319 }
320
321 void
322 LegacyRobotStateMemoryAdapter::commitArmar3RobotDescription()
323 {
324 ARMARX_IMPORTANT << "Commiting Armar3 to descriptions";
325 armem::arondto::RobotDescription desc;
326 desc.name = "Armar3";
327 desc.timestamp = armem::Time::Now();
328 desc.xml.package = "RobotAPI";
329 desc.xml.path = "robots/Armar3/ArmarIII.xml";
330
332 auto& entityUpdate = c.add();
333
334 entityUpdate.confidence = 1.0;
335 entityUpdate.entityID.memoryName = armarx::armem::robot_state::constants::memoryName;
336 entityUpdate.entityID.coreSegmentName =
338 entityUpdate.entityID.providerSegmentName = "Armar3";
339 entityUpdate.entityID.entityName = "Armar3";
340
341 entityUpdate.instancesData = {desc.toAron()};
342 entityUpdate.referencedTime = armem::Time::Now();
343 auto res = memoryWriter.commit(c);
344 if (!res.allSuccess())
345 {
346 ARMARX_WARNING << "Could not send data to memory." << res.allErrorMessages();
347 }
348 }
349
350 void
352 {
353 usingProxy(properties.memoryNameSystemName);
354
355 const int minFrequency = 1;
356 const int maxFrequency = 100;
357 properties.frequency = std::clamp(properties.frequency, minFrequency, maxFrequency);
358 const int fInMS = (1000 / properties.frequency);
359
360 // create running task and run method checkUpdateAndSendToMemory in a loop
362 this, &LegacyRobotStateMemoryAdapter::checkUpdateAndSendToMemory, fInMS);
363 }
364
365 void
367 {
369 getProxy<mns::MemoryNameSystemInterfacePrx>(properties.memoryNameSystemName), this);
370
371 // Wait for the memory to become available and add it as dependency.
372 ARMARX_IMPORTANT << "Waiting for memory '" << properties.robotStateMemoryName << "' ...";
373 propEntityID.memoryName = properties.robotStateMemoryName;
374 try
375 {
376 memoryWriter = mns.useWriter(properties.robotStateMemoryName);
377 }
378 catch (const armem::error::ArMemError& e)
379 {
380 ARMARX_ERROR << e.what();
381 return;
382 }
383
384 // update RobotDescription (since there will be no robot unit, we have to submit the description by ourselves)
385 commitArmar3RobotDescription();
386
387 runningTask->start();
388 }
389
390 void
392 {
393 runningTask->stop();
394 }
395
396 void
398 {
399 runningTask->stop();
400 }
401} // namespace armarx::armem
#define ARMARX_REGISTER_COMPONENT_EXECUTABLE(ComponentT, applicationName)
Definition Decoupled.h:29
constexpr T c
Default component property definition container.
Definition Component.h:70
std::string getConfigIdentifier()
Retrieve config identifier for this component as set in constructor.
Definition Component.cpp:90
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
bool usingProxy(const std::string &name, const std::string &endpoints="")
Registers a proxy for retrieval after initialization and adds it to the dependency list.
Ice::ObjectPrx getProxy(long timeoutMs=0, bool waitForScheduler=true) const
Returns the proxy of this object (optionally it waits for the proxy)
The periodic task executes one thread method repeatedly using the time period specified in the constr...
void reportJointAccelerations(const NameValueMap &, Ice::Long, bool, const Ice::Current &) override
void reportJointStatuses(const NameStatusMap &, Ice::Long, bool, const Ice::Current &) override
void reportGlobalRobotPose(const ::armarx::TransformStamped &, const ::Ice::Current &=::Ice::emptyCurrent) override
void reportJointCurrents(const NameValueMap &, Ice::Long, bool, const Ice::Current &) override
armarx::PropertyDefinitionsPtr createPropertyDefinitions() override
void reportControlModeChanged(const NameControlModeMap &, Ice::Long, bool, const Ice::Current &) override
void reportJointMotorTemperatures(const NameValueMap &, Ice::Long, bool, const Ice::Current &) override
void reportPlatformOdometryPose(Ice::Float, Ice::Float, Ice::Float, const Ice::Current &) override
void reportJointAngles(const NameValueMap &, Ice::Long, bool, const Ice::Current &) override
void reportJointVelocities(const NameValueMap &, Ice::Long, bool, const Ice::Current &) override
void reportPlatformVelocity(Ice::Float, Ice::Float, Ice::Float, const Ice::Current &) override
void reportJointTorques(const NameValueMap &, Ice::Long, bool, const Ice::Current &) override
The memory name system (MNS) client.
CommitResult commit(const Commit &commit) const
Writes a Commit to the memory.
Definition Writer.cpp:68
Base class for all exceptions thrown by the armem library.
Definition ArMemError.h:19
static DateTime Now()
Definition DateTime.cpp:51
static Duration MicroSeconds(std::int64_t microSeconds)
Constructs a duration in microseconds.
Definition Duration.cpp:24
#define ARMARX_INFO
The normal logging level.
Definition Logging.h:181
#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
#define ARMARX_WARNING
The logging level for unexpected behaviour, but not a serious problem.
Definition Logging.h:193
const std::string descriptionCoreSegment
Definition constants.h:28
armarx::core::time::DateTime Time
double v(double t, double v0, double a0, double j)
Definition CtrlUtil.h:39
std::string const OdometryFrame
Definition FramedPose.h:66
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:351
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.
A bundle of updates to be sent to the memory.
Definition Commit.h:90
MemoryID entityID
The entity's ID.
Definition Commit.h:28
Time referencedTime
Time when this entity update was created (e.g.
Definition Commit.h:37
std::vector< aron::data::DictPtr > instancesData
The entity data.
Definition Commit.h:31