RobotPoseUnitDynamicSimulation.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * Copyright (C) 2013-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 ArmarXSimulation::units
19  * @author Nikolaus Vahrenkamp
20  * @date 2016
21  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22  * GNU General Public License
23  */
24 
25 
27 
29 
30 #include <Eigen/Geometry>
31 
32 #include <cmath>
33 
34 #include <VirtualRobot/MathTools.h>
35 
36 using namespace armarx;
37 
39 {
40  //linearVelocityMax = getProperty<float>("LinearVelocityMax").getValue();
41  //linearVelocityMin = getProperty<float>("LinearVelocityMin").getValue();
42  //linearAccelerationMax = getProperty<float>("LinearAccelerationMax").getValue();
43  //angularVelocityMax = getProperty<float>("AngularVelocityMax").getValue();
44  //angularAccelerationMax = getProperty<float>("AngularAccelerationMax").getValue();
45 
46  robotName = getProperty<std::string>("RobotName").getValue();
47  //robotRootNodeName = getProperty<std::string>("RobotRootNodeName").getValue();
48 
49  simulatorPrxName = getProperty<std::string>("SimulatorProxyName").getValue();
50 
51  positionalAccuracy = 0.02f; //in m
52  orientationalAccuracy = 0.1f; //in rad
53 
54  intervalMs = getProperty<int>("IntervalMs").getValue();
55  ARMARX_VERBOSE << "Starting RobotPose unit dynamic simulation with " << intervalMs << " ms interval";
57  simulationTask->setDelayWarningTolerance(100);
58 
59  if (!robotName.empty())
60  {
61  std::string robotTopicName = "Simulator_Robot_";
62  robotTopicName += robotName;
63  ARMARX_INFO << "Simulator Robot topic: " << robotTopicName;
64  usingTopic(robotTopicName);
65  }
66  else
67  {
68  ARMARX_WARNING << "No robot loaded...";
69  }
70 
72 
74  m.setIdentity();
75  currentPose = Pose(m);
76  targetPose = Pose(m);
77 }
78 
80 {
81  ARMARX_INFO << "Stopping RobotPose unit";
82  std::unique_lock lock(currentPoseMutex);
83  simulationTask->stop();
84  simulatorPrx = nullptr;
85 }
86 
88 {
89  RobotPoseControlMode = eUnknown;
90 
91  ARMARX_INFO << "Using simulator proxy: " << simulatorPrxName;
92  simulatorPrx = getProxy<SimulatorInterfacePrx>(simulatorPrxName);
93  PoseBasePtr p = simulatorPrx->getRobotPose(robotName);
94  setCurrentPose(p);
96 
97  simulationTask->start();
98 }
99 
100 
102 {
103  simulationTask->stop();
104 }
105 
106 void RobotPoseUnitDynamicSimulation::stopMovement(const Ice::Current& current)
107 {
109  PosePtr p(new Pose(m));
110 
111  moveRelative(p, 500, 500, current);
112 }
113 
114 
116 {
117  // todo: implement velocity control....
118 
119  {
120  std::unique_lock lock(currentPoseMutex);
121 
122  if (!simulatorPrx)
123  {
124  return;
125  }
126  }
127 }
128 
129 void RobotPoseUnitDynamicSimulation::moveTo(const PoseBasePtr& targetPose, Ice::Float positionalAccuracy, Ice::Float orientationalAccuracy, const Ice::Current& c)
130 {
131  std::unique_lock lock(currentPoseMutex);
132  RobotPoseControlMode = ePositionControl;
133  ARMARX_DEBUG << "RobotPose move to" << flush;
134  this->targetPose = *(PosePtr::dynamicCast(targetPose));
135  this->positionalAccuracy = positionalAccuracy * .001f;
136  this->orientationalAccuracy = orientationalAccuracy;
137  listenerPrx->begin_reportNewTargetPose(targetPose);
138  ARMARX_LOG << deactivateSpam(5) << "New Target: " << targetPose->position->x << " " << targetPose->position->y << " " << targetPose->position->z;
139  if (simulatorPrx)
140  {
141  //simulatorPrx->begin_setRobotPose(robotName, targetPose);
142  simulatorPrx->setRobotPose(robotName, targetPose);
143  }
144 }
145 
146 Eigen::Vector3f RobotPoseUnitDynamicSimulation::getRPY(PoseBasePtr pose)
147 {
148  Eigen::Quaternionf q(QuaternionPtr::dynamicCast(pose->orientation)->toEigen());
149  Eigen::Matrix4f m = VirtualRobot::MathTools::quat2eigen4f(q.x(), q.y(), q.z(), q.w());
150  Eigen::Vector3f rpy;
151  VirtualRobot::MathTools::eigen4f2rpy(m, rpy);
152  return rpy;
153 }
154 
155 void RobotPoseUnitDynamicSimulation::move(const PoseBasePtr& targetVelocity, const Ice::Current& c)
156 {
157  std::unique_lock lock(currentPoseMutex);
158  ARMARX_INFO << deactivateSpam(1) << "NYI";
159 }
160 
161 void RobotPoseUnitDynamicSimulation::moveRelative(const PoseBasePtr& relativeTarget, float positionalAccuracy, float orientationalAccuracy, const Ice::Current& c)
162 {
163  PosePtr p;
164  {
165  std::unique_lock lock(currentPoseMutex);
166  RobotPoseControlMode = ePositionControl;
167  Eigen::Matrix4f pose;
169  Eigen::Matrix4f rel = PosePtr::dynamicCast(relativeTarget)->toEigen();
170  pose = cur * rel;
171  p = PosePtr(new Pose(pose));
172  ARMARX_DEBUG << "RobotPose move relative" << flush;
173  }
175 }
176 
177 void RobotPoseUnitDynamicSimulation::setMaxVelocities(float linearVelocity, float angularVelocity, const Ice::Current& c)
178 {
179  //linearVelocityMax = linearVelocity;
180  //angularVelocityMax = angularVelocity;
181 }
182 
183 
185 {
186  ARMARX_DEBUG << "Update new RobotPose pose" << flush;
187 
188  if (!newPose)
189  {
190  return;
191  }
192 
193  currentPose = *PosePtr::dynamicCast(newPose);
194 }
195 
196 void RobotPoseUnitDynamicSimulation::reportState(SimulatedRobotState const& state, const Ice::Current&)
197 {
198  setCurrentPose(state.pose);
199 }
200 
201 void RobotPoseUnitDynamicSimulation::setCurrentPose(const PoseBasePtr& newPose)
202 {
203  std::unique_lock lock(currentPoseMutex);
204  updateCurrentPose(newPose);
205  try
206  {
207  PosePtr p(new Pose(currentPose));
208  listenerPrx->begin_reportRobotPose(p);
209  }
210  catch (...)
211  {
212  }
213 }
214 
216 {
217  return PropertyDefinitionsPtr(
219 }
220 
armarx::RobotPoseUnitDynamicSimulation::setCurrentPose
void setCurrentPose(const PoseBasePtr &newPose)
Definition: RobotPoseUnitDynamicSimulation.cpp:201
armarx::RobotPoseUnitDynamicSimulation::currentPose
Pose currentPose
Definition: RobotPoseUnitDynamicSimulation.h:127
ARMARX_VERBOSE
#define ARMARX_VERBOSE
Definition: Logging.h:180
armarx::VariantType::Float
const VariantTypeId Float
Definition: Variant.h:918
armarx::RobotPoseUnitDynamicSimulation::getRPY
Eigen::Vector3f getRPY(PoseBasePtr pose)
Definition: RobotPoseUnitDynamicSimulation.cpp:146
armarx::navigation::core::Pose
Eigen::Isometry3f Pose
Definition: basic_types.h:31
armarx::RobotPoseUnitDynamicSimulation::moveRelative
void moveRelative(const PoseBasePtr &relativeTarget, Ice::Float positionalAccuracy, Ice::Float orientationalAccuracy, const Ice::Current &c=Ice::emptyCurrent) override
Definition: RobotPoseUnitDynamicSimulation.cpp:161
armarx::PosePtr
IceInternal::Handle< Pose > PosePtr
Definition: Pose.h:306
armarx::RobotPoseUnitDynamicSimulationPropertyDefinitions
Moves the robot around. Currently only position control is implemented.
Definition: RobotPoseUnitDynamicSimulation.h:47
armarx::RobotPoseUnitDynamicSimulation::onStartRobotPoseUnit
void onStartRobotPoseUnit() override
Definition: RobotPoseUnitDynamicSimulation.cpp:87
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:43
armarx::RobotPoseUnitDynamicSimulation::setMaxVelocities
void setMaxVelocities(Ice::Float positionalVelocity, Ice::Float orientationalVelocity, const Ice::Current &c=Ice::emptyCurrent) override
Definition: RobotPoseUnitDynamicSimulation.cpp:177
armarx::RobotPoseUnitDynamicSimulation::simulatorPrxName
std::string simulatorPrxName
Definition: RobotPoseUnitDynamicSimulation.h:132
armarx::RobotPoseUnit::listenerPrx
RobotPoseUnitListenerPrx listenerPrx
RobotPoseUnitListener proxy for publishing state updates.
Definition: RobotPoseUnit.h:122
armarx::RobotPoseUnitDynamicSimulation::robotName
std::string robotName
Definition: RobotPoseUnitDynamicSimulation.h:133
armarx::RobotPoseUnitDynamicSimulation::simulationTask
PeriodicTask< RobotPoseUnitDynamicSimulation >::pointer_type simulationTask
Definition: RobotPoseUnitDynamicSimulation.h:138
IceInternal::Handle< Pose >
GfxTL::Identity
void Identity(MatrixXX< N, N, T > *a)
Definition: MatrixXX.h:523
armarx::RobotPoseUnitDynamicSimulation::simulationFunction
void simulationFunction()
Definition: RobotPoseUnitDynamicSimulation.cpp:115
armarx::RobotPoseUnitDynamicSimulation::RobotPoseControlMode
ControlMode RobotPoseControlMode
Definition: RobotPoseUnitDynamicSimulation.h:134
armarx::RobotPoseUnitDynamicSimulation::orientationalAccuracy
::Ice::Float orientationalAccuracy
Definition: RobotPoseUnitDynamicSimulation.h:130
armarx::flush
const LogSender::manipulator flush
Definition: LogSender.h:251
ARMARX_DEBUG
#define ARMARX_DEBUG
Definition: Logging.h:177
armarx::RobotPoseUnitDynamicSimulation::targetPose
Pose targetPose
Definition: RobotPoseUnitDynamicSimulation.h:128
armarx::Pose::toEigen
virtual Eigen::Matrix4f toEigen() const
Definition: Pose.cpp:334
Synchronization.h
armarx::RobotPoseUnitDynamicSimulation::createPropertyDefinitions
PropertyDefinitionsPtr createPropertyDefinitions() override
Definition: RobotPoseUnitDynamicSimulation.cpp:215
q
#define q
ARMARX_LOG
#define ARMARX_LOG
Definition: Logging.h:163
armarx::RobotPoseUnitDynamicSimulation::updateCurrentPose
void updateCurrentPose(const PoseBasePtr &newPose)
Definition: RobotPoseUnitDynamicSimulation.cpp:184
armarx::ManagedIceObject::usingTopic
void usingTopic(const std::string &name, bool orderedPublishing=false)
Registers a proxy for subscription after initialization.
Definition: ManagedIceObject.cpp:248
armarx::Component::getConfigIdentifier
std::string getConfigIdentifier()
Retrieve config identifier for this component as set in constructor.
Definition: Component.cpp:74
armarx::RobotPoseUnitDynamicSimulation::onExitRobotPoseUnit
void onExitRobotPoseUnit() override
Definition: RobotPoseUnitDynamicSimulation.cpp:101
armarx::RobotPoseUnitDynamicSimulation::onStopRobotPoseUnit
void onStopRobotPoseUnit() override
Definition: RobotPoseUnitDynamicSimulation.cpp:79
GfxTL::Matrix4f
MatrixXX< 4, 4, float > Matrix4f
Definition: MatrixXX.h:601
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:174
armarx::RobotPoseUnitDynamicSimulation::currentPoseMutex
std::mutex currentPoseMutex
Definition: RobotPoseUnitDynamicSimulation.h:122
RobotPoseUnitDynamicSimulation.h
armarx::Quaternion< float, 0 >
IceUtil::Handle< class PropertyDefinitionContainer >
armarx::RobotPoseUnitDynamicSimulation::onInitRobotPoseUnit
void onInitRobotPoseUnit() override
Definition: RobotPoseUnitDynamicSimulation.cpp:38
armarx::RobotPoseUnitDynamicSimulation::move
void move(const PoseBasePtr &targetVelocity, const Ice::Current &c=Ice::emptyCurrent) override
Definition: RobotPoseUnitDynamicSimulation.cpp:155
armarx::RobotPoseUnitDynamicSimulation::simulatorPrx
SimulatorInterfacePrx simulatorPrx
Definition: RobotPoseUnitDynamicSimulation.h:136
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::PeriodicTask
Definition: ArmarXManager.h:70
armarx::RobotPoseUnitDynamicSimulation::intervalMs
int intervalMs
Definition: RobotPoseUnitDynamicSimulation.h:124
ARMARX_WARNING
#define ARMARX_WARNING
Definition: Logging.h:186
armarx::PropertyDefinitionsPtr
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.
Definition: forward_declarations.h:34
armarx::RobotPoseUnitDynamicSimulation::positionalAccuracy
::Ice::Float positionalAccuracy
Definition: RobotPoseUnitDynamicSimulation.h:129
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
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::RobotPoseUnitDynamicSimulation::reportState
void reportState(SimulatedRobotState const &state, const Ice::Current &) override
Definition: RobotPoseUnitDynamicSimulation.cpp:196
armarx::RobotPoseUnitDynamicSimulation::stopMovement
void stopMovement(const Ice::Current &c=Ice::emptyCurrent) override
Definition: RobotPoseUnitDynamicSimulation.cpp:106
armarx::RobotPoseUnitDynamicSimulation::moveTo
void moveTo(const PoseBasePtr &targetPose, Ice::Float positionalAccuracy, Ice::Float orientationalAccuracy, const Ice::Current &c=Ice::emptyCurrent) override
Definition: RobotPoseUnitDynamicSimulation.cpp:129