ROS study notes (three): programming basics

Create feature pack

Create a workspace, and then create a function package under the src folder

~$ mkdir -p catkin_ws/src	
~$ cd catkin_ws/src
~/catkin_ws/src$ catkin_create_pkg 功能包名称 依赖包名称列表(roscpp rospy std_msgs等)

The catkin_create_pkg command generates the CMakeLists.txt file and package.xml file as well as the include directory and the src directory required by the catkin build system when creating a function package.

~/catkin_ws/src$ ls 功能包名称
CMakeLists.txt  include  package.xml  src

CMakeLists.txt文件

The CMakeLists.txt file can be used to set the creation of executable files, the priority creation of dependent packages, and the creation of linkers.

cmake_minimum_required(VERSION 2.8.3)
project(功能包名称)
find_package(catkin REQUIRED COMPONENTS
  依赖包
)
add_message_files(
  FILES	#将引用当前功能包目录下的msg目录中的指定msg文件
  custommsg.msg
)
add_service_files(
  FILES #将引用当前功能包目录下的srv目录中的指定srv文件
  customsrv.srv
)
generate_message(
  DEPENDENCIES #将DEPENDENCIES选项设置为使用std_msgs消息包
  std_msgs
)
include_directories(
  ${
    
    catkin_INCLUDE_DIRS}
)
add_executable(可执行文件名称 src/文件名.cpp)	#引用src目录中的指定文件生成可执行文件
target_link_libraries(可执行文件名称 ${
    
    catkin_LIBRARIES}) #将库和可执行文件进行链接

package.xml file

The package.xml file is a necessary ROS configuration file written in XML encoding format, including the function package name, author, license, and dependent package information.

<?xml version="1.0"?>
<package format="2">
  <name>功能包名称</name>
  <version>0.0.0</version>
  <description>The 功能包名称 package</description>
  <maintainer email="系统用户名@todo.todo">系统用户名</maintainer>
  <license>TODO</license>
  <buildtool_depend>catkin</buildtool_depend>
  <build_depend>依赖包</build_depend>
  <build_export_depend>依赖包</build_export_depend>
  <exec_depend>依赖包</exec_depend>
</package>

Simple example

1. Create a working directory and enter the src directory

~$ mkdir -p catkin_ws/src
~$ cd catkin_ws/src

2. Create a function package, enter the src directory in it, and create a c++ file

~/catkin_ws/src$ catkin_create_pkg beginner roscpp
~/catkin_ws/src$ cd beginner/src
~/catkin_ws/src/beginner/src$ vim hello.cpp

3. Write a simple program, output hello, world

#include <ros/ros.h>
#include <iostream>
using namespace std;

int main(int argc, char **argv)
{
    
    
	ros::init(argc, argv, "hello");
	cout << "hello,world" << endl;
	return 0;
}

4. Add the following statement at the end of the CMakeLists.txt file

add_executable(hello src/hello.cpp)
target_link_libraries(hello ${
    
    catkin_LIBRARIES})

5. Return to the top-level working directory and build the system

~/catkin_ws$ catkin_make

6. First, start the master node

~/catkin_ws$ roscore

Then, run the node

~/catkin_ws$ rosrun beginner hello
hello,world

If the following error occurs when running the node

~/catkin_ws$ rosrun beginner hello
[rospack] Error: package 'beginner' not found

Execute the following command to solve

~/catkin_ws$ source ./devel/setup.bash

Guess you like

Origin blog.csdn.net/qq_42386127/article/details/98624911