ARGoS  3
A parallel, multi-engine simulator for swarm robotics
led_entity.cpp
Go to the documentation of this file.
1 
7 #include "led_entity.h"
8 #include <argos3/core/simulator/simulator.h>
9 #include <argos3/core/simulator/space/space.h>
10 #include <argos3/plugins/simulator/media/led_medium.h>
11 
12 namespace argos {
13 
14  /****************************************/
15  /****************************************/
16 
18  CPositionalEntity(pc_parent),
19  m_pcMedium(NULL) {
20  Disable();
21  }
22 
23  /****************************************/
24  /****************************************/
25 
27  const std::string& str_id,
28  const CVector3& c_position,
29  const CColor& c_color) :
30  CPositionalEntity(pc_parent, str_id, c_position, CQuaternion()),
31  m_cColor(c_color),
32  m_cInitColor(c_color),
33  m_pcMedium(NULL) {
34  SetColor(c_color);
35  }
36 
37  /****************************************/
38  /****************************************/
39 
41  try {
42  /* Parse XML */
44  GetNodeAttribute(t_tree, "color", m_cInitColor);
46  }
47  catch(CARGoSException& ex) {
48  THROW_ARGOSEXCEPTION_NESTED("Error while initializing led entity", ex);
49  }
50  }
51 
52  /****************************************/
53  /****************************************/
54 
57  }
58 
59  /****************************************/
60  /****************************************/
61 
63  if(HasMedium()) {
65  }
66  }
67 
68  /****************************************/
69  /****************************************/
70 
71  void CLEDEntity::SetEnabled(bool b_enabled) {
73  }
74 
75  /****************************************/
76  /****************************************/
77 
78  void CLEDEntity::SetColor(const CColor& c_color) {
79  m_cColor = c_color;
80  SetEnabled(c_color != CColor::BLACK);
81  }
82 
83  /****************************************/
84  /****************************************/
85 
88  m_pcMedium = &c_medium;
89  c_medium.AddEntity(*this);
90  Enable();
91  }
92 
93  /****************************************/
94  /****************************************/
95 
97  try {
98  GetMedium().RemoveEntity(*this);
99  m_pcMedium = NULL;
100  Disable();
101  }
102  catch(CARGoSException& ex) {
103  THROW_ARGOSEXCEPTION_NESTED("Can't remove LED entity \"" << GetId() << "\" from medium.", ex);
104  }
105  }
106 
107  /****************************************/
108  /****************************************/
109 
111  if(m_pcMedium == NULL) {
112  THROW_ARGOSEXCEPTION("LED entity \"" << GetId() << "\" has no medium associated.");
113  }
114  return *m_pcMedium;
115  }
116 
117  /****************************************/
118  /****************************************/
119 
121  CLEDEntity& c_element) {
122  /* Discard LEDs switched off */
123  if(c_element.GetColor() != CColor::BLACK) {
124  /* Calculate the position of the LED in the space hash */
125  c_space_hash.SpaceToHashTable(m_nI, m_nJ, m_nK, c_element.GetPosition());
126  /* Update the corresponding cell */
127  c_space_hash.UpdateCell(m_nI, m_nJ, m_nK, c_element);
128  }
129  }
130 
131  /****************************************/
132  /****************************************/
133 
135  m_cGrid(c_grid) {}
136 
137  /****************************************/
138  /****************************************/
139 
141  /* Discard LEDs switched off */
142  if(c_entity.GetColor() != CColor::BLACK) {
143  try {
144  /* Calculate the position of the LED in the space hash */
145  m_cGrid.PositionToCell(m_nI, m_nJ, m_nK, c_entity.GetPosition());
146  /* Update the corresponding cell */
147  m_cGrid.UpdateCell(m_nI, m_nJ, m_nK, c_entity);
148  }
149  catch(CARGoSException& ex) {
150  THROW_ARGOSEXCEPTION_NESTED("While updating the LED grid for LED \"" << c_entity.GetContext() << c_entity.GetId() << "\"", ex);
151  }
152  }
153  /* Continue with the other entities */
154  return true;
155  }
156 
157  /****************************************/
158  /****************************************/
159 
161 
162  /****************************************/
163  /****************************************/
164 
165 }
argos::CLEDEntity::Destroy
virtual void Destroy()
Destroys the entity, undoing whatever was done by Init() or by the standalone constructor.
Definition: led_entity.cpp:62
argos::CLEDEntity::GetColor
const CColor & GetColor() const
Returns the current color of the LED.
Definition: led_entity.h:58
argos::CAbstractSpaceHash
The abstract definition of a space hash.
Definition: space_hash.h:34
argos::CLEDEntityGridUpdater::CLEDEntityGridUpdater
CLEDEntityGridUpdater(CGrid< CLEDEntity > &c_grid)
Definition: led_entity.cpp:134
argos::CEntity::GetId
const std::string & GetId() const
Returns the id of this entity.
Definition: entity.h:157
argos::CLEDEntity::m_pcMedium
CLEDMedium * m_pcMedium
Definition: led_entity.h:133
argos
The namespace containing all the ARGoS related code.
Definition: ci_actuator.h:12
argos::CVector3
A 3D vector class.
Definition: vector3.h:29
argos::CGrid
Definition: grid.h:12
argos::CLEDEntity
Definition: led_entity.h:24
led_entity.h
argos::CComposableEntity
Basic class for an entity that contains other entities.
Definition: composable_entity.h:32
argos::CARGoSException
The exception that wraps all errors in ARGoS.
Definition: argos_exception.h:61
argos::CLEDEntity::m_cInitColor
CColor m_cInitColor
Definition: led_entity.h:132
argos::CLEDEntity::Init
virtual void Init(TConfigurationNode &t_tree)
Initializes the state of the entity from the XML configuration tree.
Definition: led_entity.cpp:40
argos::CAbstractSpaceHash::UpdateCell
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.
argos::CEntity::Enable
void Enable()
Enables the entity.
Definition: entity.h:239
argos::CLEDEntity::SetColor
void SetColor(const CColor &c_color)
Sets the current color of the LED.
Definition: led_entity.cpp:78
argos::REGISTER_STANDARD_SPACE_OPERATIONS_ON_ENTITY
REGISTER_STANDARD_SPACE_OPERATIONS_ON_ENTITY(CEntity)
argos::TConfigurationNode
ticpp::Element TConfigurationNode
The ARGoS configuration XML node.
Definition: argos_configuration.h:27
argos::CLEDEntity::RemoveFromMedium
void RemoveFromMedium()
Removes the LEDs from the wanted LED medium.
Definition: led_entity.cpp:96
argos::CLEDMedium
Definition: led_medium.h:15
argos::CPositionalEntity::GetPosition
const CVector3 & GetPosition() const
Definition: positional_entity.h:36
argos::CLEDEntity::HasMedium
bool HasMedium() const
Returns true if this LED is associated to a medium.
Definition: led_entity.h:118
argos::CQuaternion
Definition: quaternion.h:14
argos::CPositionalEntity
Definition: positional_entity.h:20
argos::CLEDEntity::CLEDEntity
CLEDEntity(CComposableEntity *pc_parent)
Definition: led_entity.cpp:17
THROW_ARGOSEXCEPTION_NESTED
#define THROW_ARGOSEXCEPTION_NESTED(message, nested)
This macro throws an ARGoS exception with the passed message and nesting the passed exception.
Definition: argos_exception.h:115
THROW_ARGOSEXCEPTION
#define THROW_ARGOSEXCEPTION(message)
This macro throws an ARGoS exception with the passed message.
Definition: argos_exception.h:111
argos::CEntity::SetEnabled
virtual void SetEnabled(bool b_enabled)
Enables or disables an entity.
Definition: entity.cpp:131
argos::CAbstractSpaceHash::SpaceToHashTable
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
argos::CLEDMedium::AddEntity
void AddEntity(CLEDEntity &c_entity)
Adds the specified entity to the list of managed entities.
Definition: led_medium.cpp:100
argos::CEntity::Disable
void Disable()
Disables the entity.
Definition: entity.h:249
argos::CColor
The basic color type.
Definition: color.h:25
argos::GetNodeAttribute
void GetNodeAttribute(TConfigurationNode &t_node, const std::string &str_attribute, T &t_buffer)
Returns the value of a node's attribute.
Definition: argos_configuration.h:208
argos::CEntity::GetContext
std::string GetContext() const
Returns the context of this entity.
Definition: entity.cpp:71
argos::CLEDEntity::AddToMedium
void AddToMedium(CLEDMedium &c_medium)
Adds the LEDs to the wanted LED medium.
Definition: led_entity.cpp:86
argos::CLEDEntity::SetEnabled
virtual void SetEnabled(bool b_enabled)
Enables or disables an entity.
Definition: led_entity.cpp:71
argos::CPositionalEntity::Init
virtual void Init(TConfigurationNode &t_tree)
Initializes the state of the entity from the XML configuration tree.
Definition: positional_entity.cpp:37
argos::CColor::BLACK
static CColor BLACK
Definition: color.h:29
argos::CLEDEntity::m_cColor
CColor m_cColor
Definition: led_entity.h:131
argos::CLEDEntity::GetMedium
CLEDMedium & GetMedium() const
Returns the medium associated to this LED.
Definition: led_entity.cpp:110
argos::CLEDMedium::RemoveEntity
void RemoveEntity(CLEDEntity &c_entity)
Removes the specified entity from the list of managed entities.
Definition: led_medium.cpp:107
argos::CLEDEntityGridUpdater::operator()
virtual bool operator()(CLEDEntity &c_entity)
Definition: led_entity.cpp:140
argos::CLEDEntity::Reset
virtual void Reset()
Resets the state of the entity to whatever it was after Init() or the standalone constructor was call...
Definition: led_entity.cpp:55
argos::CLEDEntitySpaceHashUpdater::operator()
virtual void operator()(CAbstractSpaceHash< CLEDEntity > &c_space_hash, CLEDEntity &c_element)
Updates the necessary cells of a space hash.
Definition: led_entity.cpp:120