-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfake-car.cpp
More file actions
155 lines (132 loc) · 4.1 KB
/
fake-car.cpp
File metadata and controls
155 lines (132 loc) · 4.1 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include "fake-car.h"
#include <fstream>
#include <string>
#include "string-utils.h"
#include "logger.h"
#include <algorithm>
using namespace std;
FakeCar::FakeCar(string recording_file_path):
Car(online=false) {
this->recording_file_path = recording_file_path;
reset();
step();
}
void FakeCar::reset() {
if(dynamics_file.is_open()){
dynamics_file.close();
}
dynamics_file.open(recording_file_path, ios_base::in);
if(dynamics_file.is_open()) {
step();
reset_odometry();
} else {
throw (string) "could not open " + recording_file_path;
}
}
bool FakeCar::step() {
string s;
bool got_td = false;
StampedString ss;
while(got_td == false) {
if(getline(dynamics_file,s,'\n')) {
if(! ss.set_from_string (s)) {
continue;
}
got_td = process_line_from_log(ss);
} else {
return false;
}
}
return got_td;
}
void write_dynamics_csv_from_recording_file(string recording_path, string outpath ){
log_entry_exit w("write_dynamics_csv_from_recording_file");
fstream infile(recording_path);
fstream csv;
csv.open(outpath, ios_base::out);
csv << Dynamics::csv_field_headers() << endl;
string line;
while(getline(infile, line)) {
Dynamics d;
StampedString ss;
if(ss.set_from_string(line)) {
if(Dynamics::from_log_string(d,ss)) {
csv << d.csv_fields() << endl;
}
}
}
}
void write_path_from_recording_file(string recording_path, string outpath) {
log_entry_exit w("write_path_from_recording_file");
const double min_length = 0.03;
FakeCar car(recording_path);
fstream outfile;
outfile.open(outpath, ios_base::out);
string header = join({"secs","x","y","rear_x", "rear_y", "reverse", "heading","adj","esc","str","m/s"});
outfile << header << endl;
auto next = car.current_dynamics;
auto current = car.current_dynamics;
auto start = car.current_dynamics;
Point p = car.get_front_position();
bool reverse = false;
bool next_reverse = false;
unsigned i = 0;
log_info("stepping through recording to create path");
while(car.step()) {
++i;
next = car.current_dynamics;
Point p_next = car.get_front_position();
int wheel_ticks = next.odo_fr_a+next.odo_fr_b - (current.odo_fr_a + current.odo_fr_b);
if (abs(wheel_ticks)>0 ){
next_reverse = wheel_ticks < 0;
} else {
next_reverse = reverse;
}
// skip node if distance threshold hasn't been met
if (distance(p,p_next) < min_length){
continue;
}
reverse = next_reverse;
double d = wheel_ticks * car.meters_per_odometer_tick;
double wheel_meters_per_second = d / ((next.ms - current.ms) / 1000.);
double seconds = (current.ms - start.ms)/1000.;
Point rear_p = car.get_rear_position();
current = next;
outfile << seconds << ","
<< p.x << ","
<< p.y << ","
<< rear_p.x << ","
<< rear_p.y << ","
<< next_reverse << ","
<< car.get_heading().degrees() << ","
<< " 0.0," // was heading adjustment, but we don't use that any more
<< current.rx_esc << ","
<< current.rx_str << ","
<< wheel_meters_per_second << endl;
p = p_next;
}
outfile.flush();
outfile.close();
}
#include "route.h"
void test_fake_car() {
string recording_path = "/home/brian/Desktop/Dropbox/car/tracks/avc/routes/S/recording.csv";
string route_path = "../../cpp/test_data/2/test_route.csv";
cout << "writing path " << route_path << " from recording " << recording_path << endl;
write_path_from_recording_file(recording_path, route_path);
cout << "done" << endl;
cout << "verifying" << endl;
Route r;
r.load_from_file(route_path);
cout << "max velocity before optimization " << r.get_max_velocity() << endl;
r.optimize_velocity();
cout << "max velocity after optimization " << r.get_max_velocity() << endl;
cout << "optimize done" << endl;
FakeCar car(recording_path);
cout << "processing" << endl;
int steps = 0;
while(car.step()) {
steps++;
}
cout << "processed " << steps << " steps to arrive at x:" << car.ackerman.x << " y: " << car.ackerman.y << endl;
}