SPIDAR API Library  0x16033101
Space Interface Device for Artificial Reality
DataLogger.hpp
Go to the documentation of this file.
1 
5 #ifndef SPIDAR_WIN32_DATA_LOGGER_HPP
6 #define SPIDAR_WIN32_DATA_LOGGER_HPP
7 
8 #include <windows.h>
9 #include <process.h>
10 
11 #include <cstdint>
12 #include <string>
13 #include <fstream>
14 #include <iomanip>
15 #include <vector>
16 
17 namespace Spidar
18 {
19 namespace Win32
20 {
21 
26 template <class T1, class T2, class T3>
27 struct LogData
28 {
29  typedef T1 ValueType;
30  typedef T2 VectorType;
31  typedef T3 QuaternionType;
32 
33  VectorType position;
34  VectorType velocity;
35 
36  VectorType targetForce;
37  VectorType resultantForce;
38 
39  QuaternionType rotation;
40  VectorType angularVelocity;
41 
42  VectorType targetTorque;
43  VectorType resultantTorque;
44 
45  ValueType tension[8];
46 
48  static bool outputLabel(std::ofstream& fout)
49  {
50  if (!fout.is_open()) return false;
51 
52  fout << "position x \t" << "position y \t" << "position z \t";
53 
54  fout << "velocity x \t" << "velocity y \t" << "velocity z \t";
55 
56  fout << "target Force x \t" << "target Force y \t" << "target Force z \t";
57  fout << "resultant Force x \t" << "resultant Force y \t" << "resultant Force z \t";
58 
59  fout << "rotation x \t" << "rotation y \t" << "rotation z \t" << "rotation w \t";
60  fout << "angular velocity x \t" << "angular velocity y \t" << "angular velocity z \t";
61 
62  fout << "target Torque x \t" << "target Torque y \t" << "target Torque z \t";
63  fout << "resultant Torque x \t" << "resultant Torque y \t" << "resultant Torque z \t";
64 
65  for (size_t i=0; i<8; ++i)
66  {
67  fout << "tension " << i << "\t";
68  }
69  return true;
70  }
71 
73  bool outputData(std::ofstream& fout)
74  {
75  if (!fout.is_open()) return false;
76 
77  fout << position.x() << "\t" << position.y() << "\t" << position.z() << "\t";
78  fout << velocity.x() << "\t" << velocity.y() << "\t" << velocity.z() << "\t";
79 
80  fout << targetForce.x() << "\t" << targetForce.y() << "\t" << targetForce.z() << "\t";
81  fout << resultantForce.x() << "\t" << resultantForce.y() << "\t" << resultantForce.z() << "\t";
82 
83  fout << rotation.x << "\t" << rotation.y << "\t" << rotation.z << "\t" << rotation.w << "\t";
84  fout << angularVelocity.x() << "\t" << angularVelocity.y() << "\t" << angularVelocity.z() << "\t";
85 
86  fout << targetTorque.x() << "\t" << targetTorque.y() << "\t" << targetTorque.z() << "\t";
87  fout << resultantTorque.x() << "\t" << resultantTorque.y() << "\t" << resultantTorque.z() << "\t";
88 
89  for (size_t i=0; i<8; ++i)
90  {
91  fout << tension[i] << "\t";
92  }
93  return true;
94  }
95 };
96 
101 template <class T>
103 {
104 public:
105  typedef T DataType;
106 
107  DataLogger(size_t size=10000);
108  ~DataLogger(void) {};
109 
110  bool started(void) const { return started_; }
111 
112  void start(void);
113  void add(const DataType& data);
114  void output(const std::string& filename);
115 
116 private:
117  std::vector<LARGE_INTEGER> timeStamp_;
118  std::vector<DataType> logData_;
119 
120  LARGE_INTEGER frequency_;
121  LARGE_INTEGER start_;
122 
123  bool started_;
124  bool busy_;
125 
126 }; // end of class DataLogger.
127 
132 template <class T>
133 DataLogger<T>::DataLogger(size_t size) : started_(false), busy_(false)
134 {
135  logData_.reserve(size);
136  timeStamp_.reserve(size);
137 
138  ::QueryPerformanceFrequency(&frequency_);
139 }
140 
145 template <class T>
147 {
148  started_ = false;
149 
150  logData_.clear();
151  timeStamp_.clear();
152  ::QueryPerformanceCounter(&start_);
153 
154  started_ = true;
155 }
156 
161 template <class T>
162 void DataLogger<T>::add(const DataType& data)
163 {
164  if (!started_ || busy_) return;
165 
166  LARGE_INTEGER time;
167  ::QueryPerformanceCounter(&time);
168 
169  timeStamp_.push_back(time);
170  logData_.push_back(data);
171 }
172 
177 template <class T>
178 void DataLogger<T>::output(const std::string& filename)
179 {
180  busy_ = true;
181 
182  if (logData_.size()!=timeStamp_.size()) return;
183 
184  std::ofstream fout;
185 
186  fout.open(filename, std::ios::out);
187 
188  // データラベルの出力
189  fout << "time [ms]\t";
190 
191  DataType::outputLabel(fout);
192 
193  fout << std::endl;
194 
195  // データの出力
196  for (size_t i=0; i<logData_.size(); ++i)
197  {
198  double time = static_cast<double>(timeStamp_[i].QuadPart - start_.QuadPart) / frequency_.QuadPart * 1000.0;
199 
200  fout << std::setprecision(10) << time << "\t";
201 
202  logData_[i].outputData(fout);
203 
204  fout << std::endl;
205  }
206 
207  fout.close();
208 
209  busy_ = false;
210 }
211 
212 } // end of namespace Win32.
213 } // end of namespace Spidar.
214 
215 #endif // SPIDAR_WIN32_DATA_LOGGER_HPP
216 
217 // end of file.
bool outputData(std::ofstream &fout)
ログデータを出力します.
Definition: DataLogger.hpp:73
ログデータ収集クラスです.
Definition: DataLogger.hpp:102
VectorType velocity
速度 [m/s]
Definition: DataLogger.hpp:34
VectorType resultantForce
並進力の出力値 [N]
Definition: DataLogger.hpp:37
VectorType angularVelocity
角速度 [rad/s]
Definition: DataLogger.hpp:40
VectorType targetForce
並進力の目標値 [N]
Definition: DataLogger.hpp:36
void output(const std::string &filename)
ログデータをファイル出力します.
Definition: DataLogger.hpp:178
void add(const DataType &data)
ログデータを1件追加します.
Definition: DataLogger.hpp:162
static bool outputLabel(std::ofstream &fout)
ログデータのラベルを出力します.
Definition: DataLogger.hpp:48
ValueType tension[8]
ワイヤの張力 [N]
Definition: DataLogger.hpp:45
T1 ValueType
値の型
Definition: DataLogger.hpp:29
DataLogger(size_t size=10000)
コンストラクタ.
Definition: DataLogger.hpp:133
SPIDARライブラリのルート名前空間です.
VectorType targetTorque
回転力の目標値 [Nm]
Definition: DataLogger.hpp:42
VectorType resultantTorque
回転力の出力値 [Nm]
Definition: DataLogger.hpp:43
QuaternionType rotation
回転
Definition: DataLogger.hpp:39
void start(void)
ログデータの受け入れを開始します.
Definition: DataLogger.hpp:146
T2 VectorType
ベクトルの型
Definition: DataLogger.hpp:30
ログデータ用の構造体です.
Definition: DataLogger.hpp:27
T3 QuaternionType
四元数の型
Definition: DataLogger.hpp:31
VectorType position
位置 [m]
Definition: DataLogger.hpp:33