12 return "NJointJointSpaceDMPController";
17 NJointJointSpaceDMPControllerConfigPtr cfg = NJointJointSpaceDMPControllerConfigPtr::dynamicCast(config);
18 ARMARX_CHECK_EXPRESSION_W_HINT(cfg,
"Needed type: NJointJointSpaceDMPControllerConfigPtr");
20 for (std::string jointName : cfg->jointNames)
24 targets.insert(std::make_pair(jointName, ct->
asA<ControlTarget1DoFActuatorVelocity>()));
25 positionSensors.insert(std::make_pair(jointName, sv->
asA<SensorValue1DoFActuatorPosition>()));
26 torqueSensors.insert(std::make_pair(jointName, sv->
asA<SensorValue1DoFActuatorTorque>()));
27 gravityTorqueSensors.insert(std::make_pair(jointName, sv->
asA<SensorValue1DoFGravityTorque>()));
28 velocitySensors.insert(std::make_pair(jointName, sv->
asA<SensorValue1DoFActuatorVelocity>()));
30 if (cfg->jointNames.size() == 0)
35 dmpPtr.reset(
new DMP::UMIDMP(cfg->kernelSize, cfg->DMPKd, cfg->baseMode, cfg->tau));
36 timeDuration = cfg->timeDuration;
37 canVal = timeDuration;
41 phaseDist0 = cfg->phaseDist0;
42 phaseDist1 = cfg->phaseDist1;
43 phaseKp = cfg->phaseKp;
45 isDisturbance =
false;
60 std::vector<double> currentPosition;
61 std::vector<double> currentVelocity;
62 for (
size_t i = 0; i < dimNames.size(); i++)
64 const auto& jointName = dimNames.at(i);
65 DMP::DMPState currentPos;
66 currentPos.pos = (positionSensors.count(jointName) == 1) ? positionSensors[jointName]->position : 0.0f;
67 currentPos.vel = (velocitySensors.count(jointName) == 1) ? velocitySensors[jointName]->velocity : 0.0f;
68 currentPos.vel *= timeDuration;
69 currentState.push_back(currentPos);
70 currentPosition.push_back(currentPos.pos);
71 currentVelocity.push_back(currentPos.vel);
73 error += pow(currentPos.pos - targetState[i], 2);
80 phaseDist = phaseDist1;
84 phaseDist = phaseDist0;
88 phaseStop = phaseL / (1 + exp(-phaseK * (error - phaseDist)));
89 mpcFactor = 1 - (phaseStop / phaseL);
98 isDisturbance =
false;
102 double deltaT = timeSinceLastIteration.toSecondsDouble();
103 canVal -= 1 / tau * deltaT * 1 / (1 + phaseStop);
104 double dmpDeltaT = deltaT / timeDuration;
105 dmpPtr->setTemporalFactor(tau);
107 currentState = dmpPtr->calculateDirectlyVelocity(currentState, canVal / timeDuration, dmpDeltaT, targetState);
114 for (
size_t i = 0; i < dimNames.size(); ++i)
116 const auto& jointName = dimNames.at(i);
117 if (targets.count(jointName) == 1)
119 double vel0 = currentState[i].vel / timeDuration;
120 double vel1 = phaseKp * (targetState[i] - currentPosition[i]);
121 double vel = mpcFactor * vel0 + (1 - mpcFactor) * vel1;
122 targets[jointName]->velocity = finished ? 0.0f : vel;
124 std::string targetVelstr = jointName +
"_targetvel";
125 std::string targetStatestr = jointName +
"_dmpTarget";
126 debugOutputData.
getWriteBuffer().latestTargetVelocities[jointName] = vel;
127 debugOutputData.
getWriteBuffer().dmpTargetState[jointName] = targetState[i];
138 for (
size_t i = 0; i < dimNames.size(); ++i)
140 const auto& jointName = dimNames.at(i);
141 if (targets.count(jointName) == 1)
143 targets[jointName]->velocity = 0.0f;
151 DMP::Vec<DMP::SampledTrajectoryV2 > trajs;
154 for (
size_t i = 0; i < fileNames.size(); ++i)
156 DMP::SampledTrajectoryV2 traj;
157 traj.readFromCSVFile(fileNames.at(i));
158 dimNames = traj.getDimensionNames();
160 trajs.push_back(traj);
164 ratios.push_back(1.0);
168 ratios.push_back(0.0);
171 dmpPtr->learnFromTrajectories(trajs);
172 dmpPtr->setOneStepMPC(
true);
173 dmpPtr->styleParas = dmpPtr->getStyleParasWithRatio(ratios);
180 currentState.clear();
182 for (
size_t i = 0; i < dimNames.size(); i++)
184 const auto& jointName = dimNames.at(i);
185 DMP::DMPState currentPos;
186 currentPos.pos = (positionSensors.count(jointName) == 1) ? positionSensors[jointName]->position : 0.0f;
187 currentPos.vel = (velocitySensors.count(jointName) == 1) ? velocitySensors[jointName]->velocity : 0.0f;
188 currentState.push_back(currentPos);
189 targetState.push_back(currentPos.pos);
191 dmpPtr->prepareExecution(goals, currentState, 1, tau);
232 datafields[pair.first] =
new Variant(pair.second);
236 for (
auto& pair : valuesst)
238 datafields[pair.first] =
new Variant(pair.second);
243 debugObs->setDebugChannel(
"latestDMPTargetVelocities", datafields);