Install ROS kinetic under Ubuntu16.04 LST and implement Hello, World!

Written in front, please refer to the latest reference materials to study. The authoritative information is: Wikipedia maintained by the ROS open source team:

http://wiki.ros.org/cn/ROS

http://wiki.ros.org/cn/kinetic/Installation/Ubuntu

http://wiki.ros.org/cn/ROS/Tutorials/CreatingPackage

The above information is relatively authoritative, and most people on the Internet have their own systematic and one-sided understanding (including this article).

However, the information on the Internet provides us with some references, which is a good supplement to the incomplete details provided by Wikipedia. At the same time, there are also some very classic references, and some methods of solving problems can also provide information on areas that have not been explained by the official, and can be mutually confirmed and learned from.

There may be many problems, so please don’t be discouraged and check more information on the Internet, focusing on the above-mentioned websites. You can also refer to the translated version of "A General Introduction to ROS" (A General Introduction to ROS), below called ROS introduction).

1. Download and install ROS

Source setting command, open source file command:

sudo gedit /etc/apt/sources.list (Ubuntu source)
sudo gedit /etc/apt/sources.list.d/ros-latest.list (third-party software source)

1. Set the ROS source, which can be set in the following ways:
(1) ros official source:

sudo sh -c 'echo "deb http://packages.ros.org/ros/ubuntu $(lsb_release -sc) main" > /etc/apt/sources.list.d/ros-latest.list'

(2) Domestic sources from the University of Science and Technology of China:

sudo sh -c '. /etc/lsb-release && echo "deb http://mirrors.ustc.edu.cn/ros/ubuntu/ $DISTRIB_CODENAME main" > /etc/apt/sources.list.d/ros-latest.list'

If there is a problem with the above execution, use the following methods:

sudo sh -c 'echo "deb http://mirrors.ustc.edu.cn/ros/ubuntu/ $(lsb_release -sc) main" > /etc/apt/sources.list.d/ros-latest.list'

(If there is a problem with this source prompt - a reference solution https://blog.csdn.net/xiangxianghehe/article/details/78483799)

(3) In addition, there are Tsinghuayuan, etc.

First of all, check whether your Ubunutu source is normal. If it is not normal, please choose the most appropriate source.

2.Configure key:

udo apt-key adv --keyserver hkp://ha.pool.sks-keyservers.net:80 --recv-key 421C365BD9FF1F717815A3895523BAEEB01FA116

3. Obtain and install ROS:

sudo apt-get update
sudo apt-get install ros-kinetic-desktop-full
sudo apt-get install python-rosinstall

in:

sudo apt-get update (update all software sources)
sudo apt-get install ros-kinetic-desktop-full (install ros)
and sudo apt-get install python-rosinstall

Wait for the ROS installation to be completed; I downloaded it from the official source (it took a little more than 14 minutes to download).

4. Initialize and update rosdep:

sudo rosdep init

rosdep update

5. Configure the ROS environment:

echo "source /opt/ros/kinetic/setup.bash" >> ~/.bashrc
source ~/.bashrc

The above two commands write the commands to the .bashrc file in the root directory and take effect immediately. This enables bash to find the ROS configuration file normally, and you can use ROS normally in the future.

If you have multiple ROS versions installed (kinetic, etc.), ~/.bashrc must only source the setup.bash corresponding to the version you are currently using.

If you only want to change the environment variables in the current terminal, you can execute the following command: source /opt/ros/kinetic/setup.bash

6. Install the building package dependencies:

sudo apt-get install python-rosinstall python-rosinstall-generator python-wstool build-essential

7. Test whether ros is installed successfully:

roscore -h

If normal installation information is displayed, the installation is successful. Or use the following method to test:
1) Open Termial and enter the following command to initialize the ROS environment:

roscore

2) Open a new Termial, enter the following command, and a small turtle window will pop up:

rosrun turtlesim turtlesim_node

3) Open a new Termial and enter the following command. You can control the movement of the little turtle through the arrow keys in Termial (without closing the previous turtle window):

rosrun turtlesim turtle_teleop_key

4) Open a new Termial, enter the following command, and a new window will pop up to view ROS node information:

rosrun rqt_graph rqt_graph

The previous commands need to be active under the corresponding terminal to run normally (especially controlling the movement of the turtle).
You can use Ctrl+C to stop the terminal.

2. ROS implements Hello, World!

Please refer to the Chinese Wikipedia: http://wiki.ros.org/cn/ROS/Tutorials/CreatingPackage

1. Create a workspace:

$ mkdir -p ~/test_ros/src

 Some people on the Internet have given the following two steps (workspace initialization). In fact, you don’t need to do it (the system will do it by default later - when building the package):

cd ~/test_ros/src/ 
catkin_init_workspace 

The above command creates a ROS workspace named test_ros, which is actually creating a directory. This is the standard directory of the ROS workspace. The ROS workspace contains a subfolder src, which is used to save the source code to be written, etc.

2. Create ROS function package

Reference: Introduction to ROS (a)

(1) Enter the src directory under the ROS workspace:

cd  ~/test_ros/src/

(2) Create a function package in the src directory under the workspace: agitr

(The initials of "a gentle introduction to ROS". This book has a Chinese version, but some of the content in it is not for kinetic, and errors will be reported if you are not careful. If you encounter problems after referring to the content, please refer to Wikipedia By the way, most people on the Internet refer to this book, and the author also refers to it, but found some problems, so I mention them here. In fact, it is not a problem in the book, but that I did not write the programs and commands carefully. Caused.)

 catkin_create_pkg agitr

This command will create the agitr function package in the src directory of the workspace and generate two important files in the package:

The first configuration file is called package.xml; this file needs to be modified a little bit, which will be discussed later.

The second file, called CMakeLists.txt, is a Cmake script file. Cmake is an industry-standard cross-platform compilation system. This file contains
a series of compilation instructions, including what kind of executable file should be generated, which source files are needed, and where to find the required header files and link libraries. Of course, this file shows that catkin uses Cmake internally.

Note: The naming of ROS packages follows a naming convention, only lowercase letters, numbers and underscores are allowed, and the first character must be a lowercase letter.

3. At this point you can compile and see if there are any errors. Please note that there is no programming at this time, just to see if it can be compiled and compiled.

(1) Go back to the ROS workspace (it’s the workspace!) and use catkin to compile:

(You can also compile when creating the workspace, but you need to initialize the workspace at that time)

cd ~/test_ros/
catkin_make

If the compilation should pass, if it does not, it means there is a problem with ROS. Please reinstall or solve the problem according to the problem.

If the compilation passes normally, two folders will be generated under the workspace: devel and build; under the devel folder, you can see several setup.*sh files, which contain the configuration file commands of this workspace;

(2) It needs to be written into the ~/.bashrc file mentioned before:

echo "source ~/test_ros/devel/setup.bash" >> ~/.bashrc
source ~/.bashrc

In the future, the terminal can recognize this workspace, or you can use the source devel/setup.bash command, but this command is only useful for this workspace, and the command is not written into the .bashrc file; some reports say that it will appear after restarting the computer. Problems such as unrecognition.

After completing the work related to the function package (that is, after completing a series of work such as writing, debugging, and testing, the code is basically finalized), you can safely delete the devel and build subdirectories.

(3) Check whether it has been registered in bash: (it just displays that the package address is written into the environment variable)

echo $ROS_PACKAGE_PATH  

If it has been registered in the environment variable, the package address directory of the workspace created by you will appear.

4. Finally we get to the point: create hello.cpp and write the source code

(1) Go to the src directory:

cd ~/test_ros/src/

(2) Create hello.cpp, or you can create it directly without a command

touch hello.cpp

(3) Write hello.cpp source code:

//"hello.world" program for ros.
// This header defines the standard ROS classes.
#include<ros/ros.h>
 int main( int argc,char **argv){
 
// Initialize the ROS system
	ros::init(argc,argv,"hello_ros");

// Enable ish this program as a ROS node.
	ros::NodeHandle nh;

// Send some output as a log message.
	ROS_INFO_STREAM("Hello,World!");

 }

5. Modify the makefile: the content in CMakelists.txt is changed to

cmake_minimum_required(VERSION 2.8.3)
project(agitr)

find_package(catkin REQUIRED COMPONENTS
	roscpp
)

catkin_package(
#  INCLUDE_DIRS include
#  LIBRARIES agitr
#  CATKIN_DEPENDS other_catkin_pkg
#  DEPENDS system_lib
)

include_directories(
 include
 ${catkin_INCLUDE_DIRS}
)

add_executable(hello hello.cpp)

target_link_libraries(hello ${catkin_LIBRARIES})

To specify dependent libraries, edit the CMakeLists.txt file in the package directory. The default version of this file contains the following line:
find_package(catkin REQUIRED)

Other catkin packages that are dependent on can be added
after the COMPONENTS keyword in this line, as follows:
find_package(catkin REQUIRED COMPONENTS package-names)

And add two lines inside to declare the executable and its dependencies:

add_executable(hello hello.cpp)

target_link_libraries(hello ${catkin_LIBRARIES})

6. Change the contents of the manifest file package.xml to: (Special attention)

<?xml version="1.0"?>
<package format="2">
  <name>agitr</name>
  <version>0.0.0</version>
  <description>The agitr package</description>

   <maintainer email="[email protected]">liu</maintainer>

  <license>TODO</license>

  <buildtool_depend>catkin</buildtool_depend>

  <build_depend>roscpp</build_depend>
  <!-- <run_depend>roscpp</run_depend> -->
  <!-- This is an error, the follow is ture.-->
  <exec_depend>roscpp</exec_depend>

  <!-- The export tag contains other, unspecified, tags -->
  <export>
    <!-- Other tools can request additional information be placed here -->

  </export>
</package>

We also need to list the dependent libraries in the package's manifest file, which is
achieved by using the two keywords build_depend (compilation dependency) and run_depend (run dependency):
<build_depend>package-name</build_depend>
<run_depend>package-name< /run_depend>

Note that the run_depend element can be changed to the exec_depend element (refer to Wikipedia). Since run_depend is used in the ROS introduction book mentioned above, it is given here. When the author runs it for the first time, a ros/ros.h error will be reported (not ros/ros.h, no spaces), mainly because the code is copied during use and there are space errors. Here we refer to the latest Wikipedia, which uses exec_depend. The above xml is available in actual testing and is correct. If a ros/ros.h error is reported, the reason is usually that the dependencies are not set correctly.

6. Compile the workspace, as before:

cd ~/test_ros/
catkin_make

If everything is normal up to this point, then 90% is completed.

If there are compilation errors, you will see them while performing this step. After correcting them, you can rerun catkin_make to complete the compilation.

If you see an error from catkin_make that the ros/ros.h header file cannot be found, or an error that ROS functions such as ros::init are not defined, the most likely reason is that
your CMakeLists.txt does not correctly declare a dependency on roscpp.

Originally, we needed to execute the setup.bash script, but since it was written into the .bashrc file earlier, there is no need to execute it. It is worth mentioning that this automatically generated script file sets several environment variables so that ROS can find the created function package and the newly generated executable file. Unless the directory structure has changed, you only need to execute this command once in each terminal, even if you modify the code and perform a recompile with catkin_make. If something feels wrong, the code to execute is:

source devel/setup.bash (go to the workspace first)

7. Execute hello program

(1) Execute in a new terminal:

roscore

(2) Open another terminal and run rosrun agitr hello

rosrun agitr hello

(3) The following message will appear, indicating that the execution was successful and that you are done:

[ INFO] [1546072659.372736726]: Connected to master at [localhost:11311]
[ INFO] [1546072659.375831122]: Hello,World!

Note: Don't forget to start roscore first: this program is a node, and the node requires a node manager to run properly. By the way,
the numbers in this line of output represent the time, in seconds, since January 1, 1970, which is when ROS_INFO_STREAM began executing.

This rosrun command, as well as some other ROS commands, may produce errors like the following:
[rospack] Error: stack/package package-name not found

Two possible causes of this error are:
(1) Misspelling the package name;
(2) Failure to successfully run setup.bash in your workspace.

If any questions arise, please refer to the reference materials earlier in the blog post as they are more authoritative. In addition, you can search for other people's solutions. There will always be a solution, please be patient and find a solution (o^o), and persevere, persevere, persevere. . .

The above are some of the author's insights and understandings during practice. Since it is the first time to write such a long blog post, and the time to learn ROS is short, it is inevitable that mistakes may be made. If there are any mistakes, please forgive me and correct me. At the same time, I would like to complain about the CSDN editor, which always has problems, inexplicable jumps and annoying interface display functions. If you think it is of reference value, I hope you will like it and save it. It is my honor to be able to help you.

Guess you like

Origin blog.csdn.net/liuxhCSDN/article/details/85335580