How threads works in python

160 Views Asked by At

I'm trying to create app for controlling djitello basing on some tutorials and i want to create possibility to click on some gui tabs and during that control drone by keyboard. So i thought about using threads but i can't write it. Right now when i use my app it freezes after clicking button which activates method for keyboard control. Can someone explain what am i doing wrong?

This is class for keyboardControl:

from Modules.KeyPressModule import GetKeyPressed as keyPressed
from time import sleep
import logging

class KeyboardControlService():
    def __init__(self,passedTello):
        self.tello = passedTello
        self.startControl = False

    def Initialize(self,ui):
        self.ui = ui

    def GetKeyboardInput(self):
        lr,fb,ud,yv = 0,0,0,0
        speed = 30

        if(keyPressed("left")): lr = speed
        elif (keyPressed("right")): lr = -speed

        if(keyPressed("up")): fb = speed
        elif (keyPressed("down")): fb = -speed

        if(keyPressed("w")): ud = speed
        elif (keyPressed("s")): ud = -speed

        if(keyPressed("a")): yv = speed
        elif (keyPressed("d")): yv = -speed

        if(keyPressed("q")): self.tello.land()
        elif (keyPressed("e")): self.tello.takeoff()


        return [lr,fb,ud,yv]

    def StartControl(self):
        self.startControl = True
        while self.startControl:
            values = self.GetKeyboardInput()
            print("test")
            #self.tello.send_rc_control(values[0],values[1],values[2],values[3])
            sleep(0.05)
    def EndControl(self):
        self.startControl = False

This class is for button clicking response:

from djitellopy import Tello
from threading import Thread
from Services.KeyboardControlService import KeyboardControlService as keyboardControlService

class TelloService():
    def __init__(self):
        self.tello = Tello()
        self.keyboardControlService = keyboardControlService(self.tello)

    def UseKeyboardControl(self,UseIt,UI):
        if(UseIt == True):
            self.keyboardControlService.Initialize(UI)
            self.keyboardThread = Thread(target=self.keyboardControlService.StartControl(), daemon=True)
            self.keyboardThread.start()
        else:
            self.keyboardControlService.EndControl()
            self.keyboardThread.join()
1

There are 1 best solutions below

0
On

Okey i resolved it The trouble was here :

self.keyboardThread = Thread(target=self.keyboardControlService.StartControl(), daemon=True)

I passed function not the object