상세 컨텐츠

본문 제목

3 Create python file on ROS package

머신러닝

by Riella 2020. 7. 9. 16:27

본문

728x90

1. Navigate to src/[package name]/src

2. Type

edit [name].py
:wq

':wq' is to save file.

3. I will edit with nano. Type

nano [name].py

(I named it tutorial.py)

4. Below is the code I typed

#! /usr/bin/env python
import rospy
from std_msgs.msg import String

rospy.init_node('tutorial')
publisher = rospy.Publisher('/say_hello', String, queue_size=1)
rate = rospy.Rate(3)

while not rospy.is_shutdown():
	publisher.publish('Hey!')
    rate.sleep()

4. On another new terminal, you need to turn on bash file and ...

roscore

roscore prepares ROS system to accept commands

If you don't you will get an error

ERROR: Unable to communicate with master!

5. Go back to where you have created the file. (to catkin_ws/src)

When you type

rosnode list

so far there is no node that we have created (called tutorial)

also when you type

rostopic list | grep say_hello

there is no topic that we made (called say_hello)

6. To make it executable, type

chmod +x tutorial/src/publisher.py

7. Run the code

rosrun tutorial publisher.py

8. Now you can run below two lines and check if the node and topic have created.

rosnode list
rostopic list | grep say_hello

9. Run the script

rostopic echo /say_hello

 

'머신러닝' 카테고리의 다른 글

5 roslaunch  (0) 2020.07.10
4 Turtlesim  (0) 2020.07.09
2 Create ROS Package  (0) 2020.07.09
1 Create ROS Workspace  (0) 2020.07.09
Download Python, pip, TensorFlow  (0) 2020.07.06

관련글 더보기

댓글 영역