Tutorial 5: Sensors

 

 

Sensors acquire information from a simulation.  There are several types of Sensors, including Acceleration Sensors, Raycast Sensors, and Volume Sensors.  This tutorial will show how to setup each type.  It will also give examples of how each type could be used.  Keep in mind that a Sensor can either be attached to a Solid or just positioned somewhere in the environment.

 

Let’s first create all the objects we’ll need (and assume they will be initialized elsewhere):

 

 

opal::Simulator* sim = opal::createSimulator();

opal::Solid* solid = sim->createSolid();

 

 

 

 

 

 

Acceleration Sensor

The first Sensor we will use here is an Acceleration Sensor:

 

 

opal::AccelerationSensorData data;

data.solid = solid;

data.transform.translate(4, 0, -1);

opal::AccelerationSensor* accelSensor = sim->createAccelerationSensor();

accelSensor->init(data);

 

...

 

opal::Vec3r accel = accelSensor->getGlobalLinearAccel();

 

 

 

 

 

 

 

 

 

 

 

 

The Sensor is automatically attached to the Solid if its Solid pointer is set.  It calculates the linear and angular acceleration of the attached Solid at the point of the Sensor (which can be offset from the Solid by using the Sensor’s transform).  It could be used in a simulated robot to give it the ability to sense which way it is falling.

 

Incline Sensor

The next Sensor is the Incline Sensor:

 

 

opal::InclineSensorData data;

data.solid = solid;

data.axis.set(1, 0, 0);

opal::InclineSensor* inclinometer = sim->createInclineSensor();

inclinometer->init(data);

 

...

 

opal::real angle = inclinometer->getAngle();

 

 

 

 

 

 

 

 

 

 

 

 

The Sensor takes a local rotation axis relative to its attached Solid.  It calculates and returns the angle the Solid has rotated around this axis.  Multiple Incline Sensors could be attached to a vehicle, for instance, to simulate a real inclinometer.  The combination of an Acceleration Sensor and an Incline Sensor could be used to simulate the human vestibular system.

 

Raycast Sensor

The next Sensor is the Raycast Sensor:

 

 

opal::RaycastSensorData data;

data.solid = solid;

data.ray.setOrigin(opal::Point3r(0, 0, 0));

data.ray.setDir(opal::Vec3r(0, 0, -1));

data.contactGroup = 2;

opal::RaycastSensor* raySensor = sim->createRaycastSensor();

raySensor->init(data);

 

...

 

opal::RaycastResult result = raySensor->fireRay();

opal::Point3r hitPoint = result.intersection;

opal::Solid* hitSolid = result.solid;

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Raycast Sensors must be fired manually – they do not update anything every time step.  When they are fired, they cast a ray into the environment and return data describing the closest point of intersection (if any) from the ray’s origin.  The Raycast Sensor uses a contact group to limit which objects it collides with.  (See other tutorials for more information on contact groups.)

 

Volume Sensor

The Volume Sensor is similar to the Raycast Sensor:

 

 

opal::VolumeSensorData data;

data.contactGroup = 5;

opal:: VolumeSensor* volSensor = sim->createVolumeSensor();

volSensor->init(data);

 

// Define a “volume” Solid.

opal::Solid* volume = sim->createSolid();

opal::SphereShapeData sphereData;

sphereData.radius = 5;

volume->addShape(sphereData);

 

...

 

opal::VolumeQueryResult result = volSensor->queryVolume(volume);

for (int i=0; i<result.getNumSolids(); ++i)

{

    opal::Solid* s = result.getSolid(i);

    opal::Force f;

    f.type = opal::GLOBAL_FORCE;

    f.duration = (opal::real)0.05;

    f.vec = s->getPosition() – volume->getPosition();

    f.vec *= 100;   

    s->addForce(f);

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Volume Sensors take a “volume” Solid and query the environment for Solids that collide with the volume Solid.  The resulting data structure contains a list of pointers to those Solids.  Like Raycast Sensors, they can utilize contact groups to limit the set of Solids that are collided with the volume Solid; simply set the contact group of the volume Solid.  They also are not updated every time step; they must be queried manually.  The example above shows how to simulate an explosion: it finds all the Solids within a 5 unit radius from the volume’s center and applies an outward force on each object scaled linearly by its distance from the center.

 

Destroying Sensors

When you are done with a Sensor, tell the Simulator to destroy it:

 

 

sim->destroySensor(sensor);

 

 

OPAL is Copyright © 2004-2005 Alan Fischer, Andres Reinot, and Tyler Streeter