34 #include <boost/algorithm/string.hpp> 
   40 #include <moveit_msgs/MotionPlanRequest.h> 
   42 #include <trajopt/trajectory_costs.hpp> 
   43 #include <trajopt_sco/expr_op_overloads.hpp> 
   44 #include <trajopt_sco/expr_ops.hpp> 
   45 #include <trajopt_utils/eigen_slicing.hpp> 
   46 #include <trajopt_utils/logging.hpp> 
   47 #include <trajopt_utils/vector_ops.hpp> 
   60                         const bool& apply_first = 
true)
 
   62   if (apply_first == 
true && parameter.size() == 1)
 
   64     parameter = trajopt::DblVec(expected_size, parameter[0]);
 
   65     ROS_INFO(
"1 %s given. Applying to all %i joints", 
name.c_str(), expected_size);
 
   67   else if (parameter.size() != expected_size)
 
   69     PRINT_AND_THROW(boost::format(
"wrong number of %s. expected %i got %i") % 
name % expected_size % parameter.size());
 
   80   : OptProb(problem_info.basic_info.convex_solver)
 
   82   , planning_group_(problem_info.planning_group_name)
 
   84   moveit::core::RobotModelConstPtr robot_model = planning_scene_->getRobotModel();
 
   93   ROS_INFO(
" ======================================= problem_description: limits");
 
   94   Eigen::MatrixX2d limits(dof_, 2);
 
   95   for (
int k = 0; k < limits.size() / 2; ++k)
 
  107   Eigen::VectorXd lower, upper;
 
  108   lower = limits.col(0);
 
  109   upper = limits.col(1);
 
  111   trajopt::DblVec vlower, vupper;
 
  112   std::vector<std::string> names;
 
  113   for (
int i = 0; i < n_steps; ++i)
 
  115     for (
int j = 0; j < dof_; ++j)
 
  117       names.push_back((boost::format(
"j_%i_%i") % i % j).str());
 
  119     vlower.insert(vlower.end(), lower.data(), lower.data() + lower.size());
 
  120     vupper.insert(vupper.end(), upper.data(), upper.data() + upper.size());
 
  126       names.push_back((boost::format(
"dt_%i") % i).str());
 
  130   sco::VarVector trajvarvec = createVariables(names, vlower, vupper);
 
  131   matrix_traj_vars = trajopt::VarArray(n_steps, dof_ + (problem_info.
basic_info.
use_time ? 1 : 0), trajvarvec.data());
 
  143   bool use_time = 
false;
 
  147     if (cost->term_type & 
TT_CNT)
 
  148       ROS_WARN(
"%s is listed as a type TT_CNT but was added to cost_infos", (cost->name).c_str());
 
  149     if (!(cost->getSupportedTypes() & 
TT_COST))
 
  150       PRINT_AND_THROW(boost::format(
"%s is only a constraint, but you listed it as a cost") % cost->name);
 
  155         PRINT_AND_THROW(boost::format(
"%s does not support time, but you listed it as a using time") % cost->name);
 
  161       ROS_WARN(
"%s is listed as a type TT_COST but was added to cnt_infos", (cnt->name).c_str());
 
  162     if (!(cnt->getSupportedTypes() & 
TT_CNT))
 
  163       PRINT_AND_THROW(boost::format(
"%s is only a cost, but you listed it as a constraint") % cnt->name);
 
  168         PRINT_AND_THROW(boost::format(
"%s does not support time, but you listed it as a using time") % cnt->name);
 
  174     PRINT_AND_THROW(
"A term is using time and basic_info is not set correctly. Try basic_info.use_time = true");
 
  178     PRINT_AND_THROW(
"No terms use time and basic_info is not set correctly. Try basic_info.use_time = false");
 
  180   auto prob = std::make_shared<TrajOptProblem>(pci);
 
  183   moveit::core::RobotModelConstPtr robot_model = pci.
planning_scene->getRobotModel();
 
  187   int n_dof = prob->GetNumDOF();
 
  189   std::vector<double> current_joint_values;
 
  192   trajopt::TrajArray init_traj;
 
  197     prob->SetHasTime(
true);
 
  198     if (init_traj.rows() != n_steps || init_traj.cols() != n_dof + 1)
 
  200       PRINT_AND_THROW(boost::format(
"Initial trajectory is not the right size matrix\n" 
  201                                     "Expected %i rows (time steps) x %i columns (%i dof + 1 time column)\n" 
  202                                     "Got %i rows and %i columns") %
 
  203                       n_steps % (n_dof + 1) % n_dof % init_traj.rows() % init_traj.cols());
 
  208     prob->SetHasTime(
false);
 
  209     if (init_traj.rows() != n_steps || init_traj.cols() != n_dof)
 
  211       PRINT_AND_THROW(boost::format(
"Initial trajectory is not the right size matrix\n" 
  212                                     "Expected %i rows (time steps) x %i columns\n" 
  213                                     "Got %i rows and %i columns") %
 
  214                       n_steps % n_dof % init_traj.rows() % init_traj.cols());
 
  217   prob->SetInitTraj(init_traj);
 
  219   trajopt::VarArray matrix_traj_vars_temp;
 
  223     if (init_traj.rows() < 1)
 
  225       PRINT_AND_THROW(
"Initial trajectory must contain at least the start state.");
 
  228     if (init_traj.cols() != (n_dof + (use_time ? 1 : 0)))
 
  230       PRINT_AND_THROW(
"robot dof values don't match initialization. I don't " 
  231                       "know what you want me to use for the dof values");
 
  234     for (
int j = 0; j < static_cast<int>(n_dof); ++j)
 
  236       matrix_traj_vars_temp = prob->GetVars();
 
  237       prob->addLinearConstraint(sco::exprSub(sco::AffExpr(matrix_traj_vars_temp(0, j)), init_traj(0, j)), sco::EQ);
 
  246       for (
int i = 1; i < prob->GetNumSteps(); ++i)
 
  248         matrix_traj_vars_temp = prob->GetVars();
 
  249         prob->addLinearConstraint(sco::exprSub(sco::AffExpr(matrix_traj_vars_temp(i, dof_ind)),
 
  250                                                sco::AffExpr(init_traj(0, dof_ind))),
 
  258     ci->addObjectiveTerms(*prob);
 
  261   for (
const TermInfoPtr& ci : pci.
cnt_infos)
 
  263     ci->addObjectiveTerms(*prob);
 
  279   Eigen::Isometry3d input_pose;
 
  281   input_pose.linear() = q.matrix();
 
  282   input_pose.translation() = 
xyz;
 
  286     ROS_ERROR(
"Use time version of this term has not been defined.");
 
  290     ROS_ERROR(
"Use time version of this term has not been defined.");
 
  295     prob.addCost(std::make_shared<sco::CostFromErrFunc>(
f, prob.
GetVarRow(
timestep, 0, n_dof),
 
  301     prob.addConstraint(std::make_shared<sco::ConstraintFromErrFunc>(
 
  306     ROS_WARN(
"CartPoseTermInfo does not have a valid term_type defined. No cost/constraint applied");
 
  316     coeffs = trajopt::DblVec(n_dof, 1);
 
  336     ROS_WARN(
"Last time step for JointPosTerm comes before first step. Reversing them.");
 
  348   bool is_upper_zeros =
 
  349       std::all_of(
upper_tols.begin(), 
upper_tols.end(), [](
double i) { return util::doubleEquals(i, 0.); });
 
  350   bool is_lower_zeros =
 
  351       std::all_of(
lower_tols.begin(), 
lower_tols.end(), [](
double i) { return util::doubleEquals(i, 0.); });
 
  354   trajopt::VarArray vars = prob.
GetVars();
 
  355   trajopt::VarArray joint_vars = vars.block(0, 0, vars.rows(), 
static_cast<int>(n_dof));
 
  357     ROS_INFO(
"JointPoseTermInfo does not differ based on setting of TT_USE_TIME");
 
  362     if (is_upper_zeros && is_lower_zeros)
 
  364       prob.addCost(std::make_shared<trajopt::JointPosEqCost>(joint_vars, util::toVectorXd(
coeffs),
 
  366       prob.getCosts().back()->setName(
name);
 
  370       prob.addCost(std::make_shared<trajopt::JointPosIneqCost>(joint_vars, util::toVectorXd(
coeffs),
 
  373       prob.getCosts().back()->setName(
name);
 
  379     if (is_upper_zeros && is_lower_zeros)
 
  381       prob.addConstraint(std::make_shared<trajopt::JointPosEqConstraint>(
 
  383       prob.getConstraints().back()->setName(
name);
 
  387       prob.addConstraint(std::make_shared<trajopt::JointPosIneqConstraint>(
 
  390       prob.getConstraints().back()->setName(
name);
 
  395     ROS_WARN(
"JointPosTermInfo does not have a valid term_type defined. No cost/constraint applied");
 
  405     coeffs = trajopt::DblVec(n_dof, 1);
 
  425     ROS_WARN(
"Last time step for JointVelTerm comes before first step. Reversing them.");
 
  437   bool is_upper_zeros =
 
  438       std::all_of(
upper_tols.begin(), 
upper_tols.end(), [](
double i) { return util::doubleEquals(i, 0.); });
 
  439   bool is_lower_zeros =
 
  440       std::all_of(
lower_tols.begin(), 
lower_tols.end(), [](
double i) { return util::doubleEquals(i, 0.); });
 
  443   trajopt::VarArray vars = prob.
GetVars();
 
  444   trajopt::VarArray joint_vars = vars.block(0, 0, vars.rows(), 
static_cast<int>(n_dof));
 
  451     for (
size_t j = 0; j < n_dof; ++j)
 
  458       if (is_upper_zeros && is_lower_zeros)
 
  460         trajopt::DblVec single_jnt_coeffs = trajopt::DblVec(num_vels * 2, 
coeffs[j]);
 
  461         prob.addCost(std::make_shared<sco::CostFromErrFunc>(
 
  463             std::make_shared<JointVelJacobianCalculator>(), 
concatVector(joint_vars_vec, time_vars_vec),
 
  464             util::toVectorXd(single_jnt_coeffs), sco::SQUARED, 
name + 
"_j" + std::to_string(j)));
 
  469         trajopt::DblVec single_jnt_coeffs = trajopt::DblVec(num_vels * 2, 
coeffs[j]);
 
  470         prob.addCost(std::make_shared<sco::CostFromErrFunc>(
 
  472             std::make_shared<JointVelJacobianCalculator>(), 
concatVector(joint_vars_vec, time_vars_vec),
 
  473             util::toVectorXd(single_jnt_coeffs), sco::HINGE, 
name + 
"_j" + std::to_string(j)));
 
  482     for (
size_t j = 0; j < n_dof; ++j)
 
  489       if (is_upper_zeros && is_lower_zeros)
 
  491         trajopt::DblVec single_jnt_coeffs = trajopt::DblVec(num_vels * 2, 
coeffs[j]);
 
  492         prob.addConstraint(std::make_shared<sco::ConstraintFromErrFunc>(
 
  494             std::make_shared<JointVelJacobianCalculator>(), 
concatVector(joint_vars_vec, time_vars_vec),
 
  495             util::toVectorXd(single_jnt_coeffs), sco::EQ, 
name + 
"_j" + std::to_string(j)));
 
  500         trajopt::DblVec single_jnt_coeffs = trajopt::DblVec(num_vels * 2, 
coeffs[j]);
 
  501         prob.addConstraint(std::make_shared<sco::ConstraintFromErrFunc>(
 
  503             std::make_shared<JointVelJacobianCalculator>(), 
concatVector(joint_vars_vec, time_vars_vec),
 
  504             util::toVectorXd(single_jnt_coeffs), sco::INEQ, 
name + 
"_j" + std::to_string(j))));
 
  511     if (is_upper_zeros && is_lower_zeros)
 
  513       prob.addCost(std::make_shared<trajopt::JointVelEqCost>(joint_vars, util::toVectorXd(
coeffs),
 
  515       prob.getCosts().back()->setName(
name);
 
  519       prob.addCost(std::make_shared<trajopt::JointVelIneqCost>(joint_vars, util::toVectorXd(
coeffs),
 
  522       prob.getCosts().back()->setName(
name);
 
  528     if (is_upper_zeros && is_lower_zeros)
 
  530       prob.addConstraint(std::make_shared<trajopt::JointVelEqConstraint>(
 
  532       prob.getConstraints().back()->setName(
name);
 
  536       prob.addConstraint(std::make_shared<trajopt::JointVelIneqConstraint>(
 
  539       prob.getConstraints().back()->setName(
name);
 
  544     ROS_WARN(
"JointVelTermInfo does not have a valid term_type defined. No cost/constraint applied");
 
  549                                trajopt::TrajArray& init_traj)
 
  551   Eigen::VectorXd current_pos(current_joint_values.size());
 
  552   for (
int joint_index = 0; joint_index < current_joint_values.size(); ++joint_index)
 
  554     current_pos(joint_index) = current_joint_values[joint_index];
 
  569     Eigen::VectorXd end_pos = init_info.
data;
 
  571     for (
int dof_index = 0; dof_index < current_pos.rows(); ++dof_index)
 
  573       init_traj.col(dof_index) =
 
  574           Eigen::VectorXd::LinSpaced(pci.
basic_info.
n_steps, current_pos(dof_index), end_pos(dof_index));
 
  580     init_traj = init_info.
data;
 
  584     PRINT_AND_THROW(
"Init Info did not have a valid type. Valid types are " 
  585                     "STATIONARY, JOINT_INTERPOLATED, or GIVEN_TRAJ");
 
  592     init_traj.conservativeResize(Eigen::NoChange_t(), init_traj.cols() + 1);
 
  594     init_traj.block(0, init_traj.cols() - 1, init_traj.rows(), 1) =
 
  595         Eigen::VectorXd::Constant(init_traj.rows(), init_info.
dt);
 
const JointBoundsVector & getActiveJointModelsBounds() const
Get the bounds for all the active joints.
 
const std::vector< std::string > & getActiveJointModelNames() const
Get the names of the active joints in this group. These are the names of the joints returned by getJo...
 
std::vector< VariableBounds > Bounds
The datatype for the joint bounds.
 
Representation of a robot's state. This includes position, velocity, acceleration and effort.
 
void copyJointGroupPositions(const std::string &joint_group_name, std::vector< double > &gstate) const
For a given group, copy the position values of the variables that make up the group into another loca...
 
const JointModelGroup * getJointModelGroup(const std::string &group) const
Get the model of a particular joint group.
 
planning_scene::PlanningSceneConstPtr GetPlanningScene()
 
sco::VarVector GetVarRow(int i, int start_col, int num_col)
Returns the values of the specified joints (start_col to num_col) for the specified timestep i.
 
int GetNumDOF()
Returns the problem DOF. This is the number of columns in the optization matrix. Note that this is no...
 
bool GetHasTime()
Returns TrajOptProb.has_time.
 
int GetActiveGroupNumDOF()
Returns the kinematic DOF of the active joint model group.
 
int GetNumSteps()
Returns the number of steps in the problem. This is the number of rows in the optimization matrix.
 
trajopt::VarArray & GetVars()
 
std::vector< const JointModel::Bounds * > JointBoundsVector
 
This namespace includes the central class for representing planning contexts.
 
Eigen::VectorXd concatVector(const Eigen::VectorXd &vector_a, const Eigen::VectorXd &vector_b)
Appends b to a of type VectorXd.
 
TrajOptProblemPtr ConstructProblem(const ProblemInfo &)
 
void generateInitialTrajectory(const ProblemInfo &pci, const std::vector< double > ¤t_joint_values, trajopt::TrajArray &init_traj)
 
void checkParameterSize(trajopt::DblVec ¶meter, const unsigned int &expected_size, const std::string &name, const bool &apply_first=true)
Checks the size of the parameter given and throws if incorrect.
 
int n_steps
Number of time steps (rows) in the optimization matrix.
 
double dt_upper_lim
The upper limit of 1/dt values allowed in the optimization.
 
double dt_lower_lim
The lower limit of 1/dt values allowed in the optimization.
 
bool use_time
If true, the last column in the optimization matrix will be 1/dt.
 
bool start_fixed
If true, first time step is fixed with a joint level constraint If this is false, the starting point ...
 
Eigen::Vector3d rot_coeffs
 
Eigen::Vector3d pos_coeffs
coefficients for position and rotation
 
Eigen::Vector3d xyz
Cartesian position.
 
std::string link
Link which should reach desired pose.
 
Eigen::Isometry3d tcp
Static transform applied to the link.
 
Eigen::Vector4d wxyz
Rotation quaternion.
 
void addObjectiveTerms(TrajOptProblem &prob) override
Used to add term to pci from json.
 
int timestep
Timestep at which to apply term.
 
trajopt::TrajArray data
Data used during initialization. Use depends on the initialization selected. This data will be used t...
 
Type type
Specifies the type of initialization to use.
 
double dt
Default value the final column of the optimization is initialized too if time is being used.
 
int last_step
Last time step to which the term is applied. Default: prob.GetNumSteps() - 1.
 
trajopt::DblVec upper_tols
Vector of position upper limits. Size should be the DOF of the system. Default: vector of 0's.
 
trajopt::DblVec coeffs
Vector of coefficients that scale the cost. Size should be the DOF of the system. Default: vector of ...
 
trajopt::DblVec targets
Vector of position targets. This is a required value. Size should be the DOF of the system.
 
trajopt::DblVec lower_tols
Vector of position lower limits. Size should be the DOF of the system. Default: vector of 0's.
 
int first_step
First time step to which the term is applied. Default: 0.
 
void addObjectiveTerms(TrajOptProblem &prob) override
Converts term info into cost/constraint and adds it to trajopt problem.
 
void addObjectiveTerms(TrajOptProblem &prob) override
Converts term info into cost/constraint and adds it to trajopt problem.
 
trajopt::DblVec coeffs
Vector of coefficients that scale the cost. Size should be the DOF of the system. Default: vector of ...
 
trajopt::DblVec lower_tols
Vector of velocity lower limits. Size should be the DOF of the system. Default: vector of 0's.
 
trajopt::DblVec targets
Vector of velocity targets. This is a required value. Size should be the DOF of the system.
 
trajopt::DblVec upper_tols
Vector of velocity upper limits. Size should be the DOF of the system. Default: vector of 0's.
 
int last_step
Last time step to which the term is applied. Default: prob.GetNumSteps() - 1.
 
int first_step
First time step to which the term is applied. Default: 0.
 
std::vector< TermInfoPtr > cnt_infos
 
std::vector< TermInfoPtr > cost_infos
 
planning_scene::PlanningSceneConstPtr planning_scene
 
std::string planning_group_name