RobotStateComponent.h
Go to the documentation of this file.
1/*
2 * This file is part of ArmarX.
3 *
4 * Copyright (C) 2012-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5 *
6 * ArmarX is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * ArmarX is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * @package RobotStateComponent::
19 * @author ( stefan dot ulbrich at kit dot edu)
20 * @date
21 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22 * GNU General Public License
23 */
24
25#pragma once
26
27#include <mutex>
28#include <optional>
29#include <shared_mutex>
30
35
36#include <RobotAPI/interface/core/RobotState.h>
37#include <RobotAPI/interface/units/PlatformUnitInterface.h>
40
41#include "SharedRobotServants.h"
42
43namespace armarx
44{
45 /**
46 * \class RobotStatePropertyDefinition
47 * \brief
48 */
54
55 /**
56 * \defgroup Component-RobotStateComponent RobotStateComponent
57 * \ingroup RobotAPI-Components
58 * \brief Maintains a robot representation based on VirtualRobot (see [Simox](https://git.h2t.iar.kit.edu/sw/simox/simox)).
59 *
60 * The robot can be loaded from a Simox robot XML file.
61 * Upon request, an Ice proxy to a shared instance of this internal robot
62 * can be acquired through a call to RobotStateComponent::getSynchronizedRobot().
63 * Additionally it is possible to retrieve a proxy for robot snapshot with
64 * RobotStateComponent::getRobotSnapshot().
65 * While the synchronized robot will constantly update its internal state
66 * the robot snapshot is a clone of the original robot and won't update its
67 * configuration over time. This is useful, if several components need
68 * to calculate on the same values.
69 * See \ref armarx::RemoteRobot "RemoteRobot" for more details and the usage of this component.
70 */
71
72 /**
73 * @brief The RobotStateComponent class
74 * @ingroup Component-RobotStateComponent
75 */
77 virtual public Component,
78 virtual public RobotStateComponentInterface
79 {
80 public:
81 std::string getDefaultName() const override;
82
83
84 // RobotStateComponentInterface interface
85
86 /// \return SharedRobotInterface proxy to the internal synchronized robot.
87 SharedRobotInterfacePrx getSynchronizedRobot(const Ice::Current&) const override;
88
89 /**
90 * Creates a snapshot of the robot state at this moment in time.
91 * \note You need to call destroy() or ref()/unref() to trigger deletion of this object.
92 * \return Clone of the internal synchronized robot fixed to the configuration from the time of calling this function.
93 */
94 // [[deprecated]]
95 SharedRobotInterfacePrx getRobotSnapshot(const std::string& deprecated,
96 const Ice::Current&) override;
97
99 const Ice::Current& current) override;
100 NameValueMap getJointConfigAtTimestamp(double timestamp,
101 const Ice::Current&) const override;
102 RobotStateConfig getRobotStateAtTimestamp(double timestamp,
103 const Ice::Current&) const override;
104
105 /// \return the robot xml filename as specified in the configuration
106 std::string getRobotFilename(const Ice::Current&) const override;
107
108 /// \return All dependent packages, which might contain a robot file.
109 std::vector<std::string> getArmarXPackages(const Ice::Current&) const override;
110
111 /// \return The name of this robot instance.
112 std::string getRobotName(const Ice::Current&) const override;
113
114 std::string getRobotStateTopicName(const Ice::Current&) const override;
115
116 /// \return The name of this robot instance.
117 std::string getRobotNodeSetName(const Ice::Current&) const override;
118
119 float getScaling(const Ice::Current&) const override;
120
121 RobotInfoNodePtr getRobotInfo(const Ice::Current&) const override;
122
123 // GlobalRobotPoseLocalizationListener
124 void reportGlobalRobotPose(const TransformStamped& globalRobotPose,
125 const Ice::Current& = Ice::Current()) override;
126
127 // Own interface.
129
130
131 protected:
132 // Component interface.
133
134 /**
135 * Load and create a VirtualRobot::Robot instance from the RobotFileName
136 * property. Additionally listen on the KinematicUnit topic for the
137 * RobotNodeSet specified in the RobotNodeSetName property.
138 */
139 void onInitComponent() override;
140 /// Setup RobotStateObjectFactories needed for creating RemoteRobot instances.
141 void onConnectComponent() override;
142 void onDisconnectComponent() override;
143
144 /// Calls unref() on RobotStateComponent::_synchronizedPrx.
145 ~RobotStateComponent() override;
146
147 /// Create an instance of RobotStatePropertyDefinitions.
149
150
151 // Inherited from KinematicUnitInterface
152
153 /// Does nothing.
154 void reportControlModeChanged(const NameControlModeMap& jointModes,
155 Ice::Long timestamp,
156 bool aValueChanged,
157 const Ice::Current& c = Ice::emptyCurrent) override;
158 /// Stores the reported joint angles in the joint history and publishes the new joint angles.
159 void reportJointAngles(const NameValueMap& jointAngles,
160 Ice::Long timestamp,
161 bool aValueChanged,
162 const Ice::Current& c = Ice::emptyCurrent) override;
163 /// Sends the joint velocities to the robot state observer.
164 void reportJointVelocities(const NameValueMap& jointVelocities,
165 Ice::Long timestamp,
166 bool aValueChanged,
167 const Ice::Current& c = Ice::emptyCurrent) override;
168 /// Does nothing.
169 void reportJointTorques(const NameValueMap& jointTorques,
170 Ice::Long timestamp,
171 bool aValueChanged,
172 const Ice::Current& c = Ice::emptyCurrent) override;
173 /// Does nothing.
174 void reportJointAccelerations(const NameValueMap& jointAccelerations,
175 Ice::Long timestamp,
176 bool aValueChanged,
177 const Ice::Current& c) override;
178 /// Does nothing.
179 void reportJointCurrents(const NameValueMap& jointCurrents,
180 Ice::Long timestamp,
181 bool aValueChanged,
182 const Ice::Current& c = Ice::emptyCurrent) override;
183 /// Does nothing.
184 void reportJointMotorTemperatures(const NameValueMap& jointMotorTemperatures,
185 Ice::Long timestamp,
186 bool aValueChanged,
187 const Ice::Current& c = Ice::emptyCurrent) override;
188 /// Does nothing.
189 void reportJointStatuses(const NameStatusMap& jointStatuses,
190 Ice::Long timestamp,
191 bool aValueChanged,
192 const Ice::Current& c = Ice::emptyCurrent) override;
193
194 void simulatorWasReset(const Ice::Current& = Ice::emptyCurrent) override;
195
196 private:
197 void readRobotInfo(const std::string& robotFile);
198 RobotInfoNodePtr readRobotInfo(const RapidXmlReaderNode& infoNode);
199
200 void insertPose(IceUtil::Time timestamp, const Eigen::Matrix4f& globalPose);
201
202 template <class ValueT>
203 struct Timestamped
204 {
205 IceUtil::Time timestamp;
206 ValueT value;
207 };
208
209 /// Interpolate the robot state from histories and store it in `config`.
210 std::optional<RobotStateConfig> interpolate(IceUtil::Time time) const;
211 /// Interpolate the joint angles from history and store it in `jointAngles`.
212 std::optional<Timestamped<NameValueMap>> interpolateJoints(IceUtil::Time time) const;
213 /// Interpolate the robot pose from history and store it in `pose`.
214 std::optional<Timestamped<FramedPosePtr>> interpolatePose(IceUtil::Time time) const;
215
216
217 private:
218 /// Local robot model.
219 VirtualRobot::RobotPtr _synchronized;
220 /// Local shared robot.
221 SharedRobotServantPtr _sharedRobotServant;
222 /// Ice proxy to `_sharedRobotServant`.
223 SharedRobotInterfacePrx _synchronizedPrx;
224
225 RobotStateListenerInterfacePrx robotStateListenerPrx;
226 RobotStateObserverPtr robotStateObs;
227
228 std::string robotStateTopicName;
229 std::string robotFile;
230 std::string relativeRobotFile;
231
232 mutable std::shared_mutex jointHistoryMutex;
233 std::map<IceUtil::Time, NameValueMap> jointHistory;
234 size_t jointHistoryLength;
235
236 mutable std::shared_mutex poseHistoryMutex;
237 std::map<IceUtil::Time, FramedPosePtr> poseHistory;
238 size_t poseHistoryLength;
239
240 std::string robotNodeSetName;
241
242 float robotModelScaling;
243
244 RobotInfoNodePtr robotInfo;
245 };
246
247} // namespace armarx
std::string timestamp()
#define ARMARXCOMPONENT_IMPORT_EXPORT
constexpr T c
ComponentPropertyDefinitions(std::string prefix, bool hasObjectNameParameter=true)
Definition Component.cpp:46
Component()
Protected default constructor. Used for virtual inheritance. Use createManagedIceObject() instead.
Definition Component.cpp:66
std::string prefix
Prefix of the properties such as namespace, domain, component name, etc.
The RobotStateComponent class.
void onInitComponent() override
Load and create a VirtualRobot::Robot instance from the RobotFileName property.
void reportJointMotorTemperatures(const NameValueMap &jointMotorTemperatures, Ice::Long timestamp, bool aValueChanged, const Ice::Current &c=Ice::emptyCurrent) override
Does nothing.
void reportJointVelocities(const NameValueMap &jointVelocities, Ice::Long timestamp, bool aValueChanged, const Ice::Current &c=Ice::emptyCurrent) override
Sends the joint velocities to the robot state observer.
void reportJointAngles(const NameValueMap &jointAngles, Ice::Long timestamp, bool aValueChanged, const Ice::Current &c=Ice::emptyCurrent) override
Stores the reported joint angles in the joint history and publishes the new joint angles.
float getScaling(const Ice::Current &) const override
void onDisconnectComponent() override
Hook for subclass.
void reportJointTorques(const NameValueMap &jointTorques, Ice::Long timestamp, bool aValueChanged, const Ice::Current &c=Ice::emptyCurrent) override
Does nothing.
std::vector< std::string > getArmarXPackages(const Ice::Current &) const override
void reportGlobalRobotPose(const TransformStamped &globalRobotPose, const Ice::Current &=Ice::Current()) override
std::string getRobotName(const Ice::Current &) const override
RobotStateConfig getRobotStateAtTimestamp(double timestamp, const Ice::Current &) const override
SharedRobotInterfacePrx getSynchronizedRobot(const Ice::Current &) const override
void reportControlModeChanged(const NameControlModeMap &jointModes, Ice::Long timestamp, bool aValueChanged, const Ice::Current &c=Ice::emptyCurrent) override
Does nothing.
void reportJointCurrents(const NameValueMap &jointCurrents, Ice::Long timestamp, bool aValueChanged, const Ice::Current &c=Ice::emptyCurrent) override
Does nothing.
std::string getRobotStateTopicName(const Ice::Current &) const override
void onConnectComponent() override
Setup RobotStateObjectFactories needed for creating RemoteRobot instances.
SharedRobotInterfacePrx getRobotSnapshot(const std::string &deprecated, const Ice::Current &) override
Creates a snapshot of the robot state at this moment in time.
void reportJointAccelerations(const NameValueMap &jointAccelerations, Ice::Long timestamp, bool aValueChanged, const Ice::Current &c) override
Does nothing.
void reportJointStatuses(const NameStatusMap &jointStatuses, Ice::Long timestamp, bool aValueChanged, const Ice::Current &c=Ice::emptyCurrent) override
Does nothing.
PropertyDefinitionsPtr createPropertyDefinitions() override
Create an instance of RobotStatePropertyDefinitions.
RobotInfoNodePtr getRobotInfo(const Ice::Current &) const override
std::string getRobotNodeSetName(const Ice::Current &) const override
void setRobotStateObserver(RobotStateObserverPtr observer)
std::string getRobotFilename(const Ice::Current &) const override
NameValueMap getJointConfigAtTimestamp(double timestamp, const Ice::Current &) const override
void simulatorWasReset(const Ice::Current &=Ice::emptyCurrent) override
std::string getDefaultName() const override
Retrieve default name of component.
SharedRobotInterfacePrx getRobotSnapshotAtTimestamp(double time, const Ice::Current &current) override
std::shared_ptr< class Robot > RobotPtr
Definition Bus.h:19
This file offers overloads of toIce() and fromIce() functions for STL container types.
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.
::IceInternal::ProxyHandle<::IceProxy::armarx::SharedRobotInterface > SharedRobotInterfacePrx
Definition FramedPose.h:59
IceInternal::Handle< SharedRobotServant > SharedRobotServantPtr
IceInternal::Handle< RobotStateObserver > RobotStateObserverPtr