directional_led_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  CPositionalEntity(pc_parent),
19  m_cObservableAngle(CRadians::PI),
20  m_cColor(CColor::BLACK),
21  m_cInitColor(CColor::BLACK),
22  m_pcMedium(nullptr) {
23  Disable();
24  }
25 
26  /****************************************/
27  /****************************************/
28 
30  const std::string& str_id,
31  const CVector3& c_position,
32  const CQuaternion& c_orientation,
33  const CRadians& c_observable_angle,
34  const CColor& c_color) :
35  CPositionalEntity(pc_parent, str_id, c_position, c_orientation),
36  m_cObservableAngle(c_observable_angle),
37  m_cColor(c_color),
38  m_cInitColor(c_color),
39  m_pcMedium(nullptr) {
40  Disable();
41  }
42 
43  /****************************************/
44  /****************************************/
45 
47  try {
48  /* Parse XML */
52  CDegrees cObservableAngle;
53  GetNodeAttribute(t_tree, "observable_angle", cObservableAngle);
54  m_cObservableAngle = ToRadians(cObservableAngle);
55  }
56  catch(CARGoSException& ex) {
57  THROW_ARGOSEXCEPTION_NESTED("Error while initializing directional led entity", ex);
58  }
59  }
60 
61  /****************************************/
62  /****************************************/
63 
66  }
67 
68  /****************************************/
69  /****************************************/
70 
72  Disable();
73  }
74 
75  /****************************************/
76  /****************************************/
77 
78  void CDirectionalLEDEntity::SetEnabled(bool b_enabled) {
79  /* Perform generic enable behavior */
80  CEntity::SetEnabled(b_enabled);
81  if(b_enabled) {
82  /* Enable entity in medium */
83  if(m_pcMedium && GetIndex() >= 0)
84  m_pcMedium->AddEntity(*this);
85  }
86  else {
87  /* Disable entity in medium */
88  if(m_pcMedium)
89  m_pcMedium->RemoveEntity(*this);
90  }
91  }
92 
93  /****************************************/
94  /****************************************/
95 
96  void CDirectionalLEDEntity::SetColor(const CColor& c_color) {
97  m_cColor = c_color;
98  }
99 
100  /****************************************/
101  /****************************************/
102 
104  if(m_pcMedium == nullptr) {
105  THROW_ARGOSEXCEPTION("directional LED entity \"" << GetContext() <<
106  GetId() << "\" has no associated medium.");
107  }
108  return *m_pcMedium;
109  }
110 
111  /****************************************/
112  /****************************************/
113 
115  if(m_pcMedium != nullptr && m_pcMedium != &c_medium)
116  m_pcMedium->RemoveEntity(*this);
117  m_pcMedium = &c_medium;
118  }
119 
120  /****************************************/
121  /****************************************/
122 
124  CDirectionalLEDEntity& c_element) {
125  /* Calculate the position of the LED in the space hash */
126  c_space_hash.SpaceToHashTable(m_nI, m_nJ, m_nK, c_element.GetPosition());
127  /* Update the corresponding cell */
128  c_space_hash.UpdateCell(m_nI, m_nJ, m_nK, c_element);
129  }
130 
131  /****************************************/
132  /****************************************/
133 
135  m_cGrid(c_grid) {}
136 
137  /****************************************/
138  /****************************************/
139 
141  try {
142  /* Calculate the position of the LED in the space hash */
143  m_cGrid.PositionToCell(m_nI, m_nJ, m_nK, c_entity.GetPosition());
144  /* Update the corresponding cell */
145  m_cGrid.UpdateCell(m_nI, m_nJ, m_nK, c_entity);
146  }
147  catch(CARGoSException& ex) {
148  THROW_ARGOSEXCEPTION_NESTED("While updating the directional LED grid for LED \"" <<
149  c_entity.GetContext() + c_entity.GetId() << "\"", ex);
150  }
151  /* Continue with the other entities */
152  return true;
153  }
154 
155  /****************************************/
156  /****************************************/
157 
159  public:
160  void ApplyTo(CSpace& c_space, CDirectionalLEDEntity& c_entity) {
161  /* Add entity to space - this ensures that the directional LED entity
162  * gets an id before being added to the directional LED medium */
163  c_space.AddEntity(c_entity);
164  /* Enable the directional LED entity, if it's enabled - this ensures that
165  * the entity gets added to the directional LED medium if it's enabled */
166  c_entity.SetEnabled(c_entity.IsEnabled());
167  }
168  };
169 
171  public:
172  void ApplyTo(CSpace& c_space, CDirectionalLEDEntity& c_entity) {
173  /* Disable the entity - this ensures that the entity is
174  * removed from the directional LED medium */
175  c_entity.Disable();
176  /* Remove the directional LED entity from space */
177  c_space.RemoveEntity(c_entity);
178  }
179  };
180 
187 
188  /****************************************/
189  /****************************************/
190 
191 }
#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.
The namespace containing all the ARGoS related code.
Definition: ci_actuator.h:12
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.
REGISTER_SPACE_OPERATION(CSpaceOperationAddEntity, CSpaceOperationAddCFloorEntity, CFloorEntity)
CRadians ToRadians(const CDegrees &c_degrees)
Converts CDegrees to CRadians.
Definition: angles.h:498
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.
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
bool IsEnabled() const
Returns true if the entity is enabled.
Definition: entity.h:255
virtual void SetEnabled(bool b_enabled)
Enables or disables an entity.
Definition: entity.cpp:139
ssize_t GetIndex() const
Returns the entity index.
Definition: entity.h:234
const CVector3 & GetPosition() const
virtual void Init(TConfigurationNode &t_tree)
Initializes the state of the entity from the XML configuration tree.
The abstract definition of a space hash.
Definition: space_hash.h:34
virtual void UpdateCell(SInt32 n_x, SInt32 n_y, SInt32 n_z, ENTITY &c_entity)=0
Adds an entity to a cell of the space hash.
virtual SInt32 SpaceToHashTable(Real f_coord, UInt32 un_axis)
Converts a single space coordinate into a space hash cell coordinate.
Definition: space_hash.h:163
void AddEntity(ENTITY &c_entity)
Adds an entity of the given type.
Definition: space.h:274
void RemoveEntity(ENTITY &c_entity)
Removes an entity of the given type.
Definition: space.h:307
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
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
void ApplyTo(CSpace &c_space, CDirectionalLEDEntity &c_entity)
void ApplyTo(CSpace &c_space, CDirectionalLEDEntity &c_entity)
CDirectionalLEDMedium * m_pcMedium
virtual void Destroy()
Destroys the entity, undoing whatever was done by Init() or by the standalone constructor.
void SetColor(const CColor &c_color)
Sets the current color of the LED.
void SetMedium(CDirectionalLEDMedium &c_medium)
Sets the medium associated to this entity.
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.
virtual void SetEnabled(bool b_enabled)
Enables or disables an entity.
CDirectionalLEDEntity(CComposableEntity *pc_parent)
CDirectionalLEDMedium & GetMedium() const
Returns the medium associated to this directional LED.
virtual void operator()(CAbstractSpaceHash< CDirectionalLEDEntity > &c_space_hash, CDirectionalLEDEntity &c_element)
Updates the necessary cells of a space hash.
CDirectionalLEDEntityGridUpdater(CGrid< CDirectionalLEDEntity > &c_grid)
virtual bool operator()(CDirectionalLEDEntity &c_entity)
void RemoveEntity(CDirectionalLEDEntity &c_entity)
Removes the specified entity from the list of managed entities.
void AddEntity(CDirectionalLEDEntity &c_entity)
Adds the specified entity to the list of managed entities.