ros 开发要点(初级实现topic,service,message,launch)

来源:互联网 发布:淘宝电子产品退货规定 编辑:程序博客网 时间:2024/06/06 00:56


ROS,机器人操作系统,花了一天时间实现了几个小功能:topic的订阅预发布,service的创建与请求,message的使用,launch的创建一次启动多个节点。下面是主要几个需要注意的地方,实现文档主要参考官方wiki:http://wiki.ros.org/cn/ROS/Tutorials。 



1, 环境


new ros workspace must execute "souce devel/setup.sh" to add $ROS_PACKAGE_PATH 
eg : /home/osboxes/catkin_ws/src:/opt/ros/kinetic/share


or ros filesystem level cannot find package in this new workspace !


2, 编译


compile package hello_world:


osboxes@osboxes:~/catkin_ws$ rosed hello_world CMakeLists.txt 


add (just for talker node):


include_directories(include ${catkin_INCLUDE_DIRS})


add_executable(talker src/talker.cpp)
target_link_libraries(talker ${catkin_LIBRARIES})
add_dependencies(talker hello_world_generate_messages_cpp)


3,创建一个launch


creat a launch !!!


osboxes@osboxes:~/catkin_ws$ roslaunch hello_world hellolaunch.launch


<launch>
    <node pkg="hello_world" type="talker" name="talker">
    </node>
    <node pkg="hello_world" type="listener" name="listener">
    </node>
</launch>


4,在节点中创建一个service



creat a service in a node !!!


osboxes@osboxes:~/catkin_ws$ rossrv package hello_world 
hello_world/hello_srv
osboxes@osboxes:~/catkin_ws$ rossrv show hello_world/hello_srv 
int32 A
int32 B
int32 C
---
int32 sum
//////code start


bool add(hello_world::hello_srv::Request &req,hello_world::hello_srv::Response &res)
{
  res.sum = req.A +req.B + req.C ;
  ROS_INFO("sending back response:[%ld]",(int)res.sum);
  return true ;


}
ros::ServiceServer service = n.advertiseService("add_3_ints",add);
////code end


osboxes@osboxes:~/catkin_ws$ rosservice list
/add_3_ints
osboxes@osboxes:~/catkin_ws$ rosservice type /add_3_ints 
hello_world/hello_srv
osboxes@osboxes:~/catkin_ws$ rosservice call /add_3_ints 1 1 1
sum: 3


5.在创建一个message



create a msg in a node !!!


osboxes@osboxes:~/catkin_ws$ rosmsg show hello_world/hello 
int32 A
int32 B
int32 C




////code start talker.cpp


ros::Publisher chatter_pub = n.advertise<hello_world::hello>("chatter", 1000);


////code end


////code start  lisener.cpp


void chatterCallback(const hello_world::hello::ConstPtr& msg)
{
  ROS_INFO("I heard: [%d] [%d] [%d]", msg->A, msg->B, msg->C);
}


ros::Subscriber sub = n.subscribe("chatter", 1000, chatterCallback);


////code end


6,节点中创建一个话题和订阅这个话题



///code start
ros::Publisher chatter_pub = n.advertise<hello_world::hello>("chatter", 1000);


ros::Subscriber sub = n.subscribe("chatter", 1000, chatterCallback);
///code end


7,调试

rosrun rqt_graph rqt_graph 

rosrun rqt_console rqt_console 



原创粉丝点击