Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[FEATURE] add gpsd support to live gps tracking
git-svn-id: http://svn.osgeo.org/qgis/trunk@14331 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
jef committed Oct 4, 2010
1 parent 8ffd816 commit 7b1fc1b
Show file tree
Hide file tree
Showing 7 changed files with 481 additions and 41 deletions.
27 changes: 24 additions & 3 deletions src/app/gps/qgsgpsinformationwidget.cpp
Expand Up @@ -152,15 +152,24 @@ QgsGPSInformationWidget::QgsGPSInformationWidget( QgsMapCanvas * thepCanvas, QWi
mSliderMarkerSize->setValue( mySettings.value( "/gps/markerSize", "12" ).toInt() );
mSpinTrackWidth->setValue( mySettings.value( "/gps/trackWidth", "2" ).toInt() );
QString myPortMode = mySettings.value( "/gps/portMode", "scanPorts" ).toString();

mGpsdHost->setText( mySettings.value( "/gps/gpsdHost", "localhost" ).toString() );
mGpsdPort->setText( mySettings.value( "/gps/gpsdPort", 2947 ).toString() );
mGpsdDevice->setText( mySettings.value( "/gps/gpsdDevice" ).toString() );

//port mode
if ( myPortMode == "scanPorts" )
{
mRadAutodetect->setChecked( true );
}
else
else if ( myPortMode == "explicitPort" )
{
mRadUserPath->setChecked( true );
}
else if ( myPortMode == "gpsd" )
{
mRadGpsd->setChecked( true );
}
//auto digitising behaviour
bool myAutoAddVertexFlag = mySettings.value( "/gps/autoAddVertices", "false" ).toBool();
mCbxAutoAddVertices->setChecked( myAutoAddVertexFlag );
Expand Down Expand Up @@ -198,15 +207,23 @@ QgsGPSInformationWidget::~QgsGPSInformationWidget()
mySettings.setValue( "/gps/trackWidth", mSpinTrackWidth->value() );
mySettings.setValue( "/gps/markerSize", mSliderMarkerSize->value() );
mySettings.setValue( "/gps/autoAddVertices", mCbxAutoAddVertices->isChecked() );
// scan or explicit port
// scan, explicit port or gpsd
if ( mRadAutodetect->isChecked() )
{
mySettings.setValue( "/gps/portMode", "scanPorts" );
}
else
else if ( mRadUserPath->isChecked() )
{
mySettings.setValue( "/gps/portMode", "explicitPort" );
}
else
{
mySettings.setValue( "/gps/portMode", "gpsd" );
}

mySettings.setValue( "/gps/gpsdHost", mGpsdHost->text() );
mySettings.setValue( "/gps/gpsdPort", mGpsdPort->text().toInt() );
mySettings.setValue( "/gps/gpsdDevice", mGpsdDevice->text() );

// pan mode
if ( radRecenterMap->isChecked() )
Expand Down Expand Up @@ -317,6 +334,10 @@ void QgsGPSInformationWidget::connectGps()
return;
}
}
else if ( mRadGpsd->isChecked() )
{
port = QString( "%1:%2:%3" ).arg( mGpsdHost->text() ).arg( mGpsdPort->text() ).arg( mGpsdDevice->text() );
}

mGPSTextEdit->append( tr( "Connecting..." ) );

Expand Down
2 changes: 2 additions & 0 deletions src/core/CMakeLists.txt
Expand Up @@ -11,6 +11,7 @@ SET(QGIS_CORE_SRCS
gps/qgsgpsconnection.cpp
gps/qgsgpsconnectionregistry.cpp
gps/qgsnmeaconnection.cpp
gps/qgsgpsdconnection.cpp
gps/qgsgpsdetector.cpp
gps/parse.c
gps/sentence.c
Expand Down Expand Up @@ -257,6 +258,7 @@ SET(QGIS_CORE_MOC_HDRS
gps/qgsgpsconnection.h
gps/qgsgpsdetector.h
gps/qgsnmeaconnection.h
gps/qgsgpsdconnection.h
gps/qextserialport/qextserialport.h
gps/qextserialport/qextserialenumerator.h
)
Expand Down
51 changes: 51 additions & 0 deletions src/core/gps/qgsgpsdconnection.cpp
@@ -0,0 +1,51 @@
/***************************************************************************
qgsgpsdconnection.cpp - description
---------------------
begin : October 4th, 2010
copyright : (C) 2010 by Jürgen E. Fischer, norBIT GmbH
email : jef at norbit dot de
***************************************************************************/

/***************************************************************************
* *
* 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; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#include "qgsgpsdconnection.h"
#include "qgslogger.h"

#include <QTcpSocket>

QgsGpsdConnection::QgsGpsdConnection( QString host, qint16 port, QString device )
: QgsNMEAConnection( new QTcpSocket() )
, mDevice( device )
{
QTcpSocket *socket = qobject_cast< QTcpSocket * >( mSource );

QObject::connect( socket, SIGNAL( connected() ), this, SLOT( connected() ) );
QObject::connect( socket, SIGNAL( error( QAbstractSocket::SocketError ) ), this, SLOT( error( QAbstractSocket::SocketError ) ) );
socket->connectToHost( host, port );
}

QgsGpsdConnection::~QgsGpsdConnection()
{
//connection will be closed by base class
QgsDebugMsg( "entered." );
}

void QgsGpsdConnection::connected()
{
QgsDebugMsg( "connected!" );
QTcpSocket *socket = qobject_cast< QTcpSocket * >( mSource );
socket->write( QString( "?WATCH={\"enable\":true,\"nmea\":true%1};" ).arg( mDevice.isEmpty() ? mDevice : QString( "\"device\":%1" ).arg( mDevice ) ).toUtf8() );
}

void QgsGpsdConnection::error( QAbstractSocket::SocketError socketError )
{
QTcpSocket *socket = qobject_cast< QTcpSocket * >( mSource );
QgsDebugMsg( QString( "error: %1 %2" ).arg( socketError ).arg( socket->errorString() ) );
}
42 changes: 42 additions & 0 deletions src/core/gps/qgsgpsdconnection.h
@@ -0,0 +1,42 @@
/***************************************************************************
qgsgpsdconnection.h - description
-------------------
begin : October 4th, 2010
copyright : (C) 2010 by Jürgen E. Fischer, norBIT GmbH
email : jef at norbit dot de
***************************************************************************/

/***************************************************************************
* *
* 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; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#ifndef QGSGPSDCONNECTION_H
#define QGSGPSDCONNECTION_H

#include "qgsgpsdconnection.h"
#include "qgsnmeaconnection.h"

#include <QAbstractSocket>

/**Evaluates NMEA sentences coming from gpsd*/
class CORE_EXPORT QgsGpsdConnection: public QgsNMEAConnection
{
Q_OBJECT
public:
QgsGpsdConnection( QString host, qint16 port, QString device );
~QgsGpsdConnection();

private slots:
void connected();
void error( QAbstractSocket::SocketError );

private:
QString mDevice;
};

#endif // QGSGPSDCONNECTION_H
46 changes: 34 additions & 12 deletions src/core/gps/qgsgpsdetector.cpp
Expand Up @@ -20,6 +20,7 @@
#include "qgslogger.h"
#include "qgsgpsconnection.h"
#include "qgsnmeaconnection.h"
#include "qgsgpsdconnection.h"

#include <QStringList>
#include <QFileInfo>
Expand All @@ -29,6 +30,9 @@ QList< QPair<QString, QString> > QgsGPSDetector::availablePorts()
{
QList< QPair<QString, QString> > devs;

// try local gpsd first
devs << QPair<QString, QString>( "localhost:2947:", tr( "local gpsd" ) );

#ifdef linux
// look for linux serial devices
foreach( QString linuxDev, QStringList() << "/dev/ttyS%1" << "/dev/ttyUSB%1" << "/dev/rfcomm%1" )
Expand Down Expand Up @@ -115,9 +119,9 @@ void QgsGPSDetector::advance()
delete mConn;
}

QextSerialPort *port = 0;
mConn = 0;

do
while ( !mConn )
{
mBaudIndex++;
if ( mBaudIndex == mBaudList.size() )
Expand All @@ -133,19 +137,37 @@ void QgsGPSDetector::advance()
return;
}

if ( port )
delete port;
if ( mPortList[ mPortIndex ].first.contains( ":" ) )
{
mBaudIndex = mBaudList.size() - 1;

QStringList gpsParams = mPortList[ mPortIndex ].first.split( ":" );

Q_ASSERT( gpsParams.size() == 3 );

mConn = new QgsGpsdConnection( gpsParams[0], gpsParams[1].toInt(), gpsParams[2] );
}
else
{
QextSerialPort *serial = new QextSerialPort( mPortList[ mPortIndex ].first, QextSerialPort::EventDriven );

serial->setBaudRate( mBaudList[ mBaudIndex ] );
serial->setFlowControl( FLOW_OFF );
serial->setParity( PAR_NONE );
serial->setDataBits( DATA_8 );
serial->setStopBits( STOP_1 );

port = new QextSerialPort( mPortList[ mPortIndex ].first, QextSerialPort::EventDriven );
port->setBaudRate( mBaudList[ mBaudIndex ] );
port->setFlowControl( FLOW_OFF );
port->setParity( PAR_NONE );
port->setDataBits( DATA_8 );
port->setStopBits( STOP_1 );
if ( serial->open( QIODevice::ReadOnly | QIODevice::Unbuffered ) )
{
mConn = new QgsNMEAConnection( serial );
}
else
{
delete serial;
}
}
}
while ( !port->open( QIODevice::ReadOnly | QIODevice::Unbuffered ) );

mConn = new QgsNMEAConnection( port );
connect( mConn, SIGNAL( stateChanged( const QgsGPSInformation & ) ), this, SLOT( detected( const QgsGPSInformation & ) ) );
connect( mConn, SIGNAL( destroyed( QObject * ) ), this, SLOT( connDestroyed( QObject * ) ) );

Expand Down
2 changes: 1 addition & 1 deletion src/core/gps/qgsnmeaconnection.h
Expand Up @@ -34,7 +34,7 @@ class CORE_EXPORT QgsNMEAConnection: public QgsGPSConnection
/**Parse available data source content*/
void parseData();

private:
protected:
/**Store data from the device before it is processed*/
QString mStringBuffer;
/**Splits mStringBuffer into sentences and calls libnmea*/
Expand Down

0 comments on commit 7b1fc1b

Please sign in to comment.