Skip to content
TheChatArchiveThe Chat Archive
gpt-4-0314· openaiHugging Face Suzhen/CodeChat

from pymavlink import mavutil import time Create a connection to the telemetry for…

  1. User

    from pymavlink import mavutil import time

    Create a connection to the telemetry for system ID 1

    the_connection = mavutil.mavlink_connection('udpin:localhost:14551', source_system=1, baud=57600)

    Initialize the previous_mode variable to None

    previous_mode = None

    while True: msg = the_connection.recv_match(type='HEARTBEAT', blocking=False) if msg: sysid = msg.get_srcSystem() mode = mavutil.mode_string_v10(msg) if mode != previous_mode: # check if the mode has changed previous_mode = mode # update the previous_mode variable print(f"System ID: {sysid}, Mode: {mode}") print(mode)

    # Close the connection
    the_connection.close()
    

    from the above code, make the below code such that the bekow code runs only when ṭhe mode is det to GUIDED

    Initialize the previous_mode variable to None

    time.sleep(14) cs.print("**",INFO,": STARTED to Followwing the Waypoints") time.sleep(1) the_connection.mav.send(mavutil.mavlink.MAVLink_set_position_target_local_ned_message(10, the_connection.target_system, the_connection.target_component, mavutil.mavlink.MAV_FRAME_LOCAL_NED, int(0b110111111000), 0, 5, -10, 0, 0, 0, 0, 0, 0,0, 0))

    the_connection.mav.command_long_send(the_connection.target_system, the_connection.target_component, mavutil.mavlink.MAV_CMD_DO_CHANGE_SPEED, 0, 0, 1, 0, 0, 0, 0, 0) cs.print("**",ACTION,": Followwing the Waypoints")

    if abort(): exit()

    time.sleep(1)

    time.sleep(1)

    Set mode to Return to Launch

    cs.print("**",INFO," : Set mode to Return to Launch") the_connection.mav.set_mode_send( the_connection.target_system, # Target system ID mavutil.mavlink.MAV_MODE_FLAG_CUSTOM_MODE_ENABLED, # Mode flag 6 # RTL )

    time.sleep(1) cs.print("**",INFO," : disarming the drone")

    disarming the drone

    the_connection.mav.command_long_send( the_connection.target_system, # Target system ID the_connection.target_component, # Target component ID mavutil.mavlink.MAV_CMD_COMPONENT_ARM_DISARM, # Command ID 0,
    0, # Arm (0) or disarm (1) 0,
    0, 0, 0, 0, 0 )

  2. Assistant

    You can modify the code as follows to only run the given code when the mode is set to GUIDED:

    from pymavlink import mavutil
    import time
    
    # Create a connection to the telemetry for system ID 1
    the_connection = mavutil.mavlink_connection('udpin:localhost:14551', source_system=1, baud=57600)
    
    # Initialize the previous_mode variable to None
    previous_mode = None
    
    while True:
        msg = the_connection.recv_match(type='HEARTBEAT', blocking=False)
        if msg:
            sysid = msg.get_srcSystem()
            mode = mavutil.mode_string_v10(msg)
            if mode != previous_mode: # check if the mode has changed
                previous_mode = mode # update the previous_mode variable
                print(f"System ID: {sysid}, Mode: {mode}")
                print(mode)
    
            if mode == "GUIDED":  # Run the guided mode code
                time.sleep(14)
                cs.print("**",INFO,": STARTED to Followwing the Waypoints")
                time.sleep(1)
                the_connection.mav.send(mavutil.mavlink.MAVLink_set_position_target_local_ned_message(10, the_connection.target_system,
                                    the_connection.target_component, mavutil.mavlink.MAV_FRAME_LOCAL_NED, int(0b110111111000), 0, 5, -10, 0, 0, 0, 0, 0, 0,0, 0))
                
                the_connection.mav.command_long_send(the_connection.target_system, the_connection.target_component,
                                                mavutil.mavlink.MAV_CMD_DO_CHANGE_SPEED, 0, 0, 1, 0, 0, 0, 0, 0)
                cs.print("**",ACTION,": Followwing the Waypoints")
    
                if abort():
                    exit()
    
                time.sleep(1)
    
                time.sleep(1)
                # Set mode to Return to Launch
                cs.print("**",INFO," : Set mode to Return to Launch")
                the_connection.mav.set_mode_send(
                    the_connection.target_system,   # Target system ID
                    mavutil.mavlink.MAV_MODE_FLAG_CUSTOM_MODE_ENABLED,   # Mode flag
                    6  # RTL
                )
    
                time.sleep(1)
                cs.print("**",INFO," : disarming the drone")
                # disarming the drone
                the_connection.mav.command_long_send(
                    the_connection.target_system,   # Target system ID
                    the_connection.target_component,   # Target component ID
                    mavutil.mavlink.MAV_CMD_COMPONENT_ARM_DISARM,   # Command ID
                    0,   
                    0,   # Arm (0) or disarm (1)
                    0,   
                    0,
                    0,
                    0,
                    0,
                    0
                )
            
        # Close the connection
        the_connection.close()
    

    This code changes the structure of your loop to have an if mode == "GUIDED" statement, running the code block only if the drone's mode is set to GUIDED.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.