include/sick_scansegment_xd/common.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 common.h contains basic and common definition for project sick_scansegment_xd
4  * to support the sick 3D lidar multiScan136.
5  *
6  * Copyright (C) 2020 Ing.-Buero Dr. Michael Lehning, Hildesheim
7  * Copyright (C) 2020 SICK AG, Waldkirch
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  * All rights reserved.
22  *
23  * Redistribution and use in source and binary forms, with or without
24  * modification, are permitted provided that the following conditions are met:
25  *
26  * * Redistributions of source code must retain the above copyright
27  * notice, this list of conditions and the following disclaimer.
28  * * Redistributions in binary form must reproduce the above copyright
29  * notice, this list of conditions and the following disclaimer in the
30  * documentation and/or other materials provided with the distribution.
31  * * Neither the name of SICK AG nor the names of its
32  * contributors may be used to endorse or promote products derived from
33  * this software without specific prior written permission
34  * * Neither the name of Ing.-Buero Dr. Michael Lehning nor the names of its
35  * contributors may be used to endorse or promote products derived from
36  * this software without specific prior written permission
37  *
38  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
39  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
40  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
41  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
42  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
43  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
44  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
45  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
46  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
47  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
48  * POSSIBILITY OF SUCH DAMAGE.
49  *
50  * Authors:
51  * Michael Lehning <michael.lehning@lehning.de>
52  *
53  * Copyright 2020 SICK AG
54  * Copyright 2020 Ing.-Buero Dr. Michael Lehning
55  *
56  */
57 #ifndef __SICK_SCANSEGMENT_XD_COMMON_H
58 #define __SICK_SCANSEGMENT_XD_COMMON_H
59 
60 #define _USE_MATH_DEFINES
61 #include <algorithm>
62 #include <cassert>
63 #include <chrono>
64 #include <cmath>
65 #include <fstream>
66 #include <iomanip>
67 #include <iostream>
68 #include <list>
69 #include <map>
70 #include <string>
71 #include <sstream>
72 #include <thread>
73 #include <vector>
74 
75 #ifdef _MSC_VER
76 # include <conio.h>
77 # include <direct.h>
78 # define KBHIT() ::_kbhit()
79 # define GETCH() ::_getch()
80 # define SPRINTF sprintf_s
81 #else
82 // #include <curses.h>
83 # include <sys/stat.h>
84 # include <sys/types.h>
85 # define localtime_s(a,b) localtime_r(b,a)
86 # define KBHIT() false
87 # define GETCH() 0
88 # define SPRINTF sprintf
89 #endif
90 
91 
92 #define SCANDATA_MSGPACK 1
93 #define SCANDATA_COMPACT 2
94 
95 #if defined __ROS_VERSION && __ROS_VERSION > 1
96 
97 #include <rclcpp/rclcpp.hpp>
98 #include <sensor_msgs/msg/point_cloud2.hpp>
100 typedef rclcpp::Publisher<sensor_msgs::msg::PointCloud2>::SharedPtr PointCloud2MsgPublisher;
102 typedef rclcpp::Publisher<LaserScanMsg>::SharedPtr LaserscanMsgPublisher;
104 typedef rclcpp::Publisher<ImuMsg>::SharedPtr ImuMsgPublisher;
106 typedef rclcpp::Clock rosClock;
107 
108 #elif defined __ROS_VERSION && __ROS_VERSION > 0
109 
110 #include <ros/ros.h>
111 #include <sensor_msgs/PointCloud2.h>
117 typedef ros::Time rosClock;
118 
119 #else
120 
129 typedef int rosQoS;
130 
131 #endif
132 
133 typedef std::chrono::system_clock chrono_system_clock;
134 typedef std::chrono::time_point<std::chrono::system_clock> chrono_system_time;
135 // typedef std::chrono::high_resolution_clock chrono_highres_clock;
136 // typedef std::chrono::time_point<std::chrono::high_resolution_clock> chrono_highres_time;
137 
139 {
140  /*
141  * @brief Returns the duration in seconds
142  */
144  {
145  return (1.0e-9) * (std::chrono::duration_cast<std::chrono::nanoseconds>(timestamp_end - timestamp_start)).count(); // std::chrono::duration::count() in nanoseconds
146  }
147 
148  /*
149  * @brief Formats a number according to formatting options
150  */
151  template<typename T> static std::string FormatNumber(const T& number, int width = -1, bool setfill = false, bool setfixed = false, int precision = -1)
152  {
153  std::stringstream stream;
154  if(width >= 0)
155  stream << std::setw(width);
156  if(setfill)
157  stream << std::setfill('0');
158  if(setfixed)
159  stream << std::fixed;
160  if(precision >= 0)
161  stream << std::setprecision(3);
162  stream << number;
163  return stream.str();
164  }
165 
166  /*
167  * @brief Returns true, if a file can be opened for reading, otherwise false.
168  * @param[in] filename filename incl. path
169  */
170  static bool FileReadable(const std::string& filename)
171  {
172  std::ifstream filestream(filename);
173  bool fileexists = filestream.is_open();
174  filestream.close();
175  return fileexists;
176  }
177 
178  /*
179  * @brief Creates a folder
180  * @param[in] folder directory name incl. path
181  */
182  static bool MkDir(const std::string& folder)
183  {
184  if (!folder.empty() && folder != ".")
185  {
186  std::string path = folder;
187  #ifdef _MSC_VER
188  std::replace(path.begin(), path.end(), '/', '\\');
189  return (::_mkdir(path.c_str()) == 0);
190  #else
191  std::replace(path.begin(), path.end(), '\\', '/');
192  return (mkdir(path.c_str(), 0777) == 0);
193  #endif
194  }
195  return false;
196  }
197 
198  /*
199  * @brief Extracts and returns the name of a file without optional path and extension.
200  * Example: FilenameNoPathNoExtension("../input/example.msg") returns "example"
201  */
202  static std::string FilenameNoPathNoExtension(const std::string& filepath)
203  {
204  size_t sep_pos = filepath.find_last_of("/\\");
205  std::string name = ((sep_pos != std::string::npos) ? (filepath.substr(sep_pos + 1)) : filepath);
206  size_t ext_pos = name.rfind('.');
207  if (ext_pos != std::string::npos)
208  name = name.substr(0, ext_pos);
209  return name;
210  }
211 
212 } // namespace sick_scansegment_xd
213 #endif // __SICK_SCANSEGMENT_XD_COMMON_H
sensor_msgs::Imu
::sensor_msgs::Imu_< std::allocator< void > > Imu
Definition: Imu.h:93
sick_scansegment_xd::MkDir
static bool MkDir(const std::string &folder)
Definition: include/sick_scansegment_xd/common.h:182
ImuMsg
ros_sensor_msgs::Imu ImuMsg
Definition: include/sick_scansegment_xd/common.h:125
ros::Publisher
LaserScanMsg
ros_sensor_msgs::LaserScan LaserScanMsg
Definition: include/sick_scansegment_xd/common.h:123
chrono_system_clock
std::chrono::system_clock chrono_system_clock
Definition: include/sick_scansegment_xd/common.h:133
PointCloud2Msg
ros_sensor_msgs::PointCloud2 PointCloud2Msg
Definition: include/sick_scansegment_xd/common.h:121
PointCloud2MsgPublisher
rosPublisher< PointCloud2Msg > PointCloud2MsgPublisher
Definition: include/sick_scansegment_xd/common.h:122
sick_scansegment_xd
Definition: include/sick_scansegment_xd/common.h:138
sensor_msgs::PointCloud2
::sensor_msgs::PointCloud2_< std::allocator< void > > PointCloud2
Definition: PointCloud2.h:90
sick_scansegment_xd::FormatNumber
static std::string FormatNumber(const T &number, int width=-1, bool setfill=false, bool setfixed=false, int precision=-1)
Definition: include/sick_scansegment_xd/common.h:151
add_sick_scan_base_header.folder
string folder
Definition: add_sick_scan_base_header.py:35
ROS::now
ROS::Time now(void)
Definition: ros_wrapper.cpp:116
imu_timestamp_test.filename
string filename
Definition: imu_timestamp_test.py:69
chrono_system_time
std::chrono::time_point< std::chrono::system_clock > chrono_system_time
Definition: include/sick_scansegment_xd/common.h:134
LaserscanMsgPublisher
rosPublisher< LaserScanMsg > LaserscanMsgPublisher
Definition: include/sick_scansegment_xd/common.h:124
sensor_msgs::LaserScan
::sensor_msgs::LaserScan_< std::allocator< void > > LaserScan
Definition: LaserScan.h:94
api.setup.name
name
Definition: python/api/setup.py:12
rosPublisher< PointCloud2Msg >
PointCloud2.h
multiscan_pcap_player.timestamp_end
float timestamp_end
Definition: multiscan_pcap_player.py:196
ImuMsgPublisher
rosPublisher< ImuMsg > ImuMsgPublisher
Definition: include/sick_scansegment_xd/common.h:126
sick_scansegment_xd::FilenameNoPathNoExtension
static std::string FilenameNoPathNoExtension(const std::string &filepath)
Definition: include/sick_scansegment_xd/common.h:202
sick_scansegment_xd::FileReadable
static bool FileReadable(const std::string &filename)
Definition: include/sick_scansegment_xd/common.h:170
imu_delay_tester.timestamp_start
float timestamp_start
Definition: imu_delay_tester.py:133
rosQoS
int rosQoS
Definition: include/sick_scansegment_xd/common.h:129
sensor_msgs::PointCloud2_
Definition: PointCloud2.h:25
ros::Time
sick_scan_base.h
PointField
ros_sensor_msgs::PointField PointField
Definition: include/sick_scansegment_xd/common.h:127
sensor_msgs::PointField_
Definition: PointField.h:23
sick_scansegment_xd::Seconds
static double Seconds(const chrono_system_time &timestamp_start, const chrono_system_time &timestamp_end=chrono_system_clock::now())
Definition: include/sick_scansegment_xd/common.h:143
rosClock
rosTime rosClock
Definition: include/sick_scansegment_xd/common.h:128


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