Computational Embodied Neuroscience Simulator  1.1
3D simulation library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends
cens_serialized_engine.h
Go to the documentation of this file.
1 // Computational Embodied Neuroscience Simulator (CENS) Library
2 // Copyright (c) 2013 Francesco Mannella
3 //
4 // cens_engine.h
5 // Copyright (c) 2013 Francesco Mannella
6 //
7 // This file is part of CENS library.
8 //
9 // CENS library is free software: you can redistribute it and/or modify
10 // it under the terms of the GNU General Public License as published by
11 // the Free Software Foundation, either version 3 of the License, or
12 // (at your option) any later version.
13 //
14 // CENS library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 // GNU General Public License for more details.
18 //
19 // You should have received a copy of the GNU General Public License
20 // along with CENS library. If not, see <http://www.gnu.org/licenses/>.
21 
22 #ifndef CENS_SERIALIZED_ENGINE_H
23 #define CENS_SERIALIZED_ENGINE_H
24 
25 #include "cens_engine.h"
26 #include "cens_parameter_manager.h"
27 #include <BulletWorldImporter/btBulletWorldImporter.h>
28 #include <vector>
29 #include <map>
30 #include <string>
31 #include <sstream>
32 
33 
34 namespace cens
35 {
36 
39 
40  // Inherited by btHashMap to get the array of keys
41  template <class Key, class Value>
42  class CENSHashMap : public btHashMap<Key, Value>
43  {
44  public:
45  const Key &getKeyAtIndex(int index)
46  {
47  return (btHashMap<Key, Value>::m_keyArray)[index];
48  }
49  };
50 
53 
54  // Inherited from btBulletWorldImporter to get
55  // the protected maps of rigid bodies and constraints
56  // when loading a bullet file
57  class CENSImporter : public btBulletWorldImporter
58  {
59  public:
60 
61  CENSImporter(btDynamicsWorld* world=0)
62  : btBulletWorldImporter(world)
63  {
64  };
65 
66  virtual ~CENSImporter()
67  {
68  };
69 
73  btAlignedObjectArray< char * > &getAllocatedNames()
74  {
75  return m_allocatedNames;
76  }
77 
78 
82  btHashMap<btHashString,btRigidBody*> &getBodyMap()
83  {
84  return m_nameBodyMap;
85  }
86 
90  btHashMap<btHashString,btTypedConstraint*> &getConstraintMap()
91  {
92  return m_nameConstraintMap;
93  }
94  };
95 
98 
104  {
108  std::string motor_type;
110  std::string enabled;
114  double kp;
116  double ki;
118  double kd;
120  double dyn_kd;
122  double motor_dt;
126  double lower_limit;
128  double upper_limit;
130  std::string enable_collision;
131 
133  hinge(0),
134  motor_type(CENS_DEFAULT_CONTROL),
135  enabled("TRUE"),
136  max_motor_impulse(50),
137  kp(.99),
138  ki(.0),
139  kd(.01),
140  dyn_kd(.01),
141  motor_dt(0.01),
142  current_angle(0),
143  lower_limit(-M_PI),
144  upper_limit(M_PI),
145  enable_collision("FALSE")
146  {
147  }
148 
150  };
151 
154 
160  {
161  typedef std::map<std::string, CENSHingeData> Hinges;
162  typedef std::map<std::string, btTypedConstraint *> GenericConstraints;
163  typedef std::map<std::string, btRigidBody *> Bodies;
164  typedef std::map<std::string, btTransform> Transforms;
165 
168  std::string robotName;
169 
171  Hinges hinges;
172 
174  GenericConstraints constraints;
175 
177  Bodies bodies;
178 
180  Transforms transforms;
181 
184 
185 
190  CENSSerializedRobot(const std::string robotName, CENSEngine *en):
191  CENSParameterManager(robotName),engine(en)
192  {
193 
194  }
195 
197  {
198  }
199 
204  void addHinge( std::string hingeName, CENSHingeConstraint *_hinge );
205 
210  void addGenericConstraint( std::string constraintName,
211  btTypedConstraint *_constraint );
212 
217  void addBody( std::string bodyName, btRigidBody *_body );
218 
223  void addTransform( std::string transformName, btTransform _transform );
224 
226  bool init();
227 
228  // disable all bodies and hinges in the robot
229  void disableBodies();
230 
231  // enable all bodies and hinges in the robot
232  void enableBodies();
233 
235  void update();
236 
248  void move(const std::string hingeName, double target_angle,
249  double impulse = 0);
250  };
251 
254 
256  typedef std::map<std::string, btRigidBody *> Bodies;
258  typedef std::map<std::string, btTypedConstraint *> Constraints;
260  typedef std::map<const std::string, CENSSerializedRobot *> Robots;
262  typedef std::map<const std::string, CENSTouchSensor *> Sensors;
264  typedef std::map<const std::string, int > Cameras;
265 
267  {
268  public:
269 
270 
271  virtual void step(int timestep);
272 
278  const std::string &robotName,
279  const std::string &file);
280 
287  virtual CENSTouchSensor *addTouchSensor(btRigidBody *body,
288  std::string name)
289  {
291  m_eSensors[name] = sensor;
292 
293  return sensor;
294  }
295 
296  virtual int addCamera(
297  std::string cameraName,
298  btRigidBody *body,
299  btTransform local_transform,
300  std::string screenTitle,
301  int screenWidth,
302  int screenHeight,
303  int screenXGap,
304  int screenYGap,
305  btVector3 up,
306  float target,
307  float foV=70.0,
308  float near=1.0,
309  float far=10000 )
310  {
311 
312  int index = attachCamera( body, local_transform, screenTitle,
313  screenWidth, screenHeight, screenXGap,
314  screenYGap, up, target, foV, near,far );
315 
316  m_eCameras[cameraName] = index;
317 
318  return index;
319  }
320 
321  protected:
322 
324  Robots m_eRobots;
325 
330  Sensors m_eSensors;
331 
336  Cameras m_eCameras;
337 
342  Bodies m_eBodies;
343 
348  Constraints m_eConstraints;
349  };
350 
351 }
352 
353 #endif // CENS_SERIALIZED_ENGINE_H
354 
A custom HingeConstraint Class.
Definition: cens_physics.h:374
Initializing and manipulating physics objects.
Definition: cens_engine.h:104
std::map< const std::string, CENSSerializedRobot * > Robots
Computational Embodied Neuroscience Simulator library.
Definition: cens_engine.cpp:29
std::map< std::string, btRigidBody * > Bodies
btHashMap< btHashString, btRigidBody * > & getBodyMap()
const std::string CENS_DEFAULT_CONTROL
Definition: cens_types.cpp:32
void addBody(std::string bodyName, btRigidBody *_body)
std::map< const std::string, int > Cameras
CENSSerializedRobot(const std::string robotName, CENSEngine *en)
virtual void step(int timestep)
const Key & getKeyAtIndex(int index)
std::map< std::string, btTransform > Transforms
CENSHingeConstraint * hinge
virtual CENSTouchSensor * addTouchSensor(btRigidBody *body, std::string name)
void addHinge(std::string hingeName, CENSHingeConstraint *_hinge)
void addTransform(std::string transformName, btTransform _transform)
virtual CENSTouchSensor * addTouchSensor(btRigidBody *body)
Manager of parameters' initializarion.
virtual CENSSerializedRobot * loadBulletFile(const std::string &robotName, const std::string &file)
btHashMap< btHashString, btTypedConstraint * > & getConstraintMap()
std::map< std::string, btTypedConstraint * > GenericConstraints
std::map< const std::string, CENSTouchSensor * > Sensors
std::map< std::string, btRigidBody * > Bodies
void addGenericConstraint(std::string constraintName, btTypedConstraint *_constraint)
void move(const std::string hingeName, double target_angle, double impulse=0)
CENSImporter(btDynamicsWorld *world=0)
virtual int addCamera(std::string cameraName, btRigidBody *body, btTransform local_transform, std::string screenTitle, int screenWidth, int screenHeight, int screenXGap, int screenYGap, btVector3 up, float target, float foV=70.0, float near=1.0, float far=10000)
std::map< std::string, CENSHingeData > Hinges
btAlignedObjectArray< char * > & getAllocatedNames()
std::map< std::string, btTypedConstraint * > Constraints
virtual int attachCamera(btRigidBody *body, btTransform local_transform, std::string screenTitle, int screenWidth, int screenHeight, int screenXGap, int screenYGap, btVector3 up, float target, float foV=70.0, float near=1.0, float far=10000)