iCub-main
sensors.cpp
Go to the documentation of this file.
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/Bottle.h>
19 #include <yarp/os/BufferedPort.h>
20 #include <yarp/dev/ControlBoardInterfaces.h>
21 #include <yarp/dev/MultipleAnalogSensorsInterfaces.h>
22 #include <yarp/sig/Vector.h>
23 
26 
27 using namespace std;
28 using namespace yarp::os;
29 using namespace yarp::dev;
30 using namespace yarp::sig;
31 using namespace iCub::perception;
32 
33 
34 /************************************************************************/
35 Sensor::Sensor()
36 {
37  name="";
38  source=NULL;
39  configured=false;
40 }
41 
42 
43 /************************************************************************/
44 bool SensorEncoders::configure(void *source, const Property &options)
45 {
46  if ((source==NULL) || !options.check("name") ||
47  !options.check("size") || !options.check("index"))
48  return false;
49 
50  this->source=source;
51  name=options.find("name").asString();
52  size=options.find("size").asInt32();
53  index=options.find("index").asInt32();
54 
55  return configured=true;
56 }
57 
58 
59 /************************************************************************/
60 bool SensorEncoders::getOutput(Value &in) const
61 {
62  if (!configured)
63  return false;
64 
65  Vector vect(size);
66  static_cast<IEncoders*>(source)->getEncoders(vect.data());
67  in=Value(vect[index]);
68 
69  return true;
70 }
71 
72 
73 /************************************************************************/
74 bool SensorEncoderArrays::configure(void *source, const Property &options)
75 {
76  if ((source==NULL) || !options.check("name") || !options.check("num_arrays") ||
77  !options.check("index_array") || !options.check("index_element"))
78  return false;
79 
80  this->source=source;
81  name=options.find("name").asString();
82  num_arrays=options.find("num_arrays").asInt32();
83  index_array=options.find("index_array").asInt32();
84  index_element=options.find("index_element").asInt32();
85 
86  return configured=true;
87 }
88 
89 
90 /************************************************************************/
91 bool SensorEncoderArrays::getOutput(Value &in) const
92 {
93  auto* iencarray=static_cast<IEncoderArrays*>(source);
94  if (!configured)
95  return false;
96 
97  if ((index_array<0) || (index_array>=iencarray->getNrOfEncoderArrays()))
98  return false;
99 
100  if ((index_element<0) || (index_element>=iencarray->getEncoderArraySize(index_array)))
101  return false;
102 
103  if (iencarray->getEncoderArrayStatus(index_array)!=MAS_OK)
104  return false;
105 
106  double stamp;
107  Vector vect(iencarray->getEncoderArraySize(index_array));
108  iencarray->getEncoderArrayMeasure(index_array,vect,stamp);
109  in=Value(vect[index_element]);
110 
111  return true;
112 }
113 
114 
115 /************************************************************************/
116 bool SensorPort::configure(void *source, const Property &options)
117 {
118  if ((source==NULL) || !options.check("name") || !options.check("index"))
119  return false;
120 
121  this->source=source;
122  name=options.find("name").asString();
123  index=options.find("index").asInt32();
124 
125  return configured=true;
126 }
127 
128 
129 /************************************************************************/
130 bool SensorPort::getOutput(Value &in) const
131 {
132  if (configured)
133  {
134  in=static_cast<iCub::perception::Port*>(source)->getValue(index);
135  return true;
136  }
137  else
138  return false;
139 }
140 
141 
142 
143