def main():
    pygame.init()

    cam = cv2.VideoCapture('tcp://192.168.1.1:5555')
    running2 = True
    W2, H2 = 320,240
    W, H = 640, 290
    screen = pygame.display.set_mode((W, H))
    drone = libardrone.ARDrone()
    clock = pygame.time.Clock()

    running = True
    while running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
            elif event.type == pygame.KEYUP:
                drone.hover()
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    drone.reset()
                    running = False
                # takeoff / land
                elif event.key == pygame.K_RETURN:
                    drone.takeoff()
                elif event.key == pygame.K_SPACE:
                    drone.land()
                # emergency
                elif event.key == pygame.K_BACKSPACE:
                    drone.reset()
                # forward / backward

        try:

            image_grid_array = []
            size_of_grid = 9
            grid_length = 3

            try:
                frame = None

                while running2:
                    # get current frame of video
                    running2, frame = cam.read()
                    if running2:
                        cv2.imshow('frame', frame)
                        image_grid_array.extend(crop(frame, size_of_grid))

                        Ps = 0
                        Pl = .5
                        Pr = .2

                        i = 1

                        canRead = write_to_filelock('droneRead.txt', "1", 'w')

                        for image in image_grid_array:

                            cv2.imwrite("test_img.jpeg", image)

                            canRead = 0
                            # Test for I/O file close permissions
                            while canRead == 0:

                                canRead = write_to_filelock('canRead.txt', "1", 'w')


                                # To run this in python 3.5 we will need to run a script that uses the "env"
                                # wrapper
                                # Once the pipeline is established, have the thread continuousely generate new samples
                                #
                                # can definitely recursion this size_of grid /2 + 1
                            canRead = 0

                            while (canRead == 0):
                                canRead = write_to_filelock('DroneRead.txt', "0", 'r')
                            canRead = 0

                            while (canRead == 0):
                                try:

                                    canRead = write_to_filelock('out.txt', "0", 'r')
                                    collision = float(canRead)
                                except:
                                    collision = 0
                                    print("COLLISION: -1")
                            canRead = 0
                            while (canRead == 0):
                                canRead = write_to_filelock('DroneRead.txt', "0", 'w')

                            print("Drone Read active")

                            if i % grid_length < (size_of_grid / 2 + 1):
                                Pl += collision
                            elif i % grid_length == (size_of_grid / 2 + 1):
                                Ps += collision
                            else:
                                Pr += collision

                        event = 1

                        print("we got to the good part: all images from grid read")
                        # Will need to comment out the other parts

                        # forward / backward
                        if Pr and Pl > Ps:

                            drone.move_forward()
                        else:
                            if Pr <= Pl:

                                drone.move_left()
                            else:
                                drone.move_right()
                                # turn left / turn right
                    else:
                        # error reading frame
                        print 'error reading video feed'
            except Exception as e:
                print(e)



            hud_color = (255, 0, 0) if drone.navdata.get('drone_state', dict()).get('emergency_mask', 1) else (10, 10, 255)
            bat = drone.navdata.get(0, dict()).get('battery', 0)
            f = pygame.font.Font(None, 20)
            hud = f.render('Battery: %i%%' % bat, True, hud_color)
            screen.blit(hud, (10, 10))
        except Exception as e:
            print(str(e))
            pass

        pygame.display.flip()
        clock.tick(50)
        pygame.display.set_caption("FPS: %.2f" % clock.get_fps())

    print "Shutting down...",
    drone.halt()
    print "Ok."
Esempio n. 2
0
This simple application allows to control the drone and see the drone's video
stream.
Copyright (c) 2011 Bastian Venthur
The license and distribution terms for this file may be
found in the file LICENSE in this distribution.
"""

import pygame

from pydrone import libardrone

if __name__ == '__main__':
    pygame.init()
    W, H = 320, 240
    screen = pygame.display.set_mode((W, H))
    drone = libardrone.ARDrone()
    clock = pygame.time.Clock()
    running = True
    while running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
            elif event.type == pygame.KEYUP:
                drone.hover()
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    drone.reset()
                    running = False
                # takeoff / land
                elif event.key == pygame.K_RETURN:
                    drone.takeoff()