capture_settings_controller.cpp
Go to the documentation of this file.
2 
3 #include "SettingsConfigUtils.h"
4 #include "SettingsAcquisitionConfigUtils.h"
5 #include "Settings2DConfigUtils.h"
6 #include "Settings2DAcquisitionConfigUtils.h"
7 
8 #include <Zivid/Camera.h>
9 #include <Zivid/Experimental/SettingsInfo.h>
10 
11 #include <dynamic_reconfigure/server.h>
12 
13 namespace zivid_camera
14 {
15 template <typename ConfigType, typename ZividSettings>
16 class ConfigDRServer
17 {
18 public:
19  ConfigDRServer(const std::string& name, ros::NodeHandle& nh, const Zivid::Camera& camera)
20  : name_(name), dr_server_(dr_server_mutex_, ros::NodeHandle(nh, name_)), config_(ConfigType::__getDefault__())
21  {
22  static_assert(std::is_same_v<ZividSettings, Zivid::Settings> || std::is_same_v<ZividSettings, Zivid::Settings2D> ||
23  std::is_same_v<ZividSettings, Zivid::Settings::Acquisition> ||
24  std::is_same_v<ZividSettings, Zivid::Settings2D::Acquisition>);
25 
26  const auto config_min = zividSettingsMinConfig<ConfigType, ZividSettings>(camera);
27  dr_server_.setConfigMin(config_min);
28 
29  const auto config_max = zividSettingsMaxConfig<ConfigType, ZividSettings>(camera);
30  dr_server_.setConfigMax(config_max);
31 
32  const auto config_default = zividSettingsToConfig<ConfigType>(
33  Zivid::Experimental::SettingsInfo::defaultValue<ZividSettings>(camera.info()));
34  dr_server_.setConfigDefault(config_default);
35  setConfig(config_default);
36 
37  auto cb = [this](const ConfigType& config, uint32_t /*level*/) {
38  ROS_INFO("Configuration '%s' changed", name_.c_str());
39  config_ = config;
40  };
41  using CallbackType = typename decltype(dr_server_)::CallbackType;
42  dr_server_.setCallback(CallbackType(cb));
43  }
44 
45  void setConfig(const ConfigType& cfg)
46  {
47  config_ = cfg;
48  dr_server_.updateConfig(config_);
49  }
50 
51  const ConfigType& config() const
52  {
53  return config_;
54  }
55 
56  const std::string& name() const
57  {
58  return name_;
59  }
60 
61 private:
62  std::string name_;
63  boost::recursive_mutex dr_server_mutex_;
64  dynamic_reconfigure::Server<ConfigType> dr_server_;
65  ConfigType config_;
66 };
67 
68 template <typename ZividSettingsType, typename SettingsConfigType, typename SettingsAcquisitionConfigType>
69 CaptureSettingsController<ZividSettingsType, SettingsConfigType,
70  SettingsAcquisitionConfigType>::CaptureSettingsController(ros::NodeHandle& nh,
71  Zivid::Camera& camera,
72  const std::string& config_node_name,
73  std::size_t num_acquisition_servers)
74  : config_node_name_(config_node_name)
75 {
76  general_config_dr_server_ = std::make_unique<SettingsConfigTypeDRServer>(config_node_name_, nh, camera);
77 
78  ROS_INFO("Setting up %ld %s/acquisition_<n> dynamic_reconfigure servers", num_acquisition_servers,
79  config_node_name_.c_str());
80  for (std::size_t i = 0; i < num_acquisition_servers; i++)
81  {
82  acquisition_config_dr_servers_.push_back(std::make_unique<SettingsAcquisitionConfigTypeDRServer>(
83  config_node_name_ + "/acquisition_" + std::to_string(i), nh, camera));
84  }
85 }
86 
87 template <typename ZividSettingsType, typename SettingsConfigType, typename SettingsAcquisitionConfigType>
88 CaptureSettingsController<ZividSettingsType, SettingsConfigType,
89  SettingsAcquisitionConfigType>::~CaptureSettingsController() = default;
90 
91 template <typename ZividSettingsType, typename SettingsConfigType, typename SettingsAcquisitionConfigType>
92 ZividSettingsType
94 {
95  ZividSettingsType settings;
96  applyConfigToZividSettings(general_config_dr_server_->config(), settings);
97 
98  for (const auto& dr_config_server : acquisition_config_dr_servers_)
99  {
100  if (dr_config_server->config().enabled)
101  {
102  ROS_DEBUG("Config %s is enabled", dr_config_server->name().c_str());
103  typename ZividSettingsType::Acquisition acquisition;
104  applyConfigToZividSettings(dr_config_server->config(), acquisition);
105  settings.acquisitions().emplaceBack(std::move(acquisition));
106  }
107  }
108  return settings;
109 }
110 
111 template <typename ZividSettingsType, typename SettingsConfigType, typename SettingsAcquisitionConfigType>
113  const ZividSettingsType& settings)
114 {
115  const auto& acquisitions = settings.acquisitions();
116 
117  if (acquisitions.size() > numAcquisitionConfigServers())
118  {
119  std::stringstream error;
120  error << "The number of acquisitions (" + std::to_string(acquisitions.size()) + ") "
121  << "is larger than the number of dynamic_reconfigure " + config_node_name_ + "/acquisition_<n> servers ("
122  << std::to_string(numAcquisitionConfigServers()) << "). ";
123  if constexpr (std::is_same_v<ZividSettingsType, Zivid::Settings>)
124  {
125  error << "Increase launch parameter max_capture_acquisitions. "
126  << "See README.md for more information.";
127  }
128  else if constexpr (std::is_same_v<ZividSettingsType, Zivid::Settings2D>)
129  {
130  error << "In 2D mode only a single acquisiton is supported.";
131  }
132  throw std::runtime_error(error.str());
133  }
134 
135  ROS_DEBUG_STREAM("Updating settings to " << settings);
136  general_config_dr_server_->setConfig(zividSettingsToConfig<SettingsConfigType>(settings));
137 
138  for (std::size_t i = 0; i < acquisitions.size(); i++)
139  {
140  auto config = zividSettingsToConfig<SettingsAcquisitionConfigType>(acquisitions.at(i));
141  config.enabled = true;
142  acquisition_config_dr_servers_[i]->setConfig(config);
143  }
144 
145  // Remaining acquisitions that are enabled must be disabled
146  for (std::size_t i = acquisitions.size(); i < acquisition_config_dr_servers_.size(); i++)
147  {
148  if (acquisition_config_dr_servers_[i]->config().enabled)
149  {
150  ROS_INFO_STREAM("Acquisition " << i << " was enabled, so disabling it");
151  auto config = acquisition_config_dr_servers_[i]->config();
152  config.enabled = false;
153  acquisition_config_dr_servers_[i]->setConfig(config);
154  }
155  }
156 }
157 
158 template <typename ZividSettingsType, typename SettingsConfigType, typename SettingsAcquisitionConfigType>
159 std::size_t CaptureSettingsController<ZividSettingsType, SettingsConfigType,
160  SettingsAcquisitionConfigType>::numAcquisitionConfigServers() const
161 {
162  return acquisition_config_dr_servers_.size();
163 }
164 
165 template class CaptureSettingsController<Zivid::Settings, zivid_camera::SettingsConfig,
166  zivid_camera::SettingsAcquisitionConfig>;
167 template class CaptureSettingsController<Zivid::Settings2D, zivid_camera::Settings2DConfig,
168  zivid_camera::Settings2DAcquisitionConfig>;
169 
170 } // namespace zivid_camera
void setZividSettings(const ZividSettingsType &settings)
std::unique_ptr< SettingsConfigTypeDRServer > general_config_dr_server_
#define ROS_INFO(...)
ConfigDRServer(const std::string &name, ros::NodeHandle &nh, const Zivid::Camera &camera)
#define ROS_DEBUG_STREAM(args)
std::vector< std::unique_ptr< SettingsAcquisitionConfigTypeDRServer > > acquisition_config_dr_servers_
Controller that manages dynamic_reconfigure nodes for Settings and Settings2D
#define ROS_INFO_STREAM(args)
void setConfig(const ConfigType &cfg)
dynamic_reconfigure::Server< ConfigType > dr_server_
#define ROS_DEBUG(...)


zivid_camera
Author(s): Zivid
autogenerated on Sat Apr 17 2021 02:51:05