Skip to content

Commit 002d6d1

Browse files
Add reset message callback for resets called within other callbacks
1 parent fce9dbd commit 002d6d1

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

fuse_optimizers/include/fuse_optimizers/windowed_optimizer.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
#include <fuse_optimizers/windowed_optimizer_params.h>
4141
#include <ros/ros.h>
4242
#include <std_srvs/Empty.h>
43+
#include <std_msgs/Empty.h>
4344

4445
#include <atomic>
4546
#include <condition_variable>
@@ -184,6 +185,7 @@ class WindowedOptimizer : public Optimizer
184185
// Ordering ROS objects with callbacks last
185186
ros::Timer optimize_timer_; //!< Trigger an optimization operation at a fixed frequency
186187
ros::ServiceServer reset_service_server_; //!< Service that resets the optimizer to its initial state
188+
ros::Subscriber reset_subscriber_; //!< Subscriber that resets the optimizer to its initial state
187189

188190
/**
189191
* @brief Perform any required preprocessing steps before \p computeVariablesToMarginalize() is called
@@ -281,6 +283,11 @@ class WindowedOptimizer : public Optimizer
281283
*/
282284
void processQueue(fuse_core::Transaction& transaction);
283285

286+
/**
287+
* @brief Message callback that resets the optimizer to its original state
288+
*/
289+
void resetMessageCallback(const std_msgs::Empty::ConstPtr&);
290+
284291
/**
285292
* @brief Service callback that resets the optimizer to its original state
286293
*/

fuse_optimizers/src/windowed_optimizer.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,12 @@ WindowedOptimizer::WindowedOptimizer(
103103
ros::names::resolve(params_->reset_service),
104104
&WindowedOptimizer::resetServiceCallback,
105105
this);
106+
107+
// Subscribe to a reset message that will reset the optimizer to the initial state
108+
reset_subscriber_ = node_handle_.subscribe(
109+
ros::names::resolve(params_->reset_service), 10,
110+
&WindowedOptimizer::resetMessageCallback,
111+
this);
106112
}
107113

108114
WindowedOptimizer::~WindowedOptimizer()
@@ -416,6 +422,40 @@ void WindowedOptimizer::processQueue(fuse_core::Transaction& transaction)
416422
}
417423
}
418424

425+
void WindowedOptimizer::resetMessageCallback(const std_msgs::Empty::ConstPtr&){
426+
// Tell all the plugins to stop
427+
stopPlugins();
428+
// Reset the optimizer state
429+
{
430+
std::lock_guard<std::mutex> lock(optimization_requested_mutex_);
431+
optimization_request_ = false;
432+
}
433+
started_ = false;
434+
ignited_ = false;
435+
setStartTime(ros::Time(0, 0));
436+
// DANGER: The optimizationLoop() function obtains the lock optimization_mutex_ lock and the
437+
// pending_transactions_mutex_ lock at the same time. We perform a parallel locking scheme here to
438+
// prevent the possibility of deadlocks.
439+
{
440+
std::lock_guard<std::mutex> lock(optimization_mutex_);
441+
// Clear all pending transactions
442+
{
443+
std::lock_guard<std::mutex> lock(pending_transactions_mutex_);
444+
pending_transactions_.clear();
445+
}
446+
// Clear the graph and marginal tracking states
447+
graph_->clear();
448+
marginal_transaction_ = fuse_core::Transaction();
449+
}
450+
// Perform any required reset operations for derived classes
451+
onReset();
452+
// Tell all the plugins to start
453+
startPlugins();
454+
// Test for auto-start
455+
autostart();
456+
return;
457+
}
458+
419459
bool WindowedOptimizer::resetServiceCallback(std_srvs::Empty::Request&, std_srvs::Empty::Response&)
420460
{
421461
// Tell all the plugins to stop

0 commit comments

Comments
 (0)