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:
[email protected]:~/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 !!!
osboxe[email protected]:~/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 !!!
[email protected]:~/catkin_ws$ rossrv package hello_world
hello_world/hello_srv
[email protected]:~/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
[email protected]:~/catkin_ws$ rosservice list
/add_3_ints
[email protected]:~/catkin_ws$ rosservice type /add_3_ints
hello_world/hello_srv
[email protected]:~/catkin_ws$ rosservice call /add_3_ints 1 1 1
sum: 3
5.在建立一個message
create a msg in a node !!!
[email protected]:~/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