ForceTorqueUnitDynamicSimulation.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 ArmarXCore::units
19  * @author nv
20  * @date 2014
21  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22  * GNU General Public License
23  */
24 
27 
28 #include <SimoxUtility/algorithm/string/string_tools.h>
29 
30 using namespace armarx;
31 
33 {
34  usingTopic("ForceTorqueDynamicSimulationValues");
35  agentName = getProperty<std::string>("AgentName").getValue();
36 
37  std::string framesStr = getProperty<std::string>("ReportFrames").getValue();
38  frames = simox::alg::split(framesStr, ",;");
39  ARMARX_INFO << "ForceTorqueUnit initialized." << std::endl << "AgentName: " << agentName << ". " << std::endl << "Report Frames: " << frames;
40  auto sensorRobotNodeSplit = armarx::Split(getProperty<std::string>("SensorRobotNodeMapping").getValue(), ",");
41  for (auto& elem : sensorRobotNodeSplit)
42  {
43  simox::alg::trim(elem);
44  std::vector<std::string> split = simox::alg::split(elem, ":");
45  if (split.size() == 2)
46  {
47  sensorRobotNodeMapping[simox::alg::trim_copy(split[0])] = simox::alg::trim_copy(split[1]);
48  }
49  }
50  reportInSensorFrame = getProperty<bool>("ReportInSensorFrame").getValue();
51  usingProxy(getProperty<std::string>("RobotStateComponentName").getValue());
52 }
53 
55 {
56  ARMARX_INFO << "Starting ForceTorqueUnit, requesting robot from RobotStateComponent...";
57 
58  try
59  {
60  RobotStateComponentInterfacePrx robotStateComponent = getProxy<RobotStateComponentInterfacePrx>(getProperty<std::string>("RobotStateComponentName").getValue());
61  sharedRobot = robotStateComponent->getSynchronizedRobot();
63  }
64  catch (...)
65  {
67  terminate();
68  return;
69  }
70 
71  for (auto& frame : frames)
72  {
73  if (!frame.empty() && !remoteRobot->hasRobotNode(frame))
74  {
75  ARMARX_WARNING << "Robot " << remoteRobot->getName() << " does not know report frame '" << frame << "'. Will not report FT values in this frame...";
76  }
77  }
78 }
79 
81 {
82 
83 }
84 
86 {
88 }
89 
90 
91 void ForceTorqueUnitDynamicSimulation::reportForceTorque(const Vector3BasePtr& force, const Vector3BasePtr& torque, const std::string& sensorName, const std::string& nodeName, bool aValueChanged, const Ice::Current& c)
92 {
93  std::unique_lock lock(reportMutex);
94  Vector3Ptr f = Vector3Ptr::dynamicCast(filterValues(sensorName, force, forceFilters));
95  Vector3Ptr t = Vector3Ptr::dynamicCast(filterValues(sensorName, torque, torqueFilters));
96 
97  FramedDirectionPtr framedForce = new FramedDirection(f->toEigen(), nodeName, agentName);
98  FramedDirectionPtr framedTorque = new FramedDirection(t->toEigen(), nodeName, agentName);
100  try
101  {
102  auto prx = listenerPrx->ice_batchOneway();
103  // if sensor is named as a robot node, we use the robot node as report frame
104  if (sensorName != nodeName && remoteRobot->hasRobotNode(sensorName))
105  {
106  framedForce->changeFrame(remoteRobot, sensorName);
107  framedTorque->changeFrame(remoteRobot, sensorName);
108  }
109 
111  {
112  prx->reportSensorValues(sensorName, framedForce, framedTorque);
113  }
114 
115  for (auto& frame : frames)
116  {
117  if (!frame.empty() && remoteRobot->hasRobotNode(frame))
118  {
119  FramedDirectionPtr framedForce1 = new FramedDirection(f->toEigen(), nodeName, agentName);
120  FramedDirectionPtr framedTorque1 = new FramedDirection(t->toEigen(), nodeName, agentName);
121  framedForce1->changeFrame(remoteRobot, frame);
122  framedTorque1->changeFrame(remoteRobot, frame);
123  prx->reportSensorValues(sensorName, framedForce1, framedTorque1);
124  }
125  }
126 
127  auto it = sensorRobotNodeMapping.find(sensorName);
128  if (it != sensorRobotNodeMapping.end())
129  {
130  if (remoteRobot->hasRobotNode(it->second))
131  {
132  FramedDirectionPtr framedForce1 = new FramedDirection(f->toEigen(), nodeName, agentName);
133  FramedDirectionPtr framedTorque1 = new FramedDirection(t->toEigen(), nodeName, agentName);
134  framedForce1->changeFrame(remoteRobot, it->second);
135  framedTorque1->changeFrame(remoteRobot, it->second);
136  prx->reportSensorValues(it->second, framedForce1, framedTorque1);
137  ARMARX_DEBUG << deactivateSpam(1, it->second) << "Reporting " << it->second << ": " << framedForce1->output() << "\n" << framedTorque1->output() ;
138  }
139  else
140  {
141  ARMARX_WARNING << deactivateSpam(10000, it->second) << "Robot does not have node " << it->second;
142  }
143  }
144  prx->ice_flushBatchRequests();
145  }
146  catch (...)
147  {
148  ARMARX_WARNING << deactivateSpam() << "Could not report FT vlaues:";
150  }
151 }
152 
153 Vector3BasePtr ForceTorqueUnitDynamicSimulation::filterValues(const std::string& sensorName, const Vector3BasePtr& torques, std::map<std::string, DatafieldFilterBasePtr>& filters)
154 {
155  auto filter = [&, this](const std::string & dim, float value)
156  {
157  const auto name = sensorName + dim;
158  auto itX = filters.find(name);
159  if (itX == filters.end())
160  {
161  filters.insert(std::make_pair(name, new filters::ButterworthFilter(2, 100, Lowpass, 1)));
162  itX = filters.find(name);
163  }
164  auto& filterX = *itX->second;
165  filterX.update(0, new Variant(value));
166  double r = filterX.getValue()->getDouble();
167  return r;
168  };
169 
170 
171 
172  Vector3BasePtr result = new Vector3(filter("x", torques->x),
173  filter("y", torques->y),
174  filter("z", torques->z));
175  return result;
176 }
177 
178 
179 void armarx::ForceTorqueUnitDynamicSimulation::setOffset(const armarx::FramedDirectionBasePtr&, const armarx::FramedDirectionBasePtr&, const Ice::Current&)
180 {
181  ARMARX_INFO_S << "NYI";
182 }
183 
185 {
186  ARMARX_INFO_S << "NYI";
187 }
armarx::Variant
The Variant class is described here: Variants.
Definition: Variant.h:224
armarx::RemoteRobot::synchronizeLocalClone
static bool synchronizeLocalClone(VirtualRobot::RobotPtr robot, RobotStateComponentInterfacePrx robotStatePrx)
Definition: RemoteRobot.cpp:448
armarx::ForceTorqueUnitDynamicSimulationPropertyDefinitions
Definition: ForceTorqueUnitDynamicSimulation.h:43
armarx::Split
std::vector< std::string > Split(const std::string &source, const std::string &splitBy, bool trimElements=false, bool removeEmptyElements=false)
Definition: StringHelperTemplates.h:35
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:43
armarx::ForceTorqueUnitDynamicSimulation::onStartForceTorqueUnit
void onStartForceTorqueUnit() override
Definition: ForceTorqueUnitDynamicSimulation.cpp:54
armarx::ForceTorqueUnitDynamicSimulation::remoteRobot
VirtualRobot::RobotPtr remoteRobot
Definition: ForceTorqueUnitDynamicSimulation.h:93
IceInternal::Handle< Vector3 >
armarx::ForceTorqueUnitDynamicSimulation::onExitForceTorqueUnit
void onExitForceTorqueUnit() override
Definition: ForceTorqueUnitDynamicSimulation.cpp:80
armarx::ForceTorqueUnitDynamicSimulation::filterValues
Vector3BasePtr filterValues(const std::string &sensorName, const Vector3BasePtr &torques, std::map< std::string, DatafieldFilterBasePtr > &filters)
Definition: ForceTorqueUnitDynamicSimulation.cpp:153
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:926
armarx::ForceTorqueUnitDynamicSimulation::setToNull
void setToNull(const Ice::Current &) override
Definition: ForceTorqueUnitDynamicSimulation.cpp:184
FramedPose.h
armarx::filters::ButterworthFilter
Definition: ButterworthFilter.h:40
ARMARX_DEBUG
#define ARMARX_DEBUG
Definition: Logging.h:177
armarx::ManagedIceObject::terminate
void terminate()
Initiates termination of this IceManagedObject.
Definition: ManagedIceObject.cpp:420
armarx::ForceTorqueUnitDynamicSimulation::sensorRobotNodeMapping
armarx::StringStringDictionary sensorRobotNodeMapping
Definition: ForceTorqueUnitDynamicSimulation.h:96
armarx::ForceTorqueUnitDynamicSimulation::onInitForceTorqueUnit
void onInitForceTorqueUnit() override
Definition: ForceTorqueUnitDynamicSimulation.cpp:32
armarx::ForceTorqueUnitDynamicSimulation::frames
std::vector< std::string > frames
Definition: ForceTorqueUnitDynamicSimulation.h:95
armarx::ManagedIceObject::usingTopic
void usingTopic(const std::string &name, bool orderedPublishing=false)
Registers a proxy for subscription after initialization.
Definition: ManagedIceObject.cpp:248
armarx::ForceTorqueUnitDynamicSimulation::forceFilters
std::map< std::string, DatafieldFilterBasePtr > forceFilters
Definition: ForceTorqueUnitDynamicSimulation.h:98
armarx::Component::getConfigIdentifier
std::string getConfigIdentifier()
Retrieve config identifier for this component as set in constructor.
Definition: Component.cpp:74
armarx::ForceTorqueUnit::listenerPrx
ForceTorqueUnitListenerPrx listenerPrx
Definition: ForceTorqueUnit.h:93
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:174
armarx::VariantType::FramedDirection
const VariantTypeId FramedDirection
Definition: FramedPose.h:38
armarx::RemoteRobot::createLocalClone
VirtualRobot::RobotPtr createLocalClone()
Clones the structure of this remote robot to a local instance.
Definition: RemoteRobot.cpp:328
armarx::ForceTorqueUnitDynamicSimulation::reportInSensorFrame
bool reportInSensorFrame
Definition: ForceTorqueUnitDynamicSimulation.h:99
IceUtil::Handle< class PropertyDefinitionContainer >
memoryx::KBM::Vector3
Eigen::Vector3d Vector3
Definition: kbm.h:41
IceInternal::ProxyHandle<::IceProxy::armarx::RobotStateComponentInterface >
armarx::ForceTorqueUnitDynamicSimulation::createPropertyDefinitions
PropertyDefinitionsPtr createPropertyDefinitions() override
Definition: ForceTorqueUnitDynamicSimulation.cpp:85
armarx::ForceTorqueUnitDynamicSimulation::reportMutex
std::mutex reportMutex
Definition: ForceTorqueUnitDynamicSimulation.h:100
armarx::ForceTorqueUnitDynamicSimulation::sharedRobot
SharedRobotInterfacePrx sharedRobot
Definition: ForceTorqueUnitDynamicSimulation.h:94
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::ForceTorqueUnitDynamicSimulation::agentName
std::string agentName
Definition: ForceTorqueUnitDynamicSimulation.h:87
ARMARX_INFO_S
#define ARMARX_INFO_S
Definition: Logging.h:195
armarx::ForceTorqueUnitDynamicSimulation::setOffset
void setOffset(const FramedDirectionBasePtr &, const FramedDirectionBasePtr &, const Ice::Current &) override
Definition: ForceTorqueUnitDynamicSimulation.cpp:179
armarx::handleExceptions
void handleExceptions()
Definition: Exception.cpp:141
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::ForceTorqueUnitDynamicSimulation::reportForceTorque
void reportForceTorque(const Vector3BasePtr &force, const Vector3BasePtr &torque, const std::string &sensorName, const std::string &nodeName, bool aValueChanged, const Ice::Current &c=Ice::emptyCurrent) override
Definition: ForceTorqueUnitDynamicSimulation.cpp:91
ForceTorqueUnitDynamicSimulation.h
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::ForceTorqueUnitDynamicSimulation::torqueFilters
std::map< std::string, DatafieldFilterBasePtr > torqueFilters
Definition: ForceTorqueUnitDynamicSimulation.h:97
armarx::split
std::vector< std::string > split(const std::string &source, const std::string &splitBy, bool trimElements=false, bool removeEmptyElements=false)
Definition: StringHelpers.cpp:36