msgpack_validator.h
Go to the documentation of this file.
1 #include "sick_scan/sick_scan_base.h" /* Base definitions included in all header files, added by add_sick_scan_base_header.py. Do not edit this line. */
2 /*
3  * @brief msgpack_validator validates received msgpacks against
4  * the multiScan136 filter settings (FREchoFilter, LFPangleRangeFilter,
5  * and LFPlayerFilter).
6  *
7  * If msgpacks with scan data out of filter settings (i.e. echo,
8  * azimuth or segment not configured), an error message is printed
9  * and the msgpack is discarded.
10  *
11  * By default, the msgpack_validator checks msgpacks against the full
12  * range, i.e. all echos, azimuth_start=-PI, azimuth_end=+PI,
13  * elevation_start=-PI/2, elevation_end=+PI/2 and all segments.
14  *
15  * Copyright (C) 2020 Ing.-Buero Dr. Michael Lehning, Hildesheim
16  * Copyright (C) 2020 SICK AG, Waldkirch
17  *
18  * Licensed under the Apache License, Version 2.0 (the "License");
19  * you may not use this file except in compliance with the License.
20  * You may obtain a copy of the License at
21  *
22  * http://www.apache.org/licenses/LICENSE-2.0
23  *
24  * Unless required by applicable law or agreed to in writing, software
25  * distributed under the License is distributed on an "AS IS" BASIS,
26  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
27  * See the License for the specific language governing permissions and
28  * limitations under the License.
29  *
30  * All rights reserved.
31  *
32  * Redistribution and use in source and binary forms, with or without
33  * modification, are permitted provided that the following conditions are met:
34  *
35  * * Redistributions of source code must retain the above copyright
36  * notice, this list of conditions and the following disclaimer.
37  * * Redistributions in binary form must reproduce the above copyright
38  * notice, this list of conditions and the following disclaimer in the
39  * documentation and/or other materials provided with the distribution.
40  * * Neither the name of SICK AG nor the names of its
41  * contributors may be used to endorse or promote products derived from
42  * this software without specific prior written permission
43  * * Neither the name of Ing.-Buero Dr. Michael Lehning nor the names of its
44  * contributors may be used to endorse or promote products derived from
45  * this software without specific prior written permission
46  *
47  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
48  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
51  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
52  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
53  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
54  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
55  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
56  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
57  * POSSIBILITY OF SUCH DAMAGE.
58  *
59  * Authors:
60  * Michael Lehning <michael.lehning@lehning.de>
61  *
62  * Copyright 2020 SICK AG
63  * Copyright 2020 Ing.-Buero Dr. Michael Lehning
64  *
65  */
66 #ifndef __SICK_SCANSEGMENT_XD_MSGPACK_VALIDATOR_H
67 #define __SICK_SCANSEGMENT_XD_MSGPACK_VALIDATOR_H
68 
69 #ifndef _USE_MATH_DEFINES // to ensure that M_PI is defined
70 #define _USE_MATH_DEFINES
71 #endif
72 #include <math.h>
73 #include <map>
74 #include <string>
75 #include <vector>
76 
79 
80 namespace sick_scansegment_xd
81 {
82  /*
83  * @brief class MsgPackValidatorData collects echo_idx, azimuth, elevation and segment_idx
84  * during msgpack parsing for msgpack validation.
85  */
87  {
88  public:
89 
90  /*
91  * @brief Default constructor.
92  */
94 
95  /*
96  * @brief Default destructor.
97  */
99 
100  /*
101  * @brief Updates the azimuth histogram
102  */
103  void update(int echo_idx, int segment_idx, float azimuth, float elevation);
104 
105  /*
106  * @brief Returns the resolution of azimuth histogram in degree (i.e. 1.0 degree)
107  */
108  float getAzimuthHistogramResolutionDeg(void) const;
109 
110  /*
111  * @brief Returns the resolution of azimuth histogram in radians (i.e. (PI/180) * 1.0 degree)
112  */
113  float getAzimuthHistogramResolutionRad(void) const;
114 
115  /*
116  * @brief Returns the resolution of elevation histogram in degree (i.e. 1.0 degree)
117  */
118  float getElevationHistogramResolutionDeg(void) const;
119 
120  /*
121  * @brief Returns the resolution of elevation histogram in degree (i.e. (PI/180) * 1.0 degree)
122  */
123  float getElevationHistogramResolutionRad(void) const;
124 
125  /*
126  * @brief Returns the azimuth histogram as human readable string
127  */
128  std::vector<std::string> print(void) const;
129 
130  // Histogram of azimuth angles: azimuth_count = AzimuthHistogram[azimuth_idx]
131  typedef std::map<int, int> AzimuthHistogram;
132 
133  // AzimuthHistogram per elevation: azimuth_count = AzimuthHistogramPerElevation[elevation_idx][azimuth_idx]
134  typedef std::map<int, AzimuthHistogram> AzimuthHistogramPerElevation;
135 
136  // AzimuthHistogramPerElevation per segment: azimuth_count = AzimuthHistogramPerElevationPerSegment[segment_idx][elevation_idx][azimuth_idx]
137  typedef std::map<int, AzimuthHistogramPerElevation> AzimuthHistogramPerElevationPerSegment;
138 
139  // AzimuthHistogramPerElevationPerSegment per per echo: azimuth_count = AzimuthHistogramPerElevationPerSegmentPerEcho[echo_idx][segment_idx][elevation_idx][azimuth_idx]
140  typedef std::map<int, AzimuthHistogramPerElevationPerSegment> AzimuthHistogramPerElevationPerSegmentPerEcho;
141 
142  // Returns the Histogram of azimuth angles: azimuth_count = getHistogram()[echo_idx][segment_idx][elevationToInt(elevation)][azimuthToInt(azimuth)]
144 
145  // Converts the azimuth index of the azimuth histogram to the azimuth angle in radians
146  float azimuthIndexToRad(int azimuth_idx) const { return intToAzimuth(azimuth_idx); }
147 
148  // Converts the azimuth index of the azimuth histogram to the azimuth angle in degree
149  float azimuthIndexToDeg(int azimuth_idx) const { return rad2deg(intToAzimuth(azimuth_idx)); }
150 
151  // Converts the elevation index of the histogram to the elevation angle in radians
152  float elevationIndexToRad(int elevation_idx) const { return intToElevation(elevation_idx); }
153 
154  // Converts the elevation index of the histogram to the elevation angle in degree
155  float elevationIndexToDeg(int elevation_idx) const { return rad2deg(intToElevation(elevation_idx)); }
156 
157  // Converts azimuth angle in rad to the azimuth index of the histogram
158  int azimuthRadToIndex(float azimuth_rad) const { return azimuthToInt(azimuth_rad); }
159 
160  // Converts elevation angle in rad to the elevation index of the histogram
161  int elevationRadToIndex(float elevation_rad) const { return elevationToInt(elevation_rad); }
162 
163  protected:
164 
165  #define AzimuthHistogramResolution (1.0f) // Histogram of azimuth angles in 1.0 degrees
166  #define ElevationHistogramResolution (1.0f) // Histogram of elevation angles in 1.0 degrees
167 
168  float deg2rad(float angle) const { return angle * (float)(M_PI / 180.0); }
169  float rad2deg(float angle) const { return angle * (float)(180.0 / M_PI); }
170 
172  int azimuthToInt(float azimuth_rad) const { return (int)std::round(rad2deg(azimuth_rad) / AzimuthHistogramResolution); }
173  float intToAzimuth(int azimuth_idx) const { return deg2rad(azimuth_idx * AzimuthHistogramResolution); }
174 
176  int elevationToInt(float elevation_rad) const { return (int)std::round(rad2deg(elevation_rad) / ElevationHistogramResolution); }
177  float intToElevation(int elevation_idx) const { return deg2rad(elevation_idx * ElevationHistogramResolution); }
178 
179  /*
180  * Member data
181  */
182 
183  // Histogram of azimuth angles: azimuth_count = m_azimuth_histogram[echo_idx][segment_idx][elevationToInt(elevation)][azimuthToInt(azimuth)]
185 
186  }; // class MsgPackValidatorData
187 
188  /*
189  * @brief class MsgPackValidator validates received msgpacks against
190  * the multiScan136 filter settings (FREchoFilter, LFPangleRangeFilter,
191  * and LFPlayerFilter).
192  *
193  * If msgpacks with scan data out of filter settings (i.e. echo,
194  * azimuth or segment not configured), an error message is printed
195  * and the msgpack is discarded.
196  *
197  * By default, the msgpack_validator checks msgpacks against the full
198  * range, i.e. all echos, azimuth_start=-PI, azimuth_end=+PI,
199  * elevation_start=-PI/2, elevation_end=+PI/2 and all segments.
200  */
202  {
203  public:
204 
205  /*
206  * @brief Default constructor, initializes full range validation (all echos, azimuth_start=-PI, azimuth_end=+PI,
207  * elevation_start=-PI/2, elevation_end=+PI/2, 12 segments)
208  *
209  * @param[in] echos indizes of expected echos, default: all echos required
210  * @param[in] azimuth_start start azimuth in radians, default: -PI
211  * @param[in] azimuth_end end azimuth in radians, default: +PI
212  * @param[in] elevation_start start elevation in radians, default: -PI/2
213  * @param[in] elevation_end end elevation in radians, default: +PI/2
214  * @param[in] segments indizes of expected segments, default: 12 segments
215  * @param[in] layer_filter 0 (deactivated) or 1 (activated) for each layer, default: all 16 layers activated
216  * @param[in] verbose 0: print error messages, 1: print error and informational messages, 2: print error and all messages
217  */
218  MsgPackValidator(const std::vector<int>& echos = { 0, 1, 2 },
219  float azimuth_start = -M_PI, float azimuth_end = M_PI,
220  float elevation_start = -M_PI/2.0, float elevation_end = M_PI/2.0,
221  const std::vector<int>& segments = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 },
222  const std::vector<int>& layer_filter = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
223  int verbose = 0);
224 
225  /*
226  * @brief Default destructor.
227  */
229 
230  /*
231  * @brief Validates a received msgpack against the configured limits, i.e. checks that echo, segment and azimuth are
232  * within their configured range.
233  *
234  * @param[in] data_received echos, azimuth, elevation and segments collected during msgpack parsing
235  *
236  * @return true if validation passed successfully, false otherwise.
237  */
238  bool validateNotOutOfBound(const MsgPackValidatorData& data_received) const;
239 
240  /*
241  * @brief Validates a received msgpack, i.e. checks that the required number of echos, segments and azimuth values have been received.
242  *
243  * @param[in] msgpack_data_collected echos, azimuth, elevation and segments collected during msgpack parsing
244  *
245  * @return true if validation passed successfully, false otherwise.
246  */
247  bool validateNoMissingScandata(const MsgPackValidatorData& msgpack_data_collected) const;
248 
249  protected:
250 
252  template<typename T> std::string listVector(const std::vector<T> & vec) const
253  {
254  std::stringstream s;
255  for(auto iter = vec.cbegin(); iter != vec.cend(); iter++)
256  s << (iter == vec.cbegin() ? "" : ", ") << (*iter);
257  return s.str();
258  }
259 
261  int getAzimuthHistogramCount(const MsgPackValidatorData::AzimuthHistogramPerElevationPerSegmentPerEcho& azimuth_histogram, int echo_idx, int segment_idx, int elevation_idx, int azimuth_idx) const;
262 
263  void printMissingHistogramData(const std::vector<std::string>& messages) const;
264 
265  /*
266  * Member data for msgpack validataion
267  */
268  std::vector<int> m_echos_required; // indizes of expected echos, default: all echos required
269  float m_azimuth_start; // start azimuth in radians, default: -PI
270  float m_azimuth_end; // azimuth in radians, default: +PI
271  float m_elevation_start; // start elevation in radians, default: -PI/2
272  float m_elevation_end; // end elevation in radians, default: +PI/2
273  std::vector<int> m_valid_segments; // indizes of valid segments, default: all segments, index 0 to 11
274  std::vector<int> m_layer_filter; // layer_filter, 0 (deactivated) or 1 (activated) for each layer, default: all 16 layers activated
275  int m_verbose; // 0: print error messages, 1: print error and informational messages, 2: print error and all messages
276 
277  }; // class MsgPackValidator
278 
279 } // namespace sick_scansegment_xd
280 #endif // __SICK_SCANSEGMENT_XD_MSGPACK_VALIDATOR_H
common.h
sick_scansegment_xd::MsgPackValidatorData::elevationIndexToRad
float elevationIndexToRad(int elevation_idx) const
Definition: msgpack_validator.h:152
sick_scansegment_xd::MsgPackValidatorData::azimuthRadToIndex
int azimuthRadToIndex(float azimuth_rad) const
Definition: msgpack_validator.h:158
sick_scansegment_xd::MsgPackValidator::MsgPackValidator
MsgPackValidator(const std::vector< int > &echos={ 0, 1, 2 }, float azimuth_start=-M_PI, float azimuth_end=M_PI, float elevation_start=-M_PI/2.0, float elevation_end=M_PI/2.0, const std::vector< int > &segments={ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 }, const std::vector< int > &layer_filter={ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, int verbose=0)
Definition: msgpack_validator.cpp:188
angle
TFSIMD_FORCE_INLINE tfScalar angle(const Quaternion &q1, const Quaternion &q2)
sick_scansegment_xd::MsgPackValidatorData::getAzimuthHistogramResolutionRad
float getAzimuthHistogramResolutionRad(void) const
Definition: msgpack_validator.cpp:101
sick_scansegment_xd::MsgPackValidator::getAzimuthHistogramCount
int getAzimuthHistogramCount(const MsgPackValidatorData::AzimuthHistogramPerElevationPerSegmentPerEcho &azimuth_histogram, int echo_idx, int segment_idx, int elevation_idx, int azimuth_idx) const
Definition: msgpack_validator.cpp:280
sick_scansegment_xd::MsgPackValidatorData::elevationIndexToDeg
float elevationIndexToDeg(int elevation_idx) const
Definition: msgpack_validator.h:155
s
XmlRpcServer s
sick_scansegment_xd::MsgPackValidatorData
Definition: msgpack_validator.h:86
sick_scansegment_xd::MsgPackValidator::m_layer_filter
std::vector< int > m_layer_filter
Definition: msgpack_validator.h:274
sick_scansegment_xd
Definition: include/sick_scansegment_xd/common.h:138
sick_scansegment_xd::MsgPackValidator::m_azimuth_start
float m_azimuth_start
Definition: msgpack_validator.h:269
ElevationHistogramResolution
#define ElevationHistogramResolution
Definition: msgpack_validator.h:166
sick_scansegment_xd::MsgPackValidatorData::getElevationHistogramResolutionRad
float getElevationHistogramResolutionRad(void) const
Definition: msgpack_validator.cpp:117
sick_scansegment_xd::MsgPackValidatorData::getAzimuthHistogramResolutionDeg
float getAzimuthHistogramResolutionDeg(void) const
Definition: msgpack_validator.cpp:93
sick_scansegment_xd::MsgPackValidator::m_verbose
int m_verbose
Definition: msgpack_validator.h:275
sick_scansegment_xd::MsgPackValidator::validateNotOutOfBound
bool validateNotOutOfBound(const MsgPackValidatorData &data_received) const
Definition: msgpack_validator.cpp:210
sick_scansegment_xd::MsgPackValidatorData::elevationRadToIndex
int elevationRadToIndex(float elevation_rad) const
Definition: msgpack_validator.h:161
sick_scansegment_xd::MsgPackValidator::validateNoMissingScandata
bool validateNoMissingScandata(const MsgPackValidatorData &msgpack_data_collected) const
Definition: msgpack_validator.cpp:328
sick_scansegment_xd::MsgPackValidatorData::~MsgPackValidatorData
~MsgPackValidatorData()
Definition: msgpack_validator.cpp:78
sick_ros_wrapper.h
sick_scansegment_xd::MsgPackValidatorData::MsgPackValidatorData
MsgPackValidatorData()
Definition: msgpack_validator.cpp:71
multiscan_pcap_player.verbose
int verbose
Definition: multiscan_pcap_player.py:142
sick_scansegment_xd::MsgPackValidatorData::azimuthIndexToRad
float azimuthIndexToRad(int azimuth_idx) const
Definition: msgpack_validator.h:146
sick_scansegment_xd::MsgPackValidatorData::rad2deg
float rad2deg(float angle) const
Definition: msgpack_validator.h:169
sick_scansegment_xd::MsgPackValidatorData::deg2rad
float deg2rad(float angle) const
Definition: msgpack_validator.h:168
AzimuthHistogramResolution
#define AzimuthHistogramResolution
Definition: msgpack_validator.h:165
sick_scansegment_xd::MsgPackValidator::~MsgPackValidator
~MsgPackValidator()
Definition: msgpack_validator.cpp:198
sick_scansegment_xd::MsgPackValidator::m_elevation_end
float m_elevation_end
Definition: msgpack_validator.h:272
sick_scansegment_xd::MsgPackValidatorData::m_azimuth_histogram
AzimuthHistogramPerElevationPerSegmentPerEcho m_azimuth_histogram
Definition: msgpack_validator.h:184
sick_scansegment_xd::MsgPackValidator::m_elevation_start
float m_elevation_start
Definition: msgpack_validator.h:271
sick_scansegment_xd::MsgPackValidator::m_echos_required
std::vector< int > m_echos_required
Definition: msgpack_validator.h:268
sick_scansegment_xd::MsgPackValidatorData::azimuthToInt
int azimuthToInt(float azimuth_rad) const
Definition: msgpack_validator.h:172
sick_scansegment_xd::MsgPackValidatorData::intToElevation
float intToElevation(int elevation_idx) const
Definition: msgpack_validator.h:177
sick_scansegment_xd::MsgPackValidatorData::AzimuthHistogram
std::map< int, int > AzimuthHistogram
Definition: msgpack_validator.h:131
sick_scansegment_xd::MsgPackValidatorData::azimuthIndexToDeg
float azimuthIndexToDeg(int azimuth_idx) const
Definition: msgpack_validator.h:149
sick_scansegment_xd::MsgPackValidatorData::print
std::vector< std::string > print(void) const
Definition: msgpack_validator.cpp:125
sick_scansegment_xd::MsgPackValidatorData::AzimuthHistogramPerElevation
std::map< int, AzimuthHistogram > AzimuthHistogramPerElevation
Definition: msgpack_validator.h:134
sick_scansegment_xd::MsgPackValidator::m_azimuth_end
float m_azimuth_end
Definition: msgpack_validator.h:270
sick_scansegment_xd::MsgPackValidatorData::AzimuthHistogramPerElevationPerSegmentPerEcho
std::map< int, AzimuthHistogramPerElevationPerSegment > AzimuthHistogramPerElevationPerSegmentPerEcho
Definition: msgpack_validator.h:140
sick_scansegment_xd::MsgPackValidatorData::update
void update(int echo_idx, int segment_idx, float azimuth, float elevation)
Definition: msgpack_validator.cpp:85
sick_scan_base.h
sick_scansegment_xd::MsgPackValidatorData::intToAzimuth
float intToAzimuth(int azimuth_idx) const
Definition: msgpack_validator.h:173
sick_scansegment_xd::MsgPackValidator
Definition: msgpack_validator.h:201
sick_scansegment_xd::MsgPackValidatorData::getElevationHistogramResolutionDeg
float getElevationHistogramResolutionDeg(void) const
Definition: msgpack_validator.cpp:109
sick_scansegment_xd::MsgPackValidator::m_valid_segments
std::vector< int > m_valid_segments
Definition: msgpack_validator.h:273
sick_scansegment_xd::MsgPackValidatorData::getHistogram
const AzimuthHistogramPerElevationPerSegmentPerEcho & getHistogram(void) const
Definition: msgpack_validator.h:143
sick_scansegment_xd::MsgPackValidatorData::elevationToInt
int elevationToInt(float elevation_rad) const
Definition: msgpack_validator.h:176
sick_scansegment_xd::MsgPackValidator::listVector
std::string listVector(const std::vector< T > &vec) const
Definition: msgpack_validator.h:252
sick_scansegment_xd::MsgPackValidatorData::AzimuthHistogramPerElevationPerSegment
std::map< int, AzimuthHistogramPerElevation > AzimuthHistogramPerElevationPerSegment
Definition: msgpack_validator.h:137
sick_scansegment_xd::MsgPackValidator::printMissingHistogramData
void printMissingHistogramData(const std::vector< std::string > &messages) const
Definition: msgpack_validator.cpp:312


sick_scan_xd
Author(s): Michael Lehning , Jochen Sprickerhof , Martin Günther
autogenerated on Fri Oct 25 2024 02:47:09