Drone Function Documentation
version 2.10 (Changelog)
Connection
pair()
Description
This function connects your controller with the program. You can also set the specific USB port name.
Syntax
pair()
pair(portname)
Parameters
string portname: A string containing the port name or number.
Returns
None
Example Code
#Python code
from codrone_edu.drone import *
drone = Drone()
drone.pair() # pair automatically, may not always work
# drone.pair(port_name = 'COM3') # pair with a specific port
drone.takeoff()
drone.hover(1)
drone.land()
drone.close()
close()
Description
This function closes the connection of your controller with the program.
Syntax
close()
Parameters
None
Returns
None
Example Code
#Python code
from codrone_edu.drone import *
drone = Drone()
drone.pair()
drone.takeoff()
drone.hover(1)
drone.land()
drone.close() # closes connection between controller and program
Flight Commands
takeoff()
Description
This function makes the drone takeoff at an average height of 80 centimeters and hover. The drone will always hover for 1 second in order to stabilize before it executes the next command. NOTE: The takeoff parameters or height cannot be modified.
Syntax
takeoff()
Parameters
None
Returns
None
Example Code
#Python code
from codrone_edu.drone import *
drone = Drone()
drone.pair()
drone.takeoff()
drone.hover(3)
drone.land()
drone.close()
land()
Description
This function makes the drone stop all commands, hover, and make a soft landing where it is. The function will also reset the flight motion variables to 0. NOTE: If you want to take off and immediately land, it is recommended to run a hover() or time.sleep() in between the takeoff() and land(), because the CoDrone EDU may miss the land command otherwise.
Syntax
land()
Parameters
None
Returns
None
Example Code
#Python code
from codrone_edu.drone import *
drone = Drone()
drone.pair()
drone.takeoff()
drone.hover(3) # include a hover() or time.sleep() to prevent land() from skipping
drone.land()
drone.close()
emergency_stop()
Description
This function immediately stops all commands and motors, so the drone will stop flying immediately. The function will also reset the flight motion variables to 0. NOTE: If you want to take off and emergency stop, it is recommended to run a hover() or time.sleep() in between the takeoff() and emergency_stop(), because the CoDrone EDU might miss the emergency_stop() command.
Syntax
emergency_stop()
Parameters
None
Returns
None
Example Code
#Python code
from codrone_edu.drone import *
drone = Drone()
drone.pair()
drone.takeoff()
drone.hover(3) #Recommended to have a hover() or time.sleep(1) before landing
drone.emergency_stop()
drone.close()
hover()
Description
This function makes the drone hover for a given amount of time. If given no parameters, it will hover indefinitely until given a another command.
Syntax
hover(duration)
Parameters
integer duration: Duration of the hovering in seconds
Returns
None
Example Code
#Python code
from codrone_edu.drone import *
drone = Drone()
drone.pair()
drone.takeoff()
drone.hover(3)
drone.land()
drone.close()
go()
Description
A beginner-friendly function that allows the drone to move the drone at a given direction, power, and duration.
Syntax
go(direction, power=50, duration=1)
Parameters
string direction: The direction of the movement
int power: The power at which the drone flies (0-100). Defaults to 50% if not defined.
float duration: The duration of the movement. Defaults to 1 second if not defined.
Returns
None
Example Code
#Python code
from codrone_edu.drone import *
drone = Drone()
drone.pair()
drone.takeoff()
drone.go("forward", 30, 1) # move forward at 30% power for 1 second
drone.go("backward", 30, 1) # move backward at 30% power for 1 second
drone.go("right", 30, 1) # move right at 30% power for 1 second
drone.go("left", 30, 1) # move left at 30% power for 1 second
drone.land()
drone.close()
avoid_wall()
Description
A looped method that makes the drone fly forward until it reaches a desired distance based on the front range sensor. The range of front sensor is from 0cm-100cm
Syntax
avoid_wall()
avoid_wall(timeout)
avoid_wall(distance)
avoid_wall(timeout, distance)
Parameters
integer timeout: timeout is an optional parameter that is the duration in seconds that the function will run. the default value is 2
integer distance: distance is an optional parameter that is the distance in centimeters the drone will stop at in front of an object. the default value is 70
Returns
None
Example Code
#Python code
from codrone_edu.drone import *
drone = Drone()
drone.pair()
drone.takeoff()
# fly forward until a wall is found 50 cm away. run this loop for 10 seconds.
drone.avoid_wall(10, 50)
drone.land()
drone.close()
keep_distance()
Description
A looped method that makes the drone fly forward until it reaches a desired distance. Once the desired distance in reached the drone will maintain that distance. The sensor range is up to 150 cm.
Syntax
keep_distance()
keep_distance(timeout)
keep_distance(distance)
keep_distance(timeout, distance)
Parameters
integer timeout: the duration in seconds that the function will execute. The default value is 2 seconds.
integer distance: the distance in centimeters the drone will stop and maintain distance in front of an object. The default value is 50 centimeters.
Returns
None
Example Code
#Python code
from codrone_edu.drone import *
drone = Drone()
drone.pair()
drone.takeoff()
# maintain a distance of 60cm from an object once detected for 10 seconds
drone.keep_distance(10, 60)
drone.land()
drone.close()
get_trim()
Description
This function gets the current trim values of the drone.
Syntax
get_trim()
Parameters
None
Returns
list trim data: A list of trim data — [0] for roll trim and [1] for pitch trim
Example Code
#Python code
from codrone_edu.drone import *
drone = Drone()
drone.pair()
# print the trim values
trim = drone.get_trim()
print(trim)
print(trim[0])
print(trim[1])
drone.close()
reset_trim()
Description
You can reset the roll and pitch trim of the drone in case your drone is drifting. This function will reset the roll and pitch trim values back to zero.
Syntax
reset_trim()
Parameters
None
Returns
None
Example Code
#Python code
from codrone_edu.drone import *
drone = Drone()
drone.pair()
drone.set_trim(5,0)
print(drone.get_trim())
drone.takeoff()
drone.hover(3)
drone.land()
drone.reset_trim()
print(drone.get_trim())
drone.close()
set_trim()
Description
You can set the roll and pitch trim of the drone in case your drone is drifting. For example, if the drone is drifting to its right, you may want to set the roll to a small negative value. This trim will remain saved, even after powering off until you've changed the trim either programmatically, or done a reset with the remote. NOTE: If you're setting the trim right before a takeoff, make sure to add a time.sleep(1) before the takeoff(), otherwise the takeoff commmand might be skipped.
Syntax
set_trim(roll, pitch)
Parameters
integer roll: the power of the roll (-100 - 100)
integer pitch: the power of the pitch (-100 - 100)
Returns
None
Example Code
#Python code
from codrone_edu.drone import *
import time
drone = Drone()
drone.pair()
drone.set_trim(-5, 0) # example: drone is drifting right, so trim to roll left a little bit
time.sleep(1) # Add a time.sleep(1) before takeoff if you're planning to set the trim before takeoff
drone.takeoff()
drone.hover(3)
drone.land()
drone.close()
move_forward()
Description
Moves the drone forward for a given distance. For the best performance, please make sure your drone is flying over a well-lit, patterned surface.
Syntax
move_forward(distance)
move_forward(distance, unit, speed)
Parameters
float distance: the distance to travel.
string unit: The unit of measurement for the distance flown. Available units are "cm" (centimeter), "ft" (feet), "in" (inches), "m" (meter).
float speed: The drone's speed, in meters per second. The default speed is 1.0 meter per second. Max is 2.0 meters per second.
Returns
None
Example Code
The drone will take off, move forward 50cm at 1m/s, and land.
#Python code
from codrone_edu.drone import *
import time
drone = Drone()
drone.pair()
drone.takeoff()
drone.move_forward(distance=50, units="cm", speed=1)
drone.land()
drone.close()
move_backward()
Description
Moves the drone backward for a given distance. For the best performance, please make sure your drone is flying over a well-lit, patterned surface.
Syntax
move_backward(distance)
move_backward(distance, unit, speed)
Parameters
float distance: the distance to travel.
string unit: The unit of measurement for the distance flown. Available units are "cm" (centimeter), "ft" (feet), "in" (inches), "m" (meter).
float speed: The drone's speed, in meters per second. The default speed is 1.0 meter per second. Max is 2.0 meters per second.
Returns
None
Example Code
The drone will take off, move backward 50cm at 1m/s, and land.
#Python code
from codrone_edu.drone import *
import time
drone = Drone()
drone.pair()
drone.takeoff()
drone.move_backward(distance=50, units="cm", speed=1)
drone.land()
drone.close()
move_left()
Description
Moves the drone left for a given distance. For the best performance, please make sure your drone is flying over a well-lit, patterned surface.
Syntax
move_left(distance)
move_left(distance, unit, speed)
Parameters
float distance: the distance to travel.
string unit: The unit of measurement for the distance flown. Available units are "cm" (centimeter), "ft" (feet), "in" (inches), "m" (meter).
float speed: The drone's speed, in meters per second. The default speed is 1.0 meter per second. Max is 2.0 meters per second.
Returns
None
Example Code
The drone will take off, move left 50cm at 1m/s, and land.
#Python code
from codrone_edu.drone import *
import time
drone = Drone()
drone.pair()
drone.takeoff()
drone.move_left(distance=50, units="cm", speed=1)
drone.land()
drone.close()
move_right()
Description
Moves the drone right for a given distance. For the best performance, please make sure your drone is flying over a well-lit, patterned surface.
Syntax
move_right(distance)
move_right(distance, unit, speed)
Parameters
float distance: the distance to travel.
string unit: The unit of measurement for the distance flown. Available units are "cm" (centimeter), "ft" (feet), "in" (inches), "m" (meter).
float speed: The drone's speed, in meters per second. The default speed is 1.0 meter per second. Max is 2.0 meters per second.
Returns
None
Example Code
The drone will take off, move right 50cm at 1m/s, and land.
#Python code
from codrone_edu.drone import *
import time
drone = Drone()
drone.pair()
drone.takeoff()
drone.move_right(distance=50, units="cm", speed=1)
drone.land()
drone.close()
move_upward()
Description
Moves the drone upward for a given distance. For the best performance, please make sure your drone is flying over a well-lit, patterned surface. Please note that performance may vary at heights above 1.5m since this bottom-range sensor produces less accurate readings above 1.5m.
Syntax
move_upward(distance)
move_upward(distance, unit, speed)
Parameters
float distance: the distance to travel.
string unit: The unit of measurement for the distance flown. Available units are "cm" (centimeter), "ft" (feet), "in" (inches), "m" (meter).
float speed: The drone's speed, in meters per second. The default speed is 1.0 meter per second. Max is 2.0 meters per second.
Returns
None
Example Code
The drone will take off, move upward 50cm at 1m/s, and land.
#Python code
from codrone_edu.drone import *
import time
drone = Drone()
drone.pair()
drone.takeoff()
drone.move_upward(distance=50, units="cm", speed=1)
drone.land()
drone.close()
move_downward()
Description
Moves the drone downward for a given distance. For the best performance, please make sure your drone is flying over a well-lit, patterned surface. Please note that performance may vary at heights above 1.5m since this bottom-range sensor produces less accurate readings above 1.5m.
Syntax
move_downward(distance)
move_downward(distance, unit, speed)
Parameters
float distance: the distance to travel.
string unit: The unit of measurement for the distance flown. Available units are "cm" (centimeter), "ft" (feet), "in" (inches), "m" (meter).
float speed: The drone's speed, in meters per second. The default speed is 1.0 meter per second. Max is 2.0 meters per second.
Returns
None
Example Code
The drone will take off, move downward 50cm at 1m/s, and land.
#Python code
from codrone_edu.drone import *
import time
drone = Drone()
drone.pair()
drone.takeoff()
drone.move_downward(distance=50, units="cm", speed=1)
drone.land()
drone.close()
move_distance()
Description
Moves the drone by the specified distances along the x, y, and z axes, relative to its current position and heading. If two or more distances have non-zero values, the function will move the drone by these distances simultaneously. For the best performance, please make sure your drone is flying over a well-lit, patterned surface.
Syntax
move_distance(positionX, positionY, positionZ, velocity)
Parameters
float positionX: The distance to travel in the x-direction, in meters. This corresponds to forward/backward movement.
float positionY: The distance to travel in the y-direction, in meters. This corresponds to left/right movement.
float positionZ: The distance to travel in the z-direction, in meters. This corresponds to vertical movement.
float velocity: The drone's velocity, in meters per second. The default velocity is 1.0 meter per second. Max is 2.0 meters per second.
Returns
None
Example Code 1
The drone will take off, simultaneously move forward 0.5m, left 0.5m, and upward 0.25m at 1m/s (relative to its current position and heading), move back 0.75cm at 0.75m/s (relative to its current position and heading), and land.
#Python code
from codrone_edu.drone import *
import time
drone = Drone()
drone.pair()
drone.takeoff()
drone.move_distance(0.5, 0.5, 0.25, 1) # move forward 0.5m, left 0.5m, and upward 0.25m simultaneously at 1m/s
drone.move_distance(-0.75, 0, 0, 0.75) # move back 0.75m at 0.75m/s
drone.land()
drone.close()
Example Code 2
The drone will take off, move upward 0.50m at 0.50m/s, move right 0.50m at 0.50m/s, move downward 0.50m at 0.50m/s, move left 0.50m at 0.50m/s, and land.
#Python code
from codrone_edu.drone import *
import time
drone = Drone()
drone.pair()
drone.takeoff()
drone.move_distance(0, 0, 0.5, 0.50) # move up 0.50m at 0.50m/s
drone.move_distance(0, -0.5, 0, 0.50) # move right 0.50m at 0.50m/s
drone.move_distance(0, 0, -0.5, 0.50) # move down 0.50m at 0.50m/s
drone.move_distance(0, 0.5, 0, 0.50) # move left 0.50m at 0.50m/s
drone.land()
drone.close()
send_absolute_position()
Description
Sends a movement command to the drone based on its absolute position from its first takeoff location. For the best performance, please make sure your drone is flying over a well-lit, patterned surface.
Syntax
send_absolute_position(positionX, positionY, positionZ, velocity, heading, rotationalVelocity)
Parameters
float positionX: The X position of the drone (-10m ~ 10m).
float positionY: The Y position of the drone (-10m ~ 10m).
float positionZ: The Z position of the drone (0m ~ 1.5m).
float velocity: The velocity of the drone in meters per second (0.5 ~ 2). The movement speed of the drone.
integer heading: Heading value in degrees. The heading is the z-angle of the drone.
integer rotationalVelocity: The rotational velocity of the drone in degrees per second (0 - 360). Left and right rotation speed of the drone.
Returns
None
Example Code 1
The drone takes off, moves to (0.5m, 0, 1m) at 0.5m/s, hovers at (0.5m, 0, 1m) since the drone is already in that position, and then lands.
#Python code
from codrone_edu.drone import *
drone = Drone()
drone.pair()
drone.takeoff()
# Sending the drone forward from its takeoff location 0.5 meters moving at 0.5 m/s
drone.send_absolute_position(0.5, 0, 1, 0.5, 0, 0)
# Sending the same command will cause the drone to hover around
# the same area since this command uses absolute positioning from the takeoff location
drone.send_absolute_position(0.5, 0, 1, 0.5, 0, 0)
drone.land()
drone.close()
Example Code 2
The drone takes off, moves to (0.5m, 0, 0.8m) at 0.5m/s, moves to (0.5m, 0.5m, 0.8m) at 0.5m/s, moves to (0, 0.5m, 0.8m) at 0.5m/s, moves to (0, 0, 0.8m) at 0.5m/s, and finally lands.
#Python code
from codrone_edu.drone import *
drone = Drone()
drone.pair()
drone.takeoff()
# Drone will move to (0.5m, 0, 0.8m) with 0 degree heading.
drone.send_absolute_position(0.5, 0, 0.8, 0.5, 0, 0)
# Drone will move to (0.5m, 0.5m, 0.8m) with 0 degree heading.
drone.send_absolute_position(0.5, 0.5, 0.8, 0.5, 0, 0)
# Drone will move to (0, 0.5m, 0.8m) with 0 degree heading.
drone.send_absolute_position(0, 0.5, 0.8, 0.5, 0, 0)
# Drone will move to (0, 0, 0.8m) with 0 degree heading.
drone.send_absolute_position(0, 0, 0.8, 0.5, 0, 0)
drone.land()
drone.close()
Example Code 3
The drone takes off, hovers at (0, 0, 0.8m) and rotates to 180 degree heading at 180 degrees/second, hovers at (0, 0, 0.8m) and rotates to 270 degree heading at 90 degrees/second, hovers at (0, 0, 0.8m) and rotates to 360 degree heading at 90 degrees/second, hovers at (0, 0, 0.8m) and rotates to 450 degree heading at 90 degrees/second, and finally lands.
#Python code
from codrone_edu.drone import *
drone = Drone()
drone.pair()
drone.takeoff()
# Drone will move to (0, 0, 0.8m) with 180 degree heading.
drone.send_absolute_position(0,0,0.8,0,180,180)
# Drone will move to (0, 0, 0.8m) with 270 degree heading.
drone.send_absolute_position(0,0,0.8,0,270,90)
# Drone will move to (0, 0, 0.8m) with 360 degree heading.
drone.send_absolute_position(0,0,0.8,0,360,90)
# Drone will move to (0, 0, 0.8m) with 450 degree heading.
drone.send_absolute_position(0,0,0.8,0,450,90)
drone.land()
drone.close()
turn()
Description
This function turns the drone to the left or right, based on the given power value (-100 - 100), for a given number of seconds.
Syntax
turn()
turn(power=50, seconds=None)
Parameters
integer power: The number represents the direction and power of the turn. Negative power turns the drone to the right, and positive power turns the drone to the left.
float seconds: The duration of the turn
Returns
None
Example Code
#Python code
from codrone_edu.drone import *
drone = Drone()
drone.pair()
drone.takeoff()
drone.turn(50, 2) # drone will turn left at 50% power for 2 seconds
drone.turn(-20,5) # drone will turn right 20% power for 5 seconds
drone.land()
drone.close()
turn_degree()
Description
Turns right or left to a specified heading, in degrees, with respect to drone's initial heading. The initial heading is defined by the heading right after takeoff. The function will calculate the shortest path to the specified heading and turn in that direction.
Syntax
turn_degree(degree, timeout, p_value)
Parameters
integer degree: angle of turn in degrees. This function allows values more than 360, but the drone will not do more than a full rotation. To go to degree=450, it will not calculate the difference from starting heading (0) and turn for 1.25 revolutions. It will calculate the equivalent angle (90) and take the shortest rotation.
integer timeout: optional parameter that is duration in seconds that drone will try to turn. default value is 3.
integer p_value: optional parameter that is the gain of the proportional controller, if this increased CDE will turn quicker, the smaller the slower. default value is 10.
Returns
None
Example Code
#Python code
from codrone_edu.drone import *
drone = Drone()
drone.pair()
drone.takeoff()
drone.turn_degree(90) # drone will turn left 90 degrees
drone.land()
drone.close()
turn_left()
Description
Turns the drone left using the built in gyroscope. The default degree is 90.
Syntax
turn_left()
turn_left(degree)
Parameters
integer degree: optional parameter that turns the drone in the given degree, ranging from 0 - 360. default value is 90.
Returns
None
Example Code
#Python code
from codrone_edu.drone import *
drone = Drone()
drone.pair()
drone.takeoff()
drone.turn_left() # make a 90 degree left turn.
drone.hover(1) # wait for 1 second in the air
drone.turn_left(30) # make a 30 degree left turn
drone.land()
drone.close()
turn_right()
Description
Turns the drone right using the built in gyroscope. The default degree is 90.
Syntax
turn_right()
turn_right(degree)
Parameters
integer degree: optional parameter that turns the drone in the given degree, ranging from 0 - 360. default value is 90.
Returns
None
Example Code
#Python code
from codrone_edu.drone import *
drone = Drone()
drone.pair()
drone.takeoff()
drone.turn_right() # make a 90 degree right turn.
drone.hover(1) # wait for 1 second in the air
drone.turn_right(30) # make a 30 degree right turn
drone.land()
drone.close()
Flight Sequences
circle()
Description
Flies the drone in the shape of a circle. The speed of the drone will only determine the forward speed and turning speed of the drone, not the size of the circle. The size of the circle, at any power, is roughly the same, with a diameter of 4-5ft.
Syntax
circle()
circle(speed, direction)
Parameters
integer speed: optional parameter that is the speed the drone will move and rotate(0 - 100). default value is 75.
integer direction: optional parameter that determines the direction of the circle. 1 is right, -1 is left. default value is 1.
Returns
None
Example Code
#Python code
from codrone_edu.drone import *
drone = Drone()
drone.pair()
drone.takeoff()
# default circle parameters (75, 1)
drone.circle()
drone.land()
drone.close()
flip()
Description
This functions makes the drone flip backward, forward, right, or left. Make sure your battery percentage is over 50% for the flip to execute.
Syntax
flip()
flip(direction)
Parameters
string direction: optional parameter that is the direction the drone will flip. default is "back"
Returns
None
Example Code
Add a hover or delay after the flip if you need to stabilize before your next command. The drone takes 3-4 seconds after a flip before it can do another flight command.
#Python code
import time
from codrone_edu.drone import *
drone = Drone()
drone.pair()
drone.takeoff()
drone.hover(3)
drone.flip("back") # send flip command
time.sleep(4) # wait for flip to complete
drone.set_pitch(30) # move forward for 1 second
drone.move(1)
drone.set_pitch(-30) # move backward for 1 second
drone.move(1)
drone.land()
drone.close()
spiral()
Description
Flies the drone in the shape of a downward spiral.
Syntax
spiral()
spiral(speed, seconds, direction)
Parameters
integer speed: optional parameter that is the speed the drone will move (0 - 100). default value is 50.
integer seconds: optional parameter that is the duration in seconds the drone flies in a downward spiral. default value is 5.
integer direction: optional parameter that determines the direction of the spiral. 1 is right, -1 is left. default value is 1.
Returns
None
Example Code
#Python code
from codrone_edu.drone import *
drone = Drone()
drone.pair()
drone.takeoff()
# default spiral parameters (50, 5, 1)
drone.spiral()
drone.land()
drone.close()
square()
Description
Flies the drone in the shape of a square.
Syntax
square()
square(speed, seconds, direction)
Parameters
integer speed: optional parameter that is the speed the drone will move (0 - 100). default value is 50.
integer seconds: optional parameter that is the duration in seconds the drone flies for each side of the square. default value is 5.
integer direction: optional parameter that determines the direction of the square. 1 is right, -1 is left. default value is 1.
Returns
None
Example Code
#Python code
from codrone_edu.drone import *
drone = Drone()
drone.pair()
drone.takeoff()
# default square parameters (60, 1, 1)
drone.square()
drone.land()
drone.close()
sway()
Description
Flies the drone in a swaying motion.
Syntax
sway()
sway(speed, seconds, direction)
Parameters
integer speed: optional parameter that is the speed the drone will move (0 - 100). default value is 30.
integer seconds: optional parameter that is the duration in seconds the drone will fly in each "sway" motion. default value is 2.
integer direction: optional parameter that determines the direction of the sway. 1 is left, -1 is right. default value is 1.
Returns
None
Example Code
#Python code
from codrone_edu.drone import *
drone = Drone()
drone.pair()
drone.takeoff()
# default sway parameters (30, 2, 1)
drone.sway()
drone.land()
drone.close()
triangle()
Description
Flies the drone in the shape of a triangle.
Syntax
triangle()
triangle(speed, seconds, direction)
Parameters
integer speed: optional parameter that is the speed the drone will move (0 - 100). default value is 60.
integer seconds: optional parameter that is the duration in seconds the drone flies for each side of the triangle. default value is 1.
integer direction: optional parameter that determines the direction of the triangle. 1 is right, -1 is left. default value is 1.
Returns
None
Example Code
#Python code
from codrone_edu.drone import *
drone = Drone()
drone.pair()
drone.takeoff()
# default triangle parameters (60, 1, 1)
drone.triangle()
drone.land()
drone.close()
Flight Variables
get_move_values()
Description
Previously named print_move_values(). Returns the current values of roll, pitch, yaw, and throttle flight variables.
Syntax
get_move_values()
Parameters
None
Returns
None
Example Code
# Python code
from codrone_edu.drone import *
drone = Drone()
drone.pair()
drone.set_roll(30)
drone.set_pitch(40)
drone.set_yaw(50)
drone.set_throttle(60)
move_list = drone.get_move_values() # get_move_values() returns list of flight variables
print("roll:", move_list[0])
print("pitch:", move_list[1])
print("yaw:", move_list[2])
print("throttle:", move_list[3])
drone.close()
move()
Description
The move command will move the drone based on the set flight variables (set_pitch, set_roll, etc). If given a parameter the move command will execute the movement for the given amount of seconds. If given no parameter then the drone will execute the move command indefinitley. You must takeoff() first to use a move() function.
Syntax
move(duration)
Parameters
integer duration: Duration of the movement in seconds
Returns
None
Example Code
#Python code
from codrone_edu.drone import *
drone = Drone()
drone.pair()
drone.takeoff()
# Drone goes forward for 1 second with 50 power
drone.set_pitch(50)
drone.move(1) # move command executes the movement for 1 second
drone.land()
drone.close()
print_move_values()
This function has been deprecated and will be removed in a future release. Please use get_move_values() instead, which returns move values (roll, pitch, yaw, throttle) that can be printed as needed.
Description
Prints the current values of roll, pitch, yaw, and throttle flight variables.
Syntax
print_move_values()
Parameters
None
Returns
None
Example Code
#Python code
from codrone_edu.drone import *
drone = Drone()
drone.pair()
drone.takeoff()
drone.set_pitch(50)
drone.set_roll(50)
drone.print_move_values() # will print pitch and roll at 50 and throttle and yaw at 0
drone.land()
drone.close()
reset_move()
This function has been deprecated and will be removed in a future release. Please use reset_move_values() instead.
Description
The reset_move command will reset the values of roll, pitch, yaw, and throttle to 0.
Syntax
reset_move()
reset_move(attempts)
Parameters
integer attempts: Optional parameter that sends the reset_move command multiple times.
Returns
None
Example Code
#Python code
from codrone_edu.drone import *
drone = Drone()
drone.pair()
drone.takeoff()
drone.set_pitch(50)
drone.set_roll(50)
drone.move(2)
drone.reset_move() # reset the pitch and roll to 0.
drone.move(2) # after resetting flight variables, move(2) won't move the drone
drone.land()
drone.close()
reset_move_values()
Description
The reset_move_values command will reset the values of roll, pitch, yaw, and throttle to 0.
Syntax
reset_move_values()
reset_move_values(attempts)
Parameters
integer attempts: Optional parameter that sends the reset_move_values command multiple times.
Returns
None
Example Code
#Python code
from codrone_edu.drone import *
drone = Drone()
drone.pair()
drone.takeoff()
drone.set_pitch(50)
drone.set_roll(50)
drone.move(2)
drone.reset_move_values() # reset the pitch and roll to 0.
drone.move(2) # after resetting flight variables, move(2) won't move the drone
drone.land()
drone.close()
set_pitch()
Description
This is a setter function that allows you to set the pitch variable. Once you set pitch, you have to use move() to actually execute the movement. The pitch variable will remain what you last set it until the end of the flight sequence, so you will have to set it back to 0 if you don't want the drone to pitch again.
Syntax
set_pitch(power)
Parameters
integer power: Sets the pitch variable (-100 - 100). The number represents the direction and power of the output for that flight motion variable. Negative pitch is backwards, positive pitch is forwards.
Returns
None
Example Code
#Python code
from codrone_edu.drone import *
drone = Drone()
drone.pair()
drone.takeoff()
# Drone goes forward for 1 second with 50 power
drone.set_pitch(50)
drone.move(1) # move command executes the movement for 1 second
drone.land()
drone.close()
set_roll()
Description
This is a setter function that allows you to set the roll variable. Once you set roll, you have to use move() to actually execute the movement. The roll variable will remain what you last set it until the end of the flight sequence, so you will have to set it back to 0 if you don't want the drone to roll again.
Syntax
set_roll(power)
Parameters
integer power: Sets the roll variable (-100 - 100). The number represents the direction and power of the output for that flight motion variable. Negative roll is left, positive roll is right.
Returns
None
Example Code
#Python code
from codrone_edu.drone import *
drone = Drone()
drone.pair()
drone.takeoff()
# Drone goes right for 1 second with 50 power
drone.set_roll(50)
drone.move(1) # move command executes the movement for 1 second
drone.land()
drone.close()
set_throttle()
Description
This is a setter function that allows you to set the throttle variable. Once you set throttle, you have to use move() to actually execute the movement. The throttle variable will remain what you last set it until the end of the flight sequence, so you will have to set it back to 0 if you don't want the drone to throttle again.
Syntax
set_throttle(power)
Parameters
integer power: Sets the throttle variable (-100 - 100). The number represents the direction and power of the output for that flight motion variable. Negative throttle is down, positive throttle is up.
Returns
None
Example Code
#Python code
from codrone_edu.drone import *
drone = Drone()
drone.pair()
drone.takeoff()
# Drone goes up for 1 second with 50 power
drone.set_throttle(50)
drone.move(1) # move command executes the movement for 1 second
drone.land()
drone.close()
set_yaw()
Description
This is a setter function that allows you to set the yaw variable. Once you set yaw, you have to use move()to actually execute the movement. The yaw variable will remain what you last set it until the end of the flight sequence, so you will have to set it back to 0 if you don't want the drone to yaw again.
Syntax
set_yaw(power)
Parameters
integer power: Sets the yaw variable (-100 - 100). The number represents the direction and power of the output for that flight motion variable. Negative yaw is right, positive yaw is left.
Returns
None
Example Code
#Python code
from codrone_edu.drone import *
drone = Drone()
drone.pair()
drone.takeoff()
# Drone turns left for 1 second with 50 power
drone.set_yaw(50)
drone.move(1) # move command executes the movement for 1 second
drone.land()
drone.close()
LED
controller_LED_off()
Description
Turns off the controller LEDs.
Syntax
controller_LED_off()
Parameters
None
Returns
None
Example Code
#Python code
from codrone_edu.drone import *
drone = Drone()
drone.pair()
drone.controller_LED_off()
drone.close()
drone_LED_off()
Description
Turns off the drone LED.
Syntax
drone_LED_off()
Parameters
None
Returns
None
Example Code
#Python code
from codrone_edu.drone import *
drone = Drone()
drone.pair()
drone.drone_LED_off()
drone.close()
set_controller_LED()
Description
This function sets the LED color and brightness of the CoDrone EDU controller's LEDs. This is done by setting RGB values (0 - 255) and brightness level (0 - 100).
Syntax
set_controller_LED(red, green, blue, brightness)
Parameters
integer red: pixel value for the color red (0 - 255)
integer green: pixel value for the color green (0 - 255)
integer blue: pixel value for the color blue (0 - 255)
integer brightness: brightness of the controller LED (0 - 255)
Returns
None
Example Code
#Python code
from codrone_edu.drone import *
drone = Drone()
drone.pair()
drone.set_controller_LED(0, 0, 255, 100)
drone.close()
set_drone_LED()
Description
This function sets the LED color and brightness of the CoDrone EDU's LED. This is done by setting RGB values (0 - 255) and brightness level (0 - 100).
Syntax
set_drone_LED(red, green, blue, brightness)
Parameters
integer red: pixel value for the color red (0 - 255)
integer green: pixel value for the color green (0 - 255)
integer blue: pixel value for the color blue (0 - 255)
integer brightness: brightness of the drone LED (0 - 255)
Returns
None
Example Code
#Python code
from codrone_edu.drone import *
drone = Drone()
drone.pair()
drone.set_drone_LED(0, 0, 255, 100)
drone.close()
set_controller_LED_mode()
Description
This function sets the color, mode, and speed of the mode of the CoDrone EDU controller's LED. The mode is the light pattern that the LED will have. Please note that the rainbow mode cannot be affected by the RGB values given.
Syntax
set_controller_LED_mode(r, g, b, mode, speed)
Parameters
integer red: pixel value for the color red (0 - 255)
integer green: pixel value for the color green (0 - 255)
integer blue: pixel value for the color blue (0 - 255)
string/Enum mode: the LED's light pattern, which can be an string or Enum. The available modes are:
"dimming"orControllerLEDMode.Dimming"fade_in"orControllerLEDMode.Fade_In"fade_out"orControllerLEDMode.Fade_Out"blink"orControllerLEDMode.Blink"double_blink"orControllerLEDMode.Double_Blink"rainbow"orControllerLEDMode.Rainbow
integer speed: speed of the mode (1 - 10).
Returns
None
Example Code 1
#Python code
from codrone_edu.drone import *
drone = Drone()
drone.pair()
drone.set_controller_LED_mode(0, 0, 255, "dimming", 10) # the controller's LED will have a blue dimming pattern at speed 10. The 'mode' argument is a string
drone.close()
Example Code 2
#Python code
from codrone_edu.drone import *
drone = Drone()
drone.pair()
drone.set_controller_LED_mode(0, 255, 0, ControllerLEDMode.Double_Blink, 10) # the controller's LED will have a green double-blink pattern at speed 10. The 'mode' argument is an Enum
drone.close()
Example Code 3
#Python code
from codrone_edu.drone import *
drone = Drone()
drone.pair()
drone.set_controller_LED_mode(255, 255, 255, "rainbow", 10) # the controller's LED will have a rainbow pattern at speed 10. Notice how the RGB values will not affect the rainbow pattern.
drone.close()
set_drone_LED_mode()
Description
This function sets the color, mode, and speed of the mode of the CoDrone EDU drone's LED. The mode is the light pattern that the LED will have. Please note that the rainbow mode cannot be affected by the RGB values given.
Syntax
set_drone_LED_mode(r, g, b, mode, speed)
Parameters
integer red: pixel value for the color red (0 - 255)
integer green: pixel value for the color green (0 - 255)
integer blue: pixel value for the color blue (0 - 255)
string/Enum mode: the LED's light pattern, which can be an string or Enum. The available modes are:
"dimming"orDroneLEDMode.Dimming"fade_in"orDroneLEDMode.Fade_In"fade_out"orDroneLEDMode.Fade_Out"blink"orDroneLEDMode.Blink"double_blink"orDroneLEDMode.Double_Blink"rainbow"orDroneLEDMode.Rainbow
integer speed: speed of the mode (1 - 10).
Returns
None
Example Code 1
#Python code
from codrone_edu.drone import *
drone = Drone()
drone.pair()
drone.set_drone_LED_mode(0, 0, 255, "dimming", 10) # the drone's LED will have a blue dimming pattern at speed 10. The 'mode' argument is a string
drone.close()
Example Code 2
#Python code
from codrone_edu.drone import *
drone = Drone()
drone.pair()
drone.set_drone_LED_mode(0, 255, 0, DroneLEDMode.Double_Blink, 10) # the drone's LED will have a green double-blink pattern at speed 10. The 'mode' argument is an Enum
drone.close()
Example Code 3
#Python code
from codrone_edu.drone import *
drone = Drone()
drone.pair()
drone.set_drone_LED_mode(255, 255, 255, "rainbow", 10) # the drone's LED will have a rainbow pattern at speed 10. Notice how the RGB values will not affect the rainbow pattern.
drone.close()
Sounds
controller_buzzer()
Description
Plays a note using the controller's buzzer.
Syntax
controller_buzzer(note, duration)
Parameters
integer/Note note: frequency of the note, in Hertz or a Note object
integer duration: Duration of the note in milliseconds
Returns
None
Example Code
#Python code
from codrone_edu.drone import *
drone = Drone()
drone.pair()
drone.controller_buzzer(400, 300)
drone.controller_buzzer(600, 300)
drone.close()
drone_buzzer()
Description
Plays a note using the drone's buzzer.
Syntax
drone_buzzer(note, duration)
Parameters
integer/Note note: frequency of the note, in Hertz or a Note object
integer duration: Duration of the note in milliseconds
Returns
None
Example Code
#Python code
from codrone_edu.drone import *
drone = Drone()
drone.pair()
drone.drone_buzzer(400, 300)
drone.drone_buzzer(600, 300)
drone.close()
start_drone_buzzer()
Description
This function allows the drone buzzer to be played in the background while other commands are given to the drone.
Syntax
start_drone_buzzer(note)
Parameters
integer/Note note: the frequency of the note, in Hertz or a Note object
Returns
None
Example Code
#Python code
from codrone_edu.drone import *
drone = Drone()
drone.pair()
drone.start_drone_buzzer(500) # starting the buzzer
# these commands run while the buzzer runs in the background
for i in range(5):
drone.set_drone_LED(255, 0, 0, 100)
time.sleep(0.5)
drone.set_drone_LED(0, 255, 0, 100)
time.sleep(0.5)
drone.stop_drone_buzzer() # stops the buzzer
drone.close()
stop_drone_buzzer()
Description
Stops the drone buzzer if started in the background.
Syntax
stop_drone_buzzer()
Parameters
None
Returns
None
Example Code
#Python code
from codrone_edu.drone import *
drone = Drone()
drone.pair()
drone.start_drone_buzzer(500) # starting the buzzer
# these commands run while the buzzer runs in the background
for i in range(5):
drone.set_drone_LED(255, 0, 0, 100)
time.sleep(0.5)
drone.set_drone_LED(0, 255, 0, 100)
time.sleep(0.5)
drone.stop_drone_buzzer() # stops the buzzer
drone.close()
start_controller_buzzer()
Description
This function allows the controller buzzer to be played in the background while other commands are given to the drone.
Syntax
start_controller_buzzer(note)
Parameters
integer/Note note: the frequency of the note, in Hertz or a Note object
Returns
None
Example Code
#Python code
from codrone_edu.drone import *
drone = Drone()
drone.pair()
drone.start_controller_buzzer(Note.A4) # starting the buzzer
# these commands run while the buzzer runs in the background
for i in range(3):
drone.set_controller_LED(255, 0, 0, 100)
time.sleep(0.5)
drone.set_controller_LED(0, 255, 0, 100)
time.sleep(0.5)
drone.stop_controller_buzzer() # stops the buzzer
drone.close()
stop_controller_buzzer()
Description
Stops the controller buzzer if started in the background.
Syntax
stop_controller_buzzer()
Parameters
None
Returns
None
Example Code
#Python code
from codrone_edu.drone import *
drone = Drone()
drone.pair()
drone.start_controller_buzzer(440) # starting the buzzer
# these commands run while the buzzer runs in the background
for i in range(3):
drone.set_controller_LED(255, 0, 0, 100)
time.sleep(0.5)
drone.set_controller_LED(0, 255, 0, 100)
time.sleep(0.5)
drone.stop_controller_buzzer() # stops the buzzer
drone.close()
drone_buzzer_sequence()
Description
This function will create a sound sequence using the drone's buzzer. This function can be a creative way to indicate the degree of success of the objective that the program is trying to achieve. There are three sound sequences to choose from: 'success', 'warning', 'error'.
Syntax
drone_buzzer_sequence()
Parameters
string kind: The type of sound sequence that the drone's buzzer will create.
Returns
None
Example
#Python code
from codrone_edu.drone import *
drone = Drone()
drone.pair()
x = int(input("Enter a number:"))
if x > 5:
drone.drone_buzzer_sequence("success") # drone's buzzer creates 'success' sound sequence
else:
drone.drone_buzzer_sequence("error") # drone's buzzer creates 'error' sound sequence
drone.close()
controller_buzzer_sequence()
Description
This function will create a sound sequence using the controller's buzzer. This function can be a creative way to indicate the degree of success of the objective that the program is trying to achieve. There are three sound sequences to choose from: 'success', 'warning', 'error'.
Syntax
drone_buzzer_sequence()
Parameters
string kind: The type of sound sequence that the controller's buzzer will create.
Returns
None
Example
#Python code
from codrone_edu.drone import *
drone = Drone()
drone.pair()
x = int(input("Enter a number:"))
if x > 5:
drone.controller_buzzer_sequence("success") # controller's buzzer creates 'success' sound sequence
else:
drone.controller_buzzer_sequence("error") # controller's buzzer creates 'error' sound sequence
drone.close()
ping()
Description
This function will ping the drone, creating a beeping sound while the drone's LED is blinking. The LED color for the ping command can be specified, but if nothing is specified, then the color's RGB values will be randomly selected. This is a useful, creative way to identify the drone that you are using in an environment full of other drones.
Syntax
ping()
ping(r=None,g=None,b=None)
Parameters
integer r: The red pixel value for the LED color, 0 - 255. Default is None. If None, regardless if the others are defined, the drone will use a random color.
integer g: The green pixel value for the LED color, 0 - 255. Default is None. If None, regardless if the others are defined, the drone will use a random color.
integer b: The blue pixel value for the LED color, 0 - 255. Default is None. If None, regardless if the others are defined, the drone will use a random color.
Returns
None
Example
#Python code
from codrone_edu.drone import *
drone = Drone()
drone.pair()
drone.ping() # drone's buzzer beeps and LED blinks a random color
drone.close()
Sensors (Range Sensor)
detect_wall()
Description
Returns True when a distance below the threshold is reached. The sensor range is up to 1.5 meters (150cm)
Syntax
detect_wall()
detect_wall(distance)
Parameters
integer distance: An optional parameter that is the threshold in centimeters that will return True. The default value is 50
Returns
None
Example Code
#Python code
from codrone_edu.drone import *
drone = Drone()
drone.pair()
drone.takeoff()
# if a wall is detected in less than 50cm true will be returned.
if drone.detect_wall():
print("wall detected!")
else:
print("no wall detected!")
drone.land()
drone.close()
get_bottom_range()
Description
This is a getter function which returns the current bottom range of the drone. The default unit of measurement is centimeters. This function uses the bottom range sensor to measure distance from the drone to the surface below the drone.
NOTE: The bottom range sensor will be able to get an exact reading of 0 if the drone is sitting on a surface. If the drone is sitting on a surface, the bottom range sensor turns off (causing a reading of 0), so that the color sensor can turn on and detect color without any interference. It is safe to assume that if the drone is sitting on a surface, the bottom range or height is 0.
Syntax
get_bottom_range()
get_bottom_range(unit="cm") — "m", "mm", and "in" are other options for unit
Parameters
string unit: The unit of measurement that is chosen for the height distance. Available units are "m" (meter), "cm" (centimeter), "mm" (millimeter), or "in" (inch). If a parameter is not specified cm is chosen by default.
Returns
integer bottom range: The current bottom range calculated by the bottom range sensor (cm default). -100 or 0 when the sensor returns an error. 999.9 when the detected object is out of range (1.5 meters) or the sensor timed out.
Example Code
#Python code
import time
from codrone_edu.drone import *
drone = Drone()
drone.pair()
max_bottom_range = 100
drone.takeoff()
drone.set_throttle(50)
current_bottom_range = drone.get_bottom_range("cm")
while current_bottom_range <= max_bottom_range:
drone.move()
current_bottom_range = drone.get_bottom_range("cm")
print(current_bottom_range)
drone.land()
drone.close()
get_front_range()
Description
This is a getter function which returns the current front range of the drone. The default unit of measurement is centimeters. This function uses the front range sensor to measure distance from the drone to the surface in front of the drone.
NOTE: The front range sensor will not be able to get an exact reading of 0 due to light scattering and interference. The lowest value you may see is approximately 9mm (0.9cm) by covering the sensor.
Syntax
get_front_range()
get_front_range(unit="cm") — "m", "mm", and "in" are other options for unit
Parameters
string unit: A string for the unit of measurement that is chosen for the range distance. Available units are "m" (meter), "cm" (centimeter), "mm" (millimeter), or "in" (inch). If a parameter is not specified "cm" is chosen by default.
Returns
integer front range: The current range calculated by the front range sensor (cm by default). -10 or 0 when the sensor returns an error value. 999 when the detected object is out of range (1.5 meters) or the sensor timed out.
Example Code
This example does not require any flying. Once code is running, grab and move drone towards and away from a wall to see differences in front range values on the console.
#Python code
import time
from codrone_edu.drone import *
drone = Drone()
drone.pair()
start_time = time.time() # set initial time, in seconds
# loop runs for 20 seconds
while time.time() - start_time < 20:
print(drone.get_front_range())
time.sleep(0.05)
drone.close()
get_height()
Description
This is a getter function which returns the current height of the drone. The default unit of measurement is centimeters. This function uses the bottom range sensor to measure distance from the drone to the surface below the drone.
NOTE: The bottom range sensor will be able to get an exact reading of 0 if the drone is sitting on a surface. If the drone is sitting on a surface, the bottom range sensor turns off (causing a reading of 0), so that the color sensor can turn on and detect color without any interference. It is safe to assume that if the drone is sitting on a surface, the bottom range or height is 0.
Syntax
get_height()
get_height(unit="cm") — "m", "mm", and "in" are other options for unit
Parameters
string unit: The unit of measurement that is chosen for the height distance. Available units are "m" (meter), "cm" (centimeter), "mm" (millimeter), or "in" (inch). If a parameter is not specified cm is chosen by default.
Returns
integer height: The current range calculated by the bottom range sensor (cm default). -100 or 0 when the sensor returns an error. 999.9 when the detected object is out of range (1.5 meters) or the sensor timed out.
Example Code
#Python code
import time
from codrone_edu.drone import *
drone = Drone()
drone.pair()
max_height = 100
drone.takeoff()
drone.set_throttle(50)
current_height = drone.get_height("cm")
while current_height <= max_height:
drone.move()
current_height = drone.get_height("cm")
print(current_height)
drone.land()
drone.close()
Sensors (Optical Flow Sensor)
get_pos_x()
Description
Getter function that gets the x position of the drone. (x is forwards and backwards). NOTE: The drone's position values are reset to 0 when drone takes off or battery is re-inserted.
Syntax
get_pos_x()
get_pos_x(unit="cm") — "m", "mm", and "in" are other options for unit
Parameters
string unit: The unit of measurement that is chosen for the position distance. Available units are "m" (meter), "cm" (centimeter), "mm" (millimeter), or "in" (inch). If a parameter is not specified cm is chosen by default.