task_stack_controller.h
Go to the documentation of this file.
1 /*
2  * Copyright 2017 Fraunhofer Institute for Manufacturing Engineering and Automation (IPA)
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 
18 #ifndef COB_TWIST_CONTROLLER_TASK_STACK_TASK_STACK_CONTROLLER_H
19 #define COB_TWIST_CONTROLLER_TASK_STACK_TASK_STACK_CONTROLLER_H
20 
21 #include <vector>
22 #include <string>
23 #include <stdint.h>
24 #include <ros/ros.h>
25 
26 #include <boost/shared_ptr.hpp>
27 #include <Eigen/Dense>
28 
31 
32 template
33 <typename PRIO>
34 struct Task
35 {
37  Eigen::MatrixXd task_jacobian_;
38  Eigen::VectorXd task_;
39  std::string id_;
40  bool is_active_;
42 
43  Task(PRIO prio, std::string id) : prio_(prio), id_(id), is_active_(true)
44  {}
45 
46  Task(PRIO prio, std::string id, Eigen::MatrixXd task_jacobian, Eigen::VectorXd task)
47  : prio_(prio), id_(id), task_jacobian_(task_jacobian), task_(task), is_active_(true)
48  {}
49 
50  Task(const Task& task)
51  : prio_(task.prio_),
52  id_(task.id_),
53  task_jacobian_(task.task_jacobian_),
54  task_(task.task_),
55  is_active_(task.is_active_),
56  tcp_(task.tcp_)
57  {}
58 
60  {}
61 
62  inline void setPriority(PRIO prio)
63  {
64  this->prio_ = prio;
65  }
66 
67  inline bool operator<(const Task& other) const
68  {
69  return ( this->prio_ < other.prio_ );
70  }
71 
72  inline bool operator>(const Task& other) const
73  {
74  return ( this->prio_ > other.prio_ );
75  }
76 
77  inline bool operator==(const Task& other) const
78  {
79  return ( this->prio_ == other.prio_ );
80  }
81 };
82 
83 template
84 <typename PRIO>
86 {
87  public:
88  typedef typename std::vector<Task<PRIO> >::iterator TypedIter_t;
89  typedef typename std::vector<Task<PRIO> >::const_iterator TypedConstIter_t;
90 
92  {
93  this->tasks_.clear();
94  }
95 
97  {
98  this->active_task_iter_ = this->tasks_.begin();
99  this->modification_time_ = ros::Time(0);
100  }
101 
102  void clearAllTasks();
103 
104  void addTask(Task<PRIO> t);
105 
106  void deactivateTask(typename std::vector<Task<PRIO> >::iterator it);
107  void deactivateTask(std::string task_id);
108  void activateTask(std::string task_id);
109  void deactivateAllTasks();
110 
111  void activateAllTasks();
112  void activateHighestPrioTask();
113 
114  typename std::vector<Task<PRIO> >::iterator getTasksBegin();
115  typename std::vector<Task<PRIO> >::iterator getTasksEnd();
116  typename std::vector<Task<PRIO> >::iterator nextActiveTask();
117  typename std::vector<Task<PRIO> >::iterator beginTaskIter();
118 
119  int countActiveTasks() const;
120  ros::Time getLastModificationTime() const;
121 
122  private:
123  void updateModificationTime(bool change);
124 
125  std::vector<Task<PRIO> > tasks_;
126  TypedIter_t active_task_iter_;
128 };
129 
130 template <typename PRIO>
132 {
133  int i = 0;
134  for (TypedConstIter_t it = this->tasks_.begin(); it != this->tasks_.end(); it++)
135  {
136  if (it->is_active_)
137  {
138  ++i;
139  }
140  }
141 
142  return i;
143 }
144 
148 template <typename PRIO>
150 {
151  TypedIter_t begin_it = this->tasks_.begin();
152  TypedIter_t mem_it = this->tasks_.end();
153  for (TypedIter_t it = this->tasks_.begin(); it != this->tasks_.end(); it++)
154  {
155  if (it->id_ == t.id_) // task already existent -> ignore add
156  {
157  mem_it = it;
158  it->task_jacobian_ = t.task_jacobian_;
159  it->task_ = t.task_;
160  it->tcp_ = t.tcp_;
161  break;
162  }
163  }
164 
165  if (this->tasks_.end() == mem_it)
166  {
167  for (TypedIter_t it = this->tasks_.begin(); it != this->tasks_.end(); it++)
168  {
169  if (t.prio_ < it->prio_)
170  {
171  mem_it = it;
172  break;
173  }
174  }
175 
176  if (this->tasks_.end() == mem_it)
177  {
178  this->tasks_.push_back(t);
179  }
180  else
181  {
182  this->tasks_.insert(mem_it, t);
183  }
184 
185  this->updateModificationTime(true);
186  }
187 }
188 
189 template <typename PRIO>
191 {
192  bool change = false;
193  for (TypedIter_t it = this->tasks_.begin(); it != this->tasks_.end(); it++)
194  {
195  if (!it->is_active_)
196  {
197  change = true;
198  }
199 
200  it->is_active_ = true;
201  }
202 
203  this->updateModificationTime(change);
204 }
205 
206 template <typename PRIO>
208 {
209  TypedIter_t it = this->tasks_.begin();
210  if (this->tasks_.end() != it)
211  {
212  this->updateModificationTime(!it->is_active_);
213 
214  ROS_WARN_STREAM("Activation of highest prio task in stack: " << it->id_);
215  it->is_active_ = true;
216  }
217 }
218 
219 template <typename PRIO>
220 typename std::vector<Task<PRIO> >::iterator TaskStackController<PRIO>::getTasksBegin()
221 {
222  return this->tasks_.begin();
223 }
224 
225 template <typename PRIO>
226 typename std::vector<Task<PRIO> >::iterator TaskStackController<PRIO>::getTasksEnd()
227 {
228  return this->tasks_.end();
229 }
230 
231 template <typename PRIO>
233 {
234  for (TypedIter_t it = this->tasks_.begin(); it != this->tasks_.end(); it++)
235  {
236  if (it->id_ == task_id)
237  {
238  this->updateModificationTime(!it->is_active_);
239  it->is_active_ = true;
240  break;
241  }
242  }
243 }
244 
245 template <typename PRIO>
246 void TaskStackController<PRIO>::deactivateTask(typename std::vector<Task<PRIO> >::iterator it)
247 {
248  if (std::find(this->tasks_.begin(), this->tasks_.end(), it) != this->tasks_.end())
249  {
250  this->updateModificationTime(it->is_active_);
251  it->is_active_ = false;
252  }
253 }
254 
255 template <typename PRIO>
257 {
258  for (TypedIter_t it = this->tasks_.begin(); it != this->tasks_.end(); it++)
259  {
260  if (it->id_ == task_id)
261  {
262  this->updateModificationTime(it->is_active_);
263  it->is_active_ = false;
264  break;
265  }
266  }
267 }
268 
269 template <typename PRIO>
271 {
272  bool change = false;
273  for (TypedIter_t it = this->tasks_.begin(); it != this->tasks_.end(); it++)
274  {
275  if (it->is_active_)
276  {
277  change = true;
278  }
279 
280  it->is_active_ = false;
281  }
282 
283  this->updateModificationTime(change);
284 }
285 
286 template <typename PRIO>
287 typename std::vector<Task<PRIO> >::iterator TaskStackController<PRIO>::nextActiveTask()
288 {
289  TypedIter_t ret = this->tasks_.end();
290 
291  while (this->tasks_.end() != this->active_task_iter_)
292  {
293  if (this->active_task_iter_->is_active_)
294  {
295  ret = this->active_task_iter_++;
296  break;
297  }
298 
299  this->active_task_iter_++;
300  }
301 
302  return ret;
303 }
304 
305 template <typename PRIO>
306 typename std::vector<Task<PRIO> >::iterator TaskStackController<PRIO>::beginTaskIter()
307 {
308  this->active_task_iter_ = this->tasks_.begin();
309  return this->active_task_iter_;
310 }
311 
312 template <typename PRIO>
314 {
315  this->tasks_.clear();
316  this->active_task_iter_ = this->tasks_.end();
317  this->updateModificationTime(true);
318 }
319 
320 template <typename PRIO>
322 {
323  return this->modification_time_;
324 }
325 
326 template <typename PRIO>
328 {
329  if (change)
330  {
331  this->modification_time_ = ros::Time::now();
332  }
333 }
334 
335 // -------------------- typedefs ---------------------------
338 typedef std::vector<Task_t >::iterator TaskSetIter_t;
339 
340 #endif // COB_TWIST_CONTROLLER_TASK_STACK_TASK_STACK_CONTROLLER_H
Task(const Task &task)
std::string id_
void setPriority(PRIO prio)
Task(PRIO prio, std::string id)
std::vector< Task< PRIO > >::iterator TypedIter_t
void updateModificationTime(bool change)
std::vector< Task< PRIO > > tasks_
TwistControllerParams tcp_
void addTask(Task< PRIO > t)
geometry_msgs::TransformStamped t
void activateTask(std::string task_id)
Task< uint32_t > Task_t
std::vector< Task< PRIO > >::iterator getTasksEnd()
void deactivateTask(typename std::vector< Task< PRIO > >::iterator it)
bool operator>(const Task &other) const
std::vector< Task< PRIO > >::iterator beginTaskIter()
bool operator<(const Task &other) const
std::vector< Task< PRIO > >::iterator nextActiveTask()
TaskStackController< uint32_t > TaskStackController_t
Task(PRIO prio, std::string id, Eigen::MatrixXd task_jacobian, Eigen::VectorXd task)
#define ROS_WARN_STREAM(args)
Eigen::MatrixXd task_jacobian_
ros::Time getLastModificationTime() const
static Time now()
std::vector< Task< PRIO > >::iterator getTasksBegin()
std::vector< Task< PRIO > >::const_iterator TypedConstIter_t
bool operator==(const Task &other) const
Eigen::VectorXd task_
std::vector< Task_t >::iterator TaskSetIter_t


cob_twist_controller
Author(s): Felix Messmer , Marco Bezzon , Christoph Mark , Francisco Moreno
autogenerated on Thu Apr 8 2021 02:40:00