hi there, i want to do swarming for 2 drones, one is master with system id 3 and one is…
- User
hi there, i want to do swarming for 2 drones, one is master with system id 3 and one is follower with system id 2, both of them are connected with the sametelemertry. i have implemented multiple waypoints for one drone, but now i want to do for 2 drone with swarming. here is the 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
the_connection.mav.set_mode_send( the_connection.target_system, # Target system ID mavutil.mavlink.MAV_MODE_FLAG_CUSTOM_MODE_ENABLED, # Mode flag 4 # Guided mode )
Arm the drone
the_connection.mav.command_long_send(the_connection.target_system, the_connection.target_component, mavutil.mavlink.MAV_CMD_COMPONENT_ARM_DISARM, 0, 1, 0, 0, 0, 0, 0, 0)
Take off the drone to 10 meters altitude
the_connection.mav.command_long_send(the_connection.target_system, the_connection.target_component, mavutil.mavlink.MAV_CMD_NAV_TAKEOFF, 0, 0, 0, 0, 0, 0, 0, 10)
def abort(): """ Waits for 7 seconds for the user to input 'abort'. If 'abort' is entered, sets the mode to RTL and disarms the motors. Otherwise, continues with the waypoint. """ 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...") # sets the mode to return to launch(RTL) the_connection.mav.set_mode_send( the_connection.target_system, # Target system ID mavutil.mavlink.MAV_MODE_FLAG_CUSTOM_MODE_ENABLED, # Mode flag 6 # RTL ) # disarms the drone the_connection.mav.command_long_send( the_connection.target_system, # Target system ID the_connection.target_component, # Target component ID mavutil.mavlink.MAV_CMD_COMPONENT_ARM_DISARM, # Command ID 0, # Confirmation 0, # Arm (0) or disarm (1) 0, # Unused parameters 0, 0, 0, 0, 0 ) return True return Falsebelow commented code does not work in drone !
mode = the_connection.mode_mapping()[the_connection.flightmode]
mode = 4 print(f"{mode}")
while mode == 4: time.sleep(10) # Loop through each waypoint and send MAVLink commands for i, wp in enumerate(waypoints): print("Going to waypoint", i+1) if abort(): exit() else: the_connection.mav.send(mavutil.mavlink.MAVLink_set_position_target_global_int_message( 10, # Time boot_ms the_connection.target_system, the_connection.target_component, mavutil.mavlink.MAV_FRAME_GLOBAL_RELATIVE_ALT, int(0b110111111000), # Use only lat, long, alt fields (position masking accuracy parameter) int(wp[0] * 10 ** 7), # Latitude in degrees * 10^7 int(wp[1] * 10 ** 7), # Longitude in degrees * 10^7 wp[2], # Altitude in meters 0, # No velocity control 0, # No acceleration control 0, 0, 0, 0, 0, 0 # No yaw, yaw_rate, or type_mask control ))
- Assistant
- User
in the above code, i want to give coordinated to master drone and follower shoulf follow at the distance of 5 meters and at an angle of 60 degerees.please give me the modifies correct full code
- Assistant
- User
make the above code more complex and good while keeping the concept same
- Assistant
- User
in the above code, add abort function def abort(): """ Waits for 7 seconds for the user to input 'abort'. If 'abort' is entered, sets the mode to RTL and disarms the motors. Otherwise, continues with the waypoint. """ 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...") # sets the mode to return to launch(RTL) the_connection.mav.set_mode_send( the_connection.target_system, # Target system ID mavutil.mavlink.MAV_MODE_FLAG_CUSTOM_MODE_ENABLED, # Mode flag 6 # RTL ) # disarms the drone the_connection.mav.command_long_send( the_connection.target_system, # Target system ID the_connection.target_component, # Target component ID mavutil.mavlink.MAV_CMD_COMPONENT_ARM_DISARM, # Command ID 0, # Confirmation 0, # Arm (0) or disarm (1) 0, # Unused parameters 0, 0, 0, 0, 0 ) return True return False - Assistant
I have incorporated the abort function into the updated code. Notice that the function now uses the
drone.set_mode()anddrone.arm()methods for changing flight modes and disarming the motors.from pymavlink import mavutil import math import time def abort(): """ Waits for 7 seconds for the user to input 'abort'. If 'abort' is entered, sets the mode to RTL and disarms the motors. Otherwise, continues with the waypoint. """ 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 return False # ... (the rest of the code remains the same, create Drone class, calculate_follower_coordinates, waypoints, etc.) master_drone = Drone(3, the_connection, waypoints) follower_drone = Drone(2, the_connection, follower_waypoints) # Set mode to Guided and arm both drones for drone in [master_drone, follower_drone]: drone.set_mode(4) drone.arm() drone.takeoff(10) while mode == 4: time.sleep(10) for i, (master_wp, follower_wp) in enumerate(zip(waypoints, follower_waypoints)): print("Going to waypoint", i + 1) if abort(): exit() else: master_drone.send_waypoint(master_wp) follower_drone.send_waypoint(follower_wp) if abort(): exit() time.sleep(5) # Set mode to RTL and disarm both drones for drone in [master_drone, follower_drone]: drone.set_mode(6) drone.arm(False) the_connection.close() - User
- Assistant
To make the follower drone follow the master drone every second and calculate the follower coordinates every second, you can update the main loop of your code as follows:
- User
pleasae give me the full code, i am having confusion .
- Assistant
- User
tell me in detail what is happening in above code. i want to have one master drone with system id 3 and one follower drone with system id 2 which are connected with single telemetry . i want to send the waypoint to master and follower shoulf follow the mastre at every second and at the distance of 5 meteres and at an angle of 60 degrees. if types abort, vboth the drone should rtl.
- Assistant
The code provided earlier does exactly what you require. Here is a detailed explanation of the different parts of the code and how they interact:
-
DroneClass: It's an object-oriented representation of a drone. It includes methods (set_mode,arm,takeoff,send_waypoint, andget_position) to control drones with ease. -
calculate_follower_coordinatesFunction: It calculates follower's new coordinates based on the master drone's position, distance, and angle.
-
- User
so , i will fly right? i have 2 px4 cube+ and rdf 868x telemetry
- Assistant
Yes, the provided code should work with your setup, given that you have two PX4 Cube controllers with system IDs 3 and 2, respectively, and an RFD 868x telemetry. Make sure the telemetry is configured correctly to communicate with both PX4 controllers.
You should test the code in a controlled and safe environment to ensure it performs as expected. Always follow safety precautions and any local regulations when flying drones.
Please note that due to factors such as GPS inaccuracies, environmental conditions, and hardware variations, the follower drone might not maintain the exact 5 meters distance and 60-degree angle from the master at all times. This code provides a starting point for the desired behavior, and you may need to adjust parameters or algorithms to achieve the best performance for your specific setup.
- User
please improve the algoritgm in the above code and give me the full code
- Assistant
To improve the algorithm, I recommend using a Proportional-Derivative (PD) controller to follow the master drone. A PD controller calculates the control values based on the error between the follower's desired position and the current position, as well as the rate of change of the error. This can make the follower drone's response smooth and responsive.
Here's the updated code with