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 <optional>
28 
33 
34 #include <RobotAPI/interface/core/RobotState.h>
35 #include <RobotAPI/interface/units/PlatformUnitInterface.h>
38 
39 #include "SharedRobotServants.h"
40 
41 #include <shared_mutex>
42 #include <mutex>
43 
44 
45 namespace armarx
46 {
47  /**
48  * \class RobotStatePropertyDefinition
49  * \brief
50  */
53  {
54  public:
56  };
57 
58 
59  /**
60  * \defgroup Component-RobotStateComponent RobotStateComponent
61  * \ingroup RobotAPI-Components
62  * \brief Maintains a robot representation based on VirtualRobot (see [Simox](https://git.h2t.iar.kit.edu/sw/simox/simox)).
63  *
64  * The robot can be loaded from a Simox robot XML file.
65  * Upon request, an Ice proxy to a shared instance of this internal robot
66  * can be acquired through a call to RobotStateComponent::getSynchronizedRobot().
67  * Additionally it is possible to retrieve a proxy for robot snapshot with
68  * RobotStateComponent::getRobotSnapshot().
69  * While the synchronized robot will constantly update its internal state
70  * the robot snapshot is a clone of the original robot and won't update its
71  * configuration over time. This is useful, if several components need
72  * to calculate on the same values.
73  * See \ref armarx::RemoteRobot "RemoteRobot" for more details and the usage of this component.
74  */
75 
76  /**
77  * @brief The RobotStateComponent class
78  * @ingroup Component-RobotStateComponent
79  */
81  virtual public Component,
82  virtual public RobotStateComponentInterface
83  {
84  public:
85 
86  std::string getDefaultName() const override;
87 
88 
89  // RobotStateComponentInterface interface
90 
91  /// \return SharedRobotInterface proxy to the internal synchronized robot.
92  SharedRobotInterfacePrx getSynchronizedRobot(const Ice::Current&) const override;
93 
94  /**
95  * Creates a snapshot of the robot state at this moment in time.
96  * \note You need to call destroy() or ref()/unref() to trigger deletion of this object.
97  * \return Clone of the internal synchronized robot fixed to the configuration from the time of calling this function.
98  */
99  // [[deprecated]]
100  SharedRobotInterfacePrx getRobotSnapshot(const std::string& deprecated, const Ice::Current&) override;
101 
102  SharedRobotInterfacePrx getRobotSnapshotAtTimestamp(double time, const Ice::Current& current) override;
103  NameValueMap getJointConfigAtTimestamp(double timestamp, const Ice::Current&) const override;
104  RobotStateConfig getRobotStateAtTimestamp(double timestamp, const Ice::Current&) const override;
105 
106  /// \return the robot xml filename as specified in the configuration
107  std::string getRobotFilename(const Ice::Current&) const override;
108 
109  /// \return All dependent packages, which might contain a robot file.
110  std::vector<std::string> getArmarXPackages(const Ice::Current&) const override;
111 
112  /// \return The name of this robot instance.
113  std::string getRobotName(const Ice::Current&) const override;
114 
115  std::string getRobotStateTopicName(const Ice::Current&) const override;
116 
117  /// \return The name of this robot instance.
118  std::string getRobotNodeSetName(const Ice::Current&) const override;
119 
120  float getScaling(const Ice::Current&) const override;
121 
122  RobotInfoNodePtr getRobotInfo(const Ice::Current&) const override;
123 
124  // GlobalRobotPoseLocalizationListener
125  void reportGlobalRobotPose(const TransformStamped& globalRobotPose, const Ice::Current& = Ice::Current()) override;
126 
127  // Own interface.
128  void setRobotStateObserver(RobotStateObserverPtr observer);
129 
130 
131  protected:
132 
133  // Component interface.
134 
135  /**
136  * Load and create a VirtualRobot::Robot instance from the RobotFileName
137  * property. Additionally listen on the KinematicUnit topic for the
138  * RobotNodeSet specified in the RobotNodeSetName property.
139  */
140  void onInitComponent() override;
141  /// Setup RobotStateObjectFactories needed for creating RemoteRobot instances.
142  void onConnectComponent() override;
143  void onDisconnectComponent() override;
144 
145  /// Calls unref() on RobotStateComponent::_synchronizedPrx.
146  ~RobotStateComponent() override;
147 
148  /// Create an instance of RobotStatePropertyDefinitions.
149  PropertyDefinitionsPtr createPropertyDefinitions() override;
150 
151 
152  // Inherited from KinematicUnitInterface
153 
154  /// Does nothing.
155  void reportControlModeChanged(const NameControlModeMap& jointModes, Ice::Long timestamp, bool aValueChanged, const Ice::Current& c = Ice::emptyCurrent) override;
156  /// Stores the reported joint angles in the joint history and publishes the new joint angles.
157  void reportJointAngles(const NameValueMap& jointAngles, Ice::Long timestamp, bool aValueChanged, const Ice::Current& c = Ice::emptyCurrent) override;
158  /// Sends the joint velocities to the robot state observer.
159  void reportJointVelocities(const NameValueMap& jointVelocities, Ice::Long timestamp, bool aValueChanged, const Ice::Current& c = Ice::emptyCurrent) override;
160  /// Does nothing.
161  void reportJointTorques(const NameValueMap& jointTorques, Ice::Long timestamp, bool aValueChanged, const Ice::Current& c = Ice::emptyCurrent) override;
162  /// Does nothing.
163  void reportJointAccelerations(const NameValueMap& jointAccelerations, Ice::Long timestamp, bool aValueChanged, const Ice::Current& c) override;
164  /// Does nothing.
165  void reportJointCurrents(const NameValueMap& jointCurrents, Ice::Long timestamp, bool aValueChanged, const Ice::Current& c = Ice::emptyCurrent) override;
166  /// Does nothing.
167  void reportJointMotorTemperatures(const NameValueMap& jointMotorTemperatures, Ice::Long timestamp, bool aValueChanged, const Ice::Current& c = Ice::emptyCurrent) override;
168  /// Does nothing.
169  void reportJointStatuses(const NameStatusMap& jointStatuses, Ice::Long timestamp, bool aValueChanged, const Ice::Current& c = Ice::emptyCurrent) override;
170 
171  void simulatorWasReset(const Ice::Current& = Ice::emptyCurrent) override;
172  private:
173 
174  void readRobotInfo(const std::string& robotFile);
175  RobotInfoNodePtr readRobotInfo(const RapidXmlReaderNode& infoNode);
176 
177  void insertPose(IceUtil::Time timestamp, const Eigen::Matrix4f& globalPose);
178 
179 
180  template <class ValueT>
181  struct Timestamped
182  {
183  IceUtil::Time timestamp;
184  ValueT value;
185  };
186 
187  /// Interpolate the robot state from histories and store it in `config`.
188  std::optional<RobotStateConfig> interpolate(IceUtil::Time time) const;
189  /// Interpolate the joint angles from history and store it in `jointAngles`.
190  std::optional<Timestamped<NameValueMap>> interpolateJoints(IceUtil::Time time) const;
191  /// Interpolate the robot pose from history and store it in `pose`.
192  std::optional<Timestamped<FramedPosePtr>> interpolatePose(IceUtil::Time time) const;
193 
194 
195  private:
196 
197  /// Local robot model.
198  VirtualRobot::RobotPtr _synchronized;
199  /// Local shared robot.
200  SharedRobotServantPtr _sharedRobotServant;
201  /// Ice proxy to `_sharedRobotServant`.
202  SharedRobotInterfacePrx _synchronizedPrx;
203 
204  RobotStateListenerInterfacePrx robotStateListenerPrx;
205  RobotStateObserverPtr robotStateObs;
206 
207  std::string robotStateTopicName;
208  std::string robotFile;
209  std::string relativeRobotFile;
210 
211  mutable std::shared_mutex jointHistoryMutex;
212  std::map<IceUtil::Time, NameValueMap> jointHistory;
213  size_t jointHistoryLength;
214 
215  mutable std::shared_mutex poseHistoryMutex;
216  std::map<IceUtil::Time, FramedPosePtr> poseHistory;
217  size_t poseHistoryLength;
218 
219  std::string robotNodeSetName;
220 
221  float robotModelScaling;
222 
223  RobotInfoNodePtr robotInfo;
224 
225  };
226 
227 }
Properties.h
armarx::PropertyDefinitionContainer::prefix
std::string prefix
Prefix of the properties such as namespace, domain, component name, etc.
Definition: PropertyDefinitionContainer.h:333
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:43
armarx::RobotStatePropertyDefinitions
Definition: RobotStateComponent.h:51
SharedRobotServants.h
RobotAPIObjectFactories.h
IceInternal::Handle< RobotStateObserver >
armarx::RobotStateComponent
The RobotStateComponent class.
Definition: RobotStateComponent.h:80
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:926
ARMARXCOMPONENT_IMPORT_EXPORT
#define ARMARXCOMPONENT_IMPORT_EXPORT
Definition: ImportExportComponent.h:38
armarx::VariantType::Long
const VariantTypeId Long
Definition: Variant.h:917
armarx::RapidXmlReaderNode
Definition: RapidXmlReader.h:68
armarx::channels::KinematicUnitObserver::jointVelocities
const KinematicUnitDatafieldCreator jointVelocities("jointVelocities")
armarx::RobotStatePropertyDefinitions::RobotStatePropertyDefinitions
RobotStatePropertyDefinitions(std::string prefix)
Definition: RobotStateComponent.cpp:65
armarx::armem::Time
armarx::core::time::DateTime Time
Definition: forward_declarations.h:13
Component.h
armarx::Component
Baseclass for all ArmarX ManagedIceObjects requiring properties.
Definition: Component.h:95
armarx::control::njoint_controller::platform::platform_follower_controller::NameValueMap
std::map< std::string, float > NameValueMap
Definition: PlatformFollowerController.h:91
armarx::channels::KinematicUnitObserver::jointAngles
const KinematicUnitDatafieldCreator jointAngles("jointAngles")
armarx::channels::KinematicUnitObserver::jointCurrents
const KinematicUnitDatafieldCreator jointCurrents("jointCurrents")
GfxTL::Matrix4f
MatrixXX< 4, 4, float > Matrix4f
Definition: MatrixXX.h:601
armarx::ComponentPropertyDefinitions
Default component property definition container.
Definition: Component.h:70
IceUtil::Handle< class PropertyDefinitionContainer >
IceInternal::ProxyHandle< ::IceProxy::armarx::SharedRobotInterface >
RapidXmlReader.h
armarx::channels::KinematicUnitObserver::jointTorques
const KinematicUnitDatafieldCreator jointTorques("jointTorques")
RobotStateObserver.h
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
VirtualRobot::RobotPtr
std::shared_ptr< class Robot > RobotPtr
Definition: Bus.h:18
ImportExportComponent.h