Skip to content

Commit 3d35760

Browse files
committed
rtt_rosparam: Adding ability to get and set params with different names than their associated RTT properties.
1 parent 222ea93 commit 3d35760

File tree

2 files changed

+115
-55
lines changed

2 files changed

+115
-55
lines changed

rtt_rosparam/include/rtt_rosparam/rosparam.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,13 @@ namespace rtt_rosparam {
2323
setAllComponentPrivate("setAllComponentPrivate"),
2424
setAll("setAll"),
2525
get("get"),
26+
getParam("getParam"),
2627
getRelative("getRelative"),
2728
getAbsolute("getAbsolute"),
2829
getPrivate("getPrivate"),
2930
getComponentPrivate("getComponentPrivate"),
3031
set("set"),
32+
setParam("setParam"),
3133
setRelative("setRelative"),
3234
setAbsolute("setAbsolute"),
3335
setPrivate("setPrivate"),
@@ -46,12 +48,14 @@ namespace rtt_rosparam {
4648
this->addOperationCaller(setAll);
4749

4850
this->addOperationCaller(get);
51+
this->addOperationCaller(getParam);
4952
this->addOperationCaller(getRelative);
5053
this->addOperationCaller(getAbsolute);
5154
this->addOperationCaller(getPrivate);
5255
this->addOperationCaller(getComponentPrivate);
5356

5457
this->addOperationCaller(set);
58+
this->addOperationCaller(setParam);
5559
this->addOperationCaller(setRelative);
5660
this->addOperationCaller(setAbsolute);
5761
this->addOperationCaller(setPrivate);
@@ -77,12 +81,14 @@ namespace rtt_rosparam {
7781
RTT::OperationCaller<bool(void)> setAll;
7882

7983
RTT::OperationCaller<bool(const std::string &, const unsigned int)> get;
84+
RTT::OperationCaller<bool(const std::string &, const std::string &)> getParam;
8085
RTT::OperationCaller<bool(const std::string &)> getRelative;
8186
RTT::OperationCaller<bool(const std::string &)> getAbsolute;
8287
RTT::OperationCaller<bool(const std::string &)> getPrivate;
8388
RTT::OperationCaller<bool(const std::string &)> getComponentPrivate;
8489

8590
RTT::OperationCaller<bool(const std::string &, const unsigned int)> set;
91+
RTT::OperationCaller<bool(const std::string &, const std::string &)> setParam;
8692
RTT::OperationCaller<bool(const std::string &)> setRelative;
8793
RTT::OperationCaller<bool(const std::string &)> setAbsolute;
8894
RTT::OperationCaller<bool(const std::string &)> setPrivate;

rtt_rosparam/src/rtt_rosparam_service.cpp

Lines changed: 109 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,14 @@ class ROSParamService: public RTT::Service
5555
this->addOperation("setAll", &ROSParamService::setParamsComponentPrivate, this)
5656
.doc("Stores all properties of this component (and its sub-services) on the ROS param server from the similarly-named property in the component's private namespace. This is an alias for setAllComponentPrivate().");
5757

58-
this->addOperation("get", &ROSParamService::getParam, this)
58+
this->addOperation("get", &ROSParamService::get, this)
5959
.doc("Gets one property of this component (or populates the properties of a named RTT sub-service) from the ROS param server based on the given resolution policy.")
6060
.arg("name", "Name of the property / service / parameter.")
6161
.arg("policy", "ROS parameter namespace resolution policy.");
62+
this->addOperation("getParam", &ROSParamService::getParam, this)
63+
.doc("Gets one property of this component (or populates the properties of a named RTT sub-service) from the ROS param server based on the given ROS parameter name.")
64+
.arg("param_name", "Name of the ROS parameter. Use '~' and '/' leaders for private or absolute resolution.")
65+
.arg("name", "Name of the RTT property or service.");
6266

6367
this->addOperation("getRelative", &ROSParamService::getParamRelative, this)
6468
.doc("Gets one property of this component (or populates the properties of a named RTT sub-service) from the ROS param server in the relative namespace.")
@@ -73,10 +77,14 @@ class ROSParamService: public RTT::Service
7377
.doc("Gets one property of this component (or populates the properties of a named RTT sub-service) from the ROS param server in the component's private namespace.")
7478
.arg("name", "Name of the property / service / parameter.");
7579

76-
this->addOperation("set", &ROSParamService::setParam, this)
80+
this->addOperation("set", &ROSParamService::set, this)
7781
.doc("Sets one parameter on the ROS param server from the similarly-named property of this component (or stores the properties of a named RTT sub-service) in the ROS parameter namespace based on the given resolution policy.")
7882
.arg("name", "Name of the property / service / parameter.")
7983
.arg("policy", "ROS parameter namespace resolution policy.");
84+
this->addOperation("setParam", &ROSParamService::setParam, this)
85+
.doc("Sets one parameter on the ROS param server from the similarly-named property of this component (or stores the properties of a named RTT sub-service) in the ROS parameter namespace based on the given ROS parameter name.")
86+
.arg("param_name", "Name of the ROS parameter. Use '~' and '/' leaders for private or absolute resolution.")
87+
.arg("name", "Name of the RTT property or service.");
8088

8189
this->addOperation("setRelative", &ROSParamService::setParamRelative, this)
8290
.doc("Sets one parameter on the ROS param server from the similarly-named property of this component (or stores the properties of a named RTT sub-service) in the relative namespace.")
@@ -99,55 +107,72 @@ class ROSParamService: public RTT::Service
99107
const std::string &param_name,
100108
const ROSParamService::ResolutionPolicy policy);
101109

102-
bool getParams(RTT::Service::shared_ptr service, const std::string& ns, const ROSParamService::ResolutionPolicy policy);
110+
bool getParams(RTT::Service::shared_ptr service, const std::string& ns);
103111
bool getParams(const ROSParamService::ResolutionPolicy policy);
104112
bool getParamsRelative() { return getParams(RELATIVE); }
105113
bool getParamsAbsolute() { return getParams(ABSOLUTE); }
106114
bool getParamsPrivate() { return getParams(PRIVATE); }
107115
bool getParamsComponentPrivate() { return getParams(COMPONENT); }
108116

109-
bool getParam(
117+
bool get(
110118
const std::string &param_name,
111119
const unsigned int policy = (unsigned int)ROSParamService::COMPONENT);
112-
bool getParamRelative(const std::string &name) { return getParam(name, RELATIVE); }
113-
bool getParamAbsolute(const std::string &name) { return getParam(name, ABSOLUTE); }
114-
bool getParamPrivate(const std::string &name) { return getParam(name, PRIVATE); }
115-
bool getParamComponentPrivate(const std::string &name) { return getParam(name, COMPONENT); }
116-
117-
bool setParams(RTT::Service::shared_ptr service, const std::string& ns, const ROSParamService::ResolutionPolicy policy);
120+
bool getParam(
121+
const std::string &ros_name,
122+
const std::string &rtt_name);
123+
bool getParamRelative(const std::string &name) { return get(name, RELATIVE); }
124+
bool getParamAbsolute(const std::string &name) { return get(name, ABSOLUTE); }
125+
bool getParamPrivate(const std::string &name) { return get(name, PRIVATE); }
126+
bool getParamComponentPrivate(const std::string &name) { return get(name, COMPONENT); }
127+
128+
bool setParams(RTT::Service::shared_ptr service, const std::string& ns);
118129
bool setParams(const ROSParamService::ResolutionPolicy policy);
119130
bool setParamsRelative() { return setParams(RELATIVE); }
120131
bool setParamsAbsolute() { return setParams(ABSOLUTE); }
121132
bool setParamsPrivate() { return setParams(PRIVATE); }
122133
bool setParamsComponentPrivate() { return setParams(COMPONENT); }
123134

124-
bool setParam(
135+
bool set(
125136
const std::string &param_name,
126137
const unsigned int policy = (unsigned int)ROSParamService::COMPONENT);
127-
bool setParamRelative(const std::string &name) { return setParam(name, RELATIVE); }
128-
bool setParamAbsolute(const std::string &name) { return setParam(name, ABSOLUTE); }
129-
bool setParamPrivate(const std::string &name) { return setParam(name, PRIVATE); }
130-
bool setParamComponentPrivate(const std::string &name) { return setParam(name, COMPONENT); }
138+
bool setParam(
139+
const std::string &ros_name,
140+
const std::string &rtt_name);
141+
bool setParamRelative(const std::string &name) { return set(name, RELATIVE); }
142+
bool setParamAbsolute(const std::string &name) { return set(name, ABSOLUTE); }
143+
bool setParamPrivate(const std::string &name) { return set(name, PRIVATE); }
144+
bool setParamComponentPrivate(const std::string &name) { return set(name, COMPONENT); }
131145
};
132146

133147
const std::string ROSParamService::resolvedName(
134148
const std::string &param_name,
135149
const ROSParamService::ResolutionPolicy policy)
136150
{
137-
std::string resolved_name = param_name;
151+
std::string leader = "";
152+
std::string resolved_name = "";
153+
154+
if(param_name.length() > 0) {
155+
leader = param_name[0];
156+
}
157+
138158
switch(policy) {
139159
case ROSParamService::RELATIVE:
140-
return param_name;
160+
resolved_name = param_name;
161+
break;
141162
case ROSParamService::ABSOLUTE:
142-
return std::string("/") + param_name;
163+
resolved_name = (leader == "/") ? param_name : std::string("/") + param_name;
164+
break;
143165
case ROSParamService::PRIVATE:
144-
return std::string("~") + param_name;
166+
resolved_name = (leader == "~") ? param_name : std::string("~") + param_name;
167+
break;
145168
case ROSParamService::COMPONENT:
146-
return std::string("~") + this->getOwner()->getName() + "/" + param_name;
169+
resolved_name = std::string("~") + ros::names::append(this->getOwner()->getName(),param_name);
170+
break;
147171
};
148172

149-
// Relative by default
150-
return param_name;
173+
RTT::log(RTT::Debug) << "["<<this->getOwner()->getName()<<"] Resolving ROS param \""<<param_name<<"\" to \""<<resolved_name<<"\"" << RTT::endlog();
174+
175+
return resolved_name;
151176
}
152177

153178

@@ -308,48 +333,63 @@ XmlRpc::XmlRpcValue rttPropertyBaseToXmlParam(RTT::base::PropertyBase *prop)
308333
}
309334

310335

311-
bool ROSParamService::setParam(
336+
bool ROSParamService::set(
312337
const std::string &param_name,
313338
const unsigned int policy)
314339
{
340+
RTT::Logger::In in("ROSParamService::set");
341+
342+
const std::string resolved_name = resolvedName(param_name,ResolutionPolicy(policy));
343+
344+
return this->setParam(resolved_name, param_name);
345+
}
346+
347+
bool ROSParamService::setParam(
348+
const std::string &ros_name,
349+
const std::string &rtt_name)
350+
{
351+
RTT::Logger::In in("ROSParamService::setParam");
352+
315353
XmlRpc::XmlRpcValue xml_value;
316354

317-
// Try to find a property named param_name
318-
RTT::base::PropertyBase *property = this->getOwner()->getProperty(param_name);
355+
// Try to find a property named rtt_name
356+
RTT::base::PropertyBase *property = this->getOwner()->getProperty(rtt_name);
319357
if (property) {
320-
xml_value = rttPropertyBaseToXmlParam(this->getOwner()->getProperty(param_name));
321-
ros::param::set(resolvedName(param_name,ResolutionPolicy(policy)), xml_value);
358+
xml_value = rttPropertyBaseToXmlParam(this->getOwner()->getProperty(rtt_name));
359+
ros::param::set(ros_name, xml_value);
322360
return true;
323361
}
324362

325-
// Try to find a sub-service named param_name
326-
RTT::Service::shared_ptr service = this->getOwner()->provides()->getService(param_name);
363+
// Try to find a sub-service named rtt_name
364+
RTT::Service::shared_ptr service = this->getOwner()->provides()->getService(rtt_name);
327365
if (service) {
328366
// Set all parameters of the sub-service
329-
return setParams(service, service->getName(), ResolutionPolicy(policy));
367+
return setParams(service, ros_name);
330368
}
331369

332-
RTT::log(RTT::Debug) << "RTT component does not have a property or service named \"" << param_name << "\"" << RTT::endlog();
370+
RTT::log(RTT::Debug) << "RTT component does not have a property or service named \"" << rtt_name << "\"" << RTT::endlog();
333371
return false;
334372
}
335373

336374
bool ROSParamService::setParams(const ROSParamService::ResolutionPolicy policy)
337375
{
338-
return setParams(this->getOwner()->provides(), std::string(), policy);
376+
return setParams(this->getOwner()->provides(), resolvedName(std::string(), policy));
339377
}
340378

341-
bool ROSParamService::setParams(RTT::Service::shared_ptr service, const std::string& ns, const ROSParamService::ResolutionPolicy policy) {
379+
bool ROSParamService::setParams(
380+
RTT::Service::shared_ptr service,
381+
const std::string& ns) {
342382
XmlRpc::XmlRpcValue xml_value;
343383
xml_value = rttPropertyToXmlParam(*(service->properties()));
344-
ros::param::set(resolvedName(ns, policy), xml_value);
384+
ros::param::set(ns, xml_value);
345385

346386
// Recurse into sub-services
347387
RTT::Service::ProviderNames names = service->getProviderNames();
348388
for (RTT::Service::ProviderNames::const_iterator it = names.begin(); it != names.end(); ++it)
349389
{
350390
RTT::Service::shared_ptr sub(service->getService(*it));
351391
if (sub) {
352-
if (!setParams(sub, ns + "/" + sub->getName(), policy)) return false;
392+
if (!setParams(sub, ros::names::append(ns,sub->getName()))) return false;
353393
}
354394
}
355395

@@ -610,64 +650,78 @@ bool xmlParamToProp(
610650
return false;
611651
}
612652

613-
bool ROSParamService::getParam(
653+
bool ROSParamService::get(
614654
const std::string &param_name,
615655
const unsigned int policy)
656+
{
657+
RTT::Logger::In in("ROSParamService::get");
658+
659+
const std::string resolved_name = resolvedName(
660+
param_name,ResolutionPolicy(policy));
661+
662+
return this->getParam(resolved_name, param_name);
663+
}
664+
665+
bool ROSParamService::getParam(
666+
const std::string &ros_name,
667+
const std::string &rtt_name)
616668
{
617669
RTT::Logger::In in("ROSParamService::getParam");
618670

619671
try {
620672
// Get the parameter
621673
XmlRpc::XmlRpcValue xml_value;
622674

623-
const std::string resolved_name = resolvedName(param_name,ResolutionPolicy(policy));
624-
if(!ros::param::get(resolved_name, xml_value)) {
625-
RTT::log(RTT::Debug) << "ROS Parameter \"" << resolved_name << "\" not found on the parameter server!" << RTT::endlog();
675+
if(!ros::param::get(ros_name, xml_value)) {
676+
RTT::log(RTT::Debug) << "ROS Parameter \"" << ros_name << "\" not found on the parameter server!" << RTT::endlog();
626677
return false;
627678
}
628679

629680
// Try to get the property if it exists
630-
RTT::base::PropertyBase *prop_base = this->getOwner()->getProperty(param_name);
681+
RTT::base::PropertyBase *prop_base = this->getOwner()->getProperty(rtt_name);
631682
if(prop_base) {
632683
// Deal with the xml value
633684
bool ret = xmlParamToProp(xml_value, prop_base);
634685
if(!ret) {
635-
RTT::log(RTT::Warning) << "Could not convert \"" << resolved_name << "\" from an XMLRPC value to an RTT property." << RTT::endlog();
686+
RTT::log(RTT::Warning) << "Could not convert \"" << ros_name << "\" from an XMLRPC value to an RTT property." << RTT::endlog();
636687
}
637688
return ret;
638689
}
639690

640691
// Try to get the properties of a sub-service if it exists
641-
RTT::Service::shared_ptr service = this->getOwner()->provides()->getService(param_name);
692+
RTT::Service::shared_ptr service = this->getOwner()->provides()->getService(rtt_name);
642693
if(service) {
643694
// Get all parameters of the sub-service
644-
return getParams(service, service->getName(), ResolutionPolicy(policy));
695+
return getParams(service, ros_name);
645696
}
646697

647-
RTT::log(RTT::Debug) << "RTT component does not have a property or service named \"" << param_name << "\"" << RTT::endlog();
648-
return false;
698+
RTT::log(RTT::Debug) << "RTT component does not have a property or service named \"" << rtt_name << "\"" << RTT::endlog();
699+
649700
} catch(XmlRpc::XmlRpcException &err) {
650-
RTT::log(RTT::Error) << "XmlRpcException when getting ROS parameter \""<<param_name<<"\": " << err.getMessage() << RTT::endlog();
701+
RTT::log(RTT::Error) << "XmlRpcException when getting ROS parameter \""<<ros_name<<"\": " << err.getMessage() << RTT::endlog();
651702
RTT::log(RTT::Debug) << " -- Make sure your parameters are the right primitive type." << RTT::endlog();
652-
return false;
653703
}
704+
705+
return false;
654706
}
655707

708+
656709
bool ROSParamService::getParams(const ROSParamService::ResolutionPolicy policy)
657710
{
658-
return getParams(this->getOwner()->provides(), std::string(), policy);
711+
return getParams(this->getOwner()->provides(), resolvedName(std::string(), policy));
659712
}
660713

661-
bool ROSParamService::getParams(RTT::Service::shared_ptr service, const std::string& ns, const ROSParamService::ResolutionPolicy policy)
714+
bool ROSParamService::getParams(
715+
RTT::Service::shared_ptr service,
716+
const std::string& ns)
662717
{
663718
RTT::Logger::In in("ROSParamService::getParams");
664719

665720
// Get the parameter
666721
XmlRpc::XmlRpcValue xml_value;
667722

668-
const std::string resolved_name = resolvedName(ns, policy);
669-
if(!ros::param::get(resolved_name, xml_value)) {
670-
RTT::log(RTT::Debug) << "ROS Parameter \"" << resolved_name << "\" not found on the parameter server!" << RTT::endlog();
723+
if(!ros::param::get(ns, xml_value)) {
724+
RTT::log(RTT::Debug) << "ROS Parameter namespace \"" << ns << "\" not found on the parameter server!" << RTT::endlog();
671725
return false;
672726
}
673727

@@ -679,7 +733,7 @@ bool ROSParamService::getParams(RTT::Service::shared_ptr service, const std::str
679733
// Deal with the xml value
680734
bool ret = xmlParamToProp(xml_value, &prop);
681735
if (!ret) {
682-
RTT::log(RTT::Warning) << "Could not convert \"" << resolved_name << "\" from an XMLRPC value to an RTT property." << RTT::endlog();
736+
RTT::log(RTT::Warning) << "Could not convert \"" << ns << "\" from an XMLRPC value to an RTT property." << RTT::endlog();
683737
return false;
684738
}
685739

@@ -690,8 +744,8 @@ bool ROSParamService::getParams(RTT::Service::shared_ptr service, const std::str
690744
RTT::Service::shared_ptr sub(service->getService(*it));
691745
if (sub) {
692746
std::string sub_ns = sub->getName();
693-
if (!ns.empty()) sub_ns = ns + "/" + sub_ns;
694-
getParams(sub, sub_ns, policy);
747+
if (!ns.empty()) sub_ns = ros::names::append(ns,sub_ns);
748+
getParams(sub, sub_ns);
695749
}
696750
}
697751

0 commit comments

Comments
 (0)