In this lesson we will be programming the drones to go on a patrol flight.
We have been going through each move that the drone can make.
But in this lesson we will have the drone automatically continue as certain patrol routine.
For example you can set your draw to go into the next room and fly back as your patrol route.
Now let's get started.
First we want to import a library called context lib.
Go to the drill manager in it we set sell petrol event.
We will be setting a threading event eventually but we will set it to none.
In the beginning we will be setting a method to start and stop the patrol so we can set the details
there.
Next we set self is patrol to false.
This will be a flag to see if the drone is on a patrol or not.
Then we set a self patrol semaphore and set threading semaphore followed by one.
This threading semaphore will manage to have only one thread while patrolling.
Next we set thread patrol to none we've completed four attributes.
Now we can go on to creating the methods scroll all the way down under the flip methods.
I'll move the codes away so we have a cleaner view for this lesson.
We start defining the patrol method which will start the patrol.
First we need to check if the drone is currently not armed patrol by setting if not self is patrol.
Next we set the patrol event to a threading event.
Next we set thread patrol to threading thread.
The target will be self underscore patrol and args will be set to self patrols and for and self patrol
event as a tuple type.
Set the underscore thread patrols start to start once the patrol starts.
Self is patrol will become true.
Next we define the under school patrol method.
We need to set the argument to Semaphore and stop.
Event.
Next we'll be setting the semaphore is acquire set to Semaphore with the acquire method followed by
blocking equals false by setting blocking equals false.
The second thread will be blocked.
What happens is that the first thread is acquired.
If a second thread is acquired the process will wait to finish the first thread to go onto the second
in this case.
We set the blocking to false which means the process will immediately finish the second thread if it
were to be acquired.
Now let's move on.
If the thread is acquired we can set a logger here.
Action will be sent to patrol and status to acquire.
Here we set a width statement with context lib using as it stack as stack we said stack with callback
which will be invoked once the statement process finishes.
Here we set the semaphore release method to finish the process.
Initially we set the status to zero.
We will count up the number so that the drone knows what to do.
While not stop event is set the status will count up.
If status is 1 the drone will go up.
If status is to the drone will rotate clockwise 90 degrees.
These settings is free for you to decide if status is 3.
The drone will go down if status is for the status will go back to 0 after each move.
We will make the drone hover for five seconds once the stop event is invoked.
The while loop needs to break once the loop is broke.
The stack callback will be invoked using the context lib exit stack.
We are able to create a stack of release methods but here will only set one if you are used to Python
2.
You may be using try except finally with Python 3.
This will be the new code style so remember how to use this context.
Lib exit stack
in case the threat is not acquired the process will finish.
We can also set a logger to see if there were any cases where the thread was an acquired.
We set logger warning with action underscore patrol and status not acquired.
Once we have finished underscore patrol we will go on to set the stop patrol method defined stop patrol
method.
If the drone is in patrol we want to stop it.
Set self patrol event set let's set retried to 0 while thread patrol is alive.
We want to wait four point three seconds.
Then we try again.
Up to 300 if it goes up to 300.
The Loop will break retrial needs to count up.
Lastly we need to set is patrol as follows.
Once the patrol event is set this while loop will be broken then the semaphore will be released and
we'll be able to start a new thread.
Some of you may think that the semaphore is unnecessary.
So I'll explain it to you why we set it here.
This patrol method will be invoked if is patrol is not true.
The thread will run and right after the thread starts the drone will start the patrol in this case there
is a high chance that the second thread will not run.
Based on this code.
But if you were to be using a web application and we invoke the patrol method twice at the same time
then a thread may run twice at the same time.
The chances that this happens may be very low but to avoid it to happen we need to set the semaphore
to be sure.
I hope you understand that this is an advanced way of programming with python and a way to minimize
errors to occur.
You just learned how to set a semaphore and use it.
Context lib that are advanced Python coding while you program your drone.
What a great way to strike two birds with a stone.
Now we are ready to set the commands after the drone takes off.
We will set to go on a patrol using the patrol method we'll set time sleep to 45 seconds before the
patrol stops.
Then the drone will land and complete the process.
Now let's round the code.
You can see the drone take off.
Now it waits for the patrol method.
You'll see that the patrol has started when the word acquire shows as a log first command will go up.
Then make a clockwise turn 90 degrees and goes down after the drone goes down.
The status will go back to zero and the drone will go onto its second patrol routine again.
It flies up
turns 90 degrees
and goes down once you decide on a patrol routine the drone will automatically continue the routine
until the patrol comes to a stop.
Once the stop method invokes the patrol loop will be broken and the drone will land and finish its process.
So that's all for our patrol lesson.
See you in the next lesson.