A differential drive robot has two wheels on one axle, each with its own motor; it drives by turning them at the same speed and turns by turning them at different speeds. Its state in the plane is where the centre of its axle is and which way it faces, \((x, y, \theta)\), and it is driven by its speed \(v\) and its rate of turn \(\omega\). This example drives it with DiffDriveDynamics: straight, and then a quarter of a circle.
What you need
Nothing beyond bitsim itself. See Installing bitsim.
The model
With a constant \(v\) and \(\omega\) the robot drives a circle of radius \(v / \omega\). Over a step of \(\Delta t\) it drives an arc of length \(d = v \Delta t\) that turns it by \(\phi = \omega \Delta t\):
\[x' = x + \frac{d}{\phi} (\sin\theta' - \sin\theta), \qquad
y' = y - \frac{d}{\phi} (\cos\theta' - \cos\theta), \qquad
\theta' = \theta + \phi
\]
and with no turn, \(\phi = 0\), it moves by \(d\) along its heading. This is the default version of the model, DynamicVersion::V1. Because each step is an arc, not a straight line, the result does not depend on the length of the step.
The input is a map with v [m/s], w [rad/s] and errors: two numbers added to the distance and to the turn of each step, to model what the wheels do wrong. set_time_step sets \(\Delta t\) and integrate moves the robot by one step. The second argument of the constructor says whether to keep the Jacobians of the model up to date, which the extended Kalman filter of Extended Kalman filtering needs; here they are not needed.
DiffDriveOdometry in Odometry from the wheel encoders works the other way round: it works out the motion from how far the wheels turned.
Running the example
Output
1. Straight ahead
start: x = 0.000 m, y = 0.000 m, theta = 0.0 deg
after 2 s at 0.5 m/s: x = 1.000 m, y = 0.000 m, theta = 0.0 deg
2. A quarter of a circle to the left
after 2 s at 45 deg/s: x = 1.637 m, y = 0.637 m, theta = 90.0 deg
radius v / w [m] 0.637
a quarter circle from (1, 0) facing x ends at (1.637, 0.637) facing y
Things to try
- Make the step 0.5 s and 1 s. Does the end of the quarter circle move?
- Turn to the right, with a negative \(\omega\), for 8 s. Where does the robot end up, and what is its heading?
- Add an error of 0.01 m to the distance of every step. How far off is the robot after a full circle?
The code
#include <any>
#include <array>
#include <iomanip>
#include <iostream>
#include <map>
#include <string>
{
{
return {{"v", std::any(v)}, {"w", std::any(w)}, {"errors", std::any(std::array<real_t, 2>{})}};
}
{
std::cout << " " << label << ": x = " << std::fixed << std::setprecision(3)
<< " deg\n";
}
{
robot.set_time_step(0.1);
for (int k = 0; k < 20; ++k)
{
robot.integrate(
command(0.5, 0.0));
}
for (int k = 0; k < 20; ++k)
{
}
std::cout << " a quarter circle from (1, 0) facing x ends at (" << std::setprecision(3)
<< 1.0 + radius << ", " << radius << ") facing y\n";
}
}
{
return 0;
}
The basic types of bitsim. Code outside this file names Eigen's matrix and vector types only through ...
DiffDriveDynamics class. Describes the motion of a differential drive robot in the plane: the centre ...
Definition diff_drive_dynamics.h:39
real_t get_y_position() const
Read the y-coordinate.
Definition diff_drive_dynamics.h:137
real_t get_x_position() const
Read the x-coordinate.
Definition diff_drive_dynamics.h:127
real_t get_orientation() const
Read the y-coordinate.
Definition diff_drive_dynamics.h:147
int main()
Definition discounted_returns.cpp:19
const real_t PI
The PI constant.
Definition bitsim_consts.h:63
Models of how things move: wheeled robots, a bicycle, a quadrotor and a fixed wing aircraft,...
Definition bicycle_vehicle_model_dynamics.cpp:10
Definition csv_file_reader.cpp:12
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
real_t clean(real_t x, real_t zero)
x, or 0 when zero
Definition print_utils.cpp:15
void print_title(const std::string &title, std::ostream &out)
An empty line and then the title.
Definition print_utils.cpp:17
double real_t
real_t
Definition bitsim_types.h:20
Definition diff_drive_kinematics.cpp:20
const real_t PI
Definition diff_drive_kinematics.cpp:26
void run()
Definition diff_drive_kinematics.cpp:43
const real_t DEG
Definition diff_drive_kinematics.cpp:27
void print_pose(const std::string &label, const DiffDriveDynamics &robot)
Definition diff_drive_kinematics.cpp:35
std::map< std::string, std::any > command(real_t v, real_t w)
Definition diff_drive_kinematics.cpp:29
Print labelled values, one per line, as the examples do: two spaces, the label padded to a width,...