sensors.h
1 /********************************************************************************
2  * FARSA Experimentes Library *
3  * Copyright (C) 2007-2012 *
4  * Gianluca Massera <emmegian@yahoo.it> *
5  * Stefano Nolfi <stefano.nolfi@istc.cnr.it> *
6  * Tomassino Ferrauto <tomassino.ferrauto@istc.cnr.it> *
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 Free Software *
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA *
21  ********************************************************************************/
22 
23 #ifndef SENSORS_H
24 #define SENSORS_H
25 
26 #include "experimentsconfig.h"
27 #include "neuroninterfaces.h"
28 #include "world.h"
29 #include "robots.h"
30 #include "wheeledexperimenthelper.h"
31 #include "baseexception.h"
32 #include "helperresources.h"
33 #include "intervals.h"
34 #include <QVector>
35 
36 namespace farsa {
37 
63 class FARSA_EXPERIMENTS_API FakeSensor : public Sensor
64 {
65 public:
72  FakeSensor(ConfigurationParameters& params, QString prefix);
73 
77  ~FakeSensor();
78 
86  virtual void save(ConfigurationParameters& params, QString prefix);
87 
93  static void describe(QString type);
94 
98  virtual void update();
99 
105  virtual int size();
106 
118  virtual void shareResourcesWith(ResourcesUser* other);
119 
120 protected:
128  virtual void resourceChanged(QString resourceName, ResourceChangeType changeType);
129 
134 
140 
146 
151 };
152 
156 class FARSA_EXPERIMENTS_API ObjectPositionSensor : public Sensor {
157 public:
162  ObjectPositionSensor( ConfigurationParameters& params, QString prefix );
169  void save( ConfigurationParameters& params, QString prefix );
171  static void describe( QString type );
173  void update();
175  int size();
176 protected:
182  void resourceChanged(QString resourceName, ResourceChangeType changeType);
186  QString objectName;
188  bool linearize;
190  wVector bbMin;
192  wVector bbMax;
193 };
194 
198 namespace LinearCameraOld {
199  namespace __LinearCamera_internal {
200  class LinearCameraGraphic;
201  }
202 
232  class FARSA_EXPERIMENTS_API LinearCamera : public ConcurrentResourcesUser
233  {
234  public:
250  LinearCamera(WObject* obj, wMatrix mtr, double aperture, unsigned int numReceptors, double maxDistance, QColor backgroundColor);
251 
255  virtual ~LinearCamera();
256 
260  void update();
261 
268  const QColor& colorForReceptor(int i) const
269  {
270  return m_receptors[i];
271  }
272 
278  double getAperture() const
279  {
280  return m_aperture;
281  }
282 
288  unsigned int getNumReceptors() const
289  {
290  return m_numReceptors;
291  }
292 
299  QColor getBackgroundColor() const
300  {
301  return m_backgroundColor;
302  }
303 
310  void setBackgroundColor(QColor color)
311  {
312  m_backgroundColor = color;
313  }
314 
320  void drawCamera(bool d);
321 
328  void ignoreWalls(bool ignore)
329  {
330  m_ignoreWalls = ignore;
331  }
332 
339  bool wallsIgnored() const
340  {
341  return m_ignoreWalls;
342  }
343 
344  private:
352  virtual void resourceChanged(QString resourceName, ResourceChangeType changeType);
353 
357  QVector<QColor> m_receptors;
358 
363  WObject* const m_object;
364 
372  const wMatrix m_transformation;
373 
377  const double m_aperture;
378 
382  const unsigned int m_numReceptors;
383 
387  const double m_maxDistance;
388 
394  QColor m_backgroundColor;
395 
401  const double m_apertureMin;
402 
408  const double m_apertureMax;
409 
415  const double m_receptorRange;
416 
420  Arena* m_arena;
421 
425  bool m_drawCamera;
426 
432  bool m_ignoreWalls;
433 
440  };
441 
442 }
443 
447 namespace LinearCameraNew {
448  namespace __LinearCamera_internal {
449  class LinearCameraGraphic;
450  }
451 
487  class FARSA_EXPERIMENTS_API LinearCamera : public ConcurrentResourcesUser
488  {
489  public:
507  LinearCamera(WObject* obj, wMatrix mtr, double aperture, unsigned int numReceptors, double maxDistance, QColor backgroundColor);
508 
526  LinearCamera(WObject* obj, wMatrix mtr, QVector<SimpleInterval> receptorsRanges, double maxDistance, QColor backgroundColor);
527 
531  virtual ~LinearCamera();
532 
536  void update();
537 
544  const QColor& colorForReceptor(int i) const
545  {
546  return m_receptors[i];
547  }
548 
554  unsigned int getNumReceptors() const
555  {
556  return m_receptors.size();
557  }
558 
565  QColor getBackgroundColor() const
566  {
567  return m_backgroundColor;
568  }
569 
576  void setBackgroundColor(QColor color)
577  {
578  m_backgroundColor = color;
579  }
580 
586  void drawCamera(bool d);
587 
594  void ignoreWalls(bool ignore)
595  {
596  m_ignoreWalls = ignore;
597  }
598 
605  bool wallsIgnored() const
606  {
607  return m_ignoreWalls;
608  }
609 
610  private:
618  virtual void resourceChanged(QString resourceName, ResourceChangeType changeType);
619 
623  QVector<QColor> m_receptors;
624 
629  WObject* const m_object;
630 
638  const wMatrix m_transformation;
639 
643  const QVector<SimpleInterval> m_receptorsRanges;
644 
648  const double m_maxDistance;
649 
655  QColor m_backgroundColor;
656 
660  Arena* m_arena;
661 
665  bool m_drawCamera;
666 
672  bool m_ignoreWalls;
673 
680  };
681 }
682 
686 typedef LinearCameraOld::LinearCamera LinearCamera;
687 // typedef LinearCameraNew::LinearCamera LinearCamera;
688 
693 class FARSA_EXPERIMENTS_TEMPLATE SampleFileLoadingException : public BaseException
694 {
695 public:
706  SampleFileLoadingException(const char* filename, const char* description) throw() :
707  BaseException()
708  {
709  strncpy(m_filename, filename, 256);
710  m_filename[255] = '\0';
711  strncpy(m_description, description, 256);
712  m_description[255] = '\0';
713  sprintf(m_errorMessage, "Error when loading the file with IR samples \"%s\". Description: %s", m_filename, m_description);
714  m_errorMessage[1023] = '\0';
715  }
716 
723  BaseException(other)
724  {
725  strncpy(m_filename, other.m_filename, 256);
726  m_filename[255] = '\0';
727  strncpy(m_description, other.m_description, 256);
728  m_description[255] = '\0';
729  strncpy(m_errorMessage, other.m_errorMessage, 1024);
730  m_errorMessage[1023] = '\0';
731  }
732 
739  {
740  if (&other == this) {
741  return *this;
742  }
743 
744  BaseException::operator=(other);
745  strncpy(m_filename, other.m_filename, 256);
746  m_filename[255] = '\0';
747  strncpy(m_description, other.m_description, 256);
748  m_description[255] = '\0';
749  strncpy(m_errorMessage, other.m_errorMessage, 1024);
750  m_errorMessage[1023] = '\0';
751 
752  return *this;
753  }
754 
758  virtual ~SampleFileLoadingException() throw()
759  {
760  }
761 
767  virtual const char *what() const throw()
768  {
769  return m_errorMessage;
770  }
771 
777  const char *filename() const throw()
778  {
779  return m_filename;
780  }
781 
787  const char *description() const throw()
788  {
789  return m_description;
790  }
791 
796  EXCEPTION_HELPER_FUNCTIONS(SampleFileLoadingException)
797 
798 private:
802  char m_filename[256];
803 
807  char m_description[256];
808 
812  char m_errorMessage[1024];
813 };
814 
849 class FARSA_EXPERIMENTS_API SampledIRDataLoader
850 {
851 public:
857  SampledIRDataLoader(QString filename);
858 
862  ~SampledIRDataLoader();
863 
869  const QString& filename() const
870  {
871  return m_filename;
872  }
873 
879  unsigned int numIR() const
880  {
881  return m_numIR;
882  }
883 
889  unsigned int numSamplingAngles() const
890  {
891  return m_numSamplingAngles;
892  }
893 
899  unsigned int numDistances() const
900  {
901  return m_numDistances;
902  }
903 
910  {
911  return m_initialDistance;
912  }
913 
920  {
921  return m_distanceInterval;
922  }
923 
930  {
931  return m_finalDistance;
932  }
933 
944  unsigned int getActivation(unsigned int i, real dist, real ang) const;
945 
958  QVector<unsigned int>::const_iterator getActivation(real dist, real ang) const;
959 
960 private:
969  unsigned int getLinearIndex(unsigned int id, unsigned int ang, unsigned int dist) const;
970 
974  const QString m_filename;
975 
979  unsigned int m_numIR;
980 
984  unsigned int m_numSamplingAngles;
985 
989  unsigned int m_numDistances;
990 
994  real m_initialDistance;
995 
999  real m_distanceInterval;
1000 
1004  real m_finalDistance;
1005 
1012  QVector<unsigned int> m_activations;
1013 
1020  QVector<unsigned int> m_nullActivations;
1021 };
1022 
1034 QColor FARSA_EXPERIMENTS_API getColorAtArenaGroundPosition(Arena* arena, wVector pos);
1035 
1036 } // end namespace farsa
1037 
1038 #endif
const QString m_additionalInputsResource
The name of the resource associated with the vector of additional inputs.
Definition: sensors.h:145
real initialDistance() const
Returns the initial distance in meters.
Definition: sensors.h:909
void setBackgroundColor(QColor color)
Sets the background color.
Definition: sensors.h:310
wVector bbMax
maximum 3D point for linearization
Definition: sensors.h:192
An helper class to load sampled data from file.
Definition: sensors.h:849
bool wallsIgnored() const
Whether walls are ignored or not.
Definition: sensors.h:605
real distanceInterval() const
Returns the distance interval in meters.
Definition: sensors.h:919
The class modelling an arena.
Definition: arena.h:50
wVector bbMin
minimum 3D point for linearization
Definition: sensors.h:190
The base abstract class for iterating over neurons of a neural network.
unsigned int numDistances() const
Returns the number of different distances from the object.
Definition: sensors.h:899
virtual ~SampleFileLoadingException()
Destructor.
Definition: sensors.h:758
QString objectName
the object resource name
Definition: sensors.h:186
virtual const char * what() const
Returns a C string describing the exception.
Definition: sensors.h:767
This file contains the common type defitions used on the whole framework.
const QColor & colorForReceptor(int i) const
Returns the color perceived by the i-th receptor.
Definition: sensors.h:268
const char * description() const
Returns the description of the error.
Definition: sensors.h:787
The base abstract class for the Sensor hierarchy.
unsigned int numIR() const
Returns the number of IR sensors as read from the file.
Definition: sensors.h:879
SampleFileLoadingException & operator=(const SampleFileLoadingException &other)
Copy operator.
Definition: sensors.h:738
The exception thrown when an error occurs during IR sample files loading.
Definition: sensors.h:693
real finalDistance() const
Returns the furthest sampled distance in meters.
Definition: sensors.h:929
void ignoreWalls(bool ignore)
Sets whether walls are ignored or not.
Definition: sensors.h:328
It gets the three absolute coordinate (world frame) of an object.
Definition: sensors.h:156
bool wallsIgnored() const
Whether walls are ignored or not.
Definition: sensors.h:339
The graphical representation of the linear camera.
Definition: sensors.cpp:757
An helper class for linear camera sensors of various robots.
Definition: sensors.h:232
ResourceVector< real > m_additionalInputs
The vector with additional inputs.
Definition: sensors.h:133
QColor getBackgroundColor() const
Returns the background color.
Definition: sensors.h:299
void ignoreWalls(bool ignore)
Sets whether walls are ignored or not.
Definition: sensors.h:594
float real
A class to add input neurons that can be used for custom operations.
Definition: sensors.h:63
SampleFileLoadingException(const SampleFileLoadingException &other)
Copy constructor.
Definition: sensors.h:722
void setBackgroundColor(QColor color)
Sets the background color.
Definition: sensors.h:576
const QString m_neuronsIteratorResource
The name of th resource associated with the neural network iterator.
Definition: sensors.h:139
unsigned int getNumReceptors() const
Returns the number of receptors.
Definition: sensors.h:288
QColor getBackgroundColor() const
Returns the background color.
Definition: sensors.h:565
NeuronsIterator * m_neuronsIterator
The object to iterate over neurons of the neural network.
Definition: sensors.h:150
const char * filename() const
Returns the name of the file with sampled data.
Definition: sensors.h:777
unsigned int getNumReceptors() const
Returns the number of receptors.
Definition: sensors.h:554
QString neuronsIteratorResource
The name of th resource associated with the neural network iterator.
Definition: sensors.h:184
unsigned int numSamplingAngles() const
Returns the number of different sampling angles.
Definition: sensors.h:889
const QColor & colorForReceptor(int i) const
Returns the color perceived by the i-th receptor.
Definition: sensors.h:544
SampleFileLoadingException(const char *filename, const char *description)
Constructor.
Definition: sensors.h:706
An helper class for linear camera sensors of various robots.
Definition: sensors.h:487
bool linearize
if true will use bbMin and bbMax to linearize the position into [0,1]
Definition: sensors.h:188
The graphical representation of the linear camera.
Definition: sensors.cpp:245
const QString & filename() const
Returns the name of the file from which samples are loaded.
Definition: sensors.h:869
double getAperture() const
Returns the aperture of the sensor in radiants.
Definition: sensors.h:278