Sunday, 26 March 2023

How to check if the drone is armed or not using MAVLink/MAVProxy?

I want to run a python script when the drone is armed and stop running that python script when the drone is disarmed using MAVProxy. To check that I want to first check if the drone is armed or not. I tried 2 types of code but both of them are giving inconsistent results. It is showing armed but after some time it shows disarmed even though its armed.

I tried 2 types of code but both of them are giving inconsistent results. It is showing armed but after some time it shows disarmed even though its armed.

CODE 1:

from pymavlink import mavutil
#import sensor # logging file
import time
import sys

# Start a connection listening on a UDP port
# Once connected, use 'the_connection' to get and send messages


master = mavutil.mavlink_connection('/dev/ttyACM0')


while True:
       


# Wait for the connection to establish
 master.wait_heartbeat()


#Check if the drone is armed

 if master.motors_armed():
    print("The drone is armed.")
    #sensor.s_info('on')
    

 else:
    print("The drone is disarmed.")
    #sensor.s_info('off')

 time.sleep(2)

code 2:

 import time
 from pymavlink import mavutil
 import sensor # logging file

 # create a MAVLink connection
 master = mavutil.mavlink_connection('/dev/ttyACM0')

 while True:

 # check if the drone is armed
  armed = False



     # get the current system status
  msg = master.recv_match(type='HEARTBEAT', blocking=True)

     # check if the drone is armed

  armed = (msg.base_mode & mavutil.mavlink.MAV_MODE_FLAG_SAFETY_ARMED)
  if armed:
     print('Drone is armed')
     sensor.s_info('on')
 
  else:
     print('Drone is disarmed')
     sensor.s_info('off')


from How to check if the drone is armed or not using MAVLink/MAVProxy?

No comments:

Post a Comment