is the below code correct? from pymavlink import mavutil import math import time import…
- User
is the below code correct? from pymavlink import mavutil import math import time import logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
class Drone: def init(self, sysid, master_address): self.sysid = sysid self.master_address = master_address self.connection = mavutil.mavlink_connection(self.master_address, baud=57600) self.master_position = [0, 0, 0] # Starting position for the master drone
codedef receive_messages(self): try: msg = self.connection.recv_match(type='GLOBAL_POSITION_INT', blocking=True) if msg.get_srcSystem() == self.sysid: self.master_position = [msg.lat / 10 ** 7, msg.lon / 10 ** 7, msg.alt / 10 ** 3] except Exception as e: logging.error("Error receiving messages: {}".format(e)) def send_position_target(self, x, y, z, vx, vy, vz): try: self.connection.mav.set_position_target_global_int( time.time(), self.sysid, mavutil.mavlink.MAV_COMP_ID_AUTOPILOT1, mavutil.mavlink.MAV_FRAME_GLOBAL_RELATIVE_ALT, 0b110111111000, int(x * 10 ** 7), int(y * 10 ** 7), z, vx, vy, vz, 0, 0, 0, 0, 0 ) except Exception as e: logging.error("Error sending position target: {}".format(e)) def close(self): self.connection.close()def distance_between_points(p1, p2): return math.sqrt((p1[0] - p2[0]) ** 2 + (p1[1] - p2[1]) ** 2 + (p1[2] - p2[2]) ** 2)
def abort(): print("Type 'abort' to return to Launch and disarm motors.") start_time = time.monotonic() while time.monotonic() - start_time < 7: user_input = input("Time left: {} seconds \n".format(int(7 - (time.monotonic() - start_time)))) if user_input.lower() == "abort": print("Returning to Launch and disarming motors…") for drone in [master_drone, follower_drone]: drone.set_mode(6) # RTL mode drone.arm(False) # Disarm motors return True print("7 seconds have passed. Proceeding with waypoint task...") return False
- Assistant
As an AI language model, I cannot test or run the code, but I can say that it looks logically correct in terms of syntax and structure. The code imports necessary libraries, defines a class, and uses it to control the drones and send position targets to their controllers. It also defines functions for calculating distances between waypoints and for aborting the mission if needed. However, to be sure that the code is completely correct, it should be tested in a suitable environment.
- User
- Assistant
To set the mode to RTL (Return To Launch) when the waypoint task is done, the following modifications can be made:
- Define a function
mode_rtl()that sends the RTL mode command to the drones using thecommand_long_send()function:
codedef mode_rtl(drone): drone.connection.mav.command_long_send( drone.sysid, # target_system mavutil.mavlink.MAV_COMP_ID_SYSTEM_CONTROL, # target_component mavutil.mavlink.MAV_CMD_NAV_RETURN_TO_LAUNCH, # command 0, # confirmation 0, 0, 0, 0, 0, 0, 0 # unused parameters for this command )- Call the
mode_rtl()function after the waypoint task is done:
code - Define a function