SmoothTask.h
Go to the documentation of this file.
1 /*
2  * Copyright 2015-2019 CNRS-UM LIRMM, CNRS-AIST JRL
3  */
4 
5 #pragma once
6 
7 #include <functional>
8 
9 namespace mc_tasks
10 {
11 
17 template<typename objT>
18 struct SmoothTask
19 {
20 public:
22  typedef std::function<void(double)> w_set_fn;
24  typedef std::function<double(void)> w_get_fn;
26  typedef std::function<void(const objT &)> obj_set_fn;
28  typedef std::function<const objT(void)> obj_get_fn;
29 
30 public:
53  double weight,
54  const objT & obj,
55  double percent);
56 
63  void update();
64 
75  void reset(double weight, const objT & obj, double percent);
76 
77 public:
82 
83  double weight;
84  objT obj;
85 
86  double stepW;
87  objT stepO;
88 
89  unsigned int nrIter;
90  unsigned int iter;
91 };
92 
93 template<typename objT>
95  w_get_fn w_get,
96  obj_set_fn obj_set,
97  obj_get_fn obj_get,
98  double weight,
99  const objT & obj,
100  double percent)
101 : w_set(w_set), w_get(w_get), obj_set(obj_set), obj_get(obj_get)
102 {
103  reset(weight, obj, percent);
104 }
105 
106 template<typename objT>
107 void SmoothTask<objT>::reset(double weight, const objT & obj, double percent)
108 {
109  this->weight = weight;
110  this->obj = obj;
111 
112  stepW = (weight - w_get()) * percent;
113  stepO = (obj - obj_get()) * percent;
114 
115  nrIter = static_cast<unsigned int>(ceil(1 / percent));
116  iter = 0;
117 }
118 
119 template<typename objT>
121 {
122  if(iter < nrIter)
123  {
124  w_set(w_get() + stepW);
125  obj_set(obj_get() + stepO);
126  iter++;
127  }
128  else
129  {
130  obj_set(obj);
131  w_set(weight);
132  }
133 }
134 
135 } // namespace mc_tasks
Definition: StabilizerStandingState.h:12
SmoothTask allows to smoothly reach a final weight and objective for a given task.
Definition: SmoothTask.h:19
w_get_fn w_get
Definition: SmoothTask.h:79
obj_get_fn obj_get
Definition: SmoothTask.h:81
void update()
Update the task.
Definition: SmoothTask.h:120
double stepW
Definition: SmoothTask.h:86
objT obj
Definition: SmoothTask.h:84
unsigned int iter
Definition: SmoothTask.h:90
std::function< double(void)> w_get_fn
A function to get the current weight of the task.
Definition: SmoothTask.h:24
double weight
Definition: SmoothTask.h:83
std::function< void(const objT &)> obj_set_fn
A function to set the objective of a task.
Definition: SmoothTask.h:26
void reset(double weight, const objT &obj, double percent)
Reset the target weight, target objective and interpolation step.
Definition: SmoothTask.h:107
unsigned int nrIter
Definition: SmoothTask.h:89
SmoothTask(w_set_fn w_set, w_get_fn w_get, obj_set_fn obj_set, obj_get_fn obj_get, double weight, const objT &obj, double percent)
Constructor.
Definition: SmoothTask.h:94
objT stepO
Definition: SmoothTask.h:87
std::function< void(double)> w_set_fn
A function to set the weight of the task.
Definition: SmoothTask.h:22
std::function< const objT(void)> obj_get_fn
A function to get the objective of a task.
Definition: SmoothTask.h:28
w_set_fn w_set
Definition: SmoothTask.h:78
obj_set_fn obj_set
Definition: SmoothTask.h:80