fixed_lag_smoother.h
Go to the documentation of this file.
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2019, Locus Robotics
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of the copyright holder nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  */
34 #ifndef FUSE_OPTIMIZERS_FIXED_LAG_SMOOTHER_H
35 #define FUSE_OPTIMIZERS_FIXED_LAG_SMOOTHER_H
36 
37 #include <fuse_core/graph.h>
38 #include <fuse_core/transaction.h>
42 #include <ros/ros.h>
43 #include <std_srvs/Empty.h>
44 
45 #include <atomic>
46 #include <condition_variable>
47 #include <functional>
48 #include <mutex>
49 #include <string>
50 #include <thread>
51 #include <vector>
52 
53 
54 namespace fuse_optimizers
55 {
56 
106 {
107 public:
110 
120  fuse_core::Graph::UniquePtr graph,
121  const ros::NodeHandle& node_handle = ros::NodeHandle(),
122  const ros::NodeHandle& private_node_handle = ros::NodeHandle("~"));
123 
127  virtual ~FixedLagSmoother();
128 
129 protected:
134  {
135  std::string sensor_name;
136  fuse_core::Transaction::SharedPtr transaction;
137 
138  const ros::Time& stamp() const { return transaction->stamp(); }
139  const ros::Time& minStamp() const { return transaction->minStamp(); }
140  const ros::Time& maxStamp() const { return transaction->maxStamp(); }
141  };
142 
153  using TransactionQueue = std::vector<TransactionQueueElement>;
154 
155  // Read-only after construction
156  std::thread optimization_thread_;
158 
159  // Inherently thread-safe
160  std::atomic<bool> ignited_;
161  std::atomic<bool> optimization_running_;
163  std::atomic<bool> started_;
164 
165  // Guarded by pending_transactions_mutex_
168 
171  // Guarded by optimization_mutex_
172  std::mutex optimization_mutex_;
173  // fuse_core::Graph* graph_ member from the base class
177  ceres::Solver::Summary summary_;
178 
179  // Guarded by optimization_requested_mutex_
183  std::condition_variable optimization_requested_;
184 
186  // Guarded by start_time_mutex_
187  mutable std::mutex start_time_mutex_;
189 
190  // Ordering ROS objects with callbacks last
196 
200  void autostart();
201 
207  void publishStatus(const bool running);
208 
219  void preprocessMarginalization(const fuse_core::Transaction& new_transaction);
220 
225 
235  std::vector<fuse_core::UUID> computeVariablesToMarginalize(const ros::Time& lag_expiration);
236 
246  void postprocessMarginalization(const fuse_core::Transaction& marginal_transaction);
247 
253  void optimizationLoop();
254 
263  void optimizerTimerCallback(const ros::TimerEvent& event);
264 
276  void processQueue(fuse_core::Transaction& transaction, const ros::Time& lag_expiration);
277 
281  bool resetServiceCallback(std_srvs::Empty::Request&, std_srvs::Empty::Response&);
282 
287  bool stopServiceCallback(std_srvs::Empty::Request&, std_srvs::Empty::Response&);
288 
292  bool startServiceCallback(std_srvs::Empty::Request&, std_srvs::Empty::Response&);
293 
297  void start();
298 
302  void stop();
303 
308  {
309  std::lock_guard<std::mutex> lock(start_time_mutex_);
310  return start_time_;
311  }
312 
316  void setStartTime(const ros::Time& start_time)
317  {
318  std::lock_guard<std::mutex> lock(start_time_mutex_);
319  start_time_ = start_time;
320  }
321 
333  void transactionCallback(
334  const std::string& sensor_name,
335  fuse_core::Transaction::SharedPtr transaction) override;
336 
342 };
343 
344 } // namespace fuse_optimizers
345 
346 #endif // FUSE_OPTIMIZERS_FIXED_LAG_SMOOTHER_H
fuse_optimizers::FixedLagSmoother::FixedLagSmoother
FixedLagSmoother(fuse_core::Graph::UniquePtr graph, const ros::NodeHandle &node_handle=ros::NodeHandle(), const ros::NodeHandle &private_node_handle=ros::NodeHandle("~"))
Constructor.
Definition: fixed_lag_smoother.cpp:82
graph.h
fuse_optimizers::FixedLagSmoother::start_time_mutex_
std::mutex start_time_mutex_
Synchronize modification to the start_time_ variable.
Definition: fixed_lag_smoother.h:187
variable_stamp_index.h
fuse_optimizers::FixedLagSmoother::setStartTime
void setStartTime(const ros::Time &start_time)
Thread-safe write access to the optimizer start time.
Definition: fixed_lag_smoother.h:316
ros::Publisher
fuse_optimizers::FixedLagSmoother::optimization_thread_
std::thread optimization_thread_
Thread used to run the optimizer as a background process.
Definition: fixed_lag_smoother.h:156
fuse_optimizers::FixedLagSmoother::transactionCallback
void transactionCallback(const std::string &sensor_name, fuse_core::Transaction::SharedPtr transaction) override
Callback fired every time the SensorModel plugin creates a new transaction.
Definition: fixed_lag_smoother.cpp:534
fuse_optimizers::FixedLagSmoother::optimizerTimerCallback
void optimizerTimerCallback(const ros::TimerEvent &event)
Callback fired at a fixed frequency to trigger a new optimization cycle.
Definition: fixed_lag_smoother.cpp:299
fuse_optimizers::FixedLagSmoother::FUSE_SMART_PTR_DEFINITIONS
FUSE_SMART_PTR_DEFINITIONS(FixedLagSmoother)
fuse_optimizers::FixedLagSmoother::pending_transactions_
TransactionQueue pending_transactions_
Definition: fixed_lag_smoother.h:167
fuse_optimizers::FixedLagSmoother::TransactionQueueElement::stamp
const ros::Time & stamp() const
Definition: fixed_lag_smoother.h:138
fuse_optimizers::FixedLagSmoother::TransactionQueueElement::minStamp
const ros::Time & minStamp() const
Definition: fixed_lag_smoother.h:139
ros.h
fuse_optimizers::FixedLagSmoother::start_time_
ros::Time start_time_
The timestamp of the first ignition sensor transaction.
Definition: fixed_lag_smoother.h:188
fuse_optimizers::FixedLagSmoother::publishStatus
void publishStatus(const bool running)
Publish the optimizer status message.
Definition: fixed_lag_smoother.cpp:155
fuse_optimizers::FixedLagSmoother::summary_
ceres::Solver::Summary summary_
Optimization summary, written by optimizationLoop and read by setDiagnostics.
Definition: fixed_lag_smoother.h:177
fuse_optimizers::FixedLagSmoother::autostart
void autostart()
Automatically start the smoother if no ignition sensors are specified.
Definition: fixed_lag_smoother.cpp:143
fuse_optimizers::FixedLagSmoother::optimization_requested_
std::condition_variable optimization_requested_
Definition: fixed_lag_smoother.h:183
fuse_optimizers::FixedLagSmoother::TransactionQueueElement::maxStamp
const ros::Time & maxStamp() const
Definition: fixed_lag_smoother.h:140
fuse_optimizers::FixedLagSmoother::TransactionQueueElement::sensor_name
std::string sensor_name
Definition: fixed_lag_smoother.h:135
ros::ServiceServer
fuse_optimizers::FixedLagSmoother::getStartTime
ros::Time getStartTime() const
Thread-safe read-only access to the optimizer start time.
Definition: fixed_lag_smoother.h:307
fuse_optimizers::FixedLagSmoother::TransactionQueueElement::transaction
fuse_core::Transaction::SharedPtr transaction
Definition: fixed_lag_smoother.h:136
fuse_optimizers::FixedLagSmoother::lag_expiration_
ros::Time lag_expiration_
The oldest stamp that is inside the fixed-lag smoother window.
Definition: fixed_lag_smoother.h:174
fuse_optimizers::FixedLagSmoother::optimizationLoop
void optimizationLoop()
Function that optimizes all constraints, designed to be run in a separate thread.
Definition: fixed_lag_smoother.cpp:188
fuse_optimizers::FixedLagSmoother::setDiagnostics
void setDiagnostics(diagnostic_updater::DiagnosticStatusWrapper &status) override
Update and publish diagnotics.
Definition: fixed_lag_smoother.cpp:652
fuse_core::Transaction
fuse_optimizers::FixedLagSmoother::preprocessMarginalization
void preprocessMarginalization(const fuse_core::Transaction &new_transaction)
Perform any required preprocessing steps before computeVariablesToMarginalize() is called.
Definition: fixed_lag_smoother.cpp:162
fuse_optimizers::FixedLagSmoother::params_
ParameterType params_
Configuration settings for this fixed-lag smoother.
Definition: fixed_lag_smoother.h:157
fuse_optimizers::FixedLagSmoother
A fixed-lag smoother implementation that marginalizes out variables that are older than a defined lag...
Definition: fixed_lag_smoother.h:105
fuse_optimizers::FixedLagSmoother::ignited_
std::atomic< bool > ignited_
Definition: fixed_lag_smoother.h:160
fuse_optimizers::FixedLagSmoother::optimization_mutex_
std::mutex optimization_mutex_
Mutex held while the graph is begin optimized.
Definition: fixed_lag_smoother.h:172
fuse_optimizers::FixedLagSmoother::TransactionQueueElement
Definition: fixed_lag_smoother.h:133
fuse_optimizers::FixedLagSmoother::timestamp_tracking_
VariableStampIndex timestamp_tracking_
Object that tracks the timestamp associated with each variable.
Definition: fixed_lag_smoother.h:176
fuse_optimizers::FixedLagSmoother::optimize_timer_
ros::Timer optimize_timer_
Trigger an optimization operation at a fixed frequency.
Definition: fixed_lag_smoother.h:191
fuse_optimizers::FixedLagSmoother::started_
std::atomic< bool > started_
Flag indicating the optimizer has received a transaction from an ignition sensor.
Definition: fixed_lag_smoother.h:163
fuse_optimizers::FixedLagSmoother::startServiceCallback
bool startServiceCallback(std_srvs::Empty::Request &, std_srvs::Empty::Response &)
Service callback that starts the optimizer plugins after they have been stopped.
Definition: fixed_lag_smoother.cpp:474
fuse_optimizers::FixedLagSmoother::marginal_transaction_
fuse_core::Transaction marginal_transaction_
The marginals to add during the next optimization cycle.
Definition: fixed_lag_smoother.h:175
fuse_optimizers::FixedLagSmoother::stop_service_server_
ros::ServiceServer stop_service_server_
Service that stops and clears the optimizer.
Definition: fixed_lag_smoother.h:193
fuse_optimizers::FixedLagSmoother::computeVariablesToMarginalize
std::vector< fuse_core::UUID > computeVariablesToMarginalize(const ros::Time &lag_expiration)
Compute the set of variables that should be marginalized from the graph.
Definition: fixed_lag_smoother.cpp:176
ros::TimerEvent
fuse_optimizers::FixedLagSmoother::status_publisher_
ros::Publisher status_publisher_
Publishing the started/stopped status of the optimizer.
Definition: fixed_lag_smoother.h:195
fuse_optimizers::FixedLagSmoother::~FixedLagSmoother
virtual ~FixedLagSmoother()
Destructor.
Definition: fixed_lag_smoother.cpp:131
ros::Time
fuse_optimizers::FixedLagSmoother::optimization_request_
bool optimization_request_
Flag to trigger a new optimization.
Definition: fixed_lag_smoother.h:182
fuse_optimizers::FixedLagSmoother::reset_service_server_
ros::ServiceServer reset_service_server_
Service that resets the optimizer to its initial state.
Definition: fixed_lag_smoother.h:192
optimizer.h
fuse_optimizers::VariableStampIndex
Object designed to track the timestamps associated with each variable.
Definition: variable_stamp_index.h:56
fuse_optimizers::FixedLagSmootherParams
Defines the set of parameters required by the fuse_optimizers::FixedLagSmoother class.
Definition: fixed_lag_smoother_params.h:55
diagnostic_updater::DiagnosticStatusWrapper
fixed_lag_smoother_params.h
fuse_optimizers::FixedLagSmoother::computeLagExpirationTime
ros::Time computeLagExpirationTime() const
Compute the oldest timestamp that is part of the configured lag window.
Definition: fixed_lag_smoother.cpp:167
fuse_optimizers::FixedLagSmoother::start
void start()
Start the optimizer.
Definition: fixed_lag_smoother.cpp:480
fuse_optimizers::FixedLagSmoother::start_service_server_
ros::ServiceServer start_service_server_
Service that restarts the optimizer.
Definition: fixed_lag_smoother.h:194
fuse_optimizers::FixedLagSmoother::stop
void stop()
Stop the optimizer.
Definition: fixed_lag_smoother.cpp:499
fuse_optimizers::FixedLagSmoother::TransactionQueue
std::vector< TransactionQueueElement > TransactionQueue
Queue of Transaction objects, sorted by timestamp.
Definition: fixed_lag_smoother.h:153
fuse_optimizers::FixedLagSmoother::pending_transactions_mutex_
std::mutex pending_transactions_mutex_
Synchronize modification of the pending_transactions_ container.
Definition: fixed_lag_smoother.h:166
fuse_optimizers::FixedLagSmoother::postprocessMarginalization
void postprocessMarginalization(const fuse_core::Transaction &marginal_transaction)
Perform any required post-marginalization bookkeeping.
Definition: fixed_lag_smoother.cpp:183
fuse_optimizers::FixedLagSmoother::stopServiceCallback
bool stopServiceCallback(std_srvs::Empty::Request &, std_srvs::Empty::Response &)
Service callback that stops the optimizer plugins and clears the existing graph. Essentially performs...
Definition: fixed_lag_smoother.cpp:468
fuse_optimizers::FixedLagSmoother::optimization_deadline_
ros::Time optimization_deadline_
The deadline for the optimization to complete. Triggers a warning if exceeded.
Definition: fixed_lag_smoother.h:181
ros::Timer
fuse_optimizers::Optimizer
A base class that can be used to build fuse optimizer nodes.
Definition: optimizer.h:93
transaction.h
fuse_optimizers::FixedLagSmoother::optimization_requested_mutex_
std::mutex optimization_requested_mutex_
Required condition variable mutex.
Definition: fixed_lag_smoother.h:180
fuse_optimizers::FixedLagSmoother::optimization_running_
std::atomic< bool > optimization_running_
Flag indicating the optimization thread should be running.
Definition: fixed_lag_smoother.h:162
fuse_optimizers::FixedLagSmoother::processQueue
void processQueue(fuse_core::Transaction &transaction, const ros::Time &lag_expiration)
Generate motion model constraints for pending transactions and combine them into a single transaction...
Definition: fixed_lag_smoother.cpp:325
ros::NodeHandle
fuse_optimizers::FixedLagSmoother::resetServiceCallback
bool resetServiceCallback(std_srvs::Empty::Request &, std_srvs::Empty::Response &)
Service callback that resets the optimizer to its original state.
Definition: fixed_lag_smoother.cpp:460
fuse_optimizers
Definition: batch_optimizer.h:56


fuse_optimizers
Author(s): Stephen Williams , Tom Moore
autogenerated on Thu Apr 24 2025 02:19:11