karma
All Modules
utils.cpp
1 /*
2  * Copyright (C) 2011 Department of Robotics Brain and Cognitive Sciences - Istituto Italiano di Tecnologia
3  * Author: Vadim Tikhanoff Ugo Pattacini
4  * email: vadim.tikhanoff@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 "iCub/utils.h"
21 #include "iCub/module.h"
22 
23 using namespace std;
24 using namespace yarp::os;
25 
26 /**********************************************************/
27 ParticleFilter::ParticleFilter()
28 {
29  useCallback();
30 }
31 /**********************************************************/
32 void ParticleFilter::onRead(Bottle &target)
33 {
34  //CvPoint trackLoc;
35  if (target.size()>1)
36  {
37  loc.x=(int)target.get(0).asDouble();
38  loc.y=(int)target.get(1).asDouble();
39  //fprintf( stdout, "particle something: %s \n\n",target.toString().c_str() );
40  }
41 }
42 /**********************************************************/
43 bool ParticleFilter::getTraker(cv::Point &loc)
44 {
45  loc=this->loc;
46  return true;
47 }
48 /**********************************************************/
49 void SegmentationPoint::segment(Bottle &b)
50 {
51  if (getOutputCount()>0)
52  {
53  //send 2D x y coordinates to segmentator
54  Bottle request;
55  request.addDouble(b.get(0).asDouble());
56  request.addDouble(b.get(1).asDouble());
57  write(request);
58  }
59 }
60 /**********************************************************/
61 void PointedLocation::onRead(Bottle &b)
62 {
63  fprintf(stdout, "got read from points size %d \n",b.size());
64  if (b.size()>1)
65  {
66  loc.x=(int)b.get(0).asDouble();
67  loc.y=(int)b.get(1).asDouble();
68  rxTime=Time::now();
69  }
70 }
71 /**********************************************************/
72 PointedLocation::PointedLocation()
73 {
74  useCallback();
75  rxTime=-1.0;
76  timeout=2.0;
77 }
78 /**********************************************************/
79 bool PointedLocation::getLoc(cv::Point &loc)
80 {
81  double t0=Time::now();
82 
83  if ((rxTime>0.0) && (t0-rxTime<timeout))
84  {
85  loc=this->loc;
86  return true;
87  }
88 
89  while (Time::now()-t0<timeout)
90  {
91  if ((rxTime>0.0) && (Time::now()-rxTime<timeout))
92  {
93  loc=this->loc;
94  return true;
95  }
96  Time::delay(0.1);
97  }
98  return false;
99 }