NASA Astrobee Robot Software  0.19.1
Flight software for the Astrobee robots operating inside the International Space Station.
config_server.h
Go to the documentation of this file.
1 /* Copyright (c) 2017, United States Government, as represented by the
2  * Administrator of the National Aeronautics and Space Administration.
3  *
4  * All rights reserved.
5  *
6  * The Astrobee platform is licensed under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with the
8  * License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15  * License for the specific language governing permissions and limitations
16  * under the License.
17  */
18 
19 #ifndef FF_UTIL_CONFIG_SERVER_H_
20 #define FF_UTIL_CONFIG_SERVER_H_
21 
22 #include <ros/ros.h>
23 
24 #include <ff_common/ff_names.h>
25 
27 
28 #include <dynamic_reconfigure/Reconfigure.h>
29 #include <dynamic_reconfigure/Config.h>
30 #include <dynamic_reconfigure/ConfigDescription.h>
31 
32 #include <diagnostic_msgs/KeyValue.h>
33 
34 #include <climits>
35 #include <cfloat>
36 #include <string>
37 #include <utility>
38 #include <vector>
39 #include <map>
40 
41 namespace ff_util {
42 
43 typedef boost::function<bool(dynamic_reconfigure::Config &request)> ConfigCallback;
44 
45 class ConfigServer {
46  public:
47  // Constructor opens the ASTROBEE_CONFIG_DIR/subsystem/node.config file by default
48  ConfigServer();
49  virtual ~ConfigServer();
50  // Initialize the system
51  void Initialize(ros::NodeHandle *private_nh, const std::string &fname);
52  // Manually set the path to the config file
53  void SetPath(const std::string &pname);
54  // Add another (optional) LUA config file to the configuration
55  void AddFile(const std::string &fname);
56  // Start listening for updates
57  bool Listen();
58  bool Listen(ConfigCallback f);
59  // Get and Set after initialization
60  template<typename T> bool Get(const std::string &name, T &value); // Get error-aware
61  template<typename T> bool Set(const std::string &name, const T &value); // Set
62  template<typename T> T Get(const std::string &name) {
63  T tmp;
64  if (!Get(name, tmp)) {
65  ROS_ERROR_STREAM("Cannot query parameter " << name);
66  }
67  return tmp;
68  }
69  // Lim the values of a given type a runtime (helps prevent code from getting out of sync)
70  template<typename T> bool Lim(const std::string &name, const std::map<T, std::string> &vals);
71  // Dump all parameters into a KeyValue message
72  std::vector<diagnostic_msgs::KeyValue> Dump();
73  // Get the ConfigReader pointer for advanced usage
75  // Default reconfigure callback
76  bool Reconfigure(dynamic_reconfigure::Config &request);
77 
78  private:
79  // Internal function to send dynamic_reconfigure updates
80  dynamic_reconfigure::ConfigDescription UpdateDescription();
81  dynamic_reconfigure::Config UpdateValues();
82  // Assemble an edit method
83  std::string EditMethod(const std::map<std::string, std::string> &values,
84  const std::string &ctype, const std::string &cpptype, const std::string &cconsttype);
85  // Internal callback for configuration
86  bool ReconfigureImpl(dynamic_reconfigure::Reconfigure::Request &req, dynamic_reconfigure::Reconfigure::Response &res);
87  // Update function performs the paramater update
88  static bool Update(const std::string &ns, const dynamic_reconfigure::ReconfigureRequest &request);
89  bool initialized_;
90  bool listening_;
91  ros::NodeHandle nh_;
92  ros::Publisher pub_d_;
93  ros::Publisher pub_u_;
94  ros::ServiceServer srv_r_;
95  config_reader::ConfigReader config_params_;
96  ConfigCallback callback_;
97  std::map<std::string, bool> bools_;
98  std::map<std::string, int> ints_;
99  std::map<std::string, double> doubles_;
100  std::map<std::string, std::string> strs_;
101  dynamic_reconfigure::ConfigDescription description_;
102 };
103 
104 template<> bool ConfigServer::Get<int>(const std::string &name, int &value);
105 template<> bool ConfigServer::Get<double>(const std::string &name, double &value);
106 template<> bool ConfigServer::Get<bool>(const std::string &name, bool &value);
107 template<> bool ConfigServer::Get<std::string>(const std::string &name, std::string &value);
108 
109 template<> bool ConfigServer::Set<int>(const std::string &name, const int &value);
110 template<> bool ConfigServer::Set<double>(const std::string &name, const double &value);
111 template<> bool ConfigServer::Set<bool>(const std::string &name, const bool &value);
112 template<> bool ConfigServer::Set<std::string>(const std::string &name, const std::string &value);
113 
114 template<> bool ConfigServer::Lim<int>(const std::string &name, const std::map<int, std::string> &vals);
115 template<> bool ConfigServer::Lim<double>(const std::string &name, const std::map<double, std::string> &vals);
116 template<> bool ConfigServer::Lim<bool>(const std::string &name, const std::map<bool, std::string> &vals);
117 template<> bool ConfigServer::Lim<std::string>(const std::string &name, const std::map<std::string, std::string> &vals);
118 
119 
120 } // namespace ff_util
121 
122 #endif // FF_UTIL_CONFIG_SERVER_H_
ff_util::ConfigServer::Reconfigure
bool Reconfigure(dynamic_reconfigure::Config &request)
Definition: config_server.cc:623
ff_util::ConfigServer::AddFile
void AddFile(const std::string &fname)
Definition: config_server.cc:79
ff_util::ConfigCallback
boost::function< bool(dynamic_reconfigure::Config &request)> ConfigCallback
Definition: config_server.h:43
ff_util::ConfigServer
Definition: config_server.h:45
ff_util::ConfigServer::GetConfigReader
config_reader::ConfigReader * GetConfigReader()
Definition: config_server.cc:357
ff_util::ConfigServer::~ConfigServer
virtual ~ConfigServer()
Definition: config_server.cc:57
name
std::string name
Definition: eps_simulator.cc:48
ff_util::ConfigServer::Lim
bool Lim(const std::string &name, const std::map< T, std::string > &vals)
Definition: config_server.cc:425
ff_names.h
ff_util::ConfigServer::ConfigServer
ConfigServer()
Definition: config_server.cc:36
ff_util::ConfigServer::Set
bool Set(const std::string &name, const T &value)
Definition: config_server.cc:419
config_reader::ConfigReader
Definition: config_reader.h:48
ff_util::ConfigServer::Initialize
void Initialize(ros::NodeHandle *private_nh, const std::string &fname)
Definition: config_server.cc:61
ff_util::ConfigServer::Listen
bool Listen()
Definition: config_server.cc:86
config_reader.h
ff_util::ConfigServer::SetPath
void SetPath(const std::string &pname)
Definition: config_server.cc:72
ff_util
Definition: config_client.h:31
ff_util::ConfigServer::Get
T Get(const std::string &name)
Definition: config_server.h:62
ff_util::ConfigServer::Dump
std::vector< diagnostic_msgs::KeyValue > Dump()
Definition: config_server.cc:595
ff_util::ConfigServer::Get
bool Get(const std::string &name, T &value)
Definition: config_server.cc:413