Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added time limit to ICPDriver #40

Merged
merged 2 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ jobs:
steps:
- name: Checkout
uses: actions/checkout@v4
- name: apt update
run: sudo apt-get update
- name: Install Eigen
run: sudo apt-get install libeigen3-dev
- name: Install SDL2
Expand Down
18 changes: 16 additions & 2 deletions include/icp/driver.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <optional>
#include <chrono>
#include "icp.h"

namespace icp {
Expand Down Expand Up @@ -30,7 +31,8 @@ namespace icp {
ICPDriver(std::unique_ptr<ICP> icp);

/**
* @brief Runs ICP to convergence based on the termination conditions set.
* @brief Runs ICP to convergence based on the termination conditions set. If no conditions
* are set, ICP will run indefinitely. This is rarely desirable.
*
* @param a The source point cloud.
* @param b The destination point cloud.
Expand All @@ -41,7 +43,8 @@ namespace icp {
RBTransform t);

/**
* @brief Sets the minimum number of iterations to run.
* @brief Sets the minimum number of iterations to run. This number of iterations will
* always be performed.
*
* @param min_iterations The minimum number of iterations to run.
*/
Expand Down Expand Up @@ -93,6 +96,14 @@ namespace icp {
*/
void set_transform_tolerance(double angle_tolerance, double translation_tolerance);

/**
* @brief Set the time limit for `converge`. Note that `coverge` may take slightly longer
* than this time (up to 1 iteration time) to return.
*
* @param time_limit The time limit.
*/
void set_time_limit(std::chrono::duration<double> time_limit);

private:
bool should_terminate(ConvergenceState current_state,
std::optional<ConvergenceState> last_state);
Expand All @@ -106,5 +117,8 @@ namespace icp {
std::optional<double> absolute_cost_tolerance_;
std::optional<double> angle_tolerance_rad_;
std::optional<double> translation_tolerance_;
std::optional<std::chrono::duration<double>> time_limit_;

std::chrono::time_point<std::chrono::steady_clock> start_time_;
};
}
20 changes: 16 additions & 4 deletions lib/icp/driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace icp {

ICPDriver::ConvergenceState ICPDriver::converge(const std::vector<Vector>& a,
const std::vector<Vector>& b, RBTransform t) {
start_time_ = std::chrono::steady_clock::now();
icp_->begin(a, b, t);

ConvergenceState state{};
Expand All @@ -31,10 +32,6 @@ namespace icp {
bool ICPDriver::should_terminate(ConvergenceState current_state,
std::optional<ConvergenceState> last_state) {
// absolute conditions based only on current state
if (stop_cost_ && current_state.cost < stop_cost_.value()) {
return true;
}

if (min_iterations_ && current_state.iteration_count < min_iterations_.value()) {
return false;
}
Expand All @@ -43,6 +40,17 @@ namespace icp {
return true;
}

if (stop_cost_ && current_state.cost < stop_cost_.value()) {
return true;
}

if (time_limit_) {
auto current_time = std::chrono::steady_clock::now();
if (current_time - start_time_ > time_limit_.value()) {
return true;
}
}

// end if we don't have a last state
if (!last_state) {
return false;
Expand Down Expand Up @@ -103,4 +111,8 @@ namespace icp {
angle_tolerance_rad_ = angle_tolerance;
translation_tolerance_ = translation_tolerance;
}

void ICPDriver::set_time_limit(std::chrono::duration<double> time_limit) {
time_limit_ = time_limit;
}
}
1 change: 1 addition & 0 deletions tests/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ extern "C" {

#include "icp/icp.h"
#include "icp/driver.h"
#include <chrono>

#define BURN_IN 0
#define TRANS_EPS 0.5
Expand Down
Loading