Drone

Sim2Real by Robotech NITK

Sponsered By:

logo

SIM2REAL-Swarm Drone Simulation Hackathon

Sponsered By:


Thank You for Showing Intrest

Winners of Sim2Real 2025:
1. Team ROSLEELA
2. Team Innovators
3. Team Encrypt Nation

Special thanks to our sponsors

View Event Gallery

For future events stay tune to:

Problem Statement

Develop and implement an efficient multi-stage swarm control algorithm using Crazyflie drones in the Crazyswarm simulation framework.

1. Introduction

Welcome to the Swarm Drone Simulation Hackathon! In this competition, you will develop innovative applications to control a swarm of Crazyflie drones using the pycrazyswarm Python API and ROS in simulation mode only. Your solution will be built in five stages:

All development is done in simulation mode. Remember to build your ROS workspace and

source it before running any scripts:
source ros_ws / devel / setup . bash

Stage 1: Basic Flight (Single Drone)

2.1 Objective

Initialize the simulation and perform a basic flight sequence: takeoff, hover, and land with a single Crazyflie.

2.2 Boilerplate Code

#!/ usr/bin/env python
"""
Stage 1: Basic Flight Control for a Single Drone
This script initializes the Crazyswarm simulation and commands one drone to take
off ,
hover , and land .
"""
import time
from crazyswarm import Crazyswarm
def stage1_basic_flight () :
print (" Stage 1: Initializing simulation ...")
swarm = Crazyswarm ()
allcfs = swarm . allcfs
timeHelper = swarm . timeHelper
# Takeoff : Fly to 1.0 meter over 2 seconds
print (" Taking off ...")
allcfs . takeoff ( targetHeight =1.0 , duration =2.0)
timeHelper . sleep (4.0) # Wait for stabilization
# Hover for 5 seconds
print (" Hovering ...")
timeHelper . sleep (5.0)
# Land : Descend to 0.0 meters over 2 seconds
print (" Landing ...")
allcfs . land ( targetHeight =0.0 , duration =2.0)
timeHelper . sleep (3.0)
print (" Stage 1 complete : Basic flight sequence finished .")
if __name__ == " __main__ ":
stage1_basic_flight ()
        

2.3 Detailed Explanation

Environment Setup:
– Build your ROS workspace (typically in ros_ws) and source the setup file:
source ros_ws / devel / setup . bash
Run the script in simulation mode using the –sim flag:
python stage1_basic_flight . py -- sim
  • Code Walkthrough:
  • Observation: In the simulation window, you should see the drone executing the flight sequence.

    Stage 2: Trajectory Tracking (Single Drone)

    3.1 Objective:

    Enhance Stage 1 by having the drone follow a smooth trajectory from its initial takeoff position to a specified goal position using the goTo command.

    3.2 Boilerplate Code

    #!/ usr/bin/env python
    """
    Stage 2: Single Drone Trajectory Tracking
    This script commands a single drone to take off , move to a new position using a
    smooth trajectory ,
    and then land .
    """
    import time
    from crazyswarm import Crazyswarm
    def stage2_trajectory_tracking () :
    print (" Stage 2: Initializing simulation ...")
    swarm = Crazyswarm ()
    allcfs = swarm . allcfs
    timeHelper = swarm . timeHelper
    # Takeoff : Reach 1.0 meter altitude
    print (" Taking off ...")
    allcfs . takeoff ( targetHeight =1.0 , duration =2.0)
    timeHelper . sleep (4.0)
    # Trajectory : Move to a new position (e.g. , [1.0 , 1.0 , 1.0])
    print (" Executing trajectory ( goTo )...")
    goal_position = [1.0 , 1.0 , 1.0]
    allcfs . goTo ( goal = goal_position , yaw =0.0 , duration =3.0)
    timeHelper . sleep (4.0)
    # Hover for 3 seconds at new position
    print (" Hovering at new position ...")
    timeHelper . sleep (3.0)
    # Land
    print (" Landing ...")
    allcfs . land ( targetHeight =0.0 , duration =2.0)
    timeHelper . sleep (3.0)
    print (" Stage 2 complete : Trajectory tracking finished .")
    if __name__ == " __main__ ":
    stage2_trajectory_tracking ()        
    

    3.3 Detailed Explanation

    Stage 3: Coordinated Flight (Multiple Drones)

    4.1 Objective:

    Extend your solution to control multiple Crazyflies simultaneously. Use group commands to synchronize maneuvers such as takeoff, formation flight, and landing.

    4.2 Boilerplate Code

    #!/ usr/bin/env python
    """
    Stage 3: Coordinated Flight for Multiple Drones
    This script commands multiple Crazyflies to take off , fly in a coordinated
    formation , and land simultaneously .
    """
    import time
    from crazyswarm import Crazyswarm
    def stage3_coordinated_flight () :
    print (" Stage 3: Initializing simulation for multiple drones ...")
    swarm = Crazyswarm ()
    allcfs = swarm . allcfs
    timeHelper = swarm . timeHelper
    # Set all drones to the same group (e.g. , group mask = 1)
    for cf in allcfs . crazyflies :
    cf . setGroupMask (1)
    # Synchronized Takeoff
    print (" Coordinated takeoff ...")
    allcfs . takeoff ( targetHeight =1.0 , duration =2.0)
    timeHelper . sleep (4.0)
    # Coordinated maneuver : Form a line formation
    print (" Forming a line formation ...")
    for cf in allcfs . crazyflies :
    offset = [0.5 * cf .id , 0 , 0]
    current_pos = cf . position ()
    goal = [ current_pos [0] + offset [0] , current_pos [1] , current_pos [2]]
    cf . goTo ( goal = goal , yaw =0.0 , duration =3.0 , relative = False , groupMask =1)
    timeHelper . sleep (5.0)
    # Synchronized Landing
    print (" Coordinated landing ...")
    allcfs . land ( targetHeight =0.0 , duration =2.0)
    timeHelper . sleep (3.0)
    print (" Stage 3 complete : Coordinated flight achieved .")
    if __name__ == " __main__ ":
    stage3_coordinated_flight ()        

    4.3 Detailed Explanation

    Stage 4: Dynamic Trajectory Control

    5.1 Objective:

    Implement dynamic re-planning for a single drone by commanding it to follow a circular trajectory. The drone’s setpoints are continuously updated using cmdPosition to follow the circular path.

    5.2 Boilerplate Code

    #!/ usr/bin/env python
    """
    Stage 4: Dynamic Trajectory and Re - planning
    This script demonstrates advanced control where a single drone follows a circular
    trajectory
    with continuous re - planning .
    """
    import time
    import numpy as np
    from crazyswarm import Crazyswarm
    def stage4_dynamic_trajectory () :
    print (" Stage 4: Initializing simulation for dynamic trajectory ...")
    swarm = Crazyswarm ()
    allcfs = swarm . allcfs
    timeHelper = swarm . timeHelper
    # Use the first Crazyflie
    cf = allcfs . crazyflies [0]
    # Takeoff
    print (" Taking off ...")
    cf . takeoff ( targetHeight =1.0 , duration =2.0)
    timeHelper . sleep (4.0)
    # Dynamic trajectory : circular motion in the XY plane
    print (" Executing dynamic circular trajectory ...")
    center = np . array ([1.0 , 1.0])
    radius = 0.5
    duration = 10.0
    steps = 50
    dt = duration / steps
    6
    for i in range ( steps ) :
    theta = 2 * np . pi * i / steps
    pos_x = center [0] + radius * np . cos ( theta )
    pos_y = center [1] + radius * np . sin ( theta )
    pos = [ pos_x , pos_y , 1.0]
    cf . cmdPosition ( pos , yaw =0.0)
    timeHelper . sleep ( dt )
    # Hover and then land
    print (" Hovering ...")
    timeHelper . sleep (2.0)
    print (" Landing ...")
    cf . land ( targetHeight =0.0 , duration =2.0)
    timeHelper . sleep (3.0)
    print (" Stage 4 complete : Dynamic trajectory executed .")
    if __name__ == " __main__ ":
    stage4_dynamic_trajectory ()       
    

    5.3 Detailed Explanation

    Stage 5:Advanced Swarm Behavior with Collision Avoidance.

    6.1 Objective:

    Develop an advanced control scenario where multiple drones perform coordinated flight while dynamically adjusting their trajectories to avoid collisions. This is achieved by computing avoidance offsets for each drone based on its neighbors and continuously updating setpoints using cmdPosition.

    6.2 Boilerplate Code

    #!/ usr/bin/env python
    """
    Stage 5: Advanced Swarm Behavior with Collision Avoidance
    This script demonstrates advanced behavior where multiple Crazyflies execute a
    coordinated maneuver
    and dynamically adjust trajectories to avoid simulated collisions .
    """
    import time
    import numpy as np
    from crazyswarm import Crazyswarm
    7
    def compute_avoidance_offset ( current_pos , desired_pos , other_positions ,
    min_distance =0.3) :
    """
    Computes a simple repulsive offset to maintain a minimum distance from other
    drones .
    For each drone closer than min_distance , a repulsive force is applied
    proportional to the difference .
    """
    avoidance = np . array ([0.0 , 0.0 , 0.0])
    for pos in other_positions :
    vec = np . array ( desired_pos ) - np . array ( pos )
    distance = np . linalg . norm ( vec )
    if distance < min_distance and distance > 0:
    avoidance += ( vec / distance ) * ( min_distance - distance )
    return avoidance
    def stage5_advanced_swarm () :
    print (" Stage 5: Initializing simulation for advanced swarm behavior ...")
    swarm = Crazyswarm ()
    allcfs = swarm . allcfs
    timeHelper = swarm . timeHelper
    # Set group mask for coordinated control
    for cf in allcfs . crazyflies :
    cf . setGroupMask (1)
    # Synchronized takeoff : All drones ascend to 1.0 meter .
    print (" Coordinated takeoff for swarm ...")
    allcfs . takeoff ( targetHeight =1.0 , duration =2.0)
    timeHelper . sleep (4.0)
    # Define target formation ( example formation )
    target_formation = [
    [1.0 , 1.0 , 1.0] ,
    [1.2 , 1.0 , 1.0] ,
    [1.0 , 1.2 , 1.0] ,
    [0.8 , 1.0 , 1.0] ,
    [1.0 , 0.8 , 1.0]
    ]
    steps = 30
    dt = 0.5
    # Dynamic formation and collision avoidance loop
    for step in range ( steps ) :
    for i , cf in enumerate ( allcfs . crazyflies ) :
    target = target_formation [ i % len ( target_formation ) ]
    current_pos = np . array ( cf . position () )
    desired = np . array ( target )
    error = desired - current_pos
    other_positions = [ np . array ( other_cf . position () ) for j , other_cf in
    enumerate ( allcfs . crazyflies ) if j != i ]
    avoidance = compute_avoidance_offset ( current_pos , target ,
    other_positions )
    new_setpoint = current_pos + 0.1 * error + avoidance
    cf . cmdPosition ( new_setpoint . tolist () , yaw =0.0)
    timeHelper . sleep ( dt )
    # Hover in formation for 2 seconds
    print (" Hovering in formation ...")
    8
    timeHelper . sleep (2.0)
    # Synchronized landing : All drones descend to 0.0 meters .
    print (" Coordinated landing ...")
    allcfs . land ( targetHeight =0.0 , duration =2.0)
    timeHelper . sleep (3.0)
    print (" Stage 5 complete : Advanced swarm behavior executed .")
    if __name__ == " __main__ ":
    stage5_advanced_swarm ()

    6.3 Detailed Explanation

    Submission Guidelines

    References

  • Our Team Get in Touch with us