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  { \
41  delete ptr; \
42  } \
43  CONTROLLER_MODULE_API unsigned int create_args_required() \
44  { \
45  return 4; \
46  } \
47  CONTROLLER_MODULE_API mc_control::MCController * create(const std::string &, \
48  const mc_rbdyn::RobotModulePtr & robot, \
49  const double & dt, \
50  const mc_control::Configuration & conf) \
51  { \
52  return new TYPE(robot, dt, conf); \
53  } \
54  }
55 
57 # define MULTI_CONTROLLERS_CONSTRUCTOR(NAME0, NEWCTL0, NAME1, NEWCTL1) \
58  extern "C" \
59  { \
60  CONTROLLER_MODULE_API void MC_RTC_CONTROLLER(std::vector<std::string> & names) \
61  { \
62  CONTROLLER_CHECK_VERSION(NAME0) \
63  names = {NAME0, NAME1}; \
64  } \
65  \
66  CONTROLLER_MODULE_API void destroy(mc_control::MCController * ptr) \
67  { \
68  delete ptr; \
69  } \
70  CONTROLLER_MODULE_API unsigned int create_args_required() \
71  { \
72  return 4; \
73  } \
74  CONTROLLER_MODULE_API mc_control::MCController * create( \
75  const std::string & name, \
76  const mc_rbdyn::RobotModulePtr & rm, \
77  const double & dt, \
78  [[maybe_unused]] const mc_control::Configuration & config) \
79  { \
80  if(name == NAME0) { return new NEWCTL0; } \
81  return new NEWCTL1; \
82  } \
83  }
84 
86 # define SIMPLE_CONTROLLER_CONSTRUCTOR(NAME, TYPE) \
87  extern "C" \
88  { \
89  CONTROLLER_MODULE_API void MC_RTC_CONTROLLER(std::vector<std::string> & names) \
90  { \
91  CONTROLLER_CHECK_VERSION(NAME) \
92  names = {NAME}; \
93  } \
94  CONTROLLER_MODULE_API void destroy(mc_control::MCController * ptr) \
95  { \
96  delete ptr; \
97  } \
98  CONTROLLER_MODULE_API unsigned int create_args_required() \
99  { \
100  return 4; \
101  } \
102  CONTROLLER_MODULE_API mc_control::MCController * create(const std::string &, \
103  const mc_rbdyn::RobotModulePtr & robot, \
104  const double & dt, \
105  const mc_control::Configuration &) \
106  { \
107  return new TYPE(robot, dt); \
108  } \
109  }
110 
111 #else
112 
114 
115 # define CONTROLLER_CONSTRUCTOR(NAME, TYPE) \
116  namespace \
117  { \
118  static auto registered = []() \
119  { \
120  using fn_t = std::function<TYPE *(const std::shared_ptr<mc_rbdyn::RobotModule> &, const double &, \
121  const mc_control::Configuration &)>; \
122  mc_control::ControllerLoader::loader().register_object( \
123  NAME, fn_t([](const std::shared_ptr<mc_rbdyn::RobotModule> & robot, const double & dt, \
124  const mc_control::Configuration & conf) { return new TYPE(robot, dt, conf); })); \
125  return true; \
126  }(); \
127  }
128 
129 # define MULTI_CONTROLLERS_CONSTRUCTOR(NAME0, NEWCTL0, NAME1, NEWCTL1) \
130  namespace \
131  { \
132  static auto registered = []() \
133  { \
134  using TYPE0 = decltype(NEWCTL0); \
135  using TYPE1 = decltype(NEWCTL1); \
136  using fn0_t = std::function<TYPE0 *(const std::shared_ptr<mc_rbdyn::RobotModule> &, const double &, \
137  const mc_control::Configuration &)>; \
138  mc_control::ControllerLoader::loader().register_object( \
139  NAME0, fn_t([](const std::shared_ptr<mc_rbdyn::RobotModule> & robot, const double & dt, \
140  const mc_control::Configuration & conf) { return new NEWCTL0; })); \
141  using fn1_t = std::function<TYPE1 *(const std::shared_ptr<mc_rbdyn::RobotModule> &, const double &, \
142  const mc_control::Configuration &)>; \
143  mc_control::ControllerLoader::loader().register_object( \
144  NAME1, fn_t([](const std::shared_ptr<mc_rbdyn::RobotModule> & robot, const double & dt, \
145  const mc_control::Configuration & conf) { return new NEWCTL1; })); \
146  return true; \
147  }(); \
148  }
149 
150 # define SIMPLE_CONTROLLER_CONSTRUCTOR(NAME, TYPE) \
151  namespace \
152  { \
153  static auto registered = []() \
154  { \
155  using fn_t = std::function<TYPE *(const std::shared_ptr<mc_rbdyn::RobotModule> &, const double &, \
156  const mc_control::Configuration &)>; \
157  mc_control::ControllerLoader::loader().register_object( \
158  NAME, fn_t([](const std::shared_ptr<mc_rbdyn::RobotModule> & robot, const double & dt, \
159  const mc_control::Configuration &) { return new TYPE(robot, dt); })); \
160  return true; \
161  }(); \
162  }
163 
164 #endif
MCController.h
ControllerLoader.h