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
28#include <SimoxUtility/algorithm/string/string_tools.h>
29
31
32using namespace armarx;
33
34void
36{
37 usingTopic("ForceTorqueDynamicSimulationValues");
38 agentName = getProperty<std::string>("AgentName").getValue();
39
40 std::string framesStr = getProperty<std::string>("ReportFrames").getValue();
41 frames = simox::alg::split(framesStr, ",;");
42 ARMARX_INFO << "ForceTorqueUnit initialized." << std::endl
43 << "AgentName: " << agentName << ". " << std::endl
44 << "Report Frames: " << frames;
45 auto sensorRobotNodeSplit =
46 armarx::Split(getProperty<std::string>("SensorRobotNodeMapping").getValue(), ",");
47 for (auto& elem : sensorRobotNodeSplit)
48 {
49 simox::alg::trim(elem);
50 std::vector<std::string> split = simox::alg::split(elem, ":");
51 if (split.size() == 2)
52 {
53 sensorRobotNodeMapping[simox::alg::trim_copy(split[0])] =
54 simox::alg::trim_copy(split[1]);
55 }
56 }
57 reportInSensorFrame = getProperty<bool>("ReportInSensorFrame").getValue();
58 usingProxy(getProperty<std::string>("RobotStateComponentName").getValue());
59}
60
61void
63{
64 ARMARX_INFO << "Starting ForceTorqueUnit, requesting robot from RobotStateComponent...";
65
66 try
67 {
68 RobotStateComponentInterfacePrx robotStateComponent =
70 getProperty<std::string>("RobotStateComponentName").getValue());
71 sharedRobot = robotStateComponent->getSynchronizedRobot();
73 }
74 catch (...)
75 {
77 terminate();
78 return;
79 }
80
81 for (auto& frame : frames)
82 {
83 if (!frame.empty() && !remoteRobot->hasRobotNode(frame))
84 {
85 ARMARX_WARNING << "Robot " << remoteRobot->getName() << " does not know report frame '"
86 << frame << "'. Will not report FT values in this frame...";
87 }
88 }
89}
90
91void
95
102
103void
105 const Vector3BasePtr& torque,
106 const std::string& sensorName,
107 const std::string& nodeName,
108 bool aValueChanged,
109 const Ice::Current& c)
110{
111 std::unique_lock lock(reportMutex);
112 Vector3Ptr f = Vector3Ptr::dynamicCast(filterValues(sensorName, force, forceFilters));
113 Vector3Ptr t = Vector3Ptr::dynamicCast(filterValues(sensorName, torque, torqueFilters));
114
115 FramedDirectionPtr framedForce = new FramedDirection(f->toEigen(), nodeName, agentName);
116 FramedDirectionPtr framedTorque = new FramedDirection(t->toEigen(), nodeName, agentName);
118 try
119 {
120 auto prx = listenerPrx->ice_batchOneway();
121 // if sensor is named as a robot node, we use the robot node as report frame
122 if (sensorName != nodeName && remoteRobot->hasRobotNode(sensorName))
123 {
124 framedForce->changeFrame(remoteRobot, sensorName);
125 framedTorque->changeFrame(remoteRobot, sensorName);
126 }
127
129 {
130 prx->reportSensorValues(sensorName, framedForce, framedTorque);
131 }
132
133 for (auto& frame : frames)
134 {
135 if (!frame.empty() && remoteRobot->hasRobotNode(frame))
136 {
137 FramedDirectionPtr framedForce1 =
138 new FramedDirection(f->toEigen(), nodeName, agentName);
139 FramedDirectionPtr framedTorque1 =
140 new FramedDirection(t->toEigen(), nodeName, agentName);
141 framedForce1->changeFrame(remoteRobot, frame);
142 framedTorque1->changeFrame(remoteRobot, frame);
143 prx->reportSensorValues(sensorName, framedForce1, framedTorque1);
144 }
145 }
146
147 auto it = sensorRobotNodeMapping.find(sensorName);
148 if (it != sensorRobotNodeMapping.end())
149 {
150 if (remoteRobot->hasRobotNode(it->second))
151 {
152 FramedDirectionPtr framedForce1 =
153 new FramedDirection(f->toEigen(), nodeName, agentName);
154 FramedDirectionPtr framedTorque1 =
155 new FramedDirection(t->toEigen(), nodeName, agentName);
156 framedForce1->changeFrame(remoteRobot, it->second);
157 framedTorque1->changeFrame(remoteRobot, it->second);
158 prx->reportSensorValues(it->second, framedForce1, framedTorque1);
159 ARMARX_DEBUG << deactivateSpam(1, it->second) << "Reporting " << it->second << ": "
160 << framedForce1->output() << "\n"
161 << framedTorque1->output();
162 }
163 else
164 {
165 ARMARX_WARNING << deactivateSpam(10000, it->second) << "Robot does not have node "
166 << it->second;
167 }
168 }
169 prx->ice_flushBatchRequests();
170 }
171 catch (...)
172 {
173 ARMARX_WARNING << deactivateSpam() << "Could not report FT vlaues:";
175 }
176}
177
178Vector3BasePtr
180 const std::string& sensorName,
181 const Vector3BasePtr& torques,
182 std::map<std::string, DatafieldFilterBasePtr>& filters)
183{
184 auto filter = [&, this](const std::string& dim, float value)
185 {
186 const auto name = sensorName + dim;
187 auto itX = filters.find(name);
188 if (itX == filters.end())
189 {
190 filters.insert(
191 std::make_pair(name, new filters::ButterworthFilter(2, 100, Lowpass, 1)));
192 itX = filters.find(name);
193 }
194 auto& filterX = *itX->second;
195 filterX.update(0, new Variant(value));
196 double r = filterX.getValue()->getDouble();
197 return r;
198 };
199
200
201 Vector3BasePtr result =
202 new Vector3(filter("x", torques->x), filter("y", torques->y), filter("z", torques->z));
203 return result;
204}
205
206void
207armarx::ForceTorqueUnitDynamicSimulation::setOffset(const armarx::FramedDirectionBasePtr&,
208 const armarx::FramedDirectionBasePtr&,
209 const Ice::Current&)
210{
211 ARMARX_INFO_S << "NYI";
212}
213
214void
216{
217 ARMARX_INFO_S << "NYI";
218}
219
220std::string
222{
223 return "ForceTorqueUnitDynamicSimulation";
224}
225
#define ARMARX_REGISTER_COMPONENT_EXECUTABLE(ComponentT, applicationName)
Definition Decoupled.h:29
SpamFilterDataPtr deactivateSpam(SpamFilterDataPtr const &spamFilter, float deactivationDurationSec, const std::string &identifier, bool deactivate)
Definition Logging.cpp:75
constexpr T c
std::string getConfigIdentifier()
Retrieve config identifier for this component as set in constructor.
Definition Component.cpp:90
Property< PropertyType > getProperty(const std::string &name)
This unit connects to the physics simulator topic (default: "Simulator") and reports force torque val...
std::map< std::string, DatafieldFilterBasePtr > forceFilters
Vector3BasePtr filterValues(const std::string &sensorName, const Vector3BasePtr &torques, std::map< std::string, DatafieldFilterBasePtr > &filters)
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
std::map< std::string, DatafieldFilterBasePtr > torqueFilters
void setOffset(const FramedDirectionBasePtr &, const FramedDirectionBasePtr &, const Ice::Current &) override
ForceTorqueUnitListenerPrx listenerPrx
FramedDirection is a 3 dimensional direction vector with a reference frame.
Definition FramedPose.h:87
bool usingProxy(const std::string &name, const std::string &endpoints="")
Registers a proxy for retrieval after initialization and adds it to the dependency list.
void terminate()
Initiates termination of this IceManagedObject.
void usingTopic(const std::string &name, bool orderedPublishing=false)
Registers a proxy for subscription after initialization.
Ice::ObjectPrx getProxy(long timeoutMs=0, bool waitForScheduler=true) const
Returns the proxy of this object (optionally it waits for the proxy)
VirtualRobot::RobotPtr createLocalClone()
Clones the structure of this remote robot to a local instance.
static bool synchronizeLocalClone(VirtualRobot::RobotPtr robot, RobotStateComponentInterfacePrx robotStatePrx)
The Variant class is described here: Variants.
Definition Variant.h:224
The Vector3 class.
Definition Pose.h:113
#define ARMARX_INFO
The normal logging level.
Definition Logging.h:181
#define ARMARX_INFO_S
Definition Logging.h:202
#define ARMARX_DEBUG
The logging level for output that is only interesting while debugging.
Definition Logging.h:184
#define ARMARX_WARNING
The logging level for unexpected behaviour, but not a serious problem.
Definition Logging.h:193
This file offers overloads of toIce() and fromIce() functions for STL container types.
std::vector< std::string > split(const std::string &source, const std::string &splitBy, bool trimElements=false, bool removeEmptyElements=false)
IceInternal::Handle< Vector3 > Vector3Ptr
Definition Pose.h:165
void handleExceptions()
std::vector< std::string > Split(const std::string &source, const std::string &splitBy, bool trimElements=false, bool removeEmptyElements=false)
IceInternal::Handle< FramedDirection > FramedDirectionPtr
Definition FramedPose.h:84
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.
::IceInternal::ProxyHandle<::IceProxy::armarx::RobotStateComponentInterface > RobotStateComponentInterfacePrx