linux系统下创建ros工作空间和软件包开发教程

28 篇文章 4 订阅

  在linux系统下使用ros进行开发,需要首先创建并编译工作空间和软件包,并编辑自己的内容,然后写cmake文件。这里总结了使用命令行创建并编译ros工作空间和软件包的方法,更详细的内容可以参考ros官方中文教程。开发前需要首先安装并配置好ros环境,参考这里

1. 命令

  进入到需要创建工作空间的目录下,打开终端,依次执行下列命令。

mkdir catkin_ws    #创建名为catkin_ws的工作空间目录
cd catkin_ws    #进入创建好的工作空间目录
mkdir src    #在工作空间目录下创建src目录
cd src    #进入创建好的src目录
catkin_init_workspace    #初始化工作空间
catkin_create_pkg beginner_tutorials std_msgs rospy roscpp    #创建名为beginner_tutorials的软件包,它依赖于std_msgs、rospy、roscpp这三个包
cd ..    #回到catkin_ws目录
catkin_make    #编译一下这个工作空间
source devel/setup.bash    #把该工作空间加到环境变量里
cd src/beginner_tutorials/src    #进入beginner_tutorials软件包的src目录,添加编写的源文件
gedit talker.cpp    #创建并编辑talker.cpp源文件。这里使用官方示例,源码见附录,复制粘贴并保存关闭即可。来源:http://wiki.ros.org/cn/ROS/Tutorials/WritingPublisherSubscriber%28c%2B%2B%29
gedit listener.cpp    #创建并编辑listener.cpp源文件。同talker.cpp,源码见附录,复制粘贴并保存关闭即可。
cd ..    #回到beginner_tutorials目录下
gedit CMakeLists.txt    #编辑beginner_tutorials软件包的CMakeLists.txt文件。内容见附录,复制粘贴并保存关闭即可。
cd ../..    #回到catkin_ws工作空间目录
catkin_make    #重新编译一下。至此,ros的工作空间以及功能包已经都创建并编译好了

  上述编译生成的可执行文件位于catkin_ws工作空间目录下的devel/lib/beginner_tutorials目录下:
在这里插入图片描述
  要运行这里的talker和listener,首先需要再打开一个终端,运行roscore命令:

roscore    #roscore是你在运行所有ROS程序前首先要运行的命令

  保持roscore这个终端处于运行状态。然后,在工作空间目录下再打开一个终端,执行如下命令:

source devel/setup.bash    #添加环境变量,否则rosrun无法找到beginner_tutorials
rosrun beginner_tutorials talker    #运行talker

  可以看到终端源源不断输出hellow world,编号从1一直累加,即talker.cpp文件里的内容。然后,不要关闭该终端(关闭了再执行listener也不会有输出了),与运行talker一样,再打开一个新的终端,执行如下命令:

source devel/setup.bash    #添加环境变量,否则rosrun无法找到beginner_tutorials
rosrun beginner_tutorials listener    #运行listener

  可以看到终端源源不断输出I heard,即listener.cpp文件里的内容。这里,编号是从该时刻talker里输出的那个编号开始累加的,这也展示了ros的发布和订阅机制。
  

2. 附录

talker.cpp

#include "ros/ros.h"
#include "std_msgs/String.h"

#include <sstream>

/**
 * This tutorial demonstrates simple sending of messages over the ROS system.
 */
int main(int argc, char **argv)
{
  /**
   * The ros::init() function needs to see argc and argv so that it can perform
   * any ROS arguments and name remapping that were provided at the command line.
   * For programmatic remappings you can use a different version of init() which takes
   * remappings directly, but for most command-line programs, passing argc and argv is
   * the easiest way to do it.  The third argument to init() is the name of the node.
   *
   * You must call one of the versions of ros::init() before using any other
   * part of the ROS system.
   */
  ros::init(argc, argv, "talker");

  /**
   * NodeHandle is the main access point to communications with the ROS system.
   * The first NodeHandle constructed will fully initialize this node, and the last
   * NodeHandle destructed will close down the node.
   */
  ros::NodeHandle n;

  /**
   * The advertise() function is how you tell ROS that you want to
   * publish on a given topic name. This invokes a call to the ROS
   * master node, which keeps a registry of who is publishing and who
   * is subscribing. After this advertise() call is made, the master
   * node will notify anyone who is trying to subscribe to this topic name,
   * and they will in turn negotiate a peer-to-peer connection with this
   * node.  advertise() returns a Publisher object which allows you to
   * publish messages on that topic through a call to publish().  Once
   * all copies of the returned Publisher object are destroyed, the topic
   * will be automatically unadvertised.
   *
   * The second parameter to advertise() is the size of the message queue
   * used for publishing messages.  If messages are published more quickly
   * than we can send them, the number here specifies how many messages to
   * buffer up before throwing some away.
   */
  ros::Publisher chatter_pub = n.advertise<std_msgs::String>("chatter", 1000);

  ros::Rate loop_rate(10);

  /**
   * A count of how many messages we have sent. This is used to create
   * a unique string for each message.
   */
  int count = 0;
  while (ros::ok())
  {
    /**
     * This is a message object. You stuff it with data, and then publish it.
     */
    std_msgs::String msg;

    std::stringstream ss;
    ss << "hello world " << count;
    msg.data = ss.str();

    ROS_INFO("%s", msg.data.c_str());

    /**
     * The publish() function is how you send messages. The parameter
     * is the message object. The type of this object must agree with the type
     * given as a template parameter to the advertise<>() call, as was done
     * in the constructor above.
     */
    chatter_pub.publish(msg);

    ros::spinOnce();

    loop_rate.sleep();
    ++count;
  }


  return 0;
}

  
listener.cpp

#include "ros/ros.h"
#include "std_msgs/String.h"

/**
 * This tutorial demonstrates simple receipt of messages over the ROS system.
 */
void chatterCallback(const std_msgs::String::ConstPtr& msg)
{
  ROS_INFO("I heard: [%s]", msg->data.c_str());
}

int main(int argc, char **argv)
{
  /**
   * The ros::init() function needs to see argc and argv so that it can perform
   * any ROS arguments and name remapping that were provided at the command line.
   * For programmatic remappings you can use a different version of init() which takes
   * remappings directly, but for most command-line programs, passing argc and argv is
   * the easiest way to do it.  The third argument to init() is the name of the node.
   *
   * You must call one of the versions of ros::init() before using any other
   * part of the ROS system.
   */
  ros::init(argc, argv, "listener");

  /**
   * NodeHandle is the main access point to communications with the ROS system.
   * The first NodeHandle constructed will fully initialize this node, and the last
   * NodeHandle destructed will close down the node.
   */
  ros::NodeHandle n;

  /**
   * The subscribe() call is how you tell ROS that you want to receive messages
   * on a given topic.  This invokes a call to the ROS
   * master node, which keeps a registry of who is publishing and who
   * is subscribing.  Messages are passed to a callback function, here
   * called chatterCallback.  subscribe() returns a Subscriber object that you
   * must hold on to until you want to unsubscribe.  When all copies of the Subscriber
   * object go out of scope, this callback will automatically be unsubscribed from
   * this topic.
   *
   * The second parameter to the subscribe() function is the size of the message
   * queue.  If messages are arriving faster than they are being processed, this
   * is the number of messages that will be buffered up before beginning to throw
   * away the oldest ones.
   */
  ros::Subscriber sub = n.subscribe("chatter", 1000, chatterCallback);

  /**
   * ros::spin() will enter a loop, pumping callbacks.  With this version, all
   * callbacks will be called from within this thread (the main one).  ros::spin()
   * will exit when Ctrl-C is pressed, or the node is shutdown by the master.
   */
  ros::spin();

  return 0;
}

  
CMakeLists.txt

cmake_minimum_required(VERSION 3.0.2)
project(beginner_tutorials)

set(CMAKE_BUILD_TYPE debug)

## Find catkin and any catkin packages
find_package(catkin REQUIRED COMPONENTS roscpp rospy std_msgs genmsg)

## 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/talker.cpp)
target_link_libraries(talker ${catkin_LIBRARIES})
add_dependencies(talker beginner_tutorials_generate_messages_cpp)

add_executable(listener src/listener.cpp)
target_link_libraries(listener ${catkin_LIBRARIES})
add_dependencies(listener beginner_tutorials_generate_messages_cpp)
  • 3
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zeeq_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值