Wednesday, July 2, 2025

 The following sample is an addition to the Hungarian algorithm described in the previous article to allow a drone to calculate its flight path from the current to the next position using the shortest possible route. It uses the dubins package from PyPI — a lightweight and widely used implementation for shortest-path planning under curvature constraints. 

#! /usr/bin/python 

# pip install dubins matplotlib numpy 

import dubins 
import numpy as np 
import matplotlib.pyplot as plt 
 
def compute_dubins_path( 
    start_pos: tuple[float, float, float], 
    end_pos: tuple[float, float, float], 
    turning_radius: float = 10.0, 
    step_size: float = 1.0 
): 
    """ 
    Compute the shortest Dubins path between two configurations. 
 
    Parameters: 
        start_pos: (x, y, heading in radians) 
        end_pos: (x, y, heading in radians) 
        turning_radius: Minimum turning radius of the drone 
        step_size: Distance between sampled points along the path 
 
    Returns: 
        List of (x, y, heading) tuples along the path 
    """ 
    path = dubins.shortest_path(start_pos, end_pos, turning_radius) 
    configurations, _ = path.sample_many(step_size) 
    return configurations 
 
# Example usage 
if __name__ == "__main__": 
    # Initial and final positions: (x, y, heading in radians) 
    q0 = (0.0, 0.0, np.deg2rad(0))       # Start at origin, facing east 
    q1 = (50.0, 30.0, np.deg2rad(90))    # End at (50,30), facing north 
 
    path_points = compute_dubins_path(q0, q1, turning_radius=15.0, step_size=0.5) 
 
    # Plotting the path 
    x_vals, y_vals = zip(*[(x, y) for x, y, _ in path_points]) 
    plt.figure(figsize=(8, 6)) 
    plt.plot(x_vals, y_vals, label="Dubins Path", linewidth=2) 
    plt.scatter(*q0[:2], color='green', label="Start") 
    plt.scatter(*q1[:2], color='red', label="End") 
    plt.axis("equal") 
    plt.title("Dubins Path for Drone Flight") 
    plt.xlabel("X") 
    plt.ylabel("Y") 
    plt.legend() 
    plt.grid(True) 
    plt.show() 


#Codingexercise: https://1drv.ms/w/c/d609fb70e39b65c8/EYzGgu5Fc4dEoCUHWQYxMbUBSfvC36iKh8ESBaLtozvdqA?e=uXE4Jm

Tuesday, July 1, 2025

 As an example of drone formation transformation discussed in the previous article, the following code demonstrates the application of Hungarian Algorithm to determine the position allocation in formation transformation.  

#! /usr/bin/python 

# pip install hungarian-algorithm 
 

from hungarian_algorithm import algorithm 
import numpy as np 
 
# Source: drones in a 3×3 grid on Z=0 plane 
source_positions = [ 
    (x, y, 0) 
    for y in range(3) 
    for x in range(3) 
] 
 
# Target: drones in a single horizontal line (linear flight path), spaced 10 units apart 
target_positions = [ 
    (i * 10, 0, 0) for i in range(9) 
] 
 
# Compute cost matrix (Euclidean distance) 
cost_matrix = [ 
    [ 
        np.linalg.norm(np.array(src) - np.array(dst)) 
        for dst in target_positions 
    ] 
    for src in source_positions 
] 
 
# Run Hungarian Algorithm to get minimum-cost assignment 
assignment = algorithm.find_matching(cost_matrix, matching_type='min') 
 
# Report matched pairs 
for src_idx, dst_idx in enumerate(assignment): 
    print(f"Drone {src_idx} → Target Position {dst_idx}: {target_positions[dst_idx]}") 


The above does not take velocity and heading into consideration but that can be adjusted as per trajectory.


#Codingexercise: https://1drv.ms/w/c/d609fb70e39b65c8/EYzGgu5Fc4dEoCUHWQYxMbUBSfvC36iKh8ESBaLtozvdqA?e=VfWGog