Robot Simulator of the Robotics Group for Self-Organization of Control  0.8.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
oderobot.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005-2011 LpzRobots development team *
3  * Georg Martius <georg dot martius at web dot de> *
4  * Frank Guettler <guettler at informatik dot uni-leipzig dot de *
5  * Frank Hesse <frank at nld dot ds dot mpg dot de> *
6  * Ralf Der <ralfder at mis dot mpg dot de> *
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 *
20  * Free Software Foundation, Inc., *
21  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
22  * *
23  ***************************************************************************/
24 #ifndef __ODEROBOT_H
25 #define __ODEROBOT_H
26 
27 #include <vector>
28 #include <memory>
29 
30 #include <selforg/abstractrobot.h>
31 #include <selforg/storeable.h>
32 #include "odehandle.h"
33 #include "osghandle.h"
34 #include "sensor.h"
35 #include "motor.h"
36 #include "globaldata.h"
37 #include "color.h"
38 #include "pos.h"
39 #include "osgforwarddecl.h"
40 #include "tmpprimitive.h"
41 
42 namespace lpzrobots {
43 
44  class Primitive;
45  class Joint;
46 
47  /// structure to hold attachment data for sensors and motors
48  struct Attachment {
49  Attachment(int pI = -1, int jI = -1) : primitiveIndex(pI), jointIndex(jI) {}
52  };
53 
54  typedef std::vector<Primitive*> Primitives;
55  typedef std::vector<Joint*> Joints;
56  typedef std::pair<std::shared_ptr<Sensor>, Attachment> SensorAttachment;
57  typedef std::pair<std::shared_ptr<Motor>, Attachment> MotorAttachment;
58 
59 
60  /**
61  * Abstract class for ODE robots
62  *
63  */
64  class OdeRobot : public AbstractRobot, public Storeable {
65  public:
66 
67  friend class OdeAgent;
68 
69  /**
70  * Constructor
71  */
73  const std::string& name, const std::string& revision);
74 
75  /// calls cleanup()
76  virtual ~OdeRobot();
77 
78  virtual int getSensors(double* sensors, int sensornumber) final;
79  virtual void setMotors(const double* motors, int motornumber) final;
80  virtual int getSensorNumber() final;
81  virtual int getMotorNumber() final;
82 
83  virtual std::list<SensorMotorInfo> getSensorInfos() override;
84 
85  virtual std::list<SensorMotorInfo> getMotorInfos() override;
86 
87  public: // should be protected, but too much refactoring work
88  /** overload this function in a subclass to do specific sensor handling,
89  not needed for generic sensors @see getSensors() @see addSensor() */
90  virtual int getSensorsIntern(double* sensors, int sensornumber) { return 0; };
91 
92  /** overload this function in a subclass to do specific sensor handling,
93  not needed for generic motors @see setMotors() @see addMotor() */
94  virtual void setMotorsIntern(const double* motors, int motorsnumber) { };
95 
96  /** overload this function in a subclass to specific the number of custom sensors @see getSensorNumber() */
97  virtual int getSensorNumberIntern() { return 0; };
98 
99  /** overload this function in a subclass to specific the number of custom sensors @see getMotorNumber() */
100  virtual int getMotorNumberIntern() { return 0; };
101 
102  public:
103  /** adds a sensor to the robot. Must be called before agents initializes, otherwise unknown effect.
104  @param segmentIndex index of segment of robot to which this sensor should be attached
105  */
106  virtual void addSensor(std::shared_ptr<Sensor> sensor, Attachment attachment=Attachment());
107 
108  /** adds a motor to the robot. Must be called before agents initializes, otherwise unknown effect.
109  @param segmentIndex index of segment of robot to which this motor should be attached
110  */
111  virtual void addMotor(std::shared_ptr<Motor> motor, Attachment attachment=Attachment());
112 
113  /// returns all generic sensors with their attachment
114  virtual std::list<SensorAttachment> getAttachedSensors(){
115  return sensors;
116  }
117 
118  /// returns all generic motors with their attachment
119  virtual std::list<MotorAttachment> getAttachedMotors(){
120  return motors;
121  }
122 
123  /// adds a torque sensor to each joint. Call it after placement of robot.
124  virtual void addTorqueSensors(double maxtorque = 1.0, int avg = 1);
125 
126  /// update the OSG notes here, if overwritten, call OdeRobot::update()!
127  virtual void update();
128 
129  /** sets the vehicle to position pos
130  @param pos desired position of the robot
131  */
132  virtual void place(const Pos& pos) final;
133 
134  /** sets the pose of the vehicle
135  @param pose desired 4x4 pose matrix
136  */
137  virtual void place(const osg::Matrix& pose) final;
138 
139  /// wrapper to for @see place() that is to be overloaded
140  virtual void placeIntern(const osg::Matrix& pose) = 0;
141 
142  /** @deprecated This function will be removed in 0.8
143  * Do not use it anymore, collision control is done automatically.
144  * In case of a routine return true
145  * (collision will be ignored by other objects and the default routine)
146  * else false (collision is passed to other objects and (if not treated)
147  * to the default routine).
148  */
149  virtual bool collisionCallback(void *data, dGeomID o1, dGeomID o2){ return false; };
150 
151  /** this function is called each controlstep before control.
152  This is the place the perform active sensing (e.g. Image processing).
153  If you overload this function, call the OdeRobot::sense() function.
154  @param globalData structure that contains global data from the simulation environment
155  */
156  virtual void sense(GlobalData& globalData);
157 
158  /** this function is called in each simulation timestep (always after control). It
159  should perform robot-internal checks and actions
160  like resetting certain sensors or implement velocity dependend friction and the like.
161  The attached Motors should act here (done automatically in OdeRobot);
162  If you overload this function, call the OdeRobot::doInternalStuff() function.
163  @param globalData structure that contains global data from the simulation environment
164  */
165  virtual void doInternalStuff(GlobalData& globalData);
166 
167  /** sets color of the robot
168  @param col Color struct with desired Color
169  */
170  virtual void setColor(const Color& col);
171 
172 
173  /*********** BEGIN TRACKABLE INTERFACE ****************/
174 
175  /** returns position of the object
176  @return vector of position (x,y,z)
177  */
178  virtual Position getPosition() const;
179 
180  /** returns linear speed vector of the object
181  @return vector (vx,vy,vz)
182  */
183  virtual Position getSpeed() const;
184 
185  /** returns angular velocity vector of the object
186  @return vector (wx,wy,wz)
187  */
188  virtual Position getAngularSpeed() const;
189 
190  /** returns the orientation of the object
191  @return 3x3 rotation matrix
192  */
193  virtual matrix::Matrix getOrientation() const;
194  /*********** END TRACKABLE INTERFACE ****************/
195 
196  /// return the primitive of the robot that is used for tracking and camera following
197  virtual Primitive* getMainPrimitive() const {
198  if (!objects.empty()) return objects[0]; else return 0;
199  };
200 
201  /// returns a list of all primitives of the robot (used to store and restore the robot)
202  virtual Primitives getAllPrimitives() const { return objects; };
203 
204  virtual Primitives& getAllPrimitives() { return objects; };
205  /// returns a list of all primitives of the robot (const version) (used to store and restore the robot)
206 
207  /// returns a list of all joints of the robot
208  virtual Joints getAllJoints() const { return joints; };
209 
210  virtual Joints& getAllJoints() { return joints; };
211  /// returns a list of all joints of the robot (const version)
212 
213  /* ********** STORABLE INTERFACE **************** */
214  virtual bool store(FILE* f) const;
215 
216  virtual bool restore(FILE* f);
217  /* ********** END STORABLE INTERFACE ************ */
218 
219  /** relocates robot such its primitive with the given ID
220  is at the new postion (keep current pose).
221  If primitiveID is -1 then the main primitive is used.
222  If primitiveID is -2 then the primitive with the lowest center
223  is used (the center of it, so the bounding box is not checked)
224  */
225  virtual void moveToPosition(Pos pos = Pos(0,0,0.5), int primitiveID = -1);
226  /** relocates robot such its primitive with the given ID
227  is at the new pose (keep relative pose of all primitives).
228  If primitiveID is -1 then the main primitive is used.
229  */
230  virtual void moveToPose(Pose pose, int primitiveID = -1);
231 
232  /// returns the initial pose of the main primitive (use it e.g. with moveToPose)
233  virtual Pose getInitialPose() { return initialPose; }
234  /** returns the initial relative pose of the main primitive
235  (use it with moveToPose to further translate or rotate).
236  If initialized with place(p) then moveToPose(getRelativeInitialPose()*p) whould put
237  the main primitive at the same position and pose.
238  */
240 
241  /** fixates the given primitive of the robot at its current position to the world
242  for a certain time.
243  Hint: use moveToPosition() to get the robot relocated
244  @param primitiveID if -1 then the main primitive is used, otherwise the primitive with the given index
245  @param duration time to fixate in seconds (if ==0 then indefinite)
246  */
247  virtual void fixate(GlobalData& global, int primitiveID=-1, double duration = 0);
248  /// release the robot in case it is fixated and return true in this case
249  virtual bool unFixate(GlobalData& global);
250 
251 
252  protected:
253 
254  static bool isGeomInPrimitiveList(Primitive** ps, int len, dGeomID geom);
255  static bool isGeomInPrimitiveList(std::list<Primitive*> ps, dGeomID geom);
256 
257  void attachSensor(SensorAttachment& sa);
258  void attachMotor(MotorAttachment& ma);
259 
260  /// deletes all objects (primitives) and joints (is called automatically in destructor)
261  virtual void cleanup();
262 
263  protected:
264  /// list of objects (should be populated by subclasses)
266  /// list of joints (should be populated by subclasses)
268 
269  std::list<SensorAttachment> sensors; // list of generic sensors
270  std::list<MotorAttachment> motors; // list of generic motors
271 
273  Pose initialPose; // initial pose of main primitive
274  // initial relative pose of main primitive w.r.t. to given pose in place
276 
279  dSpaceID parentspace;
280 
283  };
284 
285 }
286 
287 #endif
Matrix type.
Definition: matrix.h:65
Pose initialPose
Definition: oderobot.h:273
virtual int getSensors(double *sensors, int sensornumber) final
returns actual sensorvalues
Definition: oderobot.cpp:59
Interface for objects, that can be stored and restored to/from a file stream (binary).
Definition: storeable.h:33
virtual Primitive * getMainPrimitive() const
return the primitive of the robot that is used for tracking and camera following
Definition: oderobot.h:197
Data structure for accessing the ODE.
Definition: odehandle.h:44
virtual bool unFixate(GlobalData &global)
release the robot in case it is fixated and return true in this case
Definition: oderobot.cpp:343
virtual void doInternalStuff(GlobalData &globalData)
this function is called in each simulation timestep (always after control).
Definition: oderobot.cpp:236
structure to hold attachment data for sensors and motors
Definition: oderobot.h:48
virtual Position getPosition() const
returns position of the object
Definition: oderobot.cpp:357
virtual void setColor(const Color &col)
sets color of the robot
Definition: oderobot.cpp:203
static bool isGeomInPrimitiveList(Primitive **ps, int len, dGeomID geom)
Definition: oderobot.cpp:255
Matrixd Matrix
Definition: osgforwarddecl.h:47
virtual bool restore(FILE *f)
loads the object from the given file stream (ASCII preferred).
Definition: oderobot.cpp:412
virtual int getMotorNumberIntern()
overload this function in a subclass to specific the number of custom sensors
Definition: oderobot.h:100
virtual Primitives & getAllPrimitives()
Definition: oderobot.h:204
virtual std::list< SensorMotorInfo > getSensorInfos() override
returns the information for the sensors.
Definition: oderobot.cpp:99
double sensor
Definition: types.h:29
virtual Joints getAllJoints() const
returns a list of all primitives of the robot (const version) (used to store and restore the robot) ...
Definition: oderobot.h:208
virtual int getSensorNumberIntern()
overload this function in a subclass to specific the number of custom sensors
Definition: oderobot.h:97
std::vector< Primitive * > Primitives
Definition: oderobot.h:54
virtual int getSensorsIntern(double *sensors, int sensornumber)
overload this function in a subclass to do specific sensor handling, not needed for generic sensors ...
Definition: oderobot.h:90
OdeRobot(const OdeHandle &odeHandle, const OsgHandle &osgHandle, const std::string &name, const std::string &revision)
Constructor.
Definition: oderobot.cpp:39
virtual void update()
update the OSG notes here, if overwritten, call OdeRobot::update()!
Definition: oderobot.cpp:242
bool initialized
Definition: oderobot.h:281
Definition: pos.h:36
Data structure for accessing the OpenSceneGraph.
Definition: osghandle.h:79
virtual matrix::Matrix getOrientation() const
returns the orientation of the object
Definition: oderobot.cpp:383
virtual void moveToPose(Pose pose, int primitiveID=-1)
relocates robot such its primitive with the given ID is at the new pose (keep relative pose of all pr...
Definition: oderobot.cpp:298
virtual void setMotorsIntern(const double *motors, int motorsnumber)
overload this function in a subclass to do specific sensor handling, not needed for generic motors ...
Definition: oderobot.h:94
Attachment(int pI=-1, int jI=-1)
Definition: oderobot.h:49
Joints joints
list of joints (should be populated by subclasses)
Definition: oderobot.h:267
virtual Pose getRelativeInitialPose()
returns the initial relative pose of the main primitive (use it with moveToPose to further translate ...
Definition: oderobot.h:239
virtual bool store(FILE *f) const
returns a list of all joints of the robot (const version)
Definition: oderobot.cpp:398
virtual void addSensor(std::shared_ptr< Sensor > sensor, Attachment attachment=Attachment())
adds a sensor to the robot.
Definition: oderobot.cpp:152
std::vector< Joint * > Joints
Definition: oderobot.h:55
Definition: position.h:30
virtual void addTorqueSensors(double maxtorque=1.0, int avg=1)
adds a torque sensor to each joint. Call it after placement of robot.
Definition: oderobot.cpp:172
std::pair< std::shared_ptr< Sensor >, Attachment > SensorAttachment
Definition: oderobot.h:56
osg::Matrix Pose
Definition: pose.h:35
virtual bool collisionCallback(void *data, dGeomID o1, dGeomID o2)
Definition: oderobot.h:149
int primitiveIndex
Definition: oderobot.h:50
virtual int getSensorNumber() final
returns number of sensors
Definition: oderobot.cpp:79
void attachSensor(SensorAttachment &sa)
Definition: oderobot.cpp:120
Interface for objects, that can be stored and restored to/from a file stream (binary).
Definition: sensormotorinfo.h:33
virtual void placeIntern(const osg::Matrix &pose)=0
wrapper to for
Definition: oderobot.cpp:54
holding a temporary joint
Definition: tmpprimitive.h:101
virtual int getMotorNumber() final
returns number of motors
Definition: oderobot.cpp:89
Interface class for primitives represented in the physical and graphical world.
Definition: primitive.h:80
virtual Position getSpeed() const
returns linear speed vector of the object
Definition: oderobot.cpp:367
virtual void moveToPosition(Pos pos=Pos(0, 0, 0.5), int primitiveID=-1)
relocates robot such its primitive with the given ID is at the new postion (keep current pose)...
Definition: oderobot.cpp:269
Data structure holding all essential global information.
Definition: globaldata.h:57
Specialised agent for ode robots.
Definition: odeagent.h:62
int jointIndex
Definition: oderobot.h:51
Definition: color.h:32
virtual std::list< SensorAttachment > getAttachedSensors()
returns all generic sensors with their attachment
Definition: oderobot.h:114
std::list< SensorAttachment > sensors
Definition: oderobot.h:269
virtual Joints & getAllJoints()
Definition: oderobot.h:210
virtual Primitives getAllPrimitives() const
returns a list of all primitives of the robot (used to store and restore the robot) ...
Definition: oderobot.h:202
std::pair< std::shared_ptr< Motor >, Attachment > MotorAttachment
Definition: oderobot.h:57
virtual void place(const Pos &pos) final
sets the vehicle to position pos
Definition: oderobot.cpp:210
void attachMotor(MotorAttachment &ma)
Definition: oderobot.cpp:136
OsgHandle osgHandle
Definition: oderobot.h:278
OdeHandle odeHandle
Definition: oderobot.h:277
double motor
Definition: types.h:30
dSpaceID parentspace
Definition: oderobot.h:279
Abstract class for ODE robots.
Definition: oderobot.h:64
virtual Pose getInitialPose()
returns the initial pose of the main primitive (use it e.g. with moveToPose)
Definition: oderobot.h:233
bool askedfornumber
Definition: oderobot.h:282
Pose initialRelativePose
Definition: oderobot.h:275
virtual ~OdeRobot()
calls cleanup()
Definition: oderobot.cpp:50
Primitives objects
list of objects (should be populated by subclasses)
Definition: oderobot.h:265
virtual void cleanup()
deletes all objects (primitives) and joints (is called automatically in destructor) ...
Definition: oderobot.cpp:186
virtual std::list< MotorAttachment > getAttachedMotors()
returns all generic motors with their attachment
Definition: oderobot.h:119
virtual Position getAngularSpeed() const
returns angular velocity vector of the object
Definition: oderobot.cpp:375
virtual void sense(GlobalData &globalData)
this function is called each controlstep before control.
Definition: oderobot.cpp:230
virtual void fixate(GlobalData &global, int primitiveID=-1, double duration=0)
fixates the given primitive of the robot at its current position to the world for a certain time...
Definition: oderobot.cpp:325
std::list< MotorAttachment > motors
Definition: oderobot.h:270
virtual void setMotors(const double *motors, int motornumber) final
sets actual motorcommands
Definition: oderobot.cpp:69
Abstract class (interface) for robot in general.
Definition: abstractrobot.h:41
virtual void addMotor(std::shared_ptr< Motor > motor, Attachment attachment=Attachment())
adds a motor to the robot.
Definition: oderobot.cpp:162
virtual std::list< SensorMotorInfo > getMotorInfos() override
returns the information for the motors.
Definition: oderobot.cpp:109
TmpJoint * fixationTmpJoint
Definition: oderobot.h:272