-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGpsHandler.cpp
More file actions
116 lines (89 loc) · 1.92 KB
/
GpsHandler.cpp
File metadata and controls
116 lines (89 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include "GpsHandler.h"
using namespace std;
GpsHandler *GpsHandler::_gps = NULL;
void* acquireWrapper(void* obj){
GpsHandler* ctrl = reinterpret_cast<GpsHandler*>(obj);
ctrl->acquire();
return nullptr;
}
GpsHandler::GpsHandler(){
init();
}
GpsHandler::~GpsHandler(){
close();
}
GpsHandler *GpsHandler::get()
{
if(_gps == NULL)
{
_gps = new GpsHandler();
_gps->lock();
_gps->start();
}
return _gps;
}
void GpsHandler::kill()
{
if (NULL != _gps)
{
cout << "Killing GPS..." << endl;
_gps->stop();
delete _gps;
_gps = NULL;
cout << "GPS killed !" << endl;
}
}
void GpsHandler::start(){
pthread_create(&t_acquire, NULL, acquireWrapper, this);
}
void GpsHandler::stop(){
cout << "Stopping GPS thread..." << endl;
m_acquire = false;
pthread_join(t_acquire, NULL);
cout << "GPS thread stopped !" << endl;
}
double GpsHandler::latitude(){
return _gps->m_gpsdata->fix.latitude;
}
double GpsHandler::longitude(){
return _gps->m_gpsdata->fix.longitude;
}
void GpsHandler::init(){
string host = "localhost";
string port = "2947";
cout << "Trying to connect to " << host << ":" << port << endl;
m_gpsrec = new gpsmm((char*) host.c_str(),(char*) port.c_str());
//connect to local GPSd
if((m_gpsdata = m_gpsrec->stream(WATCH_ENABLE | WATCH_JSON)) == NULL ){
cerr << "No GPSd running." << endl;
exit(-1);
}
cout << "Gpsd Running" << endl;
}
void GpsHandler::lock(){
cout << "Locking GPS..." << endl;
while(!m_gpsrec->waiting(50000000) )
{
cout << "Waiting for GPS locking..." << endl;
}
cout << "GPS locked !" << endl;
}
void GpsHandler::acquire(){
m_acquire= true;
while(m_acquire)
{
//wait for some data
if (!m_gpsrec->waiting(5000000)){
continue;
}
if ((m_gpsdata = m_gpsrec->read()) == NULL) {
cerr << "Read error.\n" << endl;
return;
}
pthread_yield();
}
}
void GpsHandler::close(){
m_gpsdata = m_gpsrec->stream(WATCH_DISABLE);
//gps_close(m_gpsdata);
}