Last active
August 1, 2020 07:45
-
-
Save syanyong/2eed8b1c2e3824157063a9bf7542e4a7 to your computer and use it in GitHub Desktop.
ROS Training Day 2 (Starter script of publisher)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
chmod +x catkin_ws/src/my_turtlesim/script/pub.py |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import rospy # เรียก external library ชื่อ rospy | |
from geometry_msgs.msg import Twist # เรีียก lib Twist จาก package geometry_msgs.msg | |
import time # เรียก lib ชื่อ time | |
vel = Twist() # สร้าง object ชื่อ vel จาก Class Twist | |
# def คือการประกาศฟัังก์ชั่นใน Python | |
def main (): | |
# Code ชุดนี้อยู่ใน main ฟังก์ชั่น จะต้องวรรค 4 ช่อง | |
rospy.init_node('turtle_commander', anonymous=True) # ประกาศโหนดใหม่ชื่อ turtle_commander | |
rate = rospy.Rate(10) # ความถี่ในการ Publish คือ 10 Hz 100ms | |
# สร้าง obj ชื่อ pub ไว้ publish ข้อมูลไปยัง topic /turtle1/cmd_vel | |
# โดยชนิดข้อมูลคือ Twist | |
pub = rospy.Publisher('/turtle1/cmd_vel', Twist,queue_size=10) | |
timestart = time.time() # สร้างตัวแปรเพื่อเก็บเวลา เริ่มต้น | |
while not rospy.is_shutdown(): # ทำซ้ำจนกว่าเงื่อนไขจะผิด โดย is_shutdown จะเป็น จริง เมื่อเรากด ctrl+c | |
timenow = time.time() # สร้างตัวแปรเพื่อเก็บเวลา ณ ปัจจุบัน | |
counter = timenow - timestart # เวลานับตั้งแต่เริ่มรัน (หน่วย ms) | |
# TODO | |
print("Turtle is running. ", counter) # แสดงข้อความบนจอ | |
vel.linear.x = 0.1 | |
# END | |
pub.publish(vel) # ทำการ publish ข้อมูลใน vel เข้าระบบ | |
rate.sleep() # หน่วงเวลาเพื่อคุมจังหวะในการ Publish | |
if __name__ == '__main__': | |
try: | |
main () | |
except rospy.ROSInterruptException: | |
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
rosservice call /reset |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment