ROS_hydro_catkin创建工作空间~helloworld.cpp_rosrun/roslaunch

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/src
gedit 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.launch

hello_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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值