Created
October 6, 2024 09:17
-
-
Save si3mshady/9cf841c978733866b9b6cf56f68101a0 to your computer and use it in GitHub Desktop.
Drone Control with AI Agents and Natural Language Commands - Here’s a description you can use for the GitHub Gist: Drone Control with AI Agents and Natural Language Commands This Python script leverages AI agents to control the takeoff, landing, and in-flight commands of a drone using natural language input. The code is designed to allow users t…
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
from dronekit import connect, VehicleMode | |
from langchain.agents import initialize_agent, AgentType | |
from langchain_openai import OpenAI | |
from langchain.tools import Tool | |
# Set target altitude and manual arm setting | |
targetAltitude = 3 | |
manualArm = False | |
# Connect to the drone | |
vehicle = connect('/dev/ttyACM0', baud=57600, wait_ready=True) | |
# Define tools | |
def check_gps(user_input=""): | |
"""Check and return the drone's current GPS coordinates.""" | |
lat = vehicle.location.global_frame.lat | |
lon = vehicle.location.global_frame.lon | |
alt = vehicle.location.global_frame.alt | |
return f"GPS Coordinates: Latitude={lat}, Longitude={lon}, Altitude={alt} meters" | |
def land_drone(user_input=""): | |
"""Land the drone safely.""" | |
print("Initiating landing...") | |
vehicle.mode = VehicleMode("LAND") | |
# Nested loop to ensure the drone switches to LAND mode | |
while vehicle.mode != 'LAND': | |
print("Waiting for the drone to switch to LAND mode...") | |
time.sleep(1) | |
print("Drone has successfully switched to LAND mode. Landing now.") | |
return "Landing complete" | |
def arm_and_takeoff_tool(user_input=""): | |
"""Arm the drone and takeoff to the target altitude.""" | |
print("Starting arm and takeoff process.") | |
# Wait until the vehicle is armable | |
while not vehicle.is_armable: | |
print("Waiting for vehicle to become armable.") | |
time.sleep(1) | |
print("Vehicle is now armable") | |
# Set the vehicle mode to GUIDED | |
vehicle.mode = VehicleMode("GUIDED") | |
# Wait until the vehicle is in GUIDED mode | |
while vehicle.mode != 'GUIDED': | |
print("Waiting for drone to enter GUIDED flight mode") | |
time.sleep(1) | |
print("Vehicle now in GUIDED MODE. Preparing to arm.") | |
# Arm the vehicle if manualArm is False | |
if not manualArm: | |
vehicle.armed = True | |
while not vehicle.armed: | |
print("Waiting for vehicle to become armed.") | |
time.sleep(1) | |
else: | |
if not vehicle.armed: | |
print("Manual arm is enabled but the vehicle is not armed.") | |
return "Failed to arm the drone." | |
print("Props are spinning! Takeoff initiated...") | |
# Start takeoff | |
vehicle.simple_takeoff(targetAltitude) | |
return f"Takeoff initiated to {targetAltitude} meters." | |
# Initialize the language model | |
llm = OpenAI(temperature=0) | |
# Define tools for the agent | |
tools = [ | |
Tool(name="CheckGPS", func=check_gps, description="Check the drone's current GPS coordinates."), | |
Tool(name="LandDrone", func=land_drone, description="Land the drone safely."), | |
Tool(name="ArmAndTakeoff", func=arm_and_takeoff_tool, description="Arm the drone and initiate takeoff."), | |
] | |
# Initialize the agent with tools | |
agent = initialize_agent( | |
tools, | |
llm, | |
agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, | |
verbose=True | |
) | |
#########FUNCTIONS########### | |
def monitor_altitude_and_call_tools(targetHeight): | |
"""Monitor altitude and allow the user to call tools while the drone is ascending.""" | |
while True: | |
current_altitude = vehicle.location.global_relative_frame.alt | |
print(f"Current Altitude: {current_altitude:.2f} meters") | |
# Check if target altitude is reached | |
if current_altitude >= 0.95 * targetHeight: | |
print("Target altitude reached! Preparing to land automatically.") | |
vehicle.mode = VehicleMode("LAND") | |
break | |
# Use AI agent to handle user input naturally | |
user_input = input("Enter a command: ") | |
response = agent.run(user_input) # Let the agent decide which tool to use | |
print(response) | |
time.sleep(1) | |
return "Takeoff complete" | |
############MAIN############### | |
# Main loop for continuous interaction | |
while True: | |
user_input = input("Enter a command (or 'quit' to exit): ").lower() | |
if user_input == 'quit': | |
print("Exiting script...") | |
break | |
# AI agent interprets the user input and runs the relevant tool | |
response = agent.run(user_input) | |
print(response) | |
# Ensure that the drone is landed safely before exiting | |
vehicle.mode = VehicleMode("LAND") | |
# Wait until the drone is in LAND mode | |
while vehicle.mode != 'LAND': | |
print("Waiting for the drone to land...") | |
time.sleep(1) | |
print("Drone is in land mode. Exiting script.") | |
# Close the connection | |
vehicle.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment