GCC Code Coverage Report


Directory: ./
File: src/frequency_manager.cpp
Date: 2022-06-29 13:58:11
Exec Total Coverage
Lines: 16 27 59.3%
Branches: 7 12 58.3%

Line Branch Exec Source
1 #include "real_time_tools/frequency_manager.hpp"
2
3 #include <iostream>
4
5 namespace real_time_tools
6 {
7 1 FrequencyManager::FrequencyManager(double frequency)
8 1 : period_ms_((1.0 / frequency) * 1000.0), previous_time_ms_(-1)
9 {
10 1 }
11
12 FrequencyManager::FrequencyManager()
13 : period_ms_(0.0), previous_time_ms_(-1)
14 {
15 }
16
17 void FrequencyManager::set_frequency(double frequency)
18 {
19 period_ms_ = (1.0 / frequency) * 1000.0;
20 }
21
22 void FrequencyManager::set_period(double period)
23 {
24 period_ms_ = period * 1000.0;
25 }
26
27 double FrequencyManager::predict_sleeping_time() const
28 {
29 double t = Timer::get_current_time_ms();
30 if (previous_time_ms_ < 0)
31 {
32 return 0.0;
33 }
34 return (t - previous_time_ms_);
35 }
36
37 800 bool FrequencyManager::wait()
38 {
39 800 double t = Timer::get_current_time_ms();
40
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 799 times.
800 if (previous_time_ms_ < 0)
41 {
42 1 previous_time_ms_ = t;
43 1 return true;
44 }
45 799 double delta = t - previous_time_ms_;
46
2/2
✓ Branch 0 taken 400 times.
✓ Branch 1 taken 399 times.
799 if (delta > period_ms_)
47 {
48 400 previous_time_ms_ = t;
49 400 return false;
50 }
51
1/2
✓ Branch 1 taken 399 times.
✗ Branch 2 not taken.
399 Timer::sleep_ms(period_ms_ - delta);
52 399 previous_time_ms_ = Timer::get_current_time_ms();
53 399 return true;
54 }
55
2/4
✓ Branch 1 taken 14 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 14 times.
✗ Branch 4 not taken.
42 } // namespace real_time_tools
56