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 #include <SimoxUtility/math/convert/mat3f_to_rpy.h>
25 
27 #include <RobotAPI/libraries/armem_robot_state/aron/Proprioception.aron.generated.h>
28 #include <RobotAPI/libraries/armem_robot_state/aron/Transform.aron.generated.h>
29 #include <RobotAPI/libraries/armem_robot_state/aron/RobotDescription.aron.generated.h>
30 
31 namespace armarx::armem
32 {
33 
35  {
37 
38  std::string prefix = "mem.";
39  defs->optional(properties.frequency, prefix + "updateFrequency", "The frequency in Hz to check for updates and send them to the memory.");
40  defs->optional(properties.memoryNameSystemName, prefix + "memoryNameSystemName", "The name of the MemoryNameSystem.");
41  defs->optional(properties.robotStateMemoryName, prefix + "memoryName", "The name of the RobotStateMemory.");
42 
43  prefix = "listener.";
44  defs->topic<KinematicUnitListener>("RealRobotState", prefix + "KinematicUnitName");
45  defs->topic<PlatformUnitListener>("Armar6PlatformUnit", prefix + "PlatformUnitName");
46  return defs;
47  }
48 
49 
51  {
52  return "LegacyRobotStateMemoryAdapter";
53  }
54 
55  void LegacyRobotStateMemoryAdapter::checkUpdateAndSendToMemory()
56  {
57  std::lock_guard l(updateMutex);
58  if (updateChanged)
59  {
60  // If the update is not changed it has probably been committed already.
61  ARMARX_INFO << deactivateSpam() << "Try to send data to robotStateMemory but data has not changed.";
62  return;
63  }
64 
65  // convert the update into a commit and send to memory
66  arondto::Proprioception prop;
67  for (const auto& [k, v] : update.jointAngles)
68  {
69  prop.joints.position[k] = v;
70  }
71  for (const auto& [k, v] : update.jointAccelerations)
72  {
73  prop.joints.acceleration[k] = v;
74  }
75  for (const auto& [k, v] : update.jointCurrents)
76  {
77  prop.joints.motorCurrent[k] = v;
78  }
79  for (const auto& [k, v] : update.jointTorques)
80  {
81  prop.joints.torque[k] = v;
82  }
83  for (const auto& [k, v] : update.jointVelocities)
84  {
85  prop.joints.velocity[k] = v;
86  }
87 
88  // is this corect??
89  prop.platform.acceleration = Eigen::Vector3f::Zero();
90  prop.platform.relativePosition = Eigen::Vector3f(update.platformPose.x, // this should be globasl AFAIK
91  update.platformPose.y,
92  update.platformPose.rotationAroundZ);
93  prop.platform.velocity = Eigen::Vector3f(std::get<0>(update.platformVelocity),
94  std::get<1>(update.platformVelocity),
95  std::get<2>(update.platformVelocity));
96 
97  armem::EntityUpdate entityUpdate;
98  entityUpdate.entityID = propEntityID;
99  entityUpdate.referencedTime = Time(Duration::MicroSeconds(_timestampUpdateFirstModifiedInUs)); // we take the oldest timestamp
100 
101  entityUpdate.instancesData =
102  {
103  prop.toAron()
104  };
105 
106  ARMARX_DEBUG << "Committing " << entityUpdate;
107  armem::EntityUpdateResult updateResult = memoryWriter.commit(entityUpdate);
108  ARMARX_DEBUG << updateResult;
109  if (!updateResult.success)
110  {
111  ARMARX_ERROR << updateResult.errorMessage;
112  }
113 
114  // store odometry pose in localization segment
115  {
116  armem::arondto::Transform transform;
117  transform.header.agent = "Armar3";
118  transform.header.parentFrame = OdometryFrame;
120  transform.header.timestamp = armem::Time(armem::Duration::MicroSeconds(_timestampUpdateFirstModifiedInUs));
121 
122  Eigen::Isometry3f tf = Eigen::Isometry3f::Identity();
123  tf.translation().x() = (update.platformPose.x);
124  tf.translation().y() = (update.platformPose.y);
125  tf.linear() = Eigen::AngleAxisf((update.platformPose.rotationAroundZ), Eigen::Vector3f::UnitZ()).toRotationMatrix();
126 
127  transform.transform = tf.matrix();
128 
129  armem::EntityUpdate locUpdate;
130  locUpdate.entityID = locEntityID;
131  locUpdate.referencedTime = Time(Duration::MicroSeconds(_timestampUpdateFirstModifiedInUs));
132  locUpdate.instancesData =
133  {
134  transform.toAron()
135  };
136 
137  ARMARX_DEBUG << "Committing " << entityUpdate;
138  armem::EntityUpdateResult updateResult = memoryWriter.commit(locUpdate);
139  ARMARX_DEBUG << updateResult;
140  if (!updateResult.success)
141  {
142  ARMARX_ERROR << updateResult.errorMessage;
143  }
144  }
145 
146 
147  // reset update
148  updateChanged = true;
149  }
150 
151  void LegacyRobotStateMemoryAdapter::updateTimestamps(long ts)
152  {
153  if (updateChanged)
154  {
155  _timestampUpdateFirstModifiedInUs = IceUtil::Time::now().toMicroSeconds();
156  }
157  updateChanged = false;
158  }
159 
160  void LegacyRobotStateMemoryAdapter::reportControlModeChanged(const NameControlModeMap&, Ice::Long, bool, const Ice::Current&)
161  {
162 
163  }
164 
165  void LegacyRobotStateMemoryAdapter::reportJointAngles(const NameValueMap& m, Ice::Long ts, bool, const Ice::Current&)
166  {
167  ARMARX_DEBUG << "Got an update for joint angles";
168  std::lock_guard l(updateMutex);
169  update.jointAngles = m;
170  updateTimestamps(ts);
171  }
172 
174  {
175  ARMARX_DEBUG << "Got an update for joint vels";
176  std::lock_guard l(updateMutex);
177  update.jointVelocities = m;
178  updateTimestamps(ts);
179  }
180 
181  void LegacyRobotStateMemoryAdapter::reportJointTorques(const NameValueMap& m, Ice::Long ts, bool, const Ice::Current&)
182  {
183  ARMARX_DEBUG << "Got an update for joint torques";
184  std::lock_guard l(updateMutex);
185  update.jointTorques = m;
186  updateTimestamps(ts);
187  }
188 
190  {
191  ARMARX_DEBUG << "Got an update for joint accels";
192  std::lock_guard l(updateMutex);
193  update.jointAccelerations = m;
194  updateTimestamps(ts);
195  }
196 
197  void LegacyRobotStateMemoryAdapter::reportJointCurrents(const NameValueMap& m, Ice::Long ts, bool, const Ice::Current&)
198  {
199  ARMARX_DEBUG << "Got an update for joint currents";
200  std::lock_guard l(updateMutex);
201  update.jointCurrents = m;
202  updateTimestamps(ts);
203  }
204 
206  {
207 
208  }
209 
210  void LegacyRobotStateMemoryAdapter::reportJointStatuses(const NameStatusMap&, Ice::Long, bool, const Ice::Current&)
211  {
212 
213  }
214 
215  // void LegacyRobotStateMemoryAdapter::reportPlatformPose(const PlatformPose& p, const Ice::Current &)
216  // {
217  // ARMARX_DEBUG << "Got an update for platform pose";
218  // std::lock_guard l(updateMutex);
219  // update.platformPose = p;
220  // updateTimestamps(p.timestampInMicroSeconds);
221  // }
222 
223  void LegacyRobotStateMemoryAdapter::reportGlobalRobotPose(const ::armarx::TransformStamped& transformStamped, const ::Ice::Current&)
224  {
225 
226  ARMARX_DEBUG << "Got an update for platform pose";
227 
228  const Eigen::Isometry3f global_T_robot(transformStamped.transform);
229  const float yaw = simox::math::mat3f_to_rpy(global_T_robot.linear()).z();
230 
231  armarx::PlatformPose p;
232  p.x = global_T_robot.translation().x();
233  p.y = global_T_robot.translation().y();
234  p.rotationAroundZ = yaw;
235  p.timestampInMicroSeconds = transformStamped.header.timestampInMicroSeconds;
236 
237  {
238  std::lock_guard l(updateMutex);
239  update.platformPose = p;
240  updateTimestamps(p.timestampInMicroSeconds);
241  }
242  }
243 
244 
246  {
247  ARMARX_DEBUG << "Got an update for platform vels";
248  auto now = IceUtil::Time::now().toMicroSeconds();
249 
250  std::lock_guard l(updateMutex);
251  update.platformVelocity = {f1, f2, f3};
252  updateTimestamps(now);
253  }
255  {
256  ARMARX_DEBUG << "Got an update for platform odom pose";
257  auto now = IceUtil::Time::now().toMicroSeconds();
258 
259  std::lock_guard l(updateMutex);
260  update.platformOdometryPose = {f1, f2, f3};
261  updateTimestamps(now);
262  }
263 
264 
265 
266  void LegacyRobotStateMemoryAdapter::commitArmar3RobotDescription()
267  {
268  ARMARX_IMPORTANT << "Commiting Armar3 to descriptions";
269  armem::arondto::RobotDescription desc;
270  desc.name = "Armar3";
271  desc.timestamp = armem::Time::Now();
272  desc.xml.package = "RobotAPI";
273  desc.xml.path = "robots/Armar3/ArmarIII.xml";
274 
276  auto& entityUpdate = c.add();
277 
278  entityUpdate.confidence = 1.0;
279  entityUpdate.entityID.memoryName = armarx::armem::robot_state::constants::memoryName;
280  entityUpdate.entityID.coreSegmentName = armarx::armem::robot_state::constants::descriptionCoreSegment;
281  entityUpdate.entityID.providerSegmentName = "Armar3";
282  entityUpdate.entityID.entityName = "Armar3";
283 
284  entityUpdate.instancesData = { desc.toAron() };
285  entityUpdate.referencedTime = armem::Time::Now();
286  auto res = memoryWriter.commit(c);
287  if (!res.allSuccess())
288  {
289  ARMARX_WARNING << "Could not send data to memory." << res.allErrorMessages();
290  }
291  }
292 
293 
295  {
296  usingProxy(properties.memoryNameSystemName);
297 
298  const int minFrequency = 1;
299  const int maxFrequency = 100;
300  properties.frequency = std::clamp(properties.frequency, minFrequency, maxFrequency);
301  const int fInMS = (1000 / properties.frequency);
302 
303  // create running task and run method checkUpdateAndSendToMemory in a loop
304  runningTask = new PeriodicTask<LegacyRobotStateMemoryAdapter>(this, &LegacyRobotStateMemoryAdapter::checkUpdateAndSendToMemory, fInMS);
305  }
306 
307 
309  {
310  auto mns = client::MemoryNameSystem(getProxy<mns::MemoryNameSystemInterfacePrx>(properties.memoryNameSystemName), this);
311 
312  // Wait for the memory to become available and add it as dependency.
313  ARMARX_IMPORTANT << "Waiting for memory '" << properties.robotStateMemoryName << "' ...";
314  propEntityID.memoryName = properties.robotStateMemoryName;
315  try
316  {
317  memoryWriter = mns.useWriter(properties.robotStateMemoryName);
318  }
319  catch (const armem::error::ArMemError& e)
320  {
321  ARMARX_ERROR << e.what();
322  return;
323  }
324 
325  // update RobotDescription (since there will be no robot unit, we have to submit the description by ourselves)
326  commitArmar3RobotDescription();
327 
328  runningTask->start();
329  }
330 
331 
333  {
334  runningTask->stop();
335  }
336 
337 
339  {
340  runningTask->stop();
341  }
342 }
armarx::armem::LegacyRobotStateMemoryAdapter::onExitComponent
void onExitComponent() override
Definition: LegacyRobotStateMemoryAdapter.cpp:338
armarx::VariantType::Float
const VariantTypeId Float
Definition: Variant.h:918
armarx::armem::LegacyRobotStateMemoryAdapter::reportPlatformVelocity
void reportPlatformVelocity(Ice::Float, Ice::Float, Ice::Float, const Ice::Current &) override
Definition: LegacyRobotStateMemoryAdapter.cpp:245
LegacyRobotStateMemoryAdapter.h
ARMARX_IMPORTANT
#define ARMARX_IMPORTANT
Definition: Logging.h:183
armarx::armem::EntityUpdate::instancesData
std::vector< aron::data::DictPtr > instancesData
The entity data.
Definition: Commit.h:33
armarx::armem::Commit
A bundle of updates to be sent to the memory.
Definition: Commit.h:89
armarx::core::time::DateTime::Now
static DateTime Now()
Definition: DateTime.cpp:55
armarx::armem::EntityUpdateResult
Result of an EntityUpdate.
Definition: Commit.h:72
armarx::armem::robot_state::constants::memoryName
const std::string memoryName
Definition: constants.h:26
armarx::armem
Definition: LegacyRobotStateMemoryAdapter.cpp:31
armarx::armem::LegacyRobotStateMemoryAdapter::getDefaultName
std::string getDefaultName() const override
Definition: LegacyRobotStateMemoryAdapter.cpp:50
armarx::armem::error::ArMemError
Base class for all exceptions thrown by the armem library.
Definition: ArMemError.h:18
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:43
armarx::armem::LegacyRobotStateMemoryAdapter::reportJointAngles
void reportJointAngles(const NameValueMap &, Ice::Long, bool, const Ice::Current &) override
Definition: LegacyRobotStateMemoryAdapter.cpp:165
armarx::armem::robot_state::constants::robotRootNodeName
const std::string robotRootNodeName
Definition: constants.h:36
clamp
double clamp(double x, double a, double b)
Definition: point.hpp:125
armarx::armem::LegacyRobotStateMemoryAdapter::reportJointTorques
void reportJointTorques(const NameValueMap &, Ice::Long, bool, const Ice::Current &) override
Definition: LegacyRobotStateMemoryAdapter.cpp:181
armarx::armem::LegacyRobotStateMemoryAdapter::onDisconnectComponent
void onDisconnectComponent() override
Definition: LegacyRobotStateMemoryAdapter.cpp:332
armarx::armem::LegacyRobotStateMemoryAdapter::reportPlatformOdometryPose
void reportPlatformOdometryPose(Ice::Float, Ice::Float, Ice::Float, const Ice::Current &) override
Definition: LegacyRobotStateMemoryAdapter.cpp:254
GfxTL::Identity
void Identity(MatrixXX< N, N, T > *a)
Definition: MatrixXX.h:523
ARMARX_DEBUG
#define ARMARX_DEBUG
Definition: Logging.h:177
armarx::armem::LegacyRobotStateMemoryAdapter::reportGlobalRobotPose
void reportGlobalRobotPose(const ::armarx::TransformStamped &, const ::Ice::Current &=::Ice::emptyCurrent) override
Definition: LegacyRobotStateMemoryAdapter.cpp:223
armarx::armem::LegacyRobotStateMemoryAdapter::reportJointAccelerations
void reportJointAccelerations(const NameValueMap &, Ice::Long, bool, const Ice::Current &) override
Definition: LegacyRobotStateMemoryAdapter.cpp:189
armarx::armem::EntityUpdate
An update of an entity for a specific point in time.
Definition: Commit.h:27
armarx::armem::LegacyRobotStateMemoryAdapter::reportJointCurrents
void reportJointCurrents(const NameValueMap &, Ice::Long, bool, const Ice::Current &) override
Definition: LegacyRobotStateMemoryAdapter.cpp:197
armarx::VariantType::Long
const VariantTypeId Long
Definition: Variant.h:917
armarx::armem::client::Writer::commit
CommitResult commit(const Commit &commit) const
Writes a Commit to the memory.
Definition: Writer.cpp:59
armarx::armem::LegacyRobotStateMemoryAdapter::reportJointStatuses
void reportJointStatuses(const NameStatusMap &, Ice::Long, bool, const Ice::Current &) override
Definition: LegacyRobotStateMemoryAdapter.cpp:210
armarx::OdometryFrame
const std::string OdometryFrame
Definition: FramedPose.h:63
armarx::armem::LegacyRobotStateMemoryAdapter::reportJointMotorTemperatures
void reportJointMotorTemperatures(const NameValueMap &, Ice::Long, bool, const Ice::Current &) override
Definition: LegacyRobotStateMemoryAdapter.cpp:205
armarx::armem::robot_state::constants::descriptionCoreSegment
const std::string descriptionCoreSegment
Definition: constants.h:28
ARMARX_ERROR
#define ARMARX_ERROR
Definition: Logging.h:189
armarx::armem::Time
armarx::core::time::DateTime Time
Definition: forward_declarations.h:13
armarx::armem::MemoryID::memoryName
std::string memoryName
Definition: MemoryID.h:50
aron_conversions.h
armarx::control::njoint_controller::platform::platform_follower_controller::NameValueMap
std::map< std::string, float > NameValueMap
Definition: PlatformFollowerController.h:91
armarx::Component::getConfigIdentifier
std::string getConfigIdentifier()
Retrieve config identifier for this component as set in constructor.
Definition: Component.cpp:74
armarx::ctrlutil::v
double v(double t, double v0, double a0, double j)
Definition: CtrlUtil.h:39
armarx::armem::EntityUpdate::referencedTime
Time referencedTime
Time when this entity update was created (e.g.
Definition: Commit.h:39
armarx::armem::LegacyRobotStateMemoryAdapter::createPropertyDefinitions
armarx::PropertyDefinitionsPtr createPropertyDefinitions() override
Definition: LegacyRobotStateMemoryAdapter.cpp:34
armarx::ComponentPropertyDefinitions
Default component property definition container.
Definition: Component.h:70
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:174
armarx::armem::LegacyRobotStateMemoryAdapter::onConnectComponent
void onConnectComponent() override
Definition: LegacyRobotStateMemoryAdapter.cpp:308
IceUtil::Handle< class PropertyDefinitionContainer >
armarx::transform
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:315
armarx::armem::client::MemoryNameSystem
The memory name system (MNS) client.
Definition: MemoryNameSystem.h:69
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:92
armarx::armem::LegacyRobotStateMemoryAdapter::onInitComponent
void onInitComponent() override
Definition: LegacyRobotStateMemoryAdapter.cpp:294
armarx::armem::LegacyRobotStateMemoryAdapter::reportJointVelocities
void reportJointVelocities(const NameValueMap &, Ice::Long, bool, const Ice::Current &) override
Definition: LegacyRobotStateMemoryAdapter.cpp:173
armarx::PeriodicTask
Definition: ArmarXManager.h:70
ARMARX_WARNING
#define ARMARX_WARNING
Definition: Logging.h:186
armarx::armem::LegacyRobotStateMemoryAdapter::reportControlModeChanged
void reportControlModeChanged(const NameControlModeMap &, Ice::Long, bool, const Ice::Current &) override
Definition: LegacyRobotStateMemoryAdapter.cpp:160
armarx::core::time::Duration::MicroSeconds
static Duration MicroSeconds(std::int64_t microSeconds)
Constructs a duration in microseconds.
Definition: Duration.cpp:27
armarx::armem::EntityUpdate::entityID
MemoryID entityID
The entity's ID.
Definition: Commit.h:30
armarx::ManagedIceObject::usingProxy
bool usingProxy(const std::string &name, const std::string &endpoints="")
Registers a proxy for retrieval after initialization and adds it to the dependency list.
Definition: ManagedIceObject.cpp:151