in the below code, i want to move my master drone to waypoints in different directions,…
- User
in the below code, i want to move my master drone to waypoints in different directions, what would be the follower drones position? from pymavlink import mavutil import math import time
the_connection = mavutil.mavlink_connection('/dev/ttyUSB0', baud=57600) the_connection.wait_heartbeat()
msg = the_connection.recv_match(type='GLOBAL_POSITION_INT', blocking=True) master_waypoint = (msg.lat / 10 ** 7, msg.lon / 10 ** 7, 10)
waypoints = [ master_waypoint, (28.5861327, 77.3420592, 10), (28.5860912, 77.3420042, 10), # Repeat the first waypoint to make the drone return to its starting point ] distance = 5 # Distance in meters angle = 240 # Angle in degrees kp = 0.1 ki = 0.01 kd = 0.05 pid_limit = 0.0001
class Drone:
codedef __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): # Print wp and next_wp print("Current waypoint: {} | Next waypoint: {}".format(wp, next_wp)) vx, vy, vz = calculate_velocity_components(wp, next_wp, speed) # Print velocity components print("Velocity components: vx={}, vy={}, vz={}".format(vx, vy, vz)) 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, # * speed, # Multiply by speed, vy, # * speed, # Multiply by speed, vz, # * speed, # Multiply by speed, 0, 0, 0, 0, 0) ) 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, 10) - Assistant
The follower drone's position will be at a specified
distanceandanglerelative to the master drone's position. To calculate the follower drone's position, the functioncalculate_follower_coordinates()is defined in the code given.Here's how the function is called in the code:
pythonfollower_position = master_drone.get_position() follower_wp = calculate_follower_coordinates(follower_position, distance, angle)The
calculate_follower_coordinates()function takes three arguments: the master drone's position (follower_position), the distance between the master drone and the follower drone (distance), and the angle relative to the master drone's position at which the follower drone should fly (angle). The function then calculates the follower drone's latitude and longitude using these values and returns a tuple containing the latitude, longitude, and altitude of the follower drone's position.