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>
48 defineOptionalProperty<int>(
49 "IntervalMs", 10,
"The time in milliseconds between two calls to the simulation method.");
50 defineOptionalProperty<float>(
51 "Noise", 0.0f,
"Gaussian noise is added to the velocity. Value in Degree");
52 defineOptionalProperty<bool>(
"UsePDControllerForJointControl",
54 "If true a PD controller is also used in Position Mode instead of "
55 "setting the joint angles instantly");
56 defineOptionalProperty<float>(
"Kp", 3,
"proportional gain of the PID position controller.");
57 defineOptionalProperty<float>(
"Ki", 0.001f,
"integral gain of the PID position controller.");
58 defineOptionalProperty<float>(
"Kd", 0.0,
"derivative gain of the PID position controller.");
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();
101 getProperty<float>(
"Ki").getValue(),
102 getProperty<float>(
"Kd").getValue()));
106 noise = getProperty<float>(
"Noise").getValue() / 180.f *
M_PI;
112 intervalMs = getProperty<int>(
"IntervalMs").getValue();
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)
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);
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;
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(