gyrogpsc/static/scripts/speedChart.js
2021-01-15 18:41:11 +01:00

116 lines
3.3 KiB
JavaScript

/**
* This file defines a line chart used in the full replay page, showing Ublox speeds and
* smartphone speeds at a same moment in time in km/h, over time.
*
* @authors Timo Volkmann, Frank Herkommer.
*/
//List of all speeds sent in by the Ublox
let allSpeedsSerial = []
//Define the line chart and its properties
let ctx = document.getElementById('speedChart').getContext('2d');
let speedChart = new Chart(ctx, {
type: 'line',
data: {
labels: new Array(),
datasets: [{
label: 'Ublox speed',
backgroundColor: 'rgba(214, 69, 65, 1)',
borderColor: 'rgba(214, 69, 65, 1)',
borderWidth: 1,
fill: false,
pointRadius: 0.5,
lineTension: 0.5,
data: []
},
{
label: 'Smartphone speed',
backgroundColor: 'rgba(30, 139, 195, 1)',
borderColor: 'rgba(30, 139, 195, 1)',
borderWidth: 1,
fill: false,
pointRadius: 0.5,
lineTension: 0.5,
data: []
}]
},
options: {
scales: {
yAxes: [{
ticks: {
min: 0,
max: 250,
stepSize: 25
}
}],
xAxes: [{
type: 'time',
time: {
unit: 'second'
}
}]
},
animation: {
duration: 0
}
}
});
/**
* Function filtering out speed data sent in by the Ublox using only the given indexes calculated in the refull.js file,
* and adding the data to the chart.
* As there is less smartphone data collected, we only want the Ublox speeds that come in at the moment with the
* smallest time difference between the TCP message and the Serial message.
*/
function addSerialSpeedData(sensordataList) {
let speedsSerial = []
let times = []
if(indexes.length > 0){
indexes.forEach(index => {
speedsSerial.push((allSpeedsSerial[index] * 3.6).toFixed(2))
})
speedChart.data.datasets[0].data = speedsSerial;
speedChart.update();
} else {
sensordataList.forEach(sensordata => {
if (sensordata.Speed === 0) {
return;
}
let speed = sensordata.Speed
speedsSerial.push((speed * 3.6).toFixed(2));
let time = sensordata.Timestamp
times.push(time)
})
speedChart.data.labels = times;
speedChart.data.datasets[0].data = speedsSerial;
speedChart.update();
}
}
/**
* Function to extract only the speeds and Timestamps out of the messages received from the smartphone
* and add tem to the chart.
* @param sensordataList List of all received messages of the tcp source and their sensor data.
*/
function addTCPSpeedData(sensordataList) {
let speedsTCP = []
let times = []
sensordataList.forEach(sensordata => {
if (sensordata.Speed === 0) {
return;
}
let speed = sensordata.Speed
speedsTCP.push((speed * 3.6).toFixed(2));
let time = sensordata.Timestamp
times.push(time)
})
speedChart.data.labels = times;
speedChart.data.datasets[1].data = speedsTCP;
speedChart.update();
}