driver_obs_gsm.cpp 9.07 KB
Newer Older
Achim Morschhauser's avatar
Achim Morschhauser committed
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
/********************************************************************************
 * WRITTEN BY ACHIM MORSCHHAUSER, GFZ POTSDAM, April 2016			*
 * mors//gfz-potsdam.de								*
 *										*
 * This class provides the driver for a GemSys GSM-19 proton magnetometer.       *
 * 										*
 * It is based on the abstract observatory driver class driver_obs.		*
 *										*
 ********************************************************************************/

// C headers
#include <time.h>   // System time functions
#include <unistd.h> // usleep
#include <stdio.h>  // printf

// Custom C++ headers
#include <buffer_obs.hpp>
#include <data_obs_scalar.hpp>
#include <driver_obs_gsm.hpp>

/****************************************************************************
 ****************************************************************************
 *																			*
 * 				Constructors.												*
 *																			*
 ****************************************************************************
 ***************************************************************************/

/****************************************************************************
 *									 										*
 * Automatic baud rate.														*
 *																			*
 ***************************************************************************/
driver_obs_gsm::driver_obs_gsm(std::string port, buffer_obs* buffer,
		const char* init_char) :
		Driver_Obs_Serial(port,buffer) {

	set_term();
39
	find_baud(init_char);
Achim Morschhauser's avatar
Achim Morschhauser committed
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339

}


/****************************************************************************
 *									 										*
 * Pre-determined baud rate.												*
 *																			*
 ***************************************************************************/
driver_obs_gsm::driver_obs_gsm(std::string port, buffer_obs* buffer, int baud) :
		Driver_Obs_Serial(port,buffer) {

	set_term();
	set_baud(baud);

}

/****************************************************************************
 ****************************************************************************
 *																			*
 * 			      Public Methods.											*
 *																			*
 ****************************************************************************
 ***************************************************************************/

/****************************************************************************
 *									 										*
 * Run the GSM in freerun mode.	This is only for recording, the measurement	*
 * must be started with the child classes.									*
 * 																			*
 * TODO NOT TESTED															*
 *																			*
 ***************************************************************************/
int driver_obs_gsm::freerun(double freq) {

	/////////////////////////////////////////////////////////////////////////
	//
	// Initialize some variables.
	//
	/////////////////////////////////////////////////////////////////////////

	// Time of measurement
	struct timespec recv_time;

	// Receive buffer
	char buf[100];

	// Data
	double value;
	int quality;

	// Data container
	data_obs_scalar data;

	/////////////////////////////////////////////////////////////////////////
	//
	// Receive the data.
	//
	/////////////////////////////////////////////////////////////////////////

	// Infinite Loop
	while (1) {

		// Wait for answer
		if (receive(buf, sizeof(buf), 3, &recv_time) >= 0) {

			// Parse data from answer
			sscanf(buf, "%lf %d", &value, &quality);

			// Store to data structure
			data.set_data(value, quality);
			data.set_time(&recv_time);

			// Write measurement to buffer
			buffer->put(&data);

		}

	}

	return (0);

}
/****************************************************************************
 *									 										*
 * Run the GSM in triggered mode.											*
 * - The GSM-19 must be set to remote mode.									*
 *																			*
 ***************************************************************************/
int driver_obs_gsm::run(double freq_in) {

	/////////////////////////////////////////////////////////////////////////
	//
	// Some variables.
	//
	/////////////////////////////////////////////////////////////////////////

	// Integer frequency
	int freq = (int) freq_in;

	// Time of measurement
	struct timespec time, time_recv;

	// The data
	data_obs_scalar data;

	////////////////////////////////////////////////////////////////////////////////
	//
	// Find the time which the GSM needs to measure
	//
	////////////////////////////////////////////////////////////////////////////////

	// Time of received signal
	int start_sec = 0;
	int start_msec = 0;

	// Current time
	clock_gettime(CLOCK_REALTIME, &time);

	// Wait for full 5-second
	while (time.tv_sec % freq != 0 || (int) (time.tv_nsec / 1e6) != 0) {
		usleep(10);
		clock_gettime(CLOCK_REALTIME, &time);
	}

	// Do the measurement
	get(&data);

	// Get the epoch seconds of measurement
	data.get_time(&time_recv);

	// Calculate the delay
	start_sec = freq - (data.get_sec() % freq);
	start_msec = 1000 - data.get_msec();
	if (start_msec < 1000) {
		start_sec = start_sec - 1;
	}

	// Measurement time
	double mtime = (double) (time_recv.tv_sec - time.tv_sec)
			+ time_recv.tv_nsec / 1e9;

	fprintf(stderr,"Measurement time of GSM: %f\n",mtime);

	// Check if frequency is supported by measurement time
	if (mtime > freq) {
		fprintf(stderr,"WARNING: freq must be larger than measurement time: %fs\n",
				mtime);
		return (-1);
	}

	/////////////////////////////////////////////////////////////////////////
	//
	// Start continuous measurement
	//
	/////////////////////////////////////////////////////////////////////////

	// Infinite loop
	while (1) {

		// Get the current time
		clock_gettime(CLOCK_REALTIME, &time);

		// Wait for correct time to measure.
		// We want to measure at full seconds.
		while ((time.tv_sec % freq != start_sec)
				|| (((int) (time.tv_nsec / 1e6)) != start_msec)) {
			clock_gettime(CLOCK_REALTIME, &time);
			usleep(10);
		}

		// Do the measurement
		get(&data);

		// Clean the measurement time
		data.smooth_msec(2);

		// Write measurement to buffer
		buffer->put(&data);

		// Check start time for measurement and adjust if necessary
		if ((data.get_sec() % freq == 0) && (data.get_msec() > 1)) {
			start_msec -= data.get_msec();
			if (start_msec < 0) {
				start_msec = -start_msec;
				start_sec = (freq + start_sec - 1) % freq;
			}
		} else if ((data.get_sec() % freq != 0) && (data.get_msec() < 999)) {
			start_msec += 1000 - data.get_msec();
			if (start_msec >= 1000) {
				start_msec = start_msec - 1000;
				start_sec = (start_sec + 1) % freq;
			}
		}

	}

}

/****************************************************************************
 *																		 	*
 * 			Take a single measurement.										*
 * 																			*
 * Only pass the timestamp, as data_obs can't store data values.			*
 *																			*
 ***************************************************************************/
int driver_obs_gsm::get(data_obs* data) {

	data_obs_scalar fdata = data_obs_scalar();
	get(&fdata);
	data->set_time(&fdata);

	return (0);

}

/****************************************************************************
 *									 										*
 * 			Take a single measurement.										*
 *																			*
 ***************************************************************************/
int driver_obs_gsm::get(data_obs_scalar* data) {

	/////////////////////////////////////////////////////////////////////////
	//
	// Some variables.
	//
	/////////////////////////////////////////////////////////////////////////

	// Time of measurement
	struct timespec recv_time;

	// Receive buffer
	char buf[100];

	// Data
	double value;
	int quality;

	/////////////////////////////////////////////////////////////////////////
	//
	// Do the measurement.
	//
	/////////////////////////////////////////////////////////////////////////

	// Send F
	send("F");

	// Wait for answer
	if (receive(buf, sizeof(buf), 5, &recv_time) >= 0) {
		// Parse data from answer
		sscanf(buf, "%lf %d", &value, &quality);

		// Store to data structure
		data->set_data(value, quality);
		data->set_time(&recv_time);
		data->smooth_msec(1);

		return (0);

	}

	return (-1);

}

/****************************************************************************
 *									 										*
 * 			Stop the running mode of the GSM.								*
 *																			*
 ***************************************************************************/
int driver_obs_gsm::stop() {

	return (0);

}

/****************************************************************************
 ****************************************************************************
 *																			*
 * 			      Private Methods.											*
 *																			*
 ****************************************************************************
 ***************************************************************************/

/****************************************************************************
 *									 										*
 * 			Set the termination characters.									*
 *																			*
 ***************************************************************************/
int driver_obs_gsm::set_term() {

	int termlen=2;
	char term[2]={13,10};

	return(Driver_Obs_Serial::set_term(term,termlen));


}