Skip to content

Commit

Permalink
simulator: no more static storage for sensor data
Browse files Browse the repository at this point in the history
  • Loading branch information
janbar committed Sep 29, 2024
1 parent 3487569 commit 90017bc
Show file tree
Hide file tree
Showing 15 changed files with 242 additions and 123 deletions.
4 changes: 4 additions & 0 deletions simulator/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ if(SIMULATOR_WITH_READLINE)
endif()

set(simulator_SOURCES main.cpp
globalazimuth.cpp
globalposition.cpp
simulatedcompass.cpp
simulatedpositionsource.cpp
simulatedsensorplugin.cpp
Expand All @@ -40,6 +42,8 @@ set(simulator_SOURCES main.cpp
)
set(simulator_HEADERS
commandline.h
globalazimuth.h
globalposition.h
gpxrunner.h
simulatedcompass.h
simulatedpositionsource.h
Expand Down
30 changes: 30 additions & 0 deletions simulator/globalazimuth.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright (C) 2024
* Jean-Luc Barriere <jlbarriere68@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 3.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include "globalazimuth.h"

void GlobalAzimuth::resetData(qreal azimuth)
{
std::lock_guard<std::mutex> g(_mutex);
_azimuth = azimuth;
}

qreal GlobalAzimuth::data() const
{
std::lock_guard<std::mutex> g(_mutex);
return _azimuth;
}
37 changes: 37 additions & 0 deletions simulator/globalazimuth.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright (C) 2024
* Jean-Luc Barriere <jlbarriere68@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 3.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef GLOBALAZIMUTH_H
#define GLOBALAZIMUTH_H

#include <mutex>
#include <QCompassReading>

class GlobalAzimuth
{
public:
GlobalAzimuth() { }
~GlobalAzimuth() = default;

void resetData(qreal azimuth);
qreal data() const;

private:
mutable std::mutex _mutex;
qreal _azimuth = 0.0;
};

#endif // GLOBALAZIMUTH_H
34 changes: 34 additions & 0 deletions simulator/globalposition.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright (C) 2024
* Jean-Luc Barriere <jlbarriere68@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 3.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include "globalposition.h"

void GlobalPosition::resetData(double lat, double lon, double alt)
{
{
QGeoCoordinate coord(lat, lon, alt);
std::lock_guard<std::mutex> g(_mutex);
_info = QGeoPositionInfo(coord, QDateTime::currentDateTime());
}
emit dataUpdated();
}

QGeoPositionInfo GlobalPosition::data() const
{
std::lock_guard<std::mutex> g(_mutex);
return _info;
}
43 changes: 43 additions & 0 deletions simulator/globalposition.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright (C) 2024
* Jean-Luc Barriere <jlbarriere68@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 3.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef GLOBALPOSITION_H
#define GLOBALPOSITION_H

#include <mutex>
#include <QObject>
#include <QtPositioning/QGeoPositionInfoSource>

class GlobalPosition : public QObject
{
Q_OBJECT

public:
GlobalPosition() { }
~GlobalPosition() = default;

void resetData(double lat, double lon, double alt);
QGeoPositionInfo data() const;

signals:
void dataUpdated();

private:
mutable std::mutex _mutex;
QGeoPositionInfo _info;
};

#endif // GLOBALPOSITION_H
5 changes: 2 additions & 3 deletions simulator/gpxrunner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

#include "gpxrunner.h"

#include "simulatedpositionsource.h"
#include <utils.h>

#include <cmath>
Expand Down Expand Up @@ -96,7 +95,7 @@ bool GPXRunner::processNextPoint(int * waitfor)
Point point = _running->midway.front();
_running->midway.pop_front();
fprintf(stdout, "[TRANSITION %d] %3.6f %3.6f %3.0f\n", _running->pts, point.lat, point.lon, point.alt);
SimulatedPositionSource::resetData(point.lat, point.lon, point.alt);
_position.resetData(point.lat, point.lon, point.alt);
*waitfor = point.duration;
return true;
}
Expand All @@ -108,7 +107,7 @@ bool GPXRunner::processNextPoint(int * waitfor)
return false;

// apply current point
SimulatedPositionSource::resetData(
_position.resetData(
point->coord.GetLat(),
point->coord.GetLon(),
point->elevation.value_or(0.0));
Expand Down
9 changes: 6 additions & 3 deletions simulator/gpxrunner.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
#ifndef GPXRUNNER_H
#define GPXRUNNER_H

#include "globalposition.h"
#include "globalazimuth.h"
#include <gpxfilemodel.h>
#include <QObject>
#include <QThread>
Expand All @@ -27,7 +29,8 @@ class GPXRunner : public QThread
Q_OBJECT

public:
GPXRunner() { }
explicit GPXRunner(GlobalPosition& position, GlobalAzimuth& azimuth)
: _position(position), _azimuth(azimuth) { }
~GPXRunner();

bool loadGPX(const QString& fileptah);
Expand All @@ -38,8 +41,6 @@ class GPXRunner : public QThread
signals:
void pointChanged(int pts);

private slots:

private:

struct Point
Expand Down Expand Up @@ -67,6 +68,8 @@ private slots:
bool aborted;
};

GlobalPosition& _position;
GlobalAzimuth& _azimuth;
GPXFile * _gpxfile = nullptr;
Running * _running = nullptr;

Expand Down
49 changes: 21 additions & 28 deletions simulator/simulatedcompass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,16 @@

#include "simulatedcompass.h"

#include <QDebug>

#include <time.h>

#define RADIANS_TO_DEGREES 57.2957795

char const * const SimulatedCompass::id("simulated.compass");
QCompassReading SimulatedCompass::_compassReading;

SimulatedCompass::SimulatedCompass(QSensor *sensor)
: QSensorBackend(sensor)
SimulatedCompass::SimulatedCompass(GlobalAzimuth& azimuth, QSensor *sensor)
: QSensorBackend(sensor), _azimuth(azimuth)
{
setReading<QCompassReading>(&_compassReading);
setReading<QCompassReading>(&_reading);
addDataRate(1.0, 10.0);

connect(&_updateTimer, &QTimer::timeout,
Expand All @@ -40,22 +37,6 @@ SimulatedCompass::~SimulatedCompass()
{
}

quint64 SimulatedCompass::produceTimestamp()
{
struct timespec tv;
int ok;

#ifdef CLOCK_MONOTONIC_RAW
ok = clock_gettime(CLOCK_MONOTONIC_RAW, &tv);
if (ok != 0)
#endif
ok = clock_gettime(CLOCK_MONOTONIC, &tv);
Q_ASSERT(ok == 0);

quint64 result = (tv.tv_sec * 1000000ULL) + (tv.tv_nsec * 0.001); // scale to microseconds
return result;
}

void SimulatedCompass::start()
{
if (!_updateTimer.isActive())
Expand All @@ -78,14 +59,26 @@ void SimulatedCompass::stop()
}
}

void SimulatedCompass::resetData(qreal azimuth)
void SimulatedCompass::onTimeout()
{
_compassReading.setCalibrationLevel(1.0);
_compassReading.setAzimuth(azimuth);
_compassReading.setTimestamp(produceTimestamp());
_reading.setAzimuth(_azimuth.data());
_reading.setCalibrationLevel(1.0);
_reading.setTimestamp(produceTimestamp());
newReadingAvailable();
}

void SimulatedCompass::onTimeout()
quint64 SimulatedCompass::produceTimestamp()
{
newReadingAvailable();
struct timespec tv;
int ok;

#ifdef CLOCK_MONOTONIC_RAW
ok = clock_gettime(CLOCK_MONOTONIC_RAW, &tv);
if (ok != 0)
#endif
ok = clock_gettime(CLOCK_MONOTONIC, &tv);
Q_ASSERT(ok == 0);

quint64 result = (tv.tv_sec * 1000000ULL) + (tv.tv_nsec * 0.001); // scale to microseconds
return result;
}
14 changes: 5 additions & 9 deletions simulator/simulatedcompass.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,36 +17,32 @@
#ifndef SIMULATEDCOMPASS_H
#define SIMULATEDCOMPASS_H

#include "globalazimuth.h"

#include <QObject>
#include <QCompassReading>
#include <qsensorbackend.h>
#include <QTimer>

#define PI 3.1415926

class SimulatedCompass : public QSensorBackend
{
Q_OBJECT
public:
static char const * const id;
SimulatedCompass(QSensor *sensor);
SimulatedCompass(GlobalAzimuth& azimuth, QSensor *sensor);
~SimulatedCompass();
void start() Q_DECL_OVERRIDE;
void stop() Q_DECL_OVERRIDE;

static const QCompassReading& data() { return _compassReading; }
static void resetData(qreal azimuth);

signals:
void sensorError(int);

private slots:
void onTimeout();

private:
static QCompassReading _compassReading;
QTimer _updateTimer;

GlobalAzimuth& _azimuth;
QCompassReading _reading;
static quint64 produceTimestamp();
};

Expand Down
Loading

0 comments on commit 90017bc

Please sign in to comment.