Your team just launched a tech startup in Bondi, NSW. You are approached by a local council, whose members are concerned about recent shark attacks. Your task is to develop a prototype of a Shark Patrol System for shark beach patrol using a set of drones. Your simulation should be done in a school hall or a large indoor area such as the library. The challenge will be finding the ideal patrol route for the drone and using the built in sensors on the drone alongside Python programming to make sure no sharks get through the allocated patrol area.
You will need to code your drones so that they would fly around a given sector in a chosen pattern constantly checking the distance to the ground. You will also need to code a conditional statement using infrared sensor to detect ‘the shark’. If the distance between the drone and the ground gets suddenly shorter (as if the drone has just flown over a moving trolley or ‘shark’), the drone will stop in place and do a flip - which will signal the students that the drone has just detected the shark.
● Loops
● Variables
● Receiving and comparing sensor values
● Time of Flight principle (TOF)
Australian authorities are going to try out a shark detection system that uses artificial intelligence and drones, hoping to give beach goers greater protection.
Figure 1
Setting up the flying area:
Mark out squares on the ground with the masking tape (one per team) these squares should be roughly 3 meters by 3 meters (Figure 2). These areas will represent the sections of ocean that each team will be defending from sharks.
Be sure to space out the patrolling areas to ensure no drone collisions. Assign each team of students a drone to work with and have them set their drones inside of the marked squares. Ideal ratio would be 2-3 students per drone. Make sure each team has at least one laptop with Python IDLE and CODE4FUN Tello Library installed.
Figure 2
Find an object that will represent a shark: an inflatable shark would be ideal, but a wheeled trolley, or a wheeled table, or a large cardboard box attached to a string will work.
The TOF (time of flight) sensor is a way to check the distance between the drone and the ground underneath it. It works by sending out an infrared signal and timing how long it takes to bounce off the ground (or another object) and return back to the infrared sensor on the drone. Because we know the speed that light travels, we can use the time it took to calculate the distance between the drone and the object. The image on the right refers to the TOF as a vision positioning system.
A variable is something that holds a value. It’s called a variable because the value it holds might change. Variables are an important part of programming, and will be used a lot throughout this course. Figure 3 is an example of creating a variable, and using that variable in a print statement.
In the Figure 3 example, we create a variable called message and then print out that variable. Variables must have a name that starts with a letter, or an underscore. It’s a good idea to name your variables so that they hint at what the variable contains. Here, we’ve chosen the word message to represent a variable containing something we’re going to print.
Variables can hold many things, including numbers and text. We can make changes to a variable as well Figure 4. At the end of the above Figure 4 example, our `num` variable now contains the number 7.
Figure 3
Figure 4
The get_tof() function included in the Tello library returns the distance in millimeters from the drone to the ground (or object underneath the drone).
Let’s try printing the value returned from the get_tof() function to get the height of the drone in millimeters Figure 5. We will store this value in a variable called ‘result’ and then print that variable.
Our sensor is not absolutely accurate, therefore when the drone in on the ground the result should be between 0 and 100.
Figure 5
Next, let’s program our drone to take off and see how this value changes when the drone is in the air Figure 6.
The height when the drone is in the air should be around 800 mm. Later on, we’re going to be comparing the TOF sensor value to this initial value, so it’s ok if the value here is different to 800.
Figure 6
The way we’re going to tell if there is a shark beneath us is if that number is smaller than some threshold, for example we could say if that number was less than 600mm and it’s normally 800mm, then it’s fair to assume that we have flown over something. Figure 7
Figure 7
In order to do this, we need to establish a baseline value that we can use to compare to the current value. We’ll store the TOF sensor value right after takeoff in a variable called initial_tof, so we know what the TOF value is when there is nothing underneath the drone Figure 8. Later on we can compare this value to the current TOF. However, We want the drones to continuously check the distance to the ground (or shark), not just once. In order to do this, we’re going to use a loop.
Figure 8
Loops are used to run a portion of code many times, in this case our portion of code that checks the distance to the ground. The kind of loop we’re going to look at is called a “for loop”:
The print() function here will run 5 times, once for each value of the variable i from 0 up to (but not including) 5. Figure 9
For this lesson, we’re not using the value of the variable, we just want our code to run multiple times.
Figure 9
Using a for loop, we can program the drone to check the height many times: Figure 10.
Whilst this code is running, try moving an object or your hand underneath the drone and watch as the distance printed out changes.
Figure 10
Now that the drone is checking the height multiple times, we can use the value we get from it to determine whether or not it’s over an object (inflatable shark or a trolley) .
To do that, we’ll use an if statement to check if the current TOF is at least 100 mm less than our initial TOF, and if it is - then the drone should do a flip to show us it’s spotted a shark. Here is the code: Figure 11.
Figure 11
Test the above code: let the drone takeoff, and then put a hand or any large object underneath it - the drone should now flip when detecting an object Figure 12:
Figure 12
Whilst the drone is moving forward, it cannot also be scanning the ground, as it can only process one command at a time. In order for the drone to search an area, it needs to repeatedly move a small amount, then check the height.
Let’s program our drone to move forward 3 meters. We will need to use forward () command for it. Let’s move 20 cm before we check the height. If we want to fly 3 meters overall, we will need to repeat this 15 times. Let us add this to our code: Figure 13.
With this code, the drone will move 20 centimeters at a time, checking the height each time, until it has done it 15 times. This will take the drone 3 meters from its starting position.
Figure 13
Using your knowledge from previous lessons to move the drones in patterns within their patrolling area.
You can experiment with different shapes for the drone to fly in, and see which ones work best. Here are some examples:
In addition to moving 20cm each loop, drones may also be programmed to turn some amount, in order to move in a circle Figure 14.
Figure 14
To move in a square shape, a nested loop could be used to repeat the existing code 4 times, turning 90 degrees each time Figure 15.
Figure 15
Python’s Turtle library, may be helpful to visualise the motion before trying it with the drones. A Turtle simulation of these movements can be seen here: Figure 17 and the code Figure 16.
Figure 16
Now that each drone is capable of patrolling a sector, checking the distance between the drone and the ground and an object beneath it, we can test the effectiveness of each team’s drone by simulating detecting a shark as it swims across a section of the ocean.
The sectors should be rearranged into a row (see the image above), one sector after another, with a gap in between to reduce the chance of drones colliding. With the sectors rearranged this way, each drone can patrol one section of this “ocean”.
A teacher may use an inflatable shark or just a cardboard box attached to a rope, or a wheeled trolley, to simulate a shark moving through this ‘ocean area’. The shark should move through each sector in turn, and ideally each drone will detect the passing shark and do a flip to signal that it has detected it.
The shark should move slowly, in order to give each drone enough time to detect it, as the TOF sensor is not capable of detecting fast moving objects, and the drones are only able to detect the shark when stationary (ie, just after the forward command finishes).
● Understanding of the Time Of Flight(TOF) principle used to detect the distance between the drone and an object
● How loops can be used to repeat code multiple times
● How conditional statements can be used to accomplish tasks
● How variables can be used to store a value
Drones have been used on Australian beaches to detect sharks, and can give advanced warning to swimmers, surfers and lifeguards. The drones used have special software developed to detect sharks among other sea animals and swimmers. This software uses Artificial Intelligence (AI) developed using thousands of images captured by drones, and can distinguish between sharks, surfers, dolphins, whales, boats and many other objects. This software is reported to have a 90% accuracy rate, which is much higher than the accuracy of a skilled lifeguard.