Bird
Raised Fist0
ROSframework~5 mins

Collision avoidance in planning in ROS

Choose your learning style10 modes available
Introduction

Collision avoidance in planning helps robots move safely without bumping into things. It makes sure the robot finds a path that avoids obstacles.

When a robot needs to move around furniture in a room without hitting it.
When a drone flies through a forest and must avoid trees.
When an autonomous car drives on roads with other vehicles and pedestrians.
When a robot arm picks up objects without hitting nearby tools or parts.
Syntax
ROS
move_group = MoveGroupCommander("robot_arm")
move_group.set_planning_scene(planning_scene_interface)
move_group.set_pose_target(target_pose)
move_group.set_planner_id("RRTConnectkConfigDefault")
move_group.set_planning_time(10)
plan = move_group.plan()
if plan:
    move_group.execute(plan, wait=True)

This example uses the MoveIt! MoveGroupCommander in ROS to plan a path.

Collision avoidance happens automatically when the planning scene includes obstacles.

Examples
Without obstacles, the planner finds a direct path.
ROS
# Example 1: Planning with no obstacles
move_group.clear_path_constraints()
move_group.set_pose_target(target_pose)
plan = move_group.plan()
Obstacles in the scene make the planner find a path around them.
ROS
# Example 2: Planning with obstacles added to the scene
planning_scene_interface.add_box("obstacle", box_pose, box_size)
move_group.set_path_constraints(collision_avoidance_constraints)
plan = move_group.plan()
Sometimes no path is possible; handle this case gracefully.
ROS
# Example 3: Handling no valid path found
plan = move_group.plan()
if not plan:
    print("No collision-free path found. Try changing the goal or obstacles.")
Sample Program

This program initializes a ROS node and MoveIt! interfaces. It adds a box obstacle in front of the robot arm, then tries to plan a path to a target pose beyond the obstacle. The planner avoids the box automatically. It prints if a path is found and executes the movement.

ROS
#!/usr/bin/env python3
import sys
import rospy
from moveit_commander import MoveGroupCommander, PlanningSceneInterface, roscpp_initialize, roscpp_shutdown
from geometry_msgs.msg import PoseStamped

def main():
    roscpp_initialize(sys.argv)
    rospy.init_node('collision_avoidance_demo', anonymous=True)

    move_group = MoveGroupCommander("robot_arm")
    planning_scene = PlanningSceneInterface()

    rospy.sleep(2)  # Wait for scene to initialize

    # Add a box obstacle in front of the robot
    box_pose = PoseStamped()
    box_pose.header.frame_id = move_group.get_planning_frame()
    box_pose.pose.position.x = 0.5
    box_pose.pose.position.y = 0.0
    box_pose.pose.position.z = 0.5
    box_pose.pose.orientation.w = 1.0
    planning_scene.add_box("box1", box_pose, size=(0.2, 0.2, 0.2))

    rospy.sleep(1)  # Allow scene update

    # Set target pose beyond the obstacle
    target_pose = PoseStamped()
    target_pose.header.frame_id = move_group.get_planning_frame()
    target_pose.pose.position.x = 0.7
    target_pose.pose.position.y = 0.0
    target_pose.pose.position.z = 0.5
    target_pose.pose.orientation.w = 1.0

    move_group.set_pose_target(target_pose)

    print("Planning path with obstacle in scene...")
    plan = move_group.plan()

    if plan and plan.joint_trajectory.points:
        print("Path found! Executing...")
        move_group.execute(plan, wait=True)
        print("Movement complete.")
    else:
        print("No collision-free path found.")

    move_group.clear_pose_targets()
    roscpp_shutdown()

if __name__ == '__main__':
    main()
OutputSuccess
Important Notes

Collision avoidance planning usually takes more time than simple straight-line planning.

Make sure the planning scene is updated with all obstacles before planning.

If no path is found, try changing the goal or moving obstacles.

Summary

Collision avoidance helps robots move safely around obstacles.

Use planning scenes to add obstacles for the planner to avoid.

Always check if a valid path is found before executing movement.

Practice

(1/5)
1. What is the main purpose of collision avoidance in ROS planning?
easy
A. To make the robot ignore obstacles and move freely
B. To prevent the robot from hitting obstacles while moving
C. To speed up the robot's movement regardless of obstacles
D. To stop the robot from moving at all

Solution

  1. Step 1: Understand collision avoidance goal

    Collision avoidance means the robot should not bump into objects while moving.
  2. Step 2: Identify correct purpose

    The main goal is to keep the robot safe by avoiding obstacles during planning.
  3. Final Answer:

    To prevent the robot from hitting obstacles while moving -> Option B
  4. Quick Check:

    Collision avoidance = prevent hitting obstacles [OK]
Hint: Collision avoidance means safety, not speed or stopping [OK]
Common Mistakes:
  • Thinking collision avoidance speeds up movement
  • Believing robot ignores obstacles
  • Assuming robot should stop moving
2. Which ROS component is used to add obstacles for collision avoidance during planning?
easy
A. Planning Scene
B. Move Group Interface
C. Robot State Publisher
D. Joint State Listener

Solution

  1. Step 1: Identify obstacle representation in ROS planning

    Obstacles are added to the planning scene to inform the planner about their presence.
  2. Step 2: Match component to function

    The Planning Scene holds information about obstacles and environment for collision checking.
  3. Final Answer:

    Planning Scene -> Option A
  4. Quick Check:

    Obstacles added via Planning Scene [OK]
Hint: Obstacles go into the Planning Scene, not robot state [OK]
Common Mistakes:
  • Confusing Move Group Interface with obstacle storage
  • Thinking Robot State Publisher manages obstacles
  • Assuming Joint State Listener adds obstacles
3. Given this ROS planning code snippet, what will happen if the planner finds no valid path?
move_group.setStartStateToCurrentState()
move_group.setPoseTarget(target_pose)
plan = move_group.plan()
if plan:
    move_group.execute(plan)
else:
    print("No valid path found")
medium
A. The robot moves to the target pose anyway
B. The robot moves to a random position
C. The robot prints "No valid path found" and does not move
D. The planner crashes with an error

Solution

  1. Step 1: Analyze the plan check condition

    The code checks if plan is valid (truthy). If not, it prints a message.
  2. Step 2: Understand behavior when no path found

    If no valid path, the else block runs, printing "No valid path found" and skipping execution.
  3. Final Answer:

    The robot prints "No valid path found" and does not move -> Option C
  4. Quick Check:

    No path means print message, no movement [OK]
Hint: Check plan validity before execute to avoid errors [OK]
Common Mistakes:
  • Assuming robot moves without a valid plan
  • Expecting a crash instead of safe handling
  • Thinking robot moves randomly
4. Identify the error in this ROS planning snippet that prevents collision avoidance:
planning_scene = PlanningSceneInterface()
obstacle = CollisionObject()
obstacle.id = "box"
obstacle.header.frame_id = "world"
# Missing shape and pose assignment here
planning_scene.addCollisionObjects([obstacle])
medium
A. The obstacle has no shape or pose defined
B. The planning scene is not initialized
C. The obstacle ID is missing
D. The frame_id should be "base_link"

Solution

  1. Step 1: Check obstacle definition

    The obstacle lacks shape and pose, so it has no physical presence.
  2. Step 2: Understand impact on collision avoidance

    Without shape and pose, the planner cannot detect or avoid the obstacle.
  3. Final Answer:

    The obstacle has no shape or pose defined -> Option A
  4. Quick Check:

    Obstacle needs shape and pose to exist [OK]
Hint: Always define shape and pose for obstacles [OK]
Common Mistakes:
  • Ignoring shape and pose assignment
  • Thinking frame_id must be base_link always
  • Assuming missing ID causes failure
5. You want to ensure your robot avoids a newly added obstacle during planning. Which sequence correctly updates the planning scene and checks for a valid path before moving?
hard
A. Only call plan() without adding obstacle
B. Call execute() first, then add obstacle, then plan()
C. Add obstacle, execute plan without checking validity
D. Add obstacle to Planning Scene, call plan(), check if plan valid, then execute plan

Solution

  1. Step 1: Add obstacle to Planning Scene

    Adding the obstacle updates the environment for collision checking.
  2. Step 2: Plan and check validity before execution

    Call plan() to find a path avoiding obstacles, then verify plan is valid before executing.
  3. Step 3: Avoid unsafe execution

    Executing without a valid plan or before adding obstacles risks collisions or errors.
  4. Final Answer:

    Add obstacle to Planning Scene, call plan(), check if plan valid, then execute plan -> Option D
  5. Quick Check:

    Update scene, plan, validate, then execute [OK]
Hint: Always add obstacles before planning and check plan validity [OK]
Common Mistakes:
  • Executing before planning
  • Not adding obstacles before planning
  • Skipping plan validity check
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