write me a gui in python which can show the live location of both the drones on map and…
- User
write me a gui in python which can show the live location of both the drones on map and information of both the drones which comes in mavproxy
from pymavlink import mavutil import math import time
class Drone:
def __init__(self, system_id, connection): self.system_id = system_id self.connection = connection def set_mode(self, mode): self.connection.mav.set_mode_send( self.system_id, mavutil.mavlink.MAV_MODE_FLAG_CUSTOM_MODE_ENABLED, mode ) def arm(self, arm=True): self.connection.mav.command_long_send(self.system_id, self.connection.target_component, mavutil.mavlink.MAV_CMD_COMPONENT_ARM_DISARM, 0, int(arm), 0, 0, 0, 0, 0, 0) def takeoff(self, altitude): self.connection.mav.command_long_send(self.system_id, self.connection.target_component, mavutil.mavlink.MAV_CMD_NAV_TAKEOFF, 0, 0, 0, 0, 0, 0, 0, altitude) def send_waypoint(self, wp, next_wp, speed=3): vx, vy, vz = calculate_velocity_components(wp, next_wp, speed) self.connection.mav.send(mavutil.mavlink.MAVLink_set_position_target_global_int_message( 10, self.system_id, self.connection.target_component, mavutil.mavlink.MAV_FRAME_GLOBAL_RELATIVE_ALT, int(0b110111111000), int(wp[0] * 10 ** 7), int(wp[1] * 10 ** 7), wp[2], vx, vy, vz, 0, 0, 0, 0 # Set vx, vy, and vz from calculated components )) def get_position(self): self.connection.mav.request_data_stream_send( self.system_id, self.connection.target_component, mavutil.mavlink.MAV_DATA_STREAM_POSITION, 1, 1) while True: msg = self.connection.recv_match(type='GLOBAL_POSITION_INT', blocking=True) if msg.get_srcSystem() == self.system_id: return (msg.lat / 10 ** 7, msg.lon / 10 ** 7, msg.alt / 10 ** 3)class PIDController:
def __init__(self, kp, ki, kd, limit): self.kp = kp self.ki = ki self.kd = kd self.limit = limit self.prev_error = 0 self.integral = 0 def update(self, error, dt): derivative = (error - self.prev_error) / dt self.integral += error * dt self.integral = max(min(self.integral, self.limit), -self.limit) # Clamp the integral term output = self.kp * error + self.ki * self.integral + self.kd * derivative self.prev_error = error return outputdistance = 5 # Distance in meters angle = 60 # Angle in degrees kp = 0.1 ki = 0.01 kd = 0.05 pid_limit = 0.0001 pid_lat = PIDController(kp, ki, kd, pid_limit) pid_lon = PIDController(kp, ki, kd, pid_limit)
def calculate_follower_coordinates(wp, distance, angle): earth_radius = 6371000.0 # in meters latitude_change = (180 * distance * math.cos(math.radians(angle))) / (math.pi * earth_radius) longitude_change = (180 * distance * math.sin(math.radians(angle))) / ( math.pi * earth_radius * math.cos(math.radians(wp[0]))) new_latitude = wp[0] + latitude_change new_longitude = wp[1] + longitude_change return (new_latitude, new_longitude, wp[2])
- Assistant
- User
in this code, nothing is happeniung
- Assistant
- User
in the below code, nothing is showinmg import gmplot import tkinter as tk from tkinter import ttk from io import BytesIO import base64 import webbrowser from PIL import Image, ImageTk import threading
Your Drone class definition here
class DroneApp(tk.Tk): def init(self): super().init()
self.title("Drone Information") self.geometry("800x600") self.notebook = ttk.Notebook(self) self.notebook.pack(fill='both', expand=True) self.map_frame = ttk.Frame(self.notebook) self.notebook.add(self.map_frame, text="Map") self.map_label = tk.Label(self.map_frame) self.map_label.pack(fill='both', expand=True) self.create_drone_info_tab("Master Drone") self.create_drone_info_tab("Follower Drone") # Initial positions self.master_drone_position = (28.5861474, 77.3421320) self.follower_drone_position = (28.5859040, 77.3420736) self.update_map() self.update_positions() def create_drone_info_tab(self, drone_name): tab = ttk.Frame(self.notebook) self.notebook.add(tab, text=drone_name) label = tk.Label(tab, text=drone_name, font=("Arial", 20, "bold")) label.pack(anchor="w", padx=20, pady=20) def update_map(self): gmap = gmplot.GoogleMapPlotter((self.master_drone_position[0] + self.follower_drone_position[0]) / 2, (self.master_drone_position[1] + self.follower_drone_position[1]) / 2, 15) gmap.marker(self.master_drone_position[0], self.master_drone_position[1], color='red', title="Master Drone") gmap.marker(self.follower_drone_position[0], self.follower_drone_position[1], color='blue', title="Follower Drone") map_buffer = BytesIO() gmap.draw(map_buffer) img = Image.open(map_buffer) img = ImageTk.PhotoImage(img) self.map_label.config(image=img) self.map_label.image = img def update_positions(self): # You will need to update the master_drone_position and follower_drone_position # with their actual positions using your Drone class get_position method self.update_map() self.after(1000, self.update_positions) - Assistant
I apologize for the confusion. There is a mistake in the code, which caused the
__init__method of theDroneAppclass not to execute. Please fix the indentation and changeinitto__init__method as shown below:class DroneApp(tk.Tk): def __init__(self): # fix the method name here super().__init__() #...remaining codeAfter this modification, the code should work as expected when you run it. The GUI should display a window with a map showing the initial positions of the Master and Follower drones. Make sure to replace the dummy positions with the actual drone positions obtained from your
Droneclassget_positionmethod. - User
- Assistant
Yes, the code provided is aiming to achieve the following:
- Connect to the master and follower drones using MAVLink through the same telemetry.
- Set the mode to Guided for both drones, arm them, and make them take off to the altitude of 10 meters.
- Follow a pre-defined waypoint list for the master drone.
- Make the follower drone follow the master drone at a distance of 5 meters and with an angle of 60 degrees.
- Apply PID control for smooth following behavior of the follower drone.
- Allow the user to enter "abort" on the command line within a window of 7 seconds to return the drones to Launch and disarm the motors.
- Ensure that the drones only follow waypoints when their mode is in Guided mode.
However, it's important to note that the code provided does not address all your requirements:
- Handling mode changes from the RC: To detect when the operator changes the mode from the RC, you need to constantly monitor the mode and react to the changes accordingly. This is not explicitly implemented in the code.
To improve the code to better fit your needs, consider splitting it into functions that handle different aspects of the drone behavior. This will make it easier to organize and maintain.
- User
please do these and give me the full modifies code
- Handling mode changes from the RC: To detect when the operator changes the mode from the RC, you need to constantly monitor the mode and react to the changes accordingly. This is not explicitly implemented in the code.
To improve the code to better fit your needs, consider splitting it into functions that handle different aspects of the drone behavior. This will make it easier to organize and maintain.
- Assistant