raw_data_pa.cpp
Go to the documentation of this file.
1 //==============================================================================
2 // Copyright (c) 2019, Peter Weissig, TU Chemnitz
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are met:
7 // * Redistributions of source code must retain the above copyright
8 // notice, this list of conditions and the following disclaimer.
9 // * Redistributions in binary form must reproduce the above copyright
10 // notice, this list of conditions and the following disclaimer in the
11 // documentation and/or other materials provided with the distribution.
12 // * Neither the name of the Flight Systems and Automatic Control group,
13 // TU Darmstadt, nor the names of its contributors may be used to
14 // endorse or promote products derived from this software without
15 // specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 // AND
19 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
22 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 //==============================================================================
29 
30 // This file is an addon from TUC-ProAut (https://github.com/TUC-ProAut/).
31 // It was created to log all data from the ublox to a logfile and to publish
32 // the data as ros messages. This is used by our group to also evaluate the
33 // measured data with the rtklib.
34 
35 #include "ublox_gps/raw_data_pa.h"
36 #include <cmath>
37 #include <string>
38 #include <sstream>
39 
40 #include <sys/types.h>
41 #include <sys/stat.h>
42 #include <time.h>
43 
44 using namespace ublox_node;
45 
46 //
47 // ublox_node namespace
48 //
49 
50 RawDataStreamPa::RawDataStreamPa(bool is_ros_subscriber) :
51  pnh_(ros::NodeHandle("~")),
52  flag_publish_(false),
53  is_ros_subscriber_(is_ros_subscriber) {
54 
55 }
56 
58 
59  if (is_ros_subscriber_) {
60  pnh_.param<std::string>("dir", file_dir_, "");
61  } else {
62  pnh_.param<std::string>("raw_data_stream/dir", file_dir_, "");
63  pnh_.param("raw_data_stream/publish", flag_publish_, false);
64  }
65 }
66 
68 
69  if (is_ros_subscriber_) {
70  return !file_dir_.empty();
71  } else {
72  return flag_publish_ || (!file_dir_.empty());
73  }
74 }
75 
76 
78 
79  if (is_ros_subscriber_) {
80  ROS_INFO("Subscribing to raw data stream.");
81  static ros::Subscriber subscriber =
82  nh_.subscribe ("raw_data_stream", 100,
84  } else if (flag_publish_) {
85  ROS_INFO("Publishing raw data stream.");
86  RawDataStreamPa::publishMsg(std::string());
87  }
88 
89  if (!file_dir_.empty()) {
90  struct stat stat_info;
91  if (stat(file_dir_.c_str(), &stat_info ) != 0) {
92  ROS_ERROR("Can't log raw data to file. "
93  "Directory \"%s\" does not exist.", file_dir_.c_str());
94 
95  } else if ((stat_info.st_mode & S_IFDIR) != S_IFDIR) {
96  ROS_ERROR("Can't log raw data to file. "
97  "\"%s\" exists, but is not a directory.", file_dir_.c_str());
98 
99  } else {
100  if (file_dir_.back() != '/') {
101  file_dir_ += '/';
102  }
103 
104  time_t t = time(NULL);
105  struct tm time_struct = *localtime(&t);
106 
107  std::stringstream filename;
108  filename.width(4); filename.fill('0');
109  filename << time_struct.tm_year + 1900;
110  filename.width(0); filename << '_';
111  filename.width(2); filename.fill('0');
112  filename << time_struct.tm_mon + 1;
113  filename.width(0); filename << '_';
114  filename.width(2); filename.fill('0');
115  filename << time_struct.tm_mday;
116  filename.width(0); filename << '_';
117  filename.width(2); filename.fill('0');
118  filename << time_struct.tm_hour;
119  filename.width(2); filename.fill('0');
120  filename << time_struct.tm_min ;
121  filename.width(0); filename << ".log";
122  file_name_ = file_dir_ + filename.str();
123 
124  try {
125  file_handle_.open(file_name_);
126  ROS_INFO("Logging raw data to file \"%s\"",
127  file_name_.c_str());
128  } catch(const std::exception& e) {
129  ROS_ERROR("Can't log raw data to file. "
130  "Can't create file \"%s\".", file_name_.c_str());
131  }
132  }
133  }
134 }
135 
136 void RawDataStreamPa::ubloxCallback(const unsigned char* data,
137  const std::size_t size) {
138 
139  std::string str((const char*) data, size);
140 
141  if (flag_publish_) {
142  publishMsg(str);
143  }
144 
145  saveToFile(str);
146 }
147 
149  const std_msgs::UInt8MultiArray::ConstPtr& msg) {
150 
151  std::string str(msg->data.size(), ' ');
152  std::copy(msg->data.begin(), msg->data.end(), str.begin());
153  saveToFile(str);
154 }
155 
156 std_msgs::UInt8MultiArray RawDataStreamPa::str2uint8(
157  const std::string str) {
158 
159  std_msgs::UInt8MultiArray msg;
160 
161  msg.layout.data_offset = 0;
162  msg.layout.dim.push_back(std_msgs::MultiArrayDimension());
163  msg.layout.dim[0].size = str.length();
164  msg.layout.dim[0].stride = 1;
165  msg.layout.dim[0].label = "raw_data_stream";
166 
167  msg.data.resize(str.length());
168  std::copy(str.begin(), str.end(), msg.data.begin());
169 
170  return msg;
171 }
172 
173 void RawDataStreamPa::publishMsg(const std::string str) {
174 
175  static ros::Publisher publisher =
176  pnh_.advertise<std_msgs::UInt8MultiArray>("raw_data_stream", 100);
177 
178  publisher.publish(RawDataStreamPa::str2uint8(str));
179 }
180 
181 void RawDataStreamPa::saveToFile(const std::string str) {
182 
183  if (file_handle_.is_open()) {
184  try {
185  file_handle_ << str;
186  // file_handle_.flush();
187  } catch(const std::exception& e) {
188  ROS_WARN("Error writing to file \"%s\"", file_name_.c_str());
189  }
190  }
191 }
192 
msg
filename
void publish(const boost::shared_ptr< M > &message) const
void ubloxCallback(const unsigned char *data, const std::size_t size)
Callback function which handles raw data.
void saveToFile(const std::string str)
Stores data to given file.
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
std::string file_dir_
Directoy name for storing raw data.
Definition: raw_data_pa.h:122
bool flag_publish_
Flag for publishing raw data.
Definition: raw_data_pa.h:129
std_msgs::UInt8MultiArray str2uint8(const std::string str)
Converts a string into an uint8 multibyte array.
#define ROS_WARN(...)
ros::NodeHandle nh_
ROS node handle (only for subscriber)
Definition: raw_data_pa.h:139
void initialize(void)
Initializes raw data streams If storing to file is enabled, the filename is created and the correspon...
Definition: raw_data_pa.cpp:77
std::ofstream file_handle_
Handle for file access.
Definition: raw_data_pa.h:126
#define ROS_INFO(...)
bool param(const std::string &param_name, T &param_val, const T &default_val) const
Publisher advertise(const std::string &topic, uint32_t queue_size, bool latch=false)
ros::NodeHandle pnh_
ROS private node handle (for params and publisher)
Definition: raw_data_pa.h:137
RawDataStreamPa(bool is_ros_subscriber=false)
Constructor. Initialises variables and the nodehandle.
Definition: raw_data_pa.cpp:50
void publishMsg(const std::string str)
Publishes data stream as ros message.
std::string file_name_
Filename for storing raw data.
Definition: raw_data_pa.h:124
void msgCallback(const std_msgs::UInt8MultiArray::ConstPtr &msg)
Callback function which handles raw data.
bool isEnabled(void)
Returns the if raw data streaming is enabled.
Definition: raw_data_pa.cpp:67
void getRosParams(void)
Get the raw data stream parameters.
Definition: raw_data_pa.cpp:57
#define ROS_ERROR(...)


ublox_gps
Author(s): Johannes Meyer
autogenerated on Thu Jan 28 2021 03:13:52