spiri_entity.cpp
Go to the documentation of this file.
1 
7 #include "spiri_entity.h"
8 
9 #include <argos3/core/simulator/space/space.h>
10 #include <argos3/core/simulator/entity/controllable_entity.h>
11 #include <argos3/core/simulator/entity/embodied_entity.h>
12 #include <argos3/plugins/simulator/entities/led_equipped_entity.h>
13 #include <argos3/plugins/simulator/entities/light_sensor_equipped_entity.h>
14 #include <argos3/plugins/simulator/entities/perspective_camera_equipped_entity.h>
15 #include <argos3/plugins/simulator/entities/proximity_sensor_equipped_entity.h>
16 #include <argos3/plugins/simulator/entities/quadrotor_entity.h>
17 #include <argos3/plugins/simulator/entities/rab_equipped_entity.h>
18 #include <argos3/plugins/simulator/entities/battery_equipped_entity.h>
19 
20 namespace argos {
21 
22  /****************************************/
23  /****************************************/
24 
25  static const Real HEIGHT = 0.090f;
26  static const Real BODY_HEIGHT = HEIGHT * 3.0f / 4.0f;
27  static const Real BODY_SIDE = 0.470f;
28  static const Real BODY_DIAGONAL = BODY_SIDE * ::sqrt(2);
29  static const Real BODY_RADIUS = BODY_DIAGONAL / 2.0f;
30  static const Real BODY_ELEVATION = HEIGHT - BODY_HEIGHT;
31  static const Real RAB_ELEVATION = (BODY_ELEVATION + BODY_HEIGHT) / 2.0f;
32 
33  /****************************************/
34  /****************************************/
35 
37  CComposableEntity(nullptr),
38  m_pcControllableEntity(nullptr),
39  m_pcEmbodiedEntity(nullptr),
40  m_pcQuadRotorEntity(nullptr),
41  m_pcRABEquippedEntity(nullptr),
42  m_pcPerspectiveCameraEquippedEntity(nullptr),
43  m_pcBatteryEquippedEntity(nullptr) {
44  }
45 
46  /****************************************/
47  /****************************************/
48 
49  CSpiriEntity::CSpiriEntity(const std::string& str_id,
50  const std::string& str_controller_id,
51  const CVector3& c_position,
52  const CQuaternion& c_orientation,
53  Real f_rab_range,
54  size_t un_rab_data_size,
55  const std::string& str_bat_model,
56  const CRadians& c_cam_aperture,
57  Real f_cam_range) :
58  CComposableEntity(nullptr, str_id),
59  m_pcControllableEntity(nullptr),
60  m_pcEmbodiedEntity(nullptr),
61  m_pcQuadRotorEntity(nullptr),
62  m_pcRABEquippedEntity(nullptr),
63  m_pcPerspectiveCameraEquippedEntity(nullptr),
64  m_pcBatteryEquippedEntity(nullptr) {
65  try {
66  /*
67  * Create and init components
68  */
69  /*
70  * Embodied entity
71  * Better to put this first, because many other entities need this one
72  */
73  m_pcEmbodiedEntity = new CEmbodiedEntity(this, "body_0", c_position, c_orientation);
74  AddComponent(*m_pcEmbodiedEntity);
75  /* Quadrotor entity */
76  m_pcQuadRotorEntity = new CQuadRotorEntity(this, "quadrotor_0");
77  AddComponent(*m_pcQuadRotorEntity);
78  /* RAB equipped entity */
79  SAnchor& cRABAnchor = m_pcEmbodiedEntity->AddAnchor(
80  "rab",
81  CVector3(0.0f, 0.0f, RAB_ELEVATION));
82  m_pcRABEquippedEntity = new CRABEquippedEntity(
83  this,
84  "rab_0",
85  un_rab_data_size,
86  f_rab_range,
87  cRABAnchor,
88  *m_pcEmbodiedEntity);
89  AddComponent(*m_pcRABEquippedEntity);
90  m_pcEmbodiedEntity->EnableAnchor("rab");
91  /* Perspective camera */
92  SAnchor& cCameraAnchor =
93  m_pcEmbodiedEntity->AddAnchor(
94  "camera",
95  CVector3(BODY_RADIUS, 0.0f, 0.0f));
96  m_pcPerspectiveCameraEquippedEntity = new CPerspectiveCameraEquippedEntity(
97  this,
98  "perspective_camera_0",
99  c_cam_aperture,
100  0.035f,
101  f_cam_range,
102  640, 480,
103  cCameraAnchor);
104  AddComponent(*m_pcPerspectiveCameraEquippedEntity);
105  m_pcEmbodiedEntity->EnableAnchor("camera");
106  /* Battery equipped entity */
107  m_pcBatteryEquippedEntity = new CBatteryEquippedEntity(this, "battery_0", str_bat_model);
108  AddComponent(*m_pcBatteryEquippedEntity);
109  /* Controllable entity
110  It must be the last one, for actuators/sensors to link to composing entities correctly */
111  m_pcControllableEntity = new CControllableEntity(this, "controller_0");
112  AddComponent(*m_pcControllableEntity);
113  m_pcControllableEntity->SetController(str_controller_id);
114  /* Update components */
116  }
117  catch(CARGoSException& ex) {
118  THROW_ARGOSEXCEPTION_NESTED("Failed to initialize entity \"" << GetId() << "\".", ex);
119  }
120  }
121 
122  /****************************************/
123  /****************************************/
124 
126  try {
127  /*
128  * Init parent
129  */
130  CComposableEntity::Init(t_tree);
131  /*
132  * Create and init components
133  */
134  /*
135  * Embodied entity
136  * Better to put this first, because many other entities need this one
137  */
138  m_pcEmbodiedEntity = new CEmbodiedEntity(this);
139  AddComponent(*m_pcEmbodiedEntity);
140  m_pcEmbodiedEntity->Init(GetNode(t_tree, "body"));
141  /* Quadrotor entity */
142  m_pcQuadRotorEntity = new CQuadRotorEntity(this, "quadrotor_0");
143  AddComponent(*m_pcQuadRotorEntity);
144  /* RAB equipped entity */
145  Real fRABRange = 3.0f;
146  GetNodeAttributeOrDefault(t_tree, "rab_range", fRABRange, fRABRange);
147  UInt32 unRABDataSize = 10;
148  GetNodeAttributeOrDefault(t_tree, "rab_data_size", unRABDataSize, unRABDataSize);
149  SAnchor& cRABAnchor = m_pcEmbodiedEntity->AddAnchor("rab", CVector3(0.0f, 0.0f, RAB_ELEVATION));
150  m_pcRABEquippedEntity = new CRABEquippedEntity(
151  this,
152  "rab_0",
153  unRABDataSize,
154  fRABRange,
155  cRABAnchor,
156  *m_pcEmbodiedEntity);
157  AddComponent(*m_pcRABEquippedEntity);
158  m_pcEmbodiedEntity->EnableAnchor("rab");
159  /* Perspective camera */
160  CDegrees cCameraAperture(30.0f);
161  GetNodeAttributeOrDefault(t_tree, "camera_aperture", cCameraAperture, cCameraAperture);
162  Real fCameraRange = 10.0f;
163  GetNodeAttributeOrDefault(t_tree, "camera_range", fCameraRange, fCameraRange);
164  SAnchor& cCameraAnchor =
165  m_pcEmbodiedEntity->AddAnchor(
166  "camera",
167  CVector3(BODY_RADIUS, 0.0f, 0.0f));
168  m_pcPerspectiveCameraEquippedEntity = new CPerspectiveCameraEquippedEntity(
169  this,
170  "perspective_camera_0",
171  ToRadians(cCameraAperture),
172  0.035f,
173  fCameraRange,
174  640, 480,
175  cCameraAnchor);
176  AddComponent(*m_pcPerspectiveCameraEquippedEntity);
177  m_pcEmbodiedEntity->EnableAnchor("camera");
178  /* Battery equipped entity */
179  m_pcBatteryEquippedEntity = new CBatteryEquippedEntity(this, "battery_0");
180  if(NodeExists(t_tree, "battery"))
181  m_pcBatteryEquippedEntity->Init(GetNode(t_tree, "battery"));
182  AddComponent(*m_pcBatteryEquippedEntity);
183  /* Controllable entity
184  It must be the last one, for actuators/sensors to link to composing entities correctly */
185  m_pcControllableEntity = new CControllableEntity(this);
186  AddComponent(*m_pcControllableEntity);
187  m_pcControllableEntity->Init(GetNode(t_tree, "controller"));
188  /* Update components */
190  }
191  catch(CARGoSException& ex) {
192  THROW_ARGOSEXCEPTION_NESTED("Failed to initialize entity \"" << GetId() << "\".", ex);
193  }
194  }
195 
196  /****************************************/
197  /****************************************/
198 
200  /* Reset all components */
202  /* Update components */
204  }
205 
206  /****************************************/
207  /****************************************/
208 
210  "spiri",
211  "Carlo Pinciroli [ilpincy@gmail.com]",
212  "1.0",
213  "The spiri robot, developed by Pleiades Robotics.",
214  "The spiri is a quad-rotor developed by Pleiades Robotics. It is a\n"
215  "fully autonomous robot with a rich set of sensors and actuators. For more\n"
216  "information, refer to the dedicated web page (http://www.pleaides.ca/).\n\n"
217  "REQUIRED XML CONFIGURATION\n\n"
218  " <arena ...>\n"
219  " ...\n"
220  " <spiri id=\"sp0\">\n"
221  " <body position=\"0.4,2.3,0.25\" orientation=\"45,0,0\" />\n"
222  " <controller config=\"mycntrl\" />\n"
223  " </spiri>\n"
224  " ...\n"
225  " </arena>\n\n"
226  "The 'id' attribute is necessary and must be unique among the entities. If two\n"
227  "entities share the same id, initialization aborts.\n"
228  "The 'body/position' attribute specifies the position of the bottom point of the\n"
229  "spiri in the arena. When the robot is untranslated and unrotated, the\n"
230  "bottom point is in the origin and it is defined as the middle point between\n"
231  "the two wheels on the XY plane and the lowest point of the robot on the Z\n"
232  "axis, that is the point where the wheels touch the floor. The attribute values\n"
233  "are in the X,Y,Z order.\n"
234  "The 'body/orientation' attribute specifies the orientation of the spiri. All\n"
235  "rotations are performed with respect to the bottom point. The order of the\n"
236  "angles is Z,Y,X, which means that the first number corresponds to the rotation\n"
237  "around the Z axis, the second around Y and the last around X. This reflects\n"
238  "the internal convention used in ARGoS, in which rotations are performed in\n"
239  "that order. Angles are expressed in degrees. When the robot is unrotated, it\n"
240  "is oriented along the X axis.\n"
241  "The 'controller/config' attribute is used to assign a controller to the\n"
242  "spiri. The value of the attribute must be set to the id of a previously\n"
243  "defined controller. Controllers are defined in the <controllers> XML subtree.\n\n"
244  "OPTIONAL XML CONFIGURATION\n\n"
245  "You can set the emission range of the range-and-bearing system. By default, a\n"
246  "message sent by an spiri can be received up to 3m. By using the 'rab_range'\n"
247  "attribute, you can change it to, i.e., 4m as follows:\n\n"
248  " <arena ...>\n"
249  " ...\n"
250  " <spiri id=\"sp0\" rab_range=\"4\">\n"
251  " <body position=\"0.4,2.3,0.25\" orientation=\"45,0,0\" />\n"
252  " <controller config=\"mycntrl\" />\n"
253  " </spiri>\n"
254  " ...\n"
255  " </arena>\n\n"
256  "You can also set the data sent at each time step through the range-and-bearing\n"
257  "system. By default, a message sent by a spiri is 10 bytes long. By using the\n"
258  "'rab_data_size' attribute, you can change it to, i.e., 20 bytes as follows:\n\n"
259  " <arena ...>\n"
260  " ...\n"
261  " <spiri id=\"sp0\" rab_data_size=\"20\">\n"
262  " <body position=\"0.4,2.3,0.25\" orientation=\"45,0,0\" />\n"
263  " <controller config=\"mycntrl\" />\n"
264  " </spiri>\n"
265  " ...\n"
266  " </arena>\n\n"
267  "You can also configure the battery of the robot. By default, the battery never\n"
268  "depletes. You can choose among several battery discharge models, such as\n"
269  "- time: the battery depletes by a fixed amount at each time step\n"
270  "- motion: the battery depletes according to how the robot moves\n"
271  "- time_motion: a combination of the above models.\n"
272  "You can define your own models too. Follow the examples in the file\n"
273  "argos3/src/plugins/simulator/entities/battery_equipped_entity.cpp.\n\n"
274  " <arena ...>\n"
275  " ...\n"
276  " <spiri id=\"sp0\"\n"
277  " <body position=\"0.4,2.3,0.25\" orientation=\"45,0,0\" />\n"
278  " <controller config=\"mycntrl\" />\n"
279  " <battery model=\"time\" factor=\"1e-5\"/>\n"
280  " </spiri>\n"
281  " ...\n"
282  " </arena>\n\n"
283  " <arena ...>\n"
284  " ...\n"
285  " <spiri id=\"sp0\"\n"
286  " <body position=\"0.4,2.3,0.25\" orientation=\"45,0,0\" />\n"
287  " <controller config=\"mycntrl\" />\n"
288  " <battery model=\"motion\" pos_factor=\"1e-3\"\n"
289  " orient_factor=\"1e-3\"/>\n"
290  " </spiri>\n"
291  " ...\n"
292  " </arena>\n\n"
293  " <arena ...>\n"
294  " ...\n"
295  " <spiri id=\"sp0\"\n"
296  " <body position=\"0.4,2.3,0.25\" orientation=\"45,0,0\" />\n"
297  " <controller config=\"mycntrl\" />\n"
298  " <battery model=\"time_motion\" time_factor=\"1e-5\"\n"
299  " pos_factor=\"1e-3\"\n"
300  " orient_factor=\"1e-3\"/>\n"
301  " </spiri>\n"
302  " ...\n"
303  " </arena>\n\n"
304  ,
305  "Under development"
306  );
307 
308  /****************************************/
309  /****************************************/
310 
312 
313  /****************************************/
314  /****************************************/
315 
316 }
#define THROW_ARGOSEXCEPTION_NESTED(message, nested)
This macro throws an ARGoS exception with the passed message and nesting the passed exception.
unsigned int UInt32
32-bit unsigned integer.
Definition: datatypes.h:97
float Real
Collects all ARGoS code.
Definition: datatypes.h:39
The namespace containing all the ARGoS related code.
Definition: ci_actuator.h:12
TConfigurationNode & GetNode(TConfigurationNode &t_node, const std::string &str_tag)
Given a tree root node, returns the first of its child nodes with the wanted name.
void GetNodeAttributeOrDefault(TConfigurationNode &t_node, const std::string &str_attribute, T &t_buffer, const T &t_default)
Returns the value of a node's attribute, or the passed default value.
ticpp::Element TConfigurationNode
The ARGoS configuration XML node.
bool NodeExists(TConfigurationNode &t_node, const std::string &str_tag)
Given a tree root node, returns true if one of its child nodes has the wanted name.
CRadians ToRadians(const CDegrees &c_degrees)
Converts CDegrees to CRadians.
Definition: angles.h:498
REGISTER_STANDARD_SPACE_OPERATIONS_ON_COMPOSABLE(CComposableEntity)
REGISTER_ENTITY(CFloorEntity, "floor", "Carlo Pinciroli [ilpincy@gmail.com]", "1.0", "It contains the properties of the arena floor.", "The floor entity contains the properties of the arena floor. In the current\n" "implementation, it contains only the color of the floor. The floor color is\n" "detected by the robots' ground sensors.\n\n" "REQUIRED XML CONFIGURATION\n\n" " <arena ...>\n" " ...\n" " <floor id=\"floor\"\n" " source=\"SOURCE\" />\n" " ...\n" " </arena>\n\n" "The 'id' attribute is necessary and must be unique among the entities. If two\n" "entities share the same id, initialization aborts.\n" "The 'source' attribute specifies where to get the color of the floor from. Its\n" "value, here denoted as SOURCE, can assume the following values:\n\n" " image The color is calculated from the passed image file\n" " loop_functions The color is calculated calling the loop functions\n\n" "When 'source' is set to 'image', as showed in the following example, you have\n" "to specify the image path in the additional attribute 'path':\n\n" " <arena ...>\n" " ...\n" " <floor id=\"floor\"\n" " source=\"image\"\n" " path=\"/path/to/imagefile.ext\" />\n" " ...\n" " </arena>\n\n" "Many image formats are available, such as PNG, JPG, BMP, GIF and many more.\n" "Refer to the FreeImage webpage for a complete list of supported image formats\n" "(http://freeimage.sourceforge.net/features.html).\n\n" "When 'source' is set to 'loop_functions', as showed in the following example,\n" "an image is implicitly created to be used as texture for graphical\n" "visualizations. The algorithm that creates the texture needs to convert from\n" "meters (in the arena) to pixels (of the texture). You control how many pixels\n" "per meter are used with the attribute 'pixels_per_meter'. Clearly, the higher\n" "value, the higher the quality, but also the slower the algorithm and the bigger\n" "the texture. The algorithm is called only once at init time, so the fact that\n" "it is slow is not so important. However, the image size is limited by OpenGL.\n" "Every implementation has its own limit, and you should check yours if any\n" "texture-related problem arises. Now for the example:\n\n" " <arena ...>\n" " ...\n" " <floor id=\"floor\"\n" " source=\"loop_functions\"\n" " pixels_per_meter=\"100\" />\n" " ...\n" " </arena>\n\n" "OPTIONAL XML CONFIGURATION\n\n" "None for the time being.\n", "Usable")
Basic class for an entity that contains other entities.
virtual void Reset()
Resets the state of the entity to whatever it was after Init() or the standalone constructor was call...
virtual void UpdateComponents()
Calls the Update() method on all the components.
void AddComponent(CEntity &c_component)
Adds a component to this composable entity.
An entity that contains a pointer to the user-defined controller.
void SetController(const std::string &str_controller_id)
Creates and assigns a controller with the given id.
virtual void Init(TConfigurationNode &t_tree)
Initializes the state of the entity from the XML configuration tree.
This entity is a link to a body in the physics engine.
virtual void Init(TConfigurationNode &t_tree)
Initializes the state of the entity from the XML configuration tree.
void EnableAnchor(const std::string &str_id)
Enables an anchor.
SAnchor & AddAnchor(const std::string &str_id, const CVector3 &c_rel_position=CVector3(), const CQuaternion &c_rel_orientation=CQuaternion())
Adds an anchor to the embodied entity.
const std::string & GetId() const
Returns the id of this entity.
Definition: entity.h:157
virtual void Init(TConfigurationNode &t_tree)
Initializes the state of the entity from the XML configuration tree.
Definition: entity.cpp:40
An anchor related to the body of an entity.
Definition: physics_model.h:38
The exception that wraps all errors in ARGoS.
It defines the basic type CRadians, used to store an angle value in radians.
Definition: angles.h:42
It defines the basic type CDegrees, used to store an angle value in degrees.
Definition: angles.h:288
A 3D vector class.
Definition: vector3.h:31
virtual void Init(TConfigurationNode &t_tree)
Initializes the state of the entity from the XML configuration tree.
virtual void Reset()
Resets the state of the entity to whatever it was after Init() or the standalone constructor was call...
virtual void Init(TConfigurationNode &t_tree)
Initializes the state of the entity from the XML configuration tree.