赞
踩
1.1.ROS发展史
1.2.什么是ROS
1.3 ROS 安装
2.1 ROS 工程结构
3.1 ROS 通信架构(计算图级)
5.1 ROS常用工具
6.1 Client Libray(客户端库,类似于API)&&roscpp
topic_demo: 功能描述: 两个node,一个发布模拟的GPS信息(格式为自定义,包括坐标和工作状态),另一个接收并处理该信息(计算到原点的距离) 步骤: 1.package 2.msg 3.talker.cpp 4.listener.cpp 5.CMakeList.txt&package.xml 实现: 1.package $ cd ~/catkin_ws/src $ catkin_create_pkg topic_demo roscpp rospy std_msgs 2.msg $ cd topic_demo/ $ mdkir msg $ vi gps.msg gps.msg float32 x float32 y string state catkin_make ++++++> ~/catkin_ws/devel/include/topic_demo/gps.h 使用时只需 #include<topic_demo/gps.h> topic_demo::gps msg; 3. talker.cpp #include<ros/ros.h> #include<topic_demo/gps.h> int main(int argc char** argv) { // 解析参数,命名节点 ros(argc,argv,"talker") // 创建句柄,实例化node ros::NodeHandler nh; // 创建gps信息 topic_demo::gps msg; msg.x=1.0; msg.y=1.0; msg.state="working"; // 创建publisher ros::Publisher pub=nh.advertise<topic_demo::gps>("gps_info",1); // 定义循环发布的频率 ros::Rate loop_rate(1.0); while(ros::OK()){ // 以指数增长,每隔1s msg.x=1.03*msg.x; msg.y=1.01*msg.y; // 输出当前msg ROS INFO("Talker:GPS:x=%f,y=%f",msg.x,mgs.y) // 发布消息 pub.publish(msg); // 根据定义的发布频率,sleep,从而达到周期性发布消息的结果 loop_rate.sleep(); } return 0; } 4.listener.cpp #include<ros/ros.h> #include<topic_demo/gps.h> #include<std_msgs/Float332.h> void gpsCallback(const topic_demo::ggps::ConstPtr&msg) { std::msgs:Float32 distance; distacne.data=sqrt(pow(msg->x,2),pow(msg->y,2)) ROS_INFO("Listener:Distance to togin=%f,state=%s",distance.data,msg->state.c_str()); } int main(int argc,char**argv) { ros::init(argc,argv,"listener"); ros::NodeHandler n; // 创建subscriber(订阅者) ros::SubScriber sub=n.subscribe("gps_info",gpsCallback) // 反复调用当前可触发的回调函数,阻塞 // ros::spin() 函数作用: 清空当前队列 // ros::spinOnce() 非阻塞 ros::spin(); return 0; } 5.修改CMakeList.txt&package.xml 6.编译catkin_make,执行rosrun
service_demo: 功能描述: 两个node,一个发布请求(格式自定义),另一个接收处理信息,并返回信息 步骤: 1. package 2. 定义传输数据格式:srv 3. server.cpp 4. client.cpp 5. CMakeList.txt&package.xml 实现: 1.package $ cd ~/catkin_ws/src $ catkin_create_pkg service_demo roscpp rospy std_msgs 2. srv $ cd service_demo/ $ mkdir srv $ vi Greeting.srv Greeting.srv string name int32 age --- strig feedback ++++++++> ~/catkin_ws/devel/include/service_demo/Greeting.h .../GreetingRequest.h .../GreetingResponse.h 3.server.cpp #include<ros/ros.h> #include<service_demo/Greeting.h> bool handler_function(service_demo::Greeting::Request &req,service::demo::Greeting::Response &res) { // 显示请求信息 ROS_INFO("Request from %s with age %d",req.name.c_str(),req.age); // 处理请求,写入response res.feedback="Hi"+req.name+"I am server!"; // 返回true,正确处理了请求 return true; } int main(int argc,char**argv) { // 解析参数,命名节点 ros::init(argc,argv,"greeting_server"); // 创建句柄,实例化节点 ros::NodeHandler nh; ros::ServiceServer service=nh.advertiseService("greetings",handler_function); ros::spin(); return 0; } 4.client.cpp #include<ros/ros.h> #include<service_demo/Greeting.h> int main(int argc,char**argv) { // 解析参数,命名节点 ros::init(argc,argv,"greetings_server"); // 创建节点,实例化节点 ros::NodeHandler nh; ros::ServiceCLient client=nh.serviceClient<service_demo::Greeting>("greetings"); service::demo::Greeting srv; service.request.name="Wu"; service.request.age="20"; if(client.call(srv)){ ROS_INFO("Feedback from server: %s",srv.response.feedback); }else{ ROS_ERROR("Failed to call service greetings."); return 1; } return 0; } 5.修改CMakeList.txt&package.xml 6.编译catkin_make,执行rosrun
param_demo: 同一功能的两种API: ros::param和ros::NodeHandler 实现: #include<ros/ros.h> int main(int argc,char **argv) { ros::init(argc,argv,"greeting_server"); ros::NodeHandler nh; int parameter1,parameter2,parameter3,parameter4,parameter5; // 获取参数 ros::param::get("param1",parameter1); nh.getParam("param2",parameter2); // 这里123是默认值 nh.param("param3",parameter3,123) // 设置参数 ros::parameter::set("param4",parameter4); nh.setParam("param",parameter5); // 检查参数是否存在 ros::param::has("param5"); nh.hasParam("param5"); // 删除参数 ros::param::del("param5"); // 删除参数 ros::param::del("param5"); nh.deleteParam("param6"); return 0; }
7.1 Client Libray&&rospy
topic_demo: 功能描述: 两个node,一个发布模拟的GPS信息(格式为自定义,包括坐标和工作状态),另一个接收并处理该信息(计算到原点的距离) 步骤: 1.package 2.msg 3.talker.py 4.listener.py 5.CMakeList.txt&package.xml 实现: 1.package $ cd ~/catkin_ws/src $ catkin_create_pkg topic_demo roscpp rospy std_msgs 2.msg $ cd topic_demo/ $ mdkir msg $ vi gps.msg gps.msg float32 x float32 y string state catkin_make ++++++> ~/catkin_ws/devel/include/topic_demo/msg/_init_.py topic_demo::gps msg; 3. talker.py #!/usr/bin/env python import rospy from topic_demo.msg import msg def talker(): pub=rospy.Publisher('gps_info',gps,queue_size=10) ros.init_node('pytalker',anonymous=True) rate=rospy.Rate(1) x=1.0 y=2.0 state='Working' while not rospy is_shutdown(): ros.loginfo('Ta;ker:GPS:x=%,y=%f) x=1.03*x y=1.01*y rate.sleep() if __name__=='__main__': talker() 4.listener.py #!/usr/bin/env python import rospy import math from topic_demo.msg import gps def callback(gps): distance=math.sqrt(math.pow(gps.x,2),math.pow(gps.y,2)) def listener(): ros.init_node('pylistener') rospy.Subscriber('gps_info',gps,callback) rospy.spin() if __name__=='__main__': listener() 5.修改CMakeList.txt&package.xml 6.编译catkin_make,执行rosrun
service_demo: 功能描述: 两个node,一个发布请求(格式自定义),另一个接收处理信息,并返回信息 步骤: 1. package 2. 定义传输数据格式:srv 3. server.py 4. client.py 5. CMakeList.txt&package.xml 实现: 1.package $ cd ~/catkin_ws/src $ catkin_create_pkg service_demo roscpp rospy std_msgs 2. srv $ cd service_demo/ $ mkdir srv $ vi Greeting.srv Greeting.srv string name int32 age --- strig feedback ++++++++> ~/catkin_ws/devel/lib/python2.7/dis-packages/service_demo/srv/__init__.py 3.server.py #!/usr/bin/env python import rospy from service_demo.srv import * def server_srv(): rospy.init_node('greeting_server') # 定义程序的server端 s=rospy.Service('greetings',Greeting,handle_function) rospy.info('Ready to handle the request:') rospy.spin() def handle_function(req): rospy.loginfo('Request from',req.name,'with age',req.age) return GreetingResponse('Hi %s I'm server!,%server.name) if __name__='__main__': server_srv() 4.client.py import rospy from service_demo.srv import * def client_srv: rospy.init_node('greeting_client') rospy.wait_for_service('greetings') try: greetings_client=rospy.ServiceProxy('greetings',Greeting) rosp=greetings_client('HAN',20) rospy.loginfo('Message From Server:%s',%rosp.feedback) except rospy.ServiceException,e: rospy.logwarn('Service call failed:%s'%e) if __name__=='__main__': client_srv() 5.修改CMakeList.txt&package.xml 6.编译catkin_make,执行rosrun
8.1.ROS之TF&URDF
9.1.SLAM与Map
10.1 Navaigation-NavigationStack (路径规划,导航)
(源于mooc 中科院软件所video note) 学习总结,欢迎r读者批评指正!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。