-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestCar.hpp
199 lines (162 loc) · 4.62 KB
/
testCar.hpp
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#ifndef TESTCAR_HPP
#define TESTCAR_HPP
#include <iostream>
#include <cmath>
#include <string>
#include <vector>
#include <numeric>
#include <fstream>
#include <algorithm>
#include "PID.hpp"
using namespace std;
class Car
{
public:
Car(double initialSpeed, double initialPosition)
: speed(initialSpeed), position(initialPosition) {}
void updatePosition(double timeInterval)
{
position += speed * timeInterval;
}
void changeSpeed(double newSpeed)
{
speed = newSpeed;
}
double getPosition() const
{
return position;
}
double getSpeed() const
{
return speed;
}
double speed;
double position;
};
void pidDebug(string message)
{
cout << message;
}
double sum_last_squared(const std::vector<double> &v, double prop)
{
if (v.empty())
return 0.0;
size_t n = v.size();
size_t count = static_cast<size_t>(std::ceil(n * prop));
size_t start_index = n - count;
double sum_of_squares = std::accumulate(v.begin() + start_index, v.end(), 0.0,
[](double sum, double value) {
return sum + value * value;
});
return sum_of_squares;
}
vector<double> velocities(int steps)
{
vector<double> velocities(steps, 0.0);
double acceleration = 1;
double error = 0.2;
int phase_length = steps / 6;
double velocity = 0.0;
for (int i = 0; i < phase_length; ++i)
{
velocity += acceleration + ((i % 3 == 0) ? error : ((i % 3 == 1) ? (-error) : 0));
velocities[i] = velocity;
}
for (int i = phase_length; i < 2 * phase_length; ++i)
{
velocity += ((i % 3 == 0) ? error : ((i % 3 == 1) ? (-error) : 0));
velocities[i] = velocity;
}
for (int i = 2 * phase_length; i < 4 * phase_length; ++i)
{
velocity -= acceleration + ((i % 3 == 0) ? error : ((i % 3 == 1) ? (-error) : 0));
velocities[i] = velocity;
}
for (int i = 4 * phase_length; i < 5 * phase_length; ++i)
{
velocity += acceleration + ((i % 3 == 0) ? error : ((i % 3 == 1) ? (-error) : 0));
velocities[i] = velocity;
}
for (int i = 5 * phase_length; i < steps; ++i)
{
if (ABS(velocity) >= (error * (steps - i)))
{
velocity += COPYSIGN(velocity, error);
}
else
{
velocity += ((i % 3 == 0) ? error : ((i % 3 == 1) ? (-error) : 0));
}
velocities[i] = velocity;
}
return velocities;
}
vector<double> test(double maxIntTm, double maxAmp, double minKp, double maxKp, double rTiM, double TdM, double TddM, double eDPm, double eDPa, unsigned long session, double Kp, double preE = 0, unsigned long preT = 0, double timeInterval = 10, int steps = 50, bool debug = false)
{
double leaderInitialSpeed = 0;
double followerInitialSpeed = 0;
double initialDistance = 0;
vector<double> vec;
vector<double> vol = velocities(steps);
Car leader(leaderInitialSpeed, initialDistance);
Car follower(followerInitialSpeed, 0);
PID pid = PID(maxIntTm, maxAmp, minKp, maxKp, rTiM, TdM, TddM, eDPm, eDPa, session, Kp, preE, preT);
cout << fixed << setprecision(4);
for (int i = 0; i < steps; i++)
{
leader.changeSpeed(leader.getSpeed() + vol[i]);
leader.updatePosition(timeInterval);
follower.updatePosition(timeInterval);
double distanceToLeader = leader.getPosition() - follower.getPosition();
vec.push_back(distanceToLeader);
string* ptr = nullptr;
if (debug) {
cout << "TimeStep: " << i << ", distanceToLeader: " << distanceToLeader << ", FollowerSpeed: " << follower.getSpeed() << ", LeaderSpeed: " << leader.getSpeed() << "\n";
ptr = new string;
}
double fS = pid.update(distanceToLeader, i * timeInterval, ptr);
follower.changeSpeed(follower.getSpeed() + MAX(-511, MIN(fS, 511)));
if (debug) {
cout << *ptr;
delete ptr;
}
}
return vec;
}
struct pidTest
{
double maxIntTm, maxAmp, minKp, maxKp, rTiM, TdM, TddM, eDPm, eDPa;
unsigned long session;
double Kp, result;
};
bool write_results(const vector<pidTest> &data, const string &filename)
{
auto sorted_data = data;
sort(sorted_data.begin(), sorted_data.end(), [](const pidTest &a, const pidTest &b) {
return a.result < b.result;
});
ofstream outfile(filename);
if (!outfile)
{
cerr << "Error opening file for writing." << endl;
return 0;
}
outfile << "maxIntTm,maxAmp,minKp,maxKp,rTiM,TdM,TddM,eDPm,eDPa,session,Kp,result\n";
for (size_t i = 0; i < sorted_data.size(); ++i)
{
const pidTest &pt = sorted_data[i];
outfile << pt.maxIntTm << "," << pt.maxAmp << "," << pt.minKp << "," << pt.maxKp << ","
<< pt.rTiM << "," << pt.TdM << "," << pt.TddM << "," << pt.eDPm << "," << pt.eDPa << ","
<< pt.session << "," << pt.Kp << "," << pt.result << "\n";
}
outfile.close();
cout << "Data written to " << filename << endl;
return 1;
}
struct ParameterRange
{
double start;
double end;
double step;
};
#endif