where \(Q\) is the covariance of the errors.
The robot drives at 1 m/s for 150 s, in steps of 0.5 s. At 25 s it turns left by 22.5 degrees, a rate of 45 deg/s for one step, and at 50 s and at 75 s right by the same. The example runs the model twice: once on its own, the truth, and once inside the filter, and writes both to state.csv. The measurement here is the filter's own predicted position, with no noise, so the filter follows the truth exactly; add noise to get_measurement to see it filter.
Simulation time: 0
Position: 0.5, 0
Orientation: (degrees)0
V: 1, W: 0
...
Simulation time: 25
Position: 25.4872, 0.0969196
Orientation: (degrees)22.5
V: 1, W: 0.785398
Simulation time: 25.5
Position: 25.9492, 0.288261
Orientation: (degrees)22.5
V: 1, W: 0
...
Simulation time: 149.5
Position: 142.426, -19.0373
Orientation: (degrees)-22.5
V: 1, W: 0
At 25 s the robot drives the 0.5 m of that step along an arc that turns it by 22.5 degrees, and then on along its new heading. The plots compare the truth with the estimate, which lie on top of each other:
#include <any>
#include <iostream>
#include <vector>
{
class ObservationModel
{
public:
private:
};
{
H = DynMat<real_t>::Zero(2, 3);
M = DynMat<real_t>::Zero(2, 2);
H(0, 0) = 1.0;
H(1, 1) = 1.0;
M(0, 0) = 1.0;
M(1, 1) = 1.0;
}
const DynVec<real_t> ObservationModel::evaluate(const DynVec<real_t> &state) const
{
return (H *
state.transpose()).transpose();
}
const DynMat<real_t> &ObservationModel::get_matrix(const std::string &name) const
{
if (name == "H")
{
}
else if (name == "M")
{
return M;
}
throw std::logic_error("Invalid matrix name. Name " + name + " not found");
}
{
DynVec<real_t> result(2);
result[0] =
state.get(
"X");
result[1] =
state.get(
"Y");
return result;
}
}
{
auto time = 0.0;
auto dt = 0.5;
auto w = 0.0;
auto vt = 1.0;
std::array<real_t, 2> motion_control_error;
motion_control_error[0] = 0.0;
motion_control_error[1] = 0.0;
std::map<std::string, std::any> input;
input["v"] = 1.0;
input["w"] = 0.0;
input["errors"] = motion_control_error;
ekf.with_matrix(
"P", P).with_matrix(
"R",
R).with_matrix(
"Q",
Q);
writer.open();
std::vector<std::string> names{"Time", "X_true", "Y_true", "Theta_true", "X", "Y", "Theta"};
writer.write_column_names(names);
try
{
std::map<std::string, std::any> motion_input;
motion_input["v"] = vt;
motion_input["errors"] = motion_control_error;
{
if (counter == 50)
{
}
else if (counter == 100)
{
}
else if (counter == 150)
{
}
else
{
w = 0.0;
}
motion_input["w"] = w;
auto &exact_state = exact_motion_model.
evaluate(motion_input);
ekf.predict(motion_input);
ekf.update(z);
std::vector<real_t> row(7, 0.0);
row[0] = time;
row[1] = exact_state.get("X");
row[2] = exact_state.get("Y");
row[3] = exact_state.get("Theta");
row[6] =
state.get(
"Theta");
writer.write_row(row);
time += dt;
counter++;
}
}
catch (std::runtime_error &e)
{
std::cerr << e.what() << std::endl;
}
catch (std::logic_error &e)
{
std::cerr << e.what() << std::endl;
}
catch (...)
{
std::cerr << "Unknown exception was raised whilst running simulation." << std::endl;
}
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
virtual state_type & evaluate(const input_type &input) override
Evaluate the new state using the given input it also updates the various matrices if needed.
Definition diff_drive_dynamics.cpp:211
void set_time_step(real_t dt)
Set the time step.
Definition motion_model_base.h:138
void set_matrix_update_flag(bool f)
set the matrix update flag
Definition motion_model_base.h:97
SysState utility class describing the state of a system.
Definition system_state.h:25
The extended Kalman filter: it predicts the state with a non-linear motion model and corrects it with...
Definition extended_kalman_filter.h:68
The IterationCounter class.
Definition iteration_counter.h:15
The CSVWriter class. Handles writing into CSV file format.
Definition csv_file_writer.h:22
Definition extended_kalman_filter.cpp:29
const DynVec< real_t > evaluate(const DynVec< real_t > &state) const
Definition extended_kalman_filter.cpp:59
ObservationModel()
Definition extended_kalman_filter.cpp:49
const DynMat< real_t > & get_matrix(const std::string &name) const
Definition extended_kalman_filter.cpp:64
DynVec< real_t > input_type
Definition extended_kalman_filter.cpp:34
int main()
Definition discounted_returns.cpp:19
Logging for bitsim and the libraries built on it, on top of spdlog.
#define BITSIM_LOG_INFO
Definition logger.h:150
@ P
Definition fixed_wing_dynamics.h:37
real_t rad_to_degrees(real_t rad)
Definition unit_converter.h:36
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
double real_t
real_t
Definition bitsim_types.h:20
Eigen::RowVectorX< T > DynVec
Dynamically sized row vector.
Definition bitsim_types.h:101
Definition extended_kalman_filter.py:1
H
Definition extended_kalman_filter.py:42
list[float] motion_model(list[float] x, list[float] u)
Definition extended_kalman_filter.py:73
int R
Definition extended_kalman_filter.py:54
ObservationOutput observation(list[float] xTrue, list[float] xd, list[float] u)
Definition extended_kalman_filter.py:119
const DynVec< real_t > get_measurement(const SysState< 3 > &state)
Definition extended_kalman_filter.cpp:78
int Q
Definition extended_kalman_filter.py:48
state
Definition play.py:39
Vector step(const QuadrotorDynamicsConfig &config, const Vector &x, const Vector &u, real_t max_rotor_speed)
Definition quadrotor_lqr.cpp:114
Various unit conversion utilities.