EEROS  1.0.0.0
API for the EEROS Real-Time Robotics Framework
Trace.hpp
Go to the documentation of this file.
1 #ifndef ORG_EEROS_CONTROL_TRACE_HPP_
2 #define ORG_EEROS_CONTROL_TRACE_HPP_
3 
4 #include <vector>
5 #include <iostream>
6 #include <fstream>
7 #include <unistd.h>
9 #include <eeros/core/Thread.hpp>
10 #include <eeros/logger/Logger.hpp>
11 
12 #include <time.h>
13 
14 namespace eeros {
15  namespace control {
16 
17  template < typename T = double >
18  class Trace : public Block1i<T> {
19  public:
20  Trace(uint32_t bufLen) : maxBufLen(bufLen) {
21  this->buf = new T[bufLen];
22  this->timeBuf = new timestamp_t[bufLen];
23  }
24 
25  virtual void run() {
26  if (running) {
27  buf[index] = this->in.getSignal().getValue();
28  timeBuf[index] = this->in.getSignal().getTimestamp();
29  index++;
30  if (index == maxBufLen) {
31  index = 0;
32  cycle = true;
33  }
34  }
35  }
36  virtual T* getTrace() {
37  if (cycle) {
38  size = maxBufLen;
39  T* tmp = new T[maxBufLen];
40  for (int i = 0; i < maxBufLen; i++)
41  tmp[i] = buf[(i + index) % maxBufLen];
42  return tmp;
43  } else {
44  T* tmp = new T[index];
45  size = index;
46  for (int i = 0; i < index; i++)
47  tmp[i] = buf[i];
48  return tmp;
49  }
50  }
52  if (cycle) {
53  size = maxBufLen;
54  timestamp_t* tmp = new timestamp_t[maxBufLen];
55  for (int i = 0; i < maxBufLen; i++)
56  tmp[i] = timeBuf[(i + index) % maxBufLen];
57  return tmp;
58  } else {
59  size = index;
60  timestamp_t* tmp = new timestamp_t[index];
61  for (int i = 0; i < index; i++)
62  tmp[i] = timeBuf[i];
63  return tmp;
64  }
65  }
66  virtual uint32_t getSize() {return size;}
67  virtual void enable() {running = true;}
68  virtual void disable() {running = false;}
69 
70  uint32_t maxBufLen; // total size of buffer
71 
72  protected:
73  uint32_t size; // size to which the buffer is filled
74  uint32_t index = 0; // current index
75  bool cycle = false; // indicates whether wrap around occured
76  bool running = false; // indicates whether trace runs
77  T* buf;
79  };
80 
81  /********** Print functions **********/
82  template <typename T>
83  std::ostream& operator<<(std::ostream& os, Trace<T>& trace) {
84  os << "Block trace: '" << trace.getName() << "'";
85  }
86 
87 
88  template < typename T = double >
89  class TraceWriter : public eeros::Thread {
90  public:
91  explicit TraceWriter(Trace<T>& trace, std::string fileName) : trace(trace), name(fileName) { }
92  ~TraceWriter() {running = false;}
93  void write() {go = true;}
94 
95  private:
96  bool running = false, go = false;
97  virtual void run() {
98  running = true;
99  while(running) {
100  while(running && !go) usleep(1000);
101  if (!running) return;
102  go = false;
103  log.info() << "start writing trace file " + name;
104  std::ofstream file;
105 
106  time_t now = time(0);
107  struct tm tstruct;
108  char chbuf[80];
109  tstruct = *localtime(&now);
110  strftime(chbuf, sizeof(chbuf), "_%Y-%m-%d_%X", &tstruct);
111 
112  file.open(name + chbuf, std::ios::trunc);
113  timestamp_t* timeStampBuf = trace.getTimestampTrace();
114  T* buf = trace.getTrace();
115  file << "name = " << trace.getName() << ", size = " << trace.getSize() << ", maxBufLen = " << trace.maxBufLen << "\n";
116  for (int i = 0; i < trace.getSize(); i++) file << timeStampBuf[i] << " " << buf[i] << std::endl;
117  file.close();
118  log.info() << "trace file written";
119  }
120  }
121  std::string name;
122  Trace<T>& trace;
124  };
125 
126  };
127 };
128 
129 #endif /* ORG_EEROS_CONTROL_TRACE_HPP_ */
virtual void enable()
Definition: Trace.hpp:67
Definition: Logger.hpp:15
bool cycle
Definition: Trace.hpp:75
void write()
Definition: Trace.hpp:93
virtual void run()
Definition: Trace.hpp:25
Definition: Trace.hpp:18
virtual void disable()
Definition: Trace.hpp:68
T * buf
Definition: Trace.hpp:77
Definition: Block1i.hpp:12
uint32_t maxBufLen
Definition: Trace.hpp:70
Definition: Config.hpp:14
~TraceWriter()
Definition: Trace.hpp:92
Trace(uint32_t bufLen)
Definition: Trace.hpp:20
timestamp_t * timeBuf
Definition: Trace.hpp:78
TraceWriter(Trace< T > &trace, std::string fileName)
Definition: Trace.hpp:91
virtual timestamp_t * getTimestampTrace()
Definition: Trace.hpp:51
Definition: Thread.hpp:12
uint32_t index
Definition: Trace.hpp:74
bool running
Definition: Trace.hpp:76
uint32_t size
Definition: Trace.hpp:73
void endl(LogWriter &w)
Definition: RecordWriter.hpp:19
virtual uint32_t getSize()
Definition: Trace.hpp:66
Input< T > in
Definition: Block1i.hpp:21
Definition: Trace.hpp:89
uint64_t timestamp_t
Definition: types.hpp:12
virtual T * getTrace()
Definition: Trace.hpp:36