Create a ROS Workspace
Let's create a catkin workspace:
$ mkdir -p ~/catkin_ws/src
$ cd ~/catkin_ws/src
$ catkin_init_workspace
Even though the workspace is empty (there are no packages in the 'src' folder, just a singleCMakeLists.txt link) you can still "build" the workspace:
$ cd ~/catkin_ws/
$ catkin_make
The catkin_make command is a convenience tool for working with catkin workspaces. If you look in your current directory you should now have a 'build' and 'devel' folder. Inside the 'devel' folder you can see that there are now several setup.*sh files. Sourcing any of these files will overlay this workspace on top of your environment. To understand more about this see the general catkin documentation:catkin. Before continuing source your new setup.*sh file:
$ source devel/setup.bash
To make sure your workspace is properly overlayed by the setup script, make sureROS_PACKAGE_PATH environment variable includes the directory you're in.
$ echo $ROS_PACKAGE_PATH
/home/youruser/catkin_ws/src:/opt/ros/indigo/share:/opt/ros/indigo/stacks
Now that your environment is setup
Creating a catkin Package
This tutorial will demonstrate how to use the catkin_create_pkg script to create a new catkin package, and what you can do with it after it has been created.
First change to the source space directory of the catkin workspace you created in theCreating a Workspace for catkin tutorial:
# You should have created this in the Creating a Workspace Tutorial
$ cd ~/catkin_ws/src
Now use the catkin_create_pkg script to create a new package called 'beginner_tutorials' which depends on std_msgs, roscpp, and rospy:
$ catkin_create_pkg beginner_tutorials std_msgs rospy roscpp
This will create a beginner_tutorials folder which contains apackage.xml and aCMakeLists.txt, which have been partially filled out with the information you gavecatkin_create_pkg.
catkin_create_pkg requires that you give it apackage_name and optionally a list of dependencies on which that package depends:
# This is an example, do not try to run this # catkin_create_pkg <package_name> [depend1] [depend2] [depend3]
catkin_create_pkalities which is described incatkin/commands/catkin_create_pkg.
Building a catkin workspace and sourcing the setup file
Now you need to build the packages in the catkin workspace:
$ cd ~/catkin_ws
$ catkin_make
After the workspace has been built it has created a similar structure in thedevel subfolder as you usually find under/opt/ros/$ROSDISTRO_NAME.
To add the workspace to your ROS environment you need to source the generated setup file:
$ . ~/catkin_ws/devel/setup.bash
创建hello_world.cpp
cd ~/catkin_ws/src/beginner_tutorials/srcgedit hello_world.cpp
代码如下:
#include <ros/ros.h>
int main(int argc, char** argv)
{
ros::init(argc, argv, "hello_meta_package_node");
ros::NodeHandle nh;
ROS_INFO("Hello ROS!");
ros::spin();
}
CMakeLists.txt加入
add_executable(hello_world src/hello_world.cpp)
target_link_libraries(hello_world ${catkin_LIBRARIES} )
编译cpp即节点
cd ~/catkin_ws
catkin_make
运行
rosrun beginner_tutorials hello_world
PS:若出现错误[rospack] Error: stack/package beginner_tutorials not found
类似错误都可以这样解决
source devel/setup.bash
或者
$ cd ~
$gedit .bashrc
再后面加入两行
source /opt/ros/hydro/setup.bash
source /工作空间/devel/setup.bash使用launch文件
$ roscd beginner_tutorials
$ mkdir launch
$ cd launch
$gedit hello_world.launchhello_world.launch内容:
<launch>
<node pkg="beginner_tutorials" name="hello_world" type="hello_world">
</node>
</launch>
roslaunch beginner_tutorials hello_world.launch
为了看到信息,我们要打开 rqt_console
$ rqt_console
$roslaunch beginner_tutorials hello_world.launch
参考:官网
http://blog.csdn.net/yaked/article/details/50083783
http://blog.csdn.net/sfe1012/article/details/42344041