Skip to content

Commit

Permalink
Implement long_callback Python decorator addressing #218
Browse files Browse the repository at this point in the history
Clover package Python library added.
  • Loading branch information
okalachev committed Nov 2, 2022
1 parent ccbd1cb commit ce0b4eb
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 1 deletion.
1 change: 1 addition & 0 deletions builder/test/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from led_msgs.srv import SetLEDs
from led_msgs.msg import LEDStateArray, LEDState
from aruco_pose.msg import Marker, MarkerArray, Point2D
from clover import long_callback

import dynamic_reconfigure.client

Expand Down
2 changes: 1 addition & 1 deletion clover/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ find_package(OpenCV ${_opencv_version} REQUIRED
## Uncomment this if the package has a setup.py. This macro ensures
## modules and global scripts declared therein get installed
## See http://ros.org/doc/api/catkin/html/user_guide/setup_dot_py.html
# catkin_python_setup()
catkin_python_setup()

################################################
## Declare ROS messages, services and actions ##
Expand Down
11 changes: 11 additions & 0 deletions clover/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
## ! DO NOT MANUALLY INVOKE THIS setup.py, USE CATKIN INSTEAD

from distutils.core import setup
from catkin_pkg.python_setup import generate_distutils_setup

# fetch values from package.xml
setup_args = generate_distutils_setup(
packages=['clover'],
package_dir={'': 'src'})

setup(**setup_args)
35 changes: 35 additions & 0 deletions clover/src/clover/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import rospy
from threading import Thread, Event

def long_callback(fn):
"""
Decorator fixing a rospy issue for long-running topic callbacks, primarily
for image processing.
See: https://github.com/ros/ros_comm/issues/1901.
Usage example:
@long_callback
def image_callback(msg):
# perform image processing
# ...
rospy.Subscriber('main_camera/image_raw', Image, image_callback)
"""
e = Event()

def thread():
while not rospy.is_shutdown():
e.wait()
e.clear()
fn(thread.current_msg)

thread.current_msg = None
Thread(target=thread, daemon=True).start()

def wrapper(msg):
thread.current_msg = msg
e.set()

return wrapper
16 changes: 16 additions & 0 deletions clover/test/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import pytest
from mavros_msgs.msg import State
from clover import srv
import time

@pytest.fixture()
def node():
Expand Down Expand Up @@ -60,3 +61,18 @@ def wait_print():

t.join()
assert wait_print.result == 'test'

def test_long_callback():
from clover import long_callback
from time import sleep

# very basic test for long_callback
@long_callback
def cb(i):
cb.counter += i
cb.counter = 0
cb(2)
sleep(0.1)
cb(3)
sleep(1)
assert cb.counter == 5

0 comments on commit ce0b4eb

Please sign in to comment.