Write the first Ros (Create Workspace workspace and Feature Pack package)

Source: Internet
Author: User
Tags naming convention

Just contact Ros, learn to write the first program, afraid of forgetting later, the steps to record it down.

First you must make sure that your computer is installed with Ros configured.

1. Create a workspace (workspace)

The package we created is supposed to be all in one called working space (workspace) in the directory. You can store the directory anywhere in your account. For example, the workspace I created is path/home, and you can name your workspace with any name you like, my workspace is named Test, and now use the standard mkdir command line to create a workspace. I first set up a workspace, named Test,

The workspace created here also needs to create a subdirectory in the workspace directory called SRC. This subdirectory will be used to store the source code for the Feature Pack. and "-P" in the Code, the mkdir command automatically creates all directories that do not exist.

Type in the Terminal command (the shortcut key for opening the terminal is "ctrl+alt+t")

Mkdir-p ~/TEST/SRC

After running, you will find that you have a SRC subdirectory in your workspace named Test. This way your workspace setup is complete.

2. Create a Feature Pack (package)

The command to create a new Ros Feature Pack should run in the SRC directory in your workspace: catkin_create_pkg package-name,package-name refers to the name of your Feature Pack, the Feature Pack I created is named Amin, Note that you are running in the SRC directory in your workspace (the CD command can go to the directory you want to enter) and continue typing in the terminal

CD ~/TEST/SRC

Go to the SRC directory to run

Catkin_create_pkg Amin

In fact, this feature Pack creation command didn't do much work, it just created a directory to hold the feature pack (that is, the feature Pack Amin I created) and generated two configuration files in the Amin directory.
The first configuration file, called Package.xml, is called a manifest file.

The second file, called CMakeLists.txt, is a Cmake script file, and Cmake is a cross-platform compilation system that complies with industry standards. This file contains a series of compilation instructions, including which executable file should be generated, which source files are needed, and where to find the desired header and link libraries. Of course, this document indicates that Catkin used the Cmake internally.

Note: The name of the ROS package follows a naming convention that allows only lowercase letters, numbers, and underscores, and the first character must be a lowercase letter. Some Ros tools, including Catkin, do not support packages that do not follow this naming convention.

You can not look at this: after you edit the manifest file to create the package, you may want to edit its package.xml file, which contains some metadata that describes the package. The feature Pack created through CATKIN_CREATE_PKG contains a rich set of annotations that are largely self-explanatory to the reader. Note, however, that most of the information in the ROS is not used at compile time or at run time, and this information becomes important only when you publish your code publicly. In the spirit of keeping the document in sync with the actual function, it may be reasonable to fill in at least two parts of description and maintainer.

3. Writing Ros Programs

I'm writing a simple hello,ros!. The program, named Hello.cpp. The source file named Hello.cpp is also stored in your Feature Pack Amin folder, next to Package.xml and CMakeLists.txt.

Note: Some online tutorials suggest creating a src directory in your Feature Pack directory to save put the C + + source file. This additional organizational structure may be useful, especially for larger feature packs that contain multiple types of files, but it is not strictly necessary.

#include <ros/ros.h>intintChar * * argv)  {"Hello "  ) ;  Ros::nodehandle NH;   " " ) ;}
4. Compiling the Hello program

How do we compile and run this program? These are given to the Ros Catkin compilation system to handle.

First step: declaring a dependent library

first, we need to declare other feature packs that the program depends on. For C + + programs, this step is necessary to ensure that Catkin can provide the C + + compiler with the appropriate markup to locate the header files and link libraries needed to compile the feature pack. in order to give a dependent library,

Edit the CMakeLists.txt file under the package directory.

this article the default version of the piece contains the following line: find_package (Catkin REQUIRED)

< EM id= "__mcedel" > other Catkin packages on which you can add to the components of this line < EM id= "__mcedel", as follows: find _package (Catkin REQUIRED components package-names)

for the Hello routine, we need to add a dependent library named Roscpp, which C + + client library for ROS. Therefore, the modified Find_package line is as follows:

Find_package (Catkin REQUIRED components roscpp)

We also need to list the dependent libraries in the package manifest file Package.xml, by using the build_depend (compile-dependent) and run_depend (run-dependent) Two keyword implementations: The format is as follows:
<build_depend>package-name</build_depend>
<run_depend>package-name</run_depend>
The Hello program requires a roscpp library at compile time and run time, so the manifest file Package.xml joins:

<build_depend>roscpp</build_depend><run_depend>roscpp</run_depend>

Package.xml Open With Gedit

Note: However, the dependent libraries declared in the manifest file are not used in the compilation process; If you ignore them here, you may not see any error messages until you release your package to someone else, and they may be able to compile your published package without installing the required packages, causing an error.

Step two: Declare the executable file

Next, we need to add two lines in CMakeLists.txt to declare the executable file we need to create.

The general form is:add_executable (executable-name source-files) here Executable-name is the name of the executing file (hello), so Urce-files is the name of the source file (hello.cpp)

target_link_libraries (Executable-name ${catkin_libraries})


The first line declares the file name of the executable file that we want, and the list of source files required to generate the executable file. If you have multiple source files, list them here and separate them with a space.

The second line tells Cmake which libraries to link to when linking this executable (as defined in the find_package above). If your package includes more than one executable file, copy and modify the two lines of code for each executable file.

In my routine, we need an executable named Hello, which is compiled from a source file named Hello.cpp. So we need to add the following lines of code to CMakeLists.txt:

Add_executable (Hello hello.cpp) target_link_libraries (Hello ${catkin_libraries})

Step three: Compile the workspace

compile with the Catkin_make command, and Note that you must run from your workspace directory when this command is entered into your workspace directory.

CD ~/test

catkin_make This command it will complete some configuration steps (especially when you run this command for the first time) and create a devel and build two subdirectories in your workspace. These two new directories are used to store and compile related files, such as automatically generated compilation scripts, target code, and executables. If you like, you can safely delete devel and build two subdirectories after you have finished working with the feature Pack (the Translator notes: After a series of work, such as writing, debugging, testing, and so on, the code is basically trained).

Catkin_make

After running, you have more devel and build two subdirectories in your workspace directory. To run out of results.


Of course, it's possible if you see a compilation error from Catkin_make, a hint that you can't find the ros/ros.h header file, or an error that is not defined by the ROS function such as Ros::init, you'll see when you perform this step See them. The biggest possibility is that your CMakeLists.txt does not correctly declare a reliance on roscpp.

in correcting it later, you can run the Catkin_make again to complete the compilation work.

5. Execute the Hello program

The first thing to start Roscore: This program is a node, the node needs a node manager to be able to run normally. To start the Node Manager command:

Roscore

After you start the node Manager, execute the script file named Setup.bash , which is generated catkin_make in the devel subdirectory of your workspace.

This automatically generated script file sets a number of environment variables so that ROS can Locate the feature pack you created and the newly generated executable file. (i.e. registering the program)

Note: In addition to The non-directory structure changes, otherwise you only need to execute this command at each terminal once, Even if you modify the code and perform a recompile with Catkin_make.

Note: This command must be run in your workspace directory, and the command to enter your workspace directory has been mentioned above "CD ~/test " , or the "No file or directory" will appear

SOURCE Devel/setup.bash

The last step, run the node, with the command: Use the format "Rosrun package-name executable-name" Package-name as the Feature Pack name, Executable-name is the executable file name

Rosrun Amin Hello

Run results

Such a simple Ros program is done.

Of course, there are official tutorials on the internet, a bit different, I think this is very useful, but also successfully run. Here is a simplified version of the official tutorial, it is necessary to look at: http://blog.csdn.net/yake827/article/details/44564057

Write the first Ros (Create Workspace workspace and Feature Pack package)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.