In our previous lesson we were able to send the commands from the laptop to the drone.
In this lesson we will be writing code so we can receive the response from the drone to the laptop.
When we are sending commands the drone we are able to handle when and what to send to the drone although
when receiving a response from the drone we wouldn't be able to manage the timing to be able to receive
the response.
We will use a thread and create a while loop.
Once the laptop receives a response it will receive the data and leave a log to use the thread.
We have to import threading according to the pep 8 format.
It's best that that libraries are imported in alphabetical order so threading will come between sis
and time.
Now let's go on and add codes to receive the response before getting into it.
We've created socket said 2 with a binary command but since we created a set and command method we can
rewrite this with a send command and command.
We can do the same for a stream on now we're ready to set the response codes first we set self response
to a noun.
Here we want to use a thread.
In this case we want to stop the thread.
When an event occurs set self stop event and use threading event once we set the two codes we need to
define a method to receive the response.
The method name will be received response and set the argument as stop event.
Also we said a while loop that will go on until the stop event occurs.
Once the stop event occurs the loop should break.
Next we set.
Try self response.
Currently unknown.
We set the IP as self socket received from and we set a buffer size to three thousand.
This three thousand is the same as a sample code in the Tello document.
You can also set it to a thousand twenty four.
Once we receive a response we want to use the logger info to print it out.
Let's set action to receive response and response to self response in case a socket error occurs.
We also want to log the error so we set an exception to socket error as the ex set the logger to log
the error.
Action will be received response an exception will be x.
In this case if an error occurs let's break the loop.
If an error occurs we may not be able to receive any responses so we should finish the loop.
Next we will run this method with the thread set response thread to threading thread for the target
we set self receive response.
As this is a thread we won't need to follow with the parentheses.
Now we set ARGs and set the argument as self stop event.
Next we set the argument as a tuple type.
At last we set the response thread to start we might be using the response thread later on.
So we will add self-taught underscore before the method name to keep it private.
The timing to invoke the stop event.
We need to set it to under the stop method.
We need to set self stop event except once the drone stops the stop event is set so that the while loop
will end and your laptop won't receive any more responses.
Let's scroll down to the bottom of the script after the drone manager commands to land the drone.
We need to stop the drone manager once the stop is invoked.
Stop underscore event dot set will invoke.
Now I want to take a moment to briefly explain how the thread works especially for those of you who
are not familiar with it.
Here we set threading thread method to process multiple methods at once.
We set the target to the method name.
In this case it'll be received response when we set the args.
We need to add a comma and the end to create a tuple type.
If we don't set a comma this will become a single stop event as we create the thread we set it to start
by running the received response method within a thread.
This method will process what other methods are processed.
The while loop will go on until this stop event occurs and while the loop is going on we will continue
receiving the responses from the drone and set the data into response which will be logged by the logger.
Let's go up the script a bit.
We need to set the command and stream on after the thread starts so let's move the two methods here.
Now let's run the code once we run the code.
You can see that we are getting the log data.
The log is showing the take off command which was received and responded OK.
Now we see the drone land but we also see an error the error says bad file descriptor.
Let's go back to the script to see why we got an error.
The stop event method was invoked and broke the while loop.
The drone was trying to send back a response but at the same time since the method is running on a thread
the socket close method was also invoked.
This means the drone was trying to send a response to a closed socket.
This became the reason for the error to occur.
To solve this problem once the stop event method is invoked we need to wait until the loop comes to
an end.
Then close the socket.
We can set the system to sleep for a few seconds but in this case we want to be sure that the thread
has finished before we close the socket.
We set while self response thread is alive we will set the time sleep with point 3 seconds
if the retrial goes up to 30.
We understand it as a time up and break the loop.
If not we can't count up one now we've completed our code so let's try running the code again once we
run this code you will see that the command being sent and also the response from the drone is OK.
Now the Land Command has been set and the drone lands sending the response as OK.
This time there are no errors as we are running the process on thread the log data is showing three
okays altogether.
The responses should be paired with each commands.
Let's rewrite the code so the log comes out cleaner.
Go to the send command method set retry to zero.
This will be similar to what we did with the self socket clothes we set while self response is none.
We will set the time sleep with point three seconds if the retrial goes up to three we break the loop.
If not we can count up one Next of self response is none the above was when the retrial became more
than three.
But this next if statement will be set for when the retrial is less than three response will be set
temporarily to none.
If the response is received else response will be set to self response decode UTF 8 for the self response.
We want to initialize it back to none.
We set to return the response that is set and the temporary response.
Now the code is ready.
So let's go through it once more.
First the send command is invoked.
Let's say it's to take off the response for the take off comes back.
If there is no response the system will wait for it.
If there is no response the loop will break.
If the response does come back the value will be set to self response.
Then the response OK will be decoded and set to be returned as we set the response to be return.
We can rewrite the takeoff and land command to return as well.
We finish cleaning the code so let's run the code again and see how the results come out.
As I run the code you can see that the command and the response are paired from the top it's command
okay stream on OK.
The response to take off may take a bit of time but here you see the response come out as OK.
Now the Land Command has been sent and the response comes back.
OK here we've successfully programmed our drone with a very clean log.
Great job.
I'll see you in our next lesson.