Skip to content

Commit

Permalink
Support option to limit publish frequency
Browse files Browse the repository at this point in the history
  • Loading branch information
TomaszTB authored and ihmc-rosie committed Dec 26, 2024
1 parent d825f4d commit 5a5a0aa
Showing 1 changed file with 17 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package us.ihmc.perception;

import us.ihmc.commons.Conversions;
import us.ihmc.commons.thread.RepeatingTaskThread;
import us.ihmc.commons.thread.Throttler;
import us.ihmc.communication.packets.Packet;
import us.ihmc.ros2.ROS2Node;
import us.ihmc.ros2.ROS2Topic;
Expand All @@ -13,6 +15,9 @@ public class ImageSensorPublishThread extends RepeatingTaskThread
{
private final Map<Integer, ROS2Topic<? extends Packet<?>>> imageKeyToTopicMap;
private final RawImagePublisher publisher;
private final Throttler publishThrottler;
private double publishPeriodLimit = -1.0;

private final ImageSensor imageSensor;

public ImageSensorPublishThread(ROS2Node ros2Node, ImageSensor sensorToPublish, Map<Integer, ROS2Topic<? extends Packet<?>>> imageKeyToTopicMap)
Expand All @@ -22,6 +27,14 @@ public ImageSensorPublishThread(ROS2Node ros2Node, ImageSensor sensorToPublish,
imageSensor = sensorToPublish;
this.imageKeyToTopicMap = imageKeyToTopicMap;
publisher = new RawImagePublisher(ros2Node);
publishThrottler = new Throttler();
}

@Override
public ImageSensorPublishThread setFrequencyLimit(double publishFrequencyLimit)
{
publishPeriodLimit = Conversions.hertzToSeconds(publishFrequencyLimit);
return this;
}

@Override
Expand All @@ -31,6 +44,10 @@ protected void runTask()
{ // Wait for images to be grabbed
imageSensor.waitForGrab();

// Check if we should publish
if (publishPeriodLimit > 0.0 && !publishThrottler.run(publishPeriodLimit))
return;

for (Entry<Integer, ROS2Topic<? extends Packet<?>>> imageEntry : imageKeyToTopicMap.entrySet())
{
int imageKey = imageEntry.getKey();
Expand Down

0 comments on commit 5a5a0aa

Please sign in to comment.