The little turtle simulates and realizes circular motion
1. Basic introduction to ROS
ROS (Robot Operating System) provides a series of libraries and tools to help software developers create robot application software. It provides many functions such as hardware abstraction, device drivers, library functions, visualization, message passing and package management.
ROS is the four in one of communication mechanism + development tool + application function + ecosystem.
Features: peer-to-peer, distributed, multi-language support, lightweight, free and open source.
2. Little turtle simulation
- The first step is to open a terminal and enter the following command
roscore
- The second step is to open another terminal and enter the following command to start the little turtle emulator
rosrun turtlesim turtlesim_node
- The third step is to open another terminal and enter the following command to start the controller
rosrun turtlesim turtle_teleop_key
At this point, you can use the up, down, left, and right keys to move the little turtle.
3. Write a program to let the little turtle achieve circular motion
1. Use the topic viewer to view the message transmission between two nodes
Open thread viewer
Enter the following commands on the command line
rosrun rqt_graph rqt_graph
In this picture, it can be seen that the two nodes, turtle_teleop and turtlesim, communicate through /strong/cmd_vel, which is the topic, and the button information will be published on this topic, and then turtlesim_node receives information from this topic to control its movement, so you need to write a File sending control command.
Enter the code in the figure below to see the acquisition of the message.
Fundamentals of Baby Turtle Movement
Enter the following command to view
rostopic type /turtle1/cmd_vel
You can see the following, the data type is geometry_msgs/Twist, and you need to check further.
Enter the following code to check:
rosmsg show geometry_msgs/Twist
- Linear means linear speed;
- Angular means angular velocity;
- xyz represents the coordinates;
if you want to control the turtle to turn around, you must keep moving forward at a constant speed, and then the angular velocity should not be 0.
2. Write code to realize circle
1. Create a workspace
mkdir -p ~/catkin_ws/src
cd ~/catkin_ws/src
catkin_init_workspace
2. Compile the workspace
cd ~/catkin_ws
catkin_make
source devel/setup.bash
3. Create a ROS function package
Create a feature package:
cd ~/catkin_ws/src
catkin_create_pkg learning_topic std_msgs rospy roscpp geometry_msgs
Compile the feature pack:
cd ~/catkin_ws
catkin_make
source devel/setup.bash
4. C++ code
#include <sstream>
#include "ros/ros.h"
#include "std_msgs/String.h"
#include "geometry_msgs/Twist.h"
int main(int argc, char **argv)
{
//初始化节点
ros::init(argc, argv, "publisher");
//创建节点句柄
ros::NodeHandle n;
//创建publisher,发布话题[/turtle1/cmd_vel],消息类型[geometry_msgs::Twist]
ros::Publisher chatter_pub = n.advertise<geometry_msgs::Twist>("/turtle1/cmd_vel", 1000);
//设置循环频率
ros::Rate loop_rate(100);
while (ros::ok())
{
//定义发布的数据
geometry_msgs::Twist msg;
//定义linear数据
geometry_msgs::Vector3 linear;
linear.x=1;
linear.y=0;
linear.z=0;
//定义angular数据
geometry_msgs::Vector3 angular;
angular.x=0;
angular.y=0;
angular.z=1;
msg.linear=linear;
msg.angular=angular;
//发布msg数据
chatter_pub.publish(msg);
//循环等待回调函数
ros::spinOnce();
//按照循环频率延时
loop_rate.sleep();
}
return 0;
}
5. Add compile options
Open the CmakeLists file
cd ~/catkin_ws/src/learning_topic
gedit CMakeLists.txt
Add the following code at the end of the file:
add_executable(publisher src/publisher.cpp)
target_link_libraries(publisher ${
catkin_LIBRARIES})
Save and exit, and compile.
cd ~/catkin_ws
catkin_make
6. Start the little turtle
roscore
#打开新终端
rosrun turtlesim turtlesim_node
#打开新终端
cd ~/catkin_ws
#下面这一步是为了保证rosrun命令能够找到相应的功能包
source ~/catkin_ws/devel/setup.bash
rosrun learning_topic publisher
The result is shown in the figure below, the baby turtle performs a circular motion.
Four. Summary
This experiment is mainly to understand the message and topic sending and receiving mechanism of ROS, use the little turtle demo to experience ROS intuitively, and program to control the little turtle to perform circular motion.
5. References
Install ROS on Ubuntu 18.04——realize small turtle simulation
ROS communication practice (little turtle communication implementation) (9) C++, Python