iol
utils.cpp
1 /*
2  * Copyright (C) 2011 Department of Robotics Brain and Cognitive Sciences - Istituto Italiano di Tecnologia
3  * Author: Ugo Pattacini
4  * email: ugo.pattacini@iit.it
5  * Permission is granted to copy, distribute, and/or modify this program
6  * under the terms of the GNU General Public License, version 2 or any
7  * later version published by the Free Software Foundation.
8  *
9  * A copy of the license can be found at
10  * http://www.robotcub.org/icub/license/gpl.txt
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
15  * Public License for more details
16 */
17 
18 #include <yarp/os/Time.h>
19 
20 #include "utils.h"
21 #include "module.h"
22 
23 
24 /**********************************************************/
25 void Speaker::speak(const string &phrase, const bool force)
26 {
27  if ((force || speaking) && (getOutputCount()>0))
28  {
29  Bottle request;
30  request.addString(phrase);
31  write(request);
32  }
33 }
34 
35 
36 /**********************************************************/
37 void PointedLocationPort::onRead(Bottle &b)
38 {
39  if (b.size()>1)
40  {
41  loc.x=(int)b.get(0).asFloat64();
42  loc.y=(int)b.get(1).asFloat64();
43  rxTime=Time::now();
44  }
45 }
46 
47 
48 /**********************************************************/
49 PointedLocationPort::PointedLocationPort()
50 {
51  useCallback();
52  rxTime=-1.0;
53  timeout=5.0;
54 }
55 
56 
57 /**********************************************************/
58 bool PointedLocationPort::getLoc(cv::Point &loc)
59 {
60  double t0=Time::now();
61 
62  if ((rxTime>0.0) && (t0-rxTime<timeout))
63  {
64  loc=this->loc;
65  return true;
66  }
67 
68  while (Time::now()-t0<timeout)
69  {
70  if ((rxTime>0.0) && (Time::now()-rxTime<timeout))
71  {
72  loc=this->loc;
73  return true;
74  }
75 
76  Time::delay(0.1);
77  }
78 
79  return false;
80 }
81 
82 
83 /**********************************************************/
84 void StopCmdPort::onRead(Bottle &b)
85 {
86  if (manager!=NULL)
87  {
88  if (b.size()==2)
89  {
90  string command=b.get(0).asString();
91  double confidence=b.get(1).asFloat64();
92 
93  if ((command=="icub-stop-now") && (confidence>0.8))
94  manager->interruptMotor();
95  }
96  }
97 }
98 
99 
100 /**********************************************************/
101 StopCmdPort::StopCmdPort()
102 {
103  useCallback();
104  manager=NULL;
105 }
106 
107 
108 /**********************************************************/
109 void StopCmdPort::setManager(Manager *manager)
110 {
111  this->manager=manager;
112 }
113 
114 
115 /**********************************************************/
116 Attention::Attention() : PeriodicThread(4.0)
117 {
118  manager=NULL;
119 }
120 
121 
122 /**********************************************************/
123 void Attention::setManager(Manager *manager)
124 {
125  this->manager=manager;
126 }
127 
128 
129 /**********************************************************/
130 bool Attention::threadInit()
131 {
132  return (manager!=NULL);
133 }
134 
135 
136 /**********************************************************/
137 void Attention::suspend()
138 {
139  if (!isSuspended())
140  {
141  manager->mutexAttention.lock();
142  PeriodicThread::suspend();
143  manager->mutexAttention.unlock();
144  }
145 }
146 
147 
148 /**********************************************************/
149 void Attention::run()
150 {
151  manager->switchAttention();
152 }
153 
154 
155 /**********************************************************/
156 RtLocalization::RtLocalization() : PeriodicThread(0.03)
157 {
158  manager=NULL;
159 }
160 
161 
162 /**********************************************************/
163 void RtLocalization::setManager(Manager *manager)
164 {
165  this->manager=manager;
166 }
167 
168 
169 /**********************************************************/
170 bool RtLocalization::threadInit()
171 {
172  return ((manager!=NULL) && (getPeriod()>0.0));
173 }
174 
175 
176 /**********************************************************/
177 void RtLocalization::run()
178 {
179  manager->doLocalization();
180 }
181 
182 
183 /**********************************************************/
184 Exploration::Exploration() : PeriodicThread(0.03)
185 {
186  manager=NULL;
187 }
188 
189 
190 /**********************************************************/
191 void Exploration::setManager(Manager *manager)
192 {
193  this->manager=manager;
194 }
195 
196 
197 /**********************************************************/
198 void Exploration::setInfo(const string &object,
199  const Vector &position)
200 {
201  this->object=object;
202  this->position=position;
203 }
204 
205 
206 /**********************************************************/
207 bool Exploration::threadInit()
208 {
209  return (manager!=NULL);
210 }
211 
212 
213 /**********************************************************/
214 void Exploration::run()
215 {
216  manager->doExploration(object,position);
217 }
218 
219 
220 /**********************************************************/
221 MemoryUpdater::MemoryUpdater() : PeriodicThread(0.1)
222 {
223  manager=NULL;
224 }
225 
226 
227 /**********************************************************/
228 void MemoryUpdater::setManager(Manager *manager)
229 {
230  this->manager=manager;
231 }
232 
233 
234 /**********************************************************/
235 bool MemoryUpdater::threadInit()
236 {
237  return ((manager!=NULL) && (getPeriod()>0.0));
238 }
239 
240 
241 /**********************************************************/
242 void MemoryUpdater::run()
243 {
244  manager->updateMemory();
245 }
246 
247 
248 /**********************************************************/
249 MemoryReporter::MemoryReporter()
250 {
251  manager=NULL;
252 }
253 
254 
255 /**********************************************************/
256 void MemoryReporter::setManager(Manager *manager)
257 {
258  this->manager=manager;
259 }
260 
261 
262 /**********************************************************/
263 void MemoryReporter::report(const PortInfo &info)
264 {
265  if ((manager!=NULL) && info.created && !info.incoming)
266  manager->scheduleLoadMemory=true;
267 }
268 
269 
270 
271