ROS nanny-level tutorial ROS basic operation 1

Table of contents

Note: Preparation before using ROS

(1) Before using ROS, you need to configure the Linux environment in advance

1. Method 1: Use a virtual machine to install the ubuntu system

2. Method 2: Install Linux dual system

(2) Install ROS

(3) Plug-ins recommended for installation and operations to be used after installing ROS

1. Install Terminator

2. Install VScode

3. Solve the problem that the Ubuntu virtual machine cannot copy each other inside and outside

1. Create a workspace

(1) Create a workspace and generate a src folder under the workspace

(2) Enter the workspace

(3) Compile workspace

2. Create a function package under the workspace

(1) Enter the function package

(2) Import the dependencies of the function package

3. Write the code under the function package

(1), cpp version

1. Create and write the cpp file in the src directory of the function package

2. Edit the Cmakelist.txt file of the function package

3. Enter the workspace directory and compile

4. Execute the code

(2), python version

1. Enter the ros function package to add the scripts directory and edit the python file

2. Add executable permissions to python files

3. Edit the CamkeList.txt file under the ros package

4. Enter the workspace directory and compile

5. Execute code


Note: Preparation before using ROS

(1) Before using ROS, you need to configure the Linux environment in advance

1. Method 1: Use a virtual machine to install the ubuntu system

(1)、VMbox

(2)、vmware

The above two kinds of virtual machine software can be selected. The genuine version of vmware needs to be paid, and VMbox is free.

The download of vmware can refer to:

https://blog.csdn.net/weixin_52799373/article/details/124324077

The download of VMbox can refer to:

https://blog.csdn.net/l1015649309/article/details/124519596

2. Method 2: Install Linux dual system

The installation of dual system can refer to:

https://blog.csdn.net/hwh295/article/details/113409389

(2) Install ROS

Run the following commands line by line in the terminal:

sudo sh -c '. /etc/lsb-release && echo "deb http://mirrors.tuna.tsinghua.edu.cn/ros/ubuntu/ `lsb_release -cs` main" > /etc/apt/sources.list.d/ros-latest.list'
sudo apt-key adv --keyserver 'hkp://keyserver.ubuntu.com:80' --recv-key C1CF6E31E6BADE8868B172B4F42ED6FBAB17C654
sudo apt update
sudo apt install ros-noetic-desktop-full

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

Test after installation:

Terminal 1:

roscore

Terminal 2:

rosrun turtlesim turtlesim_node
rosrun turtlesim turtle_teleop_key

If you can use the keyboard to control the movement of the little turtle, it means that the installation has been successful

(3) Plug-ins recommended for installation and operations to be used after installing ROS

After installing ros in Ubuntu, we need to build a ros integrated development environment to facilitate related operations and download VScode and Terminator software.

1. Install Terminator

Terminator installation command:

sudo apt install terminator

2. Install VScode

(1), VScode download address: " Documentation for Visual Studio Code " After downloading, double-click to install.

(2) After VScode is installed, you need to configure tasks.json to compile ROS. After creating the function package, press the shortcut key ctrl + shift + B to invoke compilation, select: "catkin_make:build", click the gear on the right, and modify .vscode/tasks The .json file is as follows

{
// 有关 tasks.json 格式的文档,请参见
    // https://go.microsoft.com/fwlink/?LinkId=733558
    "version": "2.0.0",
    "tasks": [
        {
            "label": "catkin_make:debug", //代表提示的描述性信息
            "type": "shell",  //可以选择shell或者process,如果是shell代码是在shell里面运行一个命令,如果是process代表作为一个进程来运行
            "command": "catkin_make",//这个是我们需要运行的命令
            "args": [],//如果需要在命令后面加一些后缀,可以写在这里,比如-DCATKIN_WHITELIST_PACKAGES=“pac1;pac2”
            "group": {"kind":"build","isDefault":true},
            "presentation": {
                "reveal": "always"//可选always或者silence,代表是否输出信息
            },
            "problemMatcher": "$msCompile"
        }
    ]
}

3. Solve the problem that the Ubuntu virtual machine cannot copy each other inside and outside

sudo apt-get autoremove open-vm-tools
sudo apt-get install open-vm-tools-desktop

1. Create a workspace

(1) Create a workspace and generate a src folder under the workspace

mkdir -p 工作空间名/src

(2) Enter the workspace

cd 工作空间名

(3) Compile workspace

catkin_make

2. Create a function package under the workspace

Note: This step can also directly create a function package in vscode, just enter the src directory under the right-click workspace, select the last item "Create Catkin Package", enter the function package name, press Enter, enter the dependent package name, and enter car, you can.

(1) Enter the function package

cd src

(2) Import the dependencies of the function package

catkin_create_pkg 功能包名 roscpp rospy std_msgs

3. Write the code under the function package

(1), cpp version

1. Create and write the cpp file in the src directory of the function package

Right-click directly in vscode to create

The simplest cpp source file:

#include "ros/ros.h" //导入ros的包,任何ros的cpp文件都要导入这个头文件

int main(int argc, char *argv[]) //定义主函数,注意第二个参数中的const一定删
{
    //执行 ros 节点初始化
    ros::init(argc,argv,"hello"); //这是在初始化ros结点,“hello”为节点名
    //创建 ros 节点句柄(非必须)
    ros::NodeHandle n; //这是在初始化节点句柄,节点句柄可以用来调用各种函数
    //控制台输出 hello world
    ROS_INFO("hello world!"); //这是在进行日志输出,即在我们的终端中输出内容

    return 0;
}

注:1、当日志输出中包含中文时会出现乱码,解决方法为:在函数开头加入下面任意一句代码。2、如果没有代码提示,解决方法为:修改 .vscode/c_cpp_properties.json文件,设置 "cppStandard": "c++17"。

setlocale(LC_CTYPE, "zh_CN.utf8");
setlocale(LC_ALL, "");

2. Edit the Cmakelist.txt file of the function package

This file simply needs to add the following:

add_executable(cpp文件的映射名称
  src/xxx.cpp
)
target_link_libraries(cpp文件的映射名称
  ${catkin_LIBRARIES}
)

Note: The mapping name of cpp can be chosen at will. We will call the mapping name during execution, but generally we require it to be consistent with the name of the cpp file; xxx.cpp is to change the name of the cpp file in the src directory of the function package with a suffix. The other two commands are around lines 136 and 149 of the Cmakelist.txt file respectively.

3. Enter the workspace directory and compile

Note: This step can also be used directly in vscode, just use ctrl+shift+B to compile directly.

cd 自定义空间名称
catkin_make

4. Execute the code

First run in the first terminal:

roscore

Start the ros core, this step must be performed before using ros

Then create a second terminal, under this terminal:

cd 工作空间名
source ./devel/setup.bash
rosrun 功能包名 映射名称

注:source ./devel/setup.bash是在修改环境变量,每创建一个终端都需要运行这句话;也可以直接在.bashrc文件中进行设置,.bashrc文件隐藏在我们乌班图的主目录下,使用ctrl+H即可取消隐藏,在文件最后追加“source home/ros/工作空间名/devel/setup.bash”,然后新建终端,运行一次.bashrc文件,输入“source .bashrc”即可。设置完之后就无需每个终端都修改环境变量。

(2), python version

1. Enter the ros function package to add the scripts directory and edit the python file

First create the scripts file:

cd 功能包名
mkdir scripts

After entering scripts, right-click to create a .py file, and the file name can be defined arbitrarily

The simplest .py source file:

#! /usr/bin/env python
# 选择一个python解释器
import rospy

if __name__ == "__main__":
    rospy.init_node("Hello")  # 这是在初始化ros结点,“hello”为节点名
    rospy.loginfo("Hello World!!!!")  # 这是在进行日志输出

2. Add executable permissions to python files

First enter the scripts directory, enter ll in the terminal to view the executable permissions, and then enter the following statements:

chmod +x 文件名.py

Then enter ll to check whether the permission has changed. If there is no change, try the following statement:

sudo chmod 777 文件名.py

3. Edit the CamkeList.txt file under the ros package

catkin_install_python(PROGRAMS scripts/文件名.py
  DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
)

注:This command is around line 162 of the Cmakelist.txt file.

4. Enter the workspace directory and compile

Note: This step can also be used directly in vscode, just use ctrl+shift+B to compile directly.

cd 自定义空间名称
catkin_make

5. Execute code

First run in the first terminal:

roscore

Then create a second terminal, under this terminal:

cd 工作空间名
source ./devel/setup.bash
rosrun 功能包名 文件名.py

Guess you like

Origin blog.csdn.net/ekekex/article/details/130102946