Bayes Filters Library
FilteringAlgorithm.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2016-2019 Istituto Italiano di Tecnologia (IIT)
3  *
4  * This software may be modified and distributed under the terms of the
5  * BSD 3-Clause license. See the accompanying LICENSE file for details.
6  */
7 
9 
10 #include <iostream>
11 
12 using namespace bfl;
13 
14 
16 {
17  try
18  {
20  }
21  catch (const std::system_error& e)
22  {
23  std::cerr << "ERROR::FILTERINGALGORITHM::PREAPRE\n";
24  std::cerr << "ERROR::CODE:\n\t" << e.code() << "\n";
25  std::cerr << "ERROR::LOG:\n\t" << e.what() << std::endl;
26  return false;
27  }
28 
29  return true;
30 }
31 
32 
34 {
35  std::lock_guard<std::mutex> lk(mtx_run_);
36  run_ = true;
37  cv_run_.notify_one();
38 }
39 
40 
42 {
43  if (filtering_thread_.joinable())
44  {
45  try
46  {
47  filtering_thread_.join();
48  }
49  catch (const std::system_error& e)
50  {
51  std::cerr << "ERROR::FILTERINGALGORITHM::WAIT\n";
52  std::cerr << "ERROR::CODE:\n\t" << e.code() << "\n";
53  std::cerr << "ERROR::LOG:\n\t" << e.what() << std::endl;
54  return false;
55  }
56  }
57  else
58  {
59  std::cout << "WARNING::FILTERINGALGORITHM::WAIT\n";
60  std::cout << "WARNING::LOG:\n\tfiltering thread is not joinable. Returning 'true'." << std::endl;
61  }
62 
63  return true;
64 }
65 
66 
68 {
69  reset_ = true;
70 }
71 
72 
74 {
75  std::lock_guard<std::mutex> lk(mtx_run_);
76  reset_ = true;
77  run_ = false;
78  cv_run_.notify_one();
79 }
80 
81 
83 {
84  teardown_ = true;
85 
86  return true;
87 }
88 
89 
91 {
92  return filtering_step_;
93 }
94 
95 
97 {
98  return run_;
99 }
100 
101 
103 {
104  do
105  {
106  reset_ = false;
107  filtering_step_ = 0;
108 
109  std::unique_lock<std::mutex> lk(mtx_run_);
110  cv_run_.wait(lk, [this]{ return (this->run_ || this->teardown_); });
111  try
112  {
113  lk.unlock();
114  }
115  catch (const std::system_error& e)
116  {
117  std::cerr << "ERROR::FILTERINGALGORITHM::FILTERINGRECURSION\n";
118  std::cerr << "ERROR::CODE:\n\t" << e.code() << "\n";
119  std::cerr << "ERROR::LOG:\n\t" << e.what() << std::endl;
120  teardown_ = true;
121  }
122 
124 
125  while (run_condition() && !teardown_ && !reset_)
126  {
127  filtering_step();
128 
129  ++filtering_step_;
130  }
131  }
132  while (run_condition() && (run_ || reset_) && !teardown_);
133 
134  run_ = false;
135 }
bfl::FilteringAlgorithm::is_running
bool is_running() override
Definition: FilteringAlgorithm.cpp:96
bfl::FilteringAlgorithm::wait
bool wait() override
Definition: FilteringAlgorithm.cpp:41
bfl::FilteringAlgorithm::cv_run_
std::condition_variable cv_run_
Definition: FilteringAlgorithm.h:75
bfl::FilteringAlgorithm::step_number
unsigned int step_number() override
Definition: FilteringAlgorithm.cpp:90
bfl::FilteringAlgorithm::filtering_recursion
void filtering_recursion()
Definition: FilteringAlgorithm.cpp:102
bfl::FilteringAlgorithm::filtering_thread_
std::thread filtering_thread_
Definition: FilteringAlgorithm.h:71
bfl::FilteringAlgorithm::mtx_run_
std::mutex mtx_run_
Definition: FilteringAlgorithm.h:73
bfl
Port of boost::any for C++11 compilers.
Definition: AdditiveMeasurementModel.h:13
bfl::FilteringAlgorithm::run
void run() override
Definition: FilteringAlgorithm.cpp:33
bfl::FilteringAlgorithm::filtering_step_
unsigned int filtering_step_
Definition: FilteringAlgorithm.h:69
bfl::FilteringAlgorithm::run_condition
virtual bool run_condition()=0
bfl::FilteringAlgorithm::teardown
bool teardown() override
Definition: FilteringAlgorithm.cpp:82
bfl::FilteringAlgorithm::reset
void reset() override
Definition: FilteringAlgorithm.cpp:67
FilteringAlgorithm.h
bfl::FilteringAlgorithm::teardown_
bool teardown_
Definition: FilteringAlgorithm.h:81
bfl::FilteringAlgorithm::filtering_step
virtual void filtering_step()=0
bfl::FilteringAlgorithm::boot
bool boot() override
Definition: FilteringAlgorithm.cpp:15
bfl::FilteringAlgorithm::reset_
bool reset_
Definition: FilteringAlgorithm.h:79
bfl::FilteringAlgorithm::reboot
void reboot() override
Definition: FilteringAlgorithm.cpp:73
bfl::FilteringAlgorithm::run_
bool run_
Definition: FilteringAlgorithm.h:77
bfl::FilteringAlgorithm::initialization_step
virtual bool initialization_step()=0