Go to the documentation of this file.
15 #include <boost/bind.hpp>
16 #include <boost/bind/placeholders.hpp>
25 #include <topic_tools/shape_shifter.h>
41 std::string inTopic =
"input";
42 std::string outTopic =
"output";
49 throw std::runtime_error(
"Not enough arguments.\nUsage: repeat IN_TOPIC RATE [OUT_TOPIC].");
53 outTopic = (this->
getMyArgv().size() >= 3 ? this->
getMyArgv()[2] : (inTopic +
"_repeat"));
58 catch (
const std::invalid_argument& e)
60 CRAS_WARN(
"Could not parse the given repeat rate: %s", e.what());
65 const auto inQueueSize =
params->getParam(
"in_queue_size", 10_sz,
"messages");
66 const auto outQueueSize =
params->getParam(
"out_queue_size", inQueueSize,
"messages");
67 const auto lazy =
params->getParam(
"lazy",
false);
68 const auto tcpNoDelay =
params->getParam(
"tcp_no_delay",
false);
70 const auto rate =
params->getParam(
"rate", defaultRate,
"Hz");
71 this->
rate = std::make_unique<ros::Rate>(
rate);
72 if (
params->hasParam(
"max_age"))
74 if (
params->hasParam(
"max_repeats"))
82 this->
pubSub = std::make_unique<cras::GenericLazyPubSub>(
89 this->
pubSub->setLazy(
false);
96 CRAS_INFO(
"Created%s repeater from %s to %s at rate %s Hz.",
97 (lazy ?
" lazy" :
""), nh.resolveName(inTopic).c_str(), nh.resolveName(outTopic).c_str(),
112 std::lock_guard<std::mutex> lock(this->
msgMutex);
124 const auto&
msg =
event.getConstMessage();
126 if (!this->
hasHeader.has_value() && this->inspectStamps())
132 CRAS_ERROR(
"Running repeat with timestamp conditions on message type %s which does not have a header! "
140 cras::optional<ros::Time> stamp;
151 CRAS_INFO_THROTTLE(5.0,
"Received message too old (%.3g s > %.3g s) will be discarded.",
152 (
ros::Time::now() - stamp.value()).toSec(), this->maxAge.value().toSec());
156 && stamp.value() < this->lastMsgStamp.value())
159 5.0,
"Received message is %.3g s older than current message, it will be discarded.",
168 std::lock_guard<std::mutex> lock(this->
msgMutex);
185 if (!this->
pub || this->
msg ==
nullptr)
188 std::lock_guard<std::mutex> lock(this->
msgMutex);
190 if (this->
maxRepeats.has_value() && this->numRepeats > this->maxRepeats.value())
197 (this->lastMsgStamp.value() + this->maxAge.value()) <
ros::Time::now())
205 this->
pub.template publish(this->
msg);
::ros::Subscriber resetSub
Subscriber to the reset topic.
bool hasHeader(const ::topic_tools::ShapeShifter &msg)
Tell whether the given message has header field.
void everyPeriod(const ::ros::TimerEvent &)
Timer callback. Publish the last stored message if it is eligible.
Tools for more convenient working with ShapeShifter objects.
::cras::optional<::ros::Duration > maxAge
Maximum age of a message to allow publishing it (the message has to have a Header).
::cras::BoundParamHelperPtr privateParams(const ::std::string &ns="") const
::ros::Timer timer
The timer periodically republishing the last stored message.
::std::unique_ptr<::ros::Rate > rate
The desired output rate.
TransportHints transport_hints
ros::NodeHandle & getMTPrivateNodeHandle() const
virtual bool inspectStamps() const
Whether Header should be extracted from the message and its time stamp should undergo inspection.
bool discardOlderMessages
Whether to discard an incoming message if its stamp is older than the previously accepted message.
const V_string & getMyArgv() const
virtual void maybePublish()
Publish the last stored message (if it is eligible).
TransportHints & tcpNoDelay(bool nodelay=true)
Lazy subscriber that subscribes only when a paired publisher has subscribers. Version for unknown mes...
bool publishOnlyOnTimer
Whether to publish only on timer events, or also when an incoming message is received.
#define PLUGINLIB_EXPORT_CLASS(class_type, base_class_type)
::ros::Publisher pub
The publisher of the repeated messages.
void setPeriod(const Duration &period, bool reset=true)
void reset() override
Reset the repeater, e.g. after a time jump.
void initByFullCallbackType(const std::string &_topic, uint32_t _queue_size, const boost::function< void(P)> &_callback, const boost::function< boost::shared_ptr< typename ParameterAdapter< P >::Message >(void)> &factory_fn=DefaultMessageCreator< typename ParameterAdapter< P >::Message >())
Subscriber subscribe(const std::string &topic, uint32_t queue_size, const boost::function< void(C)> &callback, const VoidConstPtr &tracked_object=VoidConstPtr(), const TransportHints &transport_hints=TransportHints())
virtual void onReset(const ::ros::MessageEvent< const ::topic_tools::ShapeShifter > &)
Called when the repeater should be reset. The incoming message can be of any type and should not be e...
::std::unique_ptr<::cras::GenericLazyPubSub > pubSub
The lazy pair of subscriber and publisher.
size_t numRepeats
Number of times the last stored message has been repeated.
auto bind_front(F &&f, Args &&... args)
#define CRAS_INFO_THROTTLE(period,...)
#define CRAS_WARN_THROTTLE(period,...)
void processMessage(const ::ros::MessageEvent< const ::topic_tools::ShapeShifter > &event, ::ros::Publisher &pub)
Record the incoming message if it passes validations, and publish it if publishOnlyOnTimer is false.
Nodelet for repeating messages coming at a slower rate (or even just a single message).
::cras::BoundParamHelperPtr params(const ::ros::NodeHandle &node, const ::std::string &ns="") const
This is a simple implementation of a repeater nodelet.
bool resetOnMsg
Whether to reset the publication timer when a new message arrives.
::cras::optional< bool > hasHeader
M_string & getConnectionHeader() const
::cras::optional<::ros::Time > lastMsgStamp
Time stamp of the last stored message (only filled if inspectStamps() returns true.
::cras::optional<::std_msgs::Header > getHeader(const ::topic_tools::ShapeShifter &msg)
Get the header field of the given message, if it has any.
::cras::optional< size_t > maxRepeats
Maximum number of repetitions of a single message (only limited when set).
::std::mutex msgMutex
Mutex protecting msg, lastMsgStamp and numRepeats.
double parseDouble(const ::std::string &string)
ros::NodeHandle & getMTNodeHandle() const
inline ::std::string to_string(char *value)
::topic_tools::ShapeShifter::ConstPtr msg
The last stored message (null if no message has been received yet).
cras_topic_tools
Author(s): Martin Pecka
autogenerated on Wed Jan 8 2025 03:50:28