ARGoS 3
A parallel, multi-engine simulator for swarm robotics
radio_entity.cpp
Go to the documentation of this file.
1
7#include "radio_entity.h"
8#include <argos3/core/simulator/space/space.h>
9#include <argos3/core/simulator/simulator.h>
10#include <argos3/plugins/simulator/media/radio_medium.h>
11
12namespace argos {
13
14 /****************************************/
15 /****************************************/
16
18 CPositionalEntity(pc_parent),
19 m_fRange(0.0f),
20 m_pcMedium(nullptr) {}
21
22 /****************************************/
23 /****************************************/
24
26 const std::string& str_id,
27 Real f_range) :
28 CPositionalEntity(pc_parent, str_id, CVector3(), CQuaternion()),
29 m_fRange(f_range),
30 m_pcMedium(nullptr) {}
31
32 /****************************************/
33 /****************************************/
34
36 try {
37 /* Parse XML */
39 GetNodeAttribute(t_tree, "range", m_fRange);
40 }
41 catch(CARGoSException& ex) {
42 THROW_ARGOSEXCEPTION_NESTED("Error while initializing radio entity", ex);
43 }
44 }
45
46 /****************************************/
47 /****************************************/
48
50 /* Erase received messages */
51 m_vecData.clear();
52 }
53
54 /****************************************/
55 /****************************************/
56
58 Disable();
59 }
60
61 /****************************************/
62 /****************************************/
63
64 void CRadioEntity::SetEnabled(bool b_enabled) {
65 /* Perform generic enable behavior */
66 CEntity::SetEnabled(b_enabled);
67 if(b_enabled) {
68 /* Enable entity in medium */
69 if(m_pcMedium && GetIndex() >= 0)
70 m_pcMedium->AddEntity(*this);
71 }
72 else {
73 /* Disable entity in medium */
74 if(m_pcMedium)
76 }
77 }
78
79 /****************************************/
80 /****************************************/
81
83 if(m_pcMedium == nullptr) {
84 THROW_ARGOSEXCEPTION("radio entity \"" << GetContext() << GetId() <<
85 "\" has no associated medium.");
86 }
87 return *m_pcMedium;
88 }
89
90 /****************************************/
91 /****************************************/
92
94 if(m_pcMedium != nullptr && m_pcMedium != &c_medium)
96 m_pcMedium = &c_medium;
97 }
98
99 /****************************************/
100 /****************************************/
101
103 CRadioEntity& c_element) {
104 /* Calculate the position of the radop in the space hash */
105 c_space_hash.SpaceToHashTable(m_nI, m_nJ, m_nK, c_element.GetPosition());
106 /* Update the corresponding cell */
107 c_space_hash.UpdateCell(m_nI, m_nJ, m_nK, c_element);
108 }
109
110 /****************************************/
111 /****************************************/
112
115
116 /****************************************/
117 /****************************************/
118
120 try {
121 /* Calculate the position of the radio in the space hash */
122 m_cGrid.PositionToCell(m_nI, m_nJ, m_nK, c_entity.GetPosition());
123 /* Update the corresponding cell */
124 m_cGrid.UpdateCell(m_nI, m_nJ, m_nK, c_entity);
125 }
126 catch(CARGoSException& ex) {
127 THROW_ARGOSEXCEPTION_NESTED("While updating the radio grid for radio \"" <<
128 c_entity.GetContext() + c_entity.GetId() << "\"", ex);
129 }
130 /* Continue with the other entities */
131 return true;
132 }
133
134 /****************************************/
135 /****************************************/
136
138 public:
139 void ApplyTo(CSpace& c_space, CRadioEntity& c_entity) {
140 /* Add entity to space - this ensures that the radio entity
141 * gets an id before being added to the radio medium */
142 c_space.AddEntity(c_entity);
143 /* Enable the radio entity, if it's enabled - this ensures that
144 * the entity gets added to the radio if it's enabled */
145 c_entity.SetEnabled(c_entity.IsEnabled());
146 }
147 };
148
150 public:
151 void ApplyTo(CSpace& c_space, CRadioEntity& c_entity) {
152 /* Disable the entity - this ensures that the entity is
153 * removed from the radio medium */
154 c_entity.Disable();
155 /* Remove the radio entity from space */
156 c_space.RemoveEntity(c_entity);
157 }
158 };
159
166
167 /****************************************/
168 /****************************************/
169
170}
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 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.
A 3D vector class.
Definition vector3.h:31
void ApplyTo(CSpace &c_space, CRadioEntity &c_entity)
void ApplyTo(CSpace &c_space, CRadioEntity &c_entity)
void SetMedium(CRadioMedium &c_medium)
Sets the medium associated to this entity.
CRadioMedium & GetMedium() const
Returns the medium associated to this radio.
virtual void SetEnabled(bool b_enabled)
Enables or disables an entity.
CRadioMedium * m_pcMedium
std::vector< std::pair< CVector3, CByteArray > > m_vecData
virtual void Init(TConfigurationNode &t_tree)
Initializes the state of the entity from the XML configuration tree.
virtual void Reset()
Resets the state of the entity to whatever it was after Init() or the standalone constructor was call...
virtual void Destroy()
Destroys the entity, undoing whatever was done by Init() or by the standalone constructor.
CRadioEntity(CComposableEntity *pc_parent)
virtual void operator()(CAbstractSpaceHash< CRadioEntity > &c_space_hash, CRadioEntity &c_element)
Updates the necessary cells of a space hash.
CRadioEntityGridUpdater(CGrid< CRadioEntity > &c_grid)
virtual bool operator()(CRadioEntity &c_entity)
void AddEntity(CRadioEntity &c_entity)
Adds the specified entity to the list of managed entities.
void RemoveEntity(CRadioEntity &c_entity)
Removes the specified entity from the list of managed entities.