-
Notifications
You must be signed in to change notification settings - Fork 87
Expand file tree
/
Copy pathchronos.cpp
More file actions
309 lines (254 loc) · 8.04 KB
/
chronos.cpp
File metadata and controls
309 lines (254 loc) · 8.04 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
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
#include "chronos.h"
#include "utility.h"
#include <QDebug>
#include <cmath>
#include <QDateTime>
Chronos::Chronos(QObject *parent) : QObject(parent)
{
mPacket = 0;
mChronos = new ChronosComm(this);
mStartTimer = new QTimer(this);
mIsArmed = false;
mIsStarted = false;
mHeabPollCnt = 0;
connect(mStartTimer, SIGNAL(timeout()),
this, SLOT(startTimerSlot()));
connect(mChronos, SIGNAL(connectionChanged(bool,QString)),
this, SLOT(connectionChanged(bool,QString)));
connect(mChronos, SIGNAL(trajRx(chronos_traj)),
this, SLOT(processTraj(chronos_traj)));
connect(mChronos, SIGNAL(heabRx(chronos_heab)),
this, SLOT(processHeab(chronos_heab)));
connect(mChronos, SIGNAL(osemRx(chronos_osem)),
this, SLOT(processOsem(chronos_osem)));
connect(mChronos, SIGNAL(ostmRx(chronos_ostm)),
this, SLOT(processOstm(chronos_ostm)));
connect(mChronos, SIGNAL(strtRx(chronos_strt)),
this, SLOT(processStrt(chronos_strt)));
connect(mChronos, SIGNAL(oproRx(chronos_opro)),
this, SLOT(processOpro(chronos_opro)));
}
bool Chronos::startServer(PacketInterface *packet, QHostAddress addr)
{
mPacket = packet;
bool res = mChronos->startObject(addr);
if (res && mPacket) {
connect(mPacket, SIGNAL(stateReceived(quint8,CAR_STATE)),
this, SLOT(stateReceived(quint8,CAR_STATE)));
}
return res;
}
ChronosComm *Chronos::comm()
{
return mChronos;
}
void Chronos::startTimerSlot()
{
qDebug() << "Starting car";
mIsStarted = true;
if (mPacket) {
mPacket->setApActive(255, true);
mScenarioTimer.start();
}
}
void Chronos::connectionChanged(bool connected, QString address)
{
if (connected) {
qDebug() << "Chronos TCP connection accepted from" << address;
} else {
qDebug() << "Chronos TCP disconnected from" << address;
mIsArmed = false;
}
}
void Chronos::stateReceived(quint8 id, CAR_STATE state)
{
(void)id;
(void)state;
chronos_monr monr;
double yaw = state.yaw + 90.0;
while (yaw < 0) {
yaw += 360.0;
}
while (yaw > 360.0) {
yaw -= 360.0;
}
// monr has 13 fields now.
monr.gps_ms_of_week = ChronosComm::gpsMsOfWeek();
monr.x = state.px;
monr.y = state.py;
monr.z = 0;
monr.heading = yaw;
monr.lon_speed = state.speed;
monr.lat_speed = 0;
monr.lon_acc = state.accel[1]; // TODO: hmm
monr.lat_acc = state.accel[0];
monr.direction = state.speed >= 0 ? 0 : 1; // 0 means forward now.
monr.rdyToArm = 1;
monr.status = mIsArmed ? (mIsStarted ? 4 : 2) : 0;
// if armed and started -> running (4)
// if armed and !started -> armed (2)
// otherwise 0
// (Simplified)
monr.error = 0; // TODO: check for errors.
mChronos->sendMonr(monr);
}
void Chronos::processTraj(chronos_traj traj)
{
qDebug() << "TRAJ RX";
// Subsample points
QVector<chronos_traj_pt> path_reduced;
if (traj.traj_pts.size() > 0) {
path_reduced.append(traj.traj_pts.first());
for (chronos_traj_pt pt: traj.traj_pts) {
chronos_traj_pt pt_last = path_reduced.last();
if (sqrt((pt.x - pt_last.x) * (pt.x - pt_last.x) +
(pt.y - pt_last.y) * (pt.y - pt_last.y) +
(pt.z - pt_last.z) * (pt.z - pt_last.z)) > 1.0) {
path_reduced.append(pt);
}
}
// Add end point
chronos_traj_pt pt = traj.traj_pts.last();
chronos_traj_pt pt_last = path_reduced.last();
if (sqrt((pt.x - pt_last.x) * (pt.x - pt_last.x) +
(pt.y - pt_last.y) * (pt.y - pt_last.y) +
(pt.z - pt_last.z) * (pt.z - pt_last.z)) > 0.01) {
path_reduced.append(pt);
}
}
// qDebug() << "Last PT time" << path_reduced.last().tRel - mScenarioTimer.elapsed();
if (mPacket) {
mRouteLast.clear();
QList<LocPoint> points;
bool first = true;
for (chronos_traj_pt pt: path_reduced) {
LocPoint lpt;
lpt.setXY(pt.x, pt.y);
lpt.setSpeed(pt.long_speed);
lpt.setTime(pt.tRel);
points.append(lpt);
mRouteLast.append(lpt);
if (points.size() >= 20) {
if (first) {
mPacket->replaceRoute(255, points);
first = false;
} else {
mPacket->setRoutePoints(255, points);
}
points.clear();
}
}
if (points.size() > 0) {
if (first) {
mPacket->replaceRoute(255, points);
} else {
mPacket->setRoutePoints(255, points);
}
}
}
}
void Chronos::processOsem(chronos_osem osem)
{
qDebug() << "OSEM RX";
if (mIsArmed) {
qDebug() << "Ignored because car is armed";
return;
}
mLlhRef[0] = osem.lat;
mLlhRef[1] = osem.lon;
mLlhRef[2] = osem.alt;
// TODO: Rotate route with heading
if (mPacket) {
mPacket->setEnuRef(255, mLlhRef);
mPacket->clearRoute(255);
}
}
void Chronos::processOstm(chronos_ostm ostm)
{
qDebug() << "OSTM RX";
if (ostm.armed == 0x2) {
mIsArmed = true;
} else if (ostm.armed == 0x3) {
mIsArmed = false;
}
qDebug() << "Armed:" << mIsArmed;
}
void Chronos::processStrt(chronos_strt strt)
{
quint64 cTime = ChronosComm::gpsMsOfWeek();
qDebug() << "STRT RX" << cTime << strt.gps_ms_of_week;
if (!mIsArmed) {
qDebug() << "Ignored because car is not armed";
return;
}
if ((strt.gps_ms_of_week <= cTime) || (strt.gps_ms_of_week - cTime) < 10) {
startTimerSlot();
qDebug() << "Starting car now";
} else {
mStartTimer->setSingleShot(true);
mStartTimer->start(strt.gps_ms_of_week - cTime);
qDebug() << "Starting car in" << strt.gps_ms_of_week - cTime << "ms";
}
}
void Chronos::processHeab(chronos_heab heab)
{
(void)heab;
if (mPacket) {
if (heab.status == 1) {
mHeabPollCnt++;
if (mHeabPollCnt >= 4) {
mHeabPollCnt = 0;
mPacket->getState(255);
}
} else {
mPacket->setApActive(255, false);
}
}
}
void Chronos::processSypm(chronos_sypm sypm)
{
qDebug() << "SYPM RX:" << sypm.sync_point << sypm.stop_time;
mSypmLast = sypm;
// Make sure that the car stops until MTSP is received.
int closest_sync = 0;
for (int i = 0;i < mRouteLast.size();i++) {
if (fabs(mRouteLast.at(i).getTime() - mSypmLast.sync_point) <
fabs(mRouteLast.at(closest_sync).getTime() - mSypmLast.sync_point)) {
closest_sync = i;
}
}
if (mPacket) {
mPacket->setSyncPoint(255, closest_sync, 1000 * 60 * 60, 1000);
qDebug() << "Car will not start until MTSP is received";
}
}
// TODO: Not part of protocol any more...
void Chronos::processMtsp(chronos_mtsp mtsp)
{
qDebug() << "MTSP RX";
(void)mtsp;
// Find points
int closest_sync = 0;
for (int i = 0;i < mRouteLast.size();i++) {
if (fabs(mRouteLast.at(i).getTime() - mSypmLast.sync_point) <
fabs(mRouteLast.at(closest_sync).getTime() - mSypmLast.sync_point)) {
closest_sync = i;
}
}
if (mPacket) {
mPacket->setSyncPoint(255, closest_sync, mtsp.time_est - ChronosComm::gpsMsOfWeek(),
mSypmLast.sync_point - mSypmLast.stop_time);
qDebug() << closest_sync << mtsp.time_est - ChronosComm::gpsMsOfWeek() <<
mSypmLast.sync_point - mSypmLast.stop_time;
}
}
void Chronos::processOpro(chronos_opro opro)
{
// Override transmitterid with value from server in Opro message.
qDebug() << "Setting transmitter id from opro message:" << opro.transmitter_id;
mChronos->setTransmitterId(opro.transmitter_id);
qDebug() << opro.ip;
qDebug() << opro.dim_x;
qDebug() << opro.dim_y;
qDebug() << opro.dim_z;
}