19 kernelCenters = Eigen::VectorXd::Zero(numberOfKernels);
20 for(
int i=0; i<numberOfKernels-1; i++){
24 designMatrix = kernelFunction(kernelCenters);
25 designMatrixInv = designMatrix.inverse();
29 calculateRbfnWeights();
41 Eigen::MatrixXd dataMat = Eigen::MatrixXd::Zero(
nWaypoints,
nDoF+1);
43 dataMat.col(0) = kernelCenters;
50 Eigen::MatrixXd ExperimentalTrajectory::kernelFunction(
double m)
52 int nC = kernelCenters.rows();
53 int nS = maxCovariance.rows();
55 Eigen::VectorXd kern = ((-(m - kernelCenters.array()).array().square()) / kernelLengthParameter).array().exp();
57 Eigen::MatrixXd result = (maxCovariance.transpose().replicate(nC, 1)).array() * kern.replicate(1, nS).array();
59 Eigen::MatrixXd blockDiagonalResult = Eigen::MatrixXd::Zero(nS, (nS*nC));
61 for(
int i=0; i<result.rows(); i++)
63 blockDiagonalResult.block(0, i*nS, nS, nS) = result.row(i).asDiagonal();
66 return blockDiagonalResult;
70 Eigen::MatrixXd ExperimentalTrajectory::kernelFunction(Eigen::VectorXd& evalVec)
72 int sizeVec = evalVec.rows();
73 int nC = kernelCenters.rows();
74 int nS = maxCovariance.rows();
76 Eigen::MatrixXd blockDiagonalResult = Eigen::MatrixXd::Zero((nS*sizeVec), (nS*nC));
78 for(
int i=0; i<sizeVec; i++)
80 blockDiagonalResult.middleRows(i*nS, nS) = kernelFunction(evalVec(i));
83 return blockDiagonalResult;
88 Eigen::VectorXd ExperimentalTrajectory::rbfnKernelFunction(
double m)
90 return ((-(m - kernelCenters.array()).array().square()).array() / kernelLengthParameter).array().exp();
93 Eigen::MatrixXd ExperimentalTrajectory::rbfnKernelFunction(Eigen::VectorXd& evalVec)
95 int sizeVec = evalVec.rows();
96 int nC = kernelCenters.rows();
98 Eigen::MatrixXd centersMat = kernelCenters.transpose().replicate(sizeVec,1);
99 Eigen::MatrixXd evalMat = evalVec.replicate(1,nC);
101 return ((-(evalMat - centersMat).array().square()).array() / kernelLengthParameter).array().exp();
105 void ExperimentalTrajectory::calculateRbfnWeights()
107 int nC = kernelCenters.rows();
108 Eigen::MatrixXd rbfnDesignMatrix = rbfnKernelFunction(kernelCenters);
110 Eigen::MatrixXd wayTrans =
waypoints.transpose();
114 Eigen::MatrixXd A = rbfnDesignMatrix * rbfnDesignMatrix.transpose();
117 Eigen::MatrixXd b = rbfnDesignMatrix * wayTrans;
125 rbfnWeights = rbfnDesignMatrix.fullPivLu().solve(wayTrans).transpose();
130 Eigen::VectorXd ExperimentalTrajectory::getRbfnOutput(
double m)
132 Eigen::VectorXd phi = rbfnKernelFunction(m);
133 return rbfnWeights * phi;
136 Eigen::MatrixXd ExperimentalTrajectory::getRbfnOutput(Eigen::VectorXd& evalVec)
138 Eigen::MatrixXd phi = rbfnKernelFunction(evalVec);
139 return rbfnWeights * phi;
144 void ExperimentalTrajectory::precalculateTrajectory(
double DT, Eigen::MatrixXd& _path, Eigen::VectorXd& _timeline)
146 double currentTime = 0.0;
150 Eigen::MatrixXd path(numberOfDataPoints,
nDoF);
151 Eigen::VectorXd timeline(numberOfDataPoints);
160 timeline(index) = currentTime;
169 _path = path.topRows(index);
170 _timeline = timeline.head(index);
182 Eigen::MatrixXd desiredValue = Eigen::MatrixXd::Zero(
nDoF,
TRAJ_DIM);
204 double t = _time -
t0;
206 if (t<=kernelCenters.maxCoeff())
208 desiredValue.col(
POS_INDEX) = getRbfnOutput(t);
215 double delta_t = t-t_old;
240 Eigen::VectorXd variance;
243 Eigen::MatrixXd Ks = kernelFunction(t);
244 variance = maxCovariance - (Ks * designMatrixInv * Ks.transpose()).diagonal();
247 variance = Eigen::VectorXd::Zero(maxCovariance.rows());
272 int extraWaypoints = 1;
273 int nStartWp = extraWaypoints;
274 int nEndWp = extraWaypoints;
278 Eigen::MatrixXd waypointsTmp = Eigen::MatrixXd::Zero(
nDoF, nWpNew);
280 waypointsTmp.leftCols(nStartWp) =
waypoints.leftCols(1).replicate(1,nStartWp);
282 waypointsTmp.rightCols(nEndWp) =
waypoints.rightCols(1).replicate(1,nEndWp);
289 Eigen::VectorXd durationVectorTmp = Eigen::VectorXd::Zero(nWpNew-1);
291 double extraWpDt = 0.01 * kernelLengthParameter;
295 durationVectorTmp.head(nStartWp) = Eigen::VectorXd::Constant(nStartWp, extraWpDt);
297 durationVectorTmp.tail(nEndWp) = Eigen::VectorXd::Constant(nEndWp, extraWpDt);
311 return maxCovariance.maxCoeff();
313 void ExperimentalTrajectory::reinitialize()
329 std::cout <<
"Adding waypoint at: " << waypointTime <<
" seconds..." << std::endl;
331 if ( (newWaypoint.rows() ==
nDoF) && (waypointTime>=0.0) && (waypointTime<=kernelCenters.maxCoeff()) )
334 for (
int i=0; i<kernelCenters.size(); i++)
336 std::cout <<
"i = " << i << std::endl;
337 if (kernelCenters(i)>waypointTime) {
338 youngestWaypointIndex=i;
339 std::cout <<
"youngestWaypointIndex" << youngestWaypointIndex << std::endl;
340 i = kernelCenters.size();
345 Eigen::MatrixXd newWaypointsMat = Eigen::MatrixXd::Zero(
nDoF,
nWaypoints+1);
346 newWaypointsMat.leftCols(youngestWaypointIndex) =
waypoints.leftCols(youngestWaypointIndex);
347 newWaypointsMat.col(youngestWaypointIndex) = newWaypoint;
353 Eigen::VectorXd durationVectorTmp = Eigen::VectorXd::Zero(
nWaypoints);
358 durationVectorTmp.row(youngestWaypointIndex-1) << waypointTime - kernelCenters(youngestWaypointIndex-1);
372 if (newWaypoint.rows() !=
nDoF){
373 std::cout <<
"[ERROR] (ExperimentalTrajectory::addNewWaypoint): New waypoint is not the right size. Should have dim = " <<
nDoF <<
"x1." << std::endl;
375 if ((waypointTime<=0.0) || (waypointTime>=kernelCenters.maxCoeff())){
376 std::cout <<
"[ERROR] (ExperimentalTrajectory::addNewWaypoint): New waypoint time is out of time bounds. Should have fall between 0.0 and " << kernelCenters.maxCoeff() <<
" seconds." << std::endl;
390 std::cout <<
"Removing waypoint index: " << index <<
"..." << std::endl;
393 Eigen::MatrixXd newWaypointsMat = Eigen::MatrixXd::Zero(
nDoF,
nWaypoints-1);
395 newWaypointsMat.leftCols(index) =
waypoints.leftCols(index);
401 Eigen::VectorXd durationVectorTmp = Eigen::VectorXd::Zero(
nWaypoints-2);
403 int durVecIndex = index;
414 std::cout <<
"[ERROR] (ExperimentalTrajectory::addNewWaypoint): New waypoint time is out of index bounds. Should have fall between 0 and " <<
nWaypoints-1 <<
"." << std::endl;
424 int nC = kernelCenters.rows();
425 double startTime = kernelCenters(0);
426 double endTime = kernelCenters.tail(1)(0);
427 Eigen::VectorXd timeVec = Eigen::VectorXd::LinSpaced(nSteps, startTime, endTime);
429 Eigen::MatrixXd res(timeVec.rows(), nC+1);
430 res.col(0) = timeVec;
431 res.rightCols(nC) = rbfnKernelFunction(timeVec);
Eigen::VectorXd getVariance(double time)
Eigen::MatrixXd getWaypointData()
Eigen::MatrixXd waypoints
virtual void initializeTrajectory()
Eigen::VectorXd pointToPointDurationVector
Optimization-based Robot Controller namespace. a library of classes to write and solve optimization p...
void removeYoungestWaypoint()
Eigen::MatrixXd getDesiredValues(double time)
void calculateVarianceParameters()
void removeWaypoint(int index)
Eigen::MatrixXd getRbfnKernelCurves()
void addNewWaypoint(Eigen::VectorXd newWaypoint, double waypointTime)
bool varianceStartTrigger