Bird
Raised Fist0
Drone Programmingprogramming~10 mins

Collision avoidance in swarms in Drone Programming - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Collision avoidance in swarms
Start: Initialize swarm positions
↓
For each drone in swarm
↓
Check nearby drones within safety radius?
No→Maintain current path
Yes↓
Calculate avoidance vector
↓
Adjust drone velocity and direction
↓
Update drone position
↓
Repeat for all drones
↓
End of time step, repeat loop
Each drone checks nearby drones, calculates avoidance moves if too close, then updates position to avoid collisions.
Execution Sample
Drone Programming
for drone in swarm:
    neighbors = find_neighbors(drone, swarm, radius)
    if neighbors:
        avoid = compute_avoidance(drone, neighbors)
        drone.velocity += avoid
    drone.position += drone.velocity
Each drone looks for neighbors, computes avoidance if needed, then moves.
Execution Table
StepDrone IDNeighbors FoundAvoidance VectorVelocity BeforeVelocity AfterPosition BeforePosition After
1Drone 1[][0,0][1,0][1,0][0,0][1,0]
1Drone 2[Drone 1][-0.5,0][1,0][0.5,0][2,0][2.5,0]
1Drone 3[Drone 2][-0.3,0.1][1,0][0.7,0.1][4,1][4.7,1.1]
End------All drones updated positions, loop ends for this step
💡 All drones processed for this time step, positions updated to avoid collisions.
Variable Tracker
VariableStartAfter Drone 1After Drone 2After Drone 3Final
Drone 1 Position[0,0][1,0][1,0][1,0][1,0]
Drone 1 Velocity[1,0][1,0][1,0][1,0][1,0]
Drone 2 Position[2,0][2,0][2.5,0][2.5,0][2.5,0]
Drone 2 Velocity[1,0][1,0][0.5,0][0.5,0][0.5,0]
Drone 3 Position[4,1][4,1][4,1][4.7,1.1][4.7,1.1]
Drone 3 Velocity[1,0][1,0][1,0][0.7,0.1][0.7,0.1]
Key Moments - 3 Insights
Why does Drone 1 have no neighbors and no avoidance vector?
Drone 1 is the first checked and no other drones are within its safety radius yet, so it keeps its velocity unchanged (see execution_table row 1).
Why does Drone 2's velocity change after detecting Drone 1?
Drone 2 finds Drone 1 nearby, so it computes an avoidance vector to reduce collision risk, changing its velocity from [1,0] to [0.5,0] (see execution_table row 2).
How does the avoidance vector affect the drone's position?
The avoidance vector adjusts the velocity, which then updates the position by adding the new velocity to the current position (see execution_table rows 2 and 3).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is Drone 3's velocity after avoidance calculation?
A[1,0]
B[0.7,0.1]
C[-0.3,0.1]
D[0.5,0]
💡 Hint
Check the 'Velocity After' column for Drone 3 in the execution_table.
At which step does Drone 2 detect neighbors and adjust velocity?
AStep 1
BStep 2
CEnd
DNo adjustment
💡 Hint
Look at the 'Neighbors Found' and 'Velocity After' columns for Drone 2 in the execution_table.
If Drone 1 moved faster, how would that affect Drone 2's avoidance vector?
AAvoidance vector would be larger to keep distance
BAvoidance vector would be smaller
CNo change in avoidance vector
DDrone 2 would stop moving
💡 Hint
Consider how velocity affects proximity and avoidance calculations shown in variable_tracker.
Concept Snapshot
Collision avoidance in swarms:
- Each drone checks nearby drones within a safety radius.
- If neighbors are close, compute an avoidance vector.
- Adjust velocity by adding avoidance vector.
- Update position by adding velocity.
- Repeat for all drones each time step to prevent collisions.
Full Transcript
In collision avoidance for swarms, each drone looks around to find nearby drones within a safety radius. If it finds any, it calculates a small vector to move away from them, called the avoidance vector. This vector changes the drone's velocity to steer clear of collisions. Then the drone updates its position by moving according to the new velocity. This process repeats for every drone in the swarm each time step, ensuring they keep safe distances and avoid crashing into each other.

Practice

(1/5)
1. What is the main purpose of collision avoidance in drone swarms?
easy
A. To improve camera quality
B. To increase drone speed
C. To reduce battery usage
D. To keep drones from crashing into each other

Solution

  1. Step 1: Understand collision avoidance goal

    Collision avoidance is designed to prevent drones from hitting each other.
  2. Step 2: Compare options with goal

    Only To keep drones from crashing into each other matches the goal of preventing crashes.
  3. Final Answer:

    To keep drones from crashing into each other -> Option D
  4. Quick Check:

    Collision avoidance = prevent crashes [OK]
Hint: Collision avoidance means no crashes between drones [OK]
Common Mistakes:
  • Confusing collision avoidance with speed control
  • Thinking it saves battery directly
  • Assuming it improves camera quality
2. Which of the following code snippets correctly checks if two drones are too close (distance less than 5 units)?
easy
A. if distance == 5:
B. if distance > 5:
C. if distance < 5:
D. if distance <= 5:

Solution

  1. Step 1: Understand the condition for 'too close'

    'Too close' means distance is less than 5 units.
  2. Step 2: Match condition with code

    Only 'if distance < 5:' correctly checks if distance is less than 5.
  3. Final Answer:

    if distance < 5: -> Option C
  4. Quick Check:

    Less than 5 means too close [OK]
Hint: Use '<' to check if distance is less than threshold [OK]
Common Mistakes:
  • Using '>' instead of '<'
  • Using '==' which checks exact distance
  • Using '<=' which includes equal distance
3. What will be the output of this code snippet?
positions = [(0,0), (3,4), (10,10)]
threshold = 5
close_pairs = []
for i in range(len(positions)):
    for j in range(i+1, len(positions)):
        dx = positions[i][0] - positions[j][0]
        dy = positions[i][1] - positions[j][1]
        distance = (dx**2 + dy**2)**0.5
        if distance < threshold:
            close_pairs.append((i, j))
print(close_pairs)
medium
A. [(1, 2)]
B. []
C. [(0, 2)]
D. [(0, 1)]

Solution

  1. Step 1: Calculate distances between pairs

    Distance between (0,0) and (3,4) is 5; between (0,0) and (10,10) is about 14.14; between (3,4) and (10,10) is about 9.22.
  2. Step 2: Check which distances are less than threshold 5

    Only distance between (0,0) and (3,4) equals 5, but condition is distance < 5, so no pairs qualify except if distance is exactly 5 is excluded.
  3. Step 3: Re-examine condition and output

    Since distance == 5 is not < 5, no pairs added. So output is empty list [].
  4. Final Answer:

    [] -> Option B
  5. Quick Check:

    Distance less than 5 pairs = [] [OK]
Hint: Calculate distances carefully and compare with threshold [OK]
Common Mistakes:
  • Including pairs with distance equal to threshold
  • Miscomputing distance formula
  • Ignoring order of pairs
4. Identify the error in this collision avoidance code snippet:
for drone1 in swarm:
    for drone2 in swarm:
        if drone1 != drone2:
            dist = calculate_distance(drone1.position, drone2.position)
            if dist < safe_distance:
                drone1.move_away(drone2.position)
medium
A. It may cause multiple moves for one drone in one cycle
B. It compares the same drone with itself
C. It uses wrong distance calculation function
D. It does not check battery level before moving

Solution

  1. Step 1: Analyze nested loops and conditions

    Each drone compares with every other drone, excluding itself.
  2. Step 2: Consider move_away calls per cycle

    If multiple drones are too close, drone1 may call move_away multiple times in one loop, causing erratic movement.
  3. Final Answer:

    It may cause multiple moves for one drone in one cycle -> Option A
  4. Quick Check:

    Multiple moves per cycle = problem [OK]
Hint: Check if drone moves multiple times per loop [OK]
Common Mistakes:
  • Thinking comparing drone with itself is allowed
  • Assuming distance function is wrong without evidence
  • Ignoring battery checks which are unrelated here
5. You want to improve collision avoidance in a large drone swarm by reducing unnecessary moves. Which approach is best?
hard
A. Only move drones when the closest neighbor is within a smaller threshold
B. Make every drone move away from all neighbors every cycle
C. Ignore collision avoidance for drones with full battery
D. Increase drone speed to quickly pass neighbors

Solution

  1. Step 1: Understand problem of unnecessary moves

    Moving drones too often wastes energy and causes instability.
  2. Step 2: Evaluate options for reducing moves

    Only move drones when the closest neighbor is within a smaller threshold reduces moves by only reacting to closest neighbors within a smaller safe distance, minimizing unnecessary moves.
  3. Step 3: Reject other options

    Make every drone move away from all neighbors every cycle causes many moves; C ignores safety; D risks collisions by speeding up.
  4. Final Answer:

    Only move drones when the closest neighbor is within a smaller threshold -> Option A
  5. Quick Check:

    Reduce moves by tighter threshold [OK]
Hint: React only to closest neighbors within tight distance [OK]
Common Mistakes:
  • Making drones move too often
  • Ignoring safety for battery status
  • Increasing speed without safety checks
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard