Table of contents
introduce
Ros development is an essential work in robot development, and the language choice can be c++ or python. In terms of tools, they cannot be as colorful as the windows system, qtcreate, visual studio, clion. Because most of the ros environment is under the ubuntu system, it is limited to use the IDE only in the linux environment, and here we choose vscode.
premise
This article is not from scratch, from installing the ros environment to building the vscode tool, then writing code, compiling and debugging, the premise here is that our system has installed ros-melodic-desktop-full, and can run ros-related commands.
vscode installation
First install vscode, it is recommended to use the official address to download, and then manually switch to the domestic accelerator.
After clicking download, because the download of the official address is very slow, find the download item directly in the browser download, copy the download link, paste it into the address bar of the browser, and modify it to a domestic address.
for example:
The downloaded deb installation file can be installed directly through dpkg -i xxx.deb.
vscode plugin installation
We need to install two plug-ins of ros and catkin-tools in the plug-in extension Extensions.
workspace preparation
mkdir -p roshelloworld/src
cd roshelloworld/src
catkin_init_workspace
cd ..
catkin_make
Note that if there is no catkin_init_workspace command prompt here, it is an environmental problem. In fact, it has been installed when ros is installed. We need to set it up and run:
echo "source /opt/ros/melodic/setup.bash" >> /etc/profile
open vscode
code .
In the currently compiled workspace, open vscode through the command line
Create catkin package
Right-click src->Create Catkin Package
In the follow-up operation, you need to enter the package name and dependency name. The package name here is my_test_pkg, and the dependencies are roscpp, rospy, and std_msgs.
The package structure then becomes as follows:
The original roshelloworld/src has more my_test_pkg, and then my_test_pkg also has src, CMakeLists.txt files. The CMakeLists.txt here is different from the CMakeLists.txt under roshelloworld. We will modify the CMakeLists.txt file under the my_test_pkg package later.
write cpp code
We create a new my_test_pkg_node.cpp in the my_test_pkg/src directory, and add the code 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, "talker");
ros::NodeHandle n;
ros::Publisher chatter_pub = n.advertise<std_msgs::String>("chatter", 1000);
ros::Rate loop_rate(10);
int count = 0;
while(ros::ok())
{
std_msgs::String msg;
std::stringstream ss;
ss << "hello,world" << count;
msg.data = ss.str();
ROS_INFO("%s", msg.data.c_str());
chatter_pub.publish(msg);
ros::spinOnce();
loop_rate.sleep();
count++;
}
return 0;
}
This code publishes the /chatter topic, and sends hello, world + count every 10ms. If you test later, you can subscribe to the /chatter topic to see the received messages.
Modify CMakeLists.txt:
include_directories(
include
${catkin_INCLUDE_DIRS}
)
add_executable(${PROJECT_NAME}_node src/my_test_pkg_node.cpp)
target_link_libraries(${PROJECT_NAME}_node
${catkin_LIBRARIES}
)
These configurations are available in CMakeLists.txt, just open the corresponding comments.
compile
Run catkin_make on the command line
run
Start the ros service
roscore
listen topic
rostopic echo /chatter
start ros test
rosrun my_test_pkg my_test_pkg_node
This is the end of the introduction of the vscode+ros development environment. I did not debug the code and copied other people's code directly.