bitsim
Model, simulate, estimate and control robots in C++
Loading...
Searching...
No Matches
A simulated ultrasonic sensor

The smallest use of a sensor in bitsim: an ultrasonic range sensor on a box, simulated with Chrono, measuring the distance to another box in front of it. The line following examples use the same sensor on a robot; see bitsim::sensors for all the sensors.

What you need

bitsim built with Chrono; see Installing bitsim.

Sensors and backends

A sensor in bitsim is a SensorBase with a backend that does the measuring. UltrasonicSensor reads a range backend:

The program does not change with the backend: it calls read_values() on the sensor.

The scene

A fixed 2 x 2 m box, the target, has its centre 2 m ahead of a fixed 0.3 m box that carries the sensor. The face of the target is 1 m from the centre of the carrier, and the sensor reads 1 m at every step.

Running the example

./chrono_ultrasonic_sensor

Output

Sensor name: MyUltrasonicSensor
Sensor type: UltrasonicSensor
Sensor backend: SIMULATION
Measured distance: 1 in: m
...
Measured distance: 1 in: m

The code

#include "bitsim/bitsim_config.h"
#ifdef BITSIM_CHRONO
#include "chrono/physics/ChSystemSMC.h"
#include <chrono/physics/ChBodyEasy.h>
#endif
#include <iostream>
#include <memory>
namespace example
{
#ifdef BITSIM_CHRONO
void run_chrono()
{
using namespace bitsim;
// build a system to use with the backend
// the system carries the physics
chrono::ChSystemSMC sys;
sys.SetCollisionSystemType(chrono::ChCollisionSystem::Type::BULLET);
sys.SetGravitationalAcceleration(chrono::ChVector3d(0, 0, -9.81));
auto material = chrono_types::make_shared<chrono::ChContactMaterialSMC>();
material->SetYoungModulus(2e7);
material->SetFriction(0.5f);
material->SetRestitution(0.1f);
// 2. Create ground (target object)
auto ground = chrono_types::make_shared<chrono::ChBodyEasyBox>(2.0, 2.0, 0.1, // size
1000, // density
true, // visual
true, // collision
material);
ground->SetPos(chrono::ChVector3d(2.0, 0, 0.2)); // 2m in front
ground->SetFixed(true);
sys.Add(ground);
// 3. Create robot body (sensor mount)
auto robot =
chrono_types::make_shared<chrono::ChBodyEasyBox>(0.3, 0.3, 0.2, 1000, true, true, material);
robot->SetPos(chrono::ChVector3d(0, 0, 0.2));
robot->SetFixed(true);
sys.Add(robot);
sys.DoStepDynamics(0.1);
auto sensor_backend = std::make_shared<sensors::backends::CHRONO_UltrasonicBackend>(sys, robot);
sensor_backend->set_max_distance(5.0);
sensor_backend->set_sensor_units("m");
sensors::UltrasonicSensor sensor(sensor_backend, "MyUltrasonicSensor");
sensor.init();
std::cout << "Sensor name: " << sensor.sensor_name() << std::endl;
std::cout << "Sensor type: " << sensor.sensor_type() << std::endl;
std::cout << "Sensor backend: " << sensor.backend_type_str() << std::endl;
for (uint8_t i = 0; i < 10; i++)
{
sys.DoStepDynamics(0.1);
// read values for the sensor
auto values = sensor.read_values();
std::cout << "Measured distance: " << values[0] << " in: " << sensor.sensor_units()
<< std::endl;
}
}
#endif
} // namespace example
int main()
{
#ifdef BITSIM_CHRONO
#endif
return 0;
}
Class for modelling ultrasonic sensors.
Definition ultrasonic_sensor.h:21
int main()
Definition discounted_returns.cpp:19
Definition dc_motor_backend_base.h:9
Definition pytorch_linear_regression.cpp:17
void run_chrono()
Definition chrono_ultrasonic_sensor.cpp:17