Drone autonomous-flying uncontrollably while using dronekit

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("![✅](https://fonts.gstatic.com/s/e/notoemoji/16.0/2705/72.png) Connected successfully!")

return vehicle

except Exception as e:

print(f"![❌](https://fonts.gstatic.com/s/e/notoemoji/16.0/274c/72.png) 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("![❌](https://fonts.gstatic.com/s/e/notoemoji/16.0/274c/72.png) 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("![❌](https://fonts.gstatic.com/s/e/notoemoji/16.0/274c/72.png) Arming failed.")

sys.exit(1)

print("![✅](https://fonts.gstatic.com/s/e/notoemoji/16.0/2705/72.png) 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("![✅](https://fonts.gstatic.com/s/e/notoemoji/16.0/2705/72.png) Target altitude reached!")

target_reached = True

break

if throttle >= THROTTLE_MAX:

print("![⚠️](https://fonts.gstatic.com/s/e/notoemoji/16.0/26a0_fe0f/72.png) 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("![⚠️](https://fonts.gstatic.com/s/e/notoemoji/16.0/26a0_fe0f/72.png) Proceeding mission at suboptimal altitude...")

# ========== Hover in Place ==========

def hover(vehicle, duration):

print(f"![🛑](https://fonts.gstatic.com/s/e/notoemoji/16.0/1f6d1/72.png) 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"![➡️](https://fonts.gstatic.com/s/e/notoemoji/16.0/27a1_fe0f/72.png) 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("![⏹️](https://fonts.gstatic.com/s/e/notoemoji/16.0/23f9_fe0f/72.png) Forward movement stopped.")

# ========== Soft Landing ==========

def soft_land(vehicle):

print("![🪂](https://fonts.gstatic.com/s/e/notoemoji/16.0/1fa82/72.png) 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![🛑](https://fonts.gstatic.com/s/e/notoemoji/16.0/1f6d1/72.png) Emergency landing initiated!")

soft_land(vehicle)

except Exception as e:

print(f"![❌](https://fonts.gstatic.com/s/e/notoemoji/16.0/274c/72.png) Error: {e}")

soft_land(vehicle)

finally:

vehicle.channels.overrides = {}

vehicle.close()

print("![✅](https://fonts.gstatic.com/s/e/notoemoji/16.0/2705/72.png) Disconnected safely.")

# ========== Entry Point ==========

if __name__ == "__main__":

main()`Preformatted text`

Do your drone equipped with GPS?

sorry for not mentioning it in the post, we are not using gps

If you are not using GPS, how do you get position of drone?

if you don’t use GPS, it can’t be autonomosly controlled unless you are using slam or other positioning sensors.

1 Like

so just depending on the internal IMU of pixhawk will not work? But we know the proper dimensions of the area in which we are trying to fly our drone so can’t we think like we know the speed and the distance, so just hard coding the values and dimensions, we can make the drone to the things we want it to do

And the drone need to be correctly configured before it can operate autonomously.

Have you configured it correctly? Using the documentation? Or did you used ArduPilot methodic configurator software?

We have done all the configurations multiple times which are there in mission planner, but haven’t done anything with ardupilot methodic configurator software. Is there a specific configuration needed for flying drone autonomously? Sorry I am a beginner

Did you configure the notch filter and the altitude controller? Did you do a magfit flight?

There is not a single specific configuration needed for flying a drone autonomously.
There are multiple parameters and configurations that are needed for flying a drone autonomously.
All of them are covered/semi-automated by the ArduPilot methodic configurator software.

1 Like

okay so we will do all configuration using the ardupilot methodic configurator software and will get back to you, thank you for your replies and time!

Do all the steps in the correct sequence and you’ll be fine.

okay, thank you so much sir.

No, you would need an IMU order or two of magnitude more expensive than good RTK GNSS receiver, as well as dealing with ITAR/EAR KYC rules, to get useable navigation on a multirotor.

What @LupusTheCanine is saying is that you can get a multicopter operating without GPS but you need to buy a 200.000$ IMU to do it. And the longer the distances you want to fly the more expensive the IMU. Our advice: don’t do that.

Read this instead.

1 Like