NJointJointSpaceDMPController.cpp
Go to the documentation of this file.
2 
3 
4 
6 {
7 
9 
10  std::string NJointJointSpaceDMPController::getClassName(const Ice::Current&) const
11  {
12  return "NJointJointSpaceDMPController";
13  }
14 
15  NJointJointSpaceDMPController::NJointJointSpaceDMPController(armarx::RobotUnitPtr prov, const armarx::NJointControllerConfigPtr& config, const VirtualRobot::RobotPtr&)
16  {
17  NJointJointSpaceDMPControllerConfigPtr cfg = NJointJointSpaceDMPControllerConfigPtr::dynamicCast(config);
18  ARMARX_CHECK_EXPRESSION_W_HINT(cfg, "Needed type: NJointJointSpaceDMPControllerConfigPtr");
19 
20  for (std::string jointName : cfg->jointNames)
21  {
22  ControlTargetBase* ct = useControlTarget(jointName, ControlModes::Velocity1DoF);
23  const SensorValueBase* sv = useSensorValue(jointName);
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>()));
29  }
30  if (cfg->jointNames.size() == 0)
31  {
32  ARMARX_ERROR << "cfg->jointNames.size() == 0";
33  }
34 
35  dmpPtr.reset(new DMP::UMIDMP(cfg->kernelSize, cfg->DMPKd, cfg->baseMode, cfg->tau));
36  timeDuration = cfg->timeDuration;
37  canVal = timeDuration;
38  finished = false;
39  phaseL = cfg->phaseL;
40  phaseK = cfg->phaseK;
41  phaseDist0 = cfg->phaseDist0;
42  phaseDist1 = cfg->phaseDist1;
43  phaseKp = cfg->phaseKp;
44 
45  isDisturbance = false;
46 
48  initData.tau = 1.0;
49  initData.isStart = false;
50  reinitTripleBuffer(initData);
51  }
52 
53  void NJointJointSpaceDMPController::rtRun(const IceUtil::Time& sensorValuesTimestamp, const IceUtil::Time& timeSinceLastIteration)
54  {
55  if (rtGetControlStruct().isStart && !finished)
56  {
57  currentState.clear();
58  double phaseStop = 0;
59  double error = 0;
60  std::vector<double> currentPosition;
61  std::vector<double> currentVelocity;
62  for (size_t i = 0; i < dimNames.size(); i++)
63  {
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);
72 
73  error += pow(currentPos.pos - targetState[i], 2);
74  }
75 
76  double phaseDist;
77 
78  if (isDisturbance)
79  {
80  phaseDist = phaseDist1;
81  }
82  else
83  {
84  phaseDist = phaseDist0;
85  }
86 
87  error = sqrt(error);
88  phaseStop = phaseL / (1 + exp(-phaseK * (error - phaseDist)));
89  mpcFactor = 1 - (phaseStop / phaseL);
90 
91  if (mpcFactor < 0.1)
92  {
93  isDisturbance = true;
94  }
95 
96  if (mpcFactor > 0.9)
97  {
98  isDisturbance = false;
99  }
100 
101  double tau = rtGetControlStruct().tau;
102  double deltaT = timeSinceLastIteration.toSecondsDouble();
103  canVal -= 1 / tau * deltaT * 1 / (1 + phaseStop);
104  double dmpDeltaT = deltaT / timeDuration;
105  dmpPtr->setTemporalFactor(tau);
106 
107  currentState = dmpPtr->calculateDirectlyVelocity(currentState, canVal / timeDuration, dmpDeltaT, targetState);
108 
109  if (canVal < 1e-8)
110  {
111  finished = true;
112  }
113 
114  for (size_t i = 0; i < dimNames.size(); ++i)
115  {
116  const auto& jointName = dimNames.at(i);
117  if (targets.count(jointName) == 1)
118  {
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;
123 
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];
128 
129  }
130  }
131 
132  debugOutputData.getWriteBuffer().currentCanVal = canVal;
133  debugOutputData.getWriteBuffer().mpcFactor = mpcFactor;
134  debugOutputData.commitWrite();
135  }
136  else
137  {
138  for (size_t i = 0; i < dimNames.size(); ++i)
139  {
140  const auto& jointName = dimNames.at(i);
141  if (targets.count(jointName) == 1)
142  {
143  targets[jointName]->velocity = 0.0f;
144  }
145  }
146  }
147  }
148 
149  void NJointJointSpaceDMPController::learnDMPFromFiles(const Ice::StringSeq& fileNames, const Ice::Current&)
150  {
151  DMP::Vec<DMP::SampledTrajectoryV2 > trajs;
152 
153  DMP::DVec ratios;
154  for (size_t i = 0; i < fileNames.size(); ++i)
155  {
156  DMP::SampledTrajectoryV2 traj;
157  traj.readFromCSVFile(fileNames.at(i));
158  dimNames = traj.getDimensionNames();
160  trajs.push_back(traj);
161 
162  if (i == 0)
163  {
164  ratios.push_back(1.0);
165  }
166  else
167  {
168  ratios.push_back(0.0);
169  }
170  }
171  dmpPtr->learnFromTrajectories(trajs);
172  dmpPtr->setOneStepMPC(true);
173  dmpPtr->styleParas = dmpPtr->getStyleParasWithRatio(ratios);
174 
175  ARMARX_INFO << "Learned DMP ... ";
176  }
177 
178  void NJointJointSpaceDMPController::runDMP(const Ice::DoubleSeq& goals, double tau, const Ice::Current&)
179  {
180  currentState.clear();
181  targetState.clear();
182  for (size_t i = 0; i < dimNames.size(); i++)
183  {
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);
190  }
191  dmpPtr->prepareExecution(goals, currentState, 1, tau);
192  finished = false;
193 
194  this->goals = goals;
195  this->tau = tau;
196 
198  getWriterControlStruct().tau = tau;
199  getWriterControlStruct().isStart = true;
201 
202  }
203 
205  {
206  }
207 
208  void NJointJointSpaceDMPController::setTemporalFactor(double tau, const Ice::Current&)
209  {
210  this->tau = tau;
212  getWriterControlStruct().tau = tau;
213  getWriterControlStruct().isStart = true;
215  }
216 
218  {
219  }
220 
222  {
223 
224  }
225 
227  {
228  StringVariantBaseMap datafields;
229  auto values = debugOutputData.getUpToDateReadBuffer().latestTargetVelocities;
230  for (auto& pair : values)
231  {
232  datafields[pair.first] = new Variant(pair.second);
233  }
234 
235  auto valuesst = debugOutputData.getUpToDateReadBuffer().dmpTargetState;
236  for (auto& pair : valuesst)
237  {
238  datafields[pair.first] = new Variant(pair.second);
239  }
240 
241  datafields["canVal"] = new Variant(debugOutputData.getUpToDateReadBuffer().currentCanVal);
242  datafields["mpcFactor"] = new Variant(debugOutputData.getUpToDateReadBuffer().mpcFactor);
243  debugObs->setDebugChannel("latestDMPTargetVelocities", datafields);
244  }
245 
246 
247 }
NJointJointSpaceDMPController.h
armarx::NJointControllerWithTripleBuffer< NJointJointSpaceDMPControllerControlData >::reinitTripleBuffer
void reinitTripleBuffer(const NJointJointSpaceDMPControllerControlData &initial)
Definition: NJointControllerWithTripleBuffer.h:68
GfxTL::sqrt
VectorXD< D, T > sqrt(const VectorXD< D, T > &a)
Definition: VectorXD.h:662
armarx::control::deprecated_njoint_mp_controller::joint_space::NJointJointSpaceDMPController::onPublish
virtual void onPublish(const SensorAndControl &, const DebugDrawerInterfacePrx &, const DebugObserverInterfacePrx &) override
Definition: NJointJointSpaceDMPController.cpp:226
armarx::ControlTargetBase::asA
const T * asA() const
Definition: ControlTargetBase.h:76
armarx::Variant
The Variant class is described here: Variants.
Definition: Variant.h:224
armarx::control::deprecated_njoint_mp_controller::joint_space::NJointJointSpaceDMPController::setTemporalFactor
void setTemporalFactor(double tau, const Ice::Current &) override
Definition: NJointJointSpaceDMPController.cpp:208
armarx::TripleBuffer::getWriteBuffer
T & getWriteBuffer()
Definition: TripleBuffer.h:90
armarx::control::deprecated_njoint_mp_controller::joint_space::NJointJointSpaceDMPController::NJointJointSpaceDMPController
NJointJointSpaceDMPController(RobotUnitPtr prov, const NJointControllerConfigPtr &config, const VirtualRobot::RobotPtr &)
Definition: NJointJointSpaceDMPController.cpp:15
armarx::NJointControllerRegistration
Definition: NJointControllerRegistry.h:74
armarx::NJointControllerWithTripleBuffer< NJointJointSpaceDMPControllerControlData >::rtGetControlStruct
const NJointJointSpaceDMPControllerControlData & rtGetControlStruct() const
Definition: NJointControllerWithTripleBuffer.h:32
armarx::StringVariantBaseMap
std::map< std::string, VariantBasePtr > StringVariantBaseMap
Definition: ManagedIceObject.h:111
armarx::SensorValueBase::asA
const T * asA() const
Definition: SensorValueBase.h:82
armarx::SensorValueBase
The SensorValueBase class.
Definition: SensorValueBase.h:40
armarx::ControlTargetBase
Brief description of class JointControlTargetBase.
Definition: ControlTargetBase.h:47
armarx::control::deprecated_njoint_mp_controller::joint_space::NJointJointSpaceDMPControllerControlData::isStart
bool isStart
Definition: NJointJointSpaceDMPController.h:26
armarx::TripleBuffer::commitWrite
void commitWrite()
Definition: TripleBuffer.h:146
armarx::NJointControllerBase::useControlTarget
ControlTargetBase * useControlTarget(const std::string &deviceName, const std::string &controlMode)
Declares to calculate the ControlTarget for the given ControlDevice in the given ControlMode when rtR...
Definition: NJointController.cpp:410
armarx::control::deprecated_njoint_mp_controller::joint_space::NJointJointSpaceDMPController::learnDMPFromFiles
void learnDMPFromFiles(const Ice::StringSeq &fileNames, const Ice::Current &) override
Definition: NJointJointSpaceDMPController.cpp:149
ProsthesisInterface.values
values
Definition: ProsthesisInterface.py:190
armarx::control::deprecated_njoint_mp_controller::joint_space::registrationControllerNJointJointSpaceDMPController
NJointControllerRegistration< NJointJointSpaceDMPController > registrationControllerNJointJointSpaceDMPController("NJointJointSpaceDMPController")
armarx::NJointControllerWithTripleBuffer< NJointJointSpaceDMPControllerControlData >::getWriterControlStruct
NJointJointSpaceDMPControllerControlData & getWriterControlStruct()
Definition: NJointControllerWithTripleBuffer.h:54
armarx::control::deprecated_njoint_mp_controller::joint_space::NJointJointSpaceDMPControllerControlData::tau
double tau
Definition: NJointJointSpaceDMPController.h:25
armarx::NJointControllerWithTripleBuffer< NJointJointSpaceDMPControllerControlData >::LockGuardType
std::lock_guard< std::recursive_mutex > LockGuardType
Definition: NJointControllerWithTripleBuffer.h:14
armarx::detail::ControlThreadOutputBufferEntry
Definition: ControlThreadOutputBuffer.h:177
armarx::control::deprecated_njoint_mp_controller::joint_space::NJointJointSpaceDMPControllerControlData
Definition: NJointJointSpaceDMPController.h:22
armarx::NJointJointSpaceDMPControllerInterface::showMessages
void showMessages()
normalizeTimestamps
TrajectoryPtr normalizeTimestamps(TrajectoryPtr t, float maxTransVel, float maxRotVel)
Definition: PlayPlatformTrajectory.cpp:76
armarx::control::common::mp::DVec
Ice::DoubleSeq DVec
Definition: MP.h:46
armarx::NJointControllerWithTripleBuffer< NJointJointSpaceDMPControllerControlData >::writeControlStruct
void writeControlStruct()
Definition: NJointControllerWithTripleBuffer.h:44
armarx::control::deprecated_njoint_mp_controller::joint_space
Definition: NJointJointSpaceDMPController.cpp:5
armarx::NJointControllerWithTripleBuffer< NJointJointSpaceDMPControllerControlData >::controlDataMutex
MutexType controlDataMutex
Definition: NJointControllerWithTripleBuffer.h:73
ARMARX_ERROR
#define ARMARX_ERROR
Definition: Logging.h:189
armarx::armem::Time
armarx::core::time::DateTime Time
Definition: forward_declarations.h:13
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:174
armarx::control::deprecated_njoint_mp_controller::joint_space::NJointJointSpaceDMPController::runDMP
void runDMP(const Ice::DoubleSeq &goals, double tau, const Ice::Current &) override
Definition: NJointJointSpaceDMPController.cpp:178
IceInternal::ProxyHandle<::IceProxy::armarx::DebugDrawerInterface >
armarx::control::deprecated_njoint_mp_controller::joint_space::NJointJointSpaceDMPController::getClassName
std::string getClassName(const Ice::Current &) const override
Definition: NJointJointSpaceDMPController.cpp:10
armarx::TripleBuffer::getUpToDateReadBuffer
const T & getUpToDateReadBuffer() const
Definition: TripleBuffer.h:108
armarx::control::deprecated_njoint_mp_controller::joint_space::NJointJointSpaceDMPController::rtPostDeactivateController
void rtPostDeactivateController() override
This function is called after the controller is deactivated.
Definition: NJointJointSpaceDMPController.cpp:221
armarx::NJointControllerBase::useSensorValue
const SensorValueBase * useSensorValue(const std::string &sensorDeviceName) const
Get a const ptr to the given SensorDevice's SensorValue.
Definition: NJointController.cpp:383
VirtualRobot::RobotPtr
std::shared_ptr< class Robot > RobotPtr
Definition: Bus.h:18
armarx::control::deprecated_njoint_mp_controller::joint_space::NJointJointSpaceDMPController::rtRun
void rtRun(const IceUtil::Time &sensorValuesTimestamp, const IceUtil::Time &timeSinceLastIteration) override
TODO make protected and use attorneys.
Definition: NJointJointSpaceDMPController.cpp:53
armarx::control::deprecated_njoint_mp_controller::joint_space::NJointJointSpaceDMPController::rtPreActivateController
void rtPreActivateController() override
This function is called before the controller is activated.
Definition: NJointJointSpaceDMPController.cpp:217