KinematicSelfLocalization.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 ArmarXSimulation::ArmarXObjects::KinematicSelfLocalization
17  * @author Mirko Waechter ( mirko dot waechter at kit dot edu )
18  * @date 2016
19  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20  * GNU General Public License
21  */
22 
24 #include <Eigen/Geometry>
25 #include <SimoxUtility/math/convert/rpy_to_mat3f.h>
27 #include <RobotAPI/interface/core/GeometryBase.h>
28 
31 
32 using namespace armarx;
33 
35 {
36  usingProxy(getProperty<std::string>("RobotStateComponentName").getValue());
37 
38  usingProxy(getProperty<std::string>("WorkingMemoryName").getValue());
39  usingTopic(getProperty<std::string>("PlatformTopicName").getValue());
40 }
41 
43 {
44  robotStateComponentPrx = getProxy<RobotStateComponentInterfacePrx>(getProperty<std::string>("RobotStateComponentName").getValue());
46  ARMARX_INFO << "Retrieved robot state proxy...";
47 
48  memoryPrx = getProxy<memoryx::WorkingMemoryInterfacePrx>(getProperty<std::string>("WorkingMemoryName").getValue());
50  ARMARX_INFO << "Retrieved WM proxy...";
51 
52  agentsMemoryPrx = memoryPrx->getAgentInstancesSegment();
54  ARMARX_INFO << "Retrieved WM agent segment ...";
55 
56  Ice::CommunicatorPtr iceCommunicator = getIceManager()->getCommunicator();
57 
58  robotName = getProperty<std::string>("RobotName").getValue();
59  robotPoseZ = getProperty<float>("RobotPoseZ").isSet() ? getProperty<float>("RobotPoseZ").getValue() : 0;
60 
61  std::string agentName = getProperty<std::string>("AgentName").isSet() ? getProperty<std::string>("AgentName").getValue() : robotName;
62 
63 
64  ARMARX_INFO << "Creating robot agent with name " << agentName;
65  robotAgent = new memoryx::AgentInstance(agentName);
66  auto robot = robotStateComponentPrx->getSynchronizedRobot();
67  robotAgent->setSharedRobot(robot);
68  robotAgent->setName(agentName);
69  robotAgent->setStringifiedSharedRobotInterfaceProxy(iceCommunicator->proxyToString(robot));
70  robotAgent->setAgentFilePath(robotStateComponentPrx->getRobotFilename());
71  ARMARX_INFO << "Creating robot agent...done";
72 
73  // Simulating platform movement to initial pose
74  Eigen::Isometry3f global_T_robot_initial = Eigen::Isometry3f::Identity();
75  global_T_robot_initial.translation().x() = getProperty<float>("InitialPlatformPoseX").getValue();
76  global_T_robot_initial.translation().y() = getProperty<float>("InitialPlatformPoseY").getValue();
77  global_T_robot_initial.linear() = simox::math::rpy_to_mat3f(0, 0, getProperty<float>("InitialPlatformPoseAngle").getValue());
78 
79  TransformStamped transformStamped;
80  transformStamped.transform = global_T_robot_initial.matrix();
81  transformStamped.header.agent = agentName;
82  transformStamped.header.frame = GlobalFrame;
83  transformStamped.header.timestampInMicroSeconds = TimeUtil::GetTime().toMicroSeconds();
84 
85  reportGlobalRobotPose(transformStamped);
86 
87  // periodic task setup
88  cycleTime = 30;
89 
90  if (execTask)
91  {
92  execTask->stop();
93  }
94 
95  execTask = new PeriodicTask<KinematicSelfLocalization>(this, &KinematicSelfLocalization::reportRobotPose, cycleTime, false, "KinematicSelfLocalizationCalculation");
96  execTask->start();
97  execTask->setDelayWarningTolerance(100);
98 }
99 
101 {
102  execTask->stop();
103 }
104 
106 {
107 
108 }
109 
111 {
114 }
115 
117 {
118  try
119  {
120  std::unique_lock lock(dataMutex);
121  ARMARX_DEBUG << "Reporting robot agent:" << robotAgent->getName() << ", robotAgentId:" << robotAgentId << ", self:" << this->getName();
122 
123 
124  if (!currentRobotPose)
125  {
126  return;
127  }
128  robotAgent->setPose(currentRobotPose);
129 
130  robotAgentId = agentsMemoryPrx->upsertEntityByName(robotAgent->getName(), robotAgent);
131  robotAgent->setId(robotAgentId);
132 
133 
134  }
135  catch (...)
136  {
137  ARMARX_WARNING << deactivateSpam(10) << "Could not update robot pose in agents segment...\n" << GetHandledExceptionString();
138  robotAgentId.clear();
139  }
140 }
141 
142 void KinematicSelfLocalization::reportGlobalRobotPose(const ::armarx::TransformStamped& transformStamped, const ::Ice::Current&)
143 {
144  std::unique_lock lock(dataMutex);
145 
146  Eigen::Isometry3f global_T_robot(transformStamped.transform);
147  global_T_robot.translation().z() = robotPoseZ; // TODO lecagy. Is this really necessary?
148 
149  currentRobotPose = new FramedPose(global_T_robot.matrix(), armarx::GlobalFrame, "");
150 }
armarx::KinematicSelfLocalization::dataMutex
std::mutex dataMutex
Definition: KinematicSelfLocalization.h:133
armarx::KinematicSelfLocalizationPropertyDefinitions
Definition: KinematicSelfLocalization.h:48
armarx::ManagedIceObject::getIceManager
IceManagerPtr getIceManager() const
Returns the IceManager.
Definition: ManagedIceObject.cpp:353
armarx::VariantType::FramedPose
const VariantTypeId FramedPose
Definition: FramedPose.h:37
armarx::KinematicSelfLocalization::reportGlobalRobotPose
void reportGlobalRobotPose(const ::armarx::TransformStamped &, const ::Ice::Current &=::Ice::emptyCurrent) override
Definition: KinematicSelfLocalization.cpp:142
armarx::GlobalFrame
const std::string GlobalFrame
Definition: FramedPose.h:62
armarx::KinematicSelfLocalization::onInitComponent
void onInitComponent() override
Definition: KinematicSelfLocalization.cpp:34
armarx::KinematicSelfLocalization::robotName
std::string robotName
Definition: KinematicSelfLocalization.h:131
armarx::KinematicSelfLocalization::robotStateComponentPrx
RobotStateComponentInterfacePrx robotStateComponentPrx
Definition: KinematicSelfLocalization.h:124
armarx::KinematicSelfLocalization::robotAgentId
std::string robotAgentId
Definition: KinematicSelfLocalization.h:130
armarx::KinematicSelfLocalization::onDisconnectComponent
void onDisconnectComponent() override
Definition: KinematicSelfLocalization.cpp:100
IceInternal::Handle< ::Ice::Communicator >
armarx::GetHandledExceptionString
std::string GetHandledExceptionString()
Definition: Exception.cpp:147
armarx::KinematicSelfLocalization::robotAgent
memoryx::AgentInstancePtr robotAgent
Definition: KinematicSelfLocalization.h:128
GfxTL::Identity
void Identity(MatrixXX< N, N, T > *a)
Definition: MatrixXX.h:523
KinematicSelfLocalization.h
FramedPose.h
ARMARX_DEBUG
#define ARMARX_DEBUG
Definition: Logging.h:177
armarx::KinematicSelfLocalization::cycleTime
int cycleTime
Definition: KinematicSelfLocalization.h:122
MemoryXCoreObjectFactories.h
armarx::KinematicSelfLocalization::onConnectComponent
void onConnectComponent() override
Definition: KinematicSelfLocalization.cpp:42
armarx::KinematicSelfLocalization::onExitComponent
void onExitComponent() override
Definition: KinematicSelfLocalization.cpp:105
armarx::KinematicSelfLocalization::execTask
PeriodicTask< KinematicSelfLocalization >::pointer_type execTask
Definition: KinematicSelfLocalization.h:120
armarx::KinematicSelfLocalization::agentsMemoryPrx
memoryx::AgentInstancesSegmentBasePrx agentsMemoryPrx
Definition: KinematicSelfLocalization.h:127
armarx::TimeUtil::GetTime
static IceUtil::Time GetTime(TimeMode timeMode=TimeMode::VirtualTime)
Get the current time.
Definition: TimeUtil.cpp:42
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_CHECK_EXPRESSION
#define ARMARX_CHECK_EXPRESSION(expression)
This macro evaluates the expression and if it turns out to be false it will throw an ExpressionExcept...
Definition: ExpressionException.h:73
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:174
IceUtil::Handle< class PropertyDefinitionContainer >
armarx::KinematicSelfLocalization::robotPoseZ
float robotPoseZ
Definition: KinematicSelfLocalization.h:135
armarx::KinematicSelfLocalization::reportRobotPose
void reportRobotPose()
Definition: KinematicSelfLocalization.cpp:116
AgentInstance.h
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::ManagedIceObject::getName
std::string getName() const
Retrieve name of object.
Definition: ManagedIceObject.cpp:107
armarx::PeriodicTask
Definition: ArmarXManager.h:70
memoryx::AgentInstance
Definition: AgentInstance.h:44
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::KinematicSelfLocalization::memoryPrx
memoryx::WorkingMemoryInterfacePrx memoryPrx
Definition: KinematicSelfLocalization.h:126
armarx::KinematicSelfLocalization::currentRobotPose
FramedPosePtr currentRobotPose
Definition: KinematicSelfLocalization.h:123
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::KinematicSelfLocalization::createPropertyDefinitions
armarx::PropertyDefinitionsPtr createPropertyDefinitions() override
Definition: KinematicSelfLocalization.cpp:110