0

I am working on my undergraduate project and this is my first time using Python to control a parrot bebop 2 drone. I have a variable x(an int) and I want to read its value from a file. In the mean time I want the program to read this file continuously; what I mean is that my text file input will be changed each time and I want the Python code to catch this change and change the value x.

For example:

  1. if x is assigned 1 from the text file --> then the drone takes off.
  2. in the mean time after x is assigned the value 1, I want it to be assigned 2 automatically and do the second command, for example: move to the left (but without disconnecting the drone "the first command")

here is my code, but I had a lot of problems with it, it checks the value, but no commands work after it knows the value of x:

bebop = Bebop()
print("connecting")
success = bebop.connect(10)
print(success) 
f = open('EEGresults.txt')
lines = f.readlines()
list_of_elements = []
for line in lines:
   list_of_elements += map(int, line.split())
f.close()
print (list_of_elements)
x = list_of_elements[1]

bebop.smart_sleep(5)

 if x == 1:
   print ("Yay! This number is = 1")
   bebop.safe_takeoff(3)

else:
   if x == 2:
      print ("Yay! This number is = 2")
      bebop.move_relative(0,0,0,1.6)

I expect that the code will read the value of x from text file directly and continuously and at the same time it will run the commands depending what the value of x that it receives.

Fatima
  • 1
  • 1
  • You have not defined a function `Bebop()` so python searches for it and finds nothing and thus gives you an error. – shuberman Aug 26 '19 at 09:11
  • Now without knowing what is the body of `Bebop()` its hard to help you unless people know the logic of that function. – shuberman Aug 26 '19 at 09:11

1 Answers1

0

I don´t know how to use Bebop but I whould use something like this...

First I would create an object to store the data from your file. So you need something like an onValueChanged callback. You have to define this object on your own:

class NotifierVariable():
    def __init__(self):
        self.__value = 0
        self.__Listener = list()

    @NotifierVariable.setter
    def set(self, value):
        if(value != self.__value):
            self.__value = value
            for callback in self.__Listener:
                callback(self.__value)

    @property
    def get(self):
        return self.__value


    def addListener(self, callback):
            self.__Listener.append(callback)

You can use it in the following way:

class NotifierVariable():
    def __init__(self):
        self.__value = 0
        self.__Listener = list()

    def set(self, value):
        if(value != self.__value):
            self.__value = value
            for callback in self.__Listener:
                callback(self.__value)

    def get(self):
        return self.__value


    def addListener(self, callback):
            self.__Listener.append(callback)


def Func(value):
    print(value)

if __name__ == '__main__':
    x = NotifierVariable()
    x.addListener(Func)
    x.set(10)

Now you have to read the file periodicly:

while(True):

    with open('EEGresults.txt') as File:
        lines = File.readlines()
        list_of_elements = []
        for line in lines:
           list_of_elements += map(int, line.split())

        print (list_of_elements)
        x.set(list_of_elements[1])

    time.sleep(10)

You use the callback to send the command to your drone. The file is periodicly read out every 10 seconds (you can change it) and update your x variable. A callback is triggered if the value got changed and then you can send a command, based on the input, to your drone.

Kampi
  • 1,798
  • 18
  • 26