Reference article:
1, Establishment of ROS workspace
Create workspace
1. Open the Home directory, right-click in the blank space, and click New Folder to create a folder named catkin_ws
2. In the folder catkin_ Create a folder src under WS
3. Under src directory, right-click to Open in Terminal and enter catkin_init_workspace. The CMakeLists.txt file appears in the src folder
Code example:
$ mkdir -p ~/catkin_ws/src $ cd ~/catkin_ws/src $ catkin_init_workspace
Compile workspace
At catkin_ Open the terminal in the WS folder and enter catkin_make. At catkin_ build and devel folders are generated in the WS folder.
$ cd ~/catkin_ws/ $ catkin_make
Setting environment variables
The environment variable is set to let the system know where the function package is so that it can be found. Make sure that the installation script correctly covers the workspace, ROS_ PACKAGE_ The path environment variable is contained in the directory.
At catkin_ Under the WS path, enter source devel/setup.bash at the terminal to let the system know that the function package is placed in catkin_ws under this workspace.
Check environment variables
View the current environment variable and enter echo $ROS in the terminal_ PACKAGE_ PATH
$ source devel/setup.bash $ echo $ROS_PACKAGE_PATH
2, Programming of ROS robot circle drawing program
Create Feature Pack
Open the terminal in catkin_ Create my under WS / SRC path_ turtle_ Package.
$ cd catkin_ws/src #Enter workspace $ catkin_create_pkg my_turtle_package std_msgs rospy roscpp
//Compile function pack (Gu Yueju video)
Create C + + files
In my_ turtle_ Create cpp file in package / SRC
$ cd my_turtle_package/src/ $ touch draw_circle.cpp
Writing C + + files
Open draw_circle.cpp, write circle drawing code.
$ gedit draw_circle.cpp
Copy the following circle drawing program to draw_ In circle.cpp.
#include "ros/ros.h" #include<geometry_ Msgs / twist. H > / / motion speed structure type geometry_msgs::Twist definition file int main(int argc, char *argv[]) { ros::init(argc, argv, "vel_ctrl"); //Initialize the node ros::NodeHandle n; //Declare a NodeHandle object n and generate a broadcast object vel with n_ pub ros::Publisher vel_pub = n.advertise<geometry_msgs::Twist>("/turtle1/cmd_vel", 10); //vel_pub will broadcast geometry in the topic "/ cmd_vel" (robot speed control topic)_ Msgs:: twist type data ROS_INFO("draw_circle start...");//Output display information while(ros::ok()) { geometry_msgs::Twist vel_cmd; //Declare a geometry_ Msgs:: object vel of type twist_ CMD and assign the value of speed to this object vel_cmd.linear.x = 2.0;//Front and rear (+ -) m/s vel_cmd.linear.y = 0.0; //Left and right (+ -) m/s vel_cmd.linear.z = 0.0; vel_cmd.angular.x = 0; vel_cmd.angular.y = 0; vel_cmd.angular.z = 1.8; //Rotation speed of robot, + left turn, - right turn, unit: rad/s vel_pub.publish(vel_cmd); //After assignment, it is sent to the topic "/ cmd_vel". The core node of the robot will receive and send the past speed value from this topic and forward it to the hardware body for execution ros::spinOnce();//Calling this function allows other callback functions to be executed (more than the routine does not use callback functions) } return 0; }
Modify relevant documents
Continue to enter the following command in the terminal:
$ cd ~/catkin_ws/src/my_turtle_package $ gedit CMakeLists.txt #Open CMakeLists.txt
Find ##Declare a C++ executable in CMakeLists.txt file, and add the following before this line:
add_executable(draw_circle src/draw_circle.cpp) target_link_libraries(draw_circle ${catkin_LIBRARIES})
compile
Save and exit the CMakeLists.txt file. Then continue to enter the following command in terminal to compile:
$ cd ~/catkin_ws/ $ catkin_make
Compilation succeeded
3, Start of ROS robot
1. Open the first terminal and start ros
$ roscore
2. Open the second terminal and start rosnode
$ rosrun turtlesim turtlesim_node
3. Open the third terminal and start my_turtle_package node
$ cd ~/catkin_ws/ $ source devel/setup.bash $ rosrun my_turtle_package draw_circle
The little turtle began to draw a circle. Press Ctr+C to exit the node, and the little turtle stops.