1, Introduction
This section mainly uses the case of turnlesim built in ROS, combined with the information of nodes, topics, topic messages, services, service messages and parameters that have been introduced by ROS commands, and finally realizes the control of tortoise movement, subscription of tortoise posture, tortoise generation and modification of tortoise form background color in the way of coding.
2, Topic release
2.1} analyze problems
Requirement Description: coding to realize tortoise motion control and let the little tortoise make circular motion.
Implementation analysis:
- There are two key nodes for the realization of tortoise motion control. One is the tortoise motion display node turnsim_ Node and the other is the control node. The two communicate in the subscription and publishing mode. The tortoise motion display node can be called directly. The motion control node used the turtle before_ teleop_ The key is controlled through the keyboard. Now you need to customize the control node.
- When implementing the control node, you first need to understand the topics and messages used by the communication between the control node and the display node, which can be obtained by using the ros command combined with the calculation diagram.
- After knowing the topic and message, write the motion control node in C + + or Python, and publish the message according to certain logic through the specified topic.
Implementation process:
- Get the topic and message information through the calculation diagram and ros command.
- Coding to achieve motion control nodes.
- Start roscore and turnlesim_ Node and user-defined control nodes to view the operation results.
Final effect:
2.2 acquisition of topic and message format
Open the little turtle simulator first:
roscore rosrun turtlesim turtlesim_node rosrun turtlesim turtle_teleop_key
Then the same as rqt_graph to get the topic:
There are two ways to get messages:
rostopic type /turtle1/cmd_vel
rostopic info /turtle1/cmd_vel
Get message type:
rosmsg show geometry_msgs/Twist
rosmsg info geometry_msgs/Twist
In the little turtle case, there is only movement in the x direction and rotation in the z direction
2.3 code implementation (C + +)
Create a new function package named plumbing_test. The dependency is rospy , roscpp , std_msgs geometry_msgs
Create a new file under src, test01_pub_twist.cpp
#include<ros/ros.h> #include<geometry_msgs/Twist.h> /* Demand: release speed message Topic: / turtle 1 / CMD_ vel Message: geometry_msgs/Twist Operation: 1.Include header file 2.Node initialization 3.Create handle 4.Create publish object 5.Publishing logic 6.spinOnce() */ int main(int argc, char *argv[]) { ros::init(argc,argv,"HaiGui"); ros::NodeHandle nh; ros::Publisher pub = nh.advertise<geometry_msgs::Twist>("/turtle1/cmd_vel",10); ros::Rate rate(10); //Set publishing frequency geometry_msgs::Twist twist; twist.linear.x = 1.0; twist.linear.y = 0.0; twist.linear.z = 0.0; twist.angular.x = 0.0; twist.angular.y = 0.0; twist.angular.z = 1.0; while (ros::ok()) { pub.publish(twist); rate.sleep(); ros::spinOnce(); } return 0; }
Compile: Ctrl + Shift + B
Test:
2.4 code implementation (Python)
Create a new folder scripts and a new py file.
#!/usr/bin/env python # -*- coding:UTF-8 -*- import rospy from geometry_msgs.msg import Twist """ Publisher: publish speed message topic of conversation: /turtle1/cmd_vel Message: geometry_msgs/Twist Implementation process: 1.Guide Package 2.initialization ROS node 3.Create publisher object 4.Loop publishing motion control messages """ if __name__ =="__main__": # 2. Initialize ROS node rospy.init_node("control_circle_p") # 3. Create publisher object pub = rospy.Publisher("/turtle1/cmd_vel",Twist,queue_size=10) # 4. Circularly issue motion control messages rate = rospy.Rate(10) msg = Twist() msg.linear.x = 1.0 msg.linear.y = 0.0 msg.linear.z = 0.0 msg.angular.x = 0.0 msg.angular.y = 0.0 msg.angular.z = 0.5 while not rospy.is_shutdown(): pub.publish(msg) rate.sleep()
Add executable permissions:
Configure cmakelists Txt file:
Compile
Test: