in the below code, get the master drone and follower drone local location inetgrated and…
- User
in the below code, get the master drone and follower drone local location inetgrated and print them using pymavlink and give me correct code. basically i want the proximity checker, distance between master and follower drone should be always minimum 5 meters between then , if nbot, prints alert, priximity breach
main loop for the code
while True: # checking for heartbeat
msg = the_connection.recv_match(type='HEARTBEAT', blocking=False) if msg: print("***ACTION: Heartbeat connection established***") sysid = msg.get_srcSystem() # checking for mode of both drones connt. and print the current mode if sysid in [2, 3]: mode = mavutil.mode_string_v10(msg) if mode != previous_mode[sysid]: # check if the mode has changed previous_mode[sysid] = mode # update the previous_mode variable print(f"***ACTION: System ID: {sysid}, Mode: {mode}***") # save the mode for sysid 2 and 3 in separate variables if sysid == 2: mode_sysid_2 = mode elif sysid == 3: mode_sysid_3 = mode # Run the following code only when mode_sysid_3 and mode_sysid_2 is set to "GUIDED" time_start = time.time() if mode_sysid_3 == "GUIDED": while mode_sysid_2 == "GUIDED": if abort(): print("***ACTION: aborting from terminal***") exit() # runs every second if time.time() - time_start >= 1: # if mode is not set to guided, set the mode to rtl and disarm the drone for index, master_wp in enumerate(waypoints[:-1]): if mode_sysid_2 != "GUIDED": for drone in [master_drone, follower_drone]: drone.set_mode(6) drone.arm(False) # get the next waypoint of the mission next_wp = waypoints[index + 1] # send the master drone to waypoint master_drone.send_waypoint(master_wp, next_wp, speed=1) print("***ACTION: master done is going to waypoint: {}***".format(master_wp)) # get the current position of follower position follower_position = master_drone.get_position() # Print follower position print("***ACTION: follower position is : {}***".format(follower_position)) # if follower position is not found, set the mode to rtl for both drones and disarms the drones if follower_position is None: for drone in [master_drone, follower_drone]: print("INFO: setting mod to rtl because follower position is not found") drone.set_mode(6) drone.arm(False) break # calculate the follower drone coordinates with the distance and angle follower_wp = calculate_follower_coordinates(follower_position, distance, angle) dt = time.time() - time_start # get the pid latitude anad longitude for followe drone to get the accurate positioning pid_lat_output = pid_lat.update(follower_wp[0] - follower_position[0], dt) pid_lon_output = pid_lon.update(follower_wp[1] - follower_position[1], dt) # Print PID output adjustments print("***ACTION: PID adjustments: lat={}, lon={}***".format(pid_lat_output, pid_lon_output)) # get the adjusted coordinates of follower drones adjusted_follower_wp = ( follower_wp[0] + pid_lat_output, follower_wp[1] + pid_lon_output, follower_wp[2]) # Print adjusted follower waypoint print("***ACTION: Adjusted follower waypoint: {}***".format(adjusted_follower_wp)) # send the follower drone to adjusted coordinates follower_drone.send_waypoint(adjusted_follower_wp, next_wp, speed=1) # check for abort if abort(): print("***ACTION: aborting from terminal***") exit() # check for mode of master drone, if not on GUIDED mode, then set the mode to rtl for both drones if mode_sysid_2 != "GUIDED": for drone in [master_drone, follower_drone]: print("***ACTION: setting mode to rtl because mode is not set to rtl***") drone.set_mode(6) drone.arm(False) time.sleep(30) # set the mode to rtl and disarms the drone for drone in [master_drone, follower_drone]: print("***ACTION: setting mode to rtl because waypoint mision finished") drone.set_mode(6) drone.arm(False) # set mode to rtl # master_drone.set_mode(6) # follower_drone.set_mode(6) break - Assistant
Here's the corrected code that checks the proximity between the master drone and the follower drone. If the distance between them is less than 5 meters, a proximity breach alert is printed.
- User
- Assistant
Overall, the code seems fine and the logic appears to be correct. However, there may be a few minor formatting and indentation issues. I've corrected these below:
# ... # Initialize the previous_mode variable to None previous_mode = {2: None, 3: None} # initialize the previous_mode dictionary ### # Note: The while True loop is correctly aligned with the main code as needed. ### while True: # checking for heartbeat msg = the_connection.recv_match(type='HEARTBEAT', blocking=False) if msg: print("***ACTION: Heartbeat connection established***") sysid = msg.get_srcSystem() # checking for the mode of both drones connt. and print the current mode if sysid in [2, 3]: mode = mavutil.mode_string_v10(msg) if mode != previous_mode[sysid]: # check if the mode has changed previous_mode[sysid] = mode # update the previous_mode variable print(f"***ACTION: System ID: {sysid}, Mode: {mode}***") # save the mode for sysid 2 and 3 in separate variables if sysid == 2: mode_sysid_2 = mode elif sysid == 3: mode_sysid_3 = mode # Run the following code only when mode_sysid_3 and mode_sysid_2 are set to “GUIDED” time_start = time.time() if mode_sysid_3 == "GUIDED" and mode_sysid_2 == "GUIDED": # Continuation of the code... ### # Note: The connection closing code should not be inside the while True loop. Move it outside the loop as needed. ### print("***ACTION: connection is closing***") # connection close for telemetry connection the_connection.close() - User
but now it is showing coded inaccesabele for connection close part, why?
- Assistant
I apologize for the confusion. It seems that I didn't understand the structure of your code correctly. You should keep the
the_connection.close()statement inside thewhile Trueloop, but it should be aligned with theif msg:statement. Here's the corrected code:# ... # Initialize the previous_mode variable to None previous_mode = {2: None, 3: None} # initialize the previous_mode dictionary while True: # checking for heartbeat msg = the_connection.recv_match(type='HEARTBEAT', blocking=False) if msg: print("***ACTION: Heartbeat connection established***") sysid = msg.get_srcSystem() # checking for mode of both drones connt. and print the current mode if sysid in [2, 3]: mode = mavutil.mode_string_v10(msg) # ... # Continuation of the code... print("***ACTION: connection is closing***") # connection close for telemetry connection the_connection.close() else: time.sleep(0.1) # Add a small sleep to prevent excessive CPU usage when no message is receivedWith this code structure, the
the_connection.close()part is now accessible and should execute correctly. - User
- Assistant
To add the GPS check in your while loop, I integrated the GPS check code into your existing while loop. Notice how the GPS check is now included within the while loop:
gps_check_time_start = time.time() # Main while loop try: while True: # GPS Check every second if time.time() - gps_check_time_start >= 1: gps_check_time_start = time.time() # Request GPS data stream the_connection.mav.request_data_stream_send( the_connection.target_system, the_connection.target_component, mavutil.mavlink.MAV_DATA_STREAM_GPS_RAW, 1, # Request rate (Hz) 1 # Start/stop (1=start, 0=stop) ) # Wait for GPS data to be received msg = the_connection.recv_match(type='GPS_RAW_INT', blocking=True, timeout=1) if msg is not None: gps_found = msg.fix_type >= 2 and msg.satellites_visible > 10 # If GPS not found, set mode to auto RTL and disarm the drone if not gps_found: print("GPS not found - setting mode to AUTO.RTL and disarming") the_connection.mav.command_long_send( the_connection.target_system, the_connection.target_component, mavutil.mavlink.MAV_CMD_NAV_RETURN_TO_LAUNCH, 0, 0, 0, 0, 0, 0, 0, 0 ) the_connection.arducopter_disarm() # checking for heartbeat # ... # Continuation of the existing code ... finally: print("***ACTION: connection is closing***") # connection close for telemetry connection the_connection.close()
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.