My code is running as double execute

I have a code about basic movement thrusters. but it dosen’t work properly, when ı run the code first time it doesn’t work however when ı run the code again it works, this is a vicious cycle. what is the problem here ?

connection, hearbeat, arming and disarming working properly.

my code here :

from pymavlink import mavutil
import time

def arm_vehicle(master):
    if not master.motors_armed():
        master.mav.command_long_send(
            master.target_system,
            master.target_component,
            mavutil.mavlink.MAV_CMD_COMPONENT_ARM_DISARM,
            0,
            1, 0, 0, 0, 0, 0, 0
        )
        print("Waiting for the vehicle to arm")
        master.motors_armed_wait()
        print('Armed!')

def disarm_vehicle(master):
    if master.motors_armed():
        master.mav.command_long_send(
            master.target_system,
            master.target_component,
            mavutil.mavlink.MAV_CMD_COMPONENT_ARM_DISARM,
            0,
            0, 0, 0, 0, 0, 0, 0
        )
        print("Waiting for the vehicle to disarm")
        master.motors_disarmed_wait()
        print('Disarmed!')

def move_vehicle(master, x, y, z, full_power_duration, slow_down_duration, steps=10):
    # Full power movement
    master.mav.manual_control_send(
        master.target_system,
        x,  # x
        y,  # y
        z,  # z
        0,  # r
        0   # buttons
    )
    time.sleep(full_power_duration)
    
    # Slow down movement
    step_duration = slow_down_duration / steps
    for step in range(steps):
        master.mav.manual_control_send(
            master.target_system,
            int(x * (1 - step / steps)),  # x
            int(y * (1 - step / steps)),  # y
            int(z),  # z
            0,  # r
            0   # buttons
        )
        time.sleep(step_duration)
    
    # Stop movement after the duration
    master.mav.manual_control_send(
        master.target_system,
        0,  # x
        0,  # y
        500,  # z (neutral throttle)
        0,  # r
        0   # buttons
    )
    time.sleep(1)  # Ensure a brief pause to stop completely

# address = "/dev/ttyTHS1:115200"
address = "/dev/ttyACM0"
master = mavutil.mavlink_connection(address, 115200, autoreconnect=True)
master.wait_heartbeat()
arm_vehicle(master)

# Move forward for 5 seconds at full power, then slow down over 2 seconds
move_vehicle(master, 1000, 0, 500, 5, 2)
time.sleep(5)

# Move backward for 5 seconds at full power, then slow down over 2 seconds
move_vehicle(master, -1000, 0, 500, 5, 2)
time.sleep(5)

# Move right for 5 seconds at full power, then slow down over 2 seconds
move_vehicle(master, 0, 1000, 500, 5, 2)
time.sleep(5)

# Move left for 5 seconds at full power, then slow down over 2 seconds
move_vehicle(master, 0, -1000, 500, 5, 2)
time.sleep(5)

# Move up for 5 seconds at full power, then slow down over 2 seconds
move_vehicle(master, 0, 0, 1000, 5, 2)
time.sleep(5)

# Move down for 5 seconds at full power, then slow down over 2 seconds
move_vehicle(master, 0, 0, 0, 5, 2)
time.sleep(5)

disarm_vehicle(master)

Hi @maliduran, welcome to the forum :slight_smile:

Can you explain a bit more about how your system is set up, what else is connected to it?

Checking for a single heartbeat makes sense if the autopilot is the only other MAVLink component in the system, but that’s not the case for BlueOS, so you may need to explicitly check for the vehicle heartbeat.

Does this part mean that only the movement parts are failing to work as expected? It’s not clear what is and isn’t working in your testing.


By the way, I’ve edited your post to have the code in a code block, which formats it and makes it easier to read, or copy for testing. You can read more about that in the Formatting a Post/Comment section of the “How to Use the Forums” post :slight_smile: