-
Notifications
You must be signed in to change notification settings - Fork 10
/
accelerationtestwidget.cpp
338 lines (265 loc) · 13.2 KB
/
accelerationtestwidget.cpp
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/*
This file is part of the Automon Project (OBD Diagnostics) - http://www.automon.io/
Source Repository: https://github.com/donaloconnor/automon/
Copyright (c) 2015, Donal O'Connor <[email protected]>
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <QLabel>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QLCDNumber>
#include <QPushButton>
#include <QSpinBox>
#include <QTime>
#include <QGridLayout>
#include "accelerationtestwidget.h"
AccelerationTestWidget::AccelerationTestWidget(Automon * kernel, QWidget * parent)
: QWidget(parent), m_kernel(kernel)
{
/*
Create all grid layouts. The main grid is attached the the full widget hince it's parent is this.
All the rest will form child/parent relationships with eachother so object distruction will be handled
automatically by QT.
*/
m_mainLayout = new QHBoxLayout(this);
m_verticalLayout = new QVBoxLayout();
QGridLayout * gridLayout = new QGridLayout();
QHBoxLayout * kphLayout = new QHBoxLayout();
QHBoxLayout * timeElapsedLayout = new QHBoxLayout();
QHBoxLayout * destinationSpeedLayout = new QHBoxLayout();
QHBoxLayout * startTestLayout = new QHBoxLayout();
/* The following spin box allows user to specify speed for accelleration test to reach. Increments of 5kph */
m_destinationSpeed = new QSpinBox();
/* The following QLCDNumbers display a digital number for the current speed and elapsed time of acceleration test */
m_speed = new QLCDNumber();
m_accelerationTimeDisplay = new QLCDNumber();
/* Simple push button to start AND stop the acceleration test if it is running already */
m_startTest = new QPushButton(tr("Start Acceleration Test!"));
/* Create all the labels that are visible to the user */
m_header = new QLabel(tr("Acceleration Test"));
QLabel * introduction = new QLabel(tr("The acceleration test allows you to test how quick your car can reach a speed! <br /><br /><strong>Instructions</strong><br />Simply select your destination speed, get ready on track, click start and when ready put the foot down!"));
QLabel * kphDisplayLabel = new QLabel(tr("Speed (KPH): "));
QLabel * timeElapsedLabel = new QLabel(tr("Time: (Seconds)"));
QLabel * destinationSpeedLabel = new QLabel(tr("Speed to Reach: "));
/*
This QTime object is used to catch the elapsed time from the user moving car > 1kph to destinatino speed
It has no parent object so will have to be deleted in the distructor
*/
m_time = new QTime();
/* Set the destination speed spinbox properties */
m_destinationSpeed->setFixedWidth(250);
m_destinationSpeed->setSingleStep(5);
m_destinationSpeed->setMaximum(255);
m_destinationSpeed->setValue(100);
m_destinationSpeed->setSuffix(QString(tr(" KM")));
/* Set the Current Speed and Elapsed Acceleration Time LCD number properties */
m_speed->setSegmentStyle(QLCDNumber::Filled);
m_speed->setStyleSheet("color: beige");
m_speed->setFixedSize(QSize(200,100));
m_accelerationTimeDisplay->setSegmentStyle(QLCDNumber::Filled);
m_accelerationTimeDisplay->setFixedSize(QSize(200,100));
m_accelerationTimeDisplay->setStyleSheet("color: beige");
/* Set the header style to Green and larger font */
m_header->setStyleSheet("color: #ace413; font-size:25px");
/* Set the start/stop push button width */
m_startTest->setFixedWidth(200);
/* Connect the start/stop test pushbutton's clicked() signal to our defined slot in this widget */
connect(m_startTest, SIGNAL(clicked()), this, SLOT(startTest()));
/* Set some label styles */
kphDisplayLabel->setStyleSheet("font-size: 20px;");
kphDisplayLabel->setFixedWidth(160);
timeElapsedLabel->setStyleSheet("font-size: 20px;");
timeElapsedLabel->setFixedWidth(160);
destinationSpeedLabel->setStyleSheet("font-size: 20px;");
/* Set up the layouts */
m_mainLayout->addLayout(m_verticalLayout);
/* Add header widget first and introduction message along with a spacing */
m_verticalLayout->addWidget(m_header);
m_verticalLayout->addWidget(introduction);
m_verticalLayout->addStretch();
m_verticalLayout->addLayout(gridLayout);
kphLayout->addWidget(kphDisplayLabel);
kphLayout->addWidget(m_speed);
kphLayout->addStretch();
timeElapsedLayout->addWidget(timeElapsedLabel);
timeElapsedLayout->addWidget(m_accelerationTimeDisplay);
timeElapsedLayout->addStretch();
startTestLayout->addWidget(m_startTest);
startTestLayout->setAlignment(Qt::AlignCenter);
destinationSpeedLayout->addWidget(destinationSpeedLabel);
destinationSpeedLayout->addWidget(m_destinationSpeed);
gridLayout->addLayout(kphLayout,0,0);
gridLayout->addLayout(timeElapsedLayout,1,0);
gridLayout->addLayout(destinationSpeedLayout,0,1);
gridLayout->addLayout(startTestLayout,1,1);
m_verticalLayout->setAlignment(Qt::AlignTop);
m_mainLayout->setAlignment(Qt::AlignTop);
/* Set the main overall layout to this widget */
setLayout(m_mainLayout);
/* Initialise some variables */
m_carMoving = false;
/* When car is stopped, misreadings could indicate car moving 1kph. This threshold specifies that */
m_parkThreshold = 1;
}
AccelerationTestWidget::~AccelerationTestWidget()
{
/* Delete any variables created on Heap that are not part of the parent/child hierarchy */
delete(m_time);
}
void AccelerationTestWidget::display(double speed)
{
/*
This is a slot that we have defined that our sensor will connect it's signal to.
The Automon kernel provides us a function to connect a object to a sensor. That object
has to have defined, a slot of type void display(double).
Each time the sensor value changes, this slot will receive that value.
It is here we define the logic of what happens with this value
*/
/* First check if the car is moving before test starts. IE: teststarted false, speed is > 0 + park threshold */
if (!m_testStarted && !m_carMoving && speed > 0 + m_parkThreshold)
emit changeStatus(tr("Car moving already. Waiting for you to slow to 0mph and start accelerating"));
else
{
/*
If we are in here, the test is running and we are waiting for the car to reach destination speed.
We may still be under the stating speed criteria however.
*/
m_testStarted = true;
m_speed->display(speed); /* Update the current speed QLCDNumber to the current speed of the vehicle */
/* Check if car is actually > than parking threshold and this is our first time, ie: m_carMoving = false */
if (!m_carMoving && speed > 0 + m_parkThreshold && !m_testFinished)
{
/* If in here, car has started to move so start the test. Only in here once */
m_carMoving = true;
m_time->start(); /* Start our timer. This timer will keep counting from here now */
emit changeStatus(tr("Timer started. Timing until you reach "+ QString::number(m_destinationSpeed->value()).toLatin1() + "KPH"));
}
else if (m_carMoving && speed >= m_destinationSpeed->value())
{
/* If in here, we have hit our destination speed */
m_carMoving = false; /* Turn this off to prevent going into previous if block */
m_accelerationTime = m_time->elapsed(); /* Capture the elapsed time */
m_kernel->stopMonitoring(); /* Stop the serial thread and ELM327 reading the speed from ECU */
removeSensor(); /* Remove the speed sensor, 010D */
/*
Display the elapsed time. Remember speed in milliseconds, so convert to seconds.
Also set the precision so we don't end up with too accurate speeds
*/
m_accelerationTimeDisplay->display(static_cast<double>(m_accelerationTime)/1000);
emit changeStatus(tr("Acceleration test finished"));
/* Reset all check variables */
m_destinationSpeed->setEnabled(true);
m_startTest->setEnabled(true);
m_testFinished = true;
m_startButtonClicked = false;
/* Change the start/stop button text and change the style of the time so it green */
m_startTest->setText(tr("Start Another Test!"));
m_accelerationTimeDisplay->setStyleSheet("color: #ace413");
}
}
}
void AccelerationTestWidget::startTest()
{
/*
This slot is called when the user clicks the Start/Stop Acceleration test button.
It is responsible for starting the acceleration test or stopping it if already running.
*/
if (m_startButtonClicked)
{
/* If in here, the test was running so stop the test and do some cleaning up */
m_kernel->stopMonitoring(); /* Stop the serial I/O thread */
removeSensor(); /* Remove sensor from serial thread */
/* Do some status updates and change text of start/stop button */
emit changeStatus(tr("Acceleration Test Cancelled!"));
m_startTest->setText(tr("Start Another Test!"));
/* Do some cleaning up */
m_startButtonClicked = false;
m_testStarted = false;
m_carMoving = false;
/* Re enable the destination speed spin box */
m_destinationSpeed->setEnabled(true);
}
else
{
/*
If in here, we are starting the test. First of all ensure that some other widget is not
already using the serial thread. If so abort starting this test letting the user know in the status
*/
if (m_kernel->isMonitoring())
{
emit changeStatus(tr("Automon is currently busy with another task. Stop this first"));
return;
}
/* If we done test before, the time would be green. Reset to 0 */
m_accelerationTimeDisplay->setStyleSheet("color: beige");
/* Some variable initialisation/clean up */
m_testFinished = false;
m_testStarted = false;
/* Add the sensor to the serial I/O thread */
setUpSensor();
/* If we done a test before, the m_time would be further on than 0. Reset */
m_time->restart();
/* Reset LCD and time */
m_accelerationTimeDisplay->display(0.0);
m_accelerationTime = 0.0;
m_speed->display(0);
/* Start the serial I/O thread */
if (!m_kernel->startMonitoring())
{
/* We should not get in here since the check was done above. Just incase however */
emit changeStatus(tr("Failed to start serial thread. This is an exception! Restart the device."));
return;
}
/* Disable the spinbox */
m_destinationSpeed->setEnabled(false);
/* Update label on Pushbutton to stop test */
m_startTest->setText(tr("Stop Test"));
emit changeStatus(tr("Test started. Waiting for you to start accelerating before timer starts"));
/* Setting this means, next time we click button, we go into other section of code above to stop test */
m_startButtonClicked = true;
}
}
void AccelerationTestWidget::removeSensor()
{
/*
This method is responsible for removing the Speed sensor from the serial I/O thread
*/
if (m_kernel->disconnectSensorFromSlot(m_kernel->getActiveSensorByCommand("010D"),this))
{
m_kernel->removeActiveSensorByCommand("010D");
}
else
qDebug() << "Sensor not present";
}
void AccelerationTestWidget::setUpSensor()
{
/*
This method is responsible for adding the speed sensor to the serial I/O thread
*/
if(m_kernel->addActiveSensorByCommand("010D"))
{
/* Connect the sensor to the slot in this method, display(double) */
m_kernel->connectSensorToSlot(m_kernel->getActiveSensorByCommand("010D"),this);
/* Set frequency so gets updated ASAP */
m_kernel->setSensorFrequency(m_kernel->getActiveSensorByCommand("010D"), 1);
}
else
qDebug() << "Command not supported by ECU";
}