GAMS  1.4.0
Multicontroller.h
Go to the documentation of this file.
1 
54 #ifndef _GAMS_CONTROLLERS_MULTICONTROLLER_H_
55 #define _GAMS_CONTROLLERS_MULTICONTROLLER_H_
56 
57 #include "gams/GamsExport.h"
58 #include "gams/variables/Agent.h"
59 #include "gams/variables/Swarm.h"
60 #include "gams/variables/Self.h"
61 #include "gams/variables/Sensor.h"
69 #include "madara/transport/SharedMemoryPush.h"
70 
71 #include "madara/knowledge/containers/String.h"
72 #include "madara/knowledge/containers/Vector.h"
73 
74 #ifdef _GAMS_SCRIMMAGE_
75 #include <scrimmage/simcontrol/SimControl.h>
76 #endif
77 
78 #ifdef _GAMS_JAVA_
79 #include <jni.h>
80 #endif
81 
82 namespace gams
83 {
84  namespace controllers
85  {
90  {
91  public:
97  Multicontroller(size_t num_controllers = 1,
98  const ControllerSettings & settings = ControllerSettings());
99 
103  virtual ~Multicontroller();
104 
114  const std::vector <std::string> & aliases,
115  algorithms::AlgorithmFactory * factory);
116 
126  const std::vector <std::string> & aliases,
127  platforms::PlatformFactory * factory);
128 
144  const madara::transport::QoSTransportSettings & source_settings);
145 
149  void clear_knowledge(void);
150 
156  void evaluate(const std::string & logic,
157  const madara::knowledge::EvalSettings & settings =
158  madara::knowledge::EvalSettings::DELAY);
159 
166  void evaluate(size_t controller_index, const std::string & logic,
167  const madara::knowledge::EvalSettings & settings =
168  madara::knowledge::EvalSettings::DELAY);
169 
176  const std::string & algorithm,
177  const madara::knowledge::KnowledgeMap & args = madara::knowledge::KnowledgeMap());
178 
185  void init_accent(size_t controller_index,
186  const std::string & algorithm,
187  const madara::knowledge::KnowledgeMap & args = madara::knowledge::KnowledgeMap());
188 
195  const std::string & algorithm,
196  const madara::knowledge::KnowledgeMap & args = madara::knowledge::KnowledgeMap());
197 
204  void init_algorithm(size_t controller_index,
205  const std::string & algorithm,
206  const madara::knowledge::KnowledgeMap & args = madara::knowledge::KnowledgeMap());
207 
215  void init_algorithm(size_t controller_index,
216  algorithms::BaseAlgorithm * algorithm);
217 
224  const std::string & platform,
225  const madara::knowledge::KnowledgeMap & args =
226  madara::knowledge::KnowledgeMap());
227 
234  void init_platform(size_t controller_index,
235  const std::string & platform,
236  const madara::knowledge::KnowledgeMap & args =
237  madara::knowledge::KnowledgeMap());
238 
246  void init_platform(size_t controller_index,
247  platforms::BasePlatform * platform);
248 
249 #ifdef _GAMS_JAVA_
255  void init_algorithm(size_t controller_index, jobject algorithm);
256 
262  void init_platform(size_t controller_index, jobject platform);
263 #endif
264 
270  void init_vars(const madara::knowledge::KnowledgeRecord::Integer & id = 0,
271  const madara::knowledge::KnowledgeRecord::Integer & processes = -1);
272 
279  void refresh_vars(bool init_non_self_vars = false);
280 
288  void init_vars(size_t controller_index,
289  platforms::BasePlatform & platform);
290 
298  void init_vars(size_t controller_index,
299  algorithms::BaseAlgorithm & algorithm);
300 
306  algorithms::BaseAlgorithm * get_algorithm(size_t controller_index = 0);
307 
313  platforms::BasePlatform * get_platform(size_t controller_index = 0);
314 
319  size_t get_num_controllers(void);
320 
326  madara::knowledge::KnowledgeBase get_kb(size_t controller_index);
327 
332  void clear_accents(size_t controller_index = 0);
333 
340  void resize(size_t num_controllers, bool init_non_self_vars = false);
341 
348  int run_once(void);
349 
354  int run(void);
355 
367  int run(double loop_period,
368  double max_runtime = -1,
369  double send_period = -1.0);
370 
381  inline int run_hz(double loop_hz = 0.0,
382  double max_runtime = -1,
383  double send_hz = -1.0)
384  {
385  double loop_rate, send_rate;
386 
387  // check for bad data
388  if(loop_hz <= 0.0)
389  loop_rate = 0.0;
390  // otherwise, set rate to 1s divided by the intended hz
391  else
392  loop_rate = 1.0 / loop_hz;
393 
394  // copy from loop hz if send hz is non-positive
395  if(send_hz <= 0)
396  send_rate = loop_rate;
397  // otherwise, set rate to 1s divided by the intended hz
398  else
399  send_rate = 1.0 / send_hz;
400 
401  return run(loop_rate, max_runtime, send_rate);
402  }
403 
404  protected:
405 
407  std::vector <BaseController *> controllers_;
408 
410  std::vector <madara::knowledge::KnowledgeBase> kbs_;
411 
413  std::vector <madara::transport::SharedMemoryPush *> transports_;
414 
417 
419  int sim_step_ = 0;
420 
421  };
422  }
423 }
424 
425 #endif // _GAMS_CONTROLLERS_MULTICONTROLLER_H_
Copyright(c) 2014-2018 Carnegie Mellon University.
Copyright(c) 2014 Carnegie Mellon University.
Copyright(c) 2014 Carnegie Mellon University.
Copyright(c) 2014 Carnegie Mellon University.
Copyright(c) 2014 Carnegie Mellon University.
#define GAMS_EXPORT
Definition: GamsExport.h:20
Copyright(c) 2014 Carnegie Mellon University.
Copyright(c) 2014 Carnegie Mellon University.
Copyright(c) 2014 Carnegie Mellon University.
Copyright(c) 2014 Carnegie Mellon University.
Copyright(c) 2014 Carnegie Mellon University.
Base class for algorithm factories that classes derived from.
The base class that algorithms in GAMS use.
Definition: BaseAlgorithm.h:95
Settings used for initializing GAMS controllers.
A controller that has the capability of managing multiple controllers.
void evaluate(size_t controller_index, const std::string &logic, const madara::knowledge::EvalSettings &settings=madara::knowledge::EvalSettings::DELAY)
Evaluates a karl logic across all controllers and knowledge bases.
void refresh_vars(bool init_non_self_vars=false)
Calls init_vars for each controller.
void init_algorithm(size_t controller_index, const std::string &algorithm, const madara::knowledge::KnowledgeMap &args=madara::knowledge::KnowledgeMap())
Initializes a specific controller with an algorithm.
algorithms::BaseAlgorithm * get_algorithm(size_t controller_index=0)
Gets the current algorithm at the controller index.
size_t get_num_controllers(void)
Returns the number of controllers being managed.
void init_vars(size_t controller_index, algorithms::BaseAlgorithm &algorithm)
Initializes containers and knowledge base in an algorithm.
void init_algorithm(size_t controller_index, algorithms::BaseAlgorithm *algorithm)
Initializes the controller with a user-provided algorithm.
void clear_accents(size_t controller_index=0)
Clears all accent algorithms.
void add_platform_factory(const std::vector< std::string > &aliases, platforms::PlatformFactory *factory)
Adds an aliased platform factory.
int run(double loop_period, double max_runtime=-1, double send_period=-1.0)
Runs iterations of the MAPE loop with specified periods.
void init_accent(size_t controller_index, const std::string &algorithm, const madara::knowledge::KnowledgeMap &args=madara::knowledge::KnowledgeMap())
Adds an accent algorithm.
std::vector< BaseController * > controllers_
Controllers that need to be instrumented.
void init_vars(const madara::knowledge::KnowledgeRecord::Integer &id=0, const madara::knowledge::KnowledgeRecord::Integer &processes=-1)
Initializes global variable containers.
platforms::BasePlatform * get_platform(size_t controller_index=0)
Gets the current platform at the controller index.
void init_platform(size_t controller_index, platforms::BasePlatform *platform)
Initializes the controller with a user-provided platform.
void clear_knowledge(void)
Clears all controller knowledge bases.
int run_hz(double loop_hz=0.0, double max_runtime=-1, double send_hz=-1.0)
Runs iterations of the MAPE loop with specified hertz.
void init_vars(size_t controller_index, platforms::BasePlatform &platform)
Initializes containers and knowledge base in a platform This is usually the first thing a developer s...
void init_algorithm(const std::string &algorithm, const madara::knowledge::KnowledgeMap &args=madara::knowledge::KnowledgeMap())
Initializes all managed controllers with an algorithm.
void evaluate(const std::string &logic, const madara::knowledge::EvalSettings &settings=madara::knowledge::EvalSettings::DELAY)
Evaluates a karl logic across all controllers and knowledge bases.
Multicontroller(size_t num_controllers=1, const ControllerSettings &settings=ControllerSettings())
Constructor.
void init_platform(size_t controller_index, const std::string &platform, const madara::knowledge::KnowledgeMap &args=madara::knowledge::KnowledgeMap())
Initializes the platform.
std::vector< madara::knowledge::KnowledgeBase > kbs_
Knowledge base.
void add_algorithm_factory(const std::vector< std::string > &aliases, algorithms::AlgorithmFactory *factory)
Adds an aliased algorithm factory.
void add_transports(const madara::transport::QoSTransportSettings &source_settings)
Add transports to the knowledge bases that are being used by each controller.
std::vector< madara::transport::SharedMemoryPush * > transports_
transports meant for fast memory transport within the controller
int run(void)
Runs iterations of the MAPE loop with configured settings.
int run_once(void)
Runs a single iteration of the MAPE loop Always sends updates after the iteration.
void init_accent(const std::string &algorithm, const madara::knowledge::KnowledgeMap &args=madara::knowledge::KnowledgeMap())
Initializes all managed controllers with an accent algorithm.
void resize(size_t num_controllers, bool init_non_self_vars=false)
Resizes the number of controllers and knowledge bases.
madara::knowledge::KnowledgeBase get_kb(size_t controller_index)
Returns the kb that is being used by the internal controller.
virtual ~Multicontroller()
Destructor.
ControllerSettings settings_
Settings for controller management and qos.
void init_platform(const std::string &platform, const madara::knowledge::KnowledgeMap &args=madara::knowledge::KnowledgeMap())
Initializes all managed controllers with the platform.
The base platform for all platforms to use.
Definition: BasePlatform.h:113
Base class for platform factories to create BasePlatforms.
Contains all GAMS-related tools, classes and code.