How to control my Drone with OpenCV?



  • @robolink_arnold About the delay , this is my code. Today I try the code, it happened. Maybe 1/10 , the delay above 2000ms.
    from time import sleep
    from petrone.drone import *
    from petrone.protocol import *
    from petrone.system import *
    import cv2

    def eventUpdateInformation(data):
    print("eventUpdateInformation() / {0} / {1} / {2} / Ver:{3} / 20{4:02}.{5}.{6}".format(data.modeUpdate,
    data.deviceType,
    data.imageType, data.version,
    data.year, data.month,
    data.day))

    cap = cv2.VideoCapture('rtsp://192.168.100.1/cam1/mpeg4')
    #cap = cv2.VideoCapture(0)
    fourcc = cv2.VideoWriter_fourcc(*'XVID')
    out = cv2.VideoWriter('outputVideo.avi',fourcc, 20.0, (640,480))

    drone = Drone(True, True, True, True, True)

    drone.connect()
    sleep(1)

    drone.sendTakeOff()
    sleep(1)

    while (cap.isOpened()):

    ret, frame = cap.read()
    
    out.write(frame)
    
    cv2.imshow('frame', frame)
    
    key = cv2.waitKey(1) & 0xFF
    
    if key == ord("W"):
        drone.sendControl(0, 0, 0, 70)
        print("T 60")
    
    if key == ord("S"):
        drone.sendControl(0, 0, 0, -70)
        print("T -60")
    
    if key == ord("A"):
        drone.sendControl(0, 0, 50, 0)
        print("Y 100")
    
    if key == ord("D"):
        drone.sendControl(0, 0, -50, 0)
        print("Y -100")
    
    if key == ord("i"):
        drone.sendControl(0, 50, 0, 0)
        print("P 100")
    
    if key == ord("k"):
        drone.sendControl(0, -50, 0, 0)
        print("P -100")
    
    if key == ord("j"):
        drone.sendControl(-50, 0, 0, 0)
        print("R 100")
    
    if key == ord("l"):
        drone.sendControl(50, 0, 0, 0)
        print("R -100")
    
    if key == ord("z"):
        drone.sendControl(0,0,0,0)
        print("hover")
    sleep(0.05)
    
    if key == ord("q"):
        drone.sendLanding()
        print("LANDING")
        cap.release()
        cv2.destroyAllWindows()
        sleep(2)
        drone.close()
        print("CLOSE")
        break
    
    if key == ord("x"):
        drone.sendStop()
        print("STOP")
        cap.release()
        cv2.destroyAllWindows()
        sleep(2)
        drone.close()
        print("CLOSE")
        break
    

    print("Disconnecting device.")
    drone.sendLinkDisconnect()
    sleep(0.2)
    drone.close()

    cap.release()
    out.release()

    cv2.destroyAllWindows()


  • administrators

    @smilebean said in How to control my Drone with OpenCV?:

    this
    You may need to change the size at which you are saving the video at. First find out what the size of the output video is.


  • administrators

    @smilebean You should not be getting a delay of 2000ms. I know however if you send a bunch of commands one after another the CoDrone will keep a buffer of all the commands therefore doing things you told it before. When using the keys you should not hold them down but instead tap them.



  • @robolink_arnold
    Thank you! If I have any questions about openCV , i will ask for help.


Log in to reply
 

Looks like your connection to Robolink community was lost, please wait while we try to reconnect.