Drone suddenly crashed in Dronekit after Armed and Takeoff

I was trying to test the drone movement using python scripts. I already tried multiple testing and this was supposed to be the last test for today.

The aim was to let it hover at 10 meters and let it stay there for 10 seconds before it moves north, south, east, west at 2m/s.
Here’s my code:

from dronekit import connect, VehicleMode, LocationGlobalRelative
from pymavlink import mavutil
import time

vehicle = None

def connectMyCopter():
    vehicle = connect('/dev/ttyS0', baud=57600, wait_ready=True)
    print('Vehicle is ready and armed.')
    return vehicle


def arm_and_takeoff(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 ot initialise")
        time.sleep(1)
        
    
    print("Arming motors")
    #Copter should arm in GUIDED mode
    vehicle.mode = VehicleMode("GUIDED")
    while vehicle.mode!= "GUIDED":
        print("Waiting for vehicle to enter GUIDED mode")
        vehicle.mode = VehicleMode("GUIDED")
        time.sleep(1)
    
    
    vehicle.armed = True
    while not vehicle.armed:
        print("Waiting for arming...")
        vehicle.armed = True
        time.sleep(1)
        
    
    print("Taking off!")
    vehicle.simple_takeoff(aTargetAltitude) #Take off to target altitude
    
    #Check that vehicle has reached takeoff altitude
    while True:
        print("Altitude", vehicle.location.global_relative_frame.alt)
        if vehicle.location.global_relative_frame.alt >= aTargetAltitude*0.95:
            print("Reached target altitude")
            break
        time.sleep(1)
        
    print("Target altitude reached!")
    return None


def set_velocity_body(Vx, Vy, Vz):
    msg = vehicle.message_factory.set_position_target_local_ned_encode(
        0,
        0,0,
        mavutil.mavlink.MAV_FRAME_BODY_OFFSET_NED,
        0b0000111111000111, #BITMASK > Consider only the velocities
        0, 0, 0, #--Position
        Vx, Vy, Vz, #--Velocity
        0, 0, 0, #--Acceleration
        0, 0)
    vehicle.send_mavlink(msg)
    vehicle.flush()


# Main #
vehicle = connectMyCopter()

arm_and_takeoff(5)
time.sleep(10)


counter = 0
while counter<3:
    set_velocity_body(2,0,0)
    print("Direction: NORTH relative to heading of drone")
    time.sleep(1)
    counter = counter+1
 
 
counter = 0
while counter<3:
    set_velocity_body(-2,0,0)
    print("Direction: SOUTH relative to heading of drone")
    time.sleep(1)
    counter = counter+1
    
    
counter = 0
while counter<3:
    set_velocity_body(0,2,0)
    print("Direction: EAST relative to heading of drone")
    time.sleep(1)
    counter = counter+1
 
 
counter = 0
while counter<3:
    set_velocity_body(0,-2,0)
    print("Direction: WEST relative to heading of drone")
    time.sleep(1)
    counter = counter+1

print("Landing where it's launched from!")
vehicle.mode = VehicleMode("RTL") #Lands where it launched from

time.sleep(2)
vehicle.close()

However, before it even get to start moving north, it behaved abnormally. It says something about EKF variance or glitches something. I wasn’t able to record the warnings from the script compiler. I have also tried multiple testing before this happened.
I would like to ask is if the cause of this behavior due to a loss of GPS signal the moment it took off? Or are there any reasons behind its sudden abnormal behavior?