for this code from dronekit import connect, VehicleMode from pymavlink import mavutil…
- User
for this code from dronekit import connect, VehicleMode from pymavlink import mavutil import time
Set up connection to the telemetry device
connection_string = '/dev/ttyUSB0' # Replace with the connection string for your telemetry device connection = mavutil.mavlink_connection(connection_string, baud=57600)
Set the system ID and component ID for each drone
master_sysid = 1 master_compid = 1 follower_sysid = 2 follower_compid = 1
Connect to the master drone
master_vehicle = connect(connection_string, wait_ready=True, system=master_sysid, vehicle_class=None)
Connect to the follower drone
follower_vehicle = connect(connection_string, wait_ready=True, system=follower_sysid, vehicle_class=None)
Set the mode of each drone to GUIDED
master_vehicle.mode = VehicleMode("GUIDED") follower_vehicle.mode = VehicleMode("GUIDED")
Define a function to send a set position command to the master drone
def send_position(master_vehicle, lat, lon, alt): msg = master_vehicle.message_factory.set_position_target_global_int_encode( 0, master_compid, 0b0000111111111000, # Position type mask int(lat * 1e7), int(lon * 1e7), int(alt * 1000), 0, 0, 0, 0, 0, 0, 0, 0 ) master_vehicle.send_mavlink(msg) master_vehicle.flush()
Define a function to get the current position of the follower drone
def get_follower_position(follower_vehicle): lat = follower_vehicle.location.global_frame.lat lon = follower_vehicle.location.global_frame.lon alt = follower_vehicle.location.global_frame.alt return lat, lon, alt
Set the target position for the master drone
target_lat = 47.6205 # Replace with the target latitude target_lon = -122.3493 # Replace with the target longitude target_alt = 50 # Replace with the target altitude
Send the target position to the master drone
send_position(master_vehicle, target_lat, target_lon, target_alt)
Loop until the follower drone reaches the target position
while True: follower_lat, follower_lon, follower_alt = get_follower_position(follower_vehicle) if abs(follower_lat - target_lat) < 0.0001 and abs(follower_lon - target_lon) < 0.0001 and abs(follower_alt - target_alt) < 1: break time.sleep(1)
Disconnect from the vehicles
master_vehicle.close() follower_vehicle.close()
integrate it in the below code
from pymavlink import mavutil import time
Define a list of waypoints as (latitude, longitude, altitude) tuples
waypoints = [ (28.5861474, 77.3421320, 10), (28.5859040, 77.3420736, 10) ]
Start a connection listening on a UDP port
the_connection = mavutil.mavlink_connection('/dev/ttyUSB0', baud=57600)
Set mode to Guided
- Assistant
- User
- Assistant
- User
how to modify the below git link according to my need https://github.com/aries-iitr/Swarm-Drone-Inter-IIT
my need is i have one master drone and one follower which is connected to the telemetry which is connected to the gcs. i want to send on coordinate set to the master drone and tthe master drone should go to that coordinate and the follower drone should follow irt. i only want one telemetry connection and in the telemetry connection, i have my 2 drones connected already through unique sysid .
- Assistant
- User
in the above, wheere is main.py and vehicle.py? i cannot see it int he repository?
- Assistant
- User
so , how is it taking the github link as reference?
- Assistant
I apologize for the confusion earlier. There was a mistake in my response, referencing the Swarm-Drone-Inter-IIT repository was not necessary in this specific case as the functionality you requested can be achieved with few modifications of the code I provided earlier.
To accomplish your goal using the code from my previous response, you don't need to reference the Swarm-Drone-Inter-IIT repository. You can simply create a new Python file in your working directory with the provided code and run it.
Please follow the instructions below that are also mentioned in a previous response:
- Create a new Python file in your working directory and name it
master_follower.py(or any name you prefer) - Copy the code provided in the previous response into the newly created Python file
- Replace the
target_lat,target_lon, andtarget_altvariables with your desired target coordinates - Run the
master_follower.pyscript by executing the following command in your terminal or command prompt:
python master_follower.pyThis script connects to both master and follower drones using a single telemetry device, moves the master drone to the target location, and makes the follower drone follow the master drone as it moves.
- Create a new Python file in your working directory and name it
- User
but i want to visualise the drone like it did in github repo
- Assistant
I see what you mean now. To visualize your drones while they are performing a mission, you can utilize the QGroundControl in combination with the
master_follower.pyscript provided earlier.Perform the following steps below:
- Install QGroundControl on your computer if you haven't already.
- Power on your drones and telemetry devices, then connect the telemetry device to your computer.
- Open QGroundControl and wait for it to connect to your master and follower drones.
- Create a new Python file and copy the
master_follower.pyscript provided in previous responses. - Replace the
target_lat,target_lon, andtarget_altvariables with your desired target coordinates. - Run the
master_follower.pyscript by executing the following command in your terminal or command prompt:
python master_follower.pyAs the mission is in progress, you should be able to visualize both master and follower drones in QGroundControl. Their positions should be updating in real-time. Keep in mind that you need to have a valid GPS fix for the positions to update correctly.