Bird
Raised Fist0
Drone Programmingprogramming~10 mins

Altitude control in Drone Programming - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Altitude control
Read current altitude
↓
Compare with target altitude
↓
Too low?
↓
Increase thrust
↓
Adjust drone motors
↓
Repeat loop until altitude is stable
The drone reads its current altitude, compares it to the target, then adjusts motor thrust up or down to reach and maintain the desired height.
Execution Sample
Drone Programming
target_altitude = 100
current_altitude = 90
thrust = 0
while abs(current_altitude - target_altitude) > 1:
    if current_altitude < target_altitude:
        thrust += 1
    else:
        thrust -= 1
This code adjusts the drone's thrust to reach a target altitude of 100 units by increasing or decreasing thrust based on current altitude.
Execution Table
Stepcurrent_altitudetarget_altitudeCondition (abs diff > 1)Thrust actionThrust value
190100True (10 > 1)Increase thrustthrust = thrust + 1
291100True (9 > 1)Increase thrustthrust = thrust + 1
392100True (8 > 1)Increase thrustthrust = thrust + 1
493100True (7 > 1)Increase thrustthrust = thrust + 1
594100True (6 > 1)Increase thrustthrust = thrust + 1
695100True (5 > 1)Increase thrustthrust = thrust + 1
796100True (4 > 1)Increase thrustthrust = thrust + 1
897100True (3 > 1)Increase thrustthrust = thrust + 1
998100True (2 > 1)Increase thrustthrust = thrust + 1
1099100False (1 > 1)No changethrust unchanged
11100100False (0 > 1)No changethrust unchanged
💡 Loop stops when altitude difference is 1 or less, meaning drone is close enough to target altitude.
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5After 6After 7After 8After 9After 10Final
current_altitude90919293949596979899100100
thrustinitialinitial+1initial+2initial+3initial+4initial+5initial+6initial+7initial+8initial+9initial+9initial+9
Key Moments - 2 Insights
Why does the loop stop even though current_altitude is not exactly equal to target_altitude?
The loop stops when the difference between current_altitude and target_altitude is 1 or less, as shown in step 10 and 11 of the execution_table, meaning the drone is close enough to the target altitude.
Why does thrust stop increasing at step 10 even though current_altitude is less than target_altitude?
At step 10, the condition abs(current_altitude - target_altitude) > 1 becomes false (difference is 1), so the loop ends and thrust is no longer adjusted, as seen in the execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 5. What is the current_altitude value?
A95
B94
C93
D90
💡 Hint
Check the 'current_altitude' column in the execution_table row for step 5.
At which step does the condition abs(current_altitude - target_altitude) > 1 become false?
AStep 10
BStep 11
CStep 9
DStep 8
💡 Hint
Look at the 'Condition' column in the execution_table to find when it changes to false.
If the initial current_altitude was 105 instead of 90, what would be the thrust action at step 1?
AIncrease thrust
BNo change
CDecrease thrust
DLoop exits immediately
💡 Hint
Refer to the logic in the code and the execution_table for when current_altitude is greater than target_altitude.
Concept Snapshot
Altitude control loop:
- Read current altitude
- Compare with target altitude
- If too low, increase thrust
- If too high, decrease thrust
- Repeat until close enough
- Stops when difference ≤ 1 unit
Full Transcript
This example shows how a drone controls its altitude by reading its current height and comparing it to a target. If the drone is below the target, it increases thrust to go up. If it is above, it decreases thrust to go down. The loop continues adjusting thrust until the drone is close enough to the target altitude, within 1 unit difference. The execution table traces each step, showing altitude and thrust changes. Key moments clarify why the loop stops before exact equality and why thrust stops changing at the end. The quiz tests understanding of these steps and conditions.

Practice

(1/5)
1. What is the main purpose of altitude control in drone programming?
easy
A. To keep or change the drone's height safely
B. To control the drone's speed horizontally
C. To manage the drone's battery life
D. To adjust the drone's camera angle

Solution

  1. Step 1: Understand altitude control function

    Altitude control is about managing the drone's height above ground.
  2. Step 2: Compare options to this function

    Only To keep or change the drone's height safely talks about height control, others focus on speed, battery, or camera.
  3. Final Answer:

    To keep or change the drone's height safely -> Option A
  4. Quick Check:

    Altitude control = height management [OK]
Hint: Altitude control means managing drone height safely [OK]
Common Mistakes:
  • Confusing altitude with speed control
  • Thinking altitude control manages battery
  • Mixing altitude control with camera settings
2. Which of the following is the correct syntax to set a target altitude of 100 meters in a drone program?
easy
A. altitude = 100m
B. setAltitude(100)
C. targetAltitude = '100'
D. set_altitude = 100

Solution

  1. Step 1: Identify correct function call syntax

    Function calls use parentheses with numeric values, like setAltitude(100).
  2. Step 2: Check other options for syntax errors

    altitude = 100m uses invalid unit '100m' without quotes or function; C uses string instead of number; D assigns value but not a function call.
  3. Final Answer:

    setAltitude(100) -> Option B
  4. Quick Check:

    Function call with number = setAltitude(100) [OK]
Hint: Function calls use parentheses with numbers, not strings or units [OK]
Common Mistakes:
  • Using units like '100m' directly in code
  • Assigning string instead of number for altitude
  • Confusing assignment with function call
3. What will be the output of this code snippet controlling drone altitude?
current_altitude = 50
set_target = 100
if current_altitude < set_target:
    print('Ascending')
else:
    print('Holding position')
medium
A. Holding position
B. Descending
C. Ascending
D. Error

Solution

  1. Step 1: Compare current altitude and target

    current_altitude is 50, set_target is 100, so 50 < 100 is True.
  2. Step 2: Determine which print runs

    Since condition is True, it prints 'Ascending'.
  3. Final Answer:

    Ascending -> Option C
  4. Quick Check:

    50 < 100 means Ascending [OK]
Hint: If current altitude less than target, drone ascends [OK]
Common Mistakes:
  • Confusing less than with greater than
  • Expecting 'Holding position' when ascending
  • Thinking code has syntax error
4. Identify the error in this altitude control code snippet:
def adjust_altitude(current, target):
    if current > target
        return 'Descending'
    else:
        return 'Ascending'
medium
A. Indentation error in else block
B. Wrong comparison operator
C. Function missing return statement
D. Missing colon after if condition

Solution

  1. Step 1: Check syntax of if statement

    The if line lacks a colon at the end, which is required in Python.
  2. Step 2: Verify other parts

    Comparison operator and return statements are correct; indentation is consistent.
  3. Final Answer:

    Missing colon after if condition -> Option D
  4. Quick Check:

    Python if needs colon ':' [OK]
Hint: Python if statements always end with a colon ':' [OK]
Common Mistakes:
  • Forgetting colon after if condition
  • Assuming wrong operator causes error
  • Misreading indentation as error
5. You want the drone to hover steadily at 120 meters. Which code snippet correctly adjusts motor power based on current altitude to maintain this target?
def control_altitude(current):
    target = 120
    if current < target:
        power = 70
    elif current > target:
        power = 30
    else:
        power = 50
    return power

print(control_altitude(120))
hard
A. 50
B. 70
C. 30
D. 120

Solution

  1. Step 1: Understand the target and current altitude

    Target is 120 meters; current altitude passed is 120.
  2. Step 2: Follow the if-elif-else logic

    Since current equals target, the else block runs, setting power to 50.
  3. Final Answer:

    50 -> Option A
  4. Quick Check:

    Hover power at target altitude = 50 [OK]
Hint: At target altitude, motor power is set to hover value [OK]
Common Mistakes:
  • Choosing power for ascending or descending instead of hover
  • Confusing comparison operators
  • Returning altitude instead of power
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard