utils.h
Go to the documentation of this file.
1 /*
2  * @brief sim_loc_utils contains a collection of utility functions for SIM Localization.
3  *
4  * Copyright (C) 2019 Ing.-Buero Dr. Michael Lehning, Hildesheim
5  * Copyright (C) 2019 SICK AG, Waldkirch
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * All rights reserved.
20  *
21  * Redistribution and use in source and binary forms, with or without
22  * modification, are permitted provided that the following conditions are met:
23  *
24  * * Redistributions of source code must retain the above copyright
25  * notice, this list of conditions and the following disclaimer.
26  * * Redistributions in binary form must reproduce the above copyright
27  * notice, this list of conditions and the following disclaimer in the
28  * documentation and/or other materials provided with the distribution.
29  * * Neither the name of SICK AG nor the names of its
30  * contributors may be used to endorse or promote products derived from
31  * this software without specific prior written permission
32  * * Neither the name of Ing.-Buero Dr. Michael Lehning nor the names of its
33  * contributors may be used to endorse or promote products derived from
34  * this software without specific prior written permission
35  *
36  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
37  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
39  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
40  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
41  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
42  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
43  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
44  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
45  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
46  * POSSIBILITY OF SUCH DAMAGE.
47  *
48  * Authors:
49  * Michael Lehning <michael.lehning@lehning.de>
50  *
51  * Copyright 2019 SICK AG
52  * Copyright 2019 Ing.-Buero Dr. Michael Lehning
53  *
54  */
55 #ifndef __SIM_LOC_UTILS_H_INCLUDED
56 #define __SIM_LOC_UTILS_H_INCLUDED
57 
58 #include <math.h>
59 #include <string>
60 #include <sstream>
61 #include <vector>
62 #include <boost/thread.hpp>
63 
64 #include "sick_scan/ros_wrapper.h"
65 
66 namespace sick_scan
67 {
71  template<typename ElementType, typename MutexType = boost::mutex> class SetGet
72  {
73  public:
74 
76  SetGet(const ElementType & value = ElementType()) : m_value(value) {}
77 
79  void set(const ElementType & value)
80  {
81  boost::lock_guard<MutexType> value_lockguard(m_value_mutex);
82  m_value = value;
83  }
84 
86  ElementType get(void)
87  {
88  boost::lock_guard<MutexType> value_lockguard(m_value_mutex);
89  return m_value;
90  }
91 
92  protected:
93 
95  ElementType m_value;
96  MutexType m_value_mutex;
97  };
98 
102  class SetGet32 : public SetGet<uint32_t>
103  {
104  public:
106  SetGet32(const uint32_t & value = 0) : SetGet<uint32_t,boost::mutex>(value) {}
108  uint32_t inc(void)
109  {
110  boost::lock_guard<boost::mutex> value_lockguard(m_value_mutex);
111  return ++m_value;
112  }
113  };
114 
118  class Utils
119  {
120  public:
121 
127  static std::string toHexString(const std::vector<uint8_t> & binary_data);
128 
134  static std::string toAsciiString(const uint8_t* binary_data, int length);
135 
139  static void flattenString(std::string & s);
140 
144  template <typename T> static std::string flattenToString(const T & x)
145  {
146  std::stringstream s;
147  s << x;
148  std::string out(s.str());
149  flattenString(out);
150  return out;
151  }
152 
154  static std::string flattenToString(const std_msgs::Header* header)
155  {
156  std::stringstream s;
157  s << "header.stamp: " << header->stamp.sec << "." << header->stamp.NSEC;
158  return s.str();
159  }
160 
164  static std::string flattenToString(const sick_scan::SickLocColaTelegramMsg & cola_telegram)
165  {
166  std::stringstream s;
167  s << flattenToString(&cola_telegram.header)
168  << ", command_type: " << cola_telegram.command_type
169  << ", command_name: " << cola_telegram.command_name << ", parameter: [";
170  for(size_t n = 0; n < cola_telegram.parameter.size(); n++)
171  s << (n>0?",":"") << cola_telegram.parameter[n];
172  s << "]";
173  std::string out(s.str());
174  flattenString(out);
175  return out;
176  }
177 
181  static std::string flattenToString(const sick_scan::SickLocResultPortTelegramMsg & telegram)
182  {
183 
184  std::stringstream s;
185  s << flattenToString(&telegram.header)
186  << ", telegram_payload: [posex:" << telegram.telegram_payload.posex << ",posey:" << telegram.telegram_payload.posey << ",poseyaw:" << telegram.telegram_payload.poseyaw
187  << ",scancounter:" << telegram.telegram_payload.scancounter << ",timestamp:" << telegram.telegram_payload.timestamp << ",quality:" << (int)(telegram.telegram_payload.quality&0xFF)
188  << ",covariancex:" << telegram.telegram_payload.covariancex << ",covariancey:" << telegram.telegram_payload.covariancey << ",covarianceyaw:" << telegram.telegram_payload.covarianceyaw
189  << ",outliersratio:" << telegram.telegram_payload.outliersratio << ",errorcode:" << telegram.telegram_payload.errorcode
190  << "], telegram_checksum: " << telegram.telegram_trailer.checksum;
191  std::string out(s.str());
192  flattenString(out);
193  return out;
194  }
195 
201  template <typename T> static bool identicalByStream(const T & x, const T & y)
202  {
203  std::stringstream sx, sy;
204  sx << x;
205  sy << y;
206  return sx.str() == sy.str();
207  }
208 
214  template <typename T> static bool identicalByStream(const std::vector<T> & x, const std::vector<T> & y)
215  {
216  if(x.size() == y.size())
217  {
218  for(size_t n = 0; n < x.size(); n++)
219  if(!identicalByStream(x[n], y[n]))
220  return false;
221  return true;
222  }
223  return false;
224  }
225 
231  static bool identicalByStream(const SickLocResultPortTelegramMsg & x, const SickLocResultPortTelegramMsg & y)
232  {
233  #if defined __ROS_VERSION && __ROS_VERSION == 1
234  return identicalByStream(x.telegram_header, y.telegram_header)
235  && identicalByStream(x.telegram_payload, y.telegram_payload)
236  && identicalByStream(x.telegram_trailer, y.telegram_trailer);
237  #elif defined __ROS_VERSION && __ROS_VERSION == 2
238  return x.telegram_header == y.telegram_header && x.telegram_payload == y.telegram_payload && x.telegram_trailer == y.telegram_trailer;
239  //return x == y;
240  #else
241  return false;
242  #endif
243  }
244 
250  static bool identicalByStream(const SickLocColaTelegramMsg & x, const SickLocColaTelegramMsg & y)
251  {
252  return x.command_name == y.command_name && x.command_type == y.command_type && identicalByStream(x.parameter, y.parameter);
253  }
254 
260  static double normalizeAngle(double angle)
261  {
262  while(angle > M_PI)
263  angle -= (2.0 * M_PI);
264  while(angle < -M_PI)
265  angle += (2.0 * M_PI);
266  return angle;
267  }
268 
269  }; // class Utils
270 
271 } // namespace sick_scan
272 #endif // __SIM_LOC_UTILS_H_INCLUDED
SetGet32(const uint32_t &value=0)
Definition: utils.h:106
static bool identicalByStream(const SickLocResultPortTelegramMsg &x, const SickLocResultPortTelegramMsg &y)
Definition: utils.h:231
static double normalizeAngle(double angle)
Definition: utils.h:260
static bool identicalByStream(const SickLocColaTelegramMsg &x, const SickLocColaTelegramMsg &y)
Definition: utils.h:250
static std::string flattenToString(const T &x)
Definition: utils.h:144
MutexType m_value_mutex
mutex to protect value
Definition: utils.h:96
static std::string flattenToString(const sick_scan::SickLocResultPortTelegramMsg &telegram)
Definition: utils.h:181
SetGet(const ElementType &value=ElementType())
Definition: utils.h:76
std::string toHexString(UINT32 val)
Definition: toolbox.cpp:79
ElementType m_value
protected value
Definition: utils.h:95
uint32_t inc(void)
Definition: utils.h:108
static bool identicalByStream(const T &x, const T &y)
Definition: utils.h:201
static std::string flattenToString(const std_msgs::Header *header)
Definition: utils.h:154
static bool identicalByStream(const std::vector< T > &x, const std::vector< T > &y)
Definition: utils.h:214
TFSIMD_FORCE_INLINE tfScalar length(const Quaternion &q)
static sick_scan::SickScanCommonTcp * s
static std::string flattenToString(const sick_scan::SickLocColaTelegramMsg &cola_telegram)
Definition: utils.h:164


sick_scan
Author(s): Michael Lehning , Jochen Sprickerhof , Martin Günther
autogenerated on Wed Sep 7 2022 02:25:06