Autonomy Software C++ 24.5.1
Welcome to the Autonomy Software repository of the Mars Rover Design Team (MRDT) at Missouri University of Science and Technology (Missouri S&T)! API reference contains the source code and other resources for the development of the autonomy software for our Mars rover. The Autonomy Software project aims to compete in the University Rover Challenge (URC) by demonstrating advanced autonomous capabilities and robust navigation algorithms.
Loading...
Searching...
No Matches
duckdb::UnscentedKalmanFilter Class Reference
Collaboration diagram for duckdb::UnscentedKalmanFilter:

Public Member Functions

void Update (double progress, double time)
double GetEstimatedRemainingSeconds () const

Private Member Functions

vector< vector< double > > MatrixSqrt (const vector< vector< double > > &mat)
vector< vector< double > > GenerateSigmaPoints ()
vector< double > StateTransition (const vector< double > &state, double dt)
vector< double > MeasurementFunction (const vector< double > &state)
void Initialize (double initial_progress, double current_time)
void Predict (double current_time)
void UpdateInternal (double measured_progress)
double GetProgress () const
double GetVelocity () const
double GetProgressVariance () const
double GetVelocityVariance () const

Private Attributes

double lambda
vector< double > wm
vector< double > wc
vector< double > x
vector< vector< double > > P
vector< vector< double > > Q
vector< vector< double > > R
double last_time
bool initialized
double last_progress
double scale_factor

Static Private Attributes

static constexpr size_t STATE_DIM = 2
static constexpr size_t OBS_DIM = 1
static constexpr size_t SIGMA_POINTS = 2 * STATE_DIM + 1
static constexpr double ALPHA = 0.044
static constexpr double BETA = 1.0
static constexpr double KAPPA = 0.0

Constructor & Destructor Documentation

◆ UnscentedKalmanFilter()

duckdb::UnscentedKalmanFilter::UnscentedKalmanFilter ( )
93115 : x(STATE_DIM, 0.0), P(STATE_DIM, vector<double>(STATE_DIM, 0.0)), Q(STATE_DIM, vector<double>(STATE_DIM, 0.0)),
93116 R(OBS_DIM, vector<double>(OBS_DIM, 0.0)), last_time(0.0), initialized(false), last_progress(-1.0),
93117 scale_factor(1.0) {
93118 // Calculate UKF parameters
93119 lambda = ALPHA * ALPHA * (STATE_DIM + KAPPA) - STATE_DIM;
93120
93121 // Initialize weights
93122 wm.resize(SIGMA_POINTS);
93123 wc.resize(SIGMA_POINTS);
93124
93125 wm[0] = lambda / (STATE_DIM + lambda);
93126 wc[0] = lambda / (STATE_DIM + lambda) + (1 - ALPHA * ALPHA + BETA);
93127
93128 for (size_t i = 1; i < SIGMA_POINTS; i++) {
93129 wm[i] = wc[i] = 0.5 / (STATE_DIM + lambda);
93130 }
93131
93132 // Initialize covariance matrices
93133 P[0][0] = 0.165; // progress variance
93134 P[1][1] = 0.0098; // velocity variance
93135
93136 Q[0][0] = 0.01; // process noise for progress
93137 Q[1][1] = 0.0; // process noise for velocity
93138
93139 R[0][0] = 0.000077; // measurement noise for progress
93140}

Member Function Documentation

◆ MatrixSqrt()

vector< vector< double > > duckdb::UnscentedKalmanFilter::MatrixSqrt ( const vector< vector< double > > & mat)
private
93179 {
93180 size_t n = mat.size();
93181 vector<vector<double>> L(n, vector<double>(n, 0.0));
93182
93183 // Cholesky decomposition
93184 for (size_t i = 0; i < n; i++) {
93185 for (size_t j = 0; j <= i; j++) {
93186 if (i == j) {
93187 double sum = 0;
93188 for (size_t k = 0; k < j; k++) {
93189 sum += L[j][k] * L[j][k];
93190 }
93191 L[j][j] = std::sqrt(mat[j][j] - sum);
93192 } else {
93193 double sum = 0;
93194 for (size_t k = 0; k < j; k++) {
93195 sum += L[i][k] * L[j][k];
93196 }
93197 L[i][j] = (mat[i][j] - sum) / L[j][j];
93198 }
93199 }
93200 }
93201 return L;
93202}
Scalar sum(InputArray src)

◆ GenerateSigmaPoints()

vector< vector< double > > duckdb::UnscentedKalmanFilter::GenerateSigmaPoints ( )
private
93205 {
93206 vector<vector<double>> sigma_points(SIGMA_POINTS, vector<double>(STATE_DIM));
93207
93208 // Calculate sqrt((n + lambda) * P)
93209 auto scaled_P = P;
93210 double scale = STATE_DIM + lambda;
93211 for (size_t i = 0; i < STATE_DIM; i++) {
93212 for (size_t j = 0; j < STATE_DIM; j++) {
93213 scaled_P[i][j] *= scale;
93214 }
93215 }
93216
93217 auto sqrt_P = MatrixSqrt(scaled_P);
93218
93219 // First sigma point is the mean
93220 sigma_points[0] = x;
93221
93222 // Remaining sigma points
93223 for (size_t i = 0; i < STATE_DIM; i++) {
93224 for (size_t j = 0; j < STATE_DIM; j++) {
93225 sigma_points[i + 1][j] = x[j] + sqrt_P[j][i];
93226 sigma_points[i + 1 + STATE_DIM][j] = x[j] - sqrt_P[j][i];
93227 }
93228 }
93229
93230 return sigma_points;
93231}
void scale(cv::Mat &mat, const cv::Mat &range, const T min, const T max)

◆ StateTransition()

vector< double > duckdb::UnscentedKalmanFilter::StateTransition ( const vector< double > & state,
double dt )
private
93234 {
93235 vector<double> new_state(STATE_DIM);
93236 new_state[0] = state[0] + state[1] * dt; // progress += velocity * dt
93237 new_state[1] = state[1]; // velocity remains constant
93238
93239 // Clamp progress to [0, 1]
93240 new_state[0] = std::max(0.0, std::min(1.0, new_state[0]));
93241
93242 return new_state;
93243}

◆ MeasurementFunction()

vector< double > duckdb::UnscentedKalmanFilter::MeasurementFunction ( const vector< double > & state)
private
93246 {
93247 return {state[0]};
93248}

◆ Update()

void duckdb::UnscentedKalmanFilter::Update ( double progress,
double time )
93142 {
93143 progress *= scale_factor;
93144 if (!initialized) {
93145 Initialize(progress, time);
93146 return;
93147 }
93148 Predict(time);
93149 if (last_progress != progress) {
93150 UpdateInternal(progress);
93151 last_progress = progress;
93152 }
93153}

◆ GetEstimatedRemainingSeconds()

double duckdb::UnscentedKalmanFilter::GetEstimatedRemainingSeconds ( ) const
93376 {
93377 if (!initialized) {
93378 return 2147483647.0;
93379 }
93380 if (x[1] <= 0) {
93381 // velocity is negative or zero - we estimate this will never finish
93382 return NumericLimits<double>::Maximum();
93383 }
93384 double remaining_progress = (1.0 * scale_factor) - x[0];
93385 return std::max(remaining_progress / x[1], 0.0);
93386}

◆ Initialize()

void duckdb::UnscentedKalmanFilter::Initialize ( double initial_progress,
double current_time )
private
93155 {
93156 // If the initial progress is zero we can't yet initalize, since the filter
93157 // wont' have a reasonable guess about query velocity, just wait until some
93158 // progress has been made.
93159 if (initial_progress == 0.0 || current_time == 0.0) {
93160 return;
93161 }
93162 // If the initial progress value is very small, it needs to be scaled up so
93163 // its the same relative magnitude of updates that was used to determine
93164 // the P, Q, and R parameters of the Kalman filter.
93165 //
93166 // The settings for the Kalman filter make assumptions around the magnitude of
93167 // measurement noise. If the progress updates are very small, the updates
93168 // could be considered noise by the Kalman filter.
93169 scale_factor = std::max(1.0, 0.1 / initial_progress);
93170 initial_progress *= scale_factor;
93171 x[0] = initial_progress;
93172 x[1] = initial_progress / current_time; // initial velocity guess
93173 last_time = current_time;
93174 last_progress = initial_progress;
93175 initialized = true;
93176}

◆ Predict()

void duckdb::UnscentedKalmanFilter::Predict ( double current_time)
private
93250 {
93251 D_ASSERT(initialized);
93252
93253 double dt = current_time - last_time;
93254 last_time = current_time;
93255
93256 if (dt <= 0) {
93257 return;
93258 }
93259
93260 // Generate sigma points
93261 auto sigma_points = GenerateSigmaPoints();
93262
93263 // Propagate sigma points through state transition
93264 vector<vector<double>> sigma_points_pred(SIGMA_POINTS, vector<double>(STATE_DIM));
93265 for (size_t i = 0; i < SIGMA_POINTS; i++) {
93266 sigma_points_pred[i] = StateTransition(sigma_points[i], dt);
93267 }
93268
93269 // Predict state mean
93270 std::fill(x.begin(), x.end(), 0.0);
93271 for (size_t i = 0; i < SIGMA_POINTS; i++) {
93272 for (size_t j = 0; j < STATE_DIM; j++) {
93273 x[j] += wm[i] * sigma_points_pred[i][j];
93274 }
93275 }
93276
93277 // Predict covariance
93278 for (size_t i = 0; i < STATE_DIM; i++) {
93279 std::fill(P[i].begin(), P[i].end(), 0.0);
93280 }
93281
93282 for (size_t i = 0; i < SIGMA_POINTS; i++) {
93283 for (size_t j = 0; j < STATE_DIM; j++) {
93284 for (size_t k = 0; k < STATE_DIM; k++) {
93285 P[j][k] += wc[i] * (sigma_points_pred[i][j] - x[j]) * (sigma_points_pred[i][k] - x[k]);
93286 }
93287 }
93288 }
93289
93290 // Add process noise
93291 for (size_t i = 0; i < STATE_DIM; i++) {
93292 for (size_t j = 0; j < STATE_DIM; j++) {
93293 P[i][j] += Q[i][j];
93294 }
93295 }
93296}

◆ UpdateInternal()

void duckdb::UnscentedKalmanFilter::UpdateInternal ( double measured_progress)
private
93298 {
93299 D_ASSERT(initialized);
93300 // Generate sigma points
93301 auto sigma_points = GenerateSigmaPoints();
93302
93303 // Transform sigma points through measurement function
93304 vector<vector<double>> Z_sigma(SIGMA_POINTS, vector<double>(OBS_DIM));
93305 for (size_t i = 0; i < SIGMA_POINTS; i++) {
93306 Z_sigma[i] = MeasurementFunction(sigma_points[i]);
93307 }
93308
93309 // Predict measurement mean
93310 vector<double> z_pred(OBS_DIM, 0.0);
93311 for (size_t i = 0; i < SIGMA_POINTS; i++) {
93312 for (size_t j = 0; j < OBS_DIM; j++) {
93313 z_pred[j] += wm[i] * Z_sigma[i][j];
93314 }
93315 }
93316
93317 // Innovation covariance
93318 vector<vector<double>> S(OBS_DIM, vector<double>(OBS_DIM, 0.0));
93319 for (size_t i = 0; i < SIGMA_POINTS; i++) {
93320 for (size_t j = 0; j < OBS_DIM; j++) {
93321 for (size_t k = 0; k < OBS_DIM; k++) {
93322 S[j][k] += wc[i] * (Z_sigma[i][j] - z_pred[j]) * (Z_sigma[i][k] - z_pred[k]);
93323 }
93324 }
93325 }
93326
93327 // Add measurement noise
93328 for (size_t i = 0; i < OBS_DIM; i++) {
93329 for (size_t j = 0; j < OBS_DIM; j++) {
93330 S[i][j] += R[i][j];
93331 }
93332 }
93333
93334 // Cross correlation
93335 vector<vector<double>> T(STATE_DIM, vector<double>(OBS_DIM, 0.0));
93336 for (size_t i = 0; i < SIGMA_POINTS; i++) {
93337 for (size_t j = 0; j < STATE_DIM; j++) {
93338 for (size_t k = 0; k < OBS_DIM; k++) {
93339 T[j][k] += wc[i] * (sigma_points[i][j] - x[j]) * (Z_sigma[i][k] - z_pred[k]);
93340 }
93341 }
93342 }
93343
93344 // Kalman gain (simplified for 1D measurement)
93345 vector<vector<double>> K(STATE_DIM, vector<double>(OBS_DIM));
93346 double S_inv = 1.0 / S[0][0];
93347 for (size_t i = 0; i < STATE_DIM; i++) {
93348 K[i][0] = T[i][0] * S_inv;
93349 }
93350
93351 // Update state
93352 vector<double> innovation = {measured_progress - z_pred[0]};
93353 for (size_t i = 0; i < STATE_DIM; i++) {
93354 x[i] += K[i][0] * innovation[0];
93355 }
93356
93357 // Update covariance
93358 for (size_t i = 0; i < STATE_DIM; i++) {
93359 for (size_t j = 0; j < STATE_DIM; j++) {
93360 P[i][j] -= K[i][0] * S[0][0] * K[j][0];
93361 }
93362 }
93363
93364 // Ensure progress stays in bounds
93365 x[0] = std::max(0.0, std::min(scale_factor, x[0]));
93366}

◆ GetProgress()

double duckdb::UnscentedKalmanFilter::GetProgress ( ) const
private
93368 {
93369 return x[0] / scale_factor;
93370}

◆ GetVelocity()

double duckdb::UnscentedKalmanFilter::GetVelocity ( ) const
private
93372 {
93373 return x[1];
93374}

◆ GetProgressVariance()

double duckdb::UnscentedKalmanFilter::GetProgressVariance ( ) const
private
93388 {
93389 return P[0][0];
93390}

◆ GetVelocityVariance()

double duckdb::UnscentedKalmanFilter::GetVelocityVariance ( ) const
private
93392 {
93393 return P[1][1];
93394}

The documentation for this class was generated from the following file: