00001 /*************************************************************************** 00002 * Copyright (C) 2005-2011 LpzRobots development team * 00003 * Georg Martius <georg dot martius at web dot de> * 00004 * Frank Guettler <guettler at informatik dot uni-leipzig dot de * 00005 * Frank Hesse <frank at nld dot ds dot mpg dot de> * 00006 * Ralf Der <ralfder at mis dot mpg dot de> * 00007 * * 00008 * This program is free software; you can redistribute it and/or modify * 00009 * it under the terms of the GNU General Public License as published by * 00010 * the Free Software Foundation; either version 2 of the License, or * 00011 * (at your option) any later version. * 00012 * * 00013 * This program is distributed in the hope that it will be useful, * 00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 00016 * GNU General Public License for more details. * 00017 * * 00018 * You should have received a copy of the GNU General Public License * 00019 * along with this program; if not, write to the * 00020 * Free Software Foundation, Inc., * 00021 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * 00022 * * 00023 ***************************************************************************/ 00024 #ifndef __SENSOR_H 00025 #define __SENSOR_H 00026 00027 #include <list> 00028 #include <selforg/types.h> 00029 #include <selforg/stl_adds.h> 00030 #include <selforg/matrix.h> 00031 #include "globaldata.h" 00032 #include "pos.h" 00033 00034 namespace lpzrobots { 00035 00036 // forward declaration 00037 class Primitive; 00038 00039 /** Abstract class for sensors that can be plugged into a robot 00040 */ 00041 class Sensor { 00042 public: 00043 /// defines which dimensions should be sensed. The meaning is sensor specific. 00044 enum Dimensions { X = 1, Y = 2, Z = 4, XY = X | Y, XZ = X | Z, YZ = Y | Z, XYZ = X | Y | Z }; 00045 00046 Sensor() {} 00047 virtual ~Sensor() {} 00048 00049 /** initialises sensor with body of robot. This is usually done by the robot itself. 00050 */ 00051 virtual void init(Primitive* own) = 0; 00052 00053 /** performs sense action 00054 */ 00055 virtual bool sense(const GlobalData& globaldata) = 0; 00056 00057 /** returns the number of sensors values produced by this sensor 00058 */ 00059 virtual int getSensorNumber() const = 0; 00060 00061 /** returns a list of sensor values (usually in the range [0,1] ) 00062 This function should be overloaded. 00063 */ 00064 virtual std::list<sensor> get() const = 0; 00065 00066 /** to update any visual appearance 00067 */ 00068 virtual void update() {}; 00069 00070 /** writes the sensor values (usually in the range [0,1] ) 00071 into the given sensor array and returns the number of sensors written. 00072 A default implementation based on get() is provided. Only of performance 00073 matters overwrite this function. 00074 @param sensors call by refernce array which received the values 00075 @param length capacity of sensors array 00076 @return number of sensor values written 00077 */ 00078 virtual int get(sensor* sensors, int length) const { 00079 const std::list<sensor>& l = get(); 00080 assert(length>=(int)l.size()); 00081 int n=0; 00082 FOREACHC(std::list<sensor>,l,s) 00083 sensors[n++] = *s; 00084 return l.size(); 00085 }; 00086 00087 /// selects the rows specified by dimensions (X->0, Y->1, Z->2) 00088 static std::list<sensor> selectrows(const matrix::Matrix& m, short dimensions) { 00089 std::list<sensor> l; 00090 for(int i=0; i<2; i++){ 00091 if(( 1 <<i ) & dimensions) l += m.row(i).convertToList(); 00092 } 00093 return l; 00094 } 00095 /// selects the rows specified by dimensions (X->0, Y->1, Z->2) 00096 static int selectrows(sensor* sensors, int length, const matrix::Matrix& m, short dimensions) { 00097 int len=0; 00098 for(int i=0; i<3; i++){ 00099 if(( 1 << i) & dimensions) 00100 len+=m.row(i).convertToBuffer(sensors+len, length-len); 00101 } 00102 return len; 00103 } 00104 00105 }; 00106 00107 } 00108 00109 #endif