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