/
cartelemetry.h
76 lines (67 loc) · 1.87 KB
/
cartelemetry.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#ifndef _CARTELEMETRY_H
#define _CARTELEMETRY_H
#include <fstream>
class CARTELEMETRY
{
private:
typedef double T;
std::vector <std::pair <std::string, T> > variable_names;
T time;
bool wroteheader;
const std::string telemetryname;
std::ofstream file;
void WriteHeader(const std::string & filename)
{
std::ofstream f((filename+".plt").c_str());
if (f)
{
f << "plot ";
unsigned int count = 0;
for (std::vector <std::pair <std::string, T> >::iterator i =
variable_names.begin(); i != variable_names.end(); ++i)
{
f << "\\" << std::endl << "\"" << filename+".dat" << "\" u 1:" << count+2 << " t '" << i->first << "' w lines";
if (count < variable_names.size()-1)
f << ",";
f << " ";
count++;
}
f << std::endl;
}
wroteheader = true;
}
public:
CARTELEMETRY(const std::string & name) : time(0), wroteheader(false), telemetryname(name), file((name+".dat").c_str()) {}
CARTELEMETRY(const CARTELEMETRY & other) : variable_names(other.variable_names), time(other.time), wroteheader(other.wroteheader), telemetryname(other.telemetryname), file((telemetryname+".dat").c_str()) {}
void AddRecord(const std::string & name, T value)
{
bool found = false;
for (std::vector <std::pair <std::string, T> >::iterator i =
variable_names.begin(); i != variable_names.end(); ++i)
{
if (name == i->first)
{
i->second = value;
found = true;
break;
}
}
if (!found)
variable_names.push_back(std::make_pair(name, value));
}
void Update(T dt)
{
if (time != 0 && !wroteheader)
WriteHeader(telemetryname);
time += dt;
if (file)
{
file << time << " ";
for (std::vector <std::pair <std::string, T> >::iterator i =
variable_names.begin(); i != variable_names.end(); ++i)
file << i->second << " ";
file << "\n";
}
}
};
#endif //_CARTELEMETRY_H