How to make a tello drone fly indefinitely while something is happening?

267 views Asked by At

I am writing a tello drone control program. I need to make the drone fly while a key is pressed on the keyboard otherwise it stops. Here is my code:

class Keybord_Recognition(QtCore.QObject):

    def __init__(self, drone):
        super(Keybord_Recognition, self).__init__()
        self.drone = drone

    def key_recog(self, k):
        if k.event_type == 'down':
            if k.name == 'w':
                self.drone.move_forward(30)

            elif k.name == 's':
                self.drone.move_back(30)

            elif k.name == 'a':
                self.drone.move_left(30)

            elif k.name == 'd':
                self.drone.move_right(30)

            elif k.name == 'z':
                self.drone.move_up(30)

            elif k.name == 'x':
                self.drone.move_down(30)

    def run(self):
        keyboard.hook(self.key_recog)

    def stop(self):
        keyboard.unhook(self.key_recog)

How can I do what I need?

2

There are 2 answers

0
George Gotsidis On BEST ANSWER

You will need a while loop to constantly check different directions, moreover there is no need to add another position as static-fly (no move). However if you want to break the while loop for any reason simply add an except KeyboardInterrupt: at the end.

more specific change your code to this:

try:
     while True:
           if k.name == 'w':
              self.drone.move_forward(30)
    
            elif k.name == 's':
                self.drone.move_back(30)
    
            elif k.name == 'a':
                self.drone.move_left(30)
    
            elif k.name == 'd':
                self.drone.move_right(30)
    
            elif k.name == 'z':
                self.drone.move_up(30)
    
            elif k.name == 'x':
                self.drone.move_down(30)

except KeyboardInterrupt:
    print("Press Ctrl-C to exit")
    pass
0
Diego Torres Milano On

If I understood correctly, you want to send this events to the drone while the keys on the keyboard are pressed and exit after some timeout when no key is received.

This uses termios so it works on Linux and macOS.

#! /usr/bin/env python3

from functools import partial
from contextlib import contextmanager
import os
from threading import Timer

class Drone:
    def f(self, name, *args):
        print('drone', name, args[0], '\r')

    def __getattr__(self, name):
        return partial(self.f, name)


@contextmanager
def term(kbd):
    import termios, tty
    try:
        fd = kbd.fileno()
        saved = termios.tcgetattr(fd)
        tty.setraw(fd)
        os.set_blocking(fd, False)
        yield
    finally:
        termios.tcsetattr(fd, termios.TCSADRAIN, saved)


drone = Drone()
should_stop = False
timer = None
TIMEOUT = 2.0


def stop():
    global should_stop
    should_stop = True


with open('/dev/tty') as kbd:
    with term(kbd):
        while not should_stop:
            if timer is None:
                timer = Timer(TIMEOUT, stop)
                timer.start()
            ch = kbd.read(1)
            if ch == 'w':
                drone.move_forward(30)
            elif ch == 's':
                drone.move_back(30)
            elif ch == 'a':
                drone.move_left(30)
            elif ch == 'd':
                drone.move_right(30)
            elif ch == 'z':
                drone.move_up(30)
            elif ch == 'x':
                drone.move_down(30)
            else:
                continue
            timer.cancel()
            timer = None

when you run it and press some keys:

drone move_forward 30
drone move_forward 30
drone move_forward 30
drone move_forward 30
drone move_forward 30
drone move_forward 30
drone move_right 30
drone move_right 30
drone move_right 30
drone move_right 30
drone move_right 30
drone move_back 30
drone move_back 30
drone move_back 30
drone move_forward 30
drone move_forward 30
drone move_forward 30
drone move_forward 30
drone move_forward 30
drone move_left 30
drone move_left 30
drone move_left 30
drone move_left 30
drone move_left 30
drone move_left 30
<<< wait 2 secs >>