古月居 ROS入门21讲 第十六讲 参数的使用与编程方法
#include<ros/ros.h>
#include<std_srvs/Empty.h>
int main(int argc,char**argv)
{
int red,blue,green;
ros::init(argc,argv,"parameter_config");
ros::NodeHandle node;
ros::param::get("/turtlesim/background_r",red);
ros::param::get("/turtlesim/background_g",green);
ros::param::get("/turtlesim/background_b",blue);
ROS_INFO("Get background color[%d,%d,%d]",red,green,blue);
ros::param::set("/turtlesim/background_r",255);
ros::param::set("/turtlesim/background_g",255);
ros::param::set("/turtlesim/background_b",255);
ROS_INFO("Set background color[255,255,255]");
ros::param::get("/turtlesim/background_r",red);
ros::param::get("/turtlesim/background_g",green);
ros::param::get("/turtlesim/background_b",blue);
ROS_INFO("Re_get background color[%d,%d,%d]",red,green,blue);
ros::service::waitForService("/clear");
ros::ServiceClient client=node.serviceClient<std_srvs::Empty>("/clear");
std_srvs::Empty srv;
client.call(srv);
sleep(1);
return 0;
}
import sys
import rospy
from std_srvs.srv import Empty
def parameter_config():
rospy.init_node('parameter_config',anonymous=True)
red=rospy.get_param('/turtlesim/background_r')
green=rospy.get_param('/turtlesim/background_g')
blue=rospy.get_param('/turtlesim/background_b')
rospy.loginfo("Get Background color[%d,%d,%d]",red,green,blue)
red=rospy.set_param('/turtlesim/background_r',100)
green=rospy.set_param('/turtlesim/background_g',100)
blue=rospy.set_param('/turtlesim/background_b',100)
rospy.loginfo("Set Background color[100,100,100]")
red=rospy.get_param('/turtlesim/background_r')
green=rospy.get_param('/turtlesim/background_g')
blue=rospy.get_param('/turtlesim/background_b')
rospy.loginfo("Re-get Background color[%d,%d,%d]",red,green,blue)
rospy.wait_for_service("/clear")
try:
client=rospy.ServiceProxy('/clear',Empty)
response=client()
return response
except rospy.ServiceException,e:
print "Service call failed:%s"%e
if __name__=="__main__":
parameter_config()