Simple Autonomous Mode

Comprehensive Examples: 75+ autonomous flight examples organized by difficulty level
Focus: Range sensors, gyroscope, optical flow, pressure sensors
Last Updated: 2024

Basics - 30 Examples

These examples introduce fundamental autonomous flight concepts using basic movement commands and simple sensor readings.

Example 1: Basic Takeoff and Land

Description: Simple takeoff and land sequence

What this example demonstrates: This code shows how to simple takeoff and land sequence. Each line includes comments explaining its purpose and functionality.

Key concepts: Takeoff sequence, Landing sequence.

from codrone_edu.drone import *  # Import CoDrone EDU library - provides all drone control functions
drone = Drone()  # Create a Drone object instance to control the CoDrone EDU
drone.pair()  # Connect the drone controller to the program via USB port
drone.takeoff()  # Take off to ~80cm height, hover for 1 second to stabilize (height cannot be modified)
drone.hover(2)  # Hover in place for 2 seconds (stabilizes drone)
drone.land()  # Land softly at current position, reset all flight motion variables to 0
drone.close()  # Disconnect from controller - always call this at the end to properly close connection

Example 2: Forward Movement

Description: Move forward a specific distance

What this example demonstrates: This code shows how to move forward a specific distance. Each line includes comments explaining its purpose and functionality.

Key concepts: Takeoff sequence, Landing sequence, Movement using optical flow.

from codrone_edu.drone import *  # Import CoDrone EDU library - provides all drone control functions
drone = Drone()  # Create a Drone object instance to control the CoDrone EDU
drone.pair()  # Connect the drone controller to the program via USB port
drone.takeoff()  # Take off to ~80cm height, hover for 1 second to stabilize (height cannot be modified)
drone.move_forward(50)  # Move forward 50 centimeters using optical flow sensor (requires patterned surface)
drone.land()  # Land softly at current position, reset all flight motion variables to 0
drone.close()  # Disconnect from controller - always call this at the end to properly close connection

Example 3: Backward Movement

Description: Move backward a specific distance

What this example demonstrates: This code shows how to move backward a specific distance. Each line includes comments explaining its purpose and functionality.

Key concepts: Takeoff sequence, Landing sequence, Movement using optical flow.

from codrone_edu.drone import *  # Import CoDrone EDU library - provides all drone control functions
drone = Drone()  # Create a Drone object instance to control the CoDrone EDU
drone.pair()  # Connect the drone controller to the program via USB port
drone.takeoff()  # Take off to ~80cm height, hover for 1 second to stabilize (height cannot be modified)
drone.move_backward(50)  # Move backward 50 centimeters using optical flow sensor
drone.land()  # Land softly at current position, reset all flight motion variables to 0
drone.close()  # Disconnect from controller - always call this at the end to properly close connection

Example 4: Left Movement

Description: Move left a specific distance

What this example demonstrates: This code shows how to move left a specific distance. Each line includes comments explaining its purpose and functionality.

Key concepts: Takeoff sequence, Landing sequence, Movement using optical flow.

from codrone_edu.drone import *  # Import CoDrone EDU library - provides all drone control functions
drone = Drone()  # Create a Drone object instance to control the CoDrone EDU
drone.pair()  # Connect the drone controller to the program via USB port
drone.takeoff()  # Take off to ~80cm height, hover for 1 second to stabilize (height cannot be modified)
drone.move_left(50)  # Move left 50 centimeters using optical flow sensor
drone.land()  # Land softly at current position, reset all flight motion variables to 0
drone.close()  # Disconnect from controller - always call this at the end to properly close connection

Example 5: Right Movement

Description: Move right a specific distance

What this example demonstrates: This code shows how to move right a specific distance. Each line includes comments explaining its purpose and functionality.

Key concepts: Takeoff sequence, Landing sequence, Movement using optical flow.

from codrone_edu.drone import *  # Import CoDrone EDU library - provides all drone control functions
drone = Drone()  # Create a Drone object instance to control the CoDrone EDU
drone.pair()  # Connect the drone controller to the program via USB port
drone.takeoff()  # Take off to ~80cm height, hover for 1 second to stabilize (height cannot be modified)
drone.move_right(50)  # Move right 50 centimeters using optical flow sensor
drone.land()  # Land softly at current position, reset all flight motion variables to 0
drone.close()  # Disconnect from controller - always call this at the end to properly close connection

Example 6: Square Pattern

Description: Fly in a square pattern

What this example demonstrates: This code shows how to fly in a square pattern. Each line includes comments explaining its purpose and functionality.

Key concepts: Takeoff sequence, Landing sequence, Movement using optical flow, Rotation using gyroscope, Loops and iteration.

from codrone_edu.drone import *  # Import CoDrone EDU library - provides all drone control functions
drone = Drone()  # Create a Drone object instance to control the CoDrone EDU
drone.pair()  # Connect the drone controller to the program via USB port
drone.takeoff()  # Take off to ~80cm height, hover for 1 second to stabilize (height cannot be modified)
for i in range(4):  # Loop 4 times (0 to 3)
    drone.move_forward(50)  # Move forward 50 centimeters using optical flow sensor (requires patterned surface)
    drone.turn_degree(90)  # Turn 90 degrees right using gyroscope for accuracy
drone.land()  # Land softly at current position, reset all flight motion variables to 0
drone.close()  # Disconnect from controller - always call this at the end to properly close connection

Example 7: Rectangle Pattern

Description: Fly in a rectangle pattern

What this example demonstrates: This code shows how to fly in a rectangle pattern. Each line includes comments explaining its purpose and functionality.

Key concepts: Takeoff sequence, Landing sequence, Movement using optical flow, Rotation using gyroscope, Loops and iteration.

from codrone_edu.drone import *  # Import CoDrone EDU library - provides all drone control functions
drone = Drone()  # Create a Drone object instance to control the CoDrone EDU
drone.pair()  # Connect the drone controller to the program via USB port
drone.takeoff()  # Take off to ~80cm height, hover for 1 second to stabilize (height cannot be modified)
for i in range(2):  # Loop 2 times (0 to 1)
    drone.move_forward(80)  # Move forward 80 centimeters using optical flow sensor (requires patterned surface)
    drone.turn_degree(90)  # Turn 90 degrees right using gyroscope for accuracy
    drone.move_forward(50)  # Move forward 50 centimeters using optical flow sensor (requires patterned surface)
    drone.turn_degree(90)  # Turn 90 degrees right using gyroscope for accuracy
drone.land()  # Land softly at current position, reset all flight motion variables to 0
drone.close()  # Disconnect from controller - always call this at the end to properly close connection

Example 8: Triangle Pattern

Description: Fly in a triangle pattern

What this example demonstrates: This code shows how to fly in a triangle pattern. Each line includes comments explaining its purpose and functionality.

Key concepts: Takeoff sequence, Landing sequence, Movement using optical flow, Rotation using gyroscope, Loops and iteration.

from codrone_edu.drone import *  # Import CoDrone EDU library - provides all drone control functions
drone = Drone()  # Create a Drone object instance to control the CoDrone EDU
drone.pair()  # Connect the drone controller to the program via USB port
drone.takeoff()  # Take off to ~80cm height, hover for 1 second to stabilize (height cannot be modified)
for i in range(3):  # Loop 3 times (0 to 2)
    drone.move_forward(60)  # Move forward 60 centimeters using optical flow sensor (requires patterned surface)
    drone.turn_degree(120)  # Turn 120 degrees right using gyroscope for accuracy
drone.land()  # Land softly at current position, reset all flight motion variables to 0
drone.close()  # Disconnect from controller - always call this at the end to properly close connection

Example 9: Hover with Duration

Description: Hover for a specific time

What this example demonstrates: This code shows how to hover for a specific time. Each line includes comments explaining its purpose and functionality.

Key concepts: Takeoff sequence, Landing sequence.

from codrone_edu.drone import *  # Import CoDrone EDU library - provides all drone control functions
drone = Drone()  # Create a Drone object instance to control the CoDrone EDU
drone.pair()  # Connect the drone controller to the program via USB port
drone.takeoff()  # Take off to ~80cm height, hover for 1 second to stabilize (height cannot be modified)
drone.hover(5)  # Hover in place for 5 seconds (stabilizes drone)
drone.land()  # Land softly at current position, reset all flight motion variables to 0
drone.close()  # Disconnect from controller - always call this at the end to properly close connection

Example 10: Multiple Hovers

Description: Hover at different positions

What this example demonstrates: This code shows how to hover at different positions. Each line includes comments explaining its purpose and functionality.

Key concepts: Takeoff sequence, Landing sequence, Movement using optical flow.

from codrone_edu.drone import *  # Import CoDrone EDU library - provides all drone control functions
drone = Drone()  # Create a Drone object instance to control the CoDrone EDU
drone.pair()  # Connect the drone controller to the program via USB port
drone.takeoff()  # Take off to ~80cm height, hover for 1 second to stabilize (height cannot be modified)
drone.move_forward(30)  # Move forward 30 centimeters using optical flow sensor (requires patterned surface)
drone.hover(2)  # Hover in place for 2 seconds (stabilizes drone)
drone.move_backward(30)  # Move backward 30 centimeters using optical flow sensor
drone.hover(2)  # Hover in place for 2 seconds (stabilizes drone)
drone.land()  # Land softly at current position, reset all flight motion variables to 0
drone.close()  # Disconnect from controller - always call this at the end to properly close connection

Example 11: Turn Left

Description: Turn left using turn_left

What this example demonstrates: This code shows how to turn left using turn_left. Each line includes comments explaining its purpose and functionality.

Key concepts: Takeoff sequence, Landing sequence, Rotation using gyroscope.

from codrone_edu.drone import *  # Import CoDrone EDU library - provides all drone control functions
drone = Drone()  # Create a Drone object instance to control the CoDrone EDU
drone.pair()  # Connect the drone controller to the program via USB port
drone.takeoff()  # Take off to ~80cm height, hover for 1 second to stabilize (height cannot be modified)
drone.turn_left(30, 1)  # Turn left at 30% power for 1 seconds
drone.land()  # Land softly at current position, reset all flight motion variables to 0
drone.close()  # Disconnect from controller - always call this at the end to properly close connection

Example 12: Turn Right

Description: Turn right using turn_right

What this example demonstrates: This code shows how to turn right using turn_right. Each line includes comments explaining its purpose and functionality.

Key concepts: Takeoff sequence, Landing sequence, Rotation using gyroscope.

from codrone_edu.drone import *  # Import CoDrone EDU library - provides all drone control functions
drone = Drone()  # Create a Drone object instance to control the CoDrone EDU
drone.pair()  # Connect the drone controller to the program via USB port
drone.takeoff()  # Take off to ~80cm height, hover for 1 second to stabilize (height cannot be modified)
drone.turn_right(30, 1)  # Turn right at 30% power for 1 seconds
drone.land()  # Land softly at current position, reset all flight motion variables to 0
drone.close()  # Disconnect from controller - always call this at the end to properly close connection

Example 13: Turn by Degrees

Description: Turn using turn_degree

What this example demonstrates: This code shows how to turn using turn_degree. Each line includes comments explaining its purpose and functionality.

Key concepts: Takeoff sequence, Landing sequence, Rotation using gyroscope.

from codrone_edu.drone import *  # Import CoDrone EDU library - provides all drone control functions
drone = Drone()  # Create a Drone object instance to control the CoDrone EDU
drone.pair()  # Connect the drone controller to the program via USB port
drone.takeoff()  # Take off to ~80cm height, hover for 1 second to stabilize (height cannot be modified)
drone.turn_degree(90)  # Turn 90 degrees right using gyroscope for accuracy
drone.turn_degree(-90)  # Turn 90 degrees left using gyroscope for accuracy
drone.land()  # Land softly at current position, reset all flight motion variables to 0
drone.close()  # Disconnect from controller - always call this at the end to properly close connection

Example 14: Go Function - Forward

Description: Use go() to move forward

What this example demonstrates: This code shows how to use go() to move forward. Each line includes comments explaining its purpose and functionality.

Key concepts: Takeoff sequence, Landing sequence.

from codrone_edu.drone import *  # Import CoDrone EDU library - provides all drone control functions
drone = Drone()  # Create a Drone object instance to control the CoDrone EDU
drone.pair()  # Connect the drone controller to the program via USB port
drone.takeoff()  # Take off to ~80cm height, hover for 1 second to stabilize (height cannot be modified)
drone.go("forward", 40, 2)  # Move forward at 40% power for 2 seconds (beginner-friendly)
drone.land()  # Land softly at current position, reset all flight motion variables to 0
drone.close()  # Disconnect from controller - always call this at the end to properly close connection

Example 15: Go Function - All Directions

Description: Use go() in all directions

What this example demonstrates: This code shows how to use go() in all directions. Each line includes comments explaining its purpose and functionality.

Key concepts: Takeoff sequence, Landing sequence.

from codrone_edu.drone import *  # Import CoDrone EDU library - provides all drone control functions
drone = Drone()  # Create a Drone object instance to control the CoDrone EDU
drone.pair()  # Connect the drone controller to the program via USB port
drone.takeoff()  # Take off to ~80cm height, hover for 1 second to stabilize (height cannot be modified)
drone.go("forward", 30, 1)  # Move forward at 30% power for 1 seconds (beginner-friendly)
drone.go("backward", 30, 1)  # Move backward at 30% power for 1 seconds (beginner-friendly)
drone.go("left", 30, 1)  # Move left at 30% power for 1 seconds (beginner-friendly)
drone.go("right", 30, 1)  # Move right at 30% power for 1 seconds (beginner-friendly)
drone.land()  # Land softly at current position, reset all flight motion variables to 0
drone.close()  # Disconnect from controller - always call this at the end to properly close connection

Example 16: Move Distance - Forward

Description: Move specific distance forward

What this example demonstrates: This code shows how to move specific distance forward. Each line includes comments explaining its purpose and functionality.

Key concepts: Takeoff sequence, Landing sequence.

from codrone_edu.drone import *  # Import CoDrone EDU library - provides all drone control functions
drone = Drone()  # Create a Drone object instance to control the CoDrone EDU
drone.pair()  # Connect the drone controller to the program via USB port
drone.takeoff()  # Take off to ~80cm height, hover for 1 second to stabilize (height cannot be modified)
drone.move_distance("forward", 100, 50)  # Move forward 100cm at 50% power
drone.land()  # Land softly at current position, reset all flight motion variables to 0
drone.close()  # Disconnect from controller - always call this at the end to properly close connection

Example 17: Move Distance - All Directions

Description: Move distance in all directions

What this example demonstrates: This code shows how to move distance in all directions. Each line includes comments explaining its purpose and functionality.

Key concepts: Takeoff sequence, Landing sequence.

from codrone_edu.drone import *  # Import CoDrone EDU library - provides all drone control functions
drone = Drone()  # Create a Drone object instance to control the CoDrone EDU
drone.pair()  # Connect the drone controller to the program via USB port
drone.takeoff()  # Take off to ~80cm height, hover for 1 second to stabilize (height cannot be modified)
drone.move_distance("forward", 50, 40)  # Move forward 50cm at 40% power
drone.move_distance("backward", 50, 40)  # Move backward 50cm at 40% power
drone.move_distance("left", 50, 40)  # Move left 50cm at 40% power
drone.move_distance("right", 50, 40)  # Move right 50cm at 40% power
drone.land()  # Land softly at current position, reset all flight motion variables to 0
drone.close()  # Disconnect from controller - always call this at the end to properly close connection

Example 18: Circle Pattern

Description: Fly in a circle

What this example demonstrates: This code shows how to fly in a circle. Each line includes comments explaining its purpose and functionality.

Key concepts: Takeoff sequence, Landing sequence.

from codrone_edu.drone import *  # Import CoDrone EDU library - provides all drone control functions
drone = Drone()  # Create a Drone object instance to control the CoDrone EDU
drone.pair()  # Connect the drone controller to the program via USB port
drone.takeoff()  # Take off to ~80cm height, hover for 1 second to stabilize (height cannot be modified)
drone.circle(1, 50)  # Fly in a circle: 1 full rotations at 50% power
drone.land()  # Land softly at current position, reset all flight motion variables to 0
drone.close()  # Disconnect from controller - always call this at the end to properly close connection

Example 19: Spiral Pattern

Description: Fly in a spiral

What this example demonstrates: This code shows how to fly in a spiral. Each line includes comments explaining its purpose and functionality.

Key concepts: Takeoff sequence, Landing sequence.

from codrone_edu.drone import *  # Import CoDrone EDU library - provides all drone control functions
drone = Drone()  # Create a Drone object instance to control the CoDrone EDU
drone.pair()  # Connect the drone controller to the program via USB port
drone.takeoff()  # Take off to ~80cm height, hover for 1 second to stabilize (height cannot be modified)
drone.spiral(2, 50)  # Fly in a spiral pattern: 2 rotations at 50% power
drone.land()  # Land softly at current position, reset all flight motion variables to 0
drone.close()  # Disconnect from controller - always call this at the end to properly close connection

Example 20: Square Function

Description: Use built-in square function

What this example demonstrates: This code shows how to use built-in square function. Each line includes comments explaining its purpose and functionality.

Key concepts: Takeoff sequence, Landing sequence.

from codrone_edu.drone import *  # Import CoDrone EDU library - provides all drone control functions
drone = Drone()  # Create a Drone object instance to control the CoDrone EDU
drone.pair()  # Connect the drone controller to the program via USB port
drone.takeoff()  # Take off to ~80cm height, hover for 1 second to stabilize (height cannot be modified)
drone.square(50, 1)  # Fly in a square: 50cm side length, 1 rotation
drone.land()  # Land softly at current position, reset all flight motion variables to 0
drone.close()  # Disconnect from controller - always call this at the end to properly close connection

Example 21: Triangle Function

Description: Use built-in triangle function

What this example demonstrates: This code shows how to use built-in triangle function. Each line includes comments explaining its purpose and functionality.

Key concepts: Takeoff sequence, Landing sequence.

from codrone_edu.drone import *  # Import CoDrone EDU library - provides all drone control functions
drone = Drone()  # Create a Drone object instance to control the CoDrone EDU
drone.pair()  # Connect the drone controller to the program via USB port
drone.takeoff()  # Take off to ~80cm height, hover for 1 second to stabilize (height cannot be modified)
drone.triangle(50, 1)  # Fly in a triangle: 50cm side length, 1 rotation
drone.land()  # Land softly at current position, reset all flight motion variables to 0
drone.close()  # Disconnect from controller - always call this at the end to properly close connection

Example 22: Sway Pattern

Description: Sway back and forth

What this example demonstrates: This code shows how to sway back and forth. Each line includes comments explaining its purpose and functionality.

Key concepts: Takeoff sequence, Landing sequence.

from codrone_edu.drone import *  # Import CoDrone EDU library - provides all drone control functions
drone = Drone()  # Create a Drone object instance to control the CoDrone EDU
drone.pair()  # Connect the drone controller to the program via USB port
drone.takeoff()  # Take off to ~80cm height, hover for 1 second to stabilize (height cannot be modified)
drone.sway("forward", 3, 40)  # Sway forward 3 times at 40% power
drone.land()  # Land softly at current position, reset all flight motion variables to 0
drone.close()  # Disconnect from controller - always call this at the end to properly close connection

Example 23: Flip

Description: Perform a flip

What this example demonstrates: This code shows how to perform a flip. Each line includes comments explaining its purpose and functionality.

Key concepts: Takeoff sequence, Landing sequence.

from codrone_edu.drone import *  # Import CoDrone EDU library - provides all drone control functions
drone = Drone()  # Create a Drone object instance to control the CoDrone EDU
drone.pair()  # Connect the drone controller to the program via USB port
drone.takeoff()  # Take off to ~80cm height, hover for 1 second to stabilize (height cannot be modified)
drone.hover(1)  # Hover in place for 1 seconds (stabilizes drone)
drone.flip("forward")  # Perform a forward flip (requires sufficient height)
drone.land()  # Land softly at current position, reset all flight motion variables to 0
drone.close()  # Disconnect from controller - always call this at the end to properly close connection

Example 24: Multiple Flips

Description: Perform multiple flips

What this example demonstrates: This code shows how to perform multiple flips. Each line includes comments explaining its purpose and functionality.

Key concepts: Takeoff sequence, Landing sequence.

from codrone_edu.drone import *  # Import CoDrone EDU library - provides all drone control functions
drone = Drone()  # Create a Drone object instance to control the CoDrone EDU
drone.pair()  # Connect the drone controller to the program via USB port
drone.takeoff()  # Take off to ~80cm height, hover for 1 second to stabilize (height cannot be modified)
drone.hover(1)  # Hover in place for 1 seconds (stabilizes drone)
drone.flip("forward")  # Perform a forward flip (requires sufficient height)
drone.hover(1)  # Hover in place for 1 seconds (stabilizes drone)
drone.flip("backward")  # Perform a backward flip (requires sufficient height)
drone.land()  # Land softly at current position, reset all flight motion variables to 0
drone.close()  # Disconnect from controller - always call this at the end to properly close connection

Example 25: Basic Sequence

Description: Simple flight sequence

What this example demonstrates: This code shows how to simple flight sequence. Each line includes comments explaining its purpose and functionality.

Key concepts: Takeoff sequence, Landing sequence, Movement using optical flow, Rotation using gyroscope.

from codrone_edu.drone import *  # Import CoDrone EDU library - provides all drone control functions
drone = Drone()  # Create a Drone object instance to control the CoDrone EDU
drone.pair()  # Connect the drone controller to the program via USB port
drone.takeoff()  # Take off to ~80cm height, hover for 1 second to stabilize (height cannot be modified)
drone.move_forward(50)  # Move forward 50 centimeters using optical flow sensor (requires patterned surface)
drone.turn_degree(90)  # Turn 90 degrees right using gyroscope for accuracy
drone.move_forward(50)  # Move forward 50 centimeters using optical flow sensor (requires patterned surface)
drone.turn_degree(90)  # Turn 90 degrees right using gyroscope for accuracy
drone.move_forward(50)  # Move forward 50 centimeters using optical flow sensor (requires patterned surface)
drone.turn_degree(90)  # Turn 90 degrees right using gyroscope for accuracy
drone.move_forward(50)  # Move forward 50 centimeters using optical flow sensor (requires patterned surface)
drone.land()  # Land softly at current position, reset all flight motion variables to 0
drone.close()  # Disconnect from controller - always call this at the end to properly close connection

Example 26: Height Check

Description: Check height using pressure sensor

What this example demonstrates: This code shows how to check height using pressure sensor. Each line includes comments explaining its purpose and functionality.

Key concepts: Takeoff sequence, Landing sequence, Height measurement.

from codrone_edu.drone import *  # Import CoDrone EDU library - provides all drone control functions
drone = Drone()  # Create a Drone object instance to control the CoDrone EDU
drone.pair()  # Connect the drone controller to the program via USB port
drone.takeoff()  # Take off to ~80cm height, hover for 1 second to stabilize (height cannot be modified)
height = drone.get_height()  # Get current height in centimeters using pressure sensor
print(f"Height: {height} cm")  # Display value in console/output window
drone.land()  # Land softly at current position, reset all flight motion variables to 0
drone.close()  # Disconnect from controller - always call this at the end to properly close connection

Example 27: Front Range Sensor

Description: Read front range sensor

What this example demonstrates: This code shows how to read front range sensor. Each line includes comments explaining its purpose and functionality.

Key concepts: Takeoff sequence, Landing sequence, Range sensor readings.

from codrone_edu.drone import *  # Import CoDrone EDU library - provides all drone control functions
drone = Drone()  # Create a Drone object instance to control the CoDrone EDU
drone.pair()  # Connect the drone controller to the program via USB port
drone.takeoff()  # Take off to ~80cm height, hover for 1 second to stabilize (height cannot be modified)
distance = drone.get_front_range()  # Get distance to obstacle in front using range sensor (0-100cm range)
print(f"Front distance: {distance} cm")  # Display value in console/output window
drone.land()  # Land softly at current position, reset all flight motion variables to 0
drone.close()  # Disconnect from controller - always call this at the end to properly close connection

Example 28: Bottom Range Sensor

Description: Read bottom range sensor

What this example demonstrates: This code shows how to read bottom range sensor. Each line includes comments explaining its purpose and functionality.

Key concepts: Takeoff sequence, Landing sequence, Range sensor readings.

from codrone_edu.drone import *  # Import CoDrone EDU library - provides all drone control functions
drone = Drone()  # Create a Drone object instance to control the CoDrone EDU
drone.pair()  # Connect the drone controller to the program via USB port
drone.takeoff()  # Take off to ~80cm height, hover for 1 second to stabilize (height cannot be modified)
distance = drone.get_bottom_range()  # Get distance to ground below using bottom range sensor (0-100cm range)
print(f"Bottom distance: {distance} cm")  # Display value in console/output window
drone.land()  # Land softly at current position, reset all flight motion variables to 0
drone.close()  # Disconnect from controller - always call this at the end to properly close connection

Example 29: Gyroscope Reading

Description: Read gyroscope angle

What this example demonstrates: This code shows how to read gyroscope angle. Each line includes comments explaining its purpose and functionality.

Key concepts: Takeoff sequence, Landing sequence.

from codrone_edu.drone import *  # Import CoDrone EDU library - provides all drone control functions
drone = Drone()  # Create a Drone object instance to control the CoDrone EDU
drone.pair()  # Connect the drone controller to the program via USB port
drone.takeoff()  # Take off to ~80cm height, hover for 1 second to stabilize (height cannot be modified)
angle = drone.get_angle_z()  # Get Z-axis rotation angle in degrees using gyroscope (0-360)
print(f"Z angle: {angle} degrees")  # Display value in console/output window
drone.land()  # Land softly at current position, reset all flight motion variables to 0
drone.close()  # Disconnect from controller - always call this at the end to properly close connection

Example 30: Position Reading

Description: Read position using optical flow

What this example demonstrates: This code shows how to read position using optical flow. Each line includes comments explaining its purpose and functionality.

Key concepts: Takeoff sequence, Landing sequence, Position tracking.

from codrone_edu.drone import *  # Import CoDrone EDU library - provides all drone control functions
drone = Drone()  # Create a Drone object instance to control the CoDrone EDU
drone.pair()  # Connect the drone controller to the program via USB port
drone.takeoff()  # Take off to ~80cm height, hover for 1 second to stabilize (height cannot be modified)
x = drone.get_pos_x()  # Get X position in centimeters using optical flow sensor (relative to takeoff point)
y = drone.get_pos_y()  # Get Y position in centimeters using optical flow sensor (relative to takeoff point)
print(f"Position: X={x}, Y={y}")
drone.land()  # Land softly at current position, reset all flight motion variables to 0
drone.close()  # Disconnect from controller - always call this at the end to properly close connection

Intermediate - 27 Examples

These examples combine multiple sensors and introduce conditional logic, loops, and more complex flight patterns.

Example 1: Wall Detection and Avoid

Description: Detect and avoid walls

What this example demonstrates: This code shows how to detect and avoid walls. Each line includes comments explaining its purpose and functionality.

Key concepts: Takeoff sequence, Landing sequence, Movement using optical flow, Rotation using gyroscope, Range sensor readings, Loops and iteration, Conditional logic.

from codrone_edu.drone import *  # Import CoDrone EDU library - provides all drone control functions
drone = Drone()  # Create a Drone object instance to control the CoDrone EDU
drone.pair()  # Connect the drone controller to the program via USB port
drone.takeoff()  # Take off to ~80cm height, hover for 1 second to stabilize (height cannot be modified)
while drone.get_front_range() > 30:  # Continue looping while condition is True (be careful of infinite loops!)
    drone.move_forward(20)  # Move forward 20 centimeters using optical flow sensor (requires patterned surface)
    if drone.get_front_range() <= 30:  # Execute code block only if condition is True
        drone.turn_degree(90)  # Turn 90 degrees right using gyroscope for accuracy
drone.land()  # Land softly at current position, reset all flight motion variables to 0
drone.close()  # Disconnect from controller - always call this at the end to properly close connection

Example 2: Maintain Distance

Description: Maintain distance from wall

What this example demonstrates: This code shows how to maintain distance from wall. Each line includes comments explaining its purpose and functionality.

Key concepts: Takeoff sequence, Landing sequence.

from codrone_edu.drone import *  # Import CoDrone EDU library - provides all drone control functions
drone = Drone()  # Create a Drone object instance to control the CoDrone EDU
drone.pair()  # Connect the drone controller to the program via USB port
drone.takeoff()  # Take off to ~80cm height, hover for 1 second to stabilize (height cannot be modified)
drone.keep_distance(10, 50)  # Maintain 50cm distance from wall for 10 seconds (sensor range up to 150cm)
drone.land()  # Land softly at current position, reset all flight motion variables to 0
drone.close()  # Disconnect from controller - always call this at the end to properly close connection

Example 3: Avoid Wall Function

Description: Use avoid_wall function

What this example demonstrates: This code shows how to use avoid_wall function. Each line includes comments explaining its purpose and functionality.

Key concepts: Takeoff sequence, Landing sequence.

from codrone_edu.drone import *  # Import CoDrone EDU library - provides all drone control functions
drone = Drone()  # Create a Drone object instance to control the CoDrone EDU
drone.pair()  # Connect the drone controller to the program via USB port
drone.takeoff()  # Take off to ~80cm height, hover for 1 second to stabilize (height cannot be modified)
drone.avoid_wall(10, 40)  # Fly forward until 40cm from wall, timeout after 10 seconds (0-100cm range)
drone.land()  # Land softly at current position, reset all flight motion variables to 0
drone.close()  # Disconnect from controller - always call this at the end to properly close connection

Example 4: Height-Based Landing

Description: Land based on height

What this example demonstrates: This code shows how to land based on height. Each line includes comments explaining its purpose and functionality.

Key concepts: Takeoff sequence, Landing sequence, Height measurement, Loops and iteration.

from codrone_edu.drone import *  # Import CoDrone EDU library - provides all drone control functions
drone = Drone()  # Create a Drone object instance to control the CoDrone EDU
drone.pair()  # Connect the drone controller to the program via USB port
drone.takeoff()  # Take off to ~80cm height, hover for 1 second to stabilize (height cannot be modified)
while drone.get_height() > 20:  # Get current height in centimeters using pressure sensor
    drone.hover(0.5)  # Hover indefinitely until next command is given
    print(f"Height: {drone.get_height()} cm")  # Get current height in centimeters using pressure sensor
drone.land()  # Land softly at current position, reset all flight motion variables to 0
drone.close()  # Disconnect from controller - always call this at the end to properly close connection

Example 5: Position-Based Navigation

Description: Navigate using position

What this example demonstrates: This code shows how to navigate using position. Each line includes comments explaining its purpose and functionality.

Key concepts: Flight commands, Sensor usage, Autonomous navigation.

from codrone_edu.drone import *
drone = Drone()
drone.pair()
drone.takeoff()
target_x = 100
target_y = 50
while abs(drone.get_pos_x() - target_x) > 10:
    if drone.get_pos_x() < target_x:
        drone.move_forward(20)
    else:
        drone.move_backward(20)
drone.land()
drone.close()

Example 6: Gyroscope-Based Turn

Description: Turn using gyroscope

What this example demonstrates: This code shows how to turn using gyroscope. Each line includes comments explaining its purpose and functionality.

Key concepts: Takeoff sequence, Landing sequence, Rotation using gyroscope, Loops and iteration.

from codrone_edu.drone import *  # Import CoDrone EDU library - provides all drone control functions
drone = Drone()  # Create a Drone object instance to control the CoDrone EDU
drone.pair()  # Connect the drone controller to the program via USB port
drone.takeoff()  # Take off to ~80cm height, hover for 1 second to stabilize (height cannot be modified)
target_angle = 90  # Store sensor reading in variable for later use
current_angle = drone.get_angle_z()  # Get Z-axis rotation angle in degrees using gyroscope (0-360)
while abs(current_angle - target_angle) > 5:  # Continue looping while condition is True (be careful of infinite loops!)
    drone.turn_degree(target_angle - current_angle)
    current_angle = drone.get_angle_z()  # Get Z-axis rotation angle in degrees using gyroscope (0-360)
drone.land()  # Land softly at current position, reset all flight motion variables to 0
drone.close()  # Disconnect from controller - always call this at the end to properly close connection

Example 7: Square with Sensors

Description: Square pattern with sensor feedback

What this example demonstrates: This code shows how to square pattern with sensor feedback. Each line includes comments explaining its purpose and functionality.

Key concepts: Takeoff sequence, Landing sequence, Movement using optical flow, Rotation using gyroscope, Range sensor readings, Loops and iteration, Conditional logic.

from codrone_edu.drone import *  # Import CoDrone EDU library - provides all drone control functions
drone = Drone()  # Create a Drone object instance to control the CoDrone EDU
drone.pair()  # Connect the drone controller to the program via USB port
drone.takeoff()  # Take off to ~80cm height, hover for 1 second to stabilize (height cannot be modified)
for i in range(4):  # Loop 4 times (0 to 3)
    distance = drone.get_front_range()  # Get distance to obstacle in front using range sensor (0-100cm range)
    if distance > 30:  # Execute code block only if condition is True
        drone.move_forward(50)  # Move forward 50 centimeters using optical flow sensor (requires patterned surface)
    drone.turn_degree(90)  # Turn 90 degrees right using gyroscope for accuracy
drone.land()  # Land softly at current position, reset all flight motion variables to 0
drone.close()  # Disconnect from controller - always call this at the end to properly close connection

Example 8: Height Control

Description: Control height using pressure

What this example demonstrates: This code shows how to control height using pressure. Each line includes comments explaining its purpose and functionality.

Key concepts: Takeoff sequence, Landing sequence, Height measurement, Loops and iteration, Conditional logic.

from codrone_edu.drone import *  # Import CoDrone EDU library - provides all drone control functions
drone = Drone()  # Create a Drone object instance to control the CoDrone EDU
drone.pair()  # Connect the drone controller to the program via USB port
drone.takeoff()  # Take off to ~80cm height, hover for 1 second to stabilize (height cannot be modified)
target_height = 100  # Store value in variable for later use
current_height = drone.get_height()  # Get current height in centimeters using pressure sensor
while abs(current_height - target_height) > 5:  # Continue looping while condition is True (be careful of infinite loops!)
    if current_height < target_height:  # Execute code block only if condition is True
        drone.go("up", 30, 0.5)  # Move up at 30% power for 0.5 seconds (beginner-friendly)
    else:  # Execute code block if all previous conditions were False
        drone.go("down", 30, 0.5)  # Move down at 30% power for 0.5 seconds (beginner-friendly)
    current_height = drone.get_height()  # Get current height in centimeters using pressure sensor
drone.land()  # Land softly at current position, reset all flight motion variables to 0
drone.close()  # Disconnect from controller - always call this at the end to properly close connection

Example 9: Obstacle Avoidance Loop

Description: Continuous obstacle avoidance

What this example demonstrates: This code shows how to continuous obstacle avoidance. Each line includes comments explaining its purpose and functionality.

Key concepts: Takeoff sequence, Landing sequence, Movement using optical flow, Rotation using gyroscope, Range sensor readings, Loops and iteration, Conditional logic.

from codrone_edu.drone import *  # Import CoDrone EDU library - provides all drone control functions
drone = Drone()  # Create a Drone object instance to control the CoDrone EDU
drone.pair()  # Connect the drone controller to the program via USB port
drone.takeoff()  # Take off to ~80cm height, hover for 1 second to stabilize (height cannot be modified)
for i in range(10):  # Loop 10 times (0 to 9)
    if drone.get_front_range() < 40:  # Execute code block only if condition is True
        drone.turn_degree(90)  # Turn 90 degrees right using gyroscope for accuracy
    else:  # Execute code block if all previous conditions were False
        drone.move_forward(30)  # Move forward 30 centimeters using optical flow sensor (requires patterned surface)
drone.land()  # Land softly at current position, reset all flight motion variables to 0
drone.close()  # Disconnect from controller - always call this at the end to properly close connection

Example 10: Position Return

Description: Return to starting position

What this example demonstrates: This code shows how to return to starting position. Each line includes comments explaining its purpose and functionality.

Key concepts: Flight commands, Sensor usage, Autonomous navigation, Position tracking, Loops and iteration, Conditional logic.

from codrone_edu.drone import *  # Import CoDrone EDU library - provides all drone control functions
drone = Drone()  # Create a Drone object instance to control the CoDrone EDU
drone.pair()  # Connect the drone controller to the program via USB port
drone.takeoff()  # Take off to ~80cm height, hover for 1 second to stabilize (height cannot be modified)
start_x = drone.get_pos_x()  # Get X position in centimeters using optical flow sensor (relative to takeoff point)
start_y = drone.get_pos_y()  # Get Y position in centimeters using optical flow sensor (relative to takeoff point)
drone.move_forward(100)  # Move forward 100 centimeters using optical flow sensor (requires patterned surface)
drone.turn_degree(180)  # Turn 180 degrees right using gyroscope for accuracy
# Return to start
while abs(drone.get_pos_x() - start_x) > 10 or abs(drone.get_pos_y() - start_y) > 10:  # Continue looping while condition is True (be careful of infinite loops!)
    if drone.get_pos_x() < start_x:  # Execute code block only if condition is True
        drone.move_forward(20)  # Move forward 20 centimeters using optical flow sensor (requires patterned surface)
    elif drone.get_pos_x() > start_x:  # Execute code block if previous condition was False and this condition is True
        drone.move_backward(20)  # Move backward 20 centimeters using optical flow sensor
    if drone.get_pos_y() < start_y:  # Execute code block only if condition is True
        drone.move_right(20)  # Move right 20 centimeters using optical flow sensor
    elif drone.get_pos_y() > start_y:  # Execute code block if previous condition was False and this condition is True
        drone.move_left(20)  # Move left 20 centimeters using optical flow sensor
drone.land()  # Land softly at current position, reset all flight motion variables to 0
drone.close()  # Disconnect from controller - always call this at the end to properly close connection

Example 11: Spiral with Height

Description: Spiral while changing height

What this example demonstrates: This code shows how to spiral while changing height. Each line includes comments explaining its purpose and functionality.

Key concepts: Takeoff sequence, Landing sequence, Loops and iteration.

from codrone_edu.drone import *  # Import CoDrone EDU library - provides all drone control functions
drone = Drone()  # Create a Drone object instance to control the CoDrone EDU
drone.pair()  # Connect the drone controller to the program via USB port
drone.takeoff()  # Take off to ~80cm height, hover for 1 second to stabilize (height cannot be modified)
for i in range(3):  # Loop 3 times (0 to 2)
    drone.spiral(1, 40)  # Fly in a spiral pattern: 1 rotations at 40% power
    drone.go("up", 20, 0.5)  # Move up at 20% power for 0.5 seconds (beginner-friendly)
drone.land()  # Land softly at current position, reset all flight motion variables to 0
drone.close()  # Disconnect from controller - always call this at the end to properly close connection

Example 12: Pattern with Turns

Description: Complex pattern with turns

What this example demonstrates: This code shows how to complex pattern with turns. Each line includes comments explaining its purpose and functionality.

Key concepts: Takeoff sequence, Landing sequence, Movement using optical flow, Rotation using gyroscope, Loops and iteration.

from codrone_edu.drone import *  # Import CoDrone EDU library - provides all drone control functions
drone = Drone()  # Create a Drone object instance to control the CoDrone EDU
drone.pair()  # Connect the drone controller to the program via USB port
drone.takeoff()  # Take off to ~80cm height, hover for 1 second to stabilize (height cannot be modified)
for i in range(6):  # Loop 6 times (0 to 5)
    drone.move_forward(40)  # Move forward 40 centimeters using optical flow sensor (requires patterned surface)
    drone.turn_degree(60)  # Turn 60 degrees right using gyroscope for accuracy
drone.land()  # Land softly at current position, reset all flight motion variables to 0
drone.close()  # Disconnect from controller - always call this at the end to properly close connection

Example 13: Sensor-Based Hover

Description: Hover until condition met

What this example demonstrates: This code shows how to hover until condition met. Each line includes comments explaining its purpose and functionality.

Key concepts: Takeoff sequence, Landing sequence, Range sensor readings, Loops and iteration.

from codrone_edu.drone import *  # Import CoDrone EDU library - provides all drone control functions
drone = Drone()  # Create a Drone object instance to control the CoDrone EDU
drone.pair()  # Connect the drone controller to the program via USB port
drone.takeoff()  # Take off to ~80cm height, hover for 1 second to stabilize (height cannot be modified)
while drone.get_front_range() > 50:  # Get distance to obstacle in front using range sensor (0-100cm range)
    drone.hover(0.5)  # Hover indefinitely until next command is given
drone.land()  # Land softly at current position, reset all flight motion variables to 0
drone.close()  # Disconnect from controller - always call this at the end to properly close connection

Example 14: Multi-Direction Movement

Description: Move in multiple directions

What this example demonstrates: This code shows how to move in multiple directions. Each line includes comments explaining its purpose and functionality.

Key concepts: Takeoff sequence, Landing sequence, Movement using optical flow, Loops and iteration, Conditional logic.

from codrone_edu.drone import *  # Import CoDrone EDU library - provides all drone control functions
drone = Drone()  # Create a Drone object instance to control the CoDrone EDU
drone.pair()  # Connect the drone controller to the program via USB port
drone.takeoff()  # Take off to ~80cm height, hover for 1 second to stabilize (height cannot be modified)
directions = ["forward", "right", "backward", "left"]
for direction in directions:
    if direction == "forward":  # Execute code block only if condition is True
        drone.move_forward(50)  # Move forward 50 centimeters using optical flow sensor (requires patterned surface)
    elif direction == "backward":  # Execute code block if previous condition was False and this condition is True
        drone.move_backward(50)  # Move backward 50 centimeters using optical flow sensor
    elif direction == "left":  # Execute code block if previous condition was False and this condition is True
        drone.move_left(50)  # Move left 50 centimeters using optical flow sensor
    elif direction == "right":  # Execute code block if previous condition was False and this condition is True
        drone.move_right(50)  # Move right 50 centimeters using optical flow sensor
drone.land()  # Land softly at current position, reset all flight motion variables to 0
drone.close()  # Disconnect from controller - always call this at the end to properly close connection

Example 15: Distance-Based Movement

Description: Move based on sensor distance

What this example demonstrates: This code shows how to move based on sensor distance. Each line includes comments explaining its purpose and functionality.

Key concepts: Takeoff sequence, Landing sequence, Movement using optical flow, Range sensor readings, Loops and iteration, Conditional logic.

from codrone_edu.drone import *  # Import CoDrone EDU library - provides all drone control functions
drone = Drone()  # Create a Drone object instance to control the CoDrone EDU
drone.pair()  # Connect the drone controller to the program via USB port
drone.takeoff()  # Take off to ~80cm height, hover for 1 second to stabilize (height cannot be modified)
target_distance = 50  # Store sensor reading in variable for later use
current_distance = drone.get_front_range()  # Get distance to obstacle in front using range sensor (0-100cm range)
while abs(current_distance - target_distance) > 10:  # Continue looping while condition is True (be careful of infinite loops!)
    if current_distance > target_distance:  # Execute code block only if condition is True
        drone.move_forward(20)  # Move forward 20 centimeters using optical flow sensor (requires patterned surface)
    else:  # Execute code block if all previous conditions were False
        drone.move_backward(20)  # Move backward 20 centimeters using optical flow sensor
    current_distance = drone.get_front_range()  # Get distance to obstacle in front using range sensor (0-100cm range)
drone.land()  # Land softly at current position, reset all flight motion variables to 0
drone.close()  # Disconnect from controller - always call this at the end to properly close connection

Example 16: Angle Correction

Description: Correct angle using gyroscope

What this example demonstrates: This code shows how to correct angle using gyroscope. Each line includes comments explaining its purpose and functionality.

Key concepts: Takeoff sequence, Landing sequence, Rotation using gyroscope, Conditional logic.

from codrone_edu.drone import *  # Import CoDrone EDU library - provides all drone control functions
drone = Drone()  # Create a Drone object instance to control the CoDrone EDU
drone.pair()  # Connect the drone controller to the program via USB port
drone.takeoff()  # Take off to ~80cm height, hover for 1 second to stabilize (height cannot be modified)
target_angle = 0  # Store sensor reading in variable for later use
current_angle = drone.get_angle_z()  # Get Z-axis rotation angle in degrees using gyroscope (0-360)
error = current_angle - target_angle
if abs(error) > 5:  # Execute code block only if condition is True
    drone.turn_degree(-error)
drone.land()  # Land softly at current position, reset all flight motion variables to 0
drone.close()  # Disconnect from controller - always call this at the end to properly close connection

Example 17: Height Pattern

Description: Fly pattern at different heights

What this example demonstrates: This code shows how to fly pattern at different heights. Each line includes comments explaining its purpose and functionality.

Key concepts: Flight commands, Sensor usage, Autonomous navigation.

from codrone_edu.drone import *
drone = Drone()
drone.pair()
drone.takeoff()
heights = [80, 100, 120]
for height in heights:
    current = drone.get_height()
    while abs(current - height) > 5:
        if current < height:
            drone.go("up", 30, 0.5)
        else:
            drone.go("down", 30, 0.5)
        current = drone.get_height()
    drone.square(40, 1)
drone.land()
drone.close()

Example 18: Wall Following

Description: Follow a wall

What this example demonstrates: This code shows how to follow a wall. Each line includes comments explaining its purpose and functionality.

Key concepts: Flight commands, Sensor usage, Autonomous navigation.

from codrone_edu.drone import *
drone = Drone()
drone.pair()
drone.takeoff()
for i in range(20):
    front_dist = drone.get_front_range()
    if front_dist < 40:
        drone.turn_degree(90)
    else:
        drone.move_forward(30)
drone.land()
drone.close()

Example 19: Position Tracking

Description: Track position during flight

What this example demonstrates: This code shows how to track position during flight. Each line includes comments explaining its purpose and functionality.

Key concepts: Takeoff sequence, Landing sequence, Movement using optical flow, Position tracking, Loops and iteration.

from codrone_edu.drone import *  # Import CoDrone EDU library - provides all drone control functions
drone = Drone()  # Create a Drone object instance to control the CoDrone EDU
drone.pair()  # Connect the drone controller to the program via USB port
drone.takeoff()  # Take off to ~80cm height, hover for 1 second to stabilize (height cannot be modified)
positions = []
for i in range(10):  # Loop 10 times (0 to 9)
    x = drone.get_pos_x()  # Get X position in centimeters using optical flow sensor (relative to takeoff point)
    y = drone.get_pos_y()  # Get Y position in centimeters using optical flow sensor (relative to takeoff point)
    positions.append((x, y))
    print(f"Position {i}: ({x}, {y})")  # Display value in console/output window
    drone.move_forward(30)  # Move forward 30 centimeters using optical flow sensor (requires patterned surface)
drone.land()  # Land softly at current position, reset all flight motion variables to 0
drone.close()  # Disconnect from controller - always call this at the end to properly close connection

Example 20: Conditional Movement

Description: Move based on conditions

What this example demonstrates: This code shows how to move based on conditions. Each line includes comments explaining its purpose and functionality.

Key concepts: Flight commands, Sensor usage, Autonomous navigation.

from codrone_edu.drone import *
drone = Drone()
drone.pair()
drone.takeoff()
for i in range(10):
    front = drone.get_front_range()
    height = drone.get_height()
    if front < 40:
        drone.turn_degree(90)
    elif height > 120:
        drone.go("down", 30, 0.5)
    else:
        drone.move_forward(30)
drone.land()
drone.close()

Example 21: Speed Control

Description: Control movement speed

What this example demonstrates: This code shows how to control movement speed. Each line includes comments explaining its purpose and functionality.

Key concepts: Takeoff sequence, Landing sequence, Loops and iteration.

from codrone_edu.drone import *  # Import CoDrone EDU library - provides all drone control functions
drone = Drone()  # Create a Drone object instance to control the CoDrone EDU
drone.pair()  # Connect the drone controller to the program via USB port
drone.takeoff()  # Take off to ~80cm height, hover for 1 second to stabilize (height cannot be modified)
speeds = [30, 50, 70]
for speed in speeds:
    drone.go("forward", speed, 1)
    drone.hover(0.5)  # Hover indefinitely until next command is given
drone.land()  # Land softly at current position, reset all flight motion variables to 0
drone.close()  # Disconnect from controller - always call this at the end to properly close connection

Example 22: Pattern with Hover

Description: Pattern with hover pauses

What this example demonstrates: This code shows how to pattern with hover pauses. Each line includes comments explaining its purpose and functionality.

Key concepts: Takeoff sequence, Landing sequence, Movement using optical flow, Rotation using gyroscope, Loops and iteration.

from codrone_edu.drone import *  # Import CoDrone EDU library - provides all drone control functions
drone = Drone()  # Create a Drone object instance to control the CoDrone EDU
drone.pair()  # Connect the drone controller to the program via USB port
drone.takeoff()  # Take off to ~80cm height, hover for 1 second to stabilize (height cannot be modified)
for i in range(4):  # Loop 4 times (0 to 3)
    drone.move_forward(50)  # Move forward 50 centimeters using optical flow sensor (requires patterned surface)
    drone.hover(1)  # Hover in place for 1 seconds (stabilizes drone)
    drone.turn_degree(90)  # Turn 90 degrees right using gyroscope for accuracy
    drone.hover(1)  # Hover in place for 1 seconds (stabilizes drone)
drone.land()  # Land softly at current position, reset all flight motion variables to 0
drone.close()  # Disconnect from controller - always call this at the end to properly close connection

Example 23: Sensor Monitoring

Description: Monitor sensors during flight

What this example demonstrates: This code shows how to monitor sensors during flight. Each line includes comments explaining its purpose and functionality.

Key concepts: Takeoff sequence, Landing sequence, Movement using optical flow, Range sensor readings, Height measurement, Loops and iteration.

from codrone_edu.drone import *  # Import CoDrone EDU library - provides all drone control functions
drone = Drone()  # Create a Drone object instance to control the CoDrone EDU
drone.pair()  # Connect the drone controller to the program via USB port
drone.takeoff()  # Take off to ~80cm height, hover for 1 second to stabilize (height cannot be modified)
for i in range(5):  # Loop 5 times (0 to 4)
    front = drone.get_front_range()  # Get distance to obstacle in front using range sensor (0-100cm range)
    bottom = drone.get_bottom_range()  # Get distance to ground below using bottom range sensor (0-100cm range)
    height = drone.get_height()  # Get current height in centimeters using pressure sensor
    angle = drone.get_angle_z()  # Get Z-axis rotation angle in degrees using gyroscope (0-360)
    print(f"Front: {front}, Bottom: {bottom}, Height: {height}, Angle: {angle}")  # Display value in console/output window
    drone.move_forward(40)  # Move forward 40 centimeters using optical flow sensor (requires patterned surface)
drone.land()  # Land softly at current position, reset all flight motion variables to 0
drone.close()  # Disconnect from controller - always call this at the end to properly close connection

Example 24: Adaptive Movement

Description: Adapt movement to environment

What this example demonstrates: This code shows how to adapt movement to environment. Each line includes comments explaining its purpose and functionality.

Key concepts: Takeoff sequence, Landing sequence, Movement using optical flow, Rotation using gyroscope, Range sensor readings, Loops and iteration, Conditional logic.

from codrone_edu.drone import *  # Import CoDrone EDU library - provides all drone control functions
drone = Drone()  # Create a Drone object instance to control the CoDrone EDU
drone.pair()  # Connect the drone controller to the program via USB port
drone.takeoff()  # Take off to ~80cm height, hover for 1 second to stabilize (height cannot be modified)
for i in range(15):  # Loop 15 times (0 to 14)
    front = drone.get_front_range()  # Get distance to obstacle in front using range sensor (0-100cm range)
    if front > 80:  # Execute code block only if condition is True
        drone.move_forward(50)  # Move forward 50 centimeters using optical flow sensor (requires patterned surface)
    elif front > 40:  # Execute code block if previous condition was False and this condition is True
        drone.move_forward(30)  # Move forward 30 centimeters using optical flow sensor (requires patterned surface)
    else:  # Execute code block if all previous conditions were False
        drone.turn_degree(45)  # Turn 45 degrees right using gyroscope for accuracy
drone.land()  # Land softly at current position, reset all flight motion variables to 0
drone.close()  # Disconnect from controller - always call this at the end to properly close connection

Example 25: Multi-Sensor Decision

Description: Make decisions using multiple sensors

What this example demonstrates: This code shows how to make decisions using multiple sensors. Each line includes comments explaining its purpose and functionality.

Key concepts: Flight commands, Sensor usage, Autonomous navigation, Range sensor readings, Height measurement, Rotation using gyroscope, Loops and iteration, Conditional logic.

from codrone_edu.drone import *  # Import CoDrone EDU library - provides all drone control functions
drone = Drone()  # Create a Drone object instance to control the CoDrone EDU
drone.pair()  # Connect the drone controller to the program via USB port
drone.takeoff()  # Take off to ~80cm height, hover for 1 second to stabilize (height cannot be modified)
for i in range(10):  # Loop 10 times (0 to 9)
    front = drone.get_front_range()  # Get distance to obstacle in front using range sensor (0-100cm range)
    height = drone.get_height()  # Get current height in centimeters using pressure sensor
    angle = drone.get_angle_z()  # Get Z-axis rotation angle in degrees using gyroscope (0-360)
    if front < 30 and height > 100:  # Execute code block only if condition is True
        drone.go("down", 30, 0.5)  # Move down at 30% power for 0.5 seconds (beginner-friendly)
        drone.turn_degree(90)  # Turn 90 degrees right using gyroscope for accuracy
    elif front < 30:  # Execute code block if previous condition was False and this condition is True
        drone.turn_degree(90)  # Turn 90 degrees right using gyroscope for accuracy
    else:  # Execute code block if all previous conditions were False
        drone.move_forward(40)  # Move forward 40 centimeters using optical flow sensor (requires patterned surface)
drone.land()  # Land softly at current position, reset all flight motion variables to 0
drone.close()  # Disconnect from controller - always call this at the end to properly close connection

Example 26: Timed Pattern

Description: Pattern with time-based control

What this example demonstrates: This code shows how to pattern with time-based control. Each line includes comments explaining its purpose and functionality.

Key concepts: Flight commands, Sensor usage, Autonomous navigation, Timing control, Loops and iteration.

from codrone_edu.drone import *  # Import CoDrone EDU library - provides all drone control functions
import time  # Import time module for sleep/delay functions
drone = Drone()  # Create a Drone object instance to control the CoDrone EDU
drone.pair()  # Connect the drone controller to the program via USB port
drone.takeoff()  # Take off to ~80cm height, hover for 1 second to stabilize (height cannot be modified)
start_time = time.time()  # Get current time in seconds since epoch (used for timing calculations)
while time.time() - start_time < 30:  # Continue looping while condition is True (be careful of infinite loops!)
    drone.move_forward(40)  # Move forward 40 centimeters using optical flow sensor (requires patterned surface)
    drone.turn_degree(90)  # Turn 90 degrees right using gyroscope for accuracy
    time.sleep(0.5)  # Pause program execution for 0.5 seconds
drone.land()  # Land softly at current position, reset all flight motion variables to 0
drone.close()  # Disconnect from controller - always call this at the end to properly close connection

Advanced - 27 Examples

These examples demonstrate complex navigation, pathfinding, multi-waypoint missions, and advanced sensor fusion techniques.

Example 1: Complex Navigation

Description: Navigate to multiple waypoints

What this example demonstrates: This code shows how to navigate to multiple waypoints. Each line includes comments explaining its purpose and functionality.

Key concepts: Flight commands, Sensor usage, Autonomous navigation.

from codrone_edu.drone import *
drone = Drone()
drone.pair()
drone.takeoff()
waypoints = [(100, 0), (100, 100), (0, 100), (0, 0)]
for wx, wy in waypoints:
    while True:
        x = drone.get_pos_x()
        y = drone.get_pos_y()
        dx = wx - x
        dy = wy - y
        if abs(dx) < 10 and abs(dy) < 10:
            break
        if abs(dx) > abs(dy):
            if dx > 0:
                drone.move_forward(20)
            else:
                drone.move_backward(20)
        else:
            if dy > 0:
                drone.move_right(20)
            else:
                drone.move_left(20)
    drone.hover(1)
drone.land()
drone.close()

Example 2: Advanced Obstacle Avoidance

Description: Advanced obstacle avoidance with pathfinding

What this example demonstrates: This code shows how to advanced obstacle avoidance with pathfinding. Each line includes comments explaining its purpose and functionality.

Key concepts: Flight commands, Sensor usage, Autonomous navigation.

from codrone_edu.drone import *
drone = Drone()
drone.pair()
drone.takeoff()
for i in range(30):
    front = drone.get_front_range()
    left_dist = 0
    right_dist = 0
    if front < 50:
        drone.turn_degree(45)
        left_dist = drone.get_front_range()
        drone.turn_degree(-90)
        right_dist = drone.get_front_range()
        drone.turn_degree(45)
        if left_dist > right_dist:
            drone.turn_degree(90)
        else:
            drone.turn_degree(-90)
    drone.move_forward(30)
drone.land()
drone.close()

Example 3: Precision Landing

Description: Precise landing using sensors

What this example demonstrates: This code shows how to precise landing using sensors. Each line includes comments explaining its purpose and functionality.

Key concepts: Flight commands, Sensor usage, Autonomous navigation.

from codrone_edu.drone import *
drone = Drone()
drone.pair()
drone.takeoff()
target_x = 0
target_y = 0
target_height = 20
while True:
    x = drone.get_pos_x()
    y = drone.get_pos_y()
    height = drone.get_height()
    if abs(x - target_x) < 5 and abs(y - target_y) < 5 and abs(height - target_height) < 5:
        break
    if abs(x - target_x) > 5:
        if x > target_x:
            drone.move_backward(10)
        else:
            drone.move_forward(10)
    if abs(y - target_y) > 5:
        if y > target_y:
            drone.move_left(10)
        else:
            drone.move_right(10)
    if abs(height - target_height) > 5:
        if height > target_height:
            drone.go("down", 20, 0.3)
        else:
            drone.go("up", 20, 0.3)
drone.land()
drone.close()

Example 4: Search Pattern

Description: Search pattern with sensor feedback

What this example demonstrates: This code shows how to search pattern with sensor feedback. Each line includes comments explaining its purpose and functionality.

Key concepts: Flight commands, Sensor usage, Autonomous navigation.

from codrone_edu.drone import *
drone = Drone()
drone.pair()
drone.takeoff()
search_radius = 200
start_x = drone.get_pos_x()
start_y = drone.get_pos_y()
for angle in range(0, 360, 45):
    drone.turn_degree(angle - drone.get_angle_z())
    for distance in range(0, search_radius, 50):
        if drone.get_front_range() < 30:
            break
        drone.move_forward(50)
    # Return to start
    while abs(drone.get_pos_x() - start_x) > 10 or abs(drone.get_pos_y() - start_y) > 10:
        if drone.get_pos_x() < start_x:
            drone.move_forward(20)
        elif drone.get_pos_x() > start_x:
            drone.move_backward(20)
        if drone.get_pos_y() < start_y:
            drone.move_right(20)
        elif drone.get_pos_y() > start_y:
            drone.move_left(20)
drone.land()
drone.close()

Example 5: Height-Based Pattern

Description: Pattern that adapts to height

What this example demonstrates: This code shows how to pattern that adapts to height. Each line includes comments explaining its purpose and functionality.

Key concepts: Flight commands, Sensor usage, Autonomous navigation, Height measurement, Loops and iteration, Conditional logic.

from codrone_edu.drone import *  # Import CoDrone EDU library - provides all drone control functions
drone = Drone()  # Create a Drone object instance to control the CoDrone EDU
drone.pair()  # Connect the drone controller to the program via USB port
drone.takeoff()  # Take off to ~80cm height, hover for 1 second to stabilize (height cannot be modified)
base_height = drone.get_height()  # Get current height in centimeters using pressure sensor
for i in range(4):  # Loop 4 times (0 to 3)
    # Maintain base height
    current = drone.get_height()  # Get current height in centimeters using pressure sensor
    if abs(current - base_height) > 10:  # Execute code block only if condition is True
        if current < base_height:  # Execute code block only if condition is True
            drone.go("up", 30, 0.5)  # Move up at 30% power for 0.5 seconds (beginner-friendly)
        else:  # Execute code block if all previous conditions were False
            drone.go("down", 30, 0.5)  # Move down at 30% power for 0.5 seconds (beginner-friendly)
    drone.move_forward(60)  # Move forward 60 centimeters using optical flow sensor (requires patterned surface)
    drone.turn_degree(90)  # Turn 90 degrees right using gyroscope for accuracy
drone.land()  # Land softly at current position, reset all flight motion variables to 0
drone.close()  # Disconnect from controller - always call this at the end to properly close connection

Example 6: Multi-Layer Flight

Description: Fly at multiple height layers

What this example demonstrates: This code shows how to fly at multiple height layers. Each line includes comments explaining its purpose and functionality.

Key concepts: Flight commands, Sensor usage, Autonomous navigation, Height measurement, Loops and iteration, Conditional logic.

from codrone_edu.drone import *  # Import CoDrone EDU library - provides all drone control functions
drone = Drone()  # Create a Drone object instance to control the CoDrone EDU
drone.pair()  # Connect the drone controller to the program via USB port
drone.takeoff()  # Take off to ~80cm height, hover for 1 second to stabilize (height cannot be modified)
layers = [80, 100, 120, 100, 80]  # Store value in variable for later use
for layer in layers:  # Loop through each item in the collection (list, tuple, etc.)
    current = drone.get_height()  # Get current height in centimeters using pressure sensor
    while abs(current - layer) > 5:  # Continue looping while condition is True (be careful of infinite loops!)
        if current < layer:  # Execute code block only if condition is True
            drone.go("up", 30, 0.5)  # Move up at 30% power for 0.5 seconds (beginner-friendly)
        else:  # Execute code block if all previous conditions were False
            drone.go("down", 30, 0.5)  # Move down at 30% power for 0.5 seconds (beginner-friendly)
        current = drone.get_height()  # Get current height in centimeters using pressure sensor
    drone.square(50, 1)  # Fly in a square: 50cm side length, 1 rotation
drone.land()  # Land softly at current position, reset all flight motion variables to 0
drone.close()  # Disconnect from controller - always call this at the end to properly close connection

Example 7: Dynamic Path Planning

Description: Dynamic path planning with obstacles

What this example demonstrates: This code shows how to dynamic path planning with obstacles. Each line includes comments explaining its purpose and functionality.

Key concepts: Flight commands, Sensor usage, Autonomous navigation.

from codrone_edu.drone import *
drone = Drone()
drone.pair()
drone.takeoff()
target_x = 200
target_y = 200
while True:
    x = drone.get_pos_x()
    y = drone.get_pos_y()
    if abs(x - target_x) < 15 and abs(y - target_y) < 15:
        break
    front = drone.get_front_range()
    if front < 40:
        # Obstacle detected, go around
        drone.turn_degree(90)
        drone.move_forward(50)
        drone.turn_degree(-90)
        drone.move_forward(50)
        drone.turn_degree(-90)
        drone.move_forward(50)
        drone.turn_degree(90)
    else:
        # Move toward target
        dx = target_x - x
        dy = target_y - y
        if abs(dx) > abs(dy):
            if dx > 0:
                drone.move_forward(30)
            else:
                drone.move_backward(30)
        else:
            if dy > 0:
                drone.move_right(30)
            else:
                drone.move_left(30)
drone.land()
drone.close()

Example 8: Sensor Fusion Navigation

Description: Navigate using multiple sensors

What this example demonstrates: This code shows how to navigate using multiple sensors. Each line includes comments explaining its purpose and functionality.

Key concepts: Takeoff sequence, Landing sequence, Movement using optical flow, Rotation using gyroscope, Range sensor readings, Height measurement, Position tracking, Loops and iteration, Conditional logic.

from codrone_edu.drone import *  # Import CoDrone EDU library - provides all drone control functions
drone = Drone()  # Create a Drone object instance to control the CoDrone EDU
drone.pair()  # Connect the drone controller to the program via USB port
drone.takeoff()  # Take off to ~80cm height, hover for 1 second to stabilize (height cannot be modified)
target_x = 150  # Store sensor reading in variable for later use
target_y = 150  # Store sensor reading in variable for later use
for i in range(50):  # Loop 50 times (0 to 49)
    x = drone.get_pos_x()  # Get X position in centimeters using optical flow sensor (relative to takeoff point)
    y = drone.get_pos_y()  # Get Y position in centimeters using optical flow sensor (relative to takeoff point)
    front = drone.get_front_range()  # Get distance to obstacle in front using range sensor (0-100cm range)
    angle = drone.get_angle_z()  # Get Z-axis rotation angle in degrees using gyroscope (0-360)
    height = drone.get_height()  # Get current height in centimeters using pressure sensor
    # Calculate desired angle
    dx = target_x - x  # Store sensor reading in variable for later use
    dy = target_y - y  # Store sensor reading in variable for later use
    desired_angle = 0  # Store calculated value in variable
    if dx != 0 or dy != 0:  # Execute code block only if condition is True
        import math  # Import math module for mathematical functions (sqrt, atan2, degrees, etc.)
        desired_angle = math.degrees(math.atan2(dy, dx))  # Store calculated value in variable
    # Turn to face target
    angle_error = desired_angle - angle
    if abs(angle_error) > 10:  # Execute code block only if condition is True
        drone.turn_degree(angle_error)
    # Move forward if clear
    if front > 40:  # Execute code block only if condition is True
        drone.move_forward(30)  # Move forward 30 centimeters using optical flow sensor (requires patterned surface)
    else:  # Execute code block if all previous conditions were False
        drone.turn_degree(45)  # Turn 45 degrees right using gyroscope for accuracy
drone.land()  # Land softly at current position, reset all flight motion variables to 0
drone.close()  # Disconnect from controller - always call this at the end to properly close connection

Example 9: Perimeter Flight

Description: Fly around a perimeter

What this example demonstrates: This code shows how to fly around a perimeter. Each line includes comments explaining its purpose and functionality.

Key concepts: Flight commands, Sensor usage, Autonomous navigation.

from codrone_edu.drone import *
drone = Drone()
drone.pair()
drone.takeoff()
start_x = drone.get_pos_x()
start_y = drone.get_pos_y()
perimeter_size = 100
# Fly square perimeter
corners = [
    (start_x + perimeter_size, start_y),
    (start_x + perimeter_size, start_y + perimeter_size),
    (start_x, start_y + perimeter_size),
    (start_x, start_y)
]
for corner_x, corner_y in corners:
    while True:
        x = drone.get_pos_x()
        y = drone.get_pos_y()
        if abs(x - corner_x) < 15 and abs(y - corner_y) < 15:
            break
        if abs(x - corner_x) > abs(y - corner_y):
            if x < corner_x:
                drone.move_forward(20)
            else:
                drone.move_backward(20)
        else:
            if y < corner_y:
                drone.move_right(20)
            else:
                drone.move_left(20)
    drone.hover(1)
drone.land()
drone.close()

Example 10: Adaptive Height Control

Description: Adaptive height based on obstacles

What this example demonstrates: This code shows how to adaptive height based on obstacles. Each line includes comments explaining its purpose and functionality.

Key concepts: Flight commands, Sensor usage, Autonomous navigation.

from codrone_edu.drone import *
drone = Drone()
drone.pair()
drone.takeoff()
base_height = 80
for i in range(20):
    front = drone.get_front_range()
    bottom = drone.get_bottom_range()
    current_height = drone.get_height()
    # Adjust height based on obstacles
    if front < 50 and current_height < 120:
        drone.go("up", 30, 0.5)
    elif bottom < 30 and current_height > 60:
        drone.go("down", 30, 0.5)
    # Maintain base height if no obstacles
    elif abs(current_height - base_height) > 10:
        if current_height < base_height:
            drone.go("up", 20, 0.3)
        else:
            drone.go("down", 20, 0.3)
    drone.move_forward(40)
drone.land()
drone.close()

Example 11: Spiral Search

Description: Spiral search pattern

What this example demonstrates: This code shows how to spiral search pattern. Each line includes comments explaining its purpose and functionality.

Key concepts: Takeoff sequence, Landing sequence, Movement using optical flow, Rotation using gyroscope, Range sensor readings, Position tracking, Loops and iteration, Conditional logic.

from codrone_edu.drone import *  # Import CoDrone EDU library - provides all drone control functions
drone = Drone()  # Create a Drone object instance to control the CoDrone EDU
drone.pair()  # Connect the drone controller to the program via USB port
drone.takeoff()  # Take off to ~80cm height, hover for 1 second to stabilize (height cannot be modified)
start_x = drone.get_pos_x()  # Get X position in centimeters using optical flow sensor (relative to takeoff point)
start_y = drone.get_pos_y()  # Get Y position in centimeters using optical flow sensor (relative to takeoff point)
radius = 50
for turn in range(4):  # Loop 4 times (0 to 3)
    for step in range(8):  # Loop 8 times (0 to 7)
        angle = (turn * 8 + step) * 45  # Store calculated value in variable
        drone.turn_degree(angle - drone.get_angle_z())
        distance = radius + (step * 10)  # Store calculated value in variable
        for move in range(int(distance / 20)):  # Loop through range of values
            if drone.get_front_range() > 30:  # Get distance to obstacle in front using range sensor (0-100cm range)
                drone.move_forward(20)  # Move forward 20 centimeters using optical flow sensor (requires patterned surface)
            else:  # Execute code block if all previous conditions were False
                break
    radius += 30
drone.land()  # Land softly at current position, reset all flight motion variables to 0
drone.close()  # Disconnect from controller - always call this at the end to properly close connection

Example 12: Grid Pattern

Description: Fly in a grid pattern

What this example demonstrates: This code shows how to fly in a grid pattern. Each line includes comments explaining its purpose and functionality.

Key concepts: Flight commands, Sensor usage, Autonomous navigation.

from codrone_edu.drone import *
drone = Drone()
drone.pair()
drone.takeoff()
start_x = drone.get_pos_x()
start_y = drone.get_pos_y()
grid_size = 50
rows = 3
cols = 3
for row in range(rows):
    for col in range(cols):
        target_x = start_x + col * grid_size
        target_y = start_y + row * grid_size
        while True:
            x = drone.get_pos_x()
            y = drone.get_pos_y()
            if abs(x - target_x) < 10 and abs(y - target_y) < 10:
                break
            if abs(x - target_x) > abs(y - target_y):
                if x < target_x:
                    drone.move_forward(20)
                else:
                    drone.move_backward(20)
            else:
                if y < target_y:
                    drone.move_right(20)
                else:
                    drone.move_left(20)
        drone.hover(0.5)
drone.land()
drone.close()

Example 13: Angle-Based Navigation

Description: Navigate using angle calculations

What this example demonstrates: This code shows how to navigate using angle calculations. Each line includes comments explaining its purpose and functionality.

Key concepts: Flight commands, Sensor usage, Autonomous navigation.

from codrone_edu.drone import *
import math
drone = Drone()
drone.pair()
drone.takeoff()
waypoints = [(100, 0), (100, 100), (0, 100), (0, 0)]
for wx, wy in waypoints:
    while True:
        x = drone.get_pos_x()
        y = drone.get_pos_y()
        dx = wx - x
        dy = wy - y
        distance = math.sqrt(dx*dx + dy*dy)
        if distance < 15:
            break
        target_angle = math.degrees(math.atan2(dy, dx))
        current_angle = drone.get_angle_z()
        angle_diff = target_angle - current_angle
        # Normalize angle
        while angle_diff > 180:
            angle_diff -= 360
        while angle_diff < -180:
            angle_diff += 360
        if abs(angle_diff) > 10:
            drone.turn_degree(angle_diff)
        else:
            move_dist = min(30, distance)
            drone.move_forward(int(move_dist))
drone.land()
drone.close()

Example 14: Obstacle Mapping

Description: Map obstacles while flying

What this example demonstrates: This code shows how to map obstacles while flying. Each line includes comments explaining its purpose and functionality.

Key concepts: Flight commands, Sensor usage, Autonomous navigation.

from codrone_edu.drone import *
drone = Drone()
drone.pair()
drone.takeoff()
obstacles = []
for i in range(30):
    x = drone.get_pos_x()
    y = drone.get_pos_y()
    front = drone.get_front_range()
    if front < 50:
        obstacles.append((x, y, front))
        drone.turn_degree(90)
    drone.move_forward(30)
print(f"Found {len(obstacles)} obstacles")
for obs in obstacles:
    print(f"Obstacle at ({obs[0]}, {obs[1]}) distance: {obs[2]} cm")
drone.land()
drone.close()

Example 15: Height Profile Flight

Description: Fly with height profile

What this example demonstrates: This code shows how to fly with height profile. Each line includes comments explaining its purpose and functionality.

Key concepts: Flight commands, Sensor usage, Autonomous navigation.

from codrone_edu.drone import *
drone = Drone()
drone.pair()
drone.takeoff()
profile = [80, 100, 120, 140, 120, 100, 80]
for target_height in profile:
    current = drone.get_height()
    while abs(current - target_height) > 5:
        if current < target_height:
            drone.go("up", 40, 0.5)
        else:
            drone.go("down", 40, 0.5)
        current = drone.get_height()
    drone.move_forward(60)
    drone.hover(0.5)
drone.land()
drone.close()

Example 16: Return to Home

Description: Return to starting position

What this example demonstrates: This code shows how to return to starting position. Each line includes comments explaining its purpose and functionality.

Key concepts: Flight commands, Sensor usage, Autonomous navigation.

from codrone_edu.drone import *
import math
drone = Drone()
drone.pair()
drone.takeoff()
home_x = drone.get_pos_x()
home_y = drone.get_pos_y()
home_height = drone.get_height()
# Fly away
drone.move_forward(150)
drone.move_right(100)
drone.go("up", 30, 2)
# Return home
while True:
    x = drone.get_pos_x()
    y = drone.get_pos_y()
    height = drone.get_height()
    dx = home_x - x
    dy = home_y - y
    dh = home_height - height
    distance = math.sqrt(dx*dx + dy*dy)
    if distance < 20 and abs(dh) < 10:
        break
    # Adjust position
    if abs(dx) > 10:
        if dx > 0:
            drone.move_forward(20)
        else:
            drone.move_backward(20)
    if abs(dy) > 10:
        if dy > 0:
            drone.move_right(20)
        else:
            drone.move_left(20)
    if abs(dh) > 10:
        if dh > 0:
            drone.go("up", 30, 0.5)
        else:
            drone.go("down", 30, 0.5)
drone.land()
drone.close()

Example 17: Pattern with Obstacle Avoidance

Description: Pattern with integrated obstacle avoidance

What this example demonstrates: This code shows how to pattern with integrated obstacle avoidance. Each line includes comments explaining its purpose and functionality.

Key concepts: Flight commands, Sensor usage, Autonomous navigation.

from codrone_edu.drone import *
drone = Drone()
drone.pair()
drone.takeoff()
pattern_size = 60
for i in range(4):
    for step in range(4):
        front = drone.get_front_range()
        if front < 40:
            # Avoid obstacle
            drone.turn_degree(90)
            drone.move_forward(40)
            drone.turn_degree(-90)
        else:
            drone.move_forward(pattern_size)
        drone.turn_degree(90)
drone.land()
drone.close()

Example 18: Multi-Waypoint Mission

Description: Mission with multiple waypoints

What this example demonstrates: This code shows how to mission with multiple waypoints. Each line includes comments explaining its purpose and functionality.

Key concepts: Flight commands, Sensor usage, Autonomous navigation.

from codrone_edu.drone import *
import math
drone = Drone()
drone.pair()
drone.takeoff()
waypoints = [
    (100, 0, 80),
    (100, 100, 100),
    (0, 100, 120),
    (0, 0, 100),
    (50, 50, 80)
]
for wx, wy, wh in waypoints:
    # Navigate to waypoint
    while True:
        x = drone.get_pos_x()
        y = drone.get_pos_y()
        height = drone.get_height()
        dx = wx - x
        dy = wy - y
        dh = wh - height
        dist = math.sqrt(dx*dx + dy*dy)
        if dist < 15 and abs(dh) < 10:
            break
        # Adjust position
        if abs(dx) > 10:
            if dx > 0:
                drone.move_forward(25)
            else:
                drone.move_backward(25)
        if abs(dy) > 10:
            if dy > 0:
                drone.move_right(25)
            else:
                drone.move_left(25)
        if abs(dh) > 10:
            if dh > 0:
                drone.go("up", 30, 0.5)
            else:
                drone.go("down", 30, 0.5)
    drone.hover(2)
drone.land()
drone.close()

Example 19: Dynamic Obstacle Avoidance

Description: Dynamic obstacle avoidance with memory

What this example demonstrates: This code shows how to dynamic obstacle avoidance with memory. Each line includes comments explaining its purpose and functionality.

Key concepts: Flight commands, Sensor usage, Autonomous navigation.

from codrone_edu.drone import *
drone = Drone()
drone.pair()
drone.takeoff()
visited = []
for i in range(40):
    x = drone.get_pos_x()
    y = drone.get_pos_y()
    front = drone.get_front_range()
    # Check if we've been here
    nearby = False
    for vx, vy in visited:
        if abs(x - vx) < 20 and abs(y - vy) < 20:
            nearby = True
            break
    if front < 40:
        # Try different directions
        drone.turn_degree(90)
        if drone.get_front_range() < 40:
            drone.turn_degree(-180)
            if drone.get_front_range() < 40:
                drone.turn_degree(90)
                drone.go("up", 30, 1)
        drone.move_forward(30)
    elif not nearby:
        drone.move_forward(40)
        visited.append((x, y))
    else:
        drone.turn_degree(45)
drone.land()
drone.close()

Example 20: Precision Formation

Description: Precision formation flight

What this example demonstrates: This code shows how to precision formation flight. Each line includes comments explaining its purpose and functionality.

Key concepts: Flight commands, Sensor usage, Autonomous navigation.

from codrone_edu.drone import *
import math
drone = Drone()
drone.pair()
drone.takeoff()
formation_points = [
    (0, 0),
    (50, 0),
    (25, 43),
    (0, 0)
]
for fx, fy in formation_points:
    while True:
        x = drone.get_pos_x()
        y = drone.get_pos_y()
        dx = fx - x
        dy = fy - y
        dist = math.sqrt(dx*dx + dy*dy)
        if dist < 10:
            break
        angle = math.degrees(math.atan2(dy, dx))
        current_angle = drone.get_angle_z()
        angle_diff = angle - current_angle
        while angle_diff > 180:
            angle_diff -= 360
        while angle_diff < -180:
            angle_diff += 360
        if abs(angle_diff) > 5:
            drone.turn_degree(angle_diff)
        else:
            drone.move_forward(min(25, int(dist)))
    drone.hover(1)
drone.land()
drone.close()

Example 21: Sensor-Based Pattern

Description: Pattern based on sensor readings

What this example demonstrates: This code shows how to pattern based on sensor readings. Each line includes comments explaining its purpose and functionality.

Key concepts: Flight commands, Sensor usage, Autonomous navigation.

from codrone_edu.drone import *
drone = Drone()
drone.pair()
drone.takeoff()
for i in range(20):
    front = drone.get_front_range()
    bottom = drone.get_bottom_range()
    height = drone.get_height()
    # Adjust pattern based on sensors
    if front < 50:
        pattern_size = 30
    else:
        pattern_size = 60
    if bottom < 40:
        target_height = height + 20
        while abs(drone.get_height() - target_height) > 5:
            drone.go("up", 30, 0.5)
    drone.move_forward(pattern_size)
    drone.turn_degree(90)
drone.land()
drone.close()

Example 22: Adaptive Speed Control

Description: Adapt speed based on environment

What this example demonstrates: This code shows how to adapt speed based on environment. Each line includes comments explaining its purpose and functionality.

Key concepts: Flight commands, Sensor usage, Autonomous navigation.

from codrone_edu.drone import *
drone = Drone()
drone.pair()
drone.takeoff()
for i in range(25):
    front = drone.get_front_range()
    height = drone.get_height()
    # Adjust speed based on conditions
    if front > 100:
        speed = 60
        duration = 1.5
    elif front > 50:
        speed = 40
        duration = 1.0
    else:
        speed = 20
        duration = 0.5
    if height > 120:
        drone.go("down", 30, 0.5)
    drone.go("forward", speed, duration)
    if front < 40:
        drone.turn_degree(90)
drone.land()
drone.close()

Example 23: Complex Search Pattern

Description: Complex search with multiple passes

What this example demonstrates: This code shows how to complex search with multiple passes. Each line includes comments explaining its purpose and functionality.

Key concepts: Flight commands, Sensor usage, Autonomous navigation.

from codrone_edu.drone import *
drone = Drone()
drone.pair()
drone.takeoff()
start_x = drone.get_pos_x()
start_y = drone.get_pos_y()
search_width = 200
search_height = 200
passes = 3
for pass_num in range(passes):
    for y_offset in range(0, search_height, 50):
        target_y = start_y + y_offset
        # Go to start of row
        while abs(drone.get_pos_y() - target_y) > 10:
            if drone.get_pos_y() < target_y:
                drone.move_right(20)
            else:
                drone.move_left(20)
        # Scan row
        for x_offset in range(0, search_width, 40):
            target_x = start_x + x_offset
            while abs(drone.get_pos_x() - target_x) > 10:
                if drone.get_pos_x() < target_x:
                    drone.move_forward(20)
                else:
                    drone.move_backward(20)
            if drone.get_front_range() < 30:
                print(f"Obstacle found at ({target_x}, {target_y})")
            drone.hover(0.3)
        # Return to start
        while abs(drone.get_pos_x() - start_x) > 10:
            if drone.get_pos_x() > start_x:
                drone.move_backward(20)
            else:
                drone.move_forward(20)
drone.land()
drone.close()

Example 24: Height-Adaptive Navigation

Description: Navigate adapting to height constraints

What this example demonstrates: This code shows how to navigate adapting to height constraints. Each line includes comments explaining its purpose and functionality.

Key concepts: Flight commands, Sensor usage, Autonomous navigation.

from codrone_edu.drone import *
drone = Drone()
drone.pair()
drone.takeoff()
target_x = 200
target_y = 200
optimal_height = 100
while True:
    x = drone.get_pos_x()
    y = drone.get_pos_y()
    height = drone.get_height()
    front = drone.get_front_range()
    if abs(x - target_x) < 15 and abs(y - target_y) < 15:
        break
    # Maintain optimal height
    if abs(height - optimal_height) > 10:
        if height < optimal_height:
            drone.go("up", 30, 0.5)
        else:
            drone.go("down", 30, 0.5)
    # Avoid obstacles by going up
    elif front < 40:
        drone.go("up", 40, 1)
        drone.move_forward(50)
        # Return to optimal height
        while abs(drone.get_height() - optimal_height) > 10:
            if drone.get_height() > optimal_height:
                drone.go("down", 30, 0.5)
            else:
                drone.go("up", 30, 0.5)
    # Move toward target
    else:
        dx = target_x - x
        dy = target_y - y
        if abs(dx) > abs(dy):
            if dx > 0:
                drone.move_forward(30)
            else:
                drone.move_backward(30)
        else:
            if dy > 0:
                drone.move_right(30)
            else:
                drone.move_left(30)
drone.land()
drone.close()

Example 25: Multi-Objective Mission

Description: Mission with multiple objectives

What this example demonstrates: This code shows how to mission with multiple objectives. Each line includes comments explaining its purpose and functionality.

Key concepts: Flight commands, Sensor usage, Autonomous navigation.

from codrone_edu.drone import *
import math
drone = Drone()
drone.pair()
drone.takeoff()
objectives = [
    {"type": "waypoint", "x": 100, "y": 0, "height": 80},
    {"type": "pattern", "pattern": "square", "size": 50},
    {"type": "waypoint", "x": 100, "y": 100, "height": 100},
    {"type": "pattern", "pattern": "circle", "radius": 1},
    {"type": "waypoint", "x": 0, "y": 100, "height": 120},
    {"type": "return", "x": 0, "y": 0, "height": 80}
]
for obj in objectives:
    if obj["type"] == "waypoint":
        wx, wy, wh = obj["x"], obj["y"], obj["height"]
        while True:
            x = drone.get_pos_x()
            y = drone.get_pos_y()
            h = drone.get_height()
            dx = wx - x
            dy = wy - y
            dh = wh - h
            if math.sqrt(dx*dx + dy*dy) < 15 and abs(dh) < 10:
                break
            if abs(dx) > 10:
                drone.move_forward(25) if dx > 0 else drone.move_backward(25)
            if abs(dy) > 10:
                drone.move_right(25) if dy > 0 else drone.move_left(25)
            if abs(dh) > 10:
                drone.go("up", 30, 0.5) if dh > 0 else drone.go("down", 30, 0.5)
    elif obj["type"] == "pattern":
        if obj["pattern"] == "square":
            drone.square(obj["size"], 1)
        elif obj["pattern"] == "circle":
            drone.circle(obj["radius"], 50)
    elif obj["type"] == "return":
        wx, wy, wh = obj["x"], obj["y"], obj["height"]
        while True:
            x = drone.get_pos_x()
            y = drone.get_pos_y()
            h = drone.get_height()
            if math.sqrt((x-wx)**2 + (y-wy)**2) < 15 and abs(h-wh) < 10:
                break
            if abs(x-wx) > 10:
                drone.move_forward(25) if (wx-x) > 0 else drone.move_backward(25)
            if abs(y-wy) > 10:
                drone.move_right(25) if (wy-y) > 0 else drone.move_left(25)
            if abs(h-wh) > 10:
                drone.go("up", 30, 0.5) if (wh-h) > 0 else drone.go("down", 30, 0.5)
    drone.hover(1)
drone.land()
drone.close()

Example 26: Intelligent Pathfinding

Description: Intelligent pathfinding with backtracking

What this example demonstrates: This code shows how to intelligent pathfinding with backtracking. Each line includes comments explaining its purpose and functionality.

Key concepts: Flight commands, Sensor usage, Autonomous navigation.

from codrone_edu.drone import *
import math
drone = Drone()
drone.pair()
drone.takeoff()
target_x = 250
target_y = 250
path = []
dead_ends = []
max_iterations = 100
for i in range(max_iterations):
    x = drone.get_pos_x()
    y = drone.get_pos_y()
    path.append((x, y))
    dist_to_target = math.sqrt((target_x-x)**2 + (target_y-y)**2)
    if dist_to_target < 20:
        break
    front = drone.get_front_range()
    # Check if dead end
    is_dead_end = False
    for dead_x, dead_y in dead_ends:
        if abs(x - dead_x) < 15 and abs(y - dead_y) < 15:
            is_dead_end = True
            break
    if is_dead_end or front < 30:
        if front < 30:
            dead_ends.append((x, y))
        # Backtrack
        if len(path) > 1:
            prev_x, prev_y = path[-2]
            dx = prev_x - x
            dy = prev_y - y
            if abs(dx) > abs(dy):
                drone.move_forward(30) if dx > 0 else drone.move_backward(30)
            else:
                drone.move_right(30) if dy > 0 else drone.move_left(30)
        else:
            drone.turn_degree(90)
    else:
        # Move toward target
        dx = target_x - x
        dy = target_y - y
        if abs(dx) > abs(dy):
            drone.move_forward(30) if dx > 0 else drone.move_backward(30)
        else:
            drone.move_right(30) if dy > 0 else drone.move_left(30)
drone.land()
drone.close()