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{
34
37 {
40
41 std::string prefix = "mem.";
42 defs->optional(properties.frequency,
43 prefix + "updateFrequency",
44 "The frequency in Hz to check for updates and send them to the memory.");
45 defs->optional(properties.memoryNameSystemName,
46 prefix + "memoryNameSystemName",
47 "The name of the MemoryNameSystem.");
48 defs->optional(properties.robotStateMemoryName,
49 prefix + "memoryName",
50 "The name of the RobotStateMemory.");
51
52 prefix = "listener.";
53 defs->topic<KinematicUnitListener>("RealRobotState", prefix + "KinematicUnitName");
54 defs->topic<PlatformUnitListener>("Armar6PlatformUnit", prefix + "PlatformUnitName");
55 return defs;
56 }
57
58 std::string
60 {
61 return "LegacyRobotStateMemoryAdapter";
62 }
63
64 void
65 LegacyRobotStateMemoryAdapter::checkUpdateAndSendToMemory()
66 {
67 std::lock_guard l(updateMutex);
68 if (updateChanged)
69 {
70 // If the update is not changed it has probably been committed already.
72 << "Try to send data to robotStateMemory but data has not changed.";
73 return;
74 }
75
76 // convert the update into a commit and send to memory
77 arondto::Proprioception prop;
78 for (const auto& [k, v] : update.jointAngles)
79 {
80 prop.joints.position[k] = v;
81 }
82 for (const auto& [k, v] : update.jointAccelerations)
83 {
84 prop.joints.acceleration[k] = v;
85 }
86 for (const auto& [k, v] : update.jointCurrents)
87 {
88 prop.joints.motorCurrent[k] = v;
89 }
90 for (const auto& [k, v] : update.jointTorques)
91 {
92 prop.joints.torque[k] = v;
93 }
94 for (const auto& [k, v] : update.jointVelocities)
95 {
96 prop.joints.velocity[k] = v;
97 }
98
99 // is this corect??
100 prop.platform.acceleration = Eigen::Vector3f::Zero();
101 prop.platform.relativePosition =
102 Eigen::Vector3f(update.platformPose.x, // this should be globasl AFAIK
103 update.platformPose.y,
104 update.platformPose.rotationAroundZ);
105 prop.platform.velocity = Eigen::Vector3f(std::get<0>(update.platformVelocity),
106 std::get<1>(update.platformVelocity),
107 std::get<2>(update.platformVelocity));
108
109 armem::EntityUpdate entityUpdate;
110 entityUpdate.entityID = propEntityID;
112 _timestampUpdateFirstModifiedInUs)); // we take the oldest timestamp
113
114 entityUpdate.instancesData = {prop.toAron()};
115
116 ARMARX_DEBUG << "Committing " << entityUpdate;
117 armem::EntityUpdateResult updateResult = memoryWriter.commit(entityUpdate);
118 ARMARX_DEBUG << updateResult;
119 if (!updateResult.success)
120 {
121 ARMARX_ERROR << updateResult.errorMessage;
122 }
123
124 // store odometry pose in localization segment
125 {
126 armem::arondto::Transform transform;
127 transform.header.agent = "Armar3";
128 transform.header.parentFrame = OdometryFrame;
130 transform.header.timestamp =
131 armem::Time(armem::Duration::MicroSeconds(_timestampUpdateFirstModifiedInUs));
132
133 Eigen::Isometry3f tf = Eigen::Isometry3f::Identity();
134 tf.translation().x() = (update.platformPose.x);
135 tf.translation().y() = (update.platformPose.y);
136 tf.linear() =
137 Eigen::AngleAxisf((update.platformPose.rotationAroundZ), Eigen::Vector3f::UnitZ())
138 .toRotationMatrix();
139
140 transform.transform = tf.matrix();
141
142 armem::EntityUpdate locUpdate;
143 locUpdate.entityID = locEntityID;
144 locUpdate.referencedTime =
145 Time(Duration::MicroSeconds(_timestampUpdateFirstModifiedInUs));
146 locUpdate.instancesData = {transform.toAron()};
147
148 ARMARX_DEBUG << "Committing " << entityUpdate;
149 armem::EntityUpdateResult updateResult = memoryWriter.commit(locUpdate);
150 ARMARX_DEBUG << updateResult;
151 if (!updateResult.success)
152 {
153 ARMARX_ERROR << updateResult.errorMessage;
154 }
155 }
156
157
158 // reset update
159 updateChanged = true;
160 }
161
162 void
163 LegacyRobotStateMemoryAdapter::updateTimestamps(long ts)
164 {
165 if (updateChanged)
166 {
167 _timestampUpdateFirstModifiedInUs = IceUtil::Time::now().toMicroSeconds();
168 }
169 updateChanged = false;
170 }
171
172 void
174 Ice::Long,
175 bool,
176 const Ice::Current&)
177 {
178 }
179
180 void
182 Ice::Long ts,
183 bool,
184 const Ice::Current&)
185 {
186 ARMARX_DEBUG << "Got an update for joint angles";
187 std::lock_guard l(updateMutex);
188 update.jointAngles = m;
189 updateTimestamps(ts);
190 }
191
192 void
194 Ice::Long ts,
195 bool,
196 const Ice::Current&)
197 {
198 ARMARX_DEBUG << "Got an update for joint vels";
199 std::lock_guard l(updateMutex);
200 update.jointVelocities = m;
201 updateTimestamps(ts);
202 }
203
204 void
206 Ice::Long ts,
207 bool,
208 const Ice::Current&)
209 {
210 ARMARX_DEBUG << "Got an update for joint torques";
211 std::lock_guard l(updateMutex);
212 update.jointTorques = m;
213 updateTimestamps(ts);
214 }
215
216 void
218 Ice::Long ts,
219 bool,
220 const Ice::Current&)
221 {
222 ARMARX_DEBUG << "Got an update for joint accels";
223 std::lock_guard l(updateMutex);
224 update.jointAccelerations = m;
225 updateTimestamps(ts);
226 }
227
228 void
230 Ice::Long ts,
231 bool,
232 const Ice::Current&)
233 {
234 ARMARX_DEBUG << "Got an update for joint currents";
235 std::lock_guard l(updateMutex);
236 update.jointCurrents = m;
237 updateTimestamps(ts);
238 }
239
240 void
242 Ice::Long,
243 bool,
244 const Ice::Current&)
245 {
246 }
247
248 void
250 Ice::Long,
251 bool,
252 const Ice::Current&)
253 {
254 }
255
256 // void LegacyRobotStateMemoryAdapter::reportPlatformPose(const PlatformPose& p, const Ice::Current &)
257 // {
258 // ARMARX_DEBUG << "Got an update for platform pose";
259 // std::lock_guard l(updateMutex);
260 // update.platformPose = p;
261 // updateTimestamps(p.timestampInMicroSeconds);
262 // }
263
264 void
266 const ::armarx::TransformStamped& transformStamped,
267 const ::Ice::Current&)
268 {
269
270 ARMARX_DEBUG << "Got an update for platform pose";
271
272 const Eigen::Isometry3f global_T_robot(transformStamped.transform);
273 const float yaw = simox::math::mat3f_to_rpy(global_T_robot.linear()).z();
274
275 armarx::PlatformPose p;
276 p.x = global_T_robot.translation().x();
277 p.y = global_T_robot.translation().y();
278 p.rotationAroundZ = yaw;
279 p.timestampInMicroSeconds = transformStamped.header.timestampInMicroSeconds;
280
281 {
282 std::lock_guard l(updateMutex);
283 update.platformPose = p;
284 updateTimestamps(p.timestampInMicroSeconds);
285 }
286 }
287
288 void
290 Ice::Float f2,
291 Ice::Float f3,
292 const Ice::Current&)
293 {
294 ARMARX_DEBUG << "Got an update for platform vels";
295 auto now = IceUtil::Time::now().toMicroSeconds();
296
297 std::lock_guard l(updateMutex);
298 update.platformVelocity = {f1, f2, f3};
299 updateTimestamps(now);
300 }
301
302 void
304 Ice::Float f2,
305 Ice::Float f3,
306 const Ice::Current&)
307 {
308 ARMARX_DEBUG << "Got an update for platform odom pose";
309 auto now = IceUtil::Time::now().toMicroSeconds();
310
311 std::lock_guard l(updateMutex);
312 update.platformOdometryPose = {f1, f2, f3};
313 updateTimestamps(now);
314 }
315
316 void
317 LegacyRobotStateMemoryAdapter::commitArmar3RobotDescription()
318 {
319 ARMARX_IMPORTANT << "Commiting Armar3 to descriptions";
320 armem::arondto::RobotDescription desc;
321 desc.name = "Armar3";
322 desc.timestamp = armem::Time::Now();
323 desc.xml.package = "RobotAPI";
324 desc.xml.path = "robots/Armar3/ArmarIII.xml";
325
327 auto& entityUpdate = c.add();
328
329 entityUpdate.confidence = 1.0;
330 entityUpdate.entityID.memoryName = armarx::armem::robot_state::constants::memoryName;
331 entityUpdate.entityID.coreSegmentName =
333 entityUpdate.entityID.providerSegmentName = "Armar3";
334 entityUpdate.entityID.entityName = "Armar3";
335
336 entityUpdate.instancesData = {desc.toAron()};
337 entityUpdate.referencedTime = armem::Time::Now();
338 auto res = memoryWriter.commit(c);
339 if (!res.allSuccess())
340 {
341 ARMARX_WARNING << "Could not send data to memory." << res.allErrorMessages();
342 }
343 }
344
345 void
347 {
348 usingProxy(properties.memoryNameSystemName);
349
350 const int minFrequency = 1;
351 const int maxFrequency = 100;
352 properties.frequency = std::clamp(properties.frequency, minFrequency, maxFrequency);
353 const int fInMS = (1000 / properties.frequency);
354
355 // create running task and run method checkUpdateAndSendToMemory in a loop
357 this, &LegacyRobotStateMemoryAdapter::checkUpdateAndSendToMemory, fInMS);
358 }
359
360 void
362 {
364 getProxy<mns::MemoryNameSystemInterfacePrx>(properties.memoryNameSystemName), this);
365
366 // Wait for the memory to become available and add it as dependency.
367 ARMARX_IMPORTANT << "Waiting for memory '" << properties.robotStateMemoryName << "' ...";
368 propEntityID.memoryName = properties.robotStateMemoryName;
369 try
370 {
371 memoryWriter = mns.useWriter(properties.robotStateMemoryName);
372 }
373 catch (const armem::error::ArMemError& e)
374 {
375 ARMARX_ERROR << e.what();
376 return;
377 }
378
379 // update RobotDescription (since there will be no robot unit, we have to submit the description by ourselves)
380 commitArmar3RobotDescription();
381
382 runningTask->start();
383 }
384
385 void
387 {
388 runningTask->stop();
389 }
390
391 void
393 {
394 runningTask->stop();
395 }
396} // namespace armarx::armem
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