Robot Simulator of the Robotics Group for Self-Organization of Control  0.8.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
sensor.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005-2011 LpzRobots development team *
3  * Georg Martius <georg dot martius at web dot de> *
4  * Frank Guettler <guettler at informatik dot uni-leipzig dot de *
5  * Frank Hesse <frank at nld dot ds dot mpg dot de> *
6  * Ralf Der <ralfder at mis dot mpg dot de> *
7  * *
8  * This program is free software; you can redistribute it and/or modify *
9  * it under the terms of the GNU General Public License as published by *
10  * the Free Software Foundation; either version 2 of the License, or *
11  * (at your option) any later version. *
12  * *
13  * This program is distributed in the hope that it will be useful, *
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16  * GNU General Public License for more details. *
17  * *
18  * You should have received a copy of the GNU General Public License *
19  * along with this program; if not, write to the *
20  * Free Software Foundation, Inc., *
21  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
22  * *
23  ***************************************************************************/
24 #ifndef __SENSOR_H
25 #define __SENSOR_H
26 
27 #include <list>
28 #include <selforg/types.h>
29 #include <selforg/stl_adds.h>
30 #include <selforg/matrix.h>
31 #include "globaldata.h"
32 #include "sensormotorinfoable.h"
33 #include "pos.h"
34 
35 namespace lpzrobots {
36 
37  // forward declaration
38  class Primitive;
39  class Joint;
40 
41  /** Abstract class for sensors that can be plugged into a robot
42  */
43  class Sensor : public virtual SensorMotorInfoAble {
44  public:
45  /// defines which dimensions should be sensed. The meaning is sensor specific.
46  enum Dimensions { X = 1, Y = 2, Z = 4, XY = X | Y, XZ = X | Z, YZ = Y | Z, XYZ = X | Y | Z };
47 
48  Sensor() {}
49  virtual ~Sensor() {}
50 
51  /** initialises sensor with a body of robot and optionally with a joint.
52  This is usually done by the robot itself (or using the Attachment())
53  */
54  virtual void init(Primitive* own, Joint* joint = 0) = 0;
55 
56  /** performs sense action
57  */
58  virtual bool sense(const GlobalData& globaldata) = 0;
59 
60  /** returns the number of sensors values produced by this sensor
61  */
62  virtual int getSensorNumber() const = 0;
63 
64  /** returns a list of sensor values (usually in the range [-1,1] )
65  This function should be overloaded.
66  If performance matters, implement get(double*, int) and use getListOfArray to implement this.
67  */
68  virtual std::list<sensor> getList() const = 0;
69 
70 
71  /** returns a list of sensor infos (@see SensorMotorInfoAble how to change the names etc) */
72  virtual std::list<SensorMotorInfo> getSensorInfos() const {
73  return getInfos(getSensorNumber());
74  };
75 
76 
77  /** to update any visual appearance
78  */
79  virtual void update() {};
80 
81  /** writes the sensor values (usually in the range [-1,1] )
82  into the given sensor array and returns the number of sensors written.
83  A default implementation based on get() is provided. Only if performance
84  matters overwrite this function.
85  @param sensors call by refernce array which received the values
86  @param length capacity of sensors array
87  @return number of sensor values written
88  */
89  virtual int get(sensor* sensors, int length) const {
90  const std::list<sensor>& l = getList();
91  assert(length>=(int)l.size());
92  int n=0;
93  FOREACHC(std::list<sensor>,l,s)
94  sensors[n++] = *s;
95  return l.size();
96  };
97 
98  /// helper function for performance implementation of list<> get() based on array-get
99  std::list<sensor> getListOfArray() const {
100  int num = getSensorNumber();
101  sensor* s = new sensor[num];
102  get(s,num);
103  std::list<sensor> result(s,s+num);
104  delete[] s;
105  return result;
106  }
107 
108  /// selects the rows specified by dimensions (X->0, Y->1, Z->2)
109  static std::list<sensor> selectrows(const matrix::Matrix& m, short dimensions) {
110  std::list<sensor> l;
111  for(int i=0; i<3; i++){
112  if(( 1 <<i ) & dimensions) l += m.row(i).convertToList();
113  }
114  return l;
115  }
116  /// selects the rows specified by dimensions (X->0, Y->1, Z->2)
117  static int selectrows(sensor* sensors, int length, const matrix::Matrix& m, short dimensions) {
118  int len=0;
119  for(int i=0; i<3; i++){
120  if(( 1 << i) & dimensions)
121  len+=m.row(i).convertToBuffer(sensors+len, length-len);
122  }
123  return len;
124  }
125 
126  // parse a string with "xyz" or "XYZ" for specifing the sensors dimension
127  static Dimensions parseSensorDimension(char* str){
128  int val=0;
129  for(unsigned int i=0; i<strlen(str); i++){
130  switch(str[i]){
131  case 'X':
132  case 'x': val|=X; break;
133  case 'Y':
134  case 'y': val|=Y; break;
135  case 'Z':
136  case 'z': val|=Z; break;
137  }
138  }
139  if(val==0) {
140  fprintf(stderr,"parseSensorDimension:Sensor must have at least one dimension");
141  val = X;
142  }
143  return (Dimensions)val;
144  }
145 
146  // prints sensor dimensions "XYZ"
147  static std::string dimensions2String(short dimensions){
148  std::string s;
149  if((dimensions & X) != 0) s+="X";
150  if((dimensions & Y) != 0) s+="Y";
151  if((dimensions & Z) != 0) s+="Z";
152  return s;
153  }
154 
155 
156  };
157 
158 }
159 
160 #endif
Matrix type.
Definition: matrix.h:65
static Dimensions parseSensorDimension(char *str)
Definition: sensor.h:127
std::list< D > convertToList() const
Definition: matrix.cpp:169
virtual int getSensorNumber() const =0
returns the number of sensors values produced by this sensor
Definition: sensor.h:46
Definition: sensor.h:46
Matrix row(I index) const
Definition: matrix.cpp:110
#define FOREACHC(colltype, coll, it)
Definition: stl_adds.h:21
virtual std::list< SensorMotorInfo > getSensorInfos() const
returns a list of sensor infos (
Definition: sensor.h:72
Definition: sensor.h:46
Definition: joint.h:41
double sensor
Definition: types.h:29
virtual void init(Primitive *own, Joint *joint=0)=0
initialises sensor with a body of robot and optionally with a joint.
virtual void update()
to update any visual appearance
Definition: sensor.h:79
Abstract class for sensors that can be plugged into a robot.
Definition: sensor.h:43
int convertToBuffer(D *buffer, I len) const
stores the content of the matrix (row-wise) in the given buffer
Definition: matrix.cpp:160
Definition: sensor.h:46
Interface class for primitives represented in the physical and graphical world.
Definition: primitive.h:80
Data structure holding all essential global information.
Definition: globaldata.h:57
virtual std::list< sensor > getList() const =0
returns a list of sensor values (usually in the range [-1,1] ) This function should be overloaded...
virtual bool sense(const GlobalData &globaldata)=0
performs sense action
Definition: sensor.h:46
Definition: sensor.h:46
static std::list< sensor > selectrows(const matrix::Matrix &m, short dimensions)
selects the rows specified by dimensions (X->0, Y->1, Z->2)
Definition: sensor.h:109
static int selectrows(sensor *sensors, int length, const matrix::Matrix &m, short dimensions)
selects the rows specified by dimensions (X->0, Y->1, Z->2)
Definition: sensor.h:117
Abstract class for giving names to sensors and motors.
Definition: sensormotorinfoable.h:37
Sensor()
Definition: sensor.h:48
std::list< SensorMotorInfo > getInfos(int number) const
get all infos.
Definition: sensormotorinfoable.h:81
Dimensions
defines which dimensions should be sensed. The meaning is sensor specific.
Definition: sensor.h:46
Definition: sensor.h:46
std::list< sensor > getListOfArray() const
helper function for performance implementation of list<> get() based on array-get ...
Definition: sensor.h:99
static std::string dimensions2String(short dimensions)
Definition: sensor.h:147
virtual ~Sensor()
Definition: sensor.h:49