Adding Logic With If/Else Statements
Making decisions is an important component of any computer program. One example is the ability our mobile phones have to make decisions such as determining which WiFi network to join, based on signal strength. This decision-making process is built into the operating system of your phone and the logic is programmed by human beings. This is true with this lesson as well. We will extend our programming knowledge to include logic, and program our blocks to make decisions, using DroneBlocks with Tello.
In the previous lesson we performed an aerial shuttle run using loops and variables. What we will accomplish in this lesson will build upon that knowledge by using logic to make a decision in each loop of the mission. Our mission will be to fly Tello in a snake pattern. This will require logic to alternate between flying left or right for each iteration of the loop. The flight pattern can be see in Figure 1. The pink icon represents Tello and the blue line represents the flight path.
Figure 1: Tello snake mission
The dotted orange line represents the end of the mission where we will command Tello to come back and land. We will demonstrate this mission in a long code format without any loops or logic in Figure 2.
Figure 2: Tello snake mission in expanded code blocks
There is an excess of redundant code in Figure 2. In addition, if you choose to fly a longer snake pattern the code redundancy would become unmanageable. Let us take a look at how we can use logic to add intelligence to our code. Figure 3 shows the revised snake mission using variables, loops, and if/else logic.
Figure 3: Refactored snake mission using if/else logic
One way to understand how if/else logic works is in a simple sentence such as this: "If it is cold outside wear a jacket, otherwise do not wear a jacket." This is a very simple logic statement that can be extended to: "If it is cold outside wear a jacket, if it is raining outside bring an umbrella, otherwise wear short sleeves." This is known as if, else if, else logic in programming. This logic can be chained together with many statements, which can even be nested within another statement.
In Figure 3 we create a variable named loop_count and increment it for each iteration of the loop. In the first loop it is zero, then one, then two, and so on. Our if/else logic checks to see if loop_count is even or odd, then makes the decision to fly left or right based on this logic. The video below walks through our mission code in detail.
Pretty cool, right? With loops, variables, and logic you will be able to create some truly powerful autonomous missions with Tello. Often, the best way to learn code is to modify someone else's creation. Reproduce the code from the video and modify a few of the variables. Observe how Tello responds. "Rinse and repeat" (this process is known as iterative programming)!
This lesson provided an overview of basic logic using the if/else block. Try adding logic to create a snake mission along the vertical axis using the "fly up" blocks. Another idea would be to make use of the if/else block to add additional nested logic. There are endless flight possibilities now that you have some fundamental programming concepts in place. Please feel free to share your creations with our community at: https://www.facebook.com/groups/droneblocks.