module_dyn_reconf.h 11.6 KB
Newer Older
1
2
3
4
5
6
#ifndef _MODULE_DYN_RECONF_H
#define _MODULE_DYN_RECONF_H

#include <dynamic_reconfigure/Reconfigure.h>
#include <iri_ros_tools/module_service.h>

7
8
9
10
11
12
13
/**
  * \brief Dynamic reconfigure client wrapper
  *
  * This class inherits from the generic CModuleService class to implement 
  * the specifics of the dynamic reconfigure service. It ptovides functions
  * to set integer, boolean, float and string parameters of remote ROS nodes.
  */
14
15
16
class CModuleDynReconf : protected CModuleService<dynamic_reconfigure::Reconfigure>
{
  private:
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
    /**
      * \brief Name of the parameter to change
      *
      * This attribute stores the name of the parameter that is being changed
      * when the service is called. It is then used by the check function to
      * make sure the value has been changed successfully.
      */
    std::string param_name;
    /**
      * \brief Boolean value to change
      *
      * This attribute stores the value of the parameter to be modified when 
      * the service is called. It is then used by the check function to make
      * sure the value has been changed correctly.
      */
32
    bool bool_value;
33
34
35
36
37
38
39
    /**
      * \brief Integer value to change
      *
      * This attribute stores the value of the parameter to be modified when 
      * the service is called. It is then used by the check function to make
      * sure the value has been changed correctly.
      */
40
    int int_value;
41
42
43
44
45
46
47
    /**
      * \brief String value to change
      *
      * This attribute stores the value of the parameter to be modified when 
      * the service is called. It is then used by the check function to make
      * sure the value has been changed correctly.
      */
48
    std::string string_value;
49
50
51
52
53
54
55
    /**
      * \brief Double value to change
      *
      * This attribute stores the value of the parameter to be modified when 
      * the service is called. It is then used by the check function to make
      * sure the value has been changed correctly.
      */
56
57
    double double_value;
  protected:
58
59
60
61
62
63
64
65
66
67
68
69
70
    /**
      * \brief Service data check callback
      *
      * This callback function is called when the service response is received
      * to check whether the parameter has been changed successfully or not.
      *
      * \param msg Dynamic reconfigure message with the request and response
      *            data.
      *
      * \return A boolean indicating whether the parameter has been changed
      *         successfully (true) or not (false)
      *            
      */
71
72
    bool check_dyn_reconf(dynamic_reconfigure::Reconfigure &msg);
  public:
73
74
75
76
    /**
      * \brief Constructor
      *
      */
77
    CModuleDynReconf(const std::string &name,const std::string &name_space=std::string(""));
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
    /**
      * \brief Sets a parameter of type boolean
      *
      * This functions sets a parameter of type boolean. It calls the dynamic 
      * reconfigure server with the provided data and waits for an answer. If
      * the data has not been changed, this function will report an error, as 
      * well as if the service call itself fails or is pending.
      * 
      * \param name String with the name of the parameter to change. This must
      *             coincide with a parameter name on the associated dynamic
      *             reconfigure server.
      *
      * \param value A boolean with the new value for the parameter.
      *
      * \return A boolean indicating whether the parameter has been successfully
      *         changed (true) or not (false)
      */
95
    bool set_parameter(const std::string &name,bool value);
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
    /**
      * \brief Sets a parameter of type integer
      *
      * This functions sets a parameter of type integer. It calls the dynamic 
      * reconfigure server with the provided data and waits for an answer. If
      * the data has not been changed, this function will report an error, as 
      * well as if the service call itself fails or is pending.
      * 
      * \param name String with the name of the parameter to change. This must
      *             coincide with a parameter name on the associated dynamic
      *             reconfigure server.
      *
      * \param value An integer with the new value for the parameter. This value
      *              must be inside the valid value range of the parameter.
      *
      * \return A boolean indicating whether the parameter has been successfully
      *         changed (true) or not (false)
      */
114
    bool set_parameter(const std::string &name,int value);
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
    /**
      * \brief Sets a parameter of type string
      *
      * This functions sets a parameter of type string. It calls the dynamic 
      * reconfigure server with the provided data and waits for an answer. If
      * the data has not been changed, this function will report an error, as 
      * well as if the service call itself fails or is pending.
      * 
      * \param name String with the name of the parameter to change. This must
      *             coincide with a parameter name on the associated dynamic
      *             reconfigure server.
      *
      * \param value A string with the new value for the parameter. This value
      *              must be inside the valid value range of the parameter.
      *
      * \return A boolean indicating whether the parameter has been successfully
      *         changed (true) or not (false)
      */
133
    bool set_parameter(const std::string &name,std::string &value);
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
    /**
      * \brief Sets a parameter of type double
      *
      * This functions sets a parameter of type double. It calls the dynamic 
      * reconfigure server with the provided data and waits for an answer. If
      * the data has not been changed, this function will report an error, as 
      * well as if the service call itself fails or is pending.
      * 
      * \param name String with the name of the parameter to change. This must
      *             coincide with a parameter name on the associated dynamic
      *             reconfigure server.
      *
      * \param value A double with the new value for the parameter. This value
      *              must be inside the valid value range of the parameter.
      *
      * \return A boolean indicating whether the parameter has been successfully
      *         changed (true) or not (false)
      */
152
    bool set_parameter(const std::string &name,double value);
153
154
155
156
    /**
      * \brief Destructor
      *
      */
157
158
159
160
161
162
163
164
165
166
167
168
169
170
    ~CModuleDynReconf();
};

CModuleDynReconf::CModuleDynReconf(const std::string &name,const std::string &name_space) : CModuleService(name,name_space)
{
  this->set_max_num_retries(1);
  this->set_call_check_function(boost::bind(&CModuleDynReconf::check_dyn_reconf,this,_1));
}
bool CModuleDynReconf::check_dyn_reconf(dynamic_reconfigure::Reconfigure &msg)
{
  unsigned int i=0;

  for(i=0;i<msg.response.config.bools.size();i++)
  {
171
    if(msg.response.config.bools[i].name==this->param_name)
172
173
174
175
176
177
178
179
180
    {
      if(msg.response.config.bools[i].value!=this->bool_value)
      {
        return false;
      }
    }
  }
  for(i=0;i<msg.response.config.ints.size();i++)
  {
181
    if(msg.response.config.ints[i].name==this->param_name)
182
183
184
185
186
187
188
189
190
    {
      if(msg.response.config.ints[i].value!=this->int_value)
      {
        return false;
      }
    }
  }
  for(i=0;i<msg.response.config.strs.size();i++)
  {
191
    if(msg.response.config.strs[i].name==this->param_name)
192
193
194
195
196
197
198
199
200
    {
      if(msg.response.config.strs[i].value!=this->string_value)
      {
        return false;
      }
    }
  }
  for(i=0;i<msg.response.config.doubles.size();i++)
  {
201
    if(msg.response.config.doubles[i].name==this->param_name)
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
    {
      if(msg.response.config.doubles[i].value!=this->double_value)
      {
        return false;
      }
    }
  }

  return true;
}

bool CModuleDynReconf::set_parameter(const std::string &name,bool value)
{
  dynamic_reconfigure::Reconfigure set_params_srv;

  ROS_DEBUG_STREAM("CModuleDynReconf::set_parameter: Sending new request on service " << this->get_name());
  ROS_DEBUG_STREAM("CModuleDynReconf::set boolean parameter " << name << " to " << value);

  set_params_srv.request.config.bools.resize(1);
  set_params_srv.request.config.bools[0].name=name;
  set_params_srv.request.config.bools[0].value=value;
223
  this->param_name=name;
224
225
226
227
228
229
230
231
232
233
  this->bool_value=value;
  switch(this->call(set_params_srv))
  {
    case ACT_SRV_SUCCESS: ROS_DEBUG_STREAM("CModule::set_parameters: Request successfull on server: " << this->get_name()); 
                          return true;
                          break;
    case ACT_SRV_PENDING:
    case ACT_SRV_FAIL: ROS_ERROR_STREAM("CModuleDynReconf::set_parameters: Request failed on server: " << this->get_name());
                       return false;
                       break;
234
    default: return false;
235
236
237
238
239
240
241
242
243
244
245
246
247
  }
}

bool CModuleDynReconf::set_parameter(const std::string &name,int value)
{
  dynamic_reconfigure::Reconfigure set_params_srv;

  ROS_DEBUG_STREAM("CModuleDynReconf::set_parameter: Sending new request on service " << this->get_name());
  ROS_DEBUG_STREAM("CModule::set integer parameter " << name << " to " << value);

  set_params_srv.request.config.ints.resize(1);
  set_params_srv.request.config.ints[0].name=name;
  set_params_srv.request.config.ints[0].value=value;
248
  this->param_name=name;
249
250
251
252
253
254
255
256
257
258
  this->int_value=value;
  switch(this->call(set_params_srv))
  {
    case ACT_SRV_SUCCESS: ROS_DEBUG_STREAM("CModule::set_parameters: Request successfull on server: " << this->get_name()); 
                          return true;
                          break;
    case ACT_SRV_PENDING:
    case ACT_SRV_FAIL: ROS_ERROR_STREAM("CModuleDynReconf::set_parameters: Request failed on server: " << this->get_name());
                       return false;
                       break;
259
    default: return false;
260
261
262
263
264
265
266
267
268
269
270
271
272
  }
}

bool CModuleDynReconf::set_parameter(const std::string &name,std::string &value)
{
  dynamic_reconfigure::Reconfigure set_params_srv;

  ROS_DEBUG_STREAM("CModuleDynReconf::set_parameter: Sending new request on service " << this->get_name());
  ROS_DEBUG_STREAM("CModuleDynReconf::set string parameter " << name << " to " << value);

  set_params_srv.request.config.strs.resize(1);
  set_params_srv.request.config.strs[0].name=name;
  set_params_srv.request.config.strs[0].value=value;
273
  this->param_name=name;
274
275
276
277
278
279
280
281
282
283
  this->string_value=value;
  switch(this->call(set_params_srv))
  {
    case ACT_SRV_SUCCESS: ROS_DEBUG_STREAM("CModule::set_parameters: Request successfull on server: " << this->get_name()); 
                          return true;
                          break;
    case ACT_SRV_PENDING:
    case ACT_SRV_FAIL: ROS_ERROR_STREAM("CModuleDynReconf::set_parameters: Request failed on server: " << this->get_name());
                       return false;
                       break;
284
    default: return false;
285
286
287
288
289
290
291
292
293
294
295
296
297
  }
}

bool CModuleDynReconf::set_parameter(const std::string &name,double value)
{
  dynamic_reconfigure::Reconfigure set_params_srv;

  ROS_DEBUG_STREAM("CModule::set_parameter: Sending new request on service " << this->get_name());
  ROS_DEBUG_STREAM("CModule::set double parameter " << name << " to " << value);

  set_params_srv.request.config.doubles.resize(1);
  set_params_srv.request.config.doubles[0].name=name;
  set_params_srv.request.config.doubles[0].value=value;
298
  this->param_name=name;
299
300
301
302
303
304
305
306
307
308
  this->double_value=value;
  switch(this->call(set_params_srv))
  {
    case ACT_SRV_SUCCESS: ROS_DEBUG_STREAM("CModule::set_parameters: Request successfull on server: " << this->get_name()); 
                          return true;
                          break;
    case ACT_SRV_PENDING:
    case ACT_SRV_FAIL: ROS_ERROR_STREAM("CModuleDynReconf::set_parameters: Request failed on server: " << this->get_name());
                       return false;
                       break;
309
    default: return false;
310
311
312
313
314
315
316
317
318
  }
}

CModuleDynReconf::~CModuleDynReconf()
{

}

#endif