Contact Sensor

Contact Sensor#

A contact sensor with filtering

The contact sensor is designed to return the net contact force acting on a given ridgid body. The sensor is written to behave as a physical object, and so the “scope” of the contact sensor is limited to the body (or bodies) that defines it. There are multiple ways to define this scope, depending on your need to filter the forces coming from the contact.

By default, the reported force is the total contact force, but your application may only care about contact forces due to specific objects. Retrieving contact forces from specific objects requires filtering, and this can only be done in a “many-to-one” way. A multi-legged robot that needs filterable contact information for its feet would require one sensor per foot to be defined in the environment, but a robotic hand with contact sensors on the tips of each finger can be defined with a single sensor.

Consider a simple environment with an Anymal Quadruped and a block

from isaaclab_assets.robots.anymal import ANYMAL_C_CFG  # isort: skip


@configclass
class ContactSensorSceneCfg(InteractiveSceneCfg):
    """Design the scene with sensors on the robot."""

    # ground plane
    ground = AssetBaseCfg(prim_path="/World/defaultGroundPlane", spawn=sim_utils.GroundPlaneCfg())

    # lights
    dome_light = AssetBaseCfg(
        prim_path="/World/Light", spawn=sim_utils.DomeLightCfg(intensity=3000.0, color=(0.75, 0.75, 0.75))
    )

    # robot
    robot = ANYMAL_C_CFG.replace(prim_path="{ENV_REGEX_NS}/Robot")

    # Rigid Object
    cube = RigidObjectCfg(
        prim_path="{ENV_REGEX_NS}/Cube",
        spawn=sim_utils.CuboidCfg(
            size=(0.5, 0.5, 0.1),
            rigid_props=sim_utils.RigidBodyPropertiesCfg(),
            mass_props=sim_utils.MassPropertiesCfg(mass=100.0),
            collision_props=sim_utils.CollisionPropertiesCfg(),
            physics_material=sim_utils.RigidBodyMaterialCfg(static_friction=1.0),
            visual_material=sim_utils.PreviewSurfaceCfg(diffuse_color=(0.0, 1.0, 0.0), metallic=0.2),
        ),
        init_state=RigidObjectCfg.InitialStateCfg(pos=(0.5, 0.5, 0.05)),
    )

    contact_forces_LF = ContactSensorCfg(
        prim_path="{ENV_REGEX_NS}/Robot/LF_FOOT",
        update_period=0.0,
        history_length=6,
        debug_vis=True,
        filter_prim_paths_expr=["{ENV_REGEX_NS}/Cube"],
    )

    contact_forces_RF = ContactSensorCfg(
        prim_path="{ENV_REGEX_NS}/Robot/RF_FOOT",
        update_period=0.0,
        history_length=6,
        debug_vis=True,
        filter_prim_paths_expr=["{ENV_REGEX_NS}/Cube"],
    )

    contact_forces_H = ContactSensorCfg(
        prim_path="{ENV_REGEX_NS}/Robot/.*H_FOOT",
        update_period=0.0,

We define the sensors on the feet of the robot in two different ways. The front feet are independent sensors (one sensor body per foot) and the “Cube” is placed under the left foot. The hind feet are defined as a single sensor with multiple bodies.

We can then run the scene and print the data from the sensors

def run_simulator(sim: sim_utils.SimulationContext, scene: InteractiveScene):
  .
  .
  .
  # Simulate physics
  while simulation_app.is_running():
    .
    .
    .
    # print information from the sensors
    print("-------------------------------")
    print(scene["contact_forces_LF"])
    print("Received force matrix of: ", scene["contact_forces_LF"].data.force_matrix_w)
    print("Received contact force of: ", scene["contact_forces_LF"].data.net_forces_w)
    print("-------------------------------")
    print(scene["contact_forces_RF"])
    print("Received force matrix of: ", scene["contact_forces_RF"].data.force_matrix_w)
    print("Received contact force of: ", scene["contact_forces_RF"].data.net_forces_w)
    print("-------------------------------")
    print(scene["contact_forces_H"])
    print("Received force matrix of: ", scene["contact_forces_H"].data.force_matrix_w)
    print("Received contact force of: ", scene["contact_forces_H"].data.net_forces_w)

Here, we print both the net contact force and the filtered force matrix for each contact sensor defined in the scene. The front left and front right feet report the following

-------------------------------
Contact sensor @ '/World/envs/env_.*/Robot/LF_FOOT':
        view type         : <class 'omni.physics.tensors.impl.api.RigidBodyView'>
        update period (s) : 0.0
        number of bodies  : 1
        body names        : ['LF_FOOT']

Received force matrix of:  tensor([[[[-1.3923e-05,  1.5727e-04,  1.1032e+02]]]], device='cuda:0')
Received contact force of:  tensor([[[-1.3923e-05,  1.5727e-04,  1.1032e+02]]], device='cuda:0')
-------------------------------
Contact sensor @ '/World/envs/env_.*/Robot/RF_FOOT':
        view type         : <class 'omni.physics.tensors.impl.api.RigidBodyView'>
        update period (s) : 0.0
        number of bodies  : 1
        body names        : ['RF_FOOT']

Received force matrix of:  tensor([[[[0., 0., 0.]]]], device='cuda:0')
Received contact force of:  tensor([[[1.3529e-05, 0.0000e+00, 1.0069e+02]]], device='cuda:0')
The contact sensor visualization

Notice that even with filtering, both sensors report the net contact force acting on the foot. However, the “force matrix” on the right foot is zero because that foot isn’t in contact with the filtered body, /World/envs/env_.*/Cube. Now, checkout the data coming from the hind feet!

-------------------------------
Contact sensor @ '/World/envs/env_.*/Robot/.*H_FOOT':
        view type         : <class 'omni.physics.tensors.impl.api.RigidBodyView'>
        update period (s) : 0.0
        number of bodies  : 2
        body names        : ['LH_FOOT', 'RH_FOOT']

Received force matrix of:  None
Received contact force of:  tensor([[[9.7227e-06, 0.0000e+00, 7.2364e+01],
        [2.4322e-05, 0.0000e+00, 1.8102e+02]]], device='cuda:0')

In this case, the contact sensor has two bodies: the left and right hind feet. When the force matrix is queried, the result is None because this is a many body sensor, and presently Isaac Lab only supports “many to one” contact force filtering. Unlike the single body contact sensor, the reported force tensor has multiple entries, with each “row” corresponding to the contact force on a single body of the sensor (matching the ordering at construction).

Code for contact_sensor.py
  1# Copyright (c) 2022-2026, The Isaac Lab Project Developers (https://github.com/isaac-sim/IsaacLab/blob/main/CONTRIBUTORS.md).
  2# All rights reserved.
  3#
  4# SPDX-License-Identifier: BSD-3-Clause
  5
  6"""Launch Isaac Sim Simulator first."""
  7
  8import argparse
  9
 10from isaaclab.app import AppLauncher
 11
 12# add argparse arguments
 13parser = argparse.ArgumentParser(description="Example on using the contact sensor.")
 14parser.add_argument("--num_envs", type=int, default=1, help="Number of environments to spawn.")
 15# append AppLauncher cli args
 16AppLauncher.add_app_launcher_args(parser)
 17# demos should open Kit visualizer by default
 18parser.set_defaults(visualizer=["kit"])
 19# parse the arguments
 20args_cli = parser.parse_args()
 21
 22# launch omniverse app
 23app_launcher = AppLauncher(args_cli)
 24simulation_app = app_launcher.app
 25
 26"""Rest everything follows."""
 27
 28import torch
 29import warp as wp
 30
 31import isaaclab.sim as sim_utils
 32from isaaclab.assets import AssetBaseCfg, RigidObjectCfg
 33from isaaclab.scene import InteractiveScene, InteractiveSceneCfg
 34from isaaclab.sensors import ContactSensorCfg
 35from isaaclab.utils import configclass
 36
 37##
 38# Pre-defined configs
 39##
 40from isaaclab_assets.robots.anymal import ANYMAL_C_CFG  # isort: skip
 41
 42
 43@configclass
 44class ContactSensorSceneCfg(InteractiveSceneCfg):
 45    """Design the scene with sensors on the robot."""
 46
 47    # ground plane
 48    ground = AssetBaseCfg(prim_path="/World/defaultGroundPlane", spawn=sim_utils.GroundPlaneCfg())
 49
 50    # lights
 51    dome_light = AssetBaseCfg(
 52        prim_path="/World/Light", spawn=sim_utils.DomeLightCfg(intensity=3000.0, color=(0.75, 0.75, 0.75))
 53    )
 54
 55    # robot
 56    robot = ANYMAL_C_CFG.replace(prim_path="{ENV_REGEX_NS}/Robot")
 57
 58    # Rigid Object
 59    cube = RigidObjectCfg(
 60        prim_path="{ENV_REGEX_NS}/Cube",
 61        spawn=sim_utils.CuboidCfg(
 62            size=(0.5, 0.5, 0.1),
 63            rigid_props=sim_utils.RigidBodyPropertiesCfg(),
 64            mass_props=sim_utils.MassPropertiesCfg(mass=100.0),
 65            collision_props=sim_utils.CollisionPropertiesCfg(),
 66            physics_material=sim_utils.RigidBodyMaterialCfg(static_friction=1.0),
 67            visual_material=sim_utils.PreviewSurfaceCfg(diffuse_color=(0.0, 1.0, 0.0), metallic=0.2),
 68        ),
 69        init_state=RigidObjectCfg.InitialStateCfg(pos=(0.5, 0.5, 0.05)),
 70    )
 71
 72    contact_forces_LF = ContactSensorCfg(
 73        prim_path="{ENV_REGEX_NS}/Robot/LF_FOOT",
 74        update_period=0.0,
 75        history_length=6,
 76        debug_vis=True,
 77        filter_prim_paths_expr=["{ENV_REGEX_NS}/Cube"],
 78    )
 79
 80    contact_forces_RF = ContactSensorCfg(
 81        prim_path="{ENV_REGEX_NS}/Robot/RF_FOOT",
 82        update_period=0.0,
 83        history_length=6,
 84        debug_vis=True,
 85        filter_prim_paths_expr=["{ENV_REGEX_NS}/Cube"],
 86    )
 87
 88    contact_forces_H = ContactSensorCfg(
 89        prim_path="{ENV_REGEX_NS}/Robot/.*H_FOOT",
 90        update_period=0.0,
 91        history_length=6,
 92        debug_vis=True,
 93    )
 94
 95
 96def run_simulator(sim: sim_utils.SimulationContext, scene: InteractiveScene):
 97    """Run the simulator."""
 98    # Define simulation stepping
 99    sim_dt = sim.get_physics_dt()
100    sim_time = 0.0
101    count = 0
102
103    # Simulate physics
104    while simulation_app.is_running():
105        if count % 500 == 0:
106            # reset counter
107            count = 0
108            # reset the scene entities
109            # root state
110            # we offset the root state by the origin since the states are written in simulation world frame
111            # if this is not done, then the robots will be spawned at the (0, 0, 0) of the simulation world
112            root_pose = wp.to_torch(scene["robot"].data.default_root_pose).clone()
113            root_pose[:, :3] += scene.env_origins
114            scene["robot"].write_root_pose_to_sim_index(root_pose=root_pose)
115            root_vel = wp.to_torch(scene["robot"].data.default_root_vel).clone()
116            scene["robot"].write_root_velocity_to_sim_index(root_velocity=root_vel)
117            # set joint positions with some noise
118            joint_pos, joint_vel = (
119                wp.to_torch(scene["robot"].data.default_joint_pos).clone(),
120                wp.to_torch(scene["robot"].data.default_joint_vel).clone(),
121            )
122            joint_pos += torch.rand_like(joint_pos) * 0.1
123            scene["robot"].write_joint_position_to_sim_index(position=joint_pos)
124            scene["robot"].write_joint_velocity_to_sim_index(velocity=joint_vel)
125            # clear internal buffers
126            scene.reset()
127            print("[INFO]: Resetting robot state...")
128        # Apply default actions to the robot
129        # -- generate actions/commands
130        targets = wp.to_torch(scene["robot"].data.default_joint_pos)
131        # -- apply action to the robot
132        scene["robot"].set_joint_position_target_index(target=targets)
133        # -- write data to sim
134        scene.write_data_to_sim()
135        # perform step
136        sim.step()
137        # update sim-time
138        sim_time += sim_dt
139        count += 1
140        # update buffers
141        scene.update(sim_dt)
142
143        # print information from the sensors
144        print("-------------------------------")
145        print(scene["contact_forces_LF"])
146        print("Received force matrix of: ", scene["contact_forces_LF"].data.force_matrix_w)
147        print("Received contact force of: ", scene["contact_forces_LF"].data.net_forces_w)
148        print("-------------------------------")
149        print(scene["contact_forces_RF"])
150        print("Received force matrix of: ", scene["contact_forces_RF"].data.force_matrix_w)
151        print("Received contact force of: ", scene["contact_forces_RF"].data.net_forces_w)
152        print("-------------------------------")
153        print(scene["contact_forces_H"])
154        print("Received force matrix of: ", scene["contact_forces_H"].data.force_matrix_w)
155        print("Received contact force of: ", scene["contact_forces_H"].data.net_forces_w)
156
157
158def main():
159    """Main function."""
160
161    # Initialize the simulation context
162    sim_cfg = sim_utils.SimulationCfg(dt=0.005, device=args_cli.device)
163    sim = sim_utils.SimulationContext(sim_cfg)
164    # Set main camera
165    sim.set_camera_view(eye=[3.5, 3.5, 3.5], target=[0.0, 0.0, 0.0])
166    # design scene
167    scene_cfg = ContactSensorSceneCfg(num_envs=args_cli.num_envs, env_spacing=2.0)
168    scene = InteractiveScene(scene_cfg)
169    # Play the simulator
170    sim.reset()
171    # Now we are ready!
172    print("[INFO]: Setup complete...")
173    # Run the simulator
174    run_simulator(sim, scene)
175
176
177if __name__ == "__main__":
178    # run the main function
179    main()
180    # close sim app
181    simulation_app.close()