How to Control Drone Yaw Using DroneKit: Simple Guide
To control drone yaw using
dronekit, use the condition_yaw() function to set the desired heading angle and rotation direction. This function commands the drone to rotate to a specific yaw angle smoothly while flying.Syntax
The condition_yaw() function controls the drone's yaw (rotation around vertical axis). It takes these main parameters:
heading: Target yaw angle in degrees (0-360).relative: Boolean to specify if yaw is absolute or relative to current heading.yaw_rate: Optional speed of yaw in degrees per second.direction: Optional rotation direction: 1 for clockwise, -1 for counter-clockwise, 0 for shortest path.
This function sends a MAVLink command to the drone to rotate its heading accordingly.
python
vehicle.condition_yaw(heading, relative=False, yaw_rate=None, direction=0)
Example
This example shows how to connect to a drone, arm it, take off, and then rotate the drone 90 degrees clockwise using condition_yaw(). It waits for the yaw to complete before landing.
python
from dronekit import connect, VehicleMode import time # Connect to the vehicle vehicle = connect('127.0.0.1:14550', wait_ready=True) # Arm and take off to 10 meters vehicle.mode = VehicleMode('GUIDED') vehicle.armed = True while not vehicle.armed: time.sleep(1) vehicle.simple_takeoff(10) # Wait until the vehicle reaches a safe height while True: if vehicle.location.global_relative_frame.alt >= 9.5: break time.sleep(1) # Rotate drone 90 degrees clockwise vehicle.condition_yaw(90, relative=True, direction=1) # Wait for yaw to complete time.sleep(5) # Land the drone vehicle.mode = VehicleMode('LAND') # Close vehicle object vehicle.close()
Output
No printed output; drone takes off, yaws 90 degrees clockwise, then lands.
Common Pitfalls
Common mistakes when controlling yaw with DroneKit include:
- Not setting
relative=Truewhen you want to rotate relative to current heading. - Ignoring the
directionparameter, which can cause the drone to rotate the long way around. - Not waiting enough time after
condition_yaw()to let the drone finish rotating. - Trying to yaw while the drone is not in
GUIDEDmode or not armed.
python
## Wrong way: yaw without relative=True vehicle.condition_yaw(90) # rotates to absolute 90 degrees, not relative ## Right way: yaw relative 90 degrees clockwise vehicle.condition_yaw(90, relative=True, direction=1)
Quick Reference
Tips for controlling yaw with DroneKit:
- Use
relative=Trueto rotate relative to current heading. - Set
direction=1for clockwise,-1for counter-clockwise rotation. - Use
yaw_rateto control rotation speed. - Always ensure drone is armed and in
GUIDEDmode before yaw commands. - Wait after yaw command to let rotation complete before next action.
Key Takeaways
Use vehicle.condition_yaw() to control drone yaw angle precisely.
Set relative=True for relative yaw rotation and direction to control rotation side.
Ensure drone is armed and in GUIDED mode before sending yaw commands.
Wait sufficient time after yaw command for the drone to complete rotation.
Yaw angles are in degrees from 0 to 360, representing compass heading.