3 #include <VirtualRobot/TimeOptimalTrajectory/TimeOptimalTrajectory.h>
6 #include <ArmarXCore/interface/observers/ObserverInterface.h>
14 NJointControllerRegistration<NJointTrajectoryController>
18 const NJointControllerConfigPtr& config,
21 cfg = NJointTrajectoryControllerConfigPtr::dynamicCast(config);
24 for (std::string jointName : cfg->jointNames)
28 targets.push_back(ct->
asA<ControlTarget1DoFActuatorVelocity>());
29 sensors.push_back(sv->
asA<SensorValue1DoFActuatorPosition>());
31 if (cfg->jointNames.size() == 0)
35 for (
auto& jointName : cfg->jointNames)
39 VirtualRobot::RobotNodePtr rn = robot->getRobotNode(jointName);
42 ls.enabled = rn->isLimitless();
43 ls.limitLo = rn->getJointLimitLo();
44 ls.limitHi = rn->getJointLimitHi();
46 ARMARX_DEBUG <<
"limitless status - " << jointName <<
": " << rn->isLimitless();
47 limitlessStates.push_back(ls);
58 return "NJointTrajectoryController";
70 plotter = getTopic<StaticPlotterInterfacePrx>(
"StaticPlotter");
71 dbgObs = getTopic<DebugObserverInterfacePrx>(
"DebugObserver");
82 const auto& dimNames = contr.
getTraj()->getDimensionNames();
83 for (
size_t i = 0; i < sensors.size(); i++)
87 currentPos(i) = sensors.at(i)->position;
91 contr.
update(timeSinceLastIteration.toSecondsDouble() * direction, currentPos);
103 (currentTimestamp >= contr.
getTraj()->rbegin()->getTimestamp() &&
105 (currentTimestamp <= contr.
getTraj()->begin()->getTimestamp() && direction == -1.0);
107 for (
size_t i = 0; i < targets.size(); ++i)
110 targets.at(i)->velocity =
111 (cfg->isPreview || finished) ? 0.0f : newVelocities(i);
115 if (finished && looping)
129 size_t trajectoryDimensions = trajectory->dim();
131 trajectory->setLimitless(limitlessStates);
133 if (cfg->considerConstraints)
135 std::list<Eigen::VectorXd> pathPoints;
136 double timeStep = cfg->preSamplingStepMs / 1000.0;
137 Eigen::VectorXd maxVelocities =
138 Eigen::VectorXd::Constant(trajectoryDimensions, cfg->maxVelocity);
139 Eigen::VectorXd maxAccelerations =
140 Eigen::VectorXd::Constant(trajectoryDimensions, cfg->maxAcceleration);
144 Eigen::VectorXd waypoint(trajectoryDimensions);
145 for (
size_t i = 0; i < trajectoryDimensions; ++i)
147 waypoint(i) = element.getPosition(i);
149 pathPoints.emplace_back(waypoint);
153 VirtualRobot::TimeOptimalTrajectory timeOptimalTraj(
154 p, maxVelocities, maxAccelerations, timeStep);
158 Ice::DoubleSeq newTimestamps;
159 double duration = timeOptimalTraj.getDuration();
160 for (
double t = 0.0; t < duration; t += timeStep)
162 newTimestamps.push_back(t);
164 newTimestamps.push_back(duration);
166 for (
size_t d = 0; d < trajectoryDimensions; d++)
176 Ice::DoubleSeq derivs;
177 for (
double t = 0.0; t < duration; t += timeStep)
180 derivs.push_back(timeOptimalTraj.getPosition(t)[d]);
181 derivs.push_back(timeOptimalTraj.getVelocity(t)[d]);
182 newTraj->addDerivationsToDimension(d, t, derivs);
185 derivs.push_back(timeOptimalTraj.getPosition(duration)[d]);
186 derivs.push_back(timeOptimalTraj.getVelocity(duration)[d]);
187 newTraj->addDerivationsToDimension(d, duration, derivs);
189 newTraj->setDimensionNames(trajectory->getDimensionNames());
190 newTraj->setLimitless(limitlessStates);
193 ARMARX_INFO <<
"Trajectory duration: " << newTraj->getTimeLength();
195 trajectory = newTraj;
198 trajEndTime = *trajectory->getTimestamps().rbegin() - *trajectory->getTimestamps().begin();
199 currentPos.resize(trajectory->getDimensionNames().size());
201 startTimestamp = *trajectory->getTimestamps().begin();
202 endTimestamp = *trajectory->getTimestamps().rbegin();
205 StringFloatSeqDict posData;
206 StringFloatSeqDict velData;
207 for (
size_t d = 0; d < trajectory->dim(); ++d)
209 auto positions = trajectory->getDimensionData(d, 0);
210 posData[trajectory->getDimensionName(d)] =
211 Ice::FloatSeq(positions.begin(), positions.end());
212 auto velocities = trajectory->getDimensionData(d, 1);
213 velData[trajectory->getDimensionName(d)] =
214 Ice::FloatSeq(velocities.begin(), velocities.end());
216 auto timestampsDouble = trajectory->getTimestamps();
217 Ice::FloatSeq timestamps(timestampsDouble.begin(), timestampsDouble.end());
218 plotter->addPlotWithTimestampVector(
"Positions", timestamps, posData);
219 plotter->addPlotWithTimestampVector(
"Velocities", timestamps, velData);
230 trajectoryCtrl.reset(
245 return currentTimestamp;
260 return trajectoryCtrl->getCurrentTimestamp();
271 this->looping = looping;
286 return TrajectoryPtr::dynamicCast(trajectoryCtrl->getTraj()->clone());
303 for (
auto&
target : targets)