driver_obs_obsdaq.cpp 35.7 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

// 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
26
#include <unordered_map>
Achim Morschhauser's avatar
Achim Morschhauser committed
27

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

Achim Morschhauser's avatar
Achim Morschhauser committed
33
34
35
36
37
38
39
40
41
42
43
/*******************************************************************************
 *******************************************************************************
 *																			*
 * 				Initialize static variables												*
 *																			*
 *******************************************************************************
 ******************************************************************************/

// Baud rate codes (Table 5 of Manual)
const std::unordered_map<int,std::string> driver_obs_obsdaq::map_baud(
    {{1200,"03"},{2400,"04"},{4800,"05"},{9600,"06"},{19200,"07"},
Achim Morschhauser's avatar
Achim Morschhauser committed
44
     {38400,"08"},{57600,"09"},{115200,"0A"},{307200,"0B"},{614400,"0C"}});
Achim Morschhauser's avatar
Achim Morschhauser committed
45

Achim Morschhauser's avatar
Achim Morschhauser committed
46
47
/*******************************************************************************
 *******************************************************************************
Achim Morschhauser's avatar
Achim Morschhauser committed
48
49
50
 *																			*
 * 				Constructors.												*
 *																			*
Achim Morschhauser's avatar
Achim Morschhauser committed
51
52
 *******************************************************************************
 ******************************************************************************/
Achim Morschhauser's avatar
Achim Morschhauser committed
53

Achim Morschhauser's avatar
Achim Morschhauser committed
54
/*******************************************************************************
Achim Morschhauser's avatar
Achim Morschhauser committed
55
56
57
58
59
*																			*
* Automatically determine the baud rate.									*
*																			*
* Default address is broadcast address: 0x0F.								*
*																			*
Achim Morschhauser's avatar
Achim Morschhauser committed
60
*******************************************************************************/
Achim Morschhauser's avatar
Achim Morschhauser committed
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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.
Achim Morschhauser's avatar
Achim Morschhauser committed
76
        find_baud();
77
        stop();
Achim Morschhauser's avatar
Achim Morschhauser committed
78
79
80
81
82
83
84
85
86
87
88
89
90

	// Initialize the OBSDAQ
	init();

}

/****************************************************************************
*																			*
* Pre-defined baud rate.													*
* 																			*
* Default address is broadcast address: 0x0F.								*
*																			*
****************************************************************************/
Achim Morschhauser's avatar
Achim Morschhauser committed
91
92
driver_obs_obsdaq::driver_obs_obsdaq(std::string port, int baud, 
                buffer_obs* buffer, Obs_Calibration_Vector* cal, int addr) :
Achim Morschhauser's avatar
Achim Morschhauser committed
93
				Driver_Obs_Serial(port, buffer, cal) {
Achim Morschhauser's avatar
Achim Morschhauser committed
94
95

	// Set the termination characters for serial port driver
96
	set_term();
Achim Morschhauser's avatar
Achim Morschhauser committed
97
98
99
100
101
102
103

	// 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
104
        /* In development
105

Achim Morschhauser's avatar
Achim Morschhauser committed
106
107
        // Check if requested baud rate is already set
        if ( check_baud(baud) != 1){
Achim Morschhauser's avatar
Achim Morschhauser committed
108
        
Achim Morschhauser's avatar
Achim Morschhauser committed
109
110
111
112
113
114
            // Check for baud rate with the right address.
            // Stop any running measurement when doing so.
            find_baud();
            
            // Set baud rate
            set_baud(baud);
Achim Morschhauser's avatar
Achim Morschhauser committed
115

Achim Morschhauser's avatar
Achim Morschhauser committed
116
        // Set the ObsDAQ baud rate
Achim Morschhauser's avatar
Achim Morschhauser committed
117

Achim Morschhauser's avatar
Update    
Achim Morschhauser committed
118
        */
Achim Morschhauser's avatar
Achim Morschhauser committed
119
	// Stop the measurement
120
121
        //TODO Reactivate later, baud rate needs to be set when calling stop() !
	//stop();
Achim Morschhauser's avatar
Update    
Achim Morschhauser committed
122

123
        if ( check_baud(baud) != 1){
124
125
126
            find_baud();
            stop();
            set_baud(baud);
127
128
        } else {
            stop();
129
130
        }
        
Achim Morschhauser's avatar
Achim Morschhauser committed
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
	// 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
179
	char buf[100]="";
Achim Morschhauser's avatar
Achim Morschhauser committed
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

	/////////////////////////////////////////////////////////////////////////
	//
	// 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
237
	char buf[200]="";
Achim Morschhauser's avatar
Achim Morschhauser committed
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

	/////////////////////////////////////////////////////////////////////////
	//
	// 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);

			}

		}

	}

}

/****************************************************************************
 *									 										*
Achim Morschhauser's avatar
Achim Morschhauser committed
288
 * Run the OBSDAQ in freerun mode (no triggering).							*
Achim Morschhauser's avatar
Achim Morschhauser committed
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
 *																			*
 ***************************************************************************/
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
304
	char buf[200]="";
Achim Morschhauser's avatar
Achim Morschhauser committed
305
306
307
	// 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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330

	/////////////////////////////////////////////////////////////////////////
	//
	// 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
GNUPLOT    
Achim Morschhauser committed
331
	// Flush
332
        usleep(1e6);
Achim Morschhauser's avatar
GNUPLOT    
Achim Morschhauser committed
333
334
	flush();
	
Achim Morschhauser's avatar
Achim Morschhauser committed
335
	if (driver_obs::freq < 10.0){
Achim Morschhauser's avatar
Achim Morschhauser committed
336
337
338
339
340
341
342
343
		// 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
GNUPLOT    
Achim Morschhauser committed
344
		
Achim Morschhauser's avatar
Achim Morschhauser committed
345
346
347
348
349
350
351
352
353
354
355
356
357
358

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

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

	// Infinite loop
	while (1) {

		// Receive data
Achim Morschhauser's avatar
Achim Morschhauser committed
359
		if (receive(buf, sizeof(buf), 0, &recv_time) >= 0) {
Achim Morschhauser's avatar
Achim Morschhauser committed
360
361
362
363
364
365
366

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

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

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

369
370
                            // Calibrate the measurement/ASCII
                            driver_obs::cal->calibrate(&data);
Achim Morschhauser's avatar
Achim Morschhauser committed
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392

			}

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

		}

	}

	return (0);

}

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

	// Receive buffer
393
	char stopstr[41]="||||||||||||||||||||||||||||||||||||||||";
Achim Morschhauser's avatar
Achim Morschhauser committed
394
395

	// Stop free-run mode
396
397
398
	send(stopstr);
	while (checked_send("$M","<ObsDaq",7,1,0) != 0) {
            send(stopstr);
Achim Morschhauser's avatar
Achim Morschhauser committed
399
400
401
402
403
404
405
	}

	return (0);

}

/****************************************************************************
406
407
408
409
410
411
412
413
414
415
 *
 * 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.
 *
Achim Morschhauser's avatar
Achim Morschhauser committed
416
417
418
 ***************************************************************************/
int driver_obs_obsdaq::auto_calibrate(int channel, int repeat){

Achim Morschhauser's avatar
Achim Morschhauser committed
419
420
421
422
423
    /////////////////////////////////////////////////////////////////////////
    //
    // Some variables.
    //
    /////////////////////////////////////////////////////////////////////////
Achim Morschhauser's avatar
Achim Morschhauser committed
424

425
    char cmd[50], recv[50]="";
Achim Morschhauser's avatar
Achim Morschhauser committed
426

Achim Morschhauser's avatar
Achim Morschhauser committed
427
    uint32_t n32, num;
Achim Morschhauser's avatar
Achim Morschhauser committed
428

Achim Morschhauser's avatar
Achim Morschhauser committed
429
430
    // The calibration constants
    double offset=0, scale=0;
Achim Morschhauser's avatar
Achim Morschhauser committed
431

Achim Morschhauser's avatar
Achim Morschhauser committed
432
433
434
435
436
    /////////////////////////////////////////////////////////////////////////
    //
    // Initialize: Set triggering off.
    //
    /////////////////////////////////////////////////////////////////////////
Achim Morschhauser's avatar
Achim Morschhauser committed
437

Achim Morschhauser's avatar
Achim Morschhauser committed
438
439
440
    // Set triggering off
    send("#PP00000000");
    usleep(1e6);
Achim Morschhauser's avatar
Achim Morschhauser committed
441

Achim Morschhauser's avatar
Achim Morschhauser committed
442
443
444
445
446
    /////////////////////////////////////////////////////////////////////////
    //
    // Calibration
    //
    /////////////////////////////////////////////////////////////////////////
Achim Morschhauser's avatar
Update    
Achim Morschhauser committed
447
448
449
450

    //
    // Repeat calibration step
    //
Achim Morschhauser's avatar
Achim Morschhauser committed
451
452
453
454
455
456
457
458
459
460
461
462
463
    
    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);
Achim Morschhauser's avatar
Achim Morschhauser committed
464
        usleep(4.0/driver_obs::freq*1e6);
Achim Morschhauser's avatar
Achim Morschhauser committed
465
466
467
468
469
470
471
472
473
474
475
476
        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;
Achim Morschhauser's avatar
Update    
Achim Morschhauser committed
477
                offset += (double) (n32 ^ num) - (double) num;
Achim Morschhauser's avatar
Achim Morschhauser committed
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
        }

        // 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
496

Achim Morschhauser's avatar
Achim Morschhauser committed
497
498
499
500
    //
    // Calculate the average of calibration constants and send them to
    // OBSDAQ.
    //
501
502
503
504
505
506
    
    // 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
507

508
    std::cerr << "Set offset: " << scale/repeat << " " << cmd << std::endl;
Achim Morschhauser's avatar
Achim Morschhauser committed
509

Achim Morschhauser's avatar
Achim Morschhauser committed
510
511
512
513
514
    // 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
515

516
    std::cerr << "Set scale: " << scale/repeat << " " << cmd << std::endl;
Achim Morschhauser's avatar
Achim Morschhauser committed
517

Achim Morschhauser's avatar
Achim Morschhauser committed
518
    return(0);
Achim Morschhauser's avatar
Achim Morschhauser committed
519
520
521

}

522
523
524
525
526
527
528
529
530
531
532
533
534
535
/***************************************************************************
 *
 * 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::full_calibrate(int channel, int repeat){
    
Achim Morschhauser's avatar
Achim Morschhauser committed
536
    return(0);
537
    
Achim Morschhauser's avatar
Achim Morschhauser committed
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
}

/****************************************************************************
 ****************************************************************************
 *																			*
 * 			      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));

}

/****************************************************************************
630
631
632
633
634
635
636
637
638
639
640
641
642
 *
 *	    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
 *
Achim Morschhauser's avatar
Achim Morschhauser committed
643
644
645
646
 ***************************************************************************/
int driver_obs_obsdaq::checked_send(const char* cmd, const char* reply,
		int digits, double timeout, std::string* recv, int verbose) {

647
    flush();
Achim Morschhauser's avatar
Achim Morschhauser committed
648

649
650
651
652
653
    /////////////////////////////////////////////////////////////////////////
    //
    // Initialize some variables.
    //
    /////////////////////////////////////////////////////////////////////////
Achim Morschhauser's avatar
Achim Morschhauser committed
654
655


656
657
658
    char buf[1000]="";
    std::string strbuf;
    std::string expected;
Achim Morschhauser's avatar
Achim Morschhauser committed
659

660
661
662
663
664
    /////////////////////////////////////////////////////////////////////////
    //
    // Send command and check answer.
    //
    /////////////////////////////////////////////////////////////////////////
Achim Morschhauser's avatar
Achim Morschhauser committed
665

666
667
    // Send the command
    send(cmd);
Achim Morschhauser's avatar
Achim Morschhauser committed
668

669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
    // Wait for an answer
    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);
    }
Achim Morschhauser's avatar
Achim Morschhauser committed
691

692
693
694
695
696
697
698
699
700
    // 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);
    }
Achim Morschhauser's avatar
Achim Morschhauser committed
701

702
703
    // Return response
    *recv = &buf[3 + digits];
Achim Morschhauser's avatar
Achim Morschhauser committed
704

705
    return (0);
Achim Morschhauser's avatar
Achim Morschhauser committed
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721

}

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

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

Achim Morschhauser's avatar
Achim Morschhauser committed
722
	double n[5];
Achim Morschhauser's avatar
Achim Morschhauser committed
723
724
725
726
	uint32_t n32, num;

	// Set data to missing
	data->set_data_MD();
Achim Morschhauser's avatar
Achim Morschhauser committed
727
        std::fill(n,n+5,data->get_MD());
Achim Morschhauser's avatar
Achim Morschhauser committed
728
729
730
731
732
733
734
735
736
737
738
739
740

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

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

	// Channels 1-3
	char tmp[7];
741
	num = (int32_t) 0x800000L; // was uint32_t
Achim Morschhauser's avatar
Achim Morschhauser committed
742
743
744
745
	while (i < 3) {
		strncpy(tmp, &buf[pos], 6);
		tmp[6] = 0;
		pos += 6;
746
		n32 = (int32_t) strtol(tmp, NULL, 16); // was strtoul // was uint32_t
Achim Morschhauser's avatar
Achim Morschhauser committed
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
		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);
Achim Morschhauser's avatar
Achim Morschhauser committed
778
779
        
        return(0);
Achim Morschhauser's avatar
Achim Morschhauser committed
780
781
782
783
784
785
786
787
788
789
790

}

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


791
792
793
794
795
    /////////////////////////////////////////////////////////////////////////
    //
    // Some variables.
    //
    /////////////////////////////////////////////////////////////////////////
Achim Morschhauser's avatar
Achim Morschhauser committed
796

Achim Morschhauser's avatar
Achim Morschhauser committed
797
    double n[5];
798
    uint32_t n32, num;
Achim Morschhauser's avatar
Achim Morschhauser committed
799

800
801
    // Record length (is updated below)
    unsigned char reclen=12;
Achim Morschhauser's avatar
Achim Morschhauser committed
802

803
804
    // Temporary variable
    int tmp_d;
Achim Morschhauser's avatar
Achim Morschhauser committed
805

806
807
    // Set data to missing
    data->set_data_MD();
Achim Morschhauser's avatar
Achim Morschhauser committed
808
    std::fill(n,n+5,data->get_MD());
Achim Morschhauser's avatar
Achim Morschhauser committed
809

810
811
812
813
814
    ////////////////////////////////////////////////////////////////////////
    // TODO Use for sampling rates higher than 10 Hz (else, suppl. data is
    //      not returned).
    // OBSDAQ uses LittleEndian
    ////////////////////////////////////////////////////////////////////////
Achim Morschhauser's avatar
Achim Morschhauser committed
815

816
817
818
819
820
821
822
    if (driver_obs::freq < 10.0){
        std::cerr << "ERROR: Binary mode not available for sampling rate "
            << "of lower than 10 Hz. Current sampling rate is "
            << driver_obs::freq 
            << " Hz." << std::endl;
        return(-1);
    }
Achim Morschhauser's avatar
Achim Morschhauser committed
823

824
825
826
827
828
    ////////////////////////////////////////////////////////////////////////
    //
    // Parse the binary data (Manual 12.5.2).
    //
    /////////////////////////////////////////////////////////////////////////
Achim Morschhauser's avatar
Update    
Achim Morschhauser committed
829

830
831
832
    //
    // Some checks on data consistency are performed
    //
Achim Morschhauser's avatar
Achim Morschhauser committed
833

834
835
836
837
    // Check for supplementary channels
    if (buf[1] & (unsigned char) 64){
        reclen = 14;
    }
Achim Morschhauser's avatar
Achim Morschhauser committed
838

839
840
841
842
843
844
845
846
    // Check 7th bits
    for (int i=0; i<strlen(buf);i++){
        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
847

848
849
850
            return(-1);
        }
    }
Achim Morschhauser's avatar
Achim Morschhauser committed
851

852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
    // Check checksum byte
    tmp_d=0;
    // Sum over N-1 data bytes
    for (int i=0; i<reclen-1; i++){
            tmp_d += buf[i];
    }
    // Last byte must contain sum of last (N-1) bytes in bits 0-6
    if ( (tmp_d&127) != (buf[reclen-1]&127) ){
        fprintf(stderr,"Faulty data: Control sum check failed for ");
        for (int i=0; i<reclen-1;i++){
            fprintf(stderr,"%d ",buf[i]);
        }
        fprintf(stderr,"and values %d %d %d.\n",
                        tmp_d,tmp_d&127,buf[reclen-1]);
        return(-2);
    }
Achim Morschhauser's avatar
Achim Morschhauser committed
868

869
870
871
872
873
874
875
876
877
878
879
880
    // 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
    //
Achim Morschhauser's avatar
Achim Morschhauser committed
881

882
    num = (uint32_t) 0x800000L;
Achim Morschhauser's avatar
Achim Morschhauser committed
883

884
885
886
887
888
889
    // 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]);
Achim Morschhauser's avatar
Achim Morschhauser committed
890

891
892
893
894
895
896
    // 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]);
Achim Morschhauser's avatar
Achim Morschhauser committed
897
898
899
900
901
902
903
904
905

    // 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]);

    //
906
    // Extract data from supplementary channels
Achim Morschhauser's avatar
Achim Morschhauser committed
907
908
    //

909
910
911
912
913
914
    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;
    }
Achim Morschhauser's avatar
Achim Morschhauser committed
915

916
917
    // Save the data
    data->set_data(n);
Achim Morschhauser's avatar
Achim Morschhauser committed
918

919
    return(0);
Achim Morschhauser's avatar
Achim Morschhauser committed
920
921
922
923
924
925
926
927
928
929
930

}

/****************************************************************************
 *									 										*
 * 			     Initialize the OBSDAQ.										*
 *																			*
 * Set filter rates.														*
 *																			*
 ***************************************************************************/
int driver_obs_obsdaq::init() {
Achim Morschhauser's avatar
Achim Morschhauser committed
931
    
932
933
    std::string recv;
    
Achim Morschhauser's avatar
Achim Morschhauser committed
934
935
936
937
938
    /////////////////////////////////////////////////////////////////////////
    //
    // Stop running data acquisition.
    //
    /////////////////////////////////////////////////////////////////////////
Achim Morschhauser's avatar
Achim Morschhauser committed
939

940
941
    // Should never run, as private method
    //stop();
Achim Morschhauser's avatar
Achim Morschhauser committed
942

Achim Morschhauser's avatar
Achim Morschhauser committed
943
944
945
946
947
    /////////////////////////////////////////////////////////////////////////
    //
    // Stop the internal trigger.
    //
    /////////////////////////////////////////////////////////////////////////
Achim Morschhauser's avatar
Achim Morschhauser committed
948

Achim Morschhauser's avatar
Achim Morschhauser committed
949
950
    send("#PP00000000");
    usleep(2e6);
Achim Morschhauser's avatar
Achim Morschhauser committed
951

952
953
954
955
956
957
958
959
960
    /////////////////////////////////////////////////////////////////////////
    //
    // Get the serial number
    //
    /////////////////////////////////////////////////////////////////////////
    
    checked_send("$SN", "SN", 2, 1, &recv);
    sn = recv;    
    
Achim Morschhauser's avatar
Achim Morschhauser committed
961
962
963
964
965
    /////////////////////////////////////////////////////////////////////////
    //
    // Get the quartz crystal frequency.
    //
    /////////////////////////////////////////////////////////////////////////
Achim Morschhauser's avatar
Achim Morschhauser committed
966

Achim Morschhauser's avatar
Achim Morschhauser committed
967
968
969
970
971
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
1000
    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;
For faster browsing, not all history is shown. View entire blame