无人机python代码
时间: 2023-09-19 14:12:25 浏览: 37
以下是无人机的 Python 代码示例:
```python
import dronekit
from dronekit import connect, VehicleMode
connection_string = 'udp:127.0.0.1:14550'
vehicle = connect(connection_string, wait_ready=True)
def arm_and_takeoff(aTargetAltitude):
"""
Arms vehicle and fly to aTargetAltitude.
"""
print("Basic pre-arm checks")
# Don't let the user try to arm until autopilot is ready
while not vehicle.is_armable:
print(" Waiting for vehicle to initialise...")
time.sleep(1)
print("Arming motors")
# Copter should arm in GUIDED mode
vehicle.mode = VehicleMode("GUIDED")
vehicle.armed = True
# Confirm vehicle armed before attempting to take off
while not vehicle.armed:
print(" Waiting for arming...")
time.sleep(1)
print("Taking off!")
vehicle.simple_takeoff(aTargetAltitude)
# Wait until the vehicle reaches a safe height before processing the goto
# (otherwise the command after Vehicle.simple_takeoff will execute
# immediately).
while True:
print(" Altitude: ", vehicle.location.global_relative_frame.alt)
# Break and return from function just below target altitude.
if vehicle.location.global_relative_frame.alt >= aTargetAltitude * 0.95:
print("Reached target altitude")
break
time.sleep(1)
arm_and_takeoff(10)
print("Landing...")
vehicle.mode = VehicleMode("LAND")
# Close vehicle object before exiting script
vehicle.close()
```
这只是一个简单的示例,你可以根据你的需求进行修改和扩展。请确保在飞行前对代码进行充分的测试,并遵守当地的无人机法规。