ARGoS 3
A parallel, multi-engine simulator for swarm robotics
box_entity.cpp
Go to the documentation of this file.
1
7#include "box_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_box_model.h>
13
14namespace 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 CBoxEntity::CBoxEntity(const std::string& str_id,
30 const CVector3& c_position,
31 const CQuaternion& c_orientation,
32 bool b_movable,
33 const CVector3& c_size,
34 Real f_mass) :
35 CComposableEntity(nullptr, str_id),
36 m_pcEmbodiedEntity(
37 new CEmbodiedEntity(this,
38 "body_0",
39 c_position,
40 c_orientation,
41 b_movable)),
42 m_pcLEDEquippedEntity(
43 new CLEDEquippedEntity(this,
44 "leds_0")),
45 m_cSize(c_size),
46 m_fMass(f_mass) {
47 AddComponent(*m_pcEmbodiedEntity);
48 AddComponent(*m_pcLEDEquippedEntity);
49 }
50
51 /****************************************/
52 /****************************************/
53
55 try {
56 /* Init parent */
58 /* Parse XML to get the size */
59 GetNodeAttribute(t_tree, "size", m_cSize);
60 /* Parse XML to get the movable attribute */
61 bool bMovable;
62 GetNodeAttribute(t_tree, "movable", bMovable);
63 if(bMovable) {
64 /* Parse XML to get the mass */
65 GetNodeAttribute(t_tree, "mass", m_fMass);
66 }
67 else {
68 m_fMass = 0.0f;
69 }
70 /* Create embodied entity using parsed data */
71 m_pcEmbodiedEntity = new CEmbodiedEntity(this);
72 AddComponent(*m_pcEmbodiedEntity);
73 m_pcEmbodiedEntity->Init(GetNode(t_tree, "body"));
74 m_pcEmbodiedEntity->SetMovable(bMovable);
75 /* Init LED equipped entity component */
76 m_pcLEDEquippedEntity = new CLEDEquippedEntity(this);
77 AddComponent(*m_pcLEDEquippedEntity);
78 if(NodeExists(t_tree, "leds")) {
79 /* Create LED equipped entity
80 * NOTE: the LEDs are not added to the medium yet
81 */
82 m_pcLEDEquippedEntity->Init(GetNode(t_tree, "leds"));
83 /* Add the LEDs to the medium */
84 std::string strMedium;
85 GetNodeAttribute(GetNode(t_tree, "leds"), "medium", strMedium);
86 m_pcLEDMedium = &CSimulator::GetInstance().GetMedium<CLEDMedium>(strMedium);
87 m_pcLEDEquippedEntity->SetMedium(*m_pcLEDMedium);
88 m_pcLEDEquippedEntity->Enable();
89 }
91 }
92 catch(CARGoSException& ex) {
93 THROW_ARGOSEXCEPTION_NESTED("Failed to initialize box entity \"" << GetId() << "\".", ex);
94 }
95 }
96
97 /****************************************/
98 /****************************************/
99
101 /* Reset all components */
103 /* Update components */
105 }
106
107 /****************************************/
108 /****************************************/
109
111 m_pcLEDMedium = &c_medium;
112 m_pcLEDEquippedEntity->SetMedium(*m_pcLEDMedium);
113 m_pcLEDEquippedEntity->Enable();
114 }
115
116 /****************************************/
117 /****************************************/
118
120 m_pcLEDEquippedEntity->Disable();
121 }
122
123 /****************************************/
124 /****************************************/
125
126 void CBoxEntity::AddLED(const CVector3& c_offset,
127 const CColor& c_color) {
128 m_pcLEDEquippedEntity->AddLED(c_offset,
129 GetEmbodiedEntity().GetOriginAnchor(),
130 c_color);
132 }
133
134 /****************************************/
135 /****************************************/
136
137 void CBoxEntity::Resize(const CVector3& c_size) {
138 /* Store size */
139 m_cSize = c_size;
140 /* Go through the physics box models and call resize on them */
141 for(size_t i = 0; i < m_pcEmbodiedEntity->GetPhysicsModelsNum(); ++i) {
142 dynamic_cast<CPhysicsBoxModel&>(m_pcEmbodiedEntity->GetPhysicsModel(i)).Resize(c_size);
143 }
144 /* Update bounding box */
145 m_pcEmbodiedEntity->CalculateBoundingBox();
146 }
147
148 /****************************************/
149 /****************************************/
150
152 "box",
153 "Carlo Pinciroli [ilpincy@gmail.com]",
154 "1.0",
155 "A stretchable 3D box.",
156 "The box entity can be used to model walls, obstacles or box-shaped grippable\n"
157 "objects. It can be movable or not. A movable object can be pushed and gripped.\n"
158 "An unmovable object is pretty much like a wall.\n\n"
159 "REQUIRED XML CONFIGURATION\n\n"
160 "To declare an unmovable object (i.e., a wall) you need the following:\n\n"
161 " <arena ...>\n"
162 " ...\n"
163 " <box id=\"box1\" size=\"0.75,0.1,0.5\" movable=\"false\">\n"
164 " <body position=\"0.4,2.3,0\" orientation=\"45,0,0\" />\n"
165 " </box>\n"
166 " ...\n"
167 " </arena>\n\n"
168 "To declare a movable object you need the following:\n\n"
169 " <arena ...>\n"
170 " ...\n"
171 " <box id=\"box1\" size=\"0.75,0.1,0.5\" movable=\"true\" mass=\"2.5\">\n"
172 " <body position=\"0.4,2.3,0\" orientation=\"45,0,0\" />\n"
173 " </box>\n"
174 " ...\n"
175 " </arena>\n\n"
176 "The 'id' attribute is necessary and must be unique among the entities. If two\n"
177 "entities share the same id, initialization aborts.\n"
178 "The 'size' attribute specifies the size of the box along the three axes, in\n"
179 "the X,Y,Z order. When you add a box, imagine it initially unrotated and\n"
180 "centered in the origin. The size, then, corresponds to the extent along the X,\n"
181 "Y and Z axes.\n"
182 "The 'movable' attribute specifies whether or not the object is movable. When\n"
183 "set to 'false', the object is unmovable: if another object pushes against it,\n"
184 "the box won't move. When the attribute is set to 'true', the box is movable\n"
185 "upon pushing or gripping. When an object is movable, the 'mass' attribute is\n"
186 "required.\n"
187 "The 'mass' attribute quantifies the mass of the box in kg.\n"
188 "The 'body/position' attribute specifies the position of the base of the box in\n"
189 "the arena. The three values are in the X,Y,Z order.\n"
190 "The 'body/orientation' attribute specifies the orientation of the 3D box. All\n"
191 "rotations are performed with respect to the center of mass. The order of the\n"
192 "angles is Z,Y,X, which means that the first number corresponds to the rotation\n"
193 "around the Z axis, the second around Y and the last around X. This reflects\n"
194 "the internal convention used in ARGoS, in which rotations are performed in\n"
195 "that order. Angles are expressed in degrees.\n\n"
196 "OPTIONAL XML CONFIGURATION\n\n"
197 "It is possible to add any number of colored LEDs to the box. In this way,\n"
198 "the box is visible with a robot camera. The position and color of the\n"
199 "LEDs is specified with the following syntax:\n\n"
200 " <arena ...>\n"
201 " ...\n"
202 " <box id=\"box1\" size=\"0.75,0.1,0.5\" movable=\"true\" mass=\"2.5\">\n"
203 " <body position=\"0.4,2.3,0\" orientation=\"45,0,0\" />\n"
204 " <leds medium=\"id_of_led_medium\">\n"
205 " <led offset=\" 0.15, 0.15,0.15\" anchor=\"origin\" color=\"white\" />\n"
206 " <led offset=\"-0.15, 0.15,0\" anchor=\"origin\" color=\"red\" />\n"
207 " <led offset=\" 0.15, 0.15,0\" anchor=\"origin\" color=\"blue\" />\n"
208 " <led offset=\" 0.15,-0.15,0\" anchor=\"origin\" color=\"green\" />\n"
209 " </leds>\n"
210 " </box>\n"
211 " ...\n"
212 " </arena>\n\n"
213 "In the example, four LEDs are added to the box. The LEDs have\n"
214 "different colors and are located one on the top and three\n"
215 "around the box. The LEDs are managed by the LED medium declared in\n"
216 "the <media> section of the configuration file with id \"id_of_led_medium\"",
217 "Usable"
218 );
219
220 /****************************************/
221 /****************************************/
222
224
225 /****************************************/
226 /****************************************/
227
228}
float Real
Collects all ARGoS code.
Definition datatypes.h:39
#define THROW_ARGOSEXCEPTION_NESTED(message, nested)
This macro throws an ARGoS exception with the passed message and nesting the passed exception.
#define REGISTER_ENTITY(CLASSNAME, LABEL, AUTHOR, VERSION, BRIEF_DESCRIPTION, LONG_DESCRIPTION, STATUS)
Definition entity.h:432
The namespace containing all the ARGoS related code.
Definition ci_actuator.h:12
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.
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.
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.
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.
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.
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
static CSimulator & GetInstance()
Returns the instance to the CSimulator class.
Definition simulator.cpp:78
T & GetMedium(const std::string &str_id)
Returns a reference to a medium.
Definition simulator.h:129
The exception that wraps all errors in ARGoS.
The basic color type.
Definition color.h:25
A 3D vector class.
Definition vector3.h:31
virtual void Reset()
Resets the state of the entity to whatever it was after Init() or the standalone constructor was call...
void EnableLEDs(CLEDMedium &c_medium)
virtual void Init(TConfigurationNode &t_tree)
Initializes the state of the entity from the XML configuration tree.
void Resize(const CVector3 &c_size)
void AddLED(const CVector3 &c_offset, const CColor &c_color=CColor::BLACK)
Adds an LED to this entity.
CEmbodiedEntity & GetEmbodiedEntity()
Definition box_entity.h:64
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 Resize(const CVector3 &c_size)=0