sensorcontrollers.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 SENSORCONTROLLERS_H
24 #define SENSORCONTROLLERS_H
25 
26 #include "world.h"
27 #include "ownable.h"
28 #include "phyobject.h"
29 #include "phyjoint.h"
30 
31 namespace farsa {
32 
43 class FARSA_WSIM_API SensorController : public Ownable
44 {
45 public:
51  SensorController(World* world);
52 
56  virtual ~SensorController();
57 
64  virtual void update() = 0;
65 
72  bool isEnabled()
73  {
74  return m_enabled;
75  }
76 
83  void setEnabled(bool b)
84  {
85  m_enabled = b;
86  }
87 
93  World* world()
94  {
95  return m_world;
96  }
97 
98 private:
102  World* m_world;
103 
107  bool m_enabled;
108 
114  SensorController(const SensorController& other);
115 
121  SensorController& operator=(const SensorController& other);
122 };
123 
132 class FARSA_WSIM_API IRSensorController : public SensorController
133 {
134 public:
141  IRSensorController(World* world, unsigned int numSensors);
142 
146  virtual ~IRSensorController();
147 
151  int nSensors() const
152  {
153  return m_activations.size();
154  }
155 
163  double activation(int i) const
164  {
165  return m_activations[i];
166  }
167 
174  bool sensorIsActive(int i) const
175  {
176  return m_activeSensor[i];
177  }
178 
187  virtual void setSensorActive(int i, bool active);
188 
189 protected:
193  QVector<double> m_activations;
194 
202  QVector<bool> m_activeSensor;
203 };
204 
205 namespace __SingleIR_internal {
206  class SingleIRGraphic;
207 }
208 
222 class FARSA_WSIM_API SingleIR
223 {
224 public:
231  SingleIR();
232 
246  SingleIR(WObject* obj, wMatrix mtr, double minDist, double maxDist, double aperture, unsigned int numRays);
247 
253  SingleIR(const SingleIR& other);
254 
261  SingleIR& operator=(const SingleIR& other);
262 
266  ~SingleIR();
267 
274  void update();
275 
288  const RayCastHit& getRayCastHit() const
289  {
290  return m_rayCastHit;
291  }
292 
306  void set(WObject* obj, wMatrix mtr, double minDist, double maxDist, double aperture, unsigned int numRays);
307 
318  void setGraphicalProperties(bool drawSensor, bool drawRay = false, bool drawRealRay = false);
319 
325  const WObject* getObject() const
326  {
327  return m_object;
328  }
329 
337  const wMatrix& getTransformation() const
338  {
339  return m_transformation;
340  }
341 
347  double getMinDistance() const
348  {
349  return m_minDist;
350  }
351 
357  double getMaxDistance() const
358  {
359  return m_maxDist;
360  }
361 
367  double getAperture() const
368  {
369  return m_aperture;
370  }
371 
377  unsigned int getNumRays() const
378  {
379  return m_numRays;
380  }
381 
387  bool isValid() const
388  {
389  return ((m_object != NULL) && (m_numRays != 0));
390  }
391 
392 private:
399  void computeRayPoints();
400 
405  void updateGraphicalRepresentation();
406 
410  WObject* m_object;
411 
416  wMatrix m_transformation;
417 
421  double m_minDist;
422 
426  double m_maxDist;
427 
431  double m_aperture;
432 
436  unsigned int m_numRays;
437 
441  bool m_drawSensor;
442 
446  bool m_drawRay;
447 
452  bool m_drawRealRay;
453 
460  QVector<wVector> m_startingRayPoints;
461 
468  QVector<wVector> m_endingRayPoints;
469 
477  RayCastHit m_rayCastHit;
478 
482  __SingleIR_internal::SingleIRGraphic* m_sensorGraphics;
483 };
484 
493 {
494 public:
501  SimulatedIRProximitySensorController(World* world, const QVector<SingleIR>& sensors);
502 
507 
511  virtual void update();
512 
520  virtual void setSensorActive(int i, bool active);
521 
531  void setGraphicalProperties(bool drawSensor, bool drawRay = false, bool drawRealRay = false);
532 
533 private:
537  QVector<SingleIR> m_sensors;
538 
542  bool m_drawSensor;
543 
547  bool m_drawRay;
548 
553  bool m_drawRealRay;
554 };
555 
566 {
567 public:
574  SimulatedIRGroundSensorController(World* world, const QVector<SingleIR>& sensors);
575 
580 
584  virtual void update();
585 
593  virtual void setSensorActive(int i, bool active);
594 
604  void setGraphicalProperties(bool drawSensor, bool drawRay = false, bool drawRealRay = false);
605 
606 private:
610  QVector<SingleIR> m_sensors;
611 
615  bool m_drawSensor;
616 
620  bool m_drawRay;
621 
626  bool m_drawRealRay;
627 };
628 
635 class FARSA_WSIM_API TractionSensorController : public SensorController
636 {
637 public:
644  TractionSensorController(World* world, const PhyJoint& j);
645 
650 
654  virtual void update();
655 
661  wVector traction() const;
662 
663 private:
667  const PhyJoint& m_joint;
668 };
669 
670 } // end namespace farsa
671 
672 #endif