Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[sensorfw] initial mag and compass chains.
  • Loading branch information
Lorn Potter committed Aug 21, 2013
1 parent 287f09a commit a9889f3
Show file tree
Hide file tree
Showing 24 changed files with 1,293 additions and 2 deletions.
4 changes: 3 additions & 1 deletion chains/chains.pro
Expand Up @@ -4,4 +4,6 @@ include( ../common-config.pri )
include( ../common-install.pri )

SUBDIRS = accelerometerchain \
orientationchain
orientationchain \
magcalibrationchain \
compasschain
215 changes: 215 additions & 0 deletions chains/compasschain/compasschain.cpp
@@ -0,0 +1,215 @@
/****************************************************************************
**
** Copyright (C) 2013 Jolla Ltd
** Contact: lorn.potter@jollamobile.com
This file is part of Sensord.
Sensord is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License
version 2.1 as published by the Free Software Foundation.
Sensord 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with Sensord. If not, see <http://www.gnu.org/licenses/>.
*/

#include <QVariant>
#include <QStringList>
#include <QDebug>

#include "compasschain.h"
#include "sensormanager.h"
#include "bin.h"
#include "bufferreader.h"
#include "config.h"
#include "logging.h"

#include "datatypes/orientationdata.h"


CompassChain::CompassChain(const QString& id) :
AbstractChain(id)
{
qDebug() << Q_FUNC_INFO << id;

SensorManager& sm = SensorManager::instance();

magChain = sm.requestChain("magcalibrationchain");
Q_ASSERT(magChain);
setValid(magChain->isValid());

// orientAdaptor = sm.requestDeviceAdaptor("orientationadaptor");
// Q_ASSERT(orientAdaptor);
// setValid(orientAdaptor->isValid());

accelerometerChain = sm.requestChain("accelerometerchain");
Q_ASSERT(accelerometerChain);
setValid(accelerometerChain->isValid());

accelerometerReader = new BufferReader<AccelerationData>(1);

magReader = new BufferReader<CalibratedMagneticFieldData>(1);

// if (orientAdaptor->isValid())
// orientationdataReader = new BufferReader<TimedXyzData>(1);

compassFilter = sm.instantiateFilter("compassfilter");
Q_ASSERT(compassFilter);

declinationFilter = sm.instantiateFilter("declinationfilter");
Q_ASSERT(declinationFilter);

downsampleFilter = sm.instantiateFilter("downsamplefilter");
Q_ASSERT(downsampleFilter);

avgaccFilter = sm.instantiateFilter("avgaccfilter");
Q_ASSERT(avgaccFilter);

trueNorthBuffer = new RingBuffer<CompassData>(1);
nameOutputBuffer("truenorth", trueNorthBuffer); //

magneticNorthBuffer = new RingBuffer<CompassData>(1);
nameOutputBuffer("magneticnorth", magneticNorthBuffer); //

// Create buffers for filter chain
filterBin = new Bin;

// if (orientAdaptor->isValid())
// filterBin->add(orientationdataReader, "orientation");

filterBin->add(magReader, "magnetometer");
filterBin->add(accelerometerReader, "accelerometer");

filterBin->add(compassFilter, "compass");
filterBin->add(avgaccFilter, "avgaccelerometer"); //normalize to flat ?
filterBin->add(downsampleFilter, "downsamplefilter");
filterBin->add(declinationFilter, "declinationcorrection");

filterBin->add(trueNorthBuffer, "truenorth");
filterBin->add(magneticNorthBuffer, "magneticnorth");

// if (orientAdaptor->isValid()) { //hybris has compass heading in orientation sensor
// if (!filterBin->join("orientation", "source", "compass", "orientsink"))
// qDebug() << Q_FUNC_INFO << "magnetometer join failed";
// } else {

if (!filterBin->join("magnetometer", "source", "compass", "magsink"))
qDebug() << Q_FUNC_INFO << "magnetometer join failed";

if (!filterBin->join("accelerometer", "source", "avgaccelerometer", "sink"))
qDebug() << Q_FUNC_INFO << "accelerometer join failed";


if (!filterBin->join("avgaccelerometer", "source", "downsamplefilter", "sink"))
qDebug() << Q_FUNC_INFO << "avgaccelerometer join failed";

if (!filterBin->join("downsamplefilter", "source", "compass", "accsink"))
qDebug() << Q_FUNC_INFO << "downsamplefilter join failed";
// }

if (!filterBin->join("compass", "magnorthangle", "magneticnorth", "sink"))
qDebug() << Q_FUNC_INFO << "compass1 join failed";

if (!filterBin->join("compass", "magnorthangle", "declinationcorrection", "sink"))
qDebug() << Q_FUNC_INFO << "compass2 join failed";

if (!filterBin->join("declinationcorrection", "source", "truenorth", "sink"))
qDebug() << Q_FUNC_INFO << "declinationfilter join failed";



if (!connectToSource(accelerometerChain, "accelerometer", accelerometerReader))
qDebug() << Q_FUNC_INFO << "accelerometer connect failed";

if (!connectToSource(magChain, "calibratedmagnetometerdata", magReader))
qDebug() << Q_FUNC_INFO << "magnetometer connect failed";

// if (orientAdaptor->isValid()) {
// if (!connectToSource(orientAdaptor, "orientation", orientationdataReader))
// qDebug() << Q_FUNC_INFO << "orientation connect failed";
// }

setDescription("Compass direction"); //compass north in degrees
introduceAvailableDataRange(DataRange(0, 359, 1));
introduceAvailableInterval(DataRange(50,200,0));

setRangeSource(magChain);
addStandbyOverrideSource(magChain);

addStandbyOverrideSource(accelerometerChain);
setIntervalSource(accelerometerChain);
}

CompassChain::~CompassChain()
{
SensorManager& sm = SensorManager::instance();

disconnectFromSource(accelerometerChain, "accelerometer", accelerometerReader);

disconnectFromSource(magChain, "magnetometer", magReader);

// disconnectFromSource(orientAdaptor, "orientation", orientationdataReader);
// sm.releaseDeviceAdaptor("orientationadaptor");

delete accelerometerReader;
delete magReader;
// delete orientationdataReader;
delete declinationFilter;
delete trueNorthBuffer;
delete magneticNorthBuffer;
delete filterBin;
}

bool CompassChain::start()
{
if (AbstractSensorChannel::start()) {
sensordLogD() << "Starting compassChain";
filterBin->start();
// if (orientAdaptor->isValid()) {
// orientAdaptor->startSensor();
// } else {
accelerometerChain->start();
magChain->start();
// }
}
return true;
}

bool CompassChain::stop()
{
if (AbstractSensorChannel::stop()) {
sensordLogD() << "Stopping compassChain";
// if (orientAdaptor->isValid()) {
// orientAdaptor->stopSensor();
// } else {
accelerometerChain->stop();
magChain->stop();
// }
filterBin->stop();
}
return true;
}

bool CompassChain::compassEnabled() const
{
return true;
}

void CompassChain::setCompassEnabled(bool enabled)
{
}

quint16 CompassChain::declinationValue() const
{
return 0;
}

void CompassChain::resetCalibration()
{
qDebug() << Q_FUNC_INFO;
}
92 changes: 92 additions & 0 deletions chains/compasschain/compasschain.h
@@ -0,0 +1,92 @@
/****************************************************************************
**
** Copyright (C) 2013 Jolla Ltd
** Contact: lorn.potter@jollamobile.com
This file is part of Sensord.
Sensord is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License
version 2.1 as published by the Free Software Foundation.
Sensord 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with Sensord. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef COMPASSCHAIN_H
#define COMPASSCHAIN_H

#include "abstractsensor.h"
#include "abstractchain.h"
#include "coordinatealignfilter.h"
#include "deviceadaptor.h"
#include "bufferreader.h"
#include "filter.h"
#include "bin.h"

#include "orientationdata.h"
#include "timedunsigned.h"

class Bin;
template <class TYPE> class BufferReader;
class FilterBase;

class CompassChain : public AbstractChain
{
Q_OBJECT

Q_PROPERTY(bool compassEnabled READ compassEnabled WRITE setCompassEnabled)
Q_PROPERTY(quint16 declinationValue READ declinationValue)
public:

static AbstractChain *factoryMethod(const QString& id)
{
CompassChain *sc = new CompassChain(id);
return sc;
}

bool compassEnabled() const;
void setCompassEnabled(bool enabled);

quint16 declinationValue() const;

public Q_SLOTS:
bool start();
bool stop();
void resetCalibration();

// declinationAngle()
protected:
CompassChain(const QString& id);
~CompassChain();

private:
Bin* filterBin;

AbstractChain *accelerometerChain;
AbstractChain *magChain;

// DeviceAdaptor *accelerometerAdaptor;
BufferReader<AccelerationData> *accelerometerReader;

// DeviceAdaptor *magAdaptor;
BufferReader<CalibratedMagneticFieldData> *magReader;

// DeviceAdaptor *orientAdaptor;
// BufferReader<TimedXyzData> *orientationdataReader;


FilterBase *compassFilter;
FilterBase *declinationFilter;
FilterBase *downsampleFilter;
FilterBase *avgaccFilter;

RingBuffer<CompassData> *trueNorthBuffer;
RingBuffer<CompassData> *magneticNorthBuffer;
};

#endif // COMPASSCHAIN_H
17 changes: 17 additions & 0 deletions chains/compasschain/compasschain.pro
@@ -0,0 +1,17 @@
TARGET = compasschain

HEADERS += compasschain.h \
compasschainplugin.h \
compassfilter.h

SOURCES += compasschain.cpp \
compasschainplugin.cpp \
compassfilter.cpp

#HEADERS += ../../filters/avgaccfilter/avgaccfilter.h
#SOURCES += ../../filters/avgaccfilter/avgaccfilter.cpp

INCLUDEPATH += ../../filters/coordinatealignfilter
#INCLUDEPATH += ../../filters/avgaccfilter

include( ../chain-config.pri )
43 changes: 43 additions & 0 deletions chains/compasschain/compasschainplugin.cpp
@@ -0,0 +1,43 @@
/****************************************************************************
**
** Copyright (C) 2013 Jolla Ltd
** Contact: lorn.potter@jollamobile.com
This file is part of Sensord.
Sensord is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License
version 2.1 as published by the Free Software Foundation.
Sensord 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with Sensord. If not, see <http://www.gnu.org/licenses/>.
*/

#include "compasschainplugin.h"
#include "compasschain.h"
#include "compassfilter.h"
#include "sensormanager.h"
#include "logging.h"

void CompassChainPlugin::Register(class Loader&)
{
sensordLogD() << "registering compasschain";
SensorManager& sm = SensorManager::instance();

sm.registerChain<CompassChain>("compasschain");
sm.registerFilter<CompassFilter>("compassfilter");
}

QStringList CompassChainPlugin::Dependencies() {
// return QString("accelerometerchain:magcalibrationchain:declinationfilter:downsamplefilter:avgaccfilterr").split(":", QString::SkipEmptyParts);
return QString("accelerometerchain:magcalibrationchain:declinationfilter:downsamplefilter:avgaccfilter:orientationadaptor").split(":", QString::SkipEmptyParts);
}

#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
Q_EXPORT_PLUGIN2(compasschain, CompassChainPlugin)
#endif

0 comments on commit a9889f3

Please sign in to comment.