Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Health_utils

Overview

This package provides utilities for health monitoring operations. Currently, it includes a class for calculating frequency of a topic

All API functions are provided as C++ library and Python bindings.

Dependency from other ROS packages

To include this package in your ROS package, add the following line to your package.xml file

<depend>health_utils</depend>

C++ API

In the CMakeLists.txt file of a ROS package, add the following lines

find_package(health_utils REQUIRED)
target_link_libraries(<your_target> health_utils)

To use the C++ API, include the following header file

#include <health_utils/health_utils.hpp>

For all the available functions, please refer to the header files in the include/health_utils directory.

Python API

All C++ API functions are also provided as Python bindings.

from health_utils import HealthUtils

Class Usage

The HealthUtils class provides utilities for monitoring and calculating event frequencies using a circular buffer.

Constructor

HealthUtils(size_t buffer_size, double timeout_seconds)
  • Initializes with buffer size and timeout.

Public Methods

  • setBufferSize(size_t new_size): Changes buffer size. Throws std::invalid_argument if size is invalid.
    Default is 10 and maximum is 100
  • incrementCounter(): Adds current timestamp to buffer. You should call this function every time you receive new data on a topic.
  • calculateFrequency(): Returns event frequency. Returns 0 if insufficient data or buffer is stale.
    Calulates the frequency based on number of data points/buffer size. It also constantly checks the new data with respect to the timeout, if the data is too old, it throws away that data.
  • setTimeout(double timeout_seconds): Sets new timeout. Default is 5.0 seconds

Thread Safety

All methods are thread-safe.

Usage Example

C++

HealthUtils health(10, 5.0);
health.incrementCounter();
double freq = health.calculateFrequency();
health.setBufferSize(20);
health.setTimeout(10.0);

Python

from health_utils import HealthUtils
import time

# Initialize HealthUtils with a buffer size of 10 and a timeout of 5.0 seconds
health = HealthUtils(10, 5.0)

# Simulate receiving new data and incrementing the counter
for i in range(15):
    health.incrementCounter()
    time.sleep(0.5)  # Simulate delay between data points

# Calculate and print the frequency
freq = health.calculateFrequency()
print(f"Calculated Frequency: {freq} Hz")

# Change the buffer size to 20
health.setBufferSize(20)

# Change the timeout to 10.0 seconds
health.setTimeout(10.0)

# Simulate receiving new data and incrementing the counter again
for i in range(25):
    health.incrementCounter()
    time.sleep(0.5)  # Simulate delay between data points

# Calculate and print the frequency again
freq = health.calculateFrequency()
print(f"Calculated Frequency after buffer size and timeout change: {freq} Hz")

ROS2 Usage examples are present in example folder.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages