Configure ROS, PCL and OpenCV development environment records in VScode under Linux environment

1. Install the necessary plug-ins

Open VScode, and install CMake, CMake Tools, ROS and catkin-tools plug-ins during development . The screenshot is as follows. After installation, reopen the VScode plug-in to take effect.

 2. Create a ROS workspace

Under the selected path, open a terminal to create a workspace. The specific commands are as follows:

mkdir -p ~/ROS_Study2/src
cd ~/ROS_Study2/src
catkin_init_workspace

cd ~/ROS_Study2
catkin_make

3. Open the workspace with VScode and create a work package

Open the workspace created above with VScode, the file directory is shown as follows:

 Right-click src, select Create Catkin Package, and name the Package HelloROS

 The next step is to add roscpp brother rospy dependency

 Next, the file directory in the src file is as follows:

and  are automatically generated in .vscodec_cpp_properties.jsonsettings.json

 Next, add a cpp file in the src/helloROS/src directory, the command is helloros.cpp, and the content is as follows:

#include <iostream>
#include <string>
#include <sstream>
#include <ros/ros.h>
#include <std_msgs/String.h>

using namespace std;

int main(int argc, char** argv)
{
	ros::init(argc, argv, "helloros");
	ros::NodeHandle n;
	ros::Publisher pub_str = n.advertise<std_msgs::String>("talker", 1000);
	ros::Rate loop_rate(10);
	int count = 0;
	while(ros::ok())
	{
		std_msgs::String msg;
		std::stringstream ss;
		ss << "hello ROS! " << count;
		msg.data = ss.str();
		ROS_INFO("%s", msg.data.c_str());
		pub_str .publish(msg);
		ros::spinOnce();
		loop_rate.sleep();
		count++;
	}
	return 0;
}

Four. Generation and configuration of json files in vscode

1. c_cpp_properties.json: used to specify the C/C++ class library and include path , press and hold Fn+F1, findC/C++:编辑配置(JSON)即可自动生成json文件(这里已经自动生成,不需要创建)

The c_cpp_properties.json file  automatically generated here , the specific content is as follows:

{
  "configurations": [
    {
      "browse": {
        "databaseFilename": "${workspaceFolder}/.vscode/browse.vc.db",
        "limitSymbolsToIncludedHeaders": false
      },
      "includePath": [
        "/opt/ros/kinetic/include/**",
        "/usr/include/**"
         //这里就可以包含ROS,PCL和OpenCV相关的头文件的路径
      ],
      "name": "ROS",
      "intelliSenseMode": "gcc-x64",
      "compilerPath": "/usr/bin/gcc",
      "cStandard": "gnu11",
      "cppStandard": "c++14"
    }
  ],
  "version": 4
}

2. tasks.json: for compiling

Press ctrl + shfit + pthe input command tasks: configure task, and then select in the drop-down catkin_make: build to automatically generate tasks.jsonthe file. The content of the generated tasks.jsonfile is as follows:

{
	"version": "2.0.0",
	"tasks": [
		{
			"type": "catkin_make",
			"args": [
				"--directory",
				"/home/yang/Project/ROS_Study2",
				"-DCMAKE_BUILD_TYPE=RelWithDebInfo"
			],
			"problemMatcher": [
				"$catkin-gcc"
			],
			"group": "build",
			"label": "catkin_make: build"
		}
	]
}

Modify the src/helloROS/CMakeLists.txt file and add the following content:

catkin_package(
  CATKIN_DEPENDS
)
# 头文件路径
include_directories(
include
  ${catkin_INCLUDE_DIRS}
)
# 生成可执行文件
add_executable( helloROS src/helloros.cpp )
# 链接库
target_link_libraries(helloROS ${catkin_LIBRARIES})

3.  launch.json, for debugging

Shortcut key ctrl+shift+d, create a new launch.json file, and the following content will be automatically generated:

{
    // 使用 IntelliSense 了解相关属性。 
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) 启动",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/devel/lib/helloROS/helloROS",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${fileDirname}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "为 gdb 启用整齐打印",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        },
    ]
}

Five running nodes

1. Run ROS MASTER

执行快捷键ctrl + shfit + p,

再输入 ROS:Start

 2. Run the node

执行快捷键ctrl + shfit + p

输入ROS:Run a Ros executable

依次输入创建的功能包的名称以及节点名称(即编译成功后二进制文件的名称,注意不是文件名)

 The terminal prints the result as follows:

 So far, the configuration of ROS in VScode is completed. The configuration of PLC and OpenCV in VScode only needs to add the corresponding header file path and related commands in c_cpp_properties.json and CMakeLists.txt, and the dynamic completion function can be realized.

Postscript: VScode uses compile_commands.json to configure the includePath environment

1. Use cmake to export compile_commands.json

命令:cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=YES ..

2. Use under ROS

命令:catkin_make -DCMAKE_EXPORT_COMPILE_COMMANDS=YES

3. Add set command in CMakeLists.txt

Instruction: set (CMAKE_EXPORT_COMPILE_COMMANDS ON)

4. Add in compile_commands.json in VScode

Add in:

"compileCommands": "${workspaceFolder}/build/compile_commands.json" //Add compile_commands.json generated by cmake

As shown in the picture:

Guess you like

Origin blog.csdn.net/qq_36812406/article/details/127696926