cylinder_entity.cpp
Go to the documentation of this file.
1 
7 #include "cylinder_entity.h"
8 #include <argos3/core/utility/math/matrix/rotationmatrix3.h>
9 #include <argos3/core/simulator/space/space.h>
10 #include <argos3/core/simulator/simulator.h>
11 #include <argos3/plugins/simulator/media/led_medium.h>
12 #include <argos3/plugins/simulator/physics_engines/physics_cylinder_model.h>
13 
14 namespace argos {
15 
16  /****************************************/
17  /****************************************/
18 
20  CComposableEntity(nullptr),
21  m_pcEmbodiedEntity(nullptr),
22  m_pcLEDEquippedEntity(nullptr),
23  m_fMass(1.0f),
24  m_pcLEDMedium(nullptr) {
25  }
26 
27  /****************************************/
28  /****************************************/
29 
30  CCylinderEntity::CCylinderEntity(const std::string& str_id,
31  const CVector3& c_position,
32  const CQuaternion& c_orientation,
33  bool b_movable,
34  Real f_radius,
35  Real f_height,
36  Real f_mass) :
37  CComposableEntity(nullptr, str_id),
38  m_pcEmbodiedEntity(
39  new CEmbodiedEntity(this,
40  "body_0",
41  c_position,
42  c_orientation,
43  b_movable)),
44  m_pcLEDEquippedEntity(
45  new CLEDEquippedEntity(this, "leds_0")),
46  m_fRadius(f_radius),
47  m_fHeight(f_height),
48  m_fMass(f_mass) {
49  AddComponent(*m_pcEmbodiedEntity);
50  AddComponent(*m_pcLEDEquippedEntity);
51  }
52 
53  /****************************************/
54  /****************************************/
55 
57  try {
58  /* Init parent */
60  /* Parse XML to get the radius */
61  GetNodeAttribute(t_tree, "radius", m_fRadius);
62  /* Parse XML to get the height */
63  GetNodeAttribute(t_tree, "height", m_fHeight);
64  /* Parse XML to get the movable attribute */
65  bool bMovable;
66  GetNodeAttribute(t_tree, "movable", bMovable);
67  if(bMovable) {
68  /* Parse XML to get the mass */
69  GetNodeAttribute(t_tree, "mass", m_fMass);
70  }
71  else {
72  m_fMass = 0.0f;
73  }
74  /* Create embodied entity using parsed data */
75  m_pcEmbodiedEntity = new CEmbodiedEntity(this);
76  AddComponent(*m_pcEmbodiedEntity);
77  m_pcEmbodiedEntity->Init(GetNode(t_tree, "body"));
78  m_pcEmbodiedEntity->SetMovable(bMovable);
79  /* Init LED equipped entity component */
80  m_pcLEDEquippedEntity = new CLEDEquippedEntity(this);
81  AddComponent(*m_pcLEDEquippedEntity);
82  if(NodeExists(t_tree, "leds")) {
83  /* Create LED equipped entity
84  * NOTE: the LEDs are not added to the medium yet
85  */
86  m_pcLEDEquippedEntity->Init(GetNode(t_tree, "leds"));
87  /* Add the LEDs to the medium */
88  std::string strMedium;
89  GetNodeAttribute(GetNode(t_tree, "leds"), "medium", strMedium);
90  m_pcLEDMedium = &CSimulator::GetInstance().GetMedium<CLEDMedium>(strMedium);
91  m_pcLEDEquippedEntity->SetMedium(*m_pcLEDMedium);
92  m_pcLEDEquippedEntity->Enable();
93  }
95  }
96  catch(CARGoSException& ex) {
97  THROW_ARGOSEXCEPTION_NESTED("Failed to initialize the cylinder entity.", ex);
98  }
99  }
100 
101  /****************************************/
102  /****************************************/
103 
105  /* Reset all components */
106  m_pcEmbodiedEntity->Reset();
107  m_pcLEDEquippedEntity->Reset();
108  /* Update components */
110  }
111 
112  /****************************************/
113  /****************************************/
114 
116  m_pcLEDMedium = &c_medium;
117  m_pcLEDEquippedEntity->SetMedium(*m_pcLEDMedium);
118  m_pcLEDEquippedEntity->Enable();
119  }
120 
121  /****************************************/
122  /****************************************/
123 
125  m_pcLEDEquippedEntity->Disable();
126  }
127 
128  /****************************************/
129  /****************************************/
130 
131  void CCylinderEntity::AddLED(const CVector3& c_offset,
132  const CColor& c_color) {
133  m_pcLEDEquippedEntity->AddLED(c_offset,
134  GetEmbodiedEntity().GetOriginAnchor(),
135  c_color);
137  }
138 
139  /****************************************/
140  /****************************************/
141 
142  void CCylinderEntity::Resize(Real f_radius, Real f_height) {
143  /* Store size */
144  m_fRadius = f_radius;
145  m_fHeight = f_height;
146  /* Go through the physics box models and call resize on them */
147  for(size_t i = 0; i < m_pcEmbodiedEntity->GetPhysicsModelsNum(); ++i) {
148  dynamic_cast<CPhysicsCylinderModel&>(m_pcEmbodiedEntity->GetPhysicsModel(i)).Resize(f_radius, f_height);
149  }
150  /* Update bounding box */
151  m_pcEmbodiedEntity->CalculateBoundingBox();
152  }
153 
154  /****************************************/
155  /****************************************/
156 
158  "cylinder",
159  "Carlo Pinciroli [ilpincy@gmail.com]",
160  "1.0",
161  "A stretchable cylinder.",
162  "The cylinder entity can be used to model obstacles or cylinder-shaped\n"
163  "grippable objects. The cylinder has a red LED on the center of one\n"
164  "of the circular surfaces, that allows perception using the cameras.\n"
165  "The cylinder can be movable or not. A movable object can be pushed\n"
166  "and gripped. An unmovable object is pretty much like a column.\n\n"
167  "REQUIRED XML CONFIGURATION\n\n"
168  "To declare an unmovable object (i.e., a column) you need the following:\n\n"
169  " <arena ...>\n"
170  " ...\n"
171  " <cylinder id=\"cyl1\" radius=\"0.8\" height=\"0.5\" movable=\"false\">\n"
172  " <body position=\"0.4,2.3,0\" orientation=\"45,0,0\" />\n"
173  " </cylinder>\n"
174  " ...\n"
175  " </arena>\n\n"
176  "To declare a movable object you need the following:\n\n"
177  " <arena ...>\n"
178  " ...\n"
179  " <cylinder id=\"cyl1\" radius=\"0.8\" height=\"0.5\"\n"
180  " movable=\"true\" mass=\"2.5\">\n"
181  " <body position=\"0.4,2.3,0\" orientation=\"45,0,0\" />\n"
182  " </cylinder>\n"
183  " ...\n"
184  " </arena>\n\n"
185  "The 'id' attribute is necessary and must be unique among the entities. If two\n"
186  "entities share the same id, initialization aborts.\n"
187  "The 'radius' and 'height' attributes specify the size of the cylinder. When\n"
188  "you add a cylinder, imagine it initially unrotated and centered in the origin.\n"
189  "The base of the cylinder, then, is parallel to the XY plane and its height\n"
190  "goes with the Z axis.\n"
191  "The 'movable' attribute specifies whether or not the object is movable. When\n"
192  "set to 'false', the object is unmovable: if another object pushes against it,\n"
193  "the cylinder won't move. When the attribute is set to 'true', the cylinder is\n"
194  "movable upon pushing or gripping. When an object is movable, the 'mass'\n"
195  "attribute is required.\n"
196  "The 'mass' attribute quantifies the mass of the cylinder in kg.\n"
197  "The 'body/position' attribute specifies the position of the base of the\n"
198  "cylinder in the arena. The three values are in the X,Y,Z order.\n"
199  "The 'body/orientation' attribute specifies the orientation of the cylinder. All\n"
200  "rotations are performed with respect to the center of mass. The order of the\n"
201  "angles is Z,Y,X, which means that the first number corresponds to the rotation\n"
202  "around the Z axis, the second around Y and the last around X. This reflects\n"
203  "the internal convention used in ARGoS, in which rotations are performed in\n"
204  "that order. Angles are expressed in degrees.\n\n"
205  "OPTIONAL XML CONFIGURATION\n\n"
206  "It is possible to add any number of colored LEDs to the cylinder. In this way,\n"
207  "the cylinder is visible with a robot camera. The position and color of the\n"
208  "LEDs is specified with the following syntax:\n\n"
209  " <arena ...>\n"
210  " ...\n"
211  " <cylinder id=\"cyl1\" radius=\"0.8\" height=\"0.5\"\n"
212  " movable=\"true\" mass=\"2.5\">\n"
213  " <body position=\"0.4,2.3,0\" orientation=\"45,0,0\" />\n"
214  " <leds medium=\"id_of_led_medium\">\n"
215  " <led offset=\" 0.15, 0.15,0.15\" anchor=\"origin\" color=\"white\" />\n"
216  " <led offset=\"-0.15, 0.15,0\" anchor=\"origin\" color=\"red\" />\n"
217  " <led offset=\" 0.15, 0.15,0\" anchor=\"origin\" color=\"blue\" />\n"
218  " <led offset=\" 0.15,-0.15,0\" anchor=\"origin\" color=\"green\" />\n"
219  " </leds>\n"
220  " </cylinder>\n"
221  " ...\n"
222  " </arena>\n\n"
223  "In the example, four LEDs are added around the cylinder. The LEDs have\n"
224  "different colors and are located around the cylinder. The LEDs are\n"
225  "managed by the LED medium declared in the <media> section of the\n"
226  "configuration file with id \"id_of_led_medium\"",
227  "Usable"
228  );
229 
230  /****************************************/
231  /****************************************/
232 
234 
235  /****************************************/
236  /****************************************/
237 
238 }
#define THROW_ARGOSEXCEPTION_NESTED(message, nested)
This macro throws an ARGoS exception with the passed message and nesting the passed exception.
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.
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.
REGISTER_STANDARD_SPACE_OPERATIONS_ON_COMPOSABLE(CComposableEntity)
void GetNodeAttribute(TConfigurationNode &t_node, const std::string &str_attribute, T &t_buffer)
Returns the value of a node's attribute.
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 UpdateComponents()
Calls the Update() method on all the components.
void AddComponent(CEntity &c_component)
Adds a component to this composable entity.
This entity is a link to a body in the physics engine.
void CalculateBoundingBox()
Calculates the bounding box of this entity.
const CPhysicsModel & GetPhysicsModel(size_t un_idx) const
Returns a physics model associated to this entity.
UInt32 GetPhysicsModelsNum() const
Returns the number of physics models associated to this entity.
virtual void Init(TConfigurationNode &t_tree)
Initializes the state of the entity from the XML configuration tree.
void SetMovable(bool b_movable)
Sets whether this entity is movable or not.
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.
Definition: entity.cpp:40
T & GetMedium(const std::string &str_id)
Returns a reference to a medium.
Definition: simulator.h:129
static CSimulator & GetInstance()
Returns the instance to the CSimulator class.
Definition: simulator.cpp:78
The exception that wraps all errors in ARGoS.
The basic color type.
Definition: color.h:25
A 3D vector class.
Definition: vector3.h:31
void AddLED(const CVector3 &c_offset, const CColor &c_color=CColor::BLACK)
Adds an LED to this entity.
void Resize(Real f_radius, Real f_height)
void EnableLEDs(CLEDMedium &c_medium)
CEmbodiedEntity & GetEmbodiedEntity()
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...
A container of CLEDEntity.
void SetMedium(CLEDMedium &c_medium)
Sets the medium associated to this entity.
void AddLED(const CVector3 &c_offset, SAnchor &s_anchor, const CColor &c_color=CColor::BLACK)
Adds an LED to this entity.
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...