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 
34 namespace farsa {
35 
61 class FARSA_EXPERIMENTS_API FakeSensor : public Sensor
62 {
63 public:
70  FakeSensor(ConfigurationParameters& params, QString prefix);
71 
75  ~FakeSensor();
76 
84  virtual void save(ConfigurationParameters& params, QString prefix);
85 
91  static void describe(QString type);
92 
96  virtual void update();
97 
103  virtual int size();
104 
116  virtual void shareResourcesWith(ResourcesUser* other);
117 
118 protected:
126  void resourceChanged(QString resourceName, ResourceChangeType changeType);
127 
132 
138 
144 
149 };
150 
154 class FARSA_EXPERIMENTS_API ObjectPositionSensor : public Sensor {
155 public:
160  ObjectPositionSensor( ConfigurationParameters& params, QString prefix );
167  void save( ConfigurationParameters& params, QString prefix );
169  static void describe( QString type );
171  void update();
173  int size();
174 protected:
180  void resourceChanged(QString resourceName, ResourceChangeType changeType);
184  QString objectName;
186  bool linearize;
188  wVector bbMin;
190  wVector bbMax;
191 };
192 
193 namespace __LinearCamera_internal {
194  class LinearCameraGraphic;
195 }
196 
223 class FARSA_EXPERIMENTS_API LinearCamera : public ConcurrentResourcesUser
224 {
225 public:
239  LinearCamera(WObject* obj, wMatrix mtr, double aperture, unsigned int numReceptors, QColor backgroundColor);
240 
244  virtual ~LinearCamera();
245 
249  void update();
250 
257  const QColor& colorForReceptor(int i) const
258  {
259  return m_receptors[i];
260  }
261 
267  double getAperture() const
268  {
269  return m_aperture;
270  }
271 
277  unsigned int getNumReceptors() const
278  {
279  return m_numReceptors;
280  }
281 
288  QColor getBackgroundColor() const
289  {
290  return m_backgroundColor;
291  }
292 
299  void setBackgroundColor(QColor color)
300  {
301  m_backgroundColor = color;
302  }
303 
309  void drawCamera(bool d);
310 
311 private:
319  virtual void resourceChanged(QString resourceName, ResourceChangeType changeType);
320 
324  QVector<QColor> m_receptors;
325 
330  WObject* const m_object;
331 
339  const wMatrix m_transformation;
340 
344  const double m_aperture;
345 
349  const unsigned int m_numReceptors;
350 
356  QColor m_backgroundColor;
357 
363  const double m_apertureMin;
364 
370  const double m_apertureMax;
371 
377  const double m_receptorRange;
378 
382  Arena* m_arena;
383 
387  bool m_drawCamera;
388 
395 };
396 
401 class FARSA_EXPERIMENTS_TEMPLATE SampleFileLoadingException : public BaseException
402 {
403 public:
414  SampleFileLoadingException(const char* filename, const char* description) throw() :
415  BaseException()
416  {
417  strncpy(m_filename, filename, 256);
418  m_filename[255] = '\0';
419  strncpy(m_description, description, 256);
420  m_description[255] = '\0';
421  sprintf(m_errorMessage, "Error when loading the file with IR samples \"%s\". Description: %s", m_filename, m_description);
422  m_errorMessage[1023] = '\0';
423  }
424 
431  BaseException(other)
432  {
433  strncpy(m_filename, other.m_filename, 256);
434  m_filename[255] = '\0';
435  strncpy(m_description, other.m_description, 256);
436  m_description[255] = '\0';
437  strncpy(m_errorMessage, other.m_errorMessage, 1024);
438  m_errorMessage[1023] = '\0';
439  }
440 
447  {
448  if (&other == this) {
449  return *this;
450  }
451 
452  BaseException::operator=(other);
453  strncpy(m_filename, other.m_filename, 256);
454  m_filename[255] = '\0';
455  strncpy(m_description, other.m_description, 256);
456  m_description[255] = '\0';
457  strncpy(m_errorMessage, other.m_errorMessage, 1024);
458  m_errorMessage[1023] = '\0';
459 
460  return *this;
461  }
462 
466  virtual ~SampleFileLoadingException() throw()
467  {
468  }
469 
475  virtual const char *what() const throw()
476  {
477  return m_errorMessage;
478  }
479 
485  const char *filename() const throw()
486  {
487  return m_filename;
488  }
489 
495  const char *description() const throw()
496  {
497  return m_description;
498  }
499 
504  EXCEPTION_HELPER_FUNCTIONS(SampleFileLoadingException)
505 
506 private:
510  char m_filename[256];
511 
515  char m_description[256];
516 
520  char m_errorMessage[1024];
521 };
522 
557 class FARSA_EXPERIMENTS_API SampledIRDataLoader
558 {
559 public:
565  SampledIRDataLoader(QString filename);
566 
570  ~SampledIRDataLoader();
571 
577  const QString& filename() const
578  {
579  return m_filename;
580  }
581 
587  unsigned int numIR() const
588  {
589  return m_numIR;
590  }
591 
597  unsigned int numSamplingAngles() const
598  {
599  return m_numSamplingAngles;
600  }
601 
607  unsigned int numDistances() const
608  {
609  return m_numDistances;
610  }
611 
617  real initialDistance() const
618  {
619  return m_initialDistance;
620  }
621 
627  real distanceInterval() const
628  {
629  return m_distanceInterval;
630  }
631 
637  real finalDistance() const
638  {
639  return m_finalDistance;
640  }
641 
652  unsigned int getActivation(unsigned int i, real dist, real ang) const;
653 
666  QVector<unsigned int>::const_iterator getActivation(real dist, real ang) const;
667 
668 private:
677  unsigned int getLinearIndex(unsigned int id, unsigned int ang, unsigned int dist) const;
678 
682  const QString m_filename;
683 
687  unsigned int m_numIR;
688 
692  unsigned int m_numSamplingAngles;
693 
697  unsigned int m_numDistances;
698 
702  real m_initialDistance;
703 
707  real m_distanceInterval;
708 
712  real m_finalDistance;
713 
720  QVector<unsigned int> m_activations;
721 
728  QVector<unsigned int> m_nullActivations;
729 };
730 
731 } // end namespace farsa
732 
733 #endif