ROS introductory tutorial (1) Python implements Hello world

code in git

link

Python implements Hello world.
Writing Python programs in ROS is similar to writing C++ programs.

step1. Create a workspace
Ctrl+Alt+T to open the terminal and enter: [Create a workspace in the main directory]

mkdir -p helloworld_ws/src
cd helloworld_ws
catkin_make

Among them, helloworld_ws is the name of the workspace we named, and catkin_make is a compilation command, which will help us create the files we need and output several logs. The successful results are as follows:

step2. Create a function package
Continue to enter in the terminal: [Create a function package in the src file of the workspace]

cd src
catkin_create_pkg helloworld roscpp rospy std_msgs

Among them, helloworld is the name of our function package, roscpp, rospy and std_msgs are the added dependencies. These dependencies are some of the most basic dependencies for us to run programs in ROS. The successful results are as follows: [The message dependency in the lower version of ROS may be ros_msgs]

step3. Edit the source file
Create a new folder named [scripts] in the function package at the same level as the [src] folder

Enter in the terminal: [Create source file]

cd ~/helloworld_ws/src/helloworld/scripts
gedit  talker.py

Enter in the blank document: 【Hello world!】

#!/home/lg/anaconda3/bin/python

import rospy
from std_msgs.msg import String

def talker():
    pub = rospy.Publisher('chatter', String, queue_size=10)
    rospy.init_node('talker', anonymous=True)
    rate = rospy.Rate(10) # 10hz
    while not rospy.is_shutdown():
        hello_str = "hello world %s" % rospy.get_time()
        rospy.loginfo(hello_str)
        pub.publish(hello_str)
        rate.sleep()

if __name__ == '__main__':
    try:
        talker()
    except rospy.ROSInterruptException:
        pass
gedit listener.py
#!/home/lg/anaconda3/bin/python


import rospy
from std_msgs.msg import String

def callback(data):
    rospy.loginfo(rospy.get_caller_id() + 'I heard %s', data.data)

def listener():

    # In ROS, nodes are uniquely named. If two nodes with the same
    # name are launched, the previous one is kicked off. The
    # anonymous=True flag means that rospy will choose a unique
    # name for our 'listener' node so that multiple listeners can
    # run simultaneously.
    rospy.init_node('listener', anonymous=True)

    rospy.Subscriber('chatter', String, callback)

    # spin() simply keeps python from exiting until this node is stopped
    rospy.spin()

if __name__ == '__main__':
    listener()

step4. Edit the configuration file
Open the CMakeLists.txt in the helloworld function package [helloworld_ws/src/helloword/CMakeLists.txt]

catkin_install_python(PROGRAMS
   scripts/talker.py
   scripts/listener.py
   DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
 )

step5. Compile and execute and open
a new input: [Start ros core]

roscore

Re-open a terminal and enter: [compile and execute in the workspace]

cd ~/helloworld_ws
catkin_make
source ./devel/setup.bash
rosrun helloworld talker.py

Re-open a terminal and enter: [compile and execute in the workspace]

cd ~/helloworld_ws
catkin_make
source ./devel/setup.bash
rosrun helloworld listener.py

insert image description here

Guess you like

Origin blog.csdn.net/luoganttcc/article/details/130047131