mc_rtc  2.16.0
mc_controller.h
Go to the documentation of this file.
1 /*
2  * Copyright 2015-2022 CNRS-UM LIRMM, CNRS-AIST JRL
3  */
4 
5 #pragma once
6 
8 
9 #ifndef MC_RTC_BUILD_STATIC
10 
11 # include <mc_rtc/version.h>
12 
22 # define CONTROLLER_CHECK_VERSION(NAME) \
23  if(mc_rtc::MC_RTC_VERSION != mc_rtc::version()) \
24  { \
25  mc_rtc::log::error("{} was compiled with {} but mc_rtc is currently at version {}, you might experience subtle " \
26  "issues and should recompile your code", \
27  NAME, mc_rtc::MC_RTC_VERSION, mc_rtc::version()); \
28  }
29 
31 # define CONTROLLER_CONSTRUCTOR(NAME, TYPE) \
32  extern "C" \
33  { \
34  CONTROLLER_MODULE_API void MC_RTC_CONTROLLER(std::vector<std::string> & names) \
35  { \
36  CONTROLLER_CHECK_VERSION(NAME) \
37  names = {NAME}; \
38  } \
39  CONTROLLER_MODULE_API void destroy(mc_control::MCController * ptr) \
40  { delete ptr; } \
41  CONTROLLER_MODULE_API unsigned int create_args_required() \
42  { return 4; } \
43  CONTROLLER_MODULE_API mc_control::MCController * create(const std::string &, \
44  const mc_rbdyn::RobotModulePtr & robot, \
45  const double & dt, \
46  const mc_control::Configuration & conf) \
47  { return new TYPE(robot, dt, conf); } \
48  }
49 
51 # define MULTI_CONTROLLERS_CONSTRUCTOR(NAME0, NEWCTL0, NAME1, NEWCTL1) \
52  extern "C" \
53  { \
54  CONTROLLER_MODULE_API void MC_RTC_CONTROLLER(std::vector<std::string> & names) \
55  { \
56  CONTROLLER_CHECK_VERSION(NAME0) \
57  names = {NAME0, NAME1}; \
58  } \
59  \
60  CONTROLLER_MODULE_API void destroy(mc_control::MCController * ptr) \
61  { delete ptr; } \
62  CONTROLLER_MODULE_API unsigned int create_args_required() \
63  { return 4; } \
64  CONTROLLER_MODULE_API mc_control::MCController * create( \
65  const std::string & name, \
66  const mc_rbdyn::RobotModulePtr & rm, \
67  const double & dt, \
68  [[maybe_unused]] const mc_control::Configuration & config) \
69  { \
70  if(name == NAME0) { return new NEWCTL0; } \
71  return new NEWCTL1; \
72  } \
73  }
74 
76 # define SIMPLE_CONTROLLER_CONSTRUCTOR(NAME, TYPE) \
77  extern "C" \
78  { \
79  CONTROLLER_MODULE_API void MC_RTC_CONTROLLER(std::vector<std::string> & names) \
80  { \
81  CONTROLLER_CHECK_VERSION(NAME) \
82  names = {NAME}; \
83  } \
84  CONTROLLER_MODULE_API void destroy(mc_control::MCController * ptr) \
85  { delete ptr; } \
86  CONTROLLER_MODULE_API unsigned int create_args_required() \
87  { return 4; } \
88  CONTROLLER_MODULE_API mc_control::MCController * create(const std::string &, \
89  const mc_rbdyn::RobotModulePtr & robot, \
90  const double & dt, \
91  const mc_control::Configuration &) \
92  { return new TYPE(robot, dt); } \
93  }
94 
95 #else
96 
98 
99 # define CONTROLLER_CONSTRUCTOR(NAME, TYPE) \
100  namespace \
101  { \
102  static auto registered = []() \
103  { \
104  using fn_t = std::function<TYPE *(const std::shared_ptr<mc_rbdyn::RobotModule> &, const double &, \
105  const mc_control::Configuration &)>; \
106  mc_control::ControllerLoader::loader().register_object( \
107  NAME, fn_t([](const std::shared_ptr<mc_rbdyn::RobotModule> & robot, const double & dt, \
108  const mc_control::Configuration & conf) { return new TYPE(robot, dt, conf); })); \
109  return true; \
110  }(); \
111  }
112 
113 # define MULTI_CONTROLLERS_CONSTRUCTOR(NAME0, NEWCTL0, NAME1, NEWCTL1) \
114  namespace \
115  { \
116  static auto registered = []() \
117  { \
118  using TYPE0 = decltype(NEWCTL0); \
119  using TYPE1 = decltype(NEWCTL1); \
120  using fn0_t = std::function<TYPE0 *(const std::shared_ptr<mc_rbdyn::RobotModule> &, const double &, \
121  const mc_control::Configuration &)>; \
122  mc_control::ControllerLoader::loader().register_object( \
123  NAME0, fn_t([](const std::shared_ptr<mc_rbdyn::RobotModule> & robot, const double & dt, \
124  const mc_control::Configuration & conf) { return new NEWCTL0; })); \
125  using fn1_t = std::function<TYPE1 *(const std::shared_ptr<mc_rbdyn::RobotModule> &, const double &, \
126  const mc_control::Configuration &)>; \
127  mc_control::ControllerLoader::loader().register_object( \
128  NAME1, fn_t([](const std::shared_ptr<mc_rbdyn::RobotModule> & robot, const double & dt, \
129  const mc_control::Configuration & conf) { return new NEWCTL1; })); \
130  return true; \
131  }(); \
132  }
133 
134 # define SIMPLE_CONTROLLER_CONSTRUCTOR(NAME, TYPE) \
135  namespace \
136  { \
137  static auto registered = []() \
138  { \
139  using fn_t = std::function<TYPE *(const std::shared_ptr<mc_rbdyn::RobotModule> &, const double &, \
140  const mc_control::Configuration &)>; \
141  mc_control::ControllerLoader::loader().register_object( \
142  NAME, fn_t([](const std::shared_ptr<mc_rbdyn::RobotModule> & robot, const double & dt, \
143  const mc_control::Configuration &) { return new TYPE(robot, dt); })); \
144  return true; \
145  }(); \
146  }
147 
148 #endif