driver_obs_obsdaq.cpp 30.2 KB
Newer Older
Achim Morschhauser's avatar
Achim Morschhauser committed
1
2
3
4
5
6
7
8
9
10
11
12
/*******************************************************************************
 *******************************************************************************
 * 
 *  WRITTEN BY ACHIM MORSCHHAUSER, GFZ POTSDAM, 2016-2018                     
 *  mors//gfz-potsdam.de							       
 *									       
 * This class provides the driver for an OBSDAQ ADC.			       
 * 									       
 * It is based on the abstract observatory driver class driver_obs.		
 *									
 *******************************************************************************
 ******************************************************************************/
Achim Morschhauser's avatar
Achim Morschhauser committed
13
14
15
16
17
18
19
20
21
22
23
24
25
26

// C headers
#include <time.h>     				// System time functions
#include <unistd.h>   				// usleep
#include <stdio.h>    				// printf
#include <stdlib.h>   				// itoa
#include <string.h>   				// strcmp
#include <inttypes.h> 				// int32_t

// C++ headers
#include <sstream>
#include <iostream>
#include <iomanip>

Achim Morschhauser's avatar
Achim Morschhauser committed
27
// Library C++ headers
Achim Morschhauser's avatar
Achim Morschhauser committed
28
#include <buffer_obs.hpp>     		// Buffer for data
Achim Morschhauser's avatar
Achim Morschhauser committed
29
#include <data_obs_vector.hpp>          // Vector data structure
Achim Morschhauser's avatar
Achim Morschhauser committed
30
31
#include <driver_obs_obsdaq.hpp>	// Generic observatory driver

Achim Morschhauser's avatar
Achim Morschhauser committed
32
33
/*******************************************************************************
 *******************************************************************************
Achim Morschhauser's avatar
Achim Morschhauser committed
34
35
36
 *																			*
 * 				Constructors.												*
 *																			*
Achim Morschhauser's avatar
Achim Morschhauser committed
37
38
 *******************************************************************************
 ******************************************************************************/
Achim Morschhauser's avatar
Achim Morschhauser committed
39

Achim Morschhauser's avatar
Achim Morschhauser committed
40
/*******************************************************************************
Achim Morschhauser's avatar
Achim Morschhauser committed
41
42
43
44
45
*																			*
* Automatically determine the baud rate.									*
*																			*
* Default address is broadcast address: 0x0F.								*
*																			*
Achim Morschhauser's avatar
Achim Morschhauser committed
46
*******************************************************************************/
Achim Morschhauser's avatar
Achim Morschhauser committed
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
driver_obs_obsdaq::driver_obs_obsdaq(std::string port, buffer_obs* buffer,
		Obs_Calibration_Vector* cal, int addr) :
		Driver_Obs_Serial(port, buffer, cal) {

	// Set the termination characters for the serial port driver
	set_term();

	// Set the RS485 address
	RS485_addr = addr;
	char addr_str[3];
	sprintf(addr_str, "%02X", addr);
	RS485_addrstr = std::string(addr_str);

	// Check for baud rate with the right address.
	// Stop any running measurement when doing so.
	const char *cmds[] = { "||||||||||||||||||||||||||||||||||||||||||||||||",
Achim Morschhauser's avatar
Achim Morschhauser committed
63
64
65
66
67
68
69
70
                               "||||||||||||||||||||||||||||||||||||||||||||||||",
			       "||||||||||||||||||||||||||||||||||||||||||||||||",
			       "||||||||||||||||||||||||||||||||||||||||||||||||",
			       "||||||||||||||||||||||||||||||||||||||||||||||||",
		               "||||||||||||||||||||||||||||||||||||||||||||||||",
		               "||||||||||||||||||||||||||||||||||||||||||||||||",
			       "||||||||||||||||||||||||||||||||||||||||||||||||",
			       "$M" };
Achim Morschhauser's avatar
Achim Morschhauser committed
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

	set_baud((char**) cmds, 9);

	// Initialize the OBSDAQ
	init();

}

/****************************************************************************
*																			*
* Pre-defined baud rate.													*
* 																			*
* Default address is broadcast address: 0x0F.								*
*																			*
****************************************************************************/
driver_obs_obsdaq::driver_obs_obsdaq(std::string port, buffer_obs* buffer,
		Obs_Calibration_Vector* cal, int baud, int addr) :
				Driver_Obs_Serial(port, buffer, (Obs_Calibration*) cal) {

	// Set the termination characters for serial port driver
	driver_obs_obsdaq::set_term();

	// Set the RS485 address
	RS485_addr = addr;
	char addr_str[3];
	sprintf(addr_str, "%02X", addr);
	RS485_addrstr = std::string(addr_str);

Achim Morschhauser's avatar
Achim Morschhauser committed
99
	// Set the hardware baud rate
Achim Morschhauser's avatar
Achim Morschhauser committed
100
	set_baud(baud);
Achim Morschhauser's avatar
Achim Morschhauser committed
101
102
        
        // Set the ObsDAQ baud rate
Achim Morschhauser's avatar
Achim Morschhauser committed
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

	// Stop the measurement
	stop();

	// Initialize
	init();

}

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

/****************************************************************************
 *																		 	*
 * Take a single measurement.												*
 *																			*
 * Only passes the time, as data_obs can't store data values.				*
 *																			*
 ***************************************************************************/

int driver_obs_obsdaq::get(data_obs* data) {

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

	return (0);

}

/****************************************************************************
 *																		 	*
 * Take a single measurement.												*
 *																			*
 ***************************************************************************/
int driver_obs_obsdaq::get(data_obs_vector* data) {

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

	// Time of measurement
	struct timespec recv_time;

	// Receive buffer
	char buf[100];

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

	// Stop free-run mode (just in case).
	stop();

	// Synchronize channels
	send("#PP10000500");
	usleep(200e3);
	send("#PP00000000");
	usleep(2e6);

	// Ask for latest values.
	send("#A");

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

		// Parse data from answer
		if (parse_data_ascii(data, buf) >= 0){

			// Set the time
			data->set_time(&recv_time);

			return(0);

		}

	}

	return (-1);

}

/****************************************************************************
 *									 										*
 * Run the OBSDAQ in triggered mode (NOT YET WORKING).						*
 *																			*
 ***************************************************************************/
//TODO This is a stub.
int driver_obs_obsdaq::run(double freq) {

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

	// Time of measurement
	struct timespec recv_time, time;
	// The data
	data_obs_vector data;
	// The receive buffer
	char buf[200];

	/////////////////////////////////////////////////////////////////////////
	//
	// Initialize: Set frequency and calibrate.
	//
	/////////////////////////////////////////////////////////////////////////

	if (init_run(freq)<0){
		return(-1);
	}

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

	/////////////////////////////////////////////////////////////////////////
	//
	// Listen to continuous measurement
	//
	/////////////////////////////////////////////////////////////////////////
	// Infinite loop
	while (1) {

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

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

			// Parse data from answer
			if (parse_data_ascii(&data, buf) >= 0){

				// Set the time
				data.set_time(&recv_time);

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

			}

		}

	}

}

/****************************************************************************
 *									 										*
 * Run the OBSDAQ in freerun mode (no triggering).											*
 *																			*
 ***************************************************************************/
int driver_obs_obsdaq::freerun(double freq) {

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

	// Time of measurement
	struct timespec recv_time, time;
	// The vector data
	data_obs_vector data;
	// The receive buffer
	char buf[200];
Achim Morschhauser's avatar
Achim Morschhauser committed
281
282
283
	// Data parser function
	int (driver_obs_obsdaq::*parse_data)(data_obs_vector* data, char* buf);
	//std::function<int(data_obs_vector* data, char* buf)> &parse_data;
Achim Morschhauser's avatar
Achim Morschhauser committed
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306

	/////////////////////////////////////////////////////////////////////////
	//
	// Initialize: Set frequency and calibrate.
	//
	/////////////////////////////////////////////////////////////////////////

	if (init_run(freq)<0){
		return(-1);
	}

	/////////////////////////////////////////////////////////////////////////
	//
	// Starting freerun mode.
	//
	/////////////////////////////////////////////////////////////////////////

	// Channel synchronization
	send("#PP10000500");
	usleep(2e5);
	send("#PP00000000");
	usleep(2e5);

Achim Morschhauser's avatar
Achim Morschhauser committed
307
308
309
310
311
312
313
314
315
316
317
318
319
320
	// Flush
        usleep(1e6);
	flush();
	
	if (freq < 10.0){
		// Use ASCII mode
		checked_send("#CS","",0,1);
		parse_data = &driver_obs_obsdaq::parse_data_ascii;
	} else {
		// Use binary mode
		checked_send("#CB","",0,1);
		parse_data = &driver_obs_obsdaq::parse_data_bin;
	}
		
Achim Morschhauser's avatar
Achim Morschhauser committed
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342

	/////////////////////////////////////////////////////////////////////////
	//
	// Listen to continuous measurement
	//
	/////////////////////////////////////////////////////////////////////////

	// Counting variable (Class variable)
	supp_count=0;

	// Infinite loop
	while (1) {

		// Receive data
		if (receive(buf, sizeof(buf), 5, &recv_time) >= 0) {

			//printf("Received: %s\n",buf);

			// Set the time
			data.set_time(&recv_time);

			// Parse data from answer
Achim Morschhauser's avatar
Achim Morschhauser committed
343
			if ( (this->*parse_data)(&data, buf) >= 0 ){
Achim Morschhauser's avatar
Achim Morschhauser committed
344

Achim Morschhauser's avatar
Achim Morschhauser committed
345
346
                            // Calibrate the measurement/ASCII
                            driver_obs::cal->calibrate(&data);
Achim Morschhauser's avatar
Achim Morschhauser committed
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371

			}

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

		}

	}

	return (0);

}

/****************************************************************************
 *									 										*
 * 			Stop the running mode of the OBSDAQ.							*
 *																			*
 ***************************************************************************/
int driver_obs_obsdaq::stop() {

	// Receive buffer
	char buf[100];

	// Stop free-run mode
372
	send("|||||||||||||||||||||||||||||||||||||||||");
Achim Morschhauser's avatar
Achim Morschhauser committed
373
	while (receive_raw(buf, sizeof(buf), 0.2) > 5) {
374
		send("||||||||||||||||||||||||||||||||||||||||||||");
Achim Morschhauser's avatar
Achim Morschhauser committed
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
	}

	return (0);

}

/****************************************************************************
 *																		 	*
 * Auto-calibrate.															*
 * 																			*
 * Channels are auto-calibrated based on actual channel settings.			*
 * Calibration is not stored.												*
 *																			*
 * IN:																		*
 *  - repeat	int 				Number of calibration steps.			*
 *  								Individual calibration steps are		*
 *  								averaged.								*
 * 																			*
 ***************************************************************************/
int driver_obs_obsdaq::auto_calibrate(int channel, int repeat){

Achim Morschhauser's avatar
Achim Morschhauser committed
396
397
398
399
400
    /////////////////////////////////////////////////////////////////////////
    //
    // Some variables.
    //
    /////////////////////////////////////////////////////////////////////////
Achim Morschhauser's avatar
Achim Morschhauser committed
401

Achim Morschhauser's avatar
Achim Morschhauser committed
402
    char cmd[50], recv[50];
Achim Morschhauser's avatar
Achim Morschhauser committed
403

Achim Morschhauser's avatar
Achim Morschhauser committed
404
    uint32_t n32, num;
Achim Morschhauser's avatar
Achim Morschhauser committed
405

Achim Morschhauser's avatar
Achim Morschhauser committed
406
407
    // The calibration constants
    double offset=0, scale=0;
Achim Morschhauser's avatar
Achim Morschhauser committed
408

Achim Morschhauser's avatar
Achim Morschhauser committed
409
410
411
412
413
    /////////////////////////////////////////////////////////////////////////
    //
    // Initialize: Set triggering off.
    //
    /////////////////////////////////////////////////////////////////////////
Achim Morschhauser's avatar
Achim Morschhauser committed
414

Achim Morschhauser's avatar
Achim Morschhauser committed
415
416
417
    // Set triggering off
    send("#PP00000000");
    usleep(1e6);
Achim Morschhauser's avatar
Achim Morschhauser committed
418

Achim Morschhauser's avatar
Achim Morschhauser committed
419
420
421
422
423
    /////////////////////////////////////////////////////////////////////////
    //
    // Calibration
    //
    /////////////////////////////////////////////////////////////////////////
Achim Morschhauser's avatar
Achim Morschhauser committed
424

Achim Morschhauser's avatar
Achim Morschhauser committed
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
    //
    // Repeat calibration step
    //
    
    for (int k=1; k<=repeat; k++){

        usleep(1e6);

        // Calibration command (Manual 12.4.15)
        
        sprintf(cmd,"$%1dWCF0",channel);
        send(cmd);

        // Check if calibration is finished
        
        sprintf(cmd,"$%1dRR",channel);
        while (checked_send(cmd,"R0",2,5)<0){
        }

        // Read the offset value
        
        sprintf(cmd,"$%1dRO",channel);
        send(cmd);
        //TODO Repeat loop instead of just omitting
        if (receive(recv,sizeof(recv),5)==0) {
                // Parse the offset value and add to average
                n32 = (uint32_t) strtoul(&recv[3], NULL, 16);
                num=0x800000L;
                offset += (double) (n32 ^ num) - (double) num;
        }

        // Necessary to avoid time outs
        usleep(1e6);

        // Read the full-scale value
        
        sprintf(cmd,"$%1dRF",channel);
        send(cmd);
        //TODO Repeat loop instead of just omitting
        if (receive(recv,sizeof(recv),5)==0) {
                // Parse the full-scale value and add to average
                n32 = (uint32_t) strtoul(&recv[3], NULL, 16);
                num=0x800000L;
                scale += (double) (n32 ^ num) - (double) num;
        }

    }
Achim Morschhauser's avatar
Achim Morschhauser committed
472

Achim Morschhauser's avatar
Achim Morschhauser committed
473
474
475
476
    //
    // Calculate the average of calibration constants and send them to
    // OBSDAQ.
    //
Achim Morschhauser's avatar
Achim Morschhauser committed
477

Achim Morschhauser's avatar
Achim Morschhauser committed
478
479
480
481
482
    // Full-scale
    n32 = ((int32_t) (offset/repeat) + (int32_t) 0x800000L)
                    ^ (int32_t) 0x800000L;
    sprintf(cmd,"$%1dWO%06X",channel,n32);
    send(cmd);
Achim Morschhauser's avatar
Achim Morschhauser committed
483

Achim Morschhauser's avatar
Achim Morschhauser committed
484
    std::cerr << "AUTO CAL: " << scale/repeat << " " << cmd << std::endl;
Achim Morschhauser's avatar
Achim Morschhauser committed
485

Achim Morschhauser's avatar
Achim Morschhauser committed
486
487
488
489
490
    // Offset
    n32 = ((int32_t) (scale/repeat)  + (int32_t) 0x800000L)
                    ^ (int32_t) 0x800000L ;
    sprintf(cmd,"$%1dWF%06X",channel,n32);
    send(cmd);
Achim Morschhauser's avatar
Achim Morschhauser committed
491

Achim Morschhauser's avatar
Achim Morschhauser committed
492
    std::cerr << "AUTO CAL: " << scale/repeat << " " << cmd << std::endl;
Achim Morschhauser's avatar
Achim Morschhauser committed
493

Achim Morschhauser's avatar
Achim Morschhauser committed
494
    return(0);
Achim Morschhauser's avatar
Achim Morschhauser committed
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704

}

/****************************************************************************
 ****************************************************************************
 *																			*
 * 			      Protected Methods.											*
 *																			*
 ****************************************************************************
 ***************************************************************************/

/****************************************************************************
 *									 										*
 *	       Set the termination characters for OBSDAQ.						*
 *																			*
 ***************************************************************************/
int driver_obs_obsdaq::set_term() {

	char term[1];

	term[0] = 13;

	if (Driver_Obs_Serial::set_term(term, 1) < 0) {
		return (-1);
	}

	return (0);

}

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

/****************************************************************************
 *								 											*
 *	       Send an OBSDAQ command.											*
 *																			*
 ***************************************************************************/
int driver_obs_obsdaq::send(const char* cmd) {

	return (send((char *) cmd));

}

/****************************************************************************
 *																			*
 *	       Send an OBSDAQ command.											*
 *																			*
 ***************************************************************************/
int driver_obs_obsdaq::send(char* cmd) {

	std::string cmd_send;

	cmd_send = cmd[0] + RS485_addrstr + &cmd[1] + (char) 13;

	//TODO Customization with waiting time for baud 115200 should go here!
	//     Currently, this is done in driver_serial::send

	Driver_Obs_Serial::send(cmd_send.c_str());

	return (0);

}

/****************************************************************************
 *									 										*
 *	    Send an OBSDAQ command to address of serial port and check reply	*
 *																			*
 * IN:																		*
 *  - cmd		const char*			Command to send							*
 *  - reply		const char*			Expected reply							*
 *  - digits	int					Number of 'digits' first characters 	*
 *                                  that are checked to match the received	*
 *                                  answer.									*
 *  - timeout	double				Time in [s] to wait for answer			*
 *  - verbose	int					Print error messages					*
 *																			*
 ***************************************************************************/
int driver_obs_obsdaq::checked_send(const char* cmd, const char* reply,
		int digits, double timeout, int verbose) {

	std::string recv;

	return(checked_send(cmd, reply, digits, timeout, &recv, verbose));

}

/****************************************************************************
 *									 										*
 *	    Send an OBSDAQ command to address of serial port and check reply	*
 *																			*
 * IN:																		*
 *  - cmd		const char*			Command to send							*
 *  - reply		const char*			Expected reply							*
 *  - digits	int					Number of 'digits' first characters 	*
 *                                  that are checked to match the received	*
 *                                  answer.									*
 *  - timeout	double				Time in [s] to wait for answer			*
 *  - recv		std::string*		Received answer							*
 *  - verbose	int					Print error messages					*
 *																			*
 ***************************************************************************/
int driver_obs_obsdaq::checked_send(const char* cmd, const char* reply,
		int digits, double timeout, std::string* recv, int verbose) {


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


	char buf[1000];
	std::string strbuf;
	std::string expected;

	/////////////////////////////////////////////////////////////////////////
	//
	// Send command and check answer.
	//
	/////////////////////////////////////////////////////////////////////////

	// Send the command
	send(cmd);

	// Wait for an answer is received
	if (receive(buf, sizeof(buf), timeout) < 0) {
		if (verbose) {
			fprintf(stderr,"Reply not received for cmd: %s\n",cmd);
		}
		return (-1);
	}
	strbuf=std::string(buf);

	// Construct expected answer
	expected = "!" + RS485_addrstr + reply;

	// Check address part of answer
	if ((expected.compare(1, 2, &buf[1], 2) != 0)
			&& (RS485_addr != 0x0F)){

		if (verbose) {
			std::cerr << "Wrong address received." << " Expected: "
		    		  << expected.substr(1, 2) << " Received: " << &buf[1]
					  << std::endl;
		}

		return (-2);

	}

	// Check response part of answer
	if ((expected.compare(3, digits, &buf[3], digits) != 0)) {

		if (verbose) {

			std::cerr << "Wrong answer received." << " Expected: "
					  << expected.substr(3, digits) << " Received: "
					  << strbuf.substr(3, digits) << std::endl;

		}

		return (-3);

	}

	// Return response
	*recv = &buf[3 + digits];

	return (0);

}

/****************************************************************************
 *									 										*
 * Parse the input in ASCII format											*
 * 																			*
 ***************************************************************************/
int driver_obs_obsdaq::parse_data_ascii(data_obs_vector* data, char* buf) {

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

	double n[5];
	uint32_t n32, num;

	// Set data to missing
	data->set_data_MD();

	/////////////////////////////////////////////////////////////////////////
	//
	// Parse each channel (Manual 12.5.1).
	//
	/////////////////////////////////////////////////////////////////////////

	// Initialize
	int pos = 1;
	int i = 0;

	// Channels 1-3
	char tmp[7];
705
	num = (int32_t) 0x800000L; // was uint32_t
Achim Morschhauser's avatar
Achim Morschhauser committed
706
707
708
709
	while (i < 3) {
		strncpy(tmp, &buf[pos], 6);
		tmp[6] = 0;
		pos += 6;
710
		n32 = (int32_t) strtol(tmp, NULL, 16); // was strtoul // was uint32_t
Achim Morschhauser's avatar
Achim Morschhauser committed
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
		n[i++] = ((double) ((int32_t) ((n32 ^ num) - num))) * M;
	}

	// If free-run format
	if (strncmp(buf, "*", 1) == 0) {
		pos = pos + 2;
	}

	// Channels 4-6 (CH4,CH5,CH6)
	// ObsDaq Voltage and Temperature is not read
	if (strlen(buf)>pos){
		pos += 8;
		num = (uint32_t) 0x8000;
		while (i < 5) {
			strncpy(tmp, &buf[pos], 4);
			tmp[4] = 0;
			pos += 4;
			n32 = (uint32_t) strtoul(tmp, NULL, 16);
			n[i++] = ((double) ((int32_t) ((n32 ^ num) - num))) / 8000.0;
		}
	}

	/*
	// ObsDAQ T
	n[3]=n[3]/128;
	// ObsDAQ V
	n[4]=n[4]*0.000575+1.0;
	*/

	// Save the data
	data->set_data(n);

}

/****************************************************************************
 *									 										*
 * 			     Parse the input in binary format							*
 * 			     															*
 ***************************************************************************/
int driver_obs_obsdaq::parse_data_bin(data_obs_vector* data, char* buf) {

	// TODO Use for sampling rates higher than 10 Hz (else, suppl. data is
	//      not returned).
	// OBSDAQ uses LittleEndian

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

	double n[5];
	uint32_t n32, num;

	// Record length (is updated below)
	unsigned char reclen=12;

	// Temporary variable
	int tmp_d;

	// Set data to missing
	data->set_data_MD();

	////////////////////////////////////////////////////////////////////////
	//
	// Parse the binary data (Manual 12.5.2).
	//
	/////////////////////////////////////////////////////////////////////////

	//
	// Some checks on data consistency are performed
	//

	// Check for supplementary channels
	if (buf[1] & (unsigned char) 64){
		reclen = 14;
	}

	// Check 7th bits
	for (int i=0; i<strlen(buf);i++){
791
792
793
794
795
796
		if ((unsigned char) buf[i]<32 || (unsigned  char) buf[i]>159){
			fprintf(stderr,"Faulty data: 7th bit check failed for byte %d: %d.\n",
					i,(unsigned char) buf[i]);
			//fprintf(stderr,"%d %d %d %d %d %d %d %d\n",buf[i]&1,(buf[i]&2)/2,
			//        (buf[i]&4)/4,(buf[i]&8)/8,(buf[i]&16)/16,(buf[i]&32)/32,
			//        (buf[i]&64)/64,(buf[i]&128)/128);
Achim Morschhauser's avatar
Achim Morschhauser committed
797
798
799
800
801

			return(-1);
		}
	}

802
	// Check checksum byte
Achim Morschhauser's avatar
Achim Morschhauser committed
803
	tmp_d=0;
804
        // Sum over N-1 data bytes
Achim Morschhauser's avatar
Achim Morschhauser committed
805
806
807
	for (int i=0; i<reclen-1; i++){
		tmp_d += buf[i];
	}
808
        // Last byte must contain sum of last (N-1) bytes in bits 0-6
Achim Morschhauser's avatar
Achim Morschhauser committed
809
810
	if ( (tmp_d&127) != (buf[reclen-1]&127) ){
		fprintf(stderr,"Faulty data: Control sum check failed for ");
811
		for (int i=0; i<reclen-1;i++){
Achim Morschhauser's avatar
Achim Morschhauser committed
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
			fprintf(stderr,"%d ",buf[i]);
		}
		fprintf(stderr,"and values %d %d %d.\n",
				tmp_d,tmp_d&127,buf[reclen-1]);
		return(-2);
	}

	// Check supplementary data
	if (buf[1]&64){
		if (buf[1]&1){
			supp_count=1;
		} else if (supp_count) {
			supp_count+=1;
		}
	}

	//
	// Extract data from channels 1-3
	//

	num = (uint32_t) 0x800000L;

	// Channel 1
	n32  = ( ( (buf[2 ]&127) + ((buf[0]&32)<<2) ) <<16 )
	     + ( ( (buf[3 ]&127) + ((buf[0]&16)<<3) ) <<8  )
		 + ( ( (buf[4 ]&127) + ((buf[0]&8 )<<4) )      );
	n[0] = ((double) ((int32_t) ((n32 ^ num) - num))) * M;
	//printf("XYZ: %u %u %f\n",buf[2]&127,n32,n[1]);

	// Channel 2
	n32  = ( ( (buf[5 ]&127) + ((buf[0]&4 )<<5) ) <<16 )
	     + ( ( (buf[6 ]&127) + ((buf[0]&2 )<<6) ) <<8  )
		 + ( ( (buf[7 ]&127) + ((buf[0]&1 )<<7) )      );
	n[1] = ((double) ((int32_t) ((n32 ^ num) - num))) * M;
	//printf("XYZ: %u %f\n",n32,n[2]);

    // Channel 3
    n32  = ( ( (buf[8 ]&127) + ((buf[1]&32)<<2) ) <<16 )
         + ( ( (buf[9 ]&127) + ((buf[1]&16)<<3))  <<8  )
         + ( ( (buf[10]&127) + ((buf[1]&8 )<<4))       );
    n[2] = ((double) ((int32_t) ((n32 ^ num) - num))) * M;
	//printf("XYZ: %u %f\n",n32,n[3]);

    //
	// Extract data from supplementary channels
    //

	num = (uint32_t) 0x8000;

	if ((buf[1]&64) && supp_count >= 3 && supp_count <=4){
		n32  = ( ( (buf[11 ]&127) + ((buf[0]&4 )<<5) ) <<8  )
			 + ( ( (buf[12 ]&127) + ((buf[0]&2 )<<6) )      );
		n[supp_count] = ((double) ((int32_t) ((n32 ^ num) - num))) / 8000.0;
	}

	// Save the data
	data->set_data(n);

	return(0);

}

/****************************************************************************
 *									 										*
 * 			     Initialize the OBSDAQ.										*
 *																			*
 * Set filter rates.														*
 *																			*
 ***************************************************************************/
int driver_obs_obsdaq::init() {
Achim Morschhauser's avatar
Achim Morschhauser committed
882
    
Achim Morschhauser's avatar
Achim Morschhauser committed
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
	/////////////////////////////////////////////////////////////////////////
	//
	// Stop running data acquisition.
	//
	/////////////////////////////////////////////////////////////////////////

	stop();

	/////////////////////////////////////////////////////////////////////////
	//
	// Stop the internal trigger.
	//
	/////////////////////////////////////////////////////////////////////////

	send("#PP00000000");
	usleep(2e6);

	/////////////////////////////////////////////////////////////////////////
	//
	// Get the quartz crystal frequency.
	//
	/////////////////////////////////////////////////////////////////////////

	std::string recv;

	checked_send("$QF", "R", 1, 1, &recv);
	std::cerr << "Received: " << recv << std::endl;

	quartz_freq = atoi(recv.c_str());

	// Set the valid output rates of the digital filter (Table 6 in Manual)

	switch (quartz_freq) {

	case 76:
		// 7.6800 MHz
		valid_filter_rates[0] = 2.5;
		valid_filter_rates[1] = 5.0;
		valid_filter_rates[2] = 10.0;
		valid_filter_rates[3] = 15.0;
		valid_filter_rates[4] = 25.0;
		valid_filter_rates[5] = 30.0;
		valid_filter_rates[6] = 50.0;
		valid_filter_rates[7] = 60.0;
		valid_filter_rates[8] = 100.0;
		valid_filter_rates[9] = 500.0;
		valid_filter_rates[10] = 1000.0;
		break;
	case 92:
		// 9.2160 MHz
		valid_filter_rates[0] = 3.0;
		valid_filter_rates[1] = 6.0;
		valid_filter_rates[2] = 12.0;
		valid_filter_rates[3] = 18.0;
		valid_filter_rates[4] = 30.0;
		valid_filter_rates[5] = 36.0;
		valid_filter_rates[6] = 60.0;
		valid_filter_rates[7] = 72.0;
		valid_filter_rates[8] = 120.0;
		valid_filter_rates[9] = 600.0;
		valid_filter_rates[10] = 1200.0;
		break;
	case 98:
		// 9.8304 MHz (default)
		valid_filter_rates[0] = 3.2;
		valid_filter_rates[1] = 6.4;
		valid_filter_rates[2] = 12.8;
		valid_filter_rates[3] = 19.2;
		valid_filter_rates[4] = 32.0;
		valid_filter_rates[5] = 38.4;
		valid_filter_rates[6] = 64.0;
		valid_filter_rates[7] = 76.8;
		valid_filter_rates[8] = 128.0;
		valid_filter_rates[9] = 640.0;
		valid_filter_rates[10] = 1280.0;
		break;
	}

	// Define the filter commands (Table 6 in Manual)
962
963
	filter_cmds[0]  = "03";
	filter_cmds[1]  = "13";
Achim Morschhauser's avatar
Achim Morschhauser committed
964
965
	filter_cmds[2]  = "23";
	filter_cmds[3]  = "33";
966
	filter_cmds[4]  = "43";
Achim Morschhauser's avatar
Achim Morschhauser committed
967
968
	filter_cmds[5]  = "53";
	filter_cmds[6]  = "63";
969
	filter_cmds[7]  = "72";
Achim Morschhauser's avatar
Achim Morschhauser committed
970
	filter_cmds[8]  = "82";
Achim Morschhauser's avatar
Achim Morschhauser committed
971
	filter_cmds[9]  = "92";
Achim Morschhauser's avatar
Achim Morschhauser committed
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
	filter_cmds[10] = "A1";

	return (0);

}

/****************************************************************************
 *									 										*
 * Prepare data acquisition.												*
 * This is common code for freerun and triggered mode.						*
 * 																			*
 * - Set the frequency														*
 * - Calibration of channels												*
 * - TODO Reset appropriate baud rate (should this be done automatically ?)	*
 *																			*
 ***************************************************************************/
int driver_obs_obsdaq::init_run(double freq){

	// The receive buffer
	char buf[200];
	// A command to send
	char cmd[20];
	// Index of the selected frequency
	int freq_sel;

	// ADC calibration
	int adc_scale, adc_offset;

Achim Morschhauser's avatar
Achim Morschhauser committed
1000
        /////////////////////////////////////////////////////////////////////