ci_controller.h
Go to the documentation of this file.
1 
7 #ifndef CCI_CONTROLLER_H
8 #define CCI_CONTROLLER_H
9 
10 namespace argos {
11  class CCI_Controller;
12 }
13 
14 #include <argos3/core/utility/configuration/base_configurable_resource.h>
15 #include <argos3/core/utility/datatypes/datatypes.h>
16 #include <argos3/core/control_interface/ci_sensor.h>
17 #include <argos3/core/control_interface/ci_actuator.h>
18 #include <argos3/core/utility/plugins/factory.h>
19 
20 #include <map>
21 #include <string>
22 #include <cxxabi.h>
23 #include <typeinfo>
24 
25 namespace argos {
26 
31 
32  public:
33 
37  virtual ~CCI_Controller();
38 
48  virtual void Init(TConfigurationNode& t_node) {}
49 
55  virtual void ControlStep() {}
56 
65  virtual void Reset() {}
66 
72  virtual void Destroy() {}
73 
78  inline const std::string& GetId() const {
79  return m_strId;
80  }
81 
88  inline void SetId(const std::string& str_id) {
89  m_strId = str_id;
90  }
91 
105  template<typename ACTUATOR_IMPL>
106  ACTUATOR_IMPL* GetActuator(const std::string& str_actuator_type) {
107  CCI_Actuator::TMap::const_iterator it = m_mapActuators.find(str_actuator_type);
108  if (it != m_mapActuators.end()) {
109  ACTUATOR_IMPL* pcActuator = dynamic_cast<ACTUATOR_IMPL*>(it->second);
110  if(pcActuator != NULL) {
111  return pcActuator;
112  }
113  else {
114  char* pchDemangledType = abi::__cxa_demangle(typeid(ACTUATOR_IMPL).name(), NULL, NULL, NULL);
115  THROW_ARGOSEXCEPTION("Actuator type " << str_actuator_type << " cannot be cast to type " << pchDemangledType);
116  }
117  }
118  else {
119  THROW_ARGOSEXCEPTION("Unknown actuator type " << str_actuator_type << " requested in controller. Did you add it to the XML file?");
120  }
121  }
122 
136  template<typename SENSOR_IMPL>
137  SENSOR_IMPL* GetSensor(const std::string& str_sensor_type) {
138  CCI_Sensor::TMap::const_iterator it = m_mapSensors.find(str_sensor_type);
139  if (it != m_mapSensors.end()) {
140  SENSOR_IMPL* pcSensor = dynamic_cast<SENSOR_IMPL*>(it->second);
141  if(pcSensor != NULL) {
142  return pcSensor;
143  }
144  else {
145  char* pchDemangledType = abi::__cxa_demangle(typeid(SENSOR_IMPL).name(), NULL, NULL, NULL);
146  THROW_ARGOSEXCEPTION("Sensor type " << str_sensor_type << " cannot be cast to type " << pchDemangledType);
147  }
148  }
149  else {
150  THROW_ARGOSEXCEPTION("Unknown sensor type " << str_sensor_type << " requested in controller. Did you add it to the XML file?");
151  }
152  }
153 
160  bool HasActuator(const std::string& str_actuator_type) const;
161 
168  bool HasSensor(const std::string& str_sensor_type) const;
169 
175  return m_mapActuators;
176  }
177 
183  return m_mapSensors;
184  }
185 
193  inline void AddActuator(const std::string& str_actuator_type,
194  CCI_Actuator* pc_actuator) {
195  m_mapActuators[str_actuator_type] = pc_actuator;
196  }
197 
205  inline void AddSensor(const std::string& str_sensor_type,
206  CCI_Sensor* pc_sensor) {
207  m_mapSensors[str_sensor_type] = pc_sensor;
208  }
209 
210  protected:
211 
214 
217 
219  std::string m_strId;
220 
221  };
222 
223 }
224 
230 #ifdef ARGOS_DYNAMIC_LIBRARY_LOADING
231 
232 #define REGISTER_CONTROLLER(CLASSNAME, LABEL) \
233  REGISTER_SYMBOL(CCI_Controller, \
234  CLASSNAME, \
235  LABEL, \
236  "undefined", \
237  "undefined", \
238  "undefined", \
239  "undefined", \
240  "undefined")
241 
242 #else
243 
244 extern "C" {
245  extern argos::CCI_Controller* ControllerMaker(const std::string& str_label);
246 }
247 
248 #define REGISTER_CONTROLLER(CLASSNAME, LABEL) \
249  extern "C" { \
250  argos::CCI_Controller* ControllerMaker(const std::string& str_label) { \
251  if(str_label != LABEL) { \
252  THROW_ARGOSEXCEPTION("Controller label \"" << \
253  str_label << \
254  "\" does not match the registered one: \"" << \
255  LABEL << "\""); \
256  } \
257  return new CLASSNAME; \
258  } \
259  }
260 
261 #endif
262 
263 #endif
argos::CCI_Controller * ControllerMaker(const std::string &str_label)
Registers a new controller inside ARGoS.
#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
ticpp::Element TConfigurationNode
The ARGoS configuration XML node.
The basic interface for all actuators.
Definition: ci_actuator.h:34
std::map< std::string, CCI_Actuator *, std::less< std::string > > TMap
Definition: ci_actuator.h:38
The basic interface for a robot controller.
Definition: ci_controller.h:30
virtual void Init(TConfigurationNode &t_node)
Initializes the controller.
Definition: ci_controller.h:48
CCI_Sensor::TMap & GetAllSensors()
Returns a map of the associated sensors.
void AddSensor(const std::string &str_sensor_type, CCI_Sensor *pc_sensor)
Adds an sensor to this controller.
bool HasSensor(const std::string &str_sensor_type) const
Returns true if an sensor with the passed type is associated to this controller.
CCI_Actuator::TMap & GetAllActuators()
Returns a map of the associated actuators.
ACTUATOR_IMPL * GetActuator(const std::string &str_actuator_type)
Returns a pointer to the an actuator, given its type.
virtual void ControlStep()
Executes a control step.
Definition: ci_controller.h:55
void SetId(const std::string &str_id)
Sets the id of the robot associated to this controller.
Definition: ci_controller.h:88
virtual void Destroy()
The default implementation of this method does nothing.
Definition: ci_controller.h:72
void AddActuator(const std::string &str_actuator_type, CCI_Actuator *pc_actuator)
Adds an actuator to this controller.
virtual void Reset()
Resets the state of the controller to what it was right after Init() was executed.
Definition: ci_controller.h:65
SENSOR_IMPL * GetSensor(const std::string &str_sensor_type)
Returns a pointer to the an sensor, given its type.
CCI_Sensor::TMap m_mapSensors
A map containing all the sensors associated to this controller.
virtual ~CCI_Controller()
Class destructor.
const std::string & GetId() const
Returns the id of the robot associated to this controller.
Definition: ci_controller.h:78
bool HasActuator(const std::string &str_actuator_type) const
Returns true if an actuator with the passed type is associated to this controller.
CCI_Actuator::TMap m_mapActuators
A map containing all the actuators associated to this controller.
std::string m_strId
The id of the robot associated to this controller
The basic interface for all sensors.
Definition: ci_sensor.h:34
std::map< std::string, CCI_Sensor *, std::less< std::string > > TMap
Definition: ci_sensor.h:38
This class is the base of all XML-configurable ARGoS interface.