ROS(drone포함)

ROS LineFollow 따라하기.[급종료]

728x90

ros로 자율주행을 위한 튜토리얼을 듣고있다.

rospy를 기반으로 수행하며 문제없이 사용이 가능.

 

https://www.youtube.com/watch?v=mqRaTg9Qnns&t=10s 

이게 모든 과정의 목표임.

 

1.ROS Tutorial - Setting up ROS Workspace, Publishing & Subscribing (Tutorial-1)

https://www.youtube.com/watch?v=jhj6QIqL5Tg 

from std_msgs.msg import UInt32를 사용하기위해 sudo apt-get install -y ros-std-msgs를 설치해줌.

 

패키지를 하나 생성해주며, CMake와 package파일은 수정을 하지 않음.

 

1)talker

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

class Talker:
    def __init__(self):
        rospy.init_node("talker")
        self.pub = rospy.Publisher('/topic', UInt32, queue_size=10)
        self.rate = rospy.Rate(10) #10hz
        self.msg = UInt32()

        self.publish()

    def publish(self):
        i=0
        while not rospy.is_shutdown():
            self.msg = i
            print(self.msg)
            self.pub.publish(self.msg)
            self.rate.sleep()
            i+=1

if __name__=='__main__':
    try:
        talker = Talker()
    except rospy.ROSInterruptException as e:
        print(e)

2)listener

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

class Listener:
    def __init__(self):
        rospy.init_node('listener')
        self.sub = rospy.Subscriber('/topic', UInt32, self.callback)

    def callback(self,data):
        print(data)

if __name__=='__main__':
    listener = Listener()
    try:
        rospy.spin()
    except rospy.ROSInterruptException as e:
        print(e)

roscore / rosrun line_follow talker.py / rosrun line_follow listener.py

정상진행.

 

rostopic list -v

rostopic echo /topic

rosrun rqt_graph rqt_graph

rostopic과 rostopic echo를 나타냄

 

2. ROS Tutorial - Talker Listener Upgrade | Transferring Velocity and Image msgs among nodes|Tutorial-2

https://www.youtube.com/watch?v=8_iL1ieMt4Q&t=12s 

1)talker

#!/usr/bin/env python
import rospy
from geometry_msgs.msg import Twist

class Talker:
    def __init__(self):
        rospy.init_node("talker")
        self.pub = rospy.Publisher('/topic', Twist, queue_size=10)
        self.rate = rospy.Rate(10) #10hz
        self.msg = Twist()

        self.publish()

    def publish(self):
        i=0
        while not rospy.is_shutdown():
            self.msg.linear.x = 6 * i
            self.msg.linear.y = 6 * i + 1
            self.msg.linear.z = 6 * i + 2 

            self.msg.linear.x = 6 * i + 3
            self.msg.linear.y = 6 * i + 4
            self.msg.linear.z = 6 * i + 5

            print(self.msg)
            self.pub.publish(self.msg)
            self.rate.sleep()
            i+=1

if __name__=='__main__':
    try:
        talker = Talker()
    except rospy.ROSInterruptException as e:
        print(e)

2) listener

#!/usr/bin/env python
import rospy
from geometry_msgs.msg import Twist

class Listener:
    def __init__(self):
        rospy.init_node('listener')
        self.sub = rospy.Subscriber('/topic', Twist, self.callback)

    def callback(self,data):
        print(data)

if __name__=='__main__':
    listener = Listener()
    try:
        rospy.spin()
    except rospy.ROSInterruptException as e:
        print(e)

talker와 listener

통신으로 동영상 재생하기 - talker

#!/usr/bin/env python
import rospy
from sensor_msgs.msg import Image
from cv_bridge import CvBridge, CvBridgeError
import cv2 as cv

class Talker:
    def __init__(self):
        rospy.init_node("talker")
        self.pub = rospy.Publisher('/topic', Image, queue_size=1)
        self.rate = rospy.Rate(10) #10hz
        self.msg = Image()
        self.bridge = CvBridge()

        self.read()

    def read(self):
        cap = cv.VideoCapture('Videos/igv.mp4')
        while not rospy.is_shutdown():
            ret,image = cap.read()
            if ret:
                cv.imshow('image', image)
                if cv.waitKey(10) & 0xFF == ord('q'):
                    break
                self.msg = self.bridge.cv2_to_imgmsg(image,'bgr8')
                self.publish()
        cap.release()
        cv.destroyAllwindows()

    def publish(self):
        self.pub.publish(self.msg)
        self.rate.sleep()

if __name__=='__main__':
    try:
        talker = Talker()
    except rospy.ROSInterruptException as e:
        print(e)

q를 눌러야 꺼짐

영상은 다음의 주소에서 다운받았다.

https://drive.google.com/file/d/1PQ-52DPjpY4kQPNLGwbK0TdIEgmZXCOo/view

#!/usr/bin/env python
import rospy
from sensor_msgs.msg import Image
from cv_bridge import CvBridge,CvBridgeError
import cv2 as cv

class Listener:
    def __init__(self):
        rospy.init_node('listener')
        self.sub = rospy.Subscriber('/topic', Image, self.callback)
        self.bridge = CvBridge()

    def callback(self,data):
        try:
            cv_image = self.bridge.imgmsg_to_cv2(data,'bgr8')
            self.perform(cv_image)
        except CvBridgeError as e:
            print(e)

    def perform(self,image):
        gray = cv.cvtColor(image, cv.COLOR_RGB2GRAY)
        cv.imshow('grat', gray)
        cv.waitKey(10)

if __name__=='__main__':
    listener = Listener()
    try:
        rospy.spin()
    except rospy.ROSInterruptException as e:
        print(e)

talker의 영상을 회색으로 돌릴수있음.

 

3. 가제보에서 제어해보기

https://www.youtube.com/watch?v=-qcsjbDsQTQ 

 

 

 

 

 

==============================================================================

유튜브 채널에서 동영상 자체를 비공개처리해버렸다..

728x90