icub-client
homeostasis.h
Go to the documentation of this file.
1 #ifndef HOMEOSTASIS_H
2 #define HOMEOSTASIS_H
3 
4 #include <string>
5 #include <iostream>
6 #include <iomanip>
7 #include <math.h>
8 #include <limits>
9 #include <time.h>
10 
14 class Drive
15 {
16 public:
17  std::string name;
18  std::string key;
19  double period;
21  bool gradient;
22  time_t start_sleep;
23  bool is_sleeping;
24  double time_to_sleep;
25 
26  Drive(std::string d_name, double d_period, std::string d_key="default", double d_value=0.5, double d_homeo_min=0.25, double d_homeo_max=0.75, double d_decay = 0.05, double d_value_min=std::numeric_limits<double>::min(), double d_value_max=std::numeric_limits<double>::max(), bool d_gradient = false) :
27  name(d_name),
28  key(d_key),
29  period(d_period),
30  value(d_value),
31  homeostasisMin(d_homeo_min),
32  homeostasisMax(d_homeo_max),
33  decay(d_decay),
34  valueMin(d_value_min),
35  valueMax(d_value_max),
36  default_value((d_homeo_max + d_homeo_min)/2.),
37  decay_multiplier(1),
38  gradient(d_gradient),
39  start_sleep(time(NULL)),
40  is_sleeping(true),
41  time_to_sleep(1e10)
42  {
43  //todo : check the min/max
44  if (d_value_min == std::numeric_limits<double>::min() && d_value_max == std::numeric_limits<double>::max()){
45  double homeoRange = homeostasisMax - homeostasisMin;
46  valueMin = homeostasisMin - 0.5 * homeoRange;
47  valueMax = homeostasisMax + 0.5 * homeoRange;
48  }
49  }
50 
51  Drive() = delete;
52 
53  void setKey(std::string d_key)
54  {
55  this->key=d_key;
56  }
57  void setValue(double d_value)
58  {
59  this->value=d_value;
60  }
61  void deltaValue(double d_value)
62  {
63  this->value += d_value;
64  }
65 
66  void setHomeostasisMin(double d_homeo_min)
67  {
68  this->homeostasisMin = d_homeo_min;
69  }
70  void deltaHomeostasisMin(double d_homeo_min)
71  {
72  this->homeostasisMin += d_homeo_min;
73  }
74 
75  void setHomeostasisMax(double d_homeo_max)
76  {
77  this->homeostasisMax = d_homeo_max;
78  }
79  void deltaHomeostasisMax(double d_homeo_max)
80  {
81  this->homeostasisMax += d_homeo_max;
82  }
83 
84  void setDecay(double d_decay)
85  {
86  this->decay = d_decay;
87  }
88  void deltaDecay(double d_decay)
89  {
90  this->decay += d_decay;
91  }
92 
93  void setDecayMultiplier(double mult)
94  {
95  this->decay_multiplier = mult;
96  }
97 
102  void sleep(double t) {
103  start_sleep = time(NULL);
104  is_sleeping = true;
105  time_to_sleep = t;
106  }
107 
111  void freeze() {
112  start_sleep = time(NULL);
113  is_sleeping = true;
114  time_to_sleep = 1e10;
115  }
116 
120  void reset(){
121  value = default_value;
122  }
123 
127  void unfreeze() {
128  is_sleeping = false;
129  }
130 
134  void update()
135  {
136  if (is_sleeping) {
137  if (difftime(time(NULL), start_sleep) > time_to_sleep) {
138  is_sleeping = false;
139  }
140  }
141  else if (! ((this->value > valueMax && this->decay<0) || (this->value < valueMin && this->decay>0))) {
142  this->value -= (this->decay * this->decay_multiplier * period);
143  }
144  }
145 
146  double getValue()
147  {
148  return(value);
149  }
150 
151  void setName(std::string n)
152  {
153  this->name = n;
154  }
155 
156  void setGradient(bool b)
157  {
158  this->gradient = b;
159  }
160 
161 };
162 
163 #endif
double default_value
Definition: homeostasis.h:20
void setValue(double d_value)
Definition: homeostasis.h:57
void setHomeostasisMax(double d_homeo_max)
Definition: homeostasis.h:75
void setHomeostasisMin(double d_homeo_min)
Definition: homeostasis.h:66
Drive()=delete
No default constructor!
double time_to_sleep
Timespan to be sleeping.
Definition: homeostasis.h:24
void deltaValue(double d_value)
Definition: homeostasis.h:61
Drive(std::string d_name, double d_period, std::string d_key="default", double d_value=0.5, double d_homeo_min=0.25, double d_homeo_max=0.75, double d_decay=0.05, double d_value_min=std::numeric_limits< double >::min(), double d_value_max=std::numeric_limits< double >::max(), bool d_gradient=false)
Definition: homeostasis.h:26
std::string name
Name of the drive.
Definition: homeostasis.h:17
bool is_sleeping
Whether drive is sleeping.
Definition: homeostasis.h:23
void reset()
reset Set value back to default_value
Definition: homeostasis.h:120
double homeostasisMax
Definition: homeostasis.h:20
double value
Definition: homeostasis.h:20
std::string key
Deprecated, not used anymore.
Definition: homeostasis.h:18
void setGradient(bool b)
Definition: homeostasis.h:156
double homeostasisMin
Definition: homeostasis.h:20
void deltaHomeostasisMax(double d_homeo_max)
Definition: homeostasis.h:79
double getValue()
Definition: homeostasis.h:146
void setName(std::string n)
Definition: homeostasis.h:151
void setDecay(double d_decay)
Definition: homeostasis.h:84
void freeze()
freeze Freeze (sleep) drive indefinitely
Definition: homeostasis.h:111
double valueMin
Definition: homeostasis.h:20
double decay
Definition: homeostasis.h:20
double valueMax
Definition: homeostasis.h:20
void deltaDecay(double d_decay)
Definition: homeostasis.h:88
double decay_multiplier
Definition: homeostasis.h:20
bool gradient
Definition: homeostasis.h:21
void setDecayMultiplier(double mult)
Definition: homeostasis.h:93
void sleep(double t)
sleep Sleep drive for t seconds, i.e.
Definition: homeostasis.h:102
void deltaHomeostasisMin(double d_homeo_min)
Definition: homeostasis.h:70
void unfreeze()
unfreeze Unfreeze drive
Definition: homeostasis.h:127
void setKey(std::string d_key)
Definition: homeostasis.h:53
time_t start_sleep
Time when the drive started to be sleeping.
Definition: homeostasis.h:22
double period
Definition: homeostasis.h:19
void update()
update Let drive decay
Definition: homeostasis.h:134