Fawkes API  Fawkes Development Version
deadspots.cpp
1 
2 /***************************************************************************
3  * deadspots.cpp - Laser data dead spots filter
4  *
5  * Created: Wed Jun 24 22:42:51 2009
6  * Copyright 2006-2011 Tim Niemueller [www.niemueller.de]
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU Library General Public License for more details.
19  *
20  * Read the full text in the LICENSE.GPL file in the doc directory.
21  */
22 
23 #include "deadspots.h"
24 
25 #include <core/exception.h>
26 #include <core/macros.h>
27 #include <utils/math/angle.h>
28 #include <utils/time/time.h>
29 #include <logging/logger.h>
30 #include <config/config.h>
31 
32 #include <cstdlib>
33 #include <cstring>
34 #include <sys/types.h>
35 #include <regex.h>
36 
37 using namespace fawkes;
38 
39 /** @class LaserDeadSpotsDataFilter "filters/deadspots.h"
40  * Erase dead spots (i.e. mounting rods in the laser range) from laser data.
41  * This filter reads a number of values stored in /hardware/laser/deadspots, where
42  * each dead spot must contain two entries, a start and an end in degrees. Each
43  * entry is stored as submembers of the given tree, for example as
44  * /hardware/laser/deadspots/0/start and /hardware/laser/deadspots/0/end.
45  * @author Tim Niemueller
46  */
47 
48 /** Constructor.
49  * @param filter_name name of this filter instance
50  * @param config configuration instance
51  * @param logger logger for informational output
52  * @param prefix configuration prefix where to log for config information
53  * @param in_data_size number of entries input value arrays
54  * @param in vector of input arrays
55  */
57  fawkes::Configuration *config,
58  fawkes::Logger *logger,
59  std::string prefix,
60  unsigned int in_data_size,
61  std::vector<LaserDataFilter::Buffer *> &in)
62  : LaserDataFilter(filter_name, in_data_size, in, in.size())
63 {
64  __logger = logger;
65 
66  regex_t pathre;
67  int error = 0;
68  if ((error = regcomp(&pathre, (prefix + "\\([^/]\\+\\)/\\(start\\|end\\)").c_str(), 0)) != 0) {
69  size_t errsize = regerror(error, &pathre, NULL, 0);
70  char tmp[errsize];
71  regerror(error, &pathre, tmp, errsize);
72  regfree(&pathre);
73  throw Exception("Failed to compile regular expression: %s", tmp);
74  }
75 
76  regmatch_t matches[2];
77 
78  std::list<std::string> entries;
79 
80  Configuration::ValueIterator *vit = config->search(prefix.c_str());
81  while (vit->next()) {
82  const char *path = vit->path();
83  if (regexec(&pathre, path, 2, matches, 0) == 0) {
84  unsigned int match1_length = matches[1].rm_eo - matches[1].rm_so;
85 
86  char entry[match1_length + 1]; entry[match1_length] = 0;
87  strncpy(entry, &(path[matches[1].rm_so]), match1_length);
88  entries.push_back(entry);
89  }
90  }
91  delete vit;
92  entries.sort();
93  entries.unique();
94 
95  __dead_spots = new unsigned int[entries.size() * 2];
96 
97  for (std::list<std::string>::iterator i = entries.begin(); i != entries.end(); ++i) {
98  std::string path = prefix + *i + "/";
99  float start = config->get_float((path + "start").c_str());
100  float end = config->get_float((path + "end").c_str());
101 
102  __logger->log_debug("LaserDeadSpotsDataFilter", "Adding dead range [%3.3f, %3.3f] (%s)",
103  start, end, i->c_str());
104  __cfg_dead_spots.push_back(std::make_pair(start, end));
105  }
106 
107  __num_spots = __cfg_dead_spots.size();
108 
109  if (__num_spots == 0) {
110  throw Exception("Dead spots filter enabled but no calibration data exists. Run fflaser_deadspots.");
111  }
112 
113  calc_spots();
114 }
115 
116 LaserDeadSpotsDataFilter::~LaserDeadSpotsDataFilter()
117 {
118  delete __dead_spots;
119 }
120 
121 
122 void
123 LaserDeadSpotsDataFilter::set_out_vector(std::vector<LaserDataFilter::Buffer *> &out)
124 {
126  calc_spots();
127 }
128 
129 void
130 LaserDeadSpotsDataFilter::calc_spots()
131 {
132  if (in_data_size != out_data_size) {
133  throw Exception("Dead spots filter requires equal input and output data size");
134  }
135 
136  // need to calculate new beam ranges and allocate different memory segment
137  float angle_factor = 360.0 / in_data_size;
138  for (unsigned int i = 0; i < __num_spots; ++i) {
139  __dead_spots[i * 2 ] =
140  std::min(in_data_size - 1,
141  (unsigned int)ceilf(__cfg_dead_spots[i].first / angle_factor));
142  __dead_spots[i * 2 + 1] =
143  std::min(in_data_size - 1,
144  (unsigned int)ceilf(__cfg_dead_spots[i].second / angle_factor));
145  }
146 }
147 
148 void
150 {
151  const unsigned int vecsize = std::min(in.size(), out.size());
152  for (unsigned int a = 0; a < vecsize; ++a) {
153  out[a]->frame = in[a]->frame;
154  out[a]->timestamp->set_time(in[a]->timestamp);
155  float *inbuf = in[a]->values;
156  float *outbuf = out[a]->values;
157 
158  unsigned int start = 0;
159  for (unsigned int i = 0; i < __num_spots; ++i) {
160  const unsigned int spot_start = __dead_spots[i * 2 ];
161  const unsigned int spot_end = __dead_spots[i * 2 + 1];
162  for (unsigned int j = start; j < spot_start; ++j) {
163  outbuf[j] = inbuf[j];
164  }
165  for (unsigned int j = spot_start; j <= spot_end; ++j) {
166  outbuf[j] = 0.0;
167  }
168  start = spot_end + 1;
169  }
170  for (unsigned int j = start; j < in_data_size; ++j) {
171  outbuf[j] = inbuf[j];
172  }
173  }
174 }
std::vector< Buffer * > out
Vector of output arrays.
Definition: filter.h:76
Fawkes library namespace.
LaserDeadSpotsDataFilter(const std::string filter_name, fawkes::Configuration *config, fawkes::Logger *logger, std::string prefix, unsigned int data_size, std::vector< LaserDataFilter::Buffer *> &in)
Constructor.
Definition: deadspots.cpp:56
virtual ValueIterator * search(const char *path)=0
Iterator with search results.
virtual bool next()=0
Check if there is another element and advance to this if possible.
Base class for exceptions in Fawkes.
Definition: exception.h:36
void filter()
Filter the incoming data.
Definition: deadspots.cpp:149
virtual const char * path() const =0
Path of value.
virtual void log_debug(const char *component, const char *format,...)=0
Log debug message.
Iterator interface to iterate over config values.
Definition: config.h:72
unsigned int out_data_size
Number of entries in output arrays.
Definition: filter.h:73
virtual void set_out_vector(std::vector< Buffer *> &out)
Set filtered data array.
Definition: filter.cpp:129
Interface for configuration handling.
Definition: config.h:67
virtual float get_float(const char *path)=0
Get value from configuration which is of type float.
std::vector< Buffer * > in
Vector of input arrays.
Definition: filter.h:75
unsigned int in_data_size
Number of entries in input arrays.
Definition: filter.h:74
Laser data filter.
Definition: filter.h:32
Interface for logging.
Definition: logger.h:34