We have been trying to fly our drone autonomously using dronekit,python, but have only been using internal IMU. The drone flies very violently, going out of control, sometimes suddenly increasing the throttle by itself and sometimes dropping fast to the ground. When we try to move the drone forward, the motors and everything shuts down, below is the code and also the log, failsafe was disabled and arming check enabled. We are not using GPS. Using pixhawk 2.4.8
Log file
Code:-
from dronekit import connect, VehicleMode
import time, sys
# ========== Configuration ==========
CONNECTION_PORT = "COM15"
TARGET_ALTITUDE = 3 # Target altitude in meters
HOVER_DURATION = 20 # Hover time in seconds
FORWARD_DURATION = 10 # Forward move time in seconds
ARMING_TIMEOUT = 30 # Timeout for arming in seconds
THROTTLE_MIN = 1250 # Starting throttle
THROTTLE_HOVER = 1400 # Estimated hover throttle (adjust per drone)
THROTTLE_MAX = 1400 # Maximum throttle
THROTTLE_LAND_MIN = 1200 # Soft landing throttle
flight_mode = "STABILIZE" # Recommended: POSHOLD or STABILIZE
# ========== Connect to the Drone ==========
def connect_drone():
print(f"Connecting to drone on {CONNECTION_PORT}...")
try:
vehicle = connect(CONNECTION_PORT, baud=57600, wait_ready=False)
print(" Connected successfully!")
return vehicle
except Exception as e:
print(f" Connection failed: {e}")
sys.exit(1)
# ========== Arm and Gradually Take Off ==========
def arm_and_takeoff(vehicle, target_altitude, arm_timeout):
print(f"Setting Mode: {flight_mode}...")
vehicle.mode = VehicleMode(flight_mode)
start_time = time.time()
while not vehicle.mode.name == flight_mode:
print(f"Waiting for mode change to {flight_mode}...")
time.sleep(1)
if time.time() - start_time > arm_timeout:
print(" Mode change failed.")
sys.exit(1)
print("Arming motors...")
vehicle.armed = True
while not vehicle.armed:
print("Waiting for arming...")
time.sleep(1)
if time.time() - start_time > arm_timeout:
print(" Arming failed.")
sys.exit(1)
print(" Motors armed. Starting takeoff...")
throttle = THROTTLE_MIN
vehicle.channels.overrides['3'] = throttle
target_reached = False
while True:
alt = vehicle.location.global_relative_frame.alt
print(f"Throttle: {throttle}, Altitude: {alt:.2f}m")
if alt >= target_altitude * 0.95:
print(" Target altitude reached!")
target_reached = True
break
if throttle >= THROTTLE_MAX:
print(" Max throttle reached, but altitude not achieved.")
break # Proceed anyway
throttle += 2
vehicle.channels.overrides['3'] = throttle
time.sleep(0.15)
if not target_reached:
print(" Proceeding mission at suboptimal altitude...")
# ========== Hover in Place ==========
def hover(vehicle, duration):
print(f" Hovering for {duration} seconds...")
vehicle.channels.overrides['3'] = THROTTLE_HOVER # Set throttle once
start = time.time()
while time.time() - start < duration:
alt = vehicle.location.global_relative_frame.alt
battery = vehicle.battery
print(f"Altitude: {alt:.2f}m")
time.sleep(0.4)
# ========== Move Forward ==========
def move_forward(vehicle, duration):
print(f" Moving forward for {duration} seconds...")
vehicle.channels.overrides = {
'2': 1600, # Pitch forward
'3': THROTTLE_HOVER # Maintain throttle
}
time.sleep(duration)
vehicle.channels.overrides = {
'2': 1495,
'3': THROTTLE_HOVER
}
print(" Forward movement stopped.")
# ========== Soft Landing ==========
def soft_land(vehicle):
print(" Initiating soft landing...")
throttle = THROTTLE_HOVER
while vehicle.armed:
alt = vehicle.location.global_relative_frame.alt
if alt > 1.0:
print(f"Descending... Altitude: {alt:.2f}m")
throttle -= 2
if throttle < THROTTLE_LAND_MIN:
throttle = THROTTLE_LAND_MIN
vehicle.channels.overrides['3'] = throttle
time.sleep(0.2)
else:
print("Final descent...")
vehicle.channels.overrides = {}
vehicle.mode = VehicleMode("LAND")
time.sleep(1)
break
# ========== Main Flight Sequence ==========
def main():
vehicle = connect_drone()
try:
arm_and_takeoff(vehicle, TARGET_ALTITUDE, ARMING_TIMEOUT)
hover(vehicle, HOVER_DURATION)
move_forward(vehicle, FORWARD_DURATION)
soft_land(vehicle)
except KeyboardInterrupt:
print("\n Emergency landing initiated!")
soft_land(vehicle)
except Exception as e:
print(f" Error: {e}")
soft_land(vehicle)
finally:
vehicle.channels.overrides = {}
vehicle.close()
print(" Disconnected safely.")
# ========== Entry Point ==========
if __name__ == "__main__":
main()`Preformatted text`