Skip to content

Commit

Permalink
Compute initial estimate in simulator
Browse files Browse the repository at this point in the history
  • Loading branch information
RainerKuemmerle committed Feb 22, 2025
1 parent 648b134 commit 7bd198d
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 5 deletions.
33 changes: 28 additions & 5 deletions g2o/simulator/simulator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,28 @@

#include "simulator.h"

#include <memory>
#include <unordered_set>

#include "g2o/core/estimate_propagator.h"
#include "g2o/core/optimizable_graph.h"

namespace g2o {

namespace {
class SimulationEstimatePropagatorCost : public EstimatePropagatorCostBase {
public:
double operator()(OptimizableGraph::Edge* edge,
const OptimizableGraph::VertexSet& from,
OptimizableGraph::Vertex* to) const override {
return edge->initialEstimatePossible(from, to);
}
[[nodiscard]] std::string_view name() const override {
return "simulation spanning tree";

Check warning on line 46 in g2o/simulator/simulator.cpp

View check run for this annotation

Codecov / codecov/patch

g2o/simulator/simulator.cpp#L45-L46

Added lines #L45 - L46 were not covered by tests
}
};
} // namespace

void BaseWorldObject::setVertex(
const std::shared_ptr<OptimizableGraph::Vertex>& vertex) {
vertex_ = vertex;
Expand Down Expand Up @@ -96,11 +112,18 @@ void Simulator::finalize() {
}
iter = world_.graph().parameters().erase(iter);
}
// TODO(Rainer): Initial estimate
/* Fails since only implemented on SparseOptimizer
EstimatePropagatorCostOdometry costFunction(&world_.graph());
world_.graph().computeInitialGuess(costFunction);
*/

// Initial estimate
OptimizableGraph::VertexSet fixedVertices;
for (const auto& id_v : world_.graph().vertices()) {
auto v = std::dynamic_pointer_cast<OptimizableGraph::Vertex>(id_v.second);
if (!v || !v->fixed()) continue;
fixedVertices.emplace(std::move(v));
}

SimulationEstimatePropagatorCost propagator;
EstimatePropagator estimatePropagator(&world_.graph());
estimatePropagator.propagate(fixedVertices, propagator);
}

} // namespace g2o
1 change: 1 addition & 0 deletions g2o/simulator/simulator.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ class Robot : public BaseRobot {
pose_ = pose;
auto po = std::make_unique<PoseObject>();
po->vertex()->setEstimate(pose_);
po->vertex()->setFixed(trajectory_.empty());
const int pose_id = world.addWorldObject(std::move(po));
trajectory_.emplace_back(pose_id);
}
Expand Down
2 changes: 2 additions & 0 deletions unit_test/simulator/simulator2D_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ TEST(Simulator2D, Odom) {
const OptimizableGraph& graph = simulator.world().graph();

EXPECT_THAT(graph.vertices(), SizeIs(simulator.config.simSteps + 1));
int count_fixed = g2o::internal::countFixed(graph);
EXPECT_THAT(count_fixed, Eq(1));
const int poses = g2o::internal::countVerticesMatchingType<VertexSE2>(graph);
EXPECT_THAT(poses, Eq(simulator.config.simSteps + 1));
const int odom_cnt = g2o::internal::countEdgesMatchingType<EdgeSE2>(graph);
Expand Down
10 changes: 10 additions & 0 deletions unit_test/test_helper/graph_functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,14 @@ int countEdgesMatchingType(const OptimizableGraph& graph) {
return ptr != nullptr;
});
}

inline int countFixed(const OptimizableGraph& graph) {
return std::count_if(
graph.vertices().begin(), graph.vertices().end(),
[](const OptimizableGraph::VertexIDMap::value_type& elem) {
auto* ptr = dynamic_cast<OptimizableGraph::Vertex*>(elem.second.get());
return ptr != nullptr && ptr->fixed();
});
}

} // namespace g2o::internal

0 comments on commit 7bd198d

Please sign in to comment.