Main Content

addCollision

Add collision geometry to rigid body

Since R2020b

Description

addCollision(body,type,parameters) adds a collision geometry of the specified type type and geometric parameters parameters to the specified rigid body body.

example

addCollision(body,collisionObj) adds a collision geometry object to the rigid body body, specified as one of these collision objects:

This syntax uses the Pose property of the specified collision object to transform the collision vertices into the rigid body frame.

addCollision(___,tform) specifies a transformation for the collision geometry relative to the body frame in addition to any combination of input arguments from previous syntaxes.

Examples

collapse all

Load a robot model and modify the collision meshes. Clear existing collision meshes, add simple collision object primitives, and check whether certain configurations are in collision.

Load Robot Model

Load a preconfigured robot model into the workspace using the loadrobot function. This model already has collision meshes specified for each body. Iterate through all the rigid body elements and clear the existing collision meshes. Confirm that the existing meshes are gone.

robot = loadrobot("kukaIiwa7",DataFormat="column");

for i = 1:robot.NumBodies
    clearCollision(robot.Bodies{i})
end

show(robot,Collisions="on",Visuals="off");

Figure contains an axes object. The axes object with xlabel X, ylabel Y contains 21 objects of type patch, line. These objects represent world, iiwa_link_0, iiwa_link_1, iiwa_link_2, iiwa_link_3, iiwa_link_4, iiwa_link_5, iiwa_link_6, iiwa_link_7, iiwa_link_ee, iiwa_link_ee_kuka, iiwa_link_0_mesh, iiwa_link_1_mesh, iiwa_link_2_mesh, iiwa_link_3_mesh, iiwa_link_4_mesh, iiwa_link_5_mesh, iiwa_link_6_mesh, iiwa_link_7_mesh.

Add Collision Cylinders

Iteratively add a collision cylinder to each body. Skip some bodies for this specific model, as they overlap and always collide with the end effector (body 10).

collisionObj = collisionCylinder(0.05,0.25);

for i = 1:robot.NumBodies
    if i > 6 && i < 10
        % Skip these bodies.
    else
        addCollision(robot.Bodies{i},collisionObj)
    end
end

show(robot,Collisions="on",Visuals="off");

Figure contains an axes object. The axes object with xlabel X, ylabel Y contains 28 objects of type patch, line. These objects represent world, iiwa_link_0, iiwa_link_1, iiwa_link_2, iiwa_link_3, iiwa_link_4, iiwa_link_5, iiwa_link_6, iiwa_link_7, iiwa_link_ee, iiwa_link_ee_kuka, iiwa_link_0_mesh, iiwa_link_1_mesh, iiwa_link_2_mesh, iiwa_link_3_mesh, iiwa_link_4_mesh, iiwa_link_5_mesh, iiwa_link_6_mesh, iiwa_link_7_mesh, iiwa_link_0_coll_mesh, iiwa_link_1_coll_mesh, iiwa_link_2_coll_mesh, iiwa_link_3_coll_mesh, iiwa_link_4_coll_mesh, iiwa_link_5_coll_mesh, iiwa_link_ee_kuka_coll_mesh.

Check for Collisions

Generate a series of random configurations. Check whether the robot is in collision at each configuration. Visualize each configuration that has a collision.

figure
rng(0) % Set random seed for repeatability.
for i = 1:20
    config = randomConfiguration(robot);
    isColliding = checkCollision(robot,config,SkippedSelfCollisions="parent");
    if isColliding
        show(robot,config,Collisions="on",Visuals="off");
        title("Collision Detected")
    else
        % Skip non-collisions.
    end
end

Figure contains an axes object. The axes object with title Collision Detected, xlabel X, ylabel Y contains 28 objects of type patch, line. These objects represent world, iiwa_link_0, iiwa_link_1, iiwa_link_2, iiwa_link_3, iiwa_link_4, iiwa_link_5, iiwa_link_6, iiwa_link_7, iiwa_link_ee, iiwa_link_ee_kuka, iiwa_link_0_mesh, iiwa_link_1_mesh, iiwa_link_2_mesh, iiwa_link_3_mesh, iiwa_link_4_mesh, iiwa_link_5_mesh, iiwa_link_6_mesh, iiwa_link_7_mesh, iiwa_link_0_coll_mesh, iiwa_link_1_coll_mesh, iiwa_link_2_coll_mesh, iiwa_link_3_coll_mesh, iiwa_link_4_coll_mesh, iiwa_link_5_coll_mesh, iiwa_link_ee_kuka_coll_mesh.

Input Arguments

collapse all

Rigid body, specified as a rigidBody object.

Geometry type for collision geometry, specified as a string scalar. The specified type determines the format of the parameters input.

  • "box"[x y z]

  • "cylinder"[radius length]

  • "capsule"[radius length]

  • "sphere"radius

  • "mesh"n-by-3 matrix of vertices or an STL or DAE file name as a string

Data Types: char | string

Collision geometry parameters, specified as a numeric vector, numeric matrix, or string scalar. The type input determines the format of this value.

  • "box"[x y z]

  • "cylinder"[radius length]

  • "capsule"[radius length]

  • "sphere"radius

  • "mesh"n-by-3 matrix of vertices or an STL or DAE file name as a string

Data Types: single | double | char | string

Collision geometry object, specified as a collisionBox, collisionCapsule, collisionCylinder, collisionSphere, or collisionMesh object.

Transformation of collision geometry, specified as a 4-by-4 homogeneous transformation. If specifying a collision object for the collisionObj input, this function applies the specified transformation to the Pose property of the specified collision object to transform the collision vertices into the rigid body frame.

Data Types: single | double

Extended Capabilities

C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.

Version History

Introduced in R2020b

expand all