3 #include <boost/archive/text_iarchive.hpp>
4 #include <boost/archive/text_oarchive.hpp>
13 NJointControllerRegistration<NJointJSDMPController>
19 return "NJointJSDMPController";
23 const armarx::NJointControllerConfigPtr& config,
26 ARMARX_INFO <<
"creating joint space dmp controller ... ";
29 cfg = NJointJointSpaceDMPControllerConfigPtr::dynamicCast(config);
32 for (std::string jointName : cfg->jointNames)
36 targets.insert(std::make_pair(jointName, ct->
asA<ControlTarget1DoFActuatorVelocity>()));
37 positionSensors.insert(
38 std::make_pair(jointName, sv->
asA<SensorValue1DoFActuatorPosition>()));
39 velocitySensors.insert(
40 std::make_pair(jointName, sv->
asA<SensorValue1DoFActuatorVelocity>()));
42 if (cfg->jointNames.size() == 0)
47 <<
" baseMode: " << cfg->baseMode;
49 dmpPtr.reset(
new DMP::UMIDMP(cfg->kernelSize, 100, cfg->baseMode, 1));
50 timeDuration = cfg->timeDuration;
53 phaseDist0 = cfg->phaseDist0;
54 phaseDist1 = cfg->phaseDist1;
55 phaseKp = cfg->phaseKp;
56 dimNames = cfg->jointNames;
59 targetVels.resize(cfg->jointNames.size());
62 for (
size_t i = 0; i < cfg->jointNames.size(); ++i)
71 NJointJSDMPControllerSensorData initSensorData;
72 initSensorData.currentTime = 0;
73 initSensorData.deltaT = 0;
74 initSensorData.currentState.resize(cfg->jointNames.size());
79 qpos.resize(dimNames.size());
80 qvel.resize(dimNames.size());
86 if (!started || finished)
88 for (
size_t i = 0; i < dimNames.size(); ++i)
100 double phaseStop = 0;
101 double mpcFactor = 1;
103 std::vector<double> currentPosition;
105 currentPosition.resize(dimNames.size());
107 for (
size_t i = 0; i < currentState.size(); i++)
109 DMP::DMPState currentPos = currentState[i];
110 currentPosition[i] = currentPos.pos;
111 error += pow(currentPos.pos - targetState[i], 2);
114 if (cfg->isPhaseStop)
120 phaseDist = phaseDist1;
124 phaseDist = phaseDist0;
128 phaseStop = phaseL / (1 + exp(-phaseK * (error - phaseDist)));
129 mpcFactor = 1 - (phaseStop / phaseL);
133 isDisturbance =
true;
138 isDisturbance =
false;
142 canVal -= tau * deltaT * 1 / (1 + phaseStop);
143 double dmpDeltaT = deltaT / timeDuration;
145 currentDMPState = dmpPtr->calculateDirectlyVelocity(
146 currentDMPState, canVal / timeDuration, dmpDeltaT, targetState);
148 for (
size_t i = 0; i < currentDMPState.size(); ++i)
150 double vel0 = tau * currentDMPState[i].vel / timeDuration;
151 double vel1 = phaseKp * (targetState[i] - currentPosition[i]);
153 double vel = vel1 + vel0;
155 debugOutputData.
getWriteBuffer().latestTargetVelocities[dimNames[i]] =
158 (
float)currentDMPState[i].pos;
168 for (
size_t i = 0; i < dimNames.size(); ++i)
184 for (
size_t i = 0; i < dimNames.size(); i++)
186 const auto& jointName = dimNames.at(i);
187 DMP::DMPState currentPos;
188 currentPos.pos = (positionSensors.count(jointName) == 1)
189 ? positionSensors[jointName]->position
191 currentPos.vel = (velocitySensors.count(jointName) == 1)
192 ? velocitySensors[jointName]->velocity
194 qpos[i] = currentPos.pos;
195 qvel[i] = currentPos.vel;
196 controllerSensorData.
getWriteBuffer().currentState[i] = currentPos;
198 controllerSensorData.
getWriteBuffer().deltaT = timeSinceLastIteration.toSecondsDouble();
200 timeSinceLastIteration.toSecondsDouble();
211 for (
size_t i = 0; i < dimNames.size(); ++i)
214 if (fabs(targetJointVels[i]) > cfg->maxJointVel)
216 targets[dimNames[i]]->velocity =
217 targetJointVels[i] < 0 ? -cfg->maxJointVel : cfg->maxJointVel;
221 targets[dimNames[i]]->velocity = targetJointVels[i];
229 DMP::Vec<DMP::SampledTrajectoryV2> trajs;
232 for (
size_t i = 0; i < fileNames.size(); ++i)
234 DMP::SampledTrajectoryV2 traj;
235 traj.readFromCSVFile(fileNames.at(i));
237 trajs.push_back(traj);
241 ratios.push_back(1.0);
245 ratios.push_back(0.0);
248 dmpPtr->learnFromTrajectories(trajs);
249 dmpPtr->styleParas = dmpPtr->getStyleParasWithRatio(ratios);
263 targetState.resize(dimNames.size());
264 currentState.clear();
265 currentState.resize(dimNames.size());
266 currentDMPState.clear();
267 currentDMPState.resize(dimNames.size());
269 std::vector<double> goalVec = goals;
270 for (
size_t i = 0; i < dimNames.size(); i++)
272 DMP::DMPState currentPos;
276 currentState[i] = currentPos;
277 currentDMPState[i] = currentPos;
279 targetState.push_back(currentPos.pos);
281 if (
rtGetRobot()->getRobotNode(dimNames[i])->isLimitless())
283 double tjv = goalVec[i];
284 double cjv = currentPos.pos;
285 double diff = std::fmod(tjv - cjv, 2 *
M_PI);
286 if (fabs(diff) >
M_PI)
290 diff = -2 *
M_PI - diff;
294 diff = 2 *
M_PI - diff;
305 <<
" current state: qpos: " << currentPos.pos
306 <<
" orig target: " << goals[i] <<
" current goal: " << tjv;
310 dmpPtr->prepareExecution(goalVec, currentDMPState, 1, 1);
311 canVal = timeDuration;
313 isDisturbance =
false;
328 std::stringstream ss;
329 boost::archive::text_oarchive oa{ss};
337 std::stringstream ss;
339 boost::archive::text_iarchive ia{ss};
340 DMP::UMIDMP* newDmpPtr;
342 dmpPtr.reset(newDmpPtr);
343 return dmpPtr->defaultGoal;
350 dmpPtr->setViaPoint(u, viapoint);
356 dmpPtr->setWeights(weights);
362 DMP::DVec2d res = dmpPtr->getWeights();
364 for (
size_t i = 0; i < res.size(); ++i)
366 std::vector<double> cvec;
367 for (
size_t j = 0; j < res[i].size(); ++j)
369 cvec.push_back(res[i][j]);
371 resvec.push_back(cvec);
403 datafields[pair.first] =
new Variant(pair.second);
409 datafields[pair.first +
"_pos"] =
new Variant(pair.second);
414 debugObs->setDebugChannel(
"latestDMPTargetVelocities", datafields);
423 runTask(
"NJointJSDMPController",
428 while (
getState() == eManagedIceObjectStarted)
434 c.waitForCycleDuration();