Skip to content

Instantly share code, notes, and snippets.

@syanyong
Created March 21, 2021 07:00
Show Gist options
  • Save syanyong/ea78508e5b915f2ce0049589805ef25f to your computer and use it in GitHub Desktop.
Save syanyong/ea78508e5b915f2ce0049589805ef25f to your computer and use it in GitHub Desktop.
Demo code for exercise
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import rospy # เรียก Libray จากภายนอก
from geometry_msgs.msg import Twist
import time
vel = Twist() # สร้าง Object สำหรับเปลี่ยนความเร็วหุ่นยนต์
def main (): # ฟังก์ชั่นหลัก
rospy.init_node('turtle_commander', anonymous=False) # Regis Node บน Master
rate = rospy.Rate(10) # คุม Sampling time ของ Node นี้ (Hz)
# สร้างตัวแปร pub สำหรับการ Publish ข้อมูล ไปยัง Topic /turtle1/cmd_vel
pub = rospy.Publisher('/turtle1/cmd_vel', Twist,queue_size=10)
timestart = time.time()
while not rospy.is_shutdown(): # ลูปในการควบคุมหุ่นยนต์
timenow = time.time()
counter = timenow - timestart # สำหรับนับเวลา (หน่วย วินาที)
# TODO
print("My first Python", counter)
if (counter <= 2):
print("Step 1")
vel.linear.x = 0.5
elif (counter > 2 and counter <= 4):
print("Step 2")
vel.linear.x = 0
vel.angular.z = 1
else:
print("Stop")
vel.linear.x = 0
vel.angular.z = 0
# END
pub.publish(vel)
rate.sleep()
if __name__ == '__main__':
try:
main ()
except rospy.ROSInterruptException:
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment