drop.cpp
Go to the documentation of this file.
1 // drop will (intentionally) drop X out of every Y messages that hits it
3 //
4 // Copyright (C) 2009, Morgan Quigley
5 //
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions are met:
8 // * Redistributions of source code must retain the above copyright notice,
9 // this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of Stanford University nor the names of its
14 // contributors may be used to endorse or promote products derived from
15 // this software without specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 // POSSIBILITY OF SUCH DAMAGE.
29 
30 #include <cstdlib>
31 #include <cstdio>
33 #include "topic_tools/parse.h"
34 
35 using std::string;
36 using std::vector;
37 using namespace topic_tools;
38 
39 static ros::NodeHandle *g_node = NULL;
40 static int g_x = 0, g_y = 1;
41 static bool g_advertised = false;
42 static string g_output_topic;
44 
46 {
47  static int s_count = 0;
48  if (!g_advertised)
49  {
50  g_pub = msg->advertise(*g_node, g_output_topic, 10);
51  g_advertised = true;
52  printf("advertised as %s\n", g_output_topic.c_str());
53  }
54  if (s_count >= g_x)
55  g_pub.publish(msg);
56  ++s_count;
57  if (s_count >= g_y)
58  s_count = 0;
59 }
60 
61 #define USAGE "\nusage: drop IN_TOPIC X Y [OUT_TOPIC]\n\n" \
62  " This program will drop X out of every Y messages from IN_TOPIC,\n" \
63  " forwarding the rest to OUT_TOPIC if given, else to a topic \n" \
64  " named IN_TOPIC_drop\n\n"
65 int main(int argc, char **argv)
66 {
67  if(argc < 2)
68  {
69  puts(USAGE);
70  return 1;
71  }
72  std::string topic_name;
73  if(!getBaseName(string(argv[1]), topic_name))
74  return 1;
75  ros::init(argc, argv, topic_name + string("_drop"),
77  if ((argc != 4 && argc != 5) || atoi(argv[2]) < 0 || atoi(argv[3]) < 1)
78  {
79  puts(USAGE);
80  return 1;
81  }
82  if (argc == 4)
83  g_output_topic = string(argv[1]) + string("_drop");
84  else // argc == 5
85  g_output_topic = string(argv[4]);
87  g_node = &n;
88  g_x = atoi(argv[2]);
89  g_y = atoi(argv[3]);
90  ros::Subscriber sub = n.subscribe<ShapeShifter>(string(argv[1]), 10, in_cb);
91  ros::spin();
92  return 0;
93 }
g_x
static int g_x
Definition: drop.cpp:40
ros::init_options::AnonymousName
AnonymousName
ros::Publisher
boost::shared_ptr
ros::init
ROSCPP_DECL void init(const M_string &remappings, const std::string &name, uint32_t options=0)
shape_shifter.h
g_advertised
static bool g_advertised
Definition: drop.cpp:41
ros::Publisher::publish
void publish(const boost::shared_ptr< M > &message) const
parse.h
USAGE
#define USAGE
Definition: drop.cpp:61
g_output_topic
static string g_output_topic
Definition: drop.cpp:42
pub_info_t::topic_name
std::string topic_name
Definition: demux.cpp:60
ros::NodeHandle::subscribe
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())
g_y
static int g_y
Definition: drop.cpp:40
topic_tools::getBaseName
TOPIC_TOOLS_DECL bool getBaseName(const std::string &full_name, std::string &base_name)
Definition: parse.cpp:40
in_cb
void in_cb(const boost::shared_ptr< ShapeShifter const > &msg)
Definition: drop.cpp:45
g_pub
static ros::Publisher g_pub
Definition: drop.cpp:43
main
int main(int argc, char **argv)
Definition: drop.cpp:65
ros::spin
ROSCPP_DECL void spin()
g_node
static ros::NodeHandle * g_node
Definition: drop.cpp:39
topic_tools::ShapeShifter
Definition: shape_shifter.h:91
topic_tools
Definition: parse.h:33
ros::NodeHandle
ros::Subscriber


topic_tools
Author(s): Morgan Quigley, Brian Gerkey, Dirk Thomas , Jacob Perron
autogenerated on Sat Sep 14 2024 03:00:05