mongodb_log_pcl.cpp
Go to the documentation of this file.
1 
2 /***************************************************************************
3  * mongodb_log_pcl.cpp - MongoDB Logger for point cloud topics
4  *
5  * Created: Mon Dec 13 15:05:37 2010
6  * Copyright 2010 Tim Niemueller [www.niemueller.de]
7  * 2010 Carnegie Mellon University
8  * 2010 Intel Labs Pittsburgh
9  ****************************************************************************/
10 
11 /* This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version. A runtime exception applies to
15  * this software (see LICENSE.GPL_WRE file mentioned below for details).
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  * GNU Library General Public License for more details.
21  *
22  * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
23  */
24 
25 #include <ros/ros.h>
26 #include <mongo/client/dbclient.h>
27 
28 #include <sensor_msgs/PointCloud.h>
29 #include <unistd.h>
30 #include <cstring>
31 
32 #include <mongodb_store/util.h>
33 
34 using namespace mongo;
35 
36 DBClientConnection *mongodb_conn;
37 std::string collection;
38 
39 unsigned int in_counter;
40 unsigned int out_counter;
41 unsigned int qsize;
42 unsigned int drop_counter;
43 
44 static pthread_mutex_t in_counter_mutex = PTHREAD_MUTEX_INITIALIZER;
45 static pthread_mutex_t out_counter_mutex = PTHREAD_MUTEX_INITIALIZER;
46 static pthread_mutex_t drop_counter_mutex = PTHREAD_MUTEX_INITIALIZER;
47 static pthread_mutex_t qsize_mutex = PTHREAD_MUTEX_INITIALIZER;
48 
49 void msg_callback(const sensor_msgs::PointCloud::ConstPtr& msg)
50 {
51  BSONObjBuilder document;
52  BSONObjBuilder transform;
53 
54 
55  const sensor_msgs::PointCloud& msg_in = *msg;
56 
57  Date_t stamp = msg_in.header.stamp.sec * 1000.0 + msg_in.header.stamp.nsec / 1000000.0;
58  document.append("header", BSON("seq" << msg_in.header.seq
59  << "stamp" << stamp
60  << "frame_id" << msg_in.header.frame_id));
61 
62  BSONArrayBuilder pointb(document.subarrayStart("points"));
63  // I can't believe it's encoded that inefficient. Someone should be sent to jail for that.
64  std::vector<geometry_msgs::Point32>::const_iterator p;
65  for (p = msg_in.points.begin(); p != msg_in.points.end(); ++p) {
66  pointb.append(BSON( "x" << p->x
67  << "y" << p->y
68  << "z" << p->z));
69  }
70  pointb.doneFast();
71 
72  BSONArrayBuilder channelsb(document.subarrayStart("channels"));
73  BSONObjBuilder cb;
74 
75  std::vector<sensor_msgs::ChannelFloat32>::const_iterator c;
76  for (c = msg_in.channels.begin(); c != msg_in.channels.end(); ++c) {
77  cb.append("name", c->name);
78 
79  BSONArrayBuilder valuesb(cb.subarrayStart("values"));
80  std::vector<float>::const_iterator v;
81  for (v = c->values.begin(); v != c->values.end(); ++v) {
82  valuesb.append(*v);
83  }
84  valuesb.doneFast();
85 
86  channelsb.append(cb.obj());
87  }
88  channelsb.doneFast();
89 
90  mongodb_store::add_meta_for_msg<sensor_msgs::PointCloud>(msg, document);
91 
92  mongodb_conn->insert(collection, document.obj());
93 
94  // If we'd get access to the message queue this could be more useful
95  // https://code.ros.org/trac/ros/ticket/744
96  pthread_mutex_lock(&in_counter_mutex);
97  ++in_counter;
98  pthread_mutex_unlock(&in_counter_mutex);
99  pthread_mutex_lock(&out_counter_mutex);
100  ++out_counter;
101  pthread_mutex_unlock(&out_counter_mutex);
102 }
103 
105 {
106  unsigned int l_in_counter, l_out_counter, l_drop_counter, l_qsize;
107 
108  pthread_mutex_lock(&in_counter_mutex);
109  l_in_counter = in_counter; in_counter = 0;
110  pthread_mutex_unlock(&in_counter_mutex);
111 
112  pthread_mutex_lock(&out_counter_mutex);
113  l_out_counter = out_counter; out_counter = 0;
114  pthread_mutex_unlock(&out_counter_mutex);
115 
116  pthread_mutex_lock(&drop_counter_mutex);
117  l_drop_counter = drop_counter; drop_counter = 0;
118  pthread_mutex_unlock(&drop_counter_mutex);
119 
120  pthread_mutex_lock(&qsize_mutex);
121  l_qsize = qsize; qsize = 0;
122  pthread_mutex_unlock(&qsize_mutex);
123 
124  printf("%u:%u:%u:%u\n", l_in_counter, l_out_counter, l_drop_counter, l_qsize);
125  fflush(stdout);
126 }
127 
128 
129 int
130 main(int argc, char **argv)
131 {
132  std::string topic = "", mongodb = "localhost", nodename = "";
133  collection = "";
134 
136 
137  int c;
138  while ((c = getopt(argc, argv, "t:m:n:c:")) != -1) {
139  if ((c == '?') || (c == ':')) {
140  printf("Usage: %s -t topic -m mongodb -n nodename -c collection\n", argv[0]);
141  exit(-1);
142  } else if (c == 't') {
143  topic = optarg;
144  } else if (c == 'm') {
145  mongodb = optarg;
146  } else if (c == 'n') {
147  nodename = optarg;
148  } else if (c == 'c') {
149  collection = optarg;
150  }
151  }
152 
153  if (topic == "") {
154  printf("No topic given.\n");
155  exit(-2);
156  } else if (nodename == "") {
157  printf("No node name given.\n");
158  exit(-2);
159  }
160 
161  ros::init(argc, argv, nodename);
162  ros::NodeHandle n;
163 
164  std::string errmsg;
165  mongodb_conn = new DBClientConnection(/* auto reconnect*/ true);
166  if (! mongodb_conn->connect(mongodb, errmsg)) {
167  ROS_ERROR("Failed to connect to MongoDB: %s", errmsg.c_str());
168  return -1;
169  }
170 
171  ros::Subscriber sub = n.subscribe<sensor_msgs::PointCloud>(topic, 1000, msg_callback);
172  ros::Timer count_print_timer = n.createTimer(ros::Duration(5, 0), print_count);
173 
174  ros::spin();
175 
176  delete mongodb_conn;
177 
178  return 0;
179 }
void print_count(const ros::TimerEvent &te)
static pthread_mutex_t qsize_mutex
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
static pthread_mutex_t out_counter_mutex
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
DBClientConnection * mongodb_conn
int main(int argc, char **argv)
std::string collection
unsigned int drop_counter
ROSCPP_DECL void spin(Spinner &spinner)
static pthread_mutex_t in_counter_mutex
std::string topic
void msg_callback(const sensor_msgs::PointCloud::ConstPtr &msg)
Timer createTimer(Rate r, Handler h, Obj o, bool oneshot=false, bool autostart=true) const
unsigned int qsize
unsigned int in_counter
#define ROS_ERROR(...)
static pthread_mutex_t drop_counter_mutex
unsigned int out_counter


mongodb_log
Author(s): Tim Niemueller
autogenerated on Sat Sep 7 2019 03:40:41