radio_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/radio_medium.h>
11 
12 namespace argos {
13 
14  /****************************************/
15  /****************************************/
16 
18  SAnchor& s_anchor,
19  const CVector3& c_offset) :
20  Radio(c_radio),
21  Anchor(s_anchor),
22  Offset(c_offset) {}
23 
24  /****************************************/
25  /****************************************/
26 
28  CComposableEntity(pc_parent) {
29  Disable();
30  }
31 
32  /****************************************/
33  /****************************************/
34 
36  const std::string& str_id) :
37  CComposableEntity(pc_parent, str_id) {
38  Disable();
39  }
40 
41  /****************************************/
42  /****************************************/
43 
45  try {
46  /* Init parent */
48  /* Go through the radop entries */
49  TConfigurationNodeIterator itRadio("radio");
50  for(itRadio = itRadio.begin(&t_tree);
51  itRadio != itRadio.end();
52  ++itRadio) {
53  /* Initialise the radio using the XML */
54  auto* pcRadio = new CRadioEntity(this);
55  pcRadio->Init(*itRadio);
56  CVector3 cOffset;
57  GetNodeAttribute(*itRadio, "position", cOffset);
58  /* Parse and look up the anchor */
59  std::string strAnchorId;
60  GetNodeAttribute(*itRadio, "anchor", strAnchorId);
61  /*
62  * NOTE: here we get a reference to the embodied entity
63  * This line works under the assumption that:
64  * 1. the RadioEquippedEntity has a parent;
65  * 2. the parent has a child whose id is "body"
66  * 3. the "body" is an embodied entity
67  * If any of the above is false, this line will bomb out.
68  */
69  auto& cBody = GetParent().GetComponent<CEmbodiedEntity>("body");
70  /* Add the radio to this container */
71  m_vecInstances.emplace_back(*pcRadio, cBody.GetAnchor(strAnchorId), cOffset);
72  AddComponent(*pcRadio);
73  }
75  }
76  catch(CARGoSException& ex) {
77  THROW_ARGOSEXCEPTION_NESTED("Failed to initialize radio equipped entity \"" <<
78  GetContext() + GetId() << "\".", ex);
79  }
80  }
81 
82  /****************************************/
83  /****************************************/
84 
86  /* Perform generic enable behavior */
88  /* Enable anchors */
89  for(SInstance& s_instance : m_vecInstances) {
90  s_instance.Anchor.Enable();
91  }
92  }
93 
94  /****************************************/
95  /****************************************/
96 
98  /* Perform generic disable behavior */
100  /* Disable anchors */
101  for(SInstance& s_instance : m_vecInstances) {
102  s_instance.Anchor.Disable();
103  }
104  }
105 
106  /****************************************/
107  /****************************************/
108 
109  void CRadioEquippedEntity::AddRadio(const std::string& str_id,
110  const CVector3& c_offset,
111  SAnchor& s_anchor,
112  Real f_transmit_range) {
113  /* create the new radio entity */
114  auto* pcRadio =
115  new CRadioEntity(this,
116  str_id,
117  f_transmit_range);
118  /* add it to the instances vector */
119  m_vecInstances.emplace_back(*pcRadio,
120  s_anchor,
121  c_offset);
122  /* inform the base class about the new entity */
123  AddComponent(*pcRadio);
125  }
126 
127 
128  /****************************************/
129  /****************************************/
130 
132  ARGOS_ASSERT(un_index < m_vecInstances.size(),
133  "CRadioEquippedEntity::GetRadio(), id=\"" <<
134  GetContext() + GetId() <<
135  "\": index out of bounds: un_index = " <<
136  un_index <<
137  ", m_vecInstances.size() = " <<
138  m_vecInstances.size());
139  return m_vecInstances[un_index].Radio;
140  }
141 
142  /****************************************/
143  /****************************************/
144 
146  CVector3 cPosition;
147  for(SInstance& s_instance : m_vecInstances) {
148  if(s_instance.Radio.IsEnabled()) {
149  cPosition = s_instance.Offset;
150  cPosition.Rotate(s_instance.Anchor.Orientation);
151  cPosition += s_instance.Anchor.Position;
152  s_instance.Radio.SetPosition(cPosition);
153  }
154  }
155  }
156 
157  /****************************************/
158  /****************************************/
159 
161  for(SInstance& s_instance : m_vecInstances) {
162  s_instance.Radio.SetMedium(c_medium);
163  }
164  }
165 
166  /****************************************/
167  /****************************************/
168 
170 
171  /****************************************/
172  /****************************************/
173 
174 }
#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.
unsigned int UInt32
32-bit unsigned integer.
Definition: datatypes.h:97
float Real
Collects all ARGoS code.
Definition: datatypes.h:39
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.
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 CRadioEntity.
SInstance::TVector m_vecInstances
List of the radios managed by this entity.
CRadioEntity & GetRadio(UInt32 un_index)
Returns a radio by numeric index.
virtual void Init(TConfigurationNode &t_tree)
Initializes the state of the entity from the XML configuration tree.
void AddRadio(const std::string &str_id, const CVector3 &c_offset, SAnchor &s_anchor, Real f_transmit_range)
Programmatically creates a new radio.
void SetMedium(CRadioMedium &c_medium)
Sets the medium associated to this entity.
CRadioEquippedEntity(CComposableEntity *pc_parent)
Class constructor.
virtual void UpdateComponents()
Calls the Update() method on all the components.
SInstance(CRadioEntity &c_radio, SAnchor &s_anchor, const CVector3 &c_offset)