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 
349  const wVectorT<true>& getPosition() const
350  {
351  return m_transformation.w_pos;
352  }
353 
359  double getMinDistance() const
360  {
361  return m_minDist;
362  }
363 
369  double getMaxDistance() const
370  {
371  return m_maxDist;
372  }
373 
379  double getAperture() const
380  {
381  return m_aperture;
382  }
383 
389  unsigned int getNumRays() const
390  {
391  return m_numRays;
392  }
393 
399  bool isValid() const
400  {
401  return ((m_object != NULL) && (m_numRays != 0));
402  }
403 
404 private:
411  void computeRayPoints();
412 
417  void updateGraphicalRepresentation();
418 
422  WObject* m_object;
423 
428  wMatrix m_transformation;
429 
433  double m_minDist;
434 
438  double m_maxDist;
439 
443  double m_aperture;
444 
448  unsigned int m_numRays;
449 
453  bool m_drawSensor;
454 
458  bool m_drawRay;
459 
464  bool m_drawRealRay;
465 
472  QVector<wVector> m_startingRayPoints;
473 
480  QVector<wVector> m_endingRayPoints;
481 
489  RayCastHit m_rayCastHit;
490 
494  __SingleIR_internal::SingleIRGraphic* m_sensorGraphics;
495 };
496 
505 {
506 public:
513  SimulatedIRProximitySensorController(World* world, const QVector<SingleIR>& sensors);
514 
519 
523  virtual void update();
524 
532  virtual void setSensorActive(int i, bool active);
533 
543  void setGraphicalProperties(bool drawSensor, bool drawRay = false, bool drawRealRay = false);
544 
550  const QVector<SingleIR>& sensors() const
551  {
552  return m_sensors;
553  }
554 
555 private:
559  QVector<SingleIR> m_sensors;
560 
564  bool m_drawSensor;
565 
569  bool m_drawRay;
570 
575  bool m_drawRealRay;
576 };
577 
588 {
589 public:
596  SimulatedIRGroundSensorController(World* world, const QVector<SingleIR>& sensors);
597 
602 
606  virtual void update();
607 
615  virtual void setSensorActive(int i, bool active);
616 
626  void setGraphicalProperties(bool drawSensor, bool drawRay = false, bool drawRealRay = false);
627 
633  const QVector<SingleIR>& sensors() const
634  {
635  return m_sensors;
636  }
637 
638 private:
642  QVector<SingleIR> m_sensors;
643 
647  bool m_drawSensor;
648 
652  bool m_drawRay;
653 
658  bool m_drawRealRay;
659 };
660 
667 class FARSA_WSIM_API TractionSensorController : public SensorController
668 {
669 public:
676  TractionSensorController(World* world, const PhyJoint& j);
677 
682 
686  virtual void update();
687 
693  wVector traction() const;
694 
695 private:
699  const PhyJoint& m_joint;
700 };
701 
702 } // end namespace farsa
703 
704 #endif