time_indexed_problem.cpp
Go to the documentation of this file.
1 //
2 // Copyright (c) 2018, University of Edinburgh
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are met:
7 //
8 // * Redistributions of source code must retain the above copyright notice,
9 // this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of nor the names of its contributors may be used to
14 // endorse or promote products derived from this software without specific
15 // prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 // POSSIBILITY OF SUCH DAMAGE.
28 //
29 
31 #include <exotica_core/setup.h>
32 
34 
35 namespace exotica
36 {
37 void TimeIndexedProblem::Instantiate(const TimeIndexedProblemInitializer& init)
38 {
39  this->parameters_ = init;
40 
41  N = scene_->GetKinematicTree().GetNumControlledJoints();
42 
43  w_scale_ = this->parameters_.Wrate;
44  W = Eigen::MatrixXd::Identity(N, N) * w_scale_;
45  if (this->parameters_.W.rows() > 0)
46  {
47  if (this->parameters_.W.rows() == N)
48  {
49  W.diagonal() = this->parameters_.W * w_scale_;
50  }
51  else
52  {
53  ThrowNamed("W dimension mismatch! Expected " << N << ", got " << this->parameters_.W.rows());
54  }
55  }
56 
57  if (init.LowerBound.rows() == N)
58  {
59  scene_->GetKinematicTree().SetJointLimitsLower(init.LowerBound);
60  }
61  else if (init.LowerBound.rows() != 0)
62  {
63  ThrowNamed("Lower bound size incorrect! Expected " << N << " got " << init.LowerBound.rows());
64  }
65  if (init.UpperBound.rows() == N)
66  {
67  scene_->GetKinematicTree().SetJointLimitsUpper(init.UpperBound);
68  }
69  else if (init.UpperBound.rows() != 0)
70  {
71  ThrowNamed("Lower bound size incorrect! Expected " << N << " got " << init.UpperBound.rows());
72  }
73 
74  use_bounds = this->parameters_.UseBounds;
75 
76  cost.Initialize(this->parameters_.Cost, shared_from_this(), cost_Phi);
77  inequality.Initialize(this->parameters_.Inequality, shared_from_this(), inequality_Phi);
78  equality.Initialize(this->parameters_.Equality, shared_from_this(), equality_Phi);
79 
80  T_ = this->parameters_.T;
81  tau_ = this->parameters_.tau;
82  SetJointVelocityLimits(this->parameters_.JointVelocityLimits); // Deprecated TODO: Replace
83  ApplyStartState(false);
85 }
86 
88 {
89  bool succeeded = true;
90  auto bounds = scene_->GetKinematicTree().GetJointLimits();
91 
92  std::cout.precision(4);
93 
94  // Check for every state
95  for (int t = 0; t < T_; ++t)
96  {
97  // Check joint limits
98  if (use_bounds)
99  {
100  for (int i = 0; i < N; ++i)
101  {
102  constexpr double tolerance = 1.e-3;
103  if (x[t](i) < bounds(i, 0) - tolerance || x[t](i) > bounds(i, 1) + tolerance)
104  {
105  if (debug_) HIGHLIGHT_NAMED("TimeIndexedProblem::IsValid", "State at timestep " << t << " is out of bounds: joint #" << i << ": " << bounds(i, 0) << " < " << x[t](i) << " < " << bounds(i, 1));
106  succeeded = false;
107  }
108  }
109  }
110 
111  // Check inequality constraints
112  if (GetInequality(t).rows() > 0)
113  {
114  if (GetInequality(t).maxCoeff() > this->parameters_.InequalityFeasibilityTolerance)
115  {
116  if (debug_) HIGHLIGHT_NAMED("TimeIndexedProblem::IsValid", "Violated inequality constraints at timestep " << t << ": " << GetInequality(t).transpose());
117  succeeded = false;
118  }
119  }
120 
121  // Check equality constraints
122  if (GetEquality(t).rows() > 0)
123  {
124  if (GetEquality(t).cwiseAbs().maxCoeff() > this->parameters_.EqualityFeasibilityTolerance)
125  {
126  if (debug_) HIGHLIGHT_NAMED("TimeIndexedProblem::IsValid", "Violated equality constraints at timestep " << t << ": " << GetEquality(t).cwiseAbs().maxCoeff());
127  succeeded = false;
128  }
129  }
130 
131  // Check joint velocity limits
132  if (q_dot_max_.maxCoeff() > 0 && t > 0)
133  {
134  if (((x[t] - x[t - 1]).cwiseAbs() - xdiff_max_).maxCoeff() > 1.e-5) // The 1e-5 are a required tolerance...
135  {
136  if (debug_) HIGHLIGHT_NAMED("TimeIndexedProblem::IsValid", "Violated joint velocity constraints at timestep " << t << ": (" << (x[t] - x[t - 1]).transpose() << "), (limit=" << xdiff_max_.transpose() << "), violation: (" << ((x[t] - x[t - 1]).cwiseAbs() - xdiff_max_).transpose() << ")");
137  succeeded = false;
138  }
139  }
140  }
141 
142  return succeeded;
143 }
144 } // namespace exotica
exotica::AbstractTimeIndexedProblem::cost_Phi
TaskSpaceVector cost_Phi
Definition: abstract_time_indexed_problem.h:265
exotica::Instantiable< TimeIndexedProblemInitializer >::parameters_
TimeIndexedProblemInitializer parameters_
Definition: property.h:139
exotica::AbstractTimeIndexedProblem::x
std::vector< Eigen::VectorXd > x
Current internal problem state.
Definition: abstract_time_indexed_problem.h:260
time_indexed_problem.h
exotica::AbstractTimeIndexedProblem::w_scale_
double w_scale_
Kinematic system transition error covariance multiplier (constant throughout the trajectory)
Definition: abstract_time_indexed_problem.h:263
exotica::AbstractTimeIndexedProblem::GetEquality
Eigen::VectorXd GetEquality() const
Returns the equality constraint values for the entire trajectory.
Definition: abstract_time_indexed_problem.cpp:354
exotica::TimeIndexedProblem::Instantiate
void Instantiate(const TimeIndexedProblemInitializer &init) override
Instantiates the problem from an Initializer.
Definition: time_indexed_problem.cpp:37
exotica::PlanningProblem::N
int N
Definition: planning_problem.h:96
exotica
Definition: collision_scene.h:46
exotica::TimeIndexedProblem::IsValid
bool IsValid() override
Evaluates whether the problem is valid, i.e., all bound and general constraints are satisfied.
Definition: time_indexed_problem.cpp:87
exotica::TimeIndexedProblem
Time-indexed problem with bound, joint velocity, and general equality/inequality constraints.
Definition: time_indexed_problem.h:39
exotica::AbstractTimeIndexedProblem::equality_Phi
TaskSpaceVector equality_Phi
Definition: abstract_time_indexed_problem.h:267
HIGHLIGHT_NAMED
#define HIGHLIGHT_NAMED(name, x)
Definition: printable.h:62
exotica::PlanningProblem::scene_
ScenePtr scene_
Definition: planning_problem.h:105
exotica::AbstractTimeIndexedProblem::T_
int T_
Number of time steps.
Definition: abstract_time_indexed_problem.h:257
exotica::AbstractTimeIndexedProblem::xdiff_max_
Eigen::VectorXd xdiff_max_
Maximum change in the variables in a single timestep tau_. Gets set/updated via SetJointVelocityLimit...
Definition: abstract_time_indexed_problem.h:275
exotica::AbstractTimeIndexedProblem::ReinitializeVariables
virtual void ReinitializeVariables()
Definition: abstract_time_indexed_problem.cpp:47
exotica::AbstractTimeIndexedProblem::tau_
double tau_
Time step duration.
Definition: abstract_time_indexed_problem.h:258
REGISTER_PROBLEM_TYPE
#define REGISTER_PROBLEM_TYPE(TYPE, DERIV)
Definition: planning_problem.h:45
exotica::AbstractTimeIndexedProblem::use_bounds
bool use_bounds
Definition: abstract_time_indexed_problem.h:239
exotica::AbstractTimeIndexedProblem::SetJointVelocityLimits
void SetJointVelocityLimits(const Eigen::VectorXd &qdot_max_in)
Sets the joint velocity limits. Supports N- and 1-dimensional vectors.
Definition: abstract_time_indexed_problem.cpp:503
exotica::Object::debug_
bool debug_
Definition: object.h:86
exotica::AbstractTimeIndexedProblem::equality
TimeIndexedTask equality
General equality task.
Definition: abstract_time_indexed_problem.h:226
exotica::TimeIndexedTask::Initialize
virtual void Initialize(const std::vector< exotica::Initializer > &inits, std::shared_ptr< PlanningProblem > prob, TaskSpaceVector &Phi)
Definition: tasks.cpp:240
exotica::AbstractTimeIndexedProblem::cost
TimeIndexedTask cost
Cost task.
Definition: abstract_time_indexed_problem.h:224
setup.h
exotica::PlanningProblem::ApplyStartState
virtual Eigen::VectorXd ApplyStartState(bool update_traj=true)
Definition: planning_problem.cpp:70
tolerance
S tolerance()
exotica::AbstractTimeIndexedProblem::q_dot_max_
Eigen::VectorXd q_dot_max_
Joint velocity limit (rad/s)
Definition: abstract_time_indexed_problem.h:274
init
void init(const M_string &remappings)
exotica::AbstractTimeIndexedProblem::inequality
TimeIndexedTask inequality
General inequality task.
Definition: abstract_time_indexed_problem.h:225
exotica::AbstractTimeIndexedProblem::inequality_Phi
TaskSpaceVector inequality_Phi
Definition: abstract_time_indexed_problem.h:266
exotica::AbstractTimeIndexedProblem::W
Eigen::MatrixXd W
Definition: abstract_time_indexed_problem.h:228
exotica::AbstractTimeIndexedProblem::GetInequality
Eigen::VectorXd GetInequality() const
Returns the inequality constraint values for the entire trajectory.
Definition: abstract_time_indexed_problem.cpp:423
t
geometry_msgs::TransformStamped t
ThrowNamed
#define ThrowNamed(m)
Definition: exception.h:42


exotica_core
Author(s): Yiming Yang, Michael Camilleri
autogenerated on Fri Aug 2 2024 08:43:02