The topic ros

--- --- restore content begins

Declare a topic to post a message

 

#!/usr/bin/env python
import rospy
from std_msgs.msg import Int32
rospy.init_node('topic_publisher')
pub = rospy.Publisher('counter',Int32)
rate = rospy.Rate(2)
count = 0
while not rospy.is_shutdown():
    pub.publish(count)
    count += 1
    rate.sleep()

The first line of

#!/usr/bin/env python

The so-called shebang tell the operating system which is a python file should be passed to the python interpreter in linux to run this system requires the use of chmod u + x name.py permission to increase

second line

import rospy

Responsible for importing basic functions that will be used

The third row

from std_msgs.msg import Int32

As used herein, the 32-bit integer introduced in order to make normal needs to import from .msg <package name> This is defined as a storage place,

Because the use of messages from the need to add other packages rely <depend package = "std_msgs" /> in the package.xml

## added in the implementation process, the error occurs during execution, there may be a position I add an incorrect =. =

 

pub = rospy.Publisher('counter',Int32)

Define a topic name (counter) and describes the type of release of information (Int32), roscore connection is established and publisher, when another topic when the subscription will be two topics connected

## An error has occurred in the implementation process and prompts the need added queue_size = 'number' but here is not run successfully after the increase in the

rate = rospy.Rate(2)

Setting rate in hertz, sent twice per second, here

 

If the node is ready to close is_shutdown () function returns a Ture otherwise it will return a False, said they did not close the sentence continued to Xunhua

ratee.sleep () call allows the program to sleep for some time, to ensure the implementation of a frequency cycle is approximately 2Hz

 

!!!!!!!!!!!!

Errors in the debugging process, in order to record

The document needs to establish in the workspace catkin, if handwritten code remember to run chmod to set permissions

And above #! Can not be changed when the / usr / bin / env python

Key: You can not add comments in the code when the specific cause is unknown

Roscore first run before running, rosrun basic topic_publisher.py run in another terminal, and then perform rostopic list in another terminal counter to see whether the topic is declared

Successful implementation of the chart:

 

 

 

 

 

 

 

--- end --- restore content

Guess you like

Origin www.cnblogs.com/miaorn/p/11710326.html