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
12namespace argos {
13
14 /****************************************/
15 /****************************************/
16
18 CPositionalEntity(pc_parent),
19 m_pcMedium(nullptr) {
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(nullptr) {
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
58
59 /****************************************/
60 /****************************************/
61
63 Disable();
64 }
65
66 /****************************************/
67 /****************************************/
68
69 void CLEDEntity::SetEnabled(bool b_enabled) {
70 /* Perform generic enable behavior */
71 CEntity::SetEnabled(b_enabled);
72 if(b_enabled) {
73 /* Enable entity in medium */
74 if(m_pcMedium && GetIndex() >= 0)
75 m_pcMedium->AddEntity(*this);
76 }
77 else {
78 /* Disable entity in medium */
79 if(m_pcMedium)
81 }
82 }
83
84 /****************************************/
85 /****************************************/
86
87 void CLEDEntity::SetColor(const CColor& c_color) {
88 m_cColor = c_color;
89 }
90
91 /****************************************/
92 /****************************************/
93
95 if(m_pcMedium == nullptr) {
96 THROW_ARGOSEXCEPTION("LED entity \"" << GetContext() << GetId() << "\" has no medium associated.");
97 }
98 return *m_pcMedium;
99 }
100
101 /****************************************/
102 /****************************************/
103
105 if(m_pcMedium != nullptr && m_pcMedium != &c_medium)
106 m_pcMedium->RemoveEntity(*this);
107 m_pcMedium = &c_medium;
108 }
109
110 /****************************************/
111 /****************************************/
112
114 CLEDEntity& c_element) {
115 /* Discard LEDs switched off */
116 if(c_element.GetColor() != CColor::BLACK) {
117 /* Calculate the position of the LED in the space hash */
118 c_space_hash.SpaceToHashTable(m_nI, m_nJ, m_nK, c_element.GetPosition());
119 /* Update the corresponding cell */
120 c_space_hash.UpdateCell(m_nI, m_nJ, m_nK, c_element);
121 }
122 }
123
124 /****************************************/
125 /****************************************/
126
129
130 /****************************************/
131 /****************************************/
132
134 /* Discard disabled and switched off LEDs */
135 if(c_entity.GetColor() != CColor::BLACK) {
136 try {
137 /* Calculate the position of the LED in the space hash */
138 m_cGrid.PositionToCell(m_nI, m_nJ, m_nK, c_entity.GetPosition());
139 /* Update the corresponding cell */
140 m_cGrid.UpdateCell(m_nI, m_nJ, m_nK, c_entity);
141 }
142 catch(CARGoSException& ex) {
143 THROW_ARGOSEXCEPTION_NESTED("While updating the LED grid for LED \"" << c_entity.GetContext() << c_entity.GetId() << "\"", ex);
144 }
145 }
146 /* Continue with the other entities */
147 return true;
148 }
149
150 /****************************************/
151 /****************************************/
152
154 public:
155 void ApplyTo(CSpace& c_space, CLEDEntity& c_entity) {
156 /* Add entity to space - this ensures that the LED entity
157 * gets an id before being added to the LED medium */
158 c_space.AddEntity(c_entity);
159 /* Enable the LED entity, if it's enabled - this ensures that
160 * the entity gets added to the LED if it's enabled */
161 c_entity.SetEnabled(c_entity.IsEnabled());
162 }
163 };
164
166 public:
167 void ApplyTo(CSpace& c_space, CLEDEntity& c_entity) {
168 /* Disable the entity - this ensures that the entity is
169 * removed from the LED medium */
170 c_entity.Disable();
171 /* Remove the LED entity from space */
172 c_space.RemoveEntity(c_entity);
173 }
174 };
175
178
179 /****************************************/
180 /****************************************/
181
182}
#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.
#define REGISTER_SPACE_OPERATION(ACTION, OPERATION, ENTITY)
Definition space.h:549
The namespace containing all the ARGoS related code.
Definition ci_actuator.h:12
ticpp::Element TConfigurationNode
The ARGoS configuration XML node.
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
static CColor BLACK
Definition color.h:29
A 3D vector class.
Definition vector3.h:31
void ApplyTo(CSpace &c_space, CLEDEntity &c_entity)
void ApplyTo(CSpace &c_space, CLEDEntity &c_entity)
const CColor & GetColor() const
Returns the current color of the LED.
Definition led_entity.h:58
virtual void Reset()
Resets the state of the entity to whatever it was after Init() or the standalone constructor was call...
CLEDMedium * m_pcMedium
Definition led_entity.h:122
void SetColor(const CColor &c_color)
Sets the current color of the LED.
virtual void SetEnabled(bool b_enabled)
Enables or disables an entity.
CLEDEntity(CComposableEntity *pc_parent)
virtual void Destroy()
Destroys the entity, undoing whatever was done by Init() or by the standalone constructor.
CLEDMedium & GetMedium() const
Returns the medium associated to this LED.
void SetMedium(CLEDMedium &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.
virtual void operator()(CAbstractSpaceHash< CLEDEntity > &c_space_hash, CLEDEntity &c_element)
Updates the necessary cells of a space hash.
virtual bool operator()(CLEDEntity &c_entity)
CLEDEntityGridUpdater(CGrid< CLEDEntity > &c_grid)
void RemoveEntity(CLEDEntity &c_entity)
Removes the specified entity from the list of managed entities.
void AddEntity(CLEDEntity &c_entity)
Adds the specified entity to the list of managed entities.