0 开始之前
确保你已经安装了ROS (Robot Operating System)。
1 第一步: 创建一个ROS包
在开始编程前,我们首先创建一个新的ROS包(package)。移动到你的catkin workspace的 'src' 文件夹下,然后运行以下命令:
cd ~/catkin_ws/src
catkin_create_pkg my_package std_msgs rospy roscpp
其中 my_package
是你的包名, std_msgs rospy roscpp
是该包依赖的包。
第二步: 创建节点源代码文件
接下来,在你的包中创建一个新的C++ 源文件:catkin_ws/src/my_package/src
pub.cpp
#include <ros/ros.h>
#include <std_msgs/String.h>int main(int argc, char **argv)
{ros::init(argc, argv, "talker");ros::NodeHandle nh;ros::Publisher chatter_pub = nh.advertise<std_msgs::String>("chatter", 1000);ros::Rate loop_rate(10);int count = 0;while (ros::ok()){std_msgs::String msg;std::stringstream ss;ss << "send test msg " << count;msg.data = ss.str();ROS_INFO("%s", msg.data.c_str());chatter_pub.publish(msg);ros::spinOnce();loop_rate.sleep();++count;}return 0;
}
sub.cpp
#include <ros/ros.h>
#include <std_msgs/String.h>void chatterCallback(const std_msgs::String::ConstPtr& msg)
{ROS_INFO("subscribe receive : [%s]", msg->data.c_str());
}int main(int argc, char **argv)
{ros::init(argc, argv, "listener");ros::NodeHandle nh;ros::Subscriber sub = nh.subscribe("chatter", 1000, chatterCallback);ros::spin();return 0;
}
第三步: 编辑CMakeLists.txt和package.xml
在你的 my_package
目录下,编辑 CMakeLists.txt
和 package.xml
文件,增加你需要的依赖和构建规则。
package.xml
<?xml version="1.0"?>
<package format="2"><name>my_package</name><version>0.0.0</version><description>The my_package package</description><!-- One maintainer tag required, multiple allowed, one person per tag --><!-- Example: --><!-- <maintainer email="jane.doe@example.com">Jane Doe</maintainer> --><maintainer email="renjianhong@todo.todo">renjianhong</maintainer><!-- One license tag required, multiple allowed, one license per tag --><!-- Commonly used license strings: --><!-- BSD, MIT, Boost Software License, GPLv2, GPLv3, LGPLv2.1, LGPLv3 --><license>TODO</license><!-- Url tags are optional, but multiple are allowed, one per tag --><!-- Optional attribute type can be: website, bugtracker, or repository --><!-- Example: --><!-- <url type="website">http://wiki.ros.org/my_package</url> --><!-- Author tags are optional, multiple are allowed, one per tag --><!-- Authors do not have to be maintainers, but could be --><!-- Example: --><!-- <author email="jane.doe@example.com">Jane Doe</author> --><!-- The *depend tags are used to specify dependencies --><!-- Dependencies can be catkin packages or system dependencies --><!-- Examples: --><!-- Use depend as a shortcut for packages that are both build and exec dependencies --><!-- <depend>roscpp</depend> --><!-- Note that this is equivalent to the following: --><!-- <build_depend>roscpp</build_depend> --><!-- <exec_depend>roscpp</exec_depend> --><!-- Use build_depend for packages you need at compile time: --><!-- <build_depend>message_generation</build_depend> --><!-- Use build_export_depend for packages you need in order to build against this package: --><!-- <build_export_depend>message_generation</build_export_depend> --><!-- Use buildtool_depend for build tool packages: --><!-- <buildtool_depend>catkin</buildtool_depend> --><!-- Use exec_depend for packages you need at runtime: --><!-- <exec_depend>message_runtime</exec_depend> --><!-- Use test_depend for packages you need only for testing: --><!-- <test_depend>gtest</test_depend> --><!-- Use doc_depend for packages you need only for building documentation: --><!-- <doc_depend>doxygen</doc_depend> --><buildtool_depend>catkin</buildtool_depend><build_depend>roscpp</build_depend><build_depend>rospy</build_depend><build_depend>std_msgs</build_depend><build_export_depend>roscpp</build_export_depend><build_export_depend>rospy</build_export_depend><build_export_depend>std_msgs</build_export_depend><exec_depend>roscpp</exec_depend><exec_depend>rospy</exec_depend><exec_depend>std_msgs</exec_depend><!-- The export tag contains other, unspecified, tags --><export><!-- Other tools can request additional information be placed here --></export>
</package>
CMakeLists.txt
cmake_minimum_required(VERSION 2.8.3)
project(my_package)## Find catkin and any catkin packages
find_package(catkin REQUIRED COMPONENTS roscpp rospy std_msgs genmsg)## Declare ROS messages and services
# add_message_files(DIRECTORY msg FILES Num.msg)
# add_service_files(DIRECTORY srv FILES AddTwoInts.srv)## Generate added messages and services
generate_messages(DEPENDENCIES std_msgs)## Declare a catkin package
catkin_package()## Build talker and listener
include_directories(include ${catkin_INCLUDE_DIRS})add_executable(talker src/pub.cpp)
target_link_libraries(talker ${catkin_LIBRARIES})
add_dependencies(talker my_ros_package_generate_messages_cpp)add_executable(listener src/sub.cpp)
target_link_libraries(listener ${catkin_LIBRARIES})
add_dependencies(listener my_ros_package_generate_messages_cpp)
第四步: 编译和运行你的节点
最后,返回到你的 catkin workspace,然后使用 catkin_make
命令编译你的包:
cd ~/catkin_ws catkin_make
如果编译成功,你可以通过运行以下命令来启动你的节点:
记住,要在运行任何ROS命令前,先运行 source devel/setup.bash
来设置你的环境变量。
以上就是如何创建一个基本的ROS节点的步骤。