tCanMessageMetaDumper.cpp
Go to the documentation of this file.
1 // -- BEGIN LICENSE BLOCK ----------------------------------------------
2 // This file is part of FZIs ic_workspace.
3 //
4 // This program is free software licensed under the LGPL
5 // (GNU LESSER GENERAL PUBLIC LICENSE Version 3).
6 // You can find a copy of this license in LICENSE folder in the top
7 // directory of the source code.
8 //
9 // © Copyright 2016 FZI Forschungszentrum Informatik, Karlsruhe, Germany
10 //
11 // -- END LICENSE BLOCK ------------------------------------------------
12 
13 // ----------------------------------------------------------
14 /*
15  * tCanMessageMetaDumper.cpp
16  * <section>
17  *
18  * Created by Hugo Ritzkowski on 24.01.2012.
19  * Copyright 2012
20  * Company Forschungszentrum Informatik (FZI), Abteilung IDS.
21  * All rights reserved.
22  *
23  */
24 // ----------------------------------------------------------
34 // ----------------------------------------------------------
35 
36 #include "tCanMessageMetaDumper.h"
37 
39 #include "icl_core_config/Config.h"
40 #include <stdio.h>
41 
42 namespace icl_hardware {
43 
44 namespace can {
45 
47  : m_baudrate(0),
48  m_data_file_name(0)
49 {
50 
51 }
52 
54 {
55 
56 }
57 
58 void tCanMessageMetaDumper::open(const std::string identifier, const int baudrate)
59 {
60  if (identifier != "")
61  {
62  setDataFileName(boost::filesystem::path(identifier));
63  }
64  else
65  {
66  LOGGING_ERROR(icl_hardware::can::CAN, "No data file provided!" << icl_core::logging::endl);
67  return;
68  }
69 
70  if (baudrate != 0)
71  {
72  m_baudrate = baudrate;
73  }
74  else
75  {
76  LOGGING_ERROR(icl_hardware::can::CAN, "No baudrate provided!" << icl_core::logging::endl);
77  return;
78  }
79 
80  int ret=prepareFile();
81 
82  if (ret!=0)
83  {
84  LOGGING_ERROR(icl_hardware::can::CAN, "Failed to open file for recording!" << icl_core::logging::endl);
85  return;
86  }
87 }
88 
90 {
91  std::cout << "</record>" << std::endl;
92  if (m_data_file.is_open())
93  {
94  m_data_file.close();
95  }
96 }
97 
105 {
106  std::string line;
107  unsigned int line_counter = 0;
108  unsigned int next_commit = 1;
109 
110  //print header
111  std::cout << "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>" << std::endl;
112  std::cout << "<record version=\"" << m_major_fileversion << "." << m_minor_fileversion << "\" baudrate=\"" << m_baudrate << "\" buffer=\"" << getDataFileName().string() << "\" >" << std::endl;
113 
114  //read through file, save every m_baudrate line the frame number, timestamp and fileposition
115  double last_timestamp = -1.;
116  while(m_data_file.good())
117  {
118  getline(m_data_file, line);
119 
120  if (line.length() == 0 || line.substr(0,1) == "#")
121  {
122  //empty line or comment
123  continue;
124  }
125  ++line_counter;
126 
127  // assure that time sequence of can file in order
128  double timestamp = atof((line.substr(0, line.find(' ', 0))).data());
129  if (last_timestamp > timestamp)
130  {
131  if (!icl_core::config::getDefault<bool>("/icl_hardware_can/force_meta_dump", false))
132  {
133  LOGGING_ERROR(icl_hardware::can::CAN, "Time sequence of can file out of order, line number: " << line_counter << "" << icl_core::logging::endl);
134  return false;
135  }
136  else
137  {
138  std::cout << "<!-- Warning: Time sequence of can file out of order -->" << std::endl;
139  }
140  }
141 
142  if (next_commit == line_counter)
143  {
144  unsigned int time_sec = static_cast<int>(timestamp);
145 
146  std::cout << "<frame number=\"" << line_counter;
147  std::cout << "\" timesec=\"" << time_sec;
148  std::cout << "\" timeusec=\"" << static_cast<int>((timestamp - time_sec)*10000000);
149  std::cout << "\" filepos=\"" << m_data_file.tellg() << "\">";
150  std::cout << " </frame>" << std::endl;
151  next_commit += m_baudrate;
152  }
153  last_timestamp = timestamp;
154  }
155 
156  if (line_counter < 1)
157  {
158  return false;
159  }
160  else
161  {
162  return true;
163  }
164 }
165 
167 {
168  m_data_file.open(getDataFileName().string().c_str(), std::ios::in);
169 
170  if (!m_data_file.is_open())
171  {
172  return -1;
173  }
174 
175  return 0;
176 }
177 
178 void tCanMessageMetaDumper::setDataFileName(const boost::filesystem::path &file_name)
179 {
180  if (m_data_file_name)
181  {
182  delete m_data_file_name;
183  }
184  m_data_file_name = new boost::filesystem::path(file_name);
185 }
186 
187 const boost::filesystem::path & tCanMessageMetaDumper::getDataFileName() const
188 {
189  return *m_data_file_name;
190 }
191 
192 }
193 
194 }
void setDataFileName(const boost::filesystem::path &file_name)
void open(const std::string identifier="", const int baudrate=0)
const boost::filesystem::path & getDataFileName() const
#define LOGGING_ERROR(streamname, arg)
ThreadStream & endl(ThreadStream &stream)
This modul gets a file with can-messages and constructs the adequate xml-file to it. Its data is written to the console.


fzi_icl_can
Author(s):
autogenerated on Mon Jun 10 2019 13:17:02