Everything in bitsim is built from a few types and helpers: its number, vector and matrix types, its constants, points, angles and units, and the JSON files that describe robots. This example shows each of them; every other example uses them.
What you need
Nothing beyond bitsim itself. See Installing bitsim.
Numbers, vectors and matrices
bitsim/bitsim_types.h names the types bitsim works with. real_t is its floating point type, a double, and uint_t its unsigned integer, a std::size_t. Vectors and matrices are Eigen's:
| Type | What it is |
| RealColVec2d, RealColVec3d, RealColVec4d | a column vector of two, three or four real_t |
| RealMat2d, RealMat3d, RealMat4d | a 2 x 2, 3 x 3 or 4 x 4 matrix |
| DynMat<T>, DynVec<T>, ColVec<T> | a matrix, a row vector and a column vector whose size is set when they are made |
| RealMat, RealVec, RealColVec | DynMat<real_t>, DynVec<real_t> and ColVec<real_t> |
| Mat<T, N, M>, SquareMat<T, N> | an N x M and an N x N matrix whose size is fixed when compiled |
| UnalignedMat<T, N, M> | an N x M matrix Eigen does not align, for members of objects made with new |
| index_t | the type of the indices and sizes of the vectors and matrices |
bitsim's code uses Eigen's vector and matrix types only through these names.
The examples about frames and kinematics use Chrono's ChVector3d and ChMatrix33d as well, because Chrono simulates them; ChMatrix33d is an Eigen matrix too.
Constants
bitsim/bitsim_consts.h has \(\pi\), consts::maths::PI, and the acceleration of gravity, consts::maths::G, a TOLERANCE for comparing numbers, INVALID_ID and INVALID_STR for what is not set, and the directories with the robot, sensor and world files, ROBOTS_DIR, SENSORS_DIR and WORLDS_DIR.
Points
GeomPoint<dim> is a point in dim dimensions. It adds, subtracts and scales like a vector, and knows its distance to other points, its norm and its dot product.
Angles and units
bitsim/utils/unit_converter.h converts degrees to radians and back, revolutions per minute to rad/s, and the speed of a wheel to the speed of the robot. maths::wrap_angle wraps an angle to \((-\pi, \pi]\): headings are compared after wrapping, since turning 350 degrees to the left ends where turning 10 degrees to the right does.
Robot files
The simulated robots of the later examples are described by JSON files in ROBOTS_DIR: the size of the chassis and the wheels, the motors, and the sensors with their backends. The example reads bitsim_diff_drive_robot.json, the robot of the line following examples, with the nlohmann/json library that bitsim ships.
Running the example
Output
1. Numbers, vectors and matrices
real_t has 8 bytes, uint_t 8
|v| 3.000
(A v)_x 5.000
M is 2 x 4
sum of M 4.000
max of the samples 0.400
2. Constants
PI 3.142
G [m/s^2] 9.810
TOLERANCE 1.0e-08
INVALID_ID is uint_t(-1) true
3. Points
a = ( 1,2 ), b = ( 4,6 ), a + b = ( 5,8 ), 2 a = ( 2,4 )
distance from a to b 5.000
|b - a| 5.000
a . b 16.000
4. Angles and units
90 deg [rad] 1.571
1 rad [deg] 57.296
60 rpm [rad/s] 6.283
1 rad/s, 0.1 m wheel [m/s] 0.100
350 deg wrapped [deg] -10.000
5. Robot files
bitsim_diff_drive_robot.json describes "Odysseus"
wheel radius [m] 0.100
axle length [m] 0.400
motor gear ratio 30.000
sensors: front-camera (downward-camera) front-sonar (ultrasonic-range) imu (imu) line-sensor (infrared-array) rear-sonar (ultrasonic-range)
The labelled lines come from bitsim/utils/io/print_utils.h, which the other examples use too.
The code
#include "bitsim/extern/nlohmann/json/json.hpp"
#include <filesystem>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>
{
{
std::cout <<
" real_t has " <<
sizeof(
real_t) <<
" bytes, uint_t " <<
sizeof(
uint_t) <<
"\n";
A(0, 1) = 2.0;
M.setConstant(0.5);
std::cout << " M is " << M.rows() << " x " << M.cols() << "\n";
const std::vector<real_t> samples{0.1, 0.4, 0.2};
}
{
}
{
std::cout << std::defaultfloat << std::setprecision(6) << " a = " << a << ", b = " << b
<< ", a + b = " << a + b << ", 2 a = " << 2.0 * a << "\n";
}
{
using namespace bitsim::utils::unit_converter;
}
{
const std::filesystem::path file =
std::ifstream in(file);
if (!in)
{
throw std::runtime_error("Cannot open the robot file " + file.string());
}
const nlohmann::json robot = nlohmann::json::parse(in);
std::cout << " " << file.filename().string() << " describes " << robot["name"] << "\n";
print_number(
"wheel radius [m]", robot[
"robot-data"][
"wheel_radius"].get<real_t>());
print_number(
"axle length [m]", robot[
"robot-data"][
"axle_length"].get<real_t>());
print_number(
"motor gear ratio", robot[
"motors"][
"parameters"][
"gear_ratio"].get<real_t>());
std::cout << " sensors:";
for (const auto &[name, sensor] : robot["sensors"]["sensors"].items())
{
std::cout <<
" " << name <<
" (" <<
sensor[
"type"].get<std::string>() <<
")";
}
std::cout << "\n";
}
}
{
try
{
return 0;
}
catch (const std::exception &e)
{
std::cerr << "Error: " << e.what() << std::endl;
return 1;
}
}
The basic types of bitsim. Code outside this file names Eigen's matrix and vector types only through ...
A class that describes a point with spacedim spatial dimension space.
Definition geom_point.h:22
int main()
Definition discounted_returns.cpp:19
Definition basic_types.cpp:22
void constants()
Definition basic_types.cpp:55
void angles_and_units()
Definition basic_types.cpp:80
void numbers_and_matrices()
Definition basic_types.cpp:29
void robot_files()
Definition basic_types.cpp:96
void points()
Definition basic_types.cpp:65
const real_t G
Acceleration due to gravity m/secs.
Definition bitsim_consts.h:68
const real_t PI
The PI constant.
Definition bitsim_consts.h:63
const real_t TOLERANCE
Tolerance used around the library.
Definition bitsim_consts.h:26
const std::string ROBOTS_DIR
Path to robots definitions.
Definition bitsim_consts.h:42
const uint_t INVALID_ID
Invalid id.
Definition bitsim_consts.h:16
real_t wrap_angle(real_t angle)
The angle [rad] wrapped to , so that angles that differ by whole turns compare equal.
Definition math_utils.h:66
VectorType::value_type max(const VectorType &vec)
Returns the max element in the vector.
Definition math_utils.h:93
Definition csv_file_reader.cpp:12
void print_error(const std::string &label, real_t value, int width)
The label and the value in scientific notation with one decimal, for small errors.
Definition print_utils.cpp:33
void print_flag(const std::string &label, bool value, int width)
The label and true or false.
Definition print_utils.cpp:27
void print_number(const std::string &label, real_t value, int width)
The label and the value, with 3 decimals.
Definition print_utils.cpp:21
void print_title(const std::string &title, std::ostream &out)
An empty line and then the title.
Definition print_utils.cpp:17
real_t rad_to_degrees(real_t rad)
Definition unit_converter.h:36
real_t rpm_to_angular_speed(uint_t rpm)
Definition unit_converter.h:21
real_t angular_speed_to_linear_speed(real_t r, real_t w)
Definition unit_converter.h:30
real_t degrees_to_rad(real_t degs)
Definition unit_converter.h:42
std::size_t uint_t
uint_t
Definition bitsim_types.h:40
Eigen::MatrixX< T > DynMat
Dynamically sized matrix to use around the library.
Definition bitsim_types.h:51
Eigen::Matrix3< real_t > RealMat3d
3×3 matrix of type double.
Definition bitsim_types.h:86
double real_t
real_t
Definition bitsim_types.h:20
Eigen::Vector3d RealColVec3d
3D column vector
Definition bitsim_types.h:137
void sensor(ConcurrentQueue< GyroReading > &queue)
Definition producer_consumer.cpp:45
Print labelled values, one per line, as the examples do: two spaces, the label padded to a width,...
Various unit conversion utilities.