23 #ifndef _ARMARX_LIB_DSController_DSRTController_H
24 #define _ARMARX_LIB_DSController_DSRTController_H
26 #include <VirtualRobot/Robot.h>
27 #include <VirtualRobot/Tools/Gravity.h>
28 #include <VirtualRobot/IK/DifferentialIK.h>
36 #include <armarx/control/ds_controller/DSControllerInterface.h>
58 std::vector<double>
Mu_;
85 std::ifstream infile { fileName };
86 std::string objDefs = { std::istreambuf_iterator<char>(infile), std::istreambuf_iterator<char>() };
88 json->fromString(objDefs);
98 scaling = json->getDouble(
"Scaling");
99 belief = json->getDouble(
"InitBelief");
101 v_max = json->getDouble(
"MaxVelocity");
104 gmm->initGMR(0, 2, 3, 5);
108 ARMARX_ERROR <<
"attractor in json file should be 6 dimension vector ... ";
111 for (
int i = 0; i < 3; ++i)
120 if (PositionError.norm() < positionErrorToleranceInMeter)
122 PositionError.setZero();
126 position_error.Resize(3);
128 for (
int i = 0; i < 3; ++i)
130 position_error(i) = PositionError(i);
134 desired_vel.Resize(3);
135 desired_vel =
gmm->getVelocity(position_error);
137 Eigen::Vector3f tcpDesiredLinearVelocity;
138 tcpDesiredLinearVelocity << desired_vel(0), desired_vel(1), desired_vel(2);
143 float lenVec = tcpDesiredLinearVelocity.norm();
144 if (std::isnan(lenVec))
146 tcpDesiredLinearVelocity.setZero();
151 tcpDesiredLinearVelocity = (
v_max / lenVec) * tcpDesiredLinearVelocity;
189 gmmMotionGenList[i]->updateDesiredVelocity(currentPositionInMeter, positionErrorToleranceInMeter);
196 std::vector<float> beliefUpdate;
199 float nullInnerSimilarity = 0;
205 float belief = currentGMM->belief;
206 Eigen::Vector3f OtherTasks =
totalDesiredVelocity - belief * currentGMM->currentDesiredVelocity;
207 float innerSimilarity = 2 * OtherTasks.dot(currentGMM->currentDesiredVelocity);
208 float outerDisSimilarity = (realVelocity - currentGMM->currentDesiredVelocity).squaredNorm();
210 if (innerSimilarity > nullInnerSimilarity)
212 nullInnerSimilarity = innerSimilarity;
215 beliefUpdate[i] = - outerDisSimilarity - innerSimilarity;
223 float nullOuterSimilarity = realVelocity.squaredNorm();
224 float zeroTaskRawBeliefUpdate = - nullInnerSimilarity - nullOuterSimilarity;
226 if (zeroTaskRawBeliefUpdate < 0.2)
228 zeroTaskRawBeliefUpdate -= 1000;
232 beliefUpdate.insert(beliefUpdate.begin(), zeroTaskRawBeliefUpdate);
234 WinnerTakeAll(beliefUpdate);
275 void WinnerTakeAll(std::vector<float>& UpdateBeliefs_)
279 size_t winner_index = 0;
281 for (
size_t i = 1; i < UpdateBeliefs_.size(); i++)
283 if (UpdateBeliefs_[i] > UpdateBeliefs_[winner_index])
291 if (winner_index != 0)
296 if (winner_belief == 1)
301 int runnerUp_index = 0;
303 if (winner_index == 0)
308 for (
size_t i = 0; i < UpdateBeliefs_.size(); i++)
310 if (i == winner_index)
315 if (UpdateBeliefs_[i] > UpdateBeliefs_[runnerUp_index])
321 float offset = 0.5 * (UpdateBeliefs_[winner_index] + UpdateBeliefs_[runnerUp_index]);
323 for (
size_t i = 0; i < UpdateBeliefs_.size(); i++)
325 UpdateBeliefs_[i] -= offset;
330 for (
size_t i = 0; i < UpdateBeliefs_.size(); i++)
338 if (belief != 0 || UpdateBeliefs_[i] > 0)
340 UpdateSum += UpdateBeliefs_[i];
344 UpdateBeliefs_[winner_index] -= UpdateSum;
385 return "DSRTController";
394 struct DSRTControllerSensorData
399 Eigen::Vector3f linearVelocity;
406 StringFloatDictionary desired_torques;
407 float desiredForce_x;
408 float desiredForce_y;
409 float desiredForce_z;
410 float tcpDesiredTorque_x;
411 float tcpDesiredTorque_y;
412 float tcpDesiredTorque_z;
414 float tcpDesiredAngularError_x;
415 float tcpDesiredAngularError_y;
416 float tcpDesiredAngularError_z;
418 float currentTCPAngularVelocity_x;
419 float currentTCPAngularVelocity_y;
420 float currentTCPAngularVelocity_z;
422 float currentTCPLinearVelocity_x;
423 float currentTCPLinearVelocity_y;
424 float currentTCPLinearVelocity_z;
432 std::vector<const SensorValue1DoFActuatorTorque*> torqueSensors;
433 std::vector<const SensorValue1DoFGravityTorque*> gravityTorqueSensors;
434 std::vector<const SensorValue1DoFActuatorVelocity*> velocitySensors;
435 std::vector<const SensorValue1DoFActuatorPosition*> positionSensors;
437 std::vector<ControlTarget1DoFActuatorTorque*> targets;
440 VirtualRobot::RobotNodePtr tcp;
442 VirtualRobot::DifferentialIKPtr ik;
443 Eigen::MatrixXf jacobip;
444 Eigen::MatrixXf jacobio;
446 Eigen::Vector3f desiredPosition;
449 Eigen::Vector3f oldPosition;
453 Eigen::Vector3f currentTCPLinearVelocity_filtered;
454 Eigen::Vector3f currentTCPAngularVelocity_filtered;
456 float filterTimeConstant;
458 std::vector<std::string> jointNames;
469 float nullspaceDamping;
471 Eigen::VectorXf qnullspace;
473 std::vector<GMMMotionGenPtr> gmmMotionGenList;
477 float positionErrorTolerance;