directional_led_equipped_entity.cpp
Go to the documentation of this file.
1 
8 #include <argos3/core/simulator/simulator.h>
9 #include <argos3/core/simulator/space/space.h>
10 #include <argos3/plugins/simulator/media/directional_led_medium.h>
11 
12 namespace argos {
13 
14  /****************************************/
15  /****************************************/
16 
18  SAnchor& s_anchor,
19  const CVector3& c_position_offset,
20  const CQuaternion& c_orientation_offset) :
21  LED(c_led),
22  Anchor(s_anchor),
23  PositionOffset(c_position_offset),
24  OrientationOffset(c_orientation_offset) {}
25 
26  /****************************************/
27  /****************************************/
28 
30  CComposableEntity(pc_parent) {
31  Disable();
32  }
33 
34  /****************************************/
35  /****************************************/
36 
38  const std::string& str_id) :
39  CComposableEntity(pc_parent, str_id) {
40  Disable();
41  }
42 
43  /****************************************/
44  /****************************************/
45 
47  try {
48  /* Init parent */
50  /* Go through the led entries */
51  TConfigurationNodeIterator itLED("directional_led");
52  for(itLED = itLED.begin(&t_tree);
53  itLED != itLED.end();
54  ++itLED) {
55  /* Initialise the LED using the XML */
56  auto* pcLED = new CDirectionalLEDEntity(this);
57  pcLED->Init(*itLED);
58  CVector3 cPositionOffset;
59  GetNodeAttribute(*itLED, "position", cPositionOffset);
60  CQuaternion cOrientationOffset;
61  GetNodeAttribute(*itLED, "orientation", cOrientationOffset);
62  /* Parse and look up the anchor */
63  std::string strAnchorId;
64  GetNodeAttribute(*itLED, "anchor", strAnchorId);
65  /*
66  * NOTE: here we get a reference to the embodied entity
67  * This line works under the assumption that:
68  * 1. the DirectionalLEDEquippedEntity has a parent;
69  * 2. the parent has a child whose id is "body"
70  * 3. the "body" is an embodied entity
71  * If any of the above is false, this line will bomb out.
72  */
73  auto& cBody =
75  /* Add the LED to this container */
76  m_vecInstances.emplace_back(*pcLED,
77  cBody.GetAnchor(strAnchorId),
78  cPositionOffset,
79  cOrientationOffset);
80  AddComponent(*pcLED);
81  }
83  }
84  catch(CARGoSException& ex) {
85  THROW_ARGOSEXCEPTION_NESTED("Failed to initialize directional LED equipped entity \"" <<
86  GetContext() + GetId() << "\".", ex);
87  }
88  }
89 
90  /****************************************/
91  /****************************************/
92 
93  void CDirectionalLEDEquippedEntity::AddLED(const std::string& str_id,
94  const CVector3& c_position,
95  const CQuaternion& c_orientation,
96  SAnchor& s_anchor,
97  const CRadians& c_observable_angle,
98  const CColor& c_color) {
99  /* create the new directional LED entity */
100  auto* pcLED =
101  new CDirectionalLEDEntity(this,
102  str_id,
103  c_position,
104  c_orientation,
105  c_observable_angle,
106  c_color);
107  /* add it to the instances vector */
108  m_vecInstances.emplace_back(*pcLED,
109  s_anchor,
110  c_position,
111  c_orientation);
112  /* inform the base class about the new entity */
113  AddComponent(*pcLED);
115  }
116 
117  /****************************************/
118  /****************************************/
119 
121  /* Perform generic enable behavior */
123  /* Enable anchors */
124  for(SInstance& s_instance : m_vecInstances) {
125  s_instance.Anchor.Enable();
126  }
127  }
128 
129  /****************************************/
130  /****************************************/
131 
133  /* Perform generic disable behavior */
135  /* Disable anchors */
136  for(SInstance& s_instance : m_vecInstances) {
137  s_instance.Anchor.Disable();
138  }
139  }
140 
141  /****************************************/
142  /****************************************/
143 
145  ARGOS_ASSERT(un_index < m_vecInstances.size(),
146  "CLEDEquippedEntity::GetLED(), id=\"" <<
147  GetContext() + GetId() <<
148  "\": index out of bounds: un_index = " <<
149  un_index <<
150  ", m_vecInstances.size() = " <<
151  m_vecInstances.size());
152  return m_vecInstances[un_index].LED;
153  }
154 
155  /****************************************/
156  /****************************************/
157 
159  ARGOS_ASSERT(un_index < m_vecInstances.size(),
160  "CLEDEquippedEntity::GetLED(), id=\"" <<
161  GetContext() + GetId() <<
162  "\": index out of bounds: un_index = " <<
163  un_index <<
164  ", m_vecInstances.size() = " <<
165  m_vecInstances.size());
166  return m_vecInstances[un_index].LED;
167  }
168 
169  /****************************************/
170  /****************************************/
171 
173  const CColor& c_color) {
174  ARGOS_ASSERT(un_index < m_vecInstances.size(),
175  "CLEDEquippedEntity::SetLEDColor(), id=\"" <<
176  GetContext() + GetId() <<
177  "\": index out of bounds: un_index = " <<
178  un_index <<
179  ", m_vecInstances.size() = " <<
180  m_vecInstances.size());
181  m_vecInstances[un_index].LED.SetColor(c_color);
182  }
183 
184  /****************************************/
185  /****************************************/
186 
188  for(SInstance& s_instance : m_vecInstances) {
189  s_instance.LED.SetColor(c_color);
190  }
191  }
192 
193  /****************************************/
194  /****************************************/
195 
196  void CDirectionalLEDEquippedEntity::SetLEDColors(const std::vector<CColor>& vec_colors) {
197  if(vec_colors.size() == m_vecInstances.size()) {
198  for(UInt32 i = 0; i < vec_colors.size(); ++i) {
199  m_vecInstances[i].LED.SetColor(vec_colors[i]);
200  }
201  }
202  else {
204  "CDirectionalLEDEquippedEntity::SetLEDColors(), id=\"" <<
205  GetContext() + GetId() <<
206  "\": number of LEDs (" <<
207  m_vecInstances.size() <<
208  ") does not equal the passed color vector size (" <<
209  vec_colors.size() <<
210  ")");
211  }
212  }
213 
214  /****************************************/
215  /****************************************/
216 
218  /* LED position wrt global reference frame */
219  CVector3 cLEDPosition;
220  CQuaternion cLEDOrientation;
221  for(SInstance& s_instance : m_vecInstances) {
222  if(s_instance.LED.IsEnabled()) {
223  cLEDPosition = s_instance.PositionOffset;
224  cLEDPosition.Rotate(s_instance.Anchor.Orientation);
225  cLEDPosition += s_instance.Anchor.Position;
226  cLEDOrientation = s_instance.Anchor.Orientation *
227  s_instance.OrientationOffset;
228  s_instance.LED.MoveTo(cLEDPosition, cLEDOrientation);
229  }
230  }
231  }
232 
233  /****************************************/
234  /****************************************/
235 
237  for(SInstance& s_instance : m_vecInstances) {
238  s_instance.LED.SetMedium(c_medium);
239  }
240  }
241 
242  /****************************************/
243  /****************************************/
244 
246 
247  /****************************************/
248  /****************************************/
249 
250 }
#define ARGOS_ASSERT(condition, message)
When code is compiled in debug, this macro throws an ARGoS exception with the passed message if the s...
#define THROW_ARGOSEXCEPTION_NESTED(message, nested)
This macro throws an ARGoS exception with the passed message and nesting the passed exception.
#define THROW_ARGOSEXCEPTION(message)
This macro throws an ARGoS exception with the passed message.
unsigned int UInt32
32-bit unsigned integer.
Definition: datatypes.h:97
The namespace containing all the ARGoS related code.
Definition: ci_actuator.h:12
ticpp::Iterator< ticpp::Element > TConfigurationNodeIterator
The iterator for the ARGoS configuration XML node.
ticpp::Element TConfigurationNode
The ARGoS configuration XML node.
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.
CEntity & GetComponent(const std::string &str_component)
Returns the component with the passed string label.
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 Disable()
Disables the entity.
Definition: entity.h:275
const std::string & GetId() const
Returns the id of this entity.
Definition: entity.h:157
std::string GetContext() const
Returns the context of this entity.
Definition: entity.cpp:79
void Enable()
Enables the entity.
Definition: entity.h:265
CComposableEntity & GetParent()
Returns this entity's parent.
Definition: entity.cpp:91
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.
The basic color type.
Definition: color.h:25
It defines the basic type CRadians, used to store an angle value in radians.
Definition: angles.h:42
A 3D vector class.
Definition: vector3.h:31
CVector3 & Rotate(const CQuaternion &c_quaternion)
Rotates this vector by the given quaternion.
Definition: vector3.cpp:23
A container of CDirectionalLEDEntity.
virtual void UpdateComponents()
Calls the Update() method on all the components.
void AddLED(const std::string &str_id, const CVector3 &c_position, const CQuaternion &c_orientation, SAnchor &s_anchor, const CRadians &c_observable_angle, const CColor &c_color)
Programmatically creates a new directional LED.
void SetLEDColor(UInt32 un_index, const CColor &c_color)
Sets the color of an LED.
void SetLEDColors(const CColor &c_color)
Sets the color of all the LEDs to the same value.
CDirectionalLEDEquippedEntity(CComposableEntity *pc_parent)
Class constructor.
SInstance::TVector m_vecInstances
List of the LEDs managed by this entity.
void SetMedium(CDirectionalLEDMedium &c_medium)
Sets the medium associated to this entity.
virtual void Init(TConfigurationNode &t_tree)
Initializes the state of the entity from the XML configuration tree.
CDirectionalLEDEntity & GetLED(UInt32 un_index)
Returns an LED by numeric index.
SInstance(CDirectionalLEDEntity &c_led, SAnchor &s_anchor, const CVector3 &c_position_offset, const CQuaternion &c_orientation_offset)