26#include <Ice/ObjectAdapter.h>
28#include <dmp/io/MMMConverter.h>
33DMP::Vec<DMP::DMPState>
36 DMP::Vec<DMP::DMPState> res;
37 res.resize(state.size());
39 for (
size_t i = 0; i < state.size(); i++)
41 res[i].pos = state[i].pos;
42 res[i].vel = state[i].vel;
52 res.resize(state.size());
53 for (
size_t i = 0; i < state.size(); i++)
55 res[i].push_back(state[i].pos);
56 res[i].push_back(state[i].vel);
66 sv.resize(dmpstate.size());
68 for (
size_t i = 0; i < dmpstate.size(); i++)
70 sv[i].pos = dmpstate[i].pos;
71 sv[i].vel = dmpstate[i].vel;
81 sv.resize(dmpstate.size());
83 for (
size_t i = 0; i < dmpstate.size(); i++)
85 sv[i].pos = dmpstate[i].at(0);
86 sv[i].vel = dmpstate[i].at(1);
107 ARMARX_WARNING <<
"Canonical value is not specified. It will be set 1.0.";
112 if (currentDMPState.size() == 0)
114 ARMARX_ERROR <<
"The current state is not available. Please specify current state with "
118 DMP::DVec goal =
dmp->getGoals();
123 double temporalFactor =
dmp->getTemporalFactor();
124 currentDMPState =
dmp->calculateTrajectoryPointBase(
137 const Ice::DoubleSeq& goal,
139 const Vec2D& currentStates,
141 double temporalFactor,
142 const ::Ice::Current&)
150 if (currentStates.size() == 0)
152 ARMARX_ERROR <<
"Current DMP State has to be specified";
155 DMP::DVec goal_v = DMP::DVec(goal);
157 if (goal.size() == 0)
163 DMP::DVec2d curStates;
165 for (std::size_t i = 0; i < currentStates.size(); ++i)
167 curStates.emplace_back(DMP::DVec(currentStates.at(i)));
172 DMP::DVec2d tmp_result =
dmp->calculateTrajectoryPointBase(
173 t, goal_v, currentT, curStates, canValues, temporalFactor);
175 Ice::DoubleSeq tmpCanVal;
176 for (std::size_t i = 0; i < canValues.size(); i++)
178 tmpCanVal.emplace_back(canValues.at(i));
183 result.canonicalValues = tmpCanVal;
186 for (std::size_t i = 0; i < tmp_result.size(); ++i)
188 ::Ice::DoubleSeq dblseq = ::Ice::DoubleSeq(tmp_result.at(i));
190 nState.emplace_back(dblseq);
193 result.nextState = nState;
201 const Vec2D& currentStates,
214 std::map<std::string, paraType>
configMap;
215 for (::std::map<::std::string, ::Ice::DoubleSeq>::const_iterator it = conf.begin();
219 configMap[it->first] = (DMP::DVec)it->second;
220 dmp->setConfiguration(it->first, it->second);
229 if (paraIDs.size() != paraVals.size())
231 ARMARX_WARNING <<
"ID list and value list have different sizes, which may cause error.";
235 for (
size_t i = 0; i < paraIDs.size(); i++)
243 configs[paraIDs[i]] = paraVals[i];
252 const Ice::DoubleSeq& value,
253 const ::Ice::Current&)
263 dmp->setConfiguration(paraID, DMP::DVec(value));
277 dmp->setAmpl(dim, value);
283 dmp->setTemporalFactor(value);
295 configs[DMP::Goal] = DMP::DVec(value);
297 dmp->setConfiguration(DMP::Goal, DMP::DVec(value));
309 configs[DMP::StartPosition] = DMP::DVec(value);
311 dmp->setConfiguration(DMP::StartPosition, DMP::DVec(value));
325 return dmp->getAmpl(dim);
331 return dmp->getTemporalFactor();
337 return dmp->_getPerturbationForce(dim, canVal[0]);
343 DMP::DVec goals =
dmp->getGoals();
346 for (
size_t i = 0; i < goals.size(); ++i)
348 res.push_back(goals.at(i));
359 Ice::DoubleSeq trajGoal;
360 for (
size_t i = 0; i <
trajs[0].dim(); i++)
362 trajGoal.push_back(
trajs[0].rbegin()->getPosition(i));
371 cStateVec startState;
373 for (
size_t i = 0; i <
trajs[0].dim(); i++)
376 state.pos =
trajs[0].begin()->getPosition(i);
377 state.vel =
trajs[0].begin()->getDeriv(i, 1);
378 state.acc =
trajs[0].begin()->getDeriv(i, 2);
380 startState.push_back(state);
389 std::string ext = file.rfind(
".") == file.npos ? file : file.substr(file.rfind(
".") + 1);
397 else if (ext ==
"csv")
399 DMP::SampledTrajectoryV2 traj;
400 traj.readFromCSVFile(file);
401 traj.gaussianFilter(0.01);
403 double startTime = 0;
404 double endTime = times;
405 traj = DMP::SampledTrajectoryV2::normalizeTimestamps(traj, startTime, endTime);
408 trajs.push_back(traj);
409 dmp->setDim(traj.dim());
413 else if (ext ==
"vsg")
433 const ::Ice::DoubleSeq& goal,
434 const cStateVec& states,
436 double temporalFactor,
446 Vec2D resultingTrajectory;
448 cStateVec nextStates = states;
449 while (
ctime < endTime)
452 ::Ice::DoubleSeq positions;
454 positions.push_back(
ctime);
455 for (
unsigned int d = 0; d < states.size(); d++)
457 positions.push_back(nextStates.at(d).pos);
460 resultingTrajectory.push_back(positions);
465 return resultingTrajectory;
470 const Ice::DoubleSeq& goal,
471 const Vec2D& initStates,
473 double temporalFactor,
476 DMP::DVec timestampsLocal = timestamps;
478 if (timestampsLocal.size() == 0)
483 DMP::DVec::const_iterator it = timestamps.begin();
484 Vec2D previousState = initStates;
485 Vec2D resultingTrajectory;
489 double prevTimestamp = *it;
493 for (; it != timestamps.end(); it++)
495 nStateValues nState =
496 calcNextState(*it, goal, prevTimestamp, previousState, canValues, temporalFactor);
497 canValues = nState.canonicalValues;
498 previousState = nState.nextState;
501 ::Ice::DoubleSeq positions;
502 for (
unsigned int d = 0; d < previousState.size(); d++)
504 positions.push_back(previousState.at(d)[0]);
506 resultingTrajectory.push_back(positions);
509 return resultingTrajectory;
514 const Vec2D& initStates,
DMP::DVec2d getDVec2dStateFromcStateVec(const cStateVec &state)
DMP::Vec< DMP::DMPState > getDMPStateFromcStateVec(const cStateVec &state)
armarx::cStateVec getcStateVec(const DMP::Vec< DMP::DMPState > &dmpstate)
void trainDMP(const ::Ice::Current &=Ice::emptyCurrent) override
void setAmpl(int dim, double value, const ::Ice::Current &=Ice::emptyCurrent) override
setAmpl: set amplitude for dmp
Vec2D calcTrajectoryFromConfig(const Ice::DoubleSeq ×tamps, const Vec2D &initStates, const Ice::DoubleSeq &canonicalValues, const Ice::Current &=Ice::emptyCurrent) override
void setTimeStep(double ts)
void setConfigurationMap(const DMPConfigMap &value, const ::Ice::Current &=Ice::emptyCurrent) override
setConfigurationMap set dmp configurations using configMap
Ice::DoubleSeq getGoal(const Ice::Current &=Ice::emptyCurrent)
void setDMPState(const cStateVec &state, const ::Ice::Current &=Ice::emptyCurrent) override
setDMPState set current state for dmp
double getAmpl(int dim, const ::Ice::Current &=Ice::emptyCurrent) override
getAmpl: get current amplitude of a dimension
Ice::DoubleSeq getTrajGoal(const ::Ice::Current &=Ice::emptyCurrent) override
getTrajGoal: get the goal of the training trajectory
void setParameter(const std::string ¶Id, const Ice::DoubleSeq &value, const ::Ice::Current &=Ice::emptyCurrent) override
setParameter set parameters for
void setCanonicalValues(const Ice::DoubleSeq &value, const ::Ice::Current &=Ice::emptyCurrent) override
setCanonicalValues: set dmp canonicalValues to control the dmp's state
double getForceTerm(const Ice::DoubleSeq &canVal, int dim, const ::Ice::Current &=Ice::emptyCurrent) override
getForceTerm: get force termf for one dimension at a specified canonical values
Vec2D calcTrajectory(double startTime, double timestep, double endTime, const ::Ice::DoubleSeq &goal, const cStateVec &initStates, const ::Ice::DoubleSeq &canonicalValues, double temporalFactor, const Ice::Current &=Ice::emptyCurrent) override
calcTrajectory solves the DMP for all Timesteps
nStateValues calcNextStateFromConfig(double t, double currentT, const Vec2D ¤tStates, const Ice::DoubleSeq &canonicalValues, const Ice::Current &=Ice::emptyCurrent) override
void setCurrentTime(double t)
Vec2D calcTrajectoryV2(const Ice::DoubleSeq ×tamps, const Ice::DoubleSeq &goal, const Vec2D &initStates, const Ice::DoubleSeq &canonicalValues, double temporalFactor, const Ice::Current &=Ice::emptyCurrent) override
void setGoal(const Ice::DoubleSeq &value, const ::Ice::Current &=Ice::emptyCurrent) override
setGoal set the goal for dmp
cStateVec getTrajStartState(const ::Ice::Current &=Ice::emptyCurrent) override
getTrajStartState: get the start state of the training trajectory
nStateValues calcNextState(double t, const Ice::DoubleSeq &goal, double currentT, const Vec2D ¤tStates, const Ice::DoubleSeq &canonicalValues, double temporalFactor, const ::Ice::Current &=Ice::emptyCurrent) override
void setTemporalFactor(double value, const ::Ice::Current &=Ice::emptyCurrent) override
setTemporalFactor: set dmp temporal factor
configMap createConfigMap(DMP::Vec< std::string > paraIDs, DMP::Vec< paraType > paraVals)
void setStartPosition(const Ice::DoubleSeq &value, const ::Ice::Current &=Ice::emptyCurrent) override
setStartPosition set the start position for dmp
DMPInstance()
DMPInstance constructs an empty instance of a basicDMP.
double getTemporalFactor(const ::Ice::Current &=Ice::emptyCurrent) override
getTemporalFactor: get current temporal factor
cStateVec getNextState(const cStateVec &states, const ::Ice::Current &=Ice::emptyCurrent) override
getNextState: get next state according to the state given
DMP::DVec canonicalValues
void readTrajectoryFromFile(const std::string &file, double times=1, const ::Ice::Current &=Ice::emptyCurrent) override
#define ARMARX_INFO
The normal logging level.
#define ARMARX_ERROR
The logging level for unexpected behaviour, that must be fixed.
#define ARMARX_WARNING
The logging level for unexpected behaviour, but not a serious problem.
This file offers overloads of toIce() and fromIce() functions for STL container types.
std::pair< std::string, paraType > configPair
std::map< std::string, paraType > configMap