time_indexed_sampling_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 {
38 {
39  flags_ = KIN_FK;
40 }
41 
43 
45 {
46  std::vector<double> bounds;
47  auto joint_limits = scene_->GetKinematicTree().GetJointLimits();
48 
49  bounds.resize(2 * N);
50  for (unsigned int i = 0; i < N; ++i)
51  {
52  bounds[i] = joint_limits(i, 0);
53  bounds[i + N] = joint_limits(i, 1);
54  }
55 
56  return bounds;
57 }
58 
59 void TimeIndexedSamplingProblem::Instantiate(const TimeIndexedSamplingProblemInitializer& init)
60 {
61  parameters = init;
62 
63  if (init.Goal.size() == N)
64  {
65  goal_ = init.Goal;
66  }
67  else if (init.Goal.size() == 0)
68  {
69  goal_ = Eigen::VectorXd::Zero(N);
70  }
71  else
72  {
73  ThrowNamed("Dimension mismatch: problem N=" << N << ", but goal state has dimension " << goal_.rows());
74  }
75 
76  t_goal_ = init.GoalTime;
77  if (t_goal_ <= t_start)
78  ThrowNamed("Invalid goal time t_goal= " << t_goal_ << ", where t_start=" << t_start);
79 
80  if (init.JointVelocityLimits.size() == N)
81  {
82  vel_limits = init.JointVelocityLimits;
83  }
84  else if (init.JointVelocityLimits.size() == 1)
85  {
86  vel_limits = init.JointVelocityLimits(0) * Eigen::VectorXd::Ones(N);
87  }
88  else
89  {
90  ThrowNamed("Dimension mismatch: problem N=" << N << ", but joint velocity limits has dimension " << vel_limits.rows());
91  }
92 
93  num_tasks = tasks_.size();
94  length_Phi = 0;
95  length_jacobian = 0;
96  for (int i = 0; i < num_tasks; ++i)
97  {
98  AppendVector(Phi.map, tasks_[i]->GetLieGroupIndices());
99  length_Phi += tasks_[i]->length;
100  length_jacobian += tasks_[i]->length_jacobian;
101  }
103 
104  inequality.Initialize(init.Inequality, shared_from_this(), constraint_phi);
105  inequality.tolerance = init.ConstraintTolerance;
106  equality.Initialize(init.Equality, shared_from_this(), constraint_phi);
107  equality.tolerance = init.ConstraintTolerance;
108 
109  ApplyStartState(false);
110 
111  if (scene_->GetKinematicTree().GetControlledBaseType() != exotica::BaseType::FIXED && init.FloatingBaseLowerLimits.rows() > 0 && init.FloatingBaseUpperLimits.rows() > 0)
112  {
113  if (scene_->GetKinematicTree().GetControlledBaseType() == exotica::BaseType::FLOATING && init.FloatingBaseLowerLimits.rows() == 6 && init.FloatingBaseUpperLimits.rows() == 6)
114  {
115  scene_->GetKinematicTree().SetFloatingBaseLimitsPosXYZEulerZYX(
116  std::vector<double>(init.FloatingBaseLowerLimits.data(), init.FloatingBaseLowerLimits.data() + init.FloatingBaseLowerLimits.size()),
117  std::vector<double>(init.FloatingBaseUpperLimits.data(), init.FloatingBaseUpperLimits.data() + init.FloatingBaseUpperLimits.size()));
118  }
119  else if (scene_->GetKinematicTree().GetControlledBaseType() == exotica::BaseType::PLANAR && init.FloatingBaseLowerLimits.rows() == 3 && init.FloatingBaseUpperLimits.rows() == 3)
120  {
121  scene_->GetKinematicTree().SetPlanarBaseLimitsPosXYEulerZ(
122  std::vector<double>(init.FloatingBaseLowerLimits.data(), init.FloatingBaseLowerLimits.data() + init.FloatingBaseLowerLimits.size()),
123  std::vector<double>(init.FloatingBaseUpperLimits.data(), init.FloatingBaseUpperLimits.data() + init.FloatingBaseUpperLimits.size()));
124  }
125  else
126  {
127  ThrowNamed("Invalid base limits!");
128  }
129  }
130 
131  PreUpdate();
132 }
133 
135 {
136  return t_goal_;
137 }
138 
140 {
141  t_goal_ = t;
142 }
143 
145 {
146  return goal_;
147 }
148 
150 {
151  if (qT.rows() != N)
152  ThrowPretty("Dimensionality of goal state wrong: Got " << qT.rows() << ", expected " << N);
153  goal_ = qT;
154 }
155 
156 void TimeIndexedSamplingProblem::SetGoalEQ(const std::string& task_name, Eigen::VectorXdRefConst goal)
157 {
158  for (int i = 0; i < equality.indexing.size(); ++i)
159  {
160  if (equality.tasks[i]->GetObjectName() == task_name)
161  {
162  if (goal.rows() != equality.indexing[i].length) ThrowPretty("Expected length of " << equality.indexing[i].length << " and got " << goal.rows());
163  equality.y.data.segment(equality.indexing[i].start, equality.indexing[i].length) = goal;
164  return;
165  }
166  }
167  ThrowPretty("Cannot set Goal. Task map '" << task_name << "' does not exist.");
168 }
169 
170 void TimeIndexedSamplingProblem::SetRhoEQ(const std::string& task_name, const double& rho)
171 {
172  for (int i = 0; i < equality.indexing.size(); ++i)
173  {
174  if (equality.tasks[i]->GetObjectName() == task_name)
175  {
176  equality.rho(equality.indexing[i].id) = rho;
177  PreUpdate();
178  return;
179  }
180  }
181  ThrowPretty("Cannot set rho. Task map '" << task_name << "' does not exist.");
182 }
183 
184 Eigen::VectorXd TimeIndexedSamplingProblem::GetGoalEQ(const std::string& task_name)
185 {
186  for (int i = 0; i < equality.indexing.size(); ++i)
187  {
188  if (equality.tasks[i]->GetObjectName() == task_name)
189  {
190  return equality.y.data.segment(equality.indexing[i].start, equality.indexing[i].length);
191  }
192  }
193  ThrowPretty("Cannot get Goal. Task map '" << task_name << "' does not exist.");
194 }
195 
196 double TimeIndexedSamplingProblem::GetRhoEQ(const std::string& task_name)
197 {
198  for (int i = 0; i < equality.indexing.size(); ++i)
199  {
200  if (equality.tasks[i]->GetObjectName() == task_name)
201  {
202  return equality.rho(equality.indexing[i].id);
203  }
204  }
205  ThrowPretty("Cannot get rho. Task map '" << task_name << "' does not exist.");
206 }
207 
208 void TimeIndexedSamplingProblem::SetGoalNEQ(const std::string& task_name, Eigen::VectorXdRefConst goal)
209 {
210  for (int i = 0; i < inequality.indexing.size(); ++i)
211  {
212  if (inequality.tasks[i]->GetObjectName() == task_name)
213  {
214  if (goal.rows() != inequality.indexing[i].length) ThrowPretty("Expected length of " << inequality.indexing[i].length << " and got " << goal.rows());
215  inequality.y.data.segment(inequality.indexing[i].start, inequality.indexing[i].length) = goal;
216  return;
217  }
218  }
219  ThrowPretty("Cannot set Goal. Task map '" << task_name << "' does not exist.");
220 }
221 
222 void TimeIndexedSamplingProblem::SetRhoNEQ(const std::string& task_name, const double& rho)
223 {
224  for (int i = 0; i < inequality.indexing.size(); ++i)
225  {
226  if (inequality.tasks[i]->GetObjectName() == task_name)
227  {
228  inequality.rho(inequality.indexing[i].id) = rho;
229  PreUpdate();
230  return;
231  }
232  }
233  ThrowPretty("Cannot set rho. Task map '" << task_name << "' does not exist.");
234 }
235 
236 Eigen::VectorXd TimeIndexedSamplingProblem::GetGoalNEQ(const std::string& task_name)
237 {
238  for (int i = 0; i < inequality.indexing.size(); ++i)
239  {
240  if (inequality.tasks[i]->GetObjectName() == task_name)
241  {
242  return inequality.y.data.segment(inequality.indexing[i].start, inequality.indexing[i].length);
243  }
244  }
245  ThrowPretty("Cannot get Goal. Task map '" << task_name << "' does not exist.");
246 }
247 
248 double TimeIndexedSamplingProblem::GetRhoNEQ(const std::string& task_name)
249 {
250  for (int i = 0; i < inequality.indexing.size(); ++i)
251  {
252  if (inequality.tasks[i]->GetObjectName() == task_name)
253  {
254  return inequality.rho(inequality.indexing[i].id);
255  }
256  }
257  ThrowPretty("Cannot get rho. Task map '" << task_name << "' does not exist.");
258 }
259 
261 {
262  scene_->Update(x, t);
263  for (int i = 0; i < num_tasks; ++i)
264  {
265  if (tasks_[i]->is_used)
266  tasks_[i]->Update(x, Phi.data.segment(tasks_[i]->start, tasks_[i]->length));
267  }
271 
272  bool inequality_is_valid = ((inequality.S * inequality.ydiff).array() <= 0.0).all();
273  bool equality_is_valid = ((equality.S * equality.ydiff).array().abs() == 0.0).all();
274 
275  if (debug_)
276  {
277  HIGHLIGHT_NAMED("TimeIndexedSamplingProblem::IsValid", "Equality valid? = " << equality_is_valid << "\tInequality valid? = " << inequality_is_valid);
278  }
279 
280  return (inequality_is_valid && equality_is_valid);
281 }
282 
284 {
286  for (int i = 0; i < tasks_.size(); ++i) tasks_[i]->is_used = false;
288  equality.UpdateS();
289 }
290 
292 {
293  IsValid(x, t);
295 }
296 
298 {
299  return N;
300 }
301 } // namespace exotica
exotica::PlanningProblem::number_of_problem_updates_
unsigned int number_of_problem_updates_
Definition: planning_problem.h:111
exotica::AppendVector
void AppendVector(std::vector< Val > &orig, const std::vector< Val > &extra)
Definition: conversions.h:223
exotica::TimeIndexedSamplingProblem::inequality
SamplingTask inequality
Definition: time_indexed_sampling_problem.h:75
exotica::TimeIndexedSamplingProblem::TimeIndexedSamplingProblem
TimeIndexedSamplingProblem()
Definition: time_indexed_sampling_problem.cpp:37
exotica::PlanningProblem::flags_
KinematicRequestFlags flags_
Definition: planning_problem.h:109
exotica::PLANAR
@ PLANAR
Definition: kinematic_tree.h:53
exotica::TimeIndexedSamplingProblem::GetSpaceDim
int GetSpaceDim()
Definition: time_indexed_sampling_problem.cpp:297
exotica::TimeIndexedSamplingProblem::t_goal_
double t_goal_
Goal time: The time at which goal_ should be reached and the upper bound for the time-dimension.
Definition: time_indexed_sampling_problem.h:85
exotica::PlanningProblem::t_start
double t_start
Definition: planning_problem.h:93
exotica::SamplingTask::Initialize
virtual void Initialize(const std::vector< exotica::Initializer > &inits, std::shared_ptr< PlanningProblem > prob, TaskSpaceVector &Phi)
Definition: tasks.cpp:494
exotica::TimeIndexedSamplingProblem::SetRhoEQ
void SetRhoEQ(const std::string &task_name, const double &rho)
Definition: time_indexed_sampling_problem.cpp:170
exotica::TimeIndexedSamplingProblem::Update
void Update(Eigen::VectorXdRefConst x, const double &t)
Definition: time_indexed_sampling_problem.cpp:291
exotica::TimeIndexedSamplingProblem::GetGoalEQ
Eigen::VectorXd GetGoalEQ(const std::string &task_name)
Definition: time_indexed_sampling_problem.cpp:184
exotica::PlanningProblem::N
int N
Definition: planning_problem.h:97
exotica::TimeIndexedSamplingProblem::vel_limits
Eigen::VectorXd vel_limits
Definition: time_indexed_sampling_problem.h:73
exotica
Definition: collision_scene.h:46
exotica::FLOATING
@ FLOATING
Definition: kinematic_tree.h:52
exotica::TimeIndexedSamplingProblem::parameters
TimeIndexedSamplingProblemInitializer parameters
Definition: time_indexed_sampling_problem.h:77
exotica::TimeIndexedSamplingProblem::Phi
TaskSpaceVector Phi
Definition: time_indexed_sampling_problem.h:74
exotica::PlanningProblem::tasks_
TaskMapVec tasks_
Definition: planning_problem.h:108
exotica::SamplingTask::rho
Eigen::VectorXd rho
Definition: tasks.h:172
HIGHLIGHT_NAMED
#define HIGHLIGHT_NAMED(name, x)
Definition: printable.h:62
exotica::SamplingTask::Update
void Update(const TaskSpaceVector &big_Phi)
Definition: tasks.cpp:545
exotica::KIN_FK
@ KIN_FK
Definition: kinematic_tree.h:58
exotica::TimeIndexedSamplingProblem::constraint_phi
TaskSpaceVector constraint_phi
Definition: time_indexed_sampling_problem.h:78
exotica::TimeIndexedSamplingProblem::GetGoalTime
double GetGoalTime() const
Definition: time_indexed_sampling_problem.cpp:134
exotica::TimeIndexedSamplingProblem::GetGoalNEQ
Eigen::VectorXd GetGoalNEQ(const std::string &task_name)
Definition: time_indexed_sampling_problem.cpp:236
exotica::PlanningProblem::scene_
ScenePtr scene_
Definition: planning_problem.h:106
time_indexed_sampling_problem.h
exotica::TimeIndexedSamplingProblem::SetGoalTime
void SetGoalTime(const double &t)
Definition: time_indexed_sampling_problem.cpp:139
exotica::TimeIndexedSamplingProblem::SetGoalNEQ
void SetGoalNEQ(const std::string &task_name, Eigen::VectorXdRefConst goal)
Definition: time_indexed_sampling_problem.cpp:208
exotica::TimeIndexedSamplingProblem::Instantiate
void Instantiate(const TimeIndexedSamplingProblemInitializer &init) override
Definition: time_indexed_sampling_problem.cpp:59
Eigen::VectorXdRefConst
const typedef Eigen::Ref< const Eigen::VectorXd > & VectorXdRefConst
Convenience wrapper for storing references to sub-matrices/vectors.
Definition: conversions.h:52
exotica::TimeIndexedSamplingProblem::SetGoalState
void SetGoalState(Eigen::VectorXdRefConst qT)
Definition: time_indexed_sampling_problem.cpp:149
REGISTER_PROBLEM_TYPE
#define REGISTER_PROBLEM_TYPE(TYPE, DERIV)
Definition: planning_problem.h:45
exotica::TimeIndexedSamplingProblem::GetBounds
std::vector< double > GetBounds()
Definition: time_indexed_sampling_problem.cpp:44
exotica::SamplingTask::y
TaskSpaceVector y
Definition: tasks.h:173
exotica::SamplingTask::ydiff
Eigen::VectorXd ydiff
Definition: tasks.h:174
exotica::Object::debug_
bool debug_
Definition: object.h:86
exotica::TaskSpaceVector::map
std::vector< TaskVectorEntry > map
Definition: task_space_vector.h:59
exotica::TimeIndexedSamplingProblem::~TimeIndexedSamplingProblem
virtual ~TimeIndexedSamplingProblem()
exotica::Task::indexing
std::vector< TaskIndexing > indexing
Definition: tasks.h:64
exotica::TimeIndexedSamplingProblem::GetRhoNEQ
double GetRhoNEQ(const std::string &task_name)
Definition: time_indexed_sampling_problem.cpp:248
exotica::TimeIndexedSamplingProblem
Definition: time_indexed_sampling_problem.h:40
exotica::TimeIndexedSamplingProblem::length_Phi
int length_Phi
Definition: time_indexed_sampling_problem.h:80
setup.h
exotica::TimeIndexedSamplingProblem::PreUpdate
void PreUpdate() override
Definition: time_indexed_sampling_problem.cpp:283
exotica::TimeIndexedSamplingProblem::goal_
Eigen::VectorXd goal_
Goal state to reach (spatial) at temporal goal (t_goal_)
Definition: time_indexed_sampling_problem.h:86
ThrowPretty
#define ThrowPretty(m)
Definition: exception.h:36
exotica::Task::tolerance
double tolerance
Definition: tasks.h:69
exotica::TimeIndexedSamplingProblem::SetGoalEQ
void SetGoalEQ(const std::string &task_name, Eigen::VectorXdRefConst goal)
Definition: time_indexed_sampling_problem.cpp:156
exotica::PlanningProblem::ApplyStartState
virtual Eigen::VectorXd ApplyStartState(bool update_traj=true)
Definition: planning_problem.cpp:70
exotica::TimeIndexedSamplingProblem::GetRhoEQ
double GetRhoEQ(const std::string &task_name)
Definition: time_indexed_sampling_problem.cpp:196
exotica::TimeIndexedSamplingProblem::SetRhoNEQ
void SetRhoNEQ(const std::string &task_name, const double &rho)
Definition: time_indexed_sampling_problem.cpp:222
exotica::Task::tasks
TaskMapVec tasks
Definition: tasks.h:63
init
void init(const M_string &remappings)
exotica::TaskSpaceVector::data
Eigen::VectorXd data
Definition: task_space_vector.h:58
x
double x
exotica::FIXED
@ FIXED
Definition: kinematic_tree.h:51
exotica::PlanningProblem::PreUpdate
virtual void PreUpdate()
Definition: planning_problem.cpp:86
exotica::PlanningProblem::IsValid
virtual bool IsValid()
Evaluates whether the problem is valid.
Definition: planning_problem.h:93
exotica::TimeIndexedSamplingProblem::equality
SamplingTask equality
Definition: time_indexed_sampling_problem.h:76
t
geometry_msgs::TransformStamped t
exotica::SamplingTask::UpdateS
void UpdateS()
Definition: tasks.cpp:533
exotica::TimeIndexedSamplingProblem::num_tasks
int num_tasks
Definition: time_indexed_sampling_problem.h:82
exotica::TimeIndexedSamplingProblem::length_jacobian
int length_jacobian
Definition: time_indexed_sampling_problem.h:81
exotica::TimeIndexedSamplingProblem::GetGoalState
Eigen::VectorXd GetGoalState() const
Definition: time_indexed_sampling_problem.cpp:144
exotica::TaskSpaceVector::SetZero
void SetZero(const int n)
Definition: task_space_vector.cpp:54
ThrowNamed
#define ThrowNamed(m)
Definition: exception.h:42
exotica::SamplingTask::S
Eigen::MatrixXd S
Definition: tasks.h:176


exotica_core
Author(s): Yiming Yang, Michael Camilleri
autogenerated on Fri Oct 20 2023 02:59:49