On Wednesday 26 November 2008 13:44:01 arne anka wrote:
> please post your .h and .cpp -- that might help, to get a clue, where to  
> look.

Hi,

here they are..

I've implemented all Gypsy get-methods and signals except the 
SatelliteChanged-singal so far..

is there a Wiki to put this functions as an example? is OM-Wiki the right 
place?


#include "MokoGPS.h"
#include <QtDBus>
#include <QDebug>
#include <QDBusReply>
#include <QDateTime>
#include <QtTest>

#include <math.h>



/****************************************************
 * << and >> Operators declaration for DBus metatypes
 *****************************************************/

 // Marshall the GPSSatellite data into a D-BUS argument
 QDBusArgument &operator<<(QDBusArgument &argument, const GPSSatellite &sat)
 {
     argument.beginStructure();
     argument << sat.ID << sat.InUse << sat.Elevation << sat.Azimuth << sat.SNR;
     argument.endStructure();
     return argument;
 }

 // Retrieve the GPSSatellite data from the D-BUS argument
 const QDBusArgument &operator>>(const QDBusArgument &argument, GPSSatellite &sat)
 {
     argument.beginStructure();
     argument >> sat.ID >> sat.InUse >> sat.Elevation >> sat.Azimuth >> sat.SNR;
     argument.endStructure();
     return argument;
 }
 
const QDebug operator<<(QDebug debug, const GPSSatellite &sat) {
	debug << sat.ID << sat.InUse << sat.Elevation << sat.Azimuth << sat.SNR;
	return debug;
}

const QDebug operator<<(QDebug debug, const QList<GPSSatellite> &lsat) {
	foreach(GPSSatellite sat, lsat) debug << sat;
	return debug;
}

/*********************************
 * MokoGPS class
 *********************************/


MokoGPS::MokoGPS(){
	
	///***************************
	///* Connection to system bus
	///***************************
	//register GPSSatellite metatype
	qDBusRegisterMetaType<GPSSatellite>();
	qDBusRegisterMetaType<QList<GPSSatellite> >();
	
	QDBusConnection GPSConnection = QDBusConnection::systemBus();
	
	/// check connection
	if (GPSConnection.isConnected())
		qDebug() << "successfully connected to system bus \n";
	else
		qFatal("Failed to connect to session bus");
	
	/// create a proxy object for method calls 
	QString ServiceBusName = "org.freedesktop.Gypsy";
	QString ObjectPath = "/org/freedesktop/Gypsy" ;
	GPSInterface = new QDBusInterface(	ServiceBusName,
										ObjectPath,
								///method path = "" search recursive in 
								///all subfolders for the called method									
										"", 
										QDBusConnection::systemBus() );		

	connect(GPSInterface, SIGNAL( FixStatusChanged(int) ),this, SLOT( handleFixStatusChanged(int) ) );
	

	//next line is to try types to connect to Signal	
	connect(GPSInterface, SIGNAL( SatellitesChanged(QDBusMessage) ) ,this, SLOT( handleSatellitesChanged(QDBusMessage) ) );
	
}

///* destructor
MokoGPS::~MokoGPS(){ 
	
	///close on prog.exit
	logPosition.close();
	logAccuracy.close();
	logCourse.close();
	//logSatellites.close();
}



/*
 * Slots to interact with GUI
 */


/// enable GPS
void MokoGPS::StartDevice()
{
	GPSInterface->call("Enable");
	qDebug()<<"device started \n";
	emit sendOutput("device started ", 1);
}

/// disable GPS
void MokoGPS::StopDevice()
{
	GPSInterface->call("Disable");
	qDebug()<<"device stopped \n";
		emit sendOutput("device stopped ", 1);
}

void MokoGPS::StartLog() {
	
		qDebug()<<"start logging";
		emit sendOutput("start logging", 1);
		/// timestamp to prepend to Logfile name
		QDateTime *thisDay = new QDateTime();
		QString thisDate = thisDay->currentDateTime().toString("yyyy-MM-dd_hh.mm.ss");
		
		///open logfiles
		QString positionlog = "GPSPosition.log";
		QString accuracylog = "GPSAccuracy.log";
		QString courselog = "GPSCourse.log";
		QString satelliteslog = "GPSSatellites.log";
		
		positionlog.prepend(thisDate);
		accuracylog.prepend(thisDate);
		courselog.prepend(thisDate);
		satelliteslog.prepend(thisDate);
		
		logPosition.open(positionlog.toLatin1() );
		logAccuracy.open(accuracylog.toLatin1() );
		logCourse.open(courselog.toLatin1() );
		//logSatellites.open(satelliteslog.toLatin1() );
	
		/// connect dbus signals
		connect(GPSInterface, SIGNAL( PositionChanged(int,int,double,double,double) ),this, SLOT( handlePositionChanged(int,int,double,double,double) ) );
		connect(GPSInterface, SIGNAL( TimeChanged(int) ),this, SLOT( handleTimeChanged(int) ) );
		connect(GPSInterface, SIGNAL( ConnectionStatusChanged(bool) ),this, SLOT( handleConnectionStatusChanged(bool) ) );	
		/// due to display this without logging in Gui it is connected in the constructor
		//connect(GPSInterface, SIGNAL( FixStatusChanged(int) ),this, SLOT( handleFixStatusChanged(int) ) );
		connect(GPSInterface, SIGNAL( CourseChanged(int,int,double,double,double) ),this, SLOT( handleCourseChanged(int,int,double,double,double) ) );
		connect(GPSInterface, SIGNAL( AccuracyChanged(int,double,double,double) ),this, SLOT( handleAccuracyChanged(int,double,double,double) ) );
		/// not jet possible to set the right parameter
		//connect(GPSInterface, SIGNAL( SatellitesChanged(QDBusArgument) ),this, SLOT( handleSatellitesChanged(QDBusArgument) ) );


}

void MokoGPS::StopLog(){
	
		qDebug()<<"stop logging";
		emit sendOutput("stop logging", 1);
		/// close logfiles
		logPosition.close();
		logAccuracy.close();
		logCourse.close();
		//logSatellites.close();
		
		/// disconnect dbus signals
		disconnect(GPSInterface, SIGNAL( PositionChanged(int,int,double,double,double) ),this, SLOT( handlePositionChanged(int,int,double,double,double) ) );
		disconnect(GPSInterface, SIGNAL( TimeChanged(int) ),this, SLOT( handleTimeChanged(int) ) );
		disconnect(GPSInterface, SIGNAL( ConnectionStatusChanged(bool) ),this, SLOT( handleConnectionStatusChanged(bool) ) );	
		/// due to display this without logging in Gui it is connected in the constructor
		//disconnect(GPSInterface, SIGNAL( FixStatusChanged(int) ),this, SLOT( handleFixStatusChanged(int) ) );
		disconnect(GPSInterface, SIGNAL( CourseChanged(int,int,double,double,double) ),this, SLOT( handleCourseChanged(int,int,double,double,double) ) );
		disconnect(GPSInterface, SIGNAL( AccuracyChanged(int,double,double,double) ),this, SLOT( handleAccuracyChanged(int,double,double,double) ) );
		///not jet possible to set the right parameter
		//disconnect(GPSInterface, SIGNAL( SatellitesChanged(QDBusMessage) ),this, SLOT( handleSatellitesChanged(QDBusMessage) ) );

}

/*
 * Slots handeling DBUS signals
 */


void MokoGPS::handlePositionChanged(int fields,int tstamp,double lat,double lon,double alt)
{
    setPosition(fields, tstamp, lat, lon, alt);
	
	logPosition << m_Position.TimeStamp
			<<" "<< m_Position.Latitude
			<<" "<< m_Position.Longitude
			<<" "<< m_Position.Altitude 
			<< "\n";
	
	//qDebug() << "position" << Position.TimeStamp;
} 

void MokoGPS::handleTimeChanged(int GPStime)
{
	qDebug() << "thime changed to:" << GPStime;
}

void MokoGPS::handleConnectionStatusChanged(bool ConnectionStatus){
	qDebug() << "connectionstatus changed to " << ConnectionStatus;
}

void MokoGPS::handleFixStatusChanged(int fixstatus){
	emit sendOutput("fixstatus is ", 1);
	emit sendOutput(fixstatus, 0);
	QString status ="";
	switch (fixstatus) {
	case 0:
		status = "device off";
		break;
	case 1:
		status = "trying";
		break;
	case 2:
		status = "2D fix";
		break;
	case 3:
		status = "3D fix";
		break;
	}
	
	emit sendFixStatus(status);
	// qDebug() << "fixstatus is " << fixstatus;
}

void MokoGPS::handleCourseChanged(int fields, int tstamp, double speed, double heading, double climb){

	//emit sendOutput("coursechanged", 1);
	logCourse << tstamp 
			<< " " << knots_to_MpS(speed)
			<< " " << heading
			<< " " << knots_to_MpS(climb);
			
	///velocity in ned (north east down)
	m_Velocity_ned.n = knots_to_MpS(speed) * cos(heading*PI/180) ;
	m_Velocity_ned.e = knots_to_MpS(speed) * sin(heading*PI/180);
	m_Velocity_ned.d =  knots_to_MpS(climb);
	
	logCourse << " " << m_Velocity_ned.n
				<< " " << m_Velocity_ned.e 
				<< " " << m_Velocity_ned.d << "\n";
	//	qDebug() << "course" << tstamp;
}

void MokoGPS::handleAccuracyChanged(int fields, double pdop, double hdop, double vdop) {
	//emit sendOutput("accuracy changed", 1);
	logAccuracy << GetTime()
				<< " " << pdop
				<< " " << hdop
				<< " " << vdop << "\n";
	//qDebug() << "accuracy" << GetTime();
}

void MokoGPS::handleSatellitesChanged(QDBusMessage SatellitesDbus) {
	qDebug() << SatellitesDbus;
	emit sendOutput("SattelitesArray signal",1);
}



/*
 * 
 * Methods to get GPS-data deliberately at any time
 * 
 */


/// returns: 3 = 3dFix ,  2= 2dFix , 1= no Fix yet , 0=not started 
int MokoGPS::GetFixStatus() 
{
	QDBusReply<int> reply = GPSInterface->call("GetFixStatus");
	FixStatus=reply.value();
	// print data to console for Debugging
	////qDebug()<<"GetFixStatus Reply: "<<FixStatus<<"\n";
	return FixStatus;
}

/// Get Time 
int MokoGPS::GetTime() 
{
	QDBusReply<int> reply = GPSInterface->call("GetTime");
	Time=reply.value();
	// print data to console for Debugging
	////qDebug()<<"Time: "<<Time<<"\n";
	
	return Time;
}

//* returns True if Device is Connected
bool MokoGPS::GetConnectionStatus() 
{
	QDBusReply<bool> reply = GPSInterface->call("GetConnectionStatus");
	ConnectionStatus=reply.value();
	// print data to console for Debugging
	////qDebug()<<"GetConnectionStatus Reply: "<<ConnectionStatus<<"\n";
	
return ConnectionStatus;
}


///	Position Fields tell which value is valid
///	GYPSY_POSITION_FIELDS_NONE = 1 << 0,
///	GYPSY_POSITION_FIELDS_LATITUDE = 1 << 1,
///	GYPSY_POSITION_FIELDS_LONGITUDE = 1 << 2,
///	GYPSY_POSITION_FIELDS_ALTITUDE = 1 << 3
/// Example 1000 means None of the values is valid

///* Fields/Timestamp/Lat/Lon/Alt
void MokoGPS::GetPosition() 
{ 
	QDBusMessage PositionReply;
	PositionReply = GPSInterface->call("GetPosition");
	QList<QVariant> ReplyData;
	ReplyData = PositionReply.arguments();
	setPosition(ReplyData[0].toInt(), //fields
				ReplyData[1].toInt(), // tstamp
				ReplyData[2].toDouble(),  //lat
				ReplyData[3].toDouble(),  //lon
				ReplyData[4].toDouble() );  //alt
	
	/* print data to console for Debugging
	qDebug() <<"Position"
			<<"Fields: "<< Position.Fields
			<<"TS: "<< Position.TimeStamp
			<<"Lat: "<<Position.Latitude
			<<"Lon: "<< Position.Longitude
			<<"Alt: "<<Position.Altitude<< "\n";
	*/
}

/// updates SatellitesArray 
/// [ID/InUse/Elevation/Azimuth/SNR/NumberOfSats]
void MokoGPS::GetSatellites() 
{ 
	QDBusMessage SatelliteReply;
	SatelliteReply = GPSInterface->call("GetSatellites");
	QList<GPSSatellite> satList = qdbus_cast<QList<GPSSatellite> >(SatelliteReply.arguments()[0]);
	
	qDebug() << satList;

}

///	Position Fields tell which value is valid
/// GYPSY_ACCURACY_FIELDS_NONE = 0,
/// GYPSY_ACCURACY_FIELDS_POSITION = 1 << 0,
/// GYPSY_ACCURACY_FIELDS_HORIZONTAL = 1 << 1,
/// GYPSY_ACCURACY_FIELDS_VERTICAL = 1 << 2,
/// Example 1000 means None of the values is valid
 
///returns /Field/Timestamp/pdop/hdop/vdop
void MokoGPS::GetAccuracy() 
{ 
	QDBusMessage AccuracyReply;
	AccuracyReply = GPSInterface->call("GetAccuracy");
	QList<QVariant> ReplyData;
	ReplyData = AccuracyReply.arguments();
	m_Accuracy.Fields=ReplyData[0].toInt();
	m_Accuracy.PDop=ReplyData[1].toDouble();
	m_Accuracy.HDop=ReplyData[2].toDouble();
	m_Accuracy.VDop=ReplyData[3].toDouble();
	/* print data to console for Debugging
	qDebug() <<"Accuracy"
			<<"Fields: "<< Accuracy.Fields
			<<"PDop: "<< Accuracy.PDop
			<<"HDop: "<<Accuracy.HDop
			<<"VDop: "<< Accuracy.VDop<<"\n";
	*/
}

void MokoGPS::GetCourse()
{
	QDBusMessage CourseReply;
	CourseReply = GPSInterface->call("GetCourse");
	QList<QVariant> ReplyData;
	ReplyData = CourseReply.arguments();
	m_Course.Fields=ReplyData[0].toInt();
	m_Course.timestamp=ReplyData[1].toInt();
	/// 1 knot = 0.51444 m/s
	m_Course.speed=ReplyData[2].toDouble()*KNOT_TO_M_PER_SECOND;  /// speed in knots calculated to m/s
	m_Course.direction=ReplyData[3].toDouble(); /// in degree 0° = north
	m_Course.climb=ReplyData[4].toDouble()*KNOT_TO_M_PER_SECOND; /// vertical speed in knots
	qDebug() << "Field: " << m_Course.Fields
			<< " speed: " << m_Course.speed
			<< " direction: " << m_Course.direction
			<< " climb: " << m_Course.climb << endl;
	
}

/************************
 * setFuctions
 ***********************/

void MokoGPS::setPosition(int fields,int tstamp,double lat,double lon,double alt){
	m_Position.Fields=fields;
	m_Position.TimeStamp=tstamp;
	m_Position.Latitude=lat;
	m_Position.Longitude=lon;
	m_Position.Altitude=alt;
}

/************************
 * convertFuctions
 ***********************/
 
double MokoGPS::knots_to_MpS(double knots) {
	double meterPerSecond = knots * KNOT_TO_M_PER_SECOND;
	return meterPerSecond;
}
 
double MokoGPS::MpS_to_knots(double meterPerSecond) {
	double knots = meterPerSecond / KNOT_TO_M_PER_SECOND;
	return knots;
}
#ifndef MOKOGPS_H_
#define MOKOGPS_H_
#include <iostream>
#include <fstream>
#include <QDBusArgument>
#include <QDBusReply>
#include <QObject>
class QDBusConnection;
class QDBusInterface;


const double KNOT_TO_KMH = 1.852;
const double KNOT_TO_M_PER_SECOND = 0.51444;

struct GPSSatellite
{
    	int ID;
		bool InUse;
		unsigned int Elevation;
		unsigned int Azimuth;
		unsigned int SNR;
};

Q_DECLARE_METATYPE(GPSSatellite);
Q_DECLARE_METATYPE(QList<GPSSatellite>);
 
class MokoGPS : public QObject
{
	Q_OBJECT

public:

	int FixStatus;
	int ConnectionStatus;
	int NumberOfSats;
	int Time;	

struct GPSAccuracy{
		int Fields;
		double PDop;
		double HDop;
		double VDop;
		}m_Accuracy;

struct GPSPosition{
		int Fields;
		int TimeStamp;
		double Latitude;
		double Longitude;
		double Altitude;
		}m_Position;

struct GPSPosition_ecef{
		double x;
		double y;
		double z;
		}m_Position_ecef;

struct GPSCourse{
		int Fields;
		int timestamp;
		double speed; //here in m/s (from NMEA retrieved in knots) 
		double direction;
		double climb; //z velocity here in m/s (from NMEA retrieved in knots)
		}m_Course;

struct GPSVelocity_ned{
		double n;
		double e;
		double d;
		}m_Velocity_ned;

private:
	QDBusInterface *GPSInterface;
	std::ofstream logPosition;
	std::ofstream logAccuracy;
	std::ofstream logCourse;
	std::ofstream logSatellites;

/*
 * functions, slots and signals
 */

public:

	MokoGPS();
	~MokoGPS();
	
	///get various GPS data
	bool GetConnectionStatus();
	void GetPosition();
	void GetSatellites();
	void GetAccuracy();
	void GetCourse();
	int GetFixStatus();
	int GetTime();
	
private:

	///setFunctions
	void setPosition(int fields,int tstamp,double lat,double lon,double alt);

	//convert functions
	double knots_to_MpS(double knots);
	double MpS_to_knots(double meterPerSecond);

public slots:

	///control Device and device information
	void StartDevice();
	void StopDevice();
	void StartLog();
	void StopLog();


	void handlePositionChanged(int fields,int tstamp,double lat,double lon,double alt);
	void handleTimeChanged(int GPStime);
	void handleConnectionStatusChanged(bool ConnectionStatus);
	void handleFixStatusChanged(int fixstatus);
	void handleCourseChanged(int fields, int tstamp, double speed, double heading, double climb);
	void handleAccuracyChanged(int fields, double pdop, double hdop, double vdop);
	void handleSatellitesChanged(QDBusMessage SatellitesDbus);

	

signals:
	void sendOutput(QVariant, bool);
	void sendFixStatus(QString);

};

#endif /*MOKOGPS_H_*/
_______________________________________________
Openmoko community mailing list
[email protected]
http://lists.openmoko.org/mailman/listinfo/community

Reply via email to