30#include <VirtualRobot/Nodes/RobotNode.h>
31#include <VirtualRobot/RobotNodeSet.h>
32#include <VirtualRobot/VirtualRobot.h>
33#include <VirtualRobot/VirtualRobotException.h>
34#include <VirtualRobot/XML/RobotIO.h>
40#include <RobotAPI/interface/units/KinematicUnitInterface.h>
49 "IntervalMs", 10,
"The time in milliseconds between two calls to the simulation method.");
51 "Noise", 0.0f,
"Gaussian noise is added to the velocity. Value in Degree");
54 "If true a PD controller is also used in Position Mode instead of "
55 "setting the joint angles instantly");
67 for (std::vector<VirtualRobot::RobotNodePtr>::iterator it =
robotNodes.begin();
71 std::string jointName = (*it)->getName();
72 jointInfos[jointName].limitLo = (*it)->getJointLimitLo();
73 jointInfos[jointName].limitHi = (*it)->getJointLimitHi();
75 if ((*it)->isRotationalJoint())
92 for (std::vector<VirtualRobot::RobotNodePtr>::iterator it =
robotNodes.begin();
96 std::string jointName = (*it)->getName();
119 "KinematicUnitSimulation");
139 double timeDeltaInSeconds =
148 bool aPosValueChanged =
false;
149 bool aVelValueChanged =
false;
150 auto signedMin = [](
float newValue,
float minAbsValue)
151 {
return std::copysign(std::min<float>(fabs(newValue), fabs(minAbsValue)), newValue); };
157 for (KinematicUnitSimulationJointStates::iterator it =
jointStates.begin();
162 float newAngle = 0.0f;
163 float newVelocity = 0.0f;
166 if (it->second.controlMode == eVelocityControl)
168 double change = (it->second.velocity * timeDeltaInSeconds);
169 newVelocity = it->second.velocity;
173 change *= 1 + randomNoiseValue;
175 newAngle = it->second.angle + change;
179 aVelValueChanged =
true;
182 else if (it->second.controlMode == ePositionControl)
186 newAngle = it->second.angle;
194 (it->second.velocity != 0.0f
195 ?
signedMin(pid->getControlValue(), it->second.velocity)
196 : pid->getControlValue());
202 pid->update(it->second.angle);
203 newAngle = it->second.angle + velValue * timeDeltaInSeconds;
207 aVelValueChanged =
true;
209 it->second.velocityActual = newVelocity = velValue;
220 newAngle = std::max<float>(newAngle, minAngle);
221 newAngle = std::min<float>(newAngle, maxAngle);
225 KinematicUnitSimulationJointStates::iterator itPrev =
230 aPosValueChanged = aVelValueChanged =
true;
236 aPosValueChanged =
true;
238 it->second.angle = newAngle;
267 const Ice::Current&
c)
269 bool aValueChanged =
false;
270 NameControlModeMap changedControlModes;
274 for (NameControlModeMap::const_iterator it = targetJointModes.begin();
275 it != targetJointModes.end();
279 std::string targetJointName = it->first;
280 ControlMode targetControlMode = it->second;
282 KinematicUnitSimulationJointStates::iterator iterJoints =
288 if (iterJoints->second.controlMode != targetControlMode)
290 aValueChanged =
true;
293 iterJoints->second.controlMode = targetControlMode;
294 changedControlModes.insert(*it);
298 ARMARX_WARNING <<
"Could not find joint with name " << targetJointName;
309 const Ice::Current&
c)
314 NameValueMap actualJointAngles;
315 bool aValueChanged =
false;
317 for (NameValueMap::const_iterator it = targetJointAngles.begin(); it != targetJointAngles.end();
321 std::string targetJointName = it->first;
322 float targetJointAngle = it->second;
325 KinematicUnitSimulationJointStates::iterator iterJoints =
jointStates.find(targetJointName);
329 if (fabs(iterJoints->second.angle - targetJointAngle) > 0)
331 aValueChanged =
true;
336 targetJointAngle = std::max<float>(targetJointAngle, jointInfo.
limitLo);
337 targetJointAngle = std::min<float>(targetJointAngle, jointInfo.
limitHi);
342 iterJoints->second.angle = targetJointAngle;
343 actualJointAngles[targetJointName] = iterJoints->second.angle;
353 float delta = targetJointAngle - iterJoints->second.angle;
354 float signedSmallestDelta = atan2(sin(delta), cos(delta));
355 targetJointAngle = iterJoints->second.angle + signedSmallestDelta;
358 pid->setTarget(targetJointAngle);
380 NameValueMap actualJointVelocities;
381 bool aValueChanged =
false;
383 for (NameValueMap::const_iterator it = targetJointVelocities.begin();
384 it != targetJointVelocities.end();
388 std::string targetJointName = it->first;
389 float targetJointVelocity = it->second;
391 KinematicUnitSimulationJointStates::iterator iterJoints =
jointStates.find(targetJointName);
396 if (fabs(iterJoints->second.velocity - targetJointVelocity) > 0)
398 aValueChanged =
true;
401 iterJoints->second.velocity = targetJointVelocity;
402 actualJointVelocities[targetJointName] = iterJoints->second.velocity;
418 NameValueMap actualJointTorques;
419 bool aValueChanged =
false;
421 for (NameValueMap::const_iterator it = targetJointTorques.begin();
422 it != targetJointTorques.end();
426 std::string targetJointName = it->first;
427 float targetJointTorque = it->second;
429 KinematicUnitSimulationJointStates::iterator iterJoints =
jointStates.find(targetJointName);
434 if (fabs(iterJoints->second.torque - targetJointTorque) > 0)
436 aValueChanged =
true;
439 iterJoints->second.torque = targetJointTorque;
440 actualJointTorques[targetJointName] = iterJoints->second.torque;
454 NameControlModeMap result;
456 for (KinematicUnitSimulationJointStates::iterator it =
jointStates.begin();
460 result[it->first] = it->second.controlMode;
470 (void)targetJointAccelerations;
477 (void)targetJointDecelerations;
486 for (KinematicUnitSimulationJointStates::iterator it =
jointStates.begin();
490 it->second.velocity = 0;
532 debugInfo.jointAngles[jointName] = info.angle;
533 debugInfo.jointVelocities[jointName] = info.velocity;
535 debugInfo.jointMotorTemperatures[jointName] = info.temperature;
537 debugInfo.jointTorques[jointName] = info.torque;
538 debugInfo.jointModes[jointName] = info.controlMode;
546 std::string compareString)
548 return (iter.first == compareString);
555 KinematicUnitSimulationJointStates::const_iterator it = std::find_first_of(
568 KinematicUnitSimulationJointStates::const_iterator it = std::find_first_of(
bool mapEntryEqualsString(std::pair< std::string, KinematicUnitSimulationJointState > iter, std::string compareString)
SpamFilterDataPtr deactivateSpam(SpamFilterDataPtr const &spamFilter, float deactivationDurationSec, const std::string &identifier, bool deactivate)
std::string getConfigIdentifier()
Retrieve config identifier for this component as set in constructor.
Property< PropertyType > getProperty(const std::string &name)
KinematicUnitPropertyDefinitions(std::string prefix)
bool continuousJoint() const
KinematicUnitSimulationPropertyDefinitions(std::string prefix)
void releaseJoints(const Ice::StringSeq &joints, const Ice::Current &c=Ice::emptyCurrent) override
IceUtil::Time lastExecutionTime
PeriodicTask< KinematicUnitSimulation >::pointer_type simulationTask
void requestJoints(const Ice::StringSeq &joints, const Ice::Current &c=Ice::emptyCurrent) override
std::normal_distribution< double > distribution
NameValueMap sensorVelocities
armarx::NameValueMap getJointVelocities(const Ice::Current &c) const override
NameValueMap sensorAngles
armarx::NameValueMap getJointAngles(const Ice::Current &c) const override
std::default_random_engine generator
void setJointAngles(const NameValueMap &targetJointAngles, const Ice::Current &c=Ice::emptyCurrent) override
void switchControlMode(const NameControlModeMap &targetJointModes, const Ice::Current &c=Ice::emptyCurrent) override
void setJointTorques(const NameValueMap &targetJointTorques, const Ice::Current &c=Ice::emptyCurrent) override
DebugInfo getDebugInfo(const Ice::Current &c=Ice::emptyCurrent) const override
bool usePDControllerForPosMode
std::map< std::string, PIDControllerPtr > positionControlPIDController
void onInitKinematicUnit() override
void stop(const Ice::Current &c=Ice::emptyCurrent) override
void setJointAccelerations(const NameValueMap &targetJointAccelerations, const Ice::Current &c=Ice::emptyCurrent) override
void setJointDecelerations(const NameValueMap &targetJointDecelerations, const Ice::Current &c=Ice::emptyCurrent) override
void onStartKinematicUnit() override
std::mutex sensorDataMutex
PropertyDefinitionsPtr createPropertyDefinitions() override
NameControlModeMap getControlModes(const Ice::Current &c=Ice::emptyCurrent) override
KinematicUnitSimulationJointStates jointStates
std::mutex jointStatesMutex
KinematicUnitSimulationJointInfos jointInfos
Ice::StringSeq getJoints(const Ice::Current &c) const override
KinematicUnitSimulationJointStates previousJointStates
void onExitKinematicUnit() override
void simulationFunction()
void setJointVelocities(const NameValueMap &targetJointVelocities, const Ice::Current &c=Ice::emptyCurrent) override
std::vector< VirtualRobot::RobotNodePtr > robotNodes
KinematicUnitListenerPrx listenerPrx
The periodic task executes one thread method repeatedly using the time period specified in the constr...
std::string prefix
Prefix of the properties such as namespace, domain, component name, etc.
PropertyDefinition< PropertyType > & defineOptionalProperty(const std::string &name, PropertyType defaultValue, const std::string &description="", PropertyDefinitionBase::PropertyConstness constness=PropertyDefinitionBase::eConstant)
void release(const Ice::Current &c=Ice::emptyCurrent) override
Release exclusive access to current unit.
void stop(const Ice::Current &c=Ice::emptyCurrent) override
Set execution state to eStopped.
void request(const Ice::Current &c=Ice::emptyCurrent) override
Request exclusive access to current unit.
static IceUtil::Time GetTime(TimeMode timeMode=TimeMode::VirtualTime)
Get the current time.
#define ARMARX_INFO
The normal logging level.
#define ARMARX_WARNING
The logging level for unexpected behaviour, but not a serious problem.
#define ARMARX_VERBOSE
The logging level for verbose information.
This file offers overloads of toIce() and fromIce() functions for STL container types.
std::shared_ptr< PIDController > PIDControllerPtr
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.
void getMapKeys(const MapType &map, OutputIteratorType it)