示例#1
0
def run(CommandsQueue):

    move_distance = 30
    idle_sleep = 2  #sec
    video_ready_sleep = 7  #sec
    is_airborn = False
    global is_active

    tello = Tello()
    tello.connect()
    tello.set_speed(10)

    droneVid = threading.Thread(target=drone_video, args=(tello, ))
    droneVid.start()
    time.sleep(video_ready_sleep)

    while is_active:

        #execute next command from the que
        if not CommandsQueue.empty():
            command = CommandsQueue.get()
            timeStamp = str(datetime.datetime.now())
            print('Command for Drone: ' + str(command[0]) + ' at time ' +
                  timeStamp)

            if command[0] == Commands.up:
                if is_airborn:
                    tello.move_up(move_distance)
                else:
                    tello.takeoff()
                    is_airborn = True
            # elif command[0] == Commands.idle:
            #     time.sleep(idle_sleep)
            elif command[0] == Commands.up and is_airborn == True:
                tello.move_up(move_distance)
            elif command[0] == Commands.down and is_airborn == True:
                tello.move_down(move_distance)
            elif command[0] == Commands.forward and is_airborn == True:
                tello.move_forward(move_distance)
            elif command[0] == Commands.back and is_airborn == True:
                tello.move_back(move_distance)
            elif command[0] == Commands.left and is_airborn == True:
                tello.move_left(move_distance)
            elif command[0] == Commands.right and is_airborn == True:
                tello.move_right(move_distance)
            elif command[0] == Commands.flip and is_airborn == True:
                tello.flip_back()
            elif command[0] == Commands.stop:
                is_active = False
                break
        # else:
        #     time.sleep(idle_sleep)

    tello.land()
    is_airborn = False
    droneVid.join()
    tello.end()
class FrontEnd(object):
    """ Maintains the Tello display and moves it through the keyboard keys.
        Press escape key to quit.
        The controls are:
            - T: Takeoff
            - L: Land
            - Arrow keys: Forward, backward, left and right.
            - A and D: Counter clockwise and clockwise rotations (yaw)
            - W and S: Up and down.
    """
    def __init__(self):
        # Init pygame
        pygame.init()

        # Creat pygame window
        pygame.display.set_caption("Tello video stream")
        self.screen = pygame.display.set_mode([960, 720])

        # Init Tello object that interacts with the Tello drone
        self.tello = Tello()

        # Drone velocities between -100~100
        self.for_back_velocity = 0
        self.left_right_velocity = 0
        self.up_down_velocity = 0
        self.yaw_velocity = 0
        self.speed = 10

        self.send_rc_control = False

        # create update timer
        pygame.time.set_timer(pygame.USEREVENT + 1, 1000 // FPS)

    def control_manual(self):
        key = input('entraste en control manual')
        time.sleep(2)
        if key == '':
            print
            return
        else:
            if key == 'w':
                print('entraste al comando')
                self.tello.move_forward(30)
            elif key == 's':
                self.tello.move_down(30)
            elif key == 'a':
                self.tello.move_left(30)
            elif key == ord('d'):
                self.tello.move_right(30)
            elif key == ord('e'):
                self.tello.rotate_clockwise(30)
            elif key == ord('q'):
                self.tello.rotate_counter_clockwise(30)
            elif key == ord('r'):
                self.tello.move_up(30)
            elif key == ord('f'):
                self.tello.move_down(30)
        print('termino control manual')

    def run(self):
        self.tello.connect()
        self.tello.set_speed(self.speed)
        # In case streaming is on. This happens when we quit this program without the escape key.
        self.tello.streamoff()
        self.tello.streamon()
        #self.tello.takeoff()
        frame_read = self.tello.get_frame_read()

        should_stop = False
        while not should_stop:

            for event in pygame.event.get():
                if event.type == pygame.USEREVENT + 1:
                    self.update()
                elif event.type == pygame.QUIT:
                    should_stop = True
                elif event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_ESCAPE:
                        should_stop = True
                    else:
                        self.keydown(event.key)
                elif event.type == pygame.KEYUP:
                    self.keyup(event.key)

            if frame_read.stopped:
                break

            self.screen.fill([0, 0, 0])

            frame = frame_read.frame
            text = "Battery: {}%".format(self.tello.get_battery())
            cv2.putText(frame, text, (5, 720 - 5), cv2.FONT_HERSHEY_SIMPLEX, 1,
                        (0, 0, 255), 2)
            frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
            frame = np.rot90(frame)
            frame = np.flipud(frame)

            frame = pygame.surfarray.make_surface(frame)
            self.screen.blit(frame, (0, 0))
            pygame.display.update()
            #x=threading.Thread(target=self.control_manual)
            #x.start()
            #x.join()
            #time.sleep(1 / FPS)

        # Call it always before finishing. To deallocate resources.
        self.tello.end()

    def keydown(self, key):
        """ Update velocities based on key pressed
        Arguments:
            key: pygame key
        """
        if key == pygame.K_UP:  # set forward velocity
            self.for_back_velocity = S
        elif key == pygame.K_DOWN:  # set backward velocity
            self.for_back_velocity = -S
        elif key == pygame.K_LEFT:  # set left velocity
            self.left_right_velocity = -S
        elif key == pygame.K_RIGHT:  # set right velocity
            self.left_right_velocity = S
        elif key == pygame.K_w:  # set up velocity
            self.up_down_velocity = S
        elif key == pygame.K_s:  # set down velocity
            self.up_down_velocity = -S
        elif key == pygame.K_a:  # set yaw counter clockwise velocity
            self.yaw_velocity = -S
        elif key == pygame.K_d:  # set yaw clockwise velocity
            self.yaw_velocity = S

    def keyup(self, key):
        """ Update velocities based on key released
        Arguments:
            key: pygame key
        """
        if key == pygame.K_UP or key == pygame.K_DOWN:  # set zero forward/backward velocity
            self.for_back_velocity = 0
        elif key == pygame.K_LEFT or key == pygame.K_RIGHT:  # set zero left/right velocity
            self.left_right_velocity = 0
        elif key == pygame.K_w or key == pygame.K_s:  # set zero up/down velocity
            self.up_down_velocity = 0
        elif key == pygame.K_a or key == pygame.K_d:  # set zero yaw velocity
            self.yaw_velocity = 0
        elif key == pygame.K_t:  # takeoff
            self.tello.takeoff()
            self.send_rc_control = True
        elif key == pygame.K_l:  # land
            not self.tello.land()
            self.send_rc_control = False

    def update(self):
        """ Update routine. Send velocities to Tello."""
        if self.send_rc_control:
            self.tello.send_rc_control(self.left_right_velocity,
                                       self.for_back_velocity,
                                       self.up_down_velocity,
                                       self.yaw_velocity)
示例#3
0
def main():
    # init global vars
    global gesture_buffer
    global gesture_id
    global battery_status

    # Argument parsing
    args = get_args()
    KEYBOARD_CONTROL = args.is_keyboard
    WRITE_CONTROL = False
    in_flight = False

    # Camera preparation
    tello = Tello()
    #print(dir(tello))

    tello.connect()
    tello.set_speed(speed["slow"])
    print("\n\n" + tello.get_speed() + "\n\n")

    tello.streamon()

    cap_drone = tello.get_frame_read()
    cap_webcam = cv.VideoCapture(0)

    # Init Tello Controllers
    gesture_controller = TelloGestureController(tello)
    keyboard_controller = TelloKeyboardController(tello)

    gesture_detector = GestureRecognition(args.use_static_image_mode,
                                          args.min_detection_confidence,
                                          args.min_tracking_confidence)
    gesture_buffer = GestureBuffer(buffer_len=args.buffer_len)

    def tello_control(key, keyboard_controller, gesture_controller):
        global gesture_buffer

        if KEYBOARD_CONTROL:
            keyboard_controller.control(key)
        else:
            gesture_controller.gesture_control(gesture_buffer)

    def tello_battery(tello):
        global battery_status
        battery_status = tello.get_battery()

    # FPS Measurement
    cv_fps_calc = CvFpsCalc(buffer_len=10)

    mode = 0
    number = -1
    battery_status = -1

    tello.move_down(20)

    while True:
        fps = cv_fps_calc.get()

        # Process Key (ESC: end)
        key = cv.waitKey(1) & 0xff
        if key == 27:  # ESC
            break
        elif key == 32:  # Space
            if not in_flight:
                # Take-off drone
                tello.takeoff()
                in_flight = True

            elif in_flight:
                # Land tello
                tello.land()
                in_flight = False

        elif key == ord('k'):
            mode = 0
            KEYBOARD_CONTROL = True
            WRITE_CONTROL = False
            tello.send_rc_control(0, 0, 0, 0)  # Stop moving
        elif key == ord('g'):
            KEYBOARD_CONTROL = False
        elif key == ord('n'):
            mode = 1
            WRITE_CONTROL = True
            KEYBOARD_CONTROL = True

        if WRITE_CONTROL:
            number = -1
            if 48 <= key <= 57:  # 0 ~ 9
                number = key - 48

        # Camera capture
        image_drone = cap_drone.frame
        image = cap_webcam.read()[1]

        try:
            debug_image, gesture_id = gesture_detector.recognize(
                image, number, mode)
            gesture_buffer.add_gesture(gesture_id)

            # Start control thread
            threading.Thread(target=tello_control,
                             args=(
                                 key,
                                 keyboard_controller,
                                 gesture_controller,
                             )).start()
            threading.Thread(target=tello_battery, args=(tello, )).start()

            debug_image = gesture_detector.draw_info(debug_image, fps, mode,
                                                     number)

            battery_str_postion = (5, 100)  # dustin webcam
            #battery_str_postion = (5, 720 - 5) # drone camera

            # Battery status and image rendering
            cv.putText(debug_image, "Battery: {}".format(battery_status),
                       battery_str_postion, cv.FONT_HERSHEY_SIMPLEX, 1,
                       (0, 0, 255), 2)

            modeStr = "gestures"
            if KEYBOARD_CONTROL:
                modeStr = "keyboard"

            cv.putText(debug_image, modeStr + " control", (5, 150),
                       cv.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)

            cv.imshow('Webcam Gesture Recognition', debug_image)
            cv.imshow('Tello drone camera', image_drone)
        except:
            print("exception")

    tello.land()
    tello.end()
    cv.destroyAllWindows()
示例#4
0
class TelloCV(object):
    """
    TelloTracker builds keyboard controls on top of TelloPy as well
    as generating images from the video stream and enabling opencv support
    """

    def __init__(self):
        self.prev_flight_data = None
        self.record = False
        self.tracking = False
        self.keydown = False
        self.date_fmt = '%Y-%m-%d_%H%M%S'
        self.speed = 50
        self.go_speed = 80
        self.drone = Tello()
        self.init_drone()
        self.init_controls()

        self.battery = self.drone.get_battery()
        self.frame_read = self.drone.get_frame_read()
        self.forward_time = 0
        self.forward_flag = True
        self.takeoff_time = 0
        self.command_time = 0
        self.command_flag = False

        # trackingimport libh264decoder a color
        # green_lower = (30, 50, 50)
        # green_upper = (80, 255, 255)
        # red_lower = (0, 50, 50)
        # red_upper = (20, 255, 255)
        blue_lower = np.array([0, 0, 0])
        upper_blue = np.array([255, 255, 180])
        bh_lower = (180, 30, 100)
        bh_upper = (275, 50, 100)
        self.track_cmd = ""
        self.tracker = Tracker(960,
                               720,
                               blue_lower, upper_blue)
        self.speed_list = [5, 10, 15, 20, 25]
        self.frame_center = 480, 360
        self.error = 60

    def init_drone(self):
        """Connect, uneable streaming and subscribe to events"""
        # self.drone.log.set_level(2)
        self.drone.connect()
        self.drone.streamon()

    def on_press(self, keyname):
        """handler for keyboard listener"""
        if self.keydown:
            return
        try:
            self.keydown = True
            keyname = str(keyname).strip('\'')
            print('+' + keyname)
            if keyname == 'Key.esc':
                self.drone.quit()
                exit(0)
            if keyname in self.controls:
                key_handler = self.controls[keyname]
                if isinstance(key_handler, str):
                    getattr(self.drone, key_handler)(self.speed)
                else:
                    key_handler(self.speed)
        except AttributeError:
            print('special key {0} pressed'.format(keyname))

    def on_release(self, keyname):
        """Reset on key up from keyboard listener"""
        self.keydown = False
        keyname = str(keyname).strip('\'')
        print('-' + keyname)
        if keyname in self.controls:
            key_handler = self.controls[keyname]
            if isinstance(key_handler, str):
                getattr(self.drone, key_handler)(0)
            else:
                key_handler(0)

    def init_controls(self):
        """Define keys and add listener"""
        self.controls = {
            'w': lambda speed: self.drone.move_forward(speed),
            's': lambda speed: self.drone.move_back(speed),
            'a': lambda speed: self.drone.move_left(speed),
            'd': lambda speed: self.drone.move_right(speed),
            'Key.space': 'up',
            'Key.shift': 'down',
            'Key.shift_r': 'down',
            'q': 'counter_clockwise',
            'e': 'clockwise',
            'i': lambda speed: self.drone.flip_forward(),
            'k': lambda speed: self.drone.flip_back(),
            'j': lambda speed: self.drone.flip_left(),
            'l': lambda speed: self.drone.flip_right(),
            # arrow keys for fast turns and altitude adjustments
            'Key.left': lambda speed: self.drone.rotate_counter_clockwise(speed),
            'Key.right': lambda speed: self.drone.rotate_clockwise(speed),
            'Key.up': lambda speed: self.drone.move_up(speed),
            'Key.down': lambda speed: self.drone.move_down(speed),
            'Key.tab': lambda speed: self.drone.takeoff(),
            # 'Key.tab': self.drone.takeoff(60),
            'Key.backspace': lambda speed: self.drone.land(),
            'p': lambda speed: self.palm_land(speed),
            't': lambda speed: self.toggle_tracking(speed),
            'r': lambda speed: self.toggle_recording(speed),
            'z': lambda speed: self.toggle_zoom(speed),
            'Key.enter': lambda speed: self.take_picture(speed)
        }
        self.key_listener = keyboard.Listener(on_press=self.on_press,
                                              on_release=self.on_release)
        self.key_listener.start()
        # self.key_listener.join()

    def process_frame(self):
        """convert frame to cv2 image and show"""
        # print("TRACKING START")
        frame = self.frame_read.frame
        # self.drone.move_up(self.speed)
        # image = self.write_hud(image)
        # if self.record:
        #    self.record_vid(frame)
        return frame

    def move_up(self):
        self.drone.move_up(self.speed)

    def take_off(self):
        self.drone.takeoff()

    def go(self):
        self.drone.move_forward(self.go_speed)

    def move_left(self):
        self.drone.move_left(270)  # speed 테스트해서 조절하기

    def go_window9(self):
        self.drone.move_forward()

    def rotate_right(self):
        self.drone.rotate_clockwise()

    def rotate_left(self):
        self.drone.rotate_counter_clockwise()

    def landing(self):
        self.drone.land()

    def write_hud(self, frame):
        """Draw drone info, tracking and record on frame"""
        stats = self.prev_flight_data.split('|')
        stats.append("Tracking:" + str(self.tracking))
        if self.drone.zoom:
            stats.append("VID")
        else:
            stats.append("PIC")
        if self.record:
            diff = int(time.time() - self.start_time)
            mins, secs = divmod(diff, 60)
            stats.append("REC {:02d}:{:02d}".format(mins, secs))

        for idx, stat in enumerate(stats):
            text = stat.lstrip()
            cv2.putText(frame, text, (0, 30 + (idx * 30)),
                        cv2.FONT_HERSHEY_SIMPLEX,
                        1.0, (255, 0, 0), lineType=30)
        return frame

    def toggle_recording(self, speed):
        """Handle recording keypress, creates output stream and file"""
        if speed == 0:
            return
        self.record = not self.record

        if self.record:
            datename = [os.getenv('HOME'), datetime.datetime.now().strftime(self.date_fmt)]
            self.out_name = '{}/Pictures/tello-{}.mp4'.format(*datename)
            print("Outputting video to:", self.out_name)
            self.out_file = av.open(self.out_name, 'w')
            self.start_time = time.time()
            self.out_stream = self.out_file.add_stream(
                'mpeg4', self.vid_stream.rate)
            self.out_stream.pix_fmt = 'yuv420p'
            self.out_stream.width = self.vid_stream.width
            self.out_stream.height = self.vid_stream.height

        if not self.record:
            print("Video saved to ", self.out_name)
            self.out_file.close()
            self.out_stream = None

    def record_vid(self, frame):
        """
        convert frames to packets and write to file
        """
        new_frame = av.VideoFrame(
            width=frame.width, height=frame.height, format=frame.format.name)
        for i in range(len(frame.planes)):
            new_frame.planes[i].update(frame.planes[i])
        pkt = None
        try:
            pkt = self.out_stream.encode(new_frame)
        except IOError as err:
            print("encoding failed: {0}".format(err))
        if pkt is not None:
            try:
                self.out_file.mux(pkt)
            except IOError:
                print('mux failed: ' + str(pkt))

    def take_picture(self, speed):
        """Tell drone to take picture, image sent to file handler"""
        if speed == 0:
            return
        self.drone.take_picture()

    def palm_land(self, speed):
        """Tell drone to land"""
        if speed == 0:
            return
        self.drone.palm_land()

    def toggle_tracking(self, speed):
        """ Handle tracking keypress"""
        if speed == 0:  # handle key up event
            return
        self.tracking = not self.tracking
        print("tracking:", self.tracking)
        return

    def toggle_zoom(self, speed):
        """
        In "video" mode the self.drone sends 1280x720 frames.
        In "photo" mode it sends 2592x1936 (952x720) frames.
        The video will always be centered in the window.
        In photo mode, if we keep the window at 1280x720 that gives us ~160px on
        each side for status information, which is ample.
        Video mode is harder because then we need to abandon the 16:9 display size
        if we want to put the HUD next to the video.
        """
        if speed == 0:
            return
        self.drone.set_video_mode(not self.drone.zoom)

    def flight_data_handler(self, event, sender, data):
        """Listener to flight data from the drone."""
        text = str(data)
        if self.prev_flight_data != text:
            self.prev_flight_data = text

    def handle_flight_received(self, event, sender, data):
        """Create a file in ~/Pictures/ to receive image from the drone"""
        path = '%s/Pictures/tello-%s.jpeg' % (
            os.getenv('HOME'),
            datetime.datetime.now().strftime(self.date_fmt))
        with open(path, 'wb') as out_file:
            out_file.write(data)
        print('Saved photo to %s' % path)

    def enable_mission_pads(self):
        self.drone.enable_mission_pads()

    def disable_mission_pads(self):
        self.drone.disable_mission_pads()

    def go_xyz_speed_mid(self, x, y, z, speed, mid):
        self.drone.go_xyz_speed_mid(x, y, z, speed, mid)

    #  if function return True, set drone center to object's center
    def track_mid(self, x, y):
        midx, midy = 480, 360
        distance_x = abs(midx - x)
        distance_y = abs(midy - y)
        print(x, y, distance_x, distance_y)
        move_done = True
        if y > midy + self.error + 15:
            self.drone.move_down(20)
            move_done = False
        elif y < midy - self.error + 5:
            self.drone.move_up(20)
            move_done = False
        elif x < midx - self.error:
            self.drone.move_left(20)
            move_done = False
        elif x > midx + self.error:
            self.drone.move_right(20)
            move_done = False
        return move_done

    def track_x(self, x, left_count, right_count):
        midx = 480
        move_done = True
        if x < midx - 100:
            self.drone.move_left(20)
            left_count += 1
            move_done = False
        elif x > midx + 100:
            self.drone.move_right(20)
            right_count += 1
            move_done = False
        return move_done


    def go_slow(self):
        self.drone.move_forward(30)

    def go_fast(self):
        self.drone.move_forward(200)
class DroneObject:

    #initialize the object
    #Set default state to IdleState()
    #Create Tello object from the API
    #set default values for coordinate, FPS, distance, and tilt
    def __init__(self):
        self.state = IdleState()
        self.tello = Tello()
        self.coordinate = (0, 0)
        self.FPS = 30
        self.distance = 30 #pre defined, to be changed later
        self.tilt = 0

    #Generates pass events to tellodrone class
    def on_event(self, event):

        self.state = self.state.on_event(event)
    
    #setter for member variables
    def set_parameter (self, x,y, dist, tilt):
        self.coordinate = (x,y)
        self.distance = dist
        self.tilt = tilt

    #for take off the drone, called when the drone is in takeoff state
    #when take off is complete, trigger event for track
    def take_off(self):
        self.tello.takeoff()
        for i in range (0,3):
            print("taking off %d /3" % (i+1))
            time.sleep(1)
        self.on_event("track")

        
    #for landing the drone, called when the drone is in landingstate
    #when landing is complete, trigger event for idle

    def land(self):
        self.tello.land()
        for i in range (0,3):
            print("landing %d / 3" % (i+1))
            time.sleep(1)
        self.on_event("idle")

    #for tracking the drone, called when the drone is in track state
    #when tilt is active, it will prioritize the turning motion over the other motions
    #controls shifting, moving up and down, forward backwards, and turning
    def track(self):
        if(self.tilt <= 0.95 and self.tilt != 0):
            self.tello.rotate_clockwise(int((1-self.tilt)*100))
            time.sleep(0.05)
        elif(self.tilt >= 1.05):
            self.tello.rotate_counter_clockwise(int((self.tilt-1)*100))
            time.sleep(0.05)
        else:
            if (self.distance > 60):
                forward = int((self.distance - 60))
                if ((forward < 20)):
                    self.tello.move_forward(20)
                else:
                    self.tello.move_forward(forward)
                time.sleep(0.05)
            elif (self.distance < 50):
                backward = int(abs(self.distance - 50))
                if ((backward < 20)):
                    self.tello.move_back(20)
                else:
                    self.tello.move_back(backward)
                time.sleep(0.05)

            if (self.coordinate[0] < 400 and self.coordinate[0] >= 0):
                self.tello.move_left(20)
                time.sleep(0.05)

            elif (self.coordinate[0] < 959 and self.coordinate[0] >= 559):
                self.tello.move_right(20)
                time.sleep(0.05)

            if (self.coordinate[1] > 0 and self.coordinate[1] <= 200):
                self.tello.move_up(20)
                time.sleep(0.05)
            elif (self.coordinate[1] >= 519 and self.coordinate[1] < 719):
                self.tello.move_down(20)
                time.sleep(0.05)
    
    #For setting up the drone
        #Establish connection
        #Initialize streamming
        #Display battery
    def setup(self):
        if not self.tello.connect():
            print("Tello not connected")
            sys.exit()

        # In case streaming is on. This happens when we quit this program without the escape key.
        if not self.tello.streamoff():
            print("Could not stop video stream")
            sys.exit()

        if not self.tello.streamon():
            print("Could not start video stream")
            sys.exit()

        print("Current battery is " + self.tello.get_battery())
        self.tello.streamon()
        frame_read = self.tello.get_frame_read()

        
    #For calling corresponding functions based on their state
    def action(self):
        print("current state is" , self.state)
        print(str(self.state))
        if (str(self.state) == "TakeOffState"):
            print("take off!")
            self.take_off()
        elif (str(self.state) == "LandState"):
            print("land")
            self.land()
        if (str(self.state)== "TrackState"):
            self.track()

        else:
            return #idle state or undefined state do nothing
        return
示例#6
0
class hand_tello_control:
    def __init__(self):
        self.action = " "
        self.mp_drawing = mp.solutions.drawing_utils
        self.mp_drawing_styles = mp.solutions.drawing_styles
        self.mp_hands = mp.solutions.hands
        self.action_done = True
        self.ffoward = 1
        self.fback = 1
        self.fright = 1
        self.fleft = 1
        self.fsquare = 1
        self.fmiddle = 1
        self.fno = 1
        self.fland = 1

    def tello_startup(self):
        # For Tello input:
        self.tello = Tello()  # Starts the tello object
        self.tello.connect()  # Connects to the drone

    def define_orientation(self, results):

        if results.multi_hand_landmarks[0].landmark[
                4].x < results.multi_hand_landmarks[0].landmark[17].x:
            orientation = "right hand"
        else:
            orientation = "left hand"
        return orientation

    def follow_hand(self, results):
        normalizedLandmark = results.multi_hand_landmarks[0].landmark[
            9]  # Normalizes the lowest middle-finger coordinate for hand tracking
        pixelCoordinatesLandmark = self.mp_drawing._normalized_to_pixel_coordinates(
            normalizedLandmark.x, normalizedLandmark.y, 255, 255
        )  #Tracks the coordinates of the same landmark in a 255x255 grid
        print(pixelCoordinatesLandmark)

        if pixelCoordinatesLandmark == None:  # If hand goes out of frame, stop following
            self.tello.send_rc_control(0, 0, 0, 0)
            return

        centerRange = [12,
                       12]  #Range for detecting commands in the x and y axis.
        centerPoint = [128, 128]  #Theoretical center of the image
        xCorrection = pixelCoordinatesLandmark[0] - centerPoint[0]
        yCorrection = pixelCoordinatesLandmark[1] - centerPoint[1]
        xSpeed = 0
        ySpeed = 0

        if xCorrection > centerRange[0] or xCorrection < -centerRange[
                0]:  #If the hand is outside the acceptable range, changes the current speed to compensate.
            xSpeed = xCorrection // 3
        if yCorrection > centerRange[1] or yCorrection < -centerRange[1]:
            ySpeed = -yCorrection // 3

        self.tello.send_rc_control(xSpeed, 0, ySpeed, 0)
        time.sleep(0.5)
        self.tello.send_rc_control(0, 0, 0, 0)

    def action_to_do(
            self, fingers, orientation,
            results):  #use the variable results for the hand tracking control

        if self.action_done == True:
            self.ffoward = 1
            self.fback = 1
            self.fright = 1
            self.fleft = 1
            self.fsquare = 1
            self.fmiddle = 1
            self.fno = 1
            self.fland = 1
            self.action_done = False
        #Left hand controls tricks, right hand controls movement
        if orientation == "left hand":  #Thumb on the left = left hand!
            if fingers == [0, 1, 0, 0, 0]:
                if self.ffoward >= 15:
                    self.action = "flip forward"
                    self.tello.flip_forward()
                    self.action_done = True
                self.ffoward = self.ffoward + 1
            elif fingers == [0, 1, 1, 0, 0] and self.battery == True:
                if self.fback >= 15:
                    self.action = "flip back"
                    self.tello.flip_back()
                    self.action_done = True
                self.fback = self.fback + 1
            elif fingers == [1, 0, 0, 0, 0] and self.battery == True:
                if self.fright >= 15:
                    self.action = "flip right"
                    self.tello.flip_right()
                    self.action_done = True
                self.fright = self.fright + 1
            elif fingers == [0, 0, 0, 0, 1] and self.battery == True:
                if self.fleft >= 15:
                    self.action = "flip left"
                    self.tello.flip_left()
                    self.action_done = True
                self.fleft = self.fleft + 1
            elif fingers == [0, 1, 1, 1, 0]:
                if self.fsquare >= 15:
                    self.action = "Square"
                    self.tello.move_left(20)
                    self.tello.move_up(40)
                    self.tello.move_right(40)
                    self.tello.move_down(40)
                    self.tello.move_left(20)
                    self.action_done = True
                self.fsquare = self.fsquare + 1
            elif fingers == [0, 0, 1, 0, 0]:
                if self.fmiddle >= 15:
                    self.action = " :( "
                    self.tello.land()
                    self.action_done = True
                self.fmiddle = self.fmiddle + 1
            elif ((self.battery == False) and
                  (fingers == [1, 0, 0, 0, 0] or fingers == [0, 1, 0, 0, 0]
                   or fingers == [0, 0, 0, 0, 1])):  #not avaiable to do tricks
                if self.fno >= 15:
                    self.tello.rotate_clockwise(45)
                    self.tello.rotate_counter_clockwise(90)
                    self.tello.rotate_clockwise(45)
                    self.action_done = True
                self.fno = self.fno + 1
            else:
                self.action = " "

        elif orientation == "right hand":  #Thumb on the right = right hand!
            if fingers == [1, 1, 1, 1, 1]:
                self.action = "Follow"
                self.follow_hand(results)
            elif fingers == [1, 0, 0, 0, 0]:
                if self.fland >= 15:
                    self.action = "Land"
                    self.tello.land()
                    self.action_done = True
                self.fland = self.fland + 1
            else:
                self.action = " "

    def fingers_position(self, results, orientation):

        # [thumb, index, middle finger, ring finger, pinky]
        # 0 for closed, 1 for open
        fingers = [0, 0, 0, 0, 0]

        if (results.multi_hand_landmarks[0].landmark[4].x >
                results.multi_hand_landmarks[0].landmark[3].x
            ) and orientation == "right hand":
            fingers[0] = 0
        if (results.multi_hand_landmarks[0].landmark[4].x <
                results.multi_hand_landmarks[0].landmark[3].x
            ) and orientation == "right hand":
            fingers[0] = 1

        if (results.multi_hand_landmarks[0].landmark[4].x >
                results.multi_hand_landmarks[0].landmark[3].x
            ) and orientation == "left hand":
            fingers[0] = 1
        if (results.multi_hand_landmarks[0].landmark[4].x <
                results.multi_hand_landmarks[0].landmark[3].x
            ) and orientation == "left hand":
            fingers[0] = 0

        fingermarkList = [8, 12, 16, 20]
        i = 1
        for k in fingermarkList:
            if results.multi_hand_landmarks[0].landmark[
                    k].y > results.multi_hand_landmarks[0].landmark[k - 2].y:
                fingers[i] = 0
            else:
                fingers[i] = 1

            i = i + 1

        return fingers

    def detection_loop(self):
        with self.mp_hands.Hands(model_complexity=0,
                                 min_detection_confidence=0.75,
                                 min_tracking_confidence=0.5) as hands:
            self.tello.streamoff(
            )  # Ends the current stream, in case it's still opened
            self.tello.streamon()  # Starts a new stream
            while True:
                frame_read = self.tello.get_frame_read(
                )  # Stores the current streamed frame
                image = frame_read.frame
                self.battery = self.tello.get_battery()

                # To improve performance, optionally mark the image as not writeable to
                # pass by reference.
                image.flags.writeable = True
                image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
                results = hands.process(image)

                # Draw the hand annotations on the image.
                image.flags.writeable = True
                image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)

                if results.multi_hand_landmarks:
                    action = " "
                    for hand_landmarks in results.multi_hand_landmarks:
                        self.mp_drawing.draw_landmarks(
                            image, hand_landmarks,
                            self.mp_hands.HAND_CONNECTIONS,
                            self.mp_drawing_styles.
                            get_default_hand_landmarks_style(),
                            self.mp_drawing_styles.
                            get_default_hand_connections_style())

                    orientation = self.define_orientation(results)
                    fingers = self.fingers_position(results, orientation)
                    #print(fingers)
                    self.action_to_do(fingers, orientation, results)

                for event in pg.event.get():
                    if event.type == pg.KEYDOWN:
                        if event.key == pg.K_l:
                            self.tello.land()
                        if event.key == pg.K_t:
                            self.tello.takeoff()
                        if event.key == pg.K_b:
                            print("A bateria esta em ",
                                  self.tello.get_battery(), "%")
                        if event.key == pg.K_m:
                            return 0

                cv2.imshow("image", image)
                if cv2.waitKey(5) & 0xFF == 27:
                    break

    def key_control(self):

        self.tello.streamoff(
        )  # Ends the current stream, in case it's still opened
        self.tello.streamon()  # Starts a new stream
        while True:
            frame_read = self.tello.get_frame_read(
            )  # Stores the current streamed frame
            image = frame_read.frame

            # To improve performance, optionally mark the image as not writeable to
            # pass by reference.
            image.flags.writeable = True
            image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

            # Draw the hand annotations on the image.
            image.flags.writeable = True
            image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)

            for event in pg.event.get():
                if event.type == pg.KEYDOWN:
                    if event.key == pg.K_w:
                        self.tello.move_forward(20)
                    if event.key == pg.K_a:
                        self.tello.move_left(20)
                    if event.key == pg.K_s:
                        self.tello.move_back(20)
                    if event.key == pg.K_d:
                        self.tello.move_right(20)
                    if event.key == pg.K_q:
                        self.tello.rotate_counter_clockwise(20)
                    if event.key == pg.K_e:
                        self.tello.rotate_clockwise(20)
                    if event.key == pg.K_SPACE:
                        self.tello.move_up(20)
                    if event.key == pg.K_LCTRL:
                        self.tello.move_down(20)
                    if event.key == pg.K_b:
                        print("A bateria esta em ", self.tello.get_battery(),
                              "%")
                    if event.key == pg.K_m:
                        return 0

            cv2.imshow("image", image)
            if cv2.waitKey(5) & 0xFF == 27:
                break

    def main_interface(self):
        telloMode = -1
        self.tello_startup()
        pg.init()
        win = pg.display.set_mode((500, 500))
        pg.display.set_caption("Test")
        #self.tello.takeoff()

        print("Para controlar pelo teclado, digite 1")
        print("Para controlar com a mao, digite 2")
        print("Para sair, digite 0")

        self.tello.streamoff(
        )  # Ends the current stream, in case it's still opened
        self.tello.streamon()  # Starts a new stream
        while telloMode != 0:
            frame_read = self.tello.get_frame_read(
            )  # Stores the current streamed frame
            image = frame_read.frame

            # To improve performance, optionally mark the image as not writeable to
            # pass by reference.
            image.flags.writeable = True
            image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

            # Draw the hand annotations on the image.
            image.flags.writeable = True
            image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)

            cv2.imshow("image", image)
            if cv2.waitKey(5) & 0xFF == 27:
                break

            if telloMode == 1:
                self.key_control()
                telloMode = -1
                print("Para controlar pelo teclado, digite 1")
                print("Para controlar com a mao, digite 2")
                print("Para sair, digite 0")
            elif telloMode == 2:
                self.detection_loop()
                telloMode = -1
                print("Para controlar pelo teclado, digite 1")
                print("Para controlar com a mao, digite 2")
                print("Para sair, digite 0")
            elif telloMode == 0:
                self.tello.land()
                telloMode = -1
                print("Obrigado por voar hoje")
            elif telloMode != -1 and telloMode != 1 and telloMode != 2:
                print("valor invalido!")
            for event in pg.event.get():
                if event.type == pg.KEYDOWN:
                    if event.key == pg.K_1:
                        telloMode = 1
                    if event.key == pg.K_2:
                        telloMode = 2
                    if event.key == pg.K_0:
                        telloMode = 0
                    if event.key == pg.K_l:
                        self.tello.land()
                    if event.key == pg.K_t:
                        self.tello.takeoff()
                    if event.key == pg.K_b:
                        print("A bateria esta em ", self.tello.get_battery(),
                              "%")
示例#7
0
drone.takeoff()
drone.move_up(6)

# Go east (forward) 5 feet and turn north (left)
# stay at 6 feet
drone.move_forward(5)
drone.rotate_counter_clockwise(90)

# Go north (forward) 6 feet and turn east (right)
# stay at 6 feet
drone.move_forward(6)
drone.rotate_clockwise(90)

# drop to 3 feet
# go east (forward) 3 and turn south (right)
drone.move_down(3)
drone.move_forward(3)
drone.rotate_clockwise(90)

# go up to 4 feet
# go south (forward) 3 and turn east (left)
drone.move_up(1)
drone.move_forward(3)
drone.rotate_counter_clockwise(90)

# stay at 4 feet
# go east (forward) 6 feet
drone.move_forward(6)

# Land the drone
drone.land()
示例#8
0
            tello.move_back(-command[1])
    elif command[0] == "h":
        if command[1] > 0:
            tello.move_right(command[1])
        else:
            tello.move_left(-command[1])
    elif command[0] == "r":
        command[1] == command[1] % 360
        while command[1] > 180:
            command[1] = -360 + command[1]
        if command[1] > 0:
            tello.rotate_clockwise(command[1])
        else:
            tello.rotate_counter_clockwise(-command[1])
    elif command[0] == "c":
        tello.move_down(50)
        time.sleep(5)
        maskP = 0
        checking = True
        time.sleep(2)
        if maskP < 0:
            print("No mask!!!")
            file = open(r"trig2.txt","w")
            file.write(command[1])
            file.close()
        checking = False
        tello.move_up(50)
        time.sleep(5)


# time.sleep(5)
示例#9
0
def main():
    mp_drawing = mp.solutions.drawing_utils
    mp_hands = mp.solutions.hands

    # Argument parsing
    in_flight = False

    # For webcam input:
    hands = mp_hands.Hands(static_image_mode=True,
                           max_num_hands=2,
                           min_detection_confidence=0.8,
                           min_tracking_confidence=0.8)

    # Camera preparation
    tello = Tello()
    tello.connect()
    tello.streamon()

    # FPS Measurement
    cv_fps_calc = CvFpsCalc(buffer_len=5)

    frame_read = tello.get_frame_read()

    while True:
        fps = cv_fps_calc.get()
        battery_status = tello.get_battery()

        # In reality you want to display frames in a seperate thread. Otherwise
        #  they will freeze while the drone moves.
        image = frame_read.frame

        # Flip the image horizontally for a later selfie-view display, and convert
        # the BGR image to RGB.
        image = cv2.cvtColor(cv2.flip(image, 1), cv2.COLOR_BGR2RGB)

        # To improve performance, optionally mark the image as not writeable to
        # pass by reference.
        image.flags.writeable = False
        results = hands.process(image)

        # Draw the hand annotations on the image.
        image.flags.writeable = True
        image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
        if results.multi_hand_landmarks:
            for hand_landmarks in results.multi_hand_landmarks:
                mp_drawing.draw_landmarks(image, hand_landmarks,
                                          mp_hands.HAND_CONNECTIONS)

        # Process Key (ESC: end)
        key = cv2.waitKey(1) & 0xff
        if key == 27:  # ESCArgument parsing
            tello.land()
            break
        elif key == 32:
            if not in_flight:
                # Take-off drone
                tello.takeoff()
                in_flight = True
                tello.move_up(50)
            elif in_flight:
                # Land tello
                tello.land()
                in_flight = False
        elif key == ord('w'):
            tello.move_forward(30)
        elif key == ord('s'):
            tello.move_back(30)
        elif key == ord('a'):
            tello.move_left(30)
        elif key == ord('d'):
            tello.move_right(30)
        elif key == ord('e'):
            tello.rotate_clockwise(30)
        elif key == ord('q'):
            tello.rotate_counter_clockwise(30)
        elif key == ord('r'):
            tello.move_up(30)
        elif key == ord('f'):
            tello.move_down(30)

        # Battery status and image rendering
        cv2.putText(image, "Battery: {} / fps:{}".format(battery_status, fps),
                    (5, 720 - 5), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
        cv2.imshow('DK_Drone', image)

    tello.land()
    tello.end()
    cv2.destroyAllWindows()
示例#10
0
def main():
    tello = Tello()
    tello.connect()

    vk_session = vk_api.VkApi(token='TOKEN')
    vk = vk_session.get_api()

    letsgo = VkKeyboard(one_time=True)
    letsgo.add_button('Начать', color=VkKeyboardColor.DEFAULT)

    keyboard_takeoff = VkKeyboard(one_time=True)
    keyboard_takeoff.add_button('Взлёт', color=VkKeyboardColor.POSITIVE)

    keyboard = VkKeyboard(one_time=True)
    keyboard.add_button('Вверх', color=VkKeyboardColor.PRIMARY)
    keyboard.add_button('Вниз', color=VkKeyboardColor.PRIMARY)
    keyboard.add_line()
    keyboard.add_button('Вперёд', color=VkKeyboardColor.PRIMARY)
    keyboard.add_line()
    keyboard.add_button('Влево', color=VkKeyboardColor.PRIMARY)
    keyboard.add_button('Флип', color=VkKeyboardColor.NEGATIVE)
    keyboard.add_button('Вправо', color=VkKeyboardColor.PRIMARY)
    keyboard.add_line()
    keyboard.add_button('Назад', color=VkKeyboardColor.PRIMARY)
    keyboard.add_line()
    keyboard.add_button('Проверка', color=VkKeyboardColor.DEFAULT)
    keyboard.add_line()
    keyboard.add_button('Посадка', color=VkKeyboardColor.NEGATIVE)

    flip_keyboard = VkKeyboard(one_time=False)
    flip_keyboard.add_button('Флип вперёд', color=VkKeyboardColor.DEFAULT)
    flip_keyboard.add_line()
    flip_keyboard.add_button('Флип влево', color=VkKeyboardColor.DEFAULT)
    flip_keyboard.add_button('Флип вправо', color=VkKeyboardColor.DEFAULT)
    flip_keyboard.add_line()
    flip_keyboard.add_button('Флип назад', color=VkKeyboardColor.DEFAULT)
    flip_keyboard.add_line()
    flip_keyboard.add_button('Доступность флипа',
                             color=VkKeyboardColor.NEGATIVE)
    flip_keyboard.add_line()
    flip_keyboard.add_button('Вернуться в меню управления',
                             color=VkKeyboardColor.NEGATIVE)

    mainmenu_keyboard = VkKeyboard(one_time=True)
    mainmenu_keyboard.add_button('Начать', color=VkKeyboardColor.DEFAULT)
    mainmenu_keyboard.add_line()
    mainmenu_keyboard.add_button('Конец работы', color=VkKeyboardColor.DEFAULT)

    longpoll = VkLongPoll(vk_session)

    for event in longpoll.listen():
        if event.type == VkEventType.MESSAGE_NEW and event.to_me and event.text:
            if event.text.lower() == 'флип':
                vk.messages.send(  # Отправляем сообщение
                    user_id=event.user_id,
                    keyboard=flip_keyboard.get_keyboard(),
                    message='Переход в меню флипов')
            if event.text.lower() == 'начать':
                vk.messages.send(  # Отправляем сообщение
                    user_id=event.user_id,
                    keyboard=keyboard_takeoff.get_keyboard(),
                    message='Взлет?')
            if event.text.lower() == 'конец работы':
                vk.messages.send(  # Отправляем сообщение
                    user_id=event.user_id,
                    message='Конец работы')
            if event.text.lower() == 'взлёт':
                vk.messages.send(  # Отправляем сообщение
                    user_id=event.user_id,
                    keyboard=keyboard.get_keyboard(),
                    message='Куда летим?')
                tello.takeoff()
                print('Взлёт')
            if event.text.lower() == 'посадка':
                vk.messages.send(  # Отправляем сообщение
                    user_id=event.user_id,
                    keyboard=mainmenu_keyboard.get_keyboard(),
                    message='Посадка')
                tello.land()
                print('Посадка')
            if event.text.lower() == 'вперёд':
                vk.messages.send(  # Отправляем сообщение
                    user_id=event.user_id,
                    keyboard=keyboard.get_keyboard(),
                    message='Полёт вперёд')
                tello.move_forward(100)
                print('Вперёд')
            if event.text.lower() == 'влево':
                vk.messages.send(  # Отправляем сообщение
                    user_id=event.user_id,
                    keyboard=keyboard.get_keyboard(),
                    message='Полёт влево')
                tello.move_left(100)
                print('Влево')
            if event.text.lower() == 'вправо':
                vk.messages.send(  # Отправляем сообщение
                    user_id=event.user_id,
                    keyboard=keyboard.get_keyboard(),
                    message='Полёт вправо')
                tello.move_right(100)
                print('Вправо')
            if event.text.lower() == 'назад':
                vk.messages.send(  # Отправляем сообщение
                    user_id=event.user_id,
                    keyboard=keyboard.get_keyboard(),
                    message='Полёт назад')
                tello.move_back(100)
                print('Назад')
            if event.text.lower() == 'проверка':
                x = tello.get_battery()
                vk.messages.send(  # Отправляем сообщение
                    user_id=event.user_id,
                    keyboard=keyboard.get_keyboard(),
                    message='Заряд баттареи ' + x)
                print('Проверка')
            if event.text.lower() == 'вверх':
                vk.messages.send(  # Отправляем сообщение
                    user_id=event.user_id,
                    keyboard=keyboard.get_keyboard(),
                    message='Полёт вверх')
                tello.move_up(20)
                print('Вверх')
            if event.text.lower() == 'вниз':
                vk.messages.send(  # Отправляем сообщение
                    user_id=event.user_id,
                    keyboard=keyboard.get_keyboard(),
                    message='Полёт вниз')
                tello.move_down(20)
                print('Вниз')
            if event.text.lower() == 'флип назад':
                vk.messages.send(  # Отправляем сообщение
                    user_id=event.user_id,
                    keyboard=flip_keyboard.get_keyboard(),
                    message='Флип назад')
                tello.flip('b')
                print('Флип назад')
            if event.text.lower() == 'флип вперёд':
                vk.messages.send(  # Отправляем сообщение
                    user_id=event.user_id,
                    keyboard=flip_keyboard.get_keyboard(),
                    message='Флип вперёд')
                tello.flip('f')
                print('Флип вперёд')
            if event.text.lower() == 'флип влево':
                vk.messages.send(  # Отправляем сообщение
                    user_id=event.user_id,
                    keyboard=flip_keyboard.get_keyboard(),
                    message='Флип влево')
                tello.flip('l')
                print('Флип влево')
            if event.text.lower() == 'флип вправо':
                vk.messages.send(  # Отправляем сообщение
                    user_id=event.user_id,
                    keyboard=flip_keyboard.get_keyboard(),
                    message='Флип вправо')
                tello.flip('r')
                print('Флип вправо')
            if event.text.lower() == 'доступность флипа':
                x = tello.get_battery()
                if x == 'ok':
                    vk.messages.send(user_id=event.user_id,
                                     keyboard=flip_keyboard.get_keyboard(),
                                     message='Нажмите ещё раз')
                else:
                    if int(x) > 50:
                        vk.messages.send(user_id=event.user_id,
                                         keyboard=flip_keyboard.get_keyboard(),
                                         message='Заряд баттареи ' + x +
                                         'Флип доступен')
                    else:
                        vk.messages.send(user_id=event.user_id,
                                         keyboard=flip_keyboard.get_keyboard(),
                                         message='Заряд баттареи ' + x +
                                         'Флип недоступен')
            if event.text.lower() == 'вернуться в меню управления':
                vk.messages.send(  # Отправляем сообщение
                    user_id=event.user_id,
                    keyboard=keyboard.get_keyboard(),
                    message='Возврат в меню управления')
                print('Возврат в меню управления')
    tello.end()
示例#11
0
camera = threading.Thread(target=cam, args=())
camera.deamon = True
# threding scanning
scanning = threading.Thread(target=scan, args=())
scanning.deamon = True

time.sleep(3)
camera.start()
scanning.start()
time.sleep(1)

while True:
    me.takeoff()
    time.sleep(6)

    me.move_down(25)
    time.sleep(6)
    me.move_up(25)
    time.sleep(6)
    me.move_up(40)
    time.sleep(6)
    me.move_up(50)
    time.sleep(6)
    me.rotate_clockwise(180)
    time.sleep(6)
    me.move_down(50)
    time.sleep(6)
    me.move_down(40)
    time.sleep(6)
    me.move_down(30)
    time.sleep(6)
class DroneControl():
    def __init__(self):
        self.drone = Tello()  # Instantiate a Tello Object
        self.drone.connect()  # Connect to the drone
        self.drone.streamoff()  # In case stream never exited before
        self.drone.streamon()  # Turn on drone camera stream
        self.timer = 0  # Timing for printing statements
        self.flying = False  # Keep track of flying state
        # How many video frames have been requested
        self.frame_count = 0
        self.init_frames = 50  # Begin flying only after init_frames
        self.myThread = threading.Thread(target=self.fly)

    def __del__(self):
        self.drone.streamoff()

    def get_state(self):
        self.drone.get_battery()
        self.drone.get_speed_x
        self.drone.get_speed_y
        self.drone.get_speed_z
        self.drone.get_height
        self.drone.get_flight_time
        self.drone.get_temperature
        self.drone.get_yaw
        self.drone.get_roll
        self.drone.get_pitch
        self.drone.get_barometer
        self.drone.get_acceleration_x
        self.drone.get_acceleration_y
        self.drone.get_acceleration_z
        self.drone.get_distance_tof
        self.drone.query_wifi_signal_noise_ratio
        ####################################################
        ############## ADD YOUR CODE HERE ##################
        ####################################################

    def fly(self):
        #time.sleep(5)
        #self.get_state()
        #time.sleep(5)
        #self.drone.takeoff()
        #time.sleep(5)
        #self.drone.move_left(50)
        #time.sleep(5)
        #self.drone.rotate_clockwise(90)
        #time.sleep(5)
        #self.drone.rotate_clockwise(180)
        #time.sleep(5)
        #self.drone.land()
        #################################################
        ##################Challenge 1####################
        #################################################
        time.sleep(5)
        self.get_state()
        time.sleep(5)
        self.drone.takeoff()
        time.sleep(5)
        self.drone.move_left(50)
        time.sleep(5)
        self.drone.move_back(50)
        time.sleep(5)
        self.drone.move_right(50)
        time.sleep(5)
        self.drone.move_forward(50)
        time.sleep(5)
        self.drone.land()
        #################################################
        ###################Challenge 2###################
        #################################################
        time.sleep(5)
        self.drone.takeoff()
        time.sleep(10)
        self.drone.move_up(40)
        time.sleep(5)
        self.drone.move_forward(50)
        time.sleep(5)
        self.drone.rotate_counter_clockwise(45)
        time.sleep(5)
        self.drone.move_forward(50)
        time.sleep(5)
        self.drone.rotate_counter_clockwise(70)
        time.sleep(5)
        self.drone.flip_left()
        time.sleep(5)
        self.drone.move_down(25)
        time.sleep(5)
        self.drone.flip_right()
        time.sleep(5)
        self.drone.flip_right()
        time.sleep(5)
        self.drone.move_back(35)
        time.sleep(5)
        self.drone.flip_back()
        time.sleep(5)
        self.get_state()
        time.sleep(5)
        curve_xyz_speed(-50, -50, 0, -200, -200, 0, 10)
        time.sleep(5)
        self.rotate_clockwise(115)
        time.sleep(5)
        self.drone.land()
        ####################################################
        ############## ADD YOUR CODE HERE ##################
        ####################################################

    def get_frame(self):
        # only begin flying once a video feed is established
        self.frame_count += 1
        if self.flying == False and self.frame_count > self.init_frames:
            self.flying = True
            # self.fly()
            self.myThread.start()
        # Grab a frame and resize it
        frame_read = self.drone.get_frame_read()
        if frame_read.stopped:
            return
        frame = cv2.resize(frame_read.frame, (360, 240))
        # encode OpenCV raw frame to jpeg
        ret, jpeg = cv2.imencode('.jpg', frame)
        return jpeg.tobytes()
示例#13
0
me.yaw_velocity = 0
me.speed = 0

print("Done Connecting:", time.process_time())
print("Battery:", me.get_battery())
d = 0
dh = 130
while True:
    print("Beginning Takeoff:", time.process_time())
    me.takeoff()
    print("Done Takeoff:", time.process_time())
    me.move_up(50)
    curr_height = me.get_height()
    while curr_height > (dh + 5) or curr_height < (dh - 5):
        if curr_height > (dh + 5):
            me.move_down(20)
            curr_height = me.get_height()
        if curr_height < (dh - 5):
            me.move_up(20)
            curr_height = me.get_height()
    print("Height", curr_height)
    '''
    print("Beginning Move:", time.process_time())
    # me.__send_stick_command()
    me.move_forward(100)
    print("Done Move:", time.process_time())
    '''
    # mission pad detection
    # me.mon()
    me.enable_mission_pads()
    me.set_mission_pad_detection_direction(0)
示例#14
0
tello.takeoff()

while True:
    # In reality you want to display frames in a seperate thread. Otherwise
    #  they will freeze while the drone moves.
    img = frame_read.framew
    cv2.imshow("drone", img)

    key = cv2.waitKey(1) & 0xff
    if key == 27:  # ESC
        break
    elif key == ord('w'):
        tello.move_forward(30)
    elif key == ord('s'):
        tello.move_back(30)
    elif key == ord('a'):
        tello.move_left(30)
    elif key == ord('d'):
        tello.move_right(30)
    elif key == ord('e'):
        tello.rotate_clockwise(30)
    elif key == ord('q'):
        tello.rotate_counter_clockwise(30)
    elif key == ord('r'):
        tello.move_up(30)
    elif key == ord('f'):
        tello.move_down(30)

tello.land()
tello.streamoff()
示例#15
0
    """Connects to the Wi-Fi with SSID and password.
    After this command the tello will reboot.
    Only works with Tello EDUs.
    """
    return self.send_command_without_return('ap %s %s' % (ssid, password))

#arucoDict = cv2.aruco.Dictionary_get(cv2.aruco.DICT_ARUCO_ORIGINAL)
#arucoParams = cv2.aruco.DetectorParameters_create()

drone = Tello()
drone.connect()
#drone.streamon()

#frame_read = drone.get_frame_read()
drone.takeoff()
drone.move_down(30)
start = time.time()
diff = 0
#
# height, width, _ = frame_read.frame.shape
# video = cv2.VideoWriter('video2.avi', cv2.VideoWriter_fourcc(*'XVID'), 30, (width, height))

while diff<30:
    temp = time.time()
    diff = temp - start
    # frame_read = drone.get_frame_read()
    # (corners, ids, rejected) = cv2.aruco.detectMarkers(frame_read.frame, arucoDict, parameters=arucoParams)
    # cv2.aruco.drawDetectedMarkers(frame_read.frame, corners, ids)
    # cv2.aruco.drawDetectedMarkers(frame_read.frame, rejected, borderColor=(100, 0, 240))
    # cv2.imshow('Live Video', frame_read.frame)
    # video.write(frame_read.frame)
示例#16
0
                else:
                   drone.move_right(20)
                   time.sleep(0.1)

            else:
                print('Stay in line')
                inline = True

            if center_height - centroids[1][1] > 95:
                print('Fly Up')
                drone.move_up(20)
                time.sleep(0.1)

            elif center_height - centroids[1][1] < 35:
                print('Fly Down')
                drone.move_down(20)
                time.sleep(0.1)
            else:
                print('Stay in Level')
                inlevel = True
                time.sleep(0.1)

            ### Draws yellow corners on "image"
            for i in corners:
                x, y = i.ravel()
                cv2.circle(image, (x, y), 1, (0, 255, 255), -1)

            target = [0, 255, 255]
            X, Y = np.array(np.where(np.all(image == target, axis=2)))

            coordinates = np.array([])
class hand_tello_control:
    def __init__(self):
        self.action = " "
        self.mp_drawing = mp.solutions.drawing_utils
        self.mp_drawing_styles = mp.solutions.drawing_styles
        self.mp_hands = mp.solutions.hands

    def tello_startup(self):
        # For Tello input:
        self.tello = Tello()  # Starts the tello object
        self.tello.connect()  # Connects to the drone

    def define_orientation(self, results):

        if results.multi_hand_landmarks[0].landmark[
                4].x < results.multi_hand_landmarks[0].landmark[17].x:
            orientation = "right hand"
        else:
            orientation = "left hand"
        return orientation

    def action_to_do(
            self, fingers, orientation,
            results):  #use the variable results for the hand tracking control

        #Left hand controls tricks, right hand controls movement

        if orientation == "left hand":  #Thumb on the left = left hand!
            if fingers == [0, 1, 0, 0, 0]:
                self.action = "flip forward"
                self.tello.flip_forward()

            elif fingers == [0, 1, 1, 0, 0]:
                self.action = "flip back"
                self.tello.flip_back()
            elif fingers == [1, 0, 0, 0, 0]:
                self.action = "flip right"
                self.tello.flip_right()
            elif fingers == [0, 0, 0, 0, 1]:
                self.action = "flip left"
                self.tello.flip_left()
            elif fingers == [0, 1, 1, 1, 0]:
                self.action = "Square"
                self.tello.move_left(20)
                self.tello.move_up(40)
                self.tello.move_right(40)
                self.tello.move_down(40)
                self.tello.move_left(20)
            elif fingers == [0, 0, 1, 0, 0]:
                self.action = " :( "
                self.tello.land()

            else:
                self.action = " "

        elif orientation == "right hand":  #Thumb on the right = right hand!
            if fingers == [0, 1, 0, 0, 0]:
                self.action = "Move up"
                self.tello.move_up(20)
            elif fingers == [1, 0, 0, 0, 0]:
                self.action = "Move left"
                self.tello.move_left(20)
            elif fingers == [0, 0, 0, 0, 1]:
                self.action = "Move right"
                self.tello.move_right(20)
            elif fingers == [0, 1, 1, 0, 0]:
                self.action = "Move down"
                self.tello.move_down(20)
            else:
                self.action = " "

    def fingers_position(self, results, orientation):

        # [thumb, index, middle finger, ring finger, pinky]
        # 0 for closed, 1 for open
        fingers = [0, 0, 0, 0, 0]

        if (results.multi_hand_landmarks[0].landmark[4].x >
                results.multi_hand_landmarks[0].landmark[3].x
            ) and orientation == "right hand":
            fingers[0] = 0
        if (results.multi_hand_landmarks[0].landmark[4].x <
                results.multi_hand_landmarks[0].landmark[3].x
            ) and orientation == "right hand":
            fingers[0] = 1

        if (results.multi_hand_landmarks[0].landmark[4].x >
                results.multi_hand_landmarks[0].landmark[3].x
            ) and orientation == "left hand":
            fingers[0] = 1
        if (results.multi_hand_landmarks[0].landmark[4].x <
                results.multi_hand_landmarks[0].landmark[3].x
            ) and orientation == "left hand":
            fingers[0] = 0

        fingermarkList = [8, 12, 16, 20]
        i = 1
        for k in fingermarkList:
            if results.multi_hand_landmarks[0].landmark[
                    k].y > results.multi_hand_landmarks[0].landmark[k - 2].y:
                fingers[i] = 0
            else:
                fingers[i] = 1

            i = i + 1

        return fingers

    def detection_loop(self):
        with self.mp_hands.Hands(model_complexity=0,
                                 min_detection_confidence=0.75,
                                 min_tracking_confidence=0.5) as hands:
            self.tello.streamoff(
            )  # Ends the current stream, in case it's still opened
            self.tello.streamon()  # Starts a new stream
            while True:
                frame_read = self.tello.get_frame_read(
                )  # Stores the current streamed frame
                image = frame_read.frame

                # To improve performance, optionally mark the image as not writeable to
                # pass by reference.
                image.flags.writeable = True
                image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
                results = hands.process(image)

                # Draw the hand annotations on the image.
                image.flags.writeable = True
                image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)

                #self.battery = self.tello.query_battery()

                #if 75 <= self.battery <= 100:
                #color = (0, 150, 0)  # Green (BGR)

                #elif 50 <= self.battery < 75:
                #color = (0, 255, 255)  # Yellow

                #elif 0 <= self.battery < 50:
                #color = (0, 0, 255)  # Red

                if results.multi_hand_landmarks:
                    action = " "
                    for hand_landmarks in results.multi_hand_landmarks:
                        self.mp_drawing.draw_landmarks(
                            image, hand_landmarks,
                            self.mp_hands.HAND_CONNECTIONS,
                            self.mp_drawing_styles.
                            get_default_hand_landmarks_style(),
                            self.mp_drawing_styles.
                            get_default_hand_connections_style())

                    orientation = self.define_orientation(results)
                    fingers = self.fingers_position(results, orientation)
                    #print(fingers)
                    self.action_to_do(fingers, orientation, results)

                cv2.putText(
                    image,
                    f'Action: {str(self.action)}',
                    (10, 70),
                    cv2.FONT_HERSHEY_PLAIN,
                    3,
                    (100, 100, 255),
                    3,
                )

                #cv2.putText(image, f'Battery: {str(self.battery)}%', (10, 450), cv2.FONT_HERSHEY_PLAIN, 3, color, 3, )

                cv2.imshow("image", image)
                if cv2.waitKey(5) & 0xFF == 27:
                    break

    def main_interface(self):
        self.tello_startup()
        self.tello.takeoff()
        self.detection_loop()