Welcome to Jetson Orin NX SUPER repository
当製品の日本語版マニュアル資料は、こちらからご取得ください。
04. Publisher
4.Publisher
4.1 Publisher
Publishers, as the name suggests, serve to publish messages. The message here can be sensor information transmitted from the lower computer to the upper computer, which is then packaged and sent to subscribers who have subscribed to the topic through the upper computer; It can also be the data from the upper computer that has been processed, packaged, and sent to subscribers who subscribe to the topic.
4.2. Create a workspace and topic feature pack
4.2.1 Creating a workspace
mkdir -p ~/catkin_ws/srccd ~/catkin_ws/srccatkin_init_workspace
4.2.2 Compilation workspace
xxxxxxxxxxcd ~/catkin_ws/catkin_make
4.2.3. Updating Environmental Variables
xxxxxxxxxxsource devel/setup.bash
4.2.4. Checking Environmental Variables
xxxxxxxxxxecho $ROS_PACKAGE_PATH
4.2.5. Create Function Package
xxxxxxxxxxcd ~/catkin_ws/srccatkin_create_pkg learning_topic std_msgs rospy roscpp geometry_msgs turtlesim
4.2.6 Compilation Function Package
xxxxxxxxxxcd ~/catkin_wscatkin_makesource ~/catkin_ws/devel/setup.bash
4.3. Creating a Publisher
4.3.1 Creation steps
- Initialize ROS node
- Create handleRegister
- node information with ROS Master, including the published topic name, message types in the topic, and queue length
- Create and initialize message data
- Cycle sending messages at a certain frequency
4.3.2. C++Language Implementation
- In the src folder of the feature pack, create a c++file (with the suffix. cpp) and name it turtle_ velocity_ publisher.cpp
- Copy and paste the program code below into the title_ velocity_ In the publisher.cpp file
x#include <ros/ros.h>#include <geometry_msgs/Twist.h>int main(int argc, char **argv){ros::init(argc, argv, "turtle_velocity_publisher");ros::NodeHandle n;ros::Publisher turtle_vel_pub = n.advertise<geometry_msgs::Twist>("/turtle1/cmd_vel", 10);ros::Rate loop_rate(10);while (ros::ok()){geometry_msgs::Twist turtle_vel_msg;turtle_vel_msg.linear.x = 0.8;turtle_vel_msg.angular.z = 0.6;turtle_vel_pub.publish(turtle_vel_msg);/ROS_INFO("Publsh turtle velocity command[%0.2f m/s, %0.2f rad/s]", turtle_vel_msg.linear.x, turtle_vel_msg.angular.z);loop_rate.sleep();/}return 0;}
- Program flowchart, corresponding to 1.3.1 content for viewing

- Configure in CMakelist.txt, under the build area, add the following content
xxxxxxxxxxadd_executable(turtle_velocity_publisher src/turtle_velocity_publisher.cpp)target_link_libraries(turtle_velocity_publisher ${catkin_LIBRARIES})
- Compiling code under workspace directory
xxxxxxxxxxcd ~/catkin_wscatkin_makesource devel/setup.bash
- run a program
- Run score
xxxxxxxxxxroscore
- Running the Little Turtle Node
xxxxxxxxxxrosrun turtlesim turtlesim_node
- Run the publisher and continuously send speed to Little Turtle
xxxxxxxxxxrosrun learning_topic turtle_velocity_publisher
- Screenshot of operation effect

- Program operation instructions
- Entering the rostopic list on the terminal to view the topic list will reveal/tourle1/cmd_ The topic of vel
- We use rostopic info/tourle1/cmd_ Looking at vel, you will find that

This indicates that Little Turtle is a subscription/tourle1/cmd_ The speed topic is vel, so the publisher keeps sending speed data. After receiving it, the little turtle starts to move according to the speed
4.3.3. Python Language Implementation
- In the feature pack directory, create a new folder called scripts, and then create a new Python file (with the suffix. py) under the scripts folder, named turtle_ velocity_ publisher.py
- Copy and paste the program code below into the title_ velocity_ In the publisher.py file
x#!/usr/bin/env python# -*- coding: utf-8 -*-import rospyfrom geometry_msgs.msg import Twistdef turtle_velocity_publisher():rospy.init_node('turtle_velocity_publisher', anonymous=True) # ROS node initializationturtle_vel_pub = rospy.Publisher('/turtle1/cmd_vel', Twist, queue_size=8)rate = rospy.Rate(10) #Set the frequency of the loopwhile not rospy.is_shutdown():# Initializes a message of type geometry_msgs::Twistturtle_vel_msg = Twist()turtle_vel_msg.linear.x = 0.8turtle_vel_msg.angular.z = 0.6# Publish a messageturtle_vel_pub.publish(turtle_vel_msg)rospy.loginfo("linear is :%0.2f m/s, angular is :%0.2f rad/s",turtle_vel_msg.linear.x, turtle_vel_msg.angular.z)rate.sleep()# Delay according to the cycle frequencyif __name__ == '__main__':try:turtle_velocity_publisher()except rospy.ROSInterruptException:pass
- Process Flow Chart
.jpg)
- run a program
- Run score
xxxxxxxxxxroscore
- Running the Little Turtle Node
xxxxxxxxxxrosrun turtlesim turtlesim_node
- Run the publisher and continuously send speed to Little Turtle
xxxxxxxxxxrosrun learning_topic turtle_velocity_publisher
Note: Before running, it is necessary to give the title_ velocity_ Publisher.py adds executable permissions in the title_ velocity_ Open the terminal in the publisher.py folder,
xxxxxxxxxxsudo chmod a+x turtle_velocity_publisher.py
All Python requires adding execute file permissions, otherwise an error will be reported!5) Refer to 1.3.2 for operational effects and program instructions.