Debian Codegraph @

comedi-calibrate/comedi_calibrate/comedi_calibrate.c

raw
1 /*
2 A little auto-calibration utility, for boards
3 that support it.
4
5 copyright (C) 1999,2000,2001,2002 by David Schleef
6 copyright (C) 2003 by Frank Mori Hess
7
8 */
9
10 /***************************************************************************
11 * *
12 * This program is free software; you can redistribute it and/or modify *
13 * it under the terms of the GNU Lesser General Public License as *
14 * published by *
15 * the Free Software Foundation; either version 2.1 of the License, or *
16 * (at your option) any later version. *
17 * *
18 ***************************************************************************/
19
20 #define _GNU_SOURCE
21
22 #include <stdio.h>
23 #include "comedilib.h"
24 #include <fcntl.h>
25 #include <unistd.h>
26 #include <errno.h>
27 #include <getopt.h>
28 #include <ctype.h>
29 #include <math.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <assert.h>
33
34 #include "calib.h"
35
36 /* global variables */
37 int verbose = 0;
38
39 struct board_struct{
40 char *name;
41 char *id;
42 int (*init_setup)( calibration_setup_t *setup, const char *device_name );
43 };
44
45 struct board_struct drivers[] = {
46 { "ni_pcimio", ni_id, ni_setup },
47 { "ni_atmio", ni_id, ni_setup },
48 { "ni_mio_cs", ni_id, ni_setup },
49 { "cb_pcidas", cb_id, cb_setup },
50 { "cb_pcidas64", cb64_id, cb64_setup },
51 { "ni_labpc", ni_labpc_id, ni_labpc_setup },
52 };
53 #define n_drivers (sizeof(drivers)/sizeof(drivers[0]))
54
55 void help(void)
56 {
57 printf("comedi_calibrate [options] - autocalibrates a Comedi device\n");
58 printf(" --verbose, -v \n");
59 printf(" --quiet, -q \n");
60 printf(" --help, -h \n");
61 printf(" --file, -f [/dev/comediN] \n");
62 printf(" --save-file, -S [filepath] \n");
63 printf(" --driver-name [driver] \n");
64 printf(" --device-name [device] \n");
65 printf(" --[no-]reset \n");
66 printf(" --[no-]calibrate \n");
67 printf(" --[no-]dump \n");
68 printf(" --[no-]results \n");
69 printf(" --[no-]output \n");
70 }
71
72 typedef struct
73 {
74 int verbose;
75 const char *file_path;
76 const char *save_file_path;
77 const char *driver_name;
78 const char *device_name;
79 int do_reset;
80 int do_dump;
81 int do_calibrate;
82 int do_results;
83 int do_output;
84 unsigned int subdevice;
85 unsigned int channel;
86 unsigned int range;
87 unsigned int aref;
88 } parsed_options_t;
89
90 static void parse_options( int argc, char *argv[], parsed_options_t *settings )
91 {
92 int c, index;
93
94 struct option options[] = {
95 { "verbose", 0, 0, 'v' },
96 { "quiet", 0, 0, 'q' },
97 { "file", 1, 0, 'f' },
98 { "save-file", 1, 0, 'S' },
99 { "help", 0, 0, 'h' },
100 { "driver-name", 1, 0, 0x1000 },
101 { "device-name", 1, 0, 0x1001 },
102 { "reset", 0, &settings->do_reset, 1 },
103 { "no-reset", 0, &settings->do_reset, 0 },
104 { "calibrate", 0, &settings->do_calibrate, 1 },
105 { "no-calibrate", 0, &settings->do_calibrate, 0 },
106 { "dump", 0, &settings->do_dump, 1 },
107 { "no-dump", 0, &settings->do_dump, 0 },
108 { "results", 0, &settings->do_results, 1 },
109 { "no-results", 0, &settings->do_results, 0 },
110 { "output", 0, &settings->do_output, 1 },
111 { "no-output", 0, &settings->do_output, 0 },
112 { "subdevice", 1, 0, 's' },
113 { "channel", 1, 0, 'c' },
114 { "range", 1, 0, 'r' },
115 { "aref", 1, 0, 'a' },
116 { 0 },
117 };
118
119 while (1) {
120 c = getopt_long(argc, argv, "f:S:vqs:c:r:a:", options, &index);
121 if (c == -1)break;
122 switch (c) {
123 case 0:
124 continue;
125 case 'h':
126 help();
127 exit(0);
128 break;
129 case 'f':
130 settings->file_path = optarg;
131 break;
132 case 'S':
133 settings->save_file_path = optarg;
134 break;
135 case 'v':
136 settings->verbose++;
137 break;
138 case 'q':
139 settings->verbose--;
140 break;
141 case 0x1000:
142 settings->driver_name = optarg;
143 break;
144 case 0x1001:
145 settings->device_name = optarg;
146 break;
147 case 's':
148 settings->subdevice = strtoul( optarg, NULL, 0 );
149 break;
150 case 'c':
151 settings->channel = strtoul( optarg, NULL, 0 );
152 break;
153 case 'r':
154 settings->range = strtoul( optarg, NULL, 0 );
155 break;
156 case 'a':
157 settings->aref = strtoul( optarg, NULL, 0 );
158 break;
159 default:
160 help();
161 exit(1);
162 }
163 }
164 }
165
166 int main(int argc, char *argv[])
167 {
168 int i;
169 struct board_struct *this_board;
170 int device_status = STATUS_UNKNOWN;
171 calibration_setup_t setup;
172 int retval;
173 parsed_options_t options;
174
175 memset( &setup, 0, sizeof( setup ) );
176 setup.sv_settling_time_ns = 99999;
177 setup.sv_order = 10;
178
179 memset( &options, 0, sizeof( options ) );
180 options.do_dump = 0;
181 options.do_reset = 0;
182 options.do_calibrate = -1;
183 options.do_results = 0;
184 options.do_output = 1;
185 options.file_path = "/dev/comedi0";
186 parse_options( argc, argv, &options );
187 verbose = options.verbose;
188
189 setup.dev = comedi_open( options.file_path );
190 if( setup.dev == NULL ) {
191 fprintf( stderr, "comedi_open() failed, with device file name: %s\n",
192 options.file_path );
193 comedi_perror("comedi_open");
194 return 1;
195 }
196
197 if( options.save_file_path == NULL )
198 options.save_file_path = comedi_get_default_calibration_path( setup.dev );
199 if(!options.driver_name)
200 options.driver_name = comedi_get_driver_name( setup.dev );
201 if(!options.device_name)
202 options.device_name = comedi_get_board_name( setup.dev );
203
204 setup.ad_subdev=comedi_find_subdevice_by_type( setup.dev,COMEDI_SUBD_AI,0);
205 setup.da_subdev=comedi_find_subdevice_by_type( setup.dev,COMEDI_SUBD_AO,0);
206 setup.caldac_subdev=comedi_find_subdevice_by_type( setup.dev,COMEDI_SUBD_CALIB,0);
207 setup.eeprom_subdev=comedi_find_subdevice_by_type( setup.dev,COMEDI_SUBD_MEMORY,0);
208
209 for(i=0;i<n_drivers;i++){
210 if(!strcmp(drivers[i].name,options.driver_name)){
211 this_board = drivers+i;
212 goto ok;
213 }
214 }
215 fprintf(stderr, "Driver %s unknown\n", options.driver_name);
216 return 1;
217
218 ok:
219
220 retval = this_board->init_setup( &setup, options.device_name );
221 if( retval < 0 ){
222 fprintf(stderr, "init_setup() failed for %s\n", options.device_name );
223 return 1;
224 }
225 device_status = setup.status;
226
227 if(device_status<STATUS_DONE){
228 printf("Warning: device may not be not fully calibrated due to "
229 "insufficient information.\n"
230 "Please file a bug report at https://bugs.comedi.org/ and attach this output.\n"
231 "This output will also allow comedi_calibrate to execute more\n"
232 "quickly in the future.\n");
233 if(verbose<1){
234 verbose=1;
235 printf("Forcing option: --verbose\n");
236 }
237 if(device_status==STATUS_UNKNOWN){
238 options.do_reset=1;
239 options.do_dump=1;
240 options.do_calibrate=0;
241 options.do_results=0;
242 printf("Forcing options: --reset --dump --no-calibrate --no-results\n");
243 }
244 if(device_status==STATUS_SOME){
245 options.do_reset=1;
246 options.do_dump=1;
247 options.do_calibrate=1;
248 options.do_results=1;
249 printf("Forcing options: --reset --dump --calibrate --results\n");
250 }
251 if(device_status==STATUS_GUESS){
252 options.do_reset=1;
253 options.do_dump=1;
254 options.do_calibrate=1;
255 options.do_results=1;
256 printf("Forcing options: --reset --dump --calibrate --results\n");
257 }
258 }
259 if(verbose>=0){
260 char *s = "$Id$";
261
262 printf("%.*s\n",(int)strlen(s)-2,s+1);
263 printf("Driver name: %s\n", options.driver_name);
264 printf("Device name: %s\n", options.device_name);
265 printf("%.*s\n",(int)strlen(this_board->id)-2,this_board->id+1);
266 printf("Comedi version: %d.%d.%d\n",
267 (comedi_get_version_code(setup.dev)>>16)&0xff,
268 (comedi_get_version_code(setup.dev)>>8)&0xff,
269 (comedi_get_version_code(setup.dev))&0xff);
270 }
271
272 if( options.do_reset == 0 )
273 setup.old_calibration = comedi_parse_calibration_file( options.save_file_path );
274 else
275 setup.old_calibration = NULL;
276 if( options.do_calibrate < 0 )
277 {
278 if( setup.old_calibration ) options.do_calibrate = 0;
279 else options.do_calibrate = 1;
280 }
281 setup.do_output = options.do_output;
282 setup.cal_save_file_path = options.save_file_path;
283
284 if(options.do_dump) observe( &setup );
285 if(options.do_calibrate && setup.do_cal)
286 {
287 setup.new_calibration = malloc( sizeof( comedi_calibration_t ) );
288 if( setup.new_calibration == NULL )
289 {
290 errno = ENOMEM;
291 perror(NULL);
292 return 1;
293 }
294 memset( setup.new_calibration, 0, sizeof( comedi_calibration_t ) );
295 setup.new_calibration->driver_name = strdup( comedi_get_driver_name( setup.dev ) );
296 if( setup.new_calibration->driver_name == NULL )
297 {
298 errno = ENOMEM;
299 perror(NULL);
300 return 1;
301 }
302 setup.new_calibration->board_name = strdup( comedi_get_board_name( setup.dev ) );
303 if( setup.new_calibration->board_name == NULL )
304 {
305 errno = ENOMEM;
306 perror(NULL);
307 return 1;
308 }
309 retval = setup.do_cal( &setup );
310 if( retval < 0 )
311 {
312 fprintf( stderr, "calibration function returned error\n" );
313 return -1;
314 }
315 }
316 if(options.do_results) observe( &setup );
317
318 if( setup.old_calibration ) comedi_cleanup_calibration( setup.old_calibration );
319 if( setup.new_calibration ) comedi_cleanup_calibration( setup.new_calibration );
320
321 retval = comedi_apply_calibration( setup.dev, options.subdevice,
322 options.channel, options.range, options.aref, setup.cal_save_file_path );
323 if( retval < 0 )
324 {
325 DPRINT( 0, "Failed to apply " );
326 }else
327 {
328 DPRINT( 0, "Applied " );
329 }
330 DPRINT( 0, "calibration for subdevice %i, channel %i, range %i, aref %i\n",
331 options.subdevice, options.channel, options.range, options.aref );
332 comedi_close(setup.dev);
333
334 return retval;
335 }
336
337 void set_target( calibration_setup_t *setup, int obs, double target)
338 {
339 comedi_range *range;
340 lsampl_t maxdata, data;
341
342 comedi_set_global_oor_behavior( COMEDI_OOR_NUMBER );
343
344 range = comedi_get_range(setup->dev,
345 setup->observables[obs].preobserve_insn.subdev,
346 CR_CHAN( setup->observables[obs].preobserve_insn.chanspec ),
347 CR_RANGE( setup->observables[obs].preobserve_insn.chanspec ));
348 assert( range );
349 maxdata = comedi_get_maxdata( setup->dev,
350 setup->observables[obs].preobserve_insn.subdev,
351 CR_CHAN(setup->observables[obs].preobserve_insn.chanspec));
352 assert( maxdata > 0 );
353 data = comedi_from_phys(target,range,maxdata);
354
355 setup->observables[obs].preobserve_data[0] = data;
356 setup->observables[obs].target = comedi_to_phys(data,range,maxdata);
357 }
358
359 static void apply_appropriate_cal( calibration_setup_t *setup, comedi_insn insn )
360 {
361 int retval = 0;
362
363 if( setup->new_calibration )
364 {
365 retval = comedi_apply_parsed_calibration( setup->dev, insn.subdev,
366 CR_CHAN( insn.chanspec ), CR_RANGE( insn.chanspec ),
367 CR_AREF( insn.chanspec ), setup->new_calibration );
368 }else if( setup->old_calibration )
369 {
370 retval = comedi_apply_parsed_calibration( setup->dev, insn.subdev,
371 CR_CHAN( insn.chanspec ), CR_RANGE( insn.chanspec ),
372 CR_AREF( insn.chanspec ), setup->old_calibration );
373 }else
374 {
375 reset_caldacs( setup );
376 return;
377 }
378 if( retval < 0 )
379 DPRINT( 1, "Failed to apply ");
380 else
381 DPRINT( 1, "Applied ");
382 DPRINT( 1, "calibration for subdev %i, channel %i, range %i, aref %i\n", insn.subdev,
383 CR_CHAN( insn.chanspec ), CR_RANGE( insn.chanspec ),
384 CR_AREF( insn.chanspec ) );
385 }
386
387 void observe( calibration_setup_t *setup )
388 {
389 int i;
390 observable *obs;
391
392 for( i = 0; i < setup->n_observables; i++){
393 obs = &setup->observables[ i ];
394 if( obs->observe_insn.n == 0 ) continue;
395 preobserve( setup, i);
396 DPRINT(0,"%s\n", setup->observables[i].name);
397 if( obs->preobserve_insn.n != 0){
398 apply_appropriate_cal( setup, obs->preobserve_insn );
399 }
400 apply_appropriate_cal( setup, obs->observe_insn );
401 measure_observable( setup, i);
402 if(verbose>=1){
403 observable_dependence( setup, i);
404 }
405 }
406 }
407
408 int preobserve( calibration_setup_t *setup, int obs)
409 {
410 int retval;
411 comedi_insn reference_source_config;
412 lsampl_t ref_data[ 2 ];
413
414 // setup reference source
415 if(setup->observables[obs].reference_source >= 0)
416 {
417 memset( &reference_source_config, 0, sizeof(reference_source_config) );
418 reference_source_config.insn = INSN_CONFIG;
419 reference_source_config.n = 2;
420 reference_source_config.subdev = setup->ad_subdev;
421 reference_source_config.data = ref_data;
422 reference_source_config.data[ 0 ] = INSN_CONFIG_ALT_SOURCE;
423 reference_source_config.data[ 1 ] = setup->observables[obs].reference_source;
424
425 retval = comedi_do_insn( setup->dev, &reference_source_config );
426 /* ignore errors for now since older ni driver doesn't
427 * support reference config insn */
428 if( retval < 0 )
429 perror("preobserve() ignoring reference config error" );
430 }
431 retval = 0;
432
433 if( setup->observables[obs].preobserve_insn.n != 0){
434 retval = comedi_do_insn( setup->dev, &setup->observables[obs].preobserve_insn);
435 }
436 if( retval < 0 )
437 perror("preobserve()");
438
439 return retval;
440 }
441
442 void measure_observable( calibration_setup_t *setup, int obs)
443 {
444 char s[100];
445 new_sv_t sv;
446
447 my_sv_init(&sv, setup,
448 setup->observables[obs].observe_insn.subdev,
449 setup->observables[obs].observe_insn.chanspec);
450 new_sv_measure(setup->dev, &sv);
451
452 sci_sprint_alt(s,sv.average,sv.error);
453 DPRINT(0,"reading %s, target %g\n",s, setup->observables[obs].target);
454 assert( isnan( setup->observables[obs].target) == 0 );
455 }
456
457 void observable_dependence(calibration_setup_t *setup, int obs)
458 {
459 int i;
460 linear_fit_t l;
461
462 for( i = 0; i < setup->n_caldacs; i++){
463 check_gain_chan_x( setup, &l,
464 setup->observables[obs].observe_insn.chanspec, i);
465 }
466
467 }
468
469
470 void postgain_cal( calibration_setup_t *setup, int obs1, int obs2, int dac)
471 {
472 double offset1,offset2;
473 linear_fit_t l;
474 double slope1,slope2;
475 double a;
476 double gain;
477 comedi_range *range1,*range2;
478
479 DPRINT(0,"postgain: %s; %s\n", setup->observables[obs1].name,
480 setup->observables[obs2].name);
481 preobserve(setup, obs1);
482 check_gain_chan_x( setup, &l, setup->observables[obs1].observe_insn.chanspec, dac);
483 offset1=linear_fit_func_y(&l, setup->caldacs[dac].value);
484 DPRINT(2,"obs1: [%d] offset %g\n",obs1,offset1);
485 range1 = comedi_get_range(setup->dev, setup->observables[obs1].observe_insn.subdev,
486 CR_CHAN( setup->observables[obs1].observe_insn.chanspec),
487 CR_RANGE( setup->observables[obs1].observe_insn.chanspec));
488 slope1=l.slope;
489
490 preobserve( setup, obs2);
491 check_gain_chan_x( setup, &l, setup->observables[obs2].observe_insn.chanspec,dac);
492 offset2=linear_fit_func_y(&l, setup->caldacs[dac].value);
493 DPRINT(2,"obs2: [%d] offset %g\n",obs2,offset2);
494 range2 = comedi_get_range(setup->dev, setup->observables[obs2].observe_insn.subdev,
495 CR_CHAN( setup->observables[obs2].observe_insn.chanspec),
496 CR_RANGE( setup->observables[obs2].observe_insn.chanspec));
497 slope2=l.slope;
498
499 gain = (range1->max-range1->min)/(range2->max-range2->min);
500 DPRINT(4,"range1 %g range2 %g\n", range1->max-range1->min,
501 range2->max-range2->min);
502 DPRINT(3,"gain: %g\n",gain);
503
504 DPRINT(3,"difference: %g\n",offset2-offset1);
505
506 a = (offset1-offset2)/(slope1-slope2);
507 a = setup->caldacs[dac].value - a;
508
509 update_caldac( setup, dac, rint(a) );
510 usleep(caldac_settle_usec);
511
512 DPRINT(0,"caldac[%d] set to %g (%g)\n",dac,rint(a),a);
513
514 if(verbose>=2){
515 preobserve( setup, obs1);
516 measure_observable( setup, obs1);
517 preobserve( setup, obs2);
518 measure_observable( setup, obs2);
519 }
520 }
521
522 void cal1( calibration_setup_t *setup, int obs, int dac)
523 {
524 linear_fit_t l;
525 double a;
526
527 DPRINT(0,"linear: %s\n", setup->observables[obs].name);
528 preobserve( setup, obs);
529 check_gain_chan_x( setup, &l, setup->observables[obs].observe_insn.chanspec,dac);
530 a=linear_fit_func_x(&l, setup->observables[obs].target);
531
532 update_caldac( setup, dac, rint(a) );
533 usleep(caldac_settle_usec);
534
535 DPRINT(0,"caldac[%d] set to %g (%g)\n",dac,rint(a),a);
536 if(verbose>=3){
537 measure_observable( setup, obs);
538 }
539 }
540
541 void cal1_fine( calibration_setup_t *setup, int obs, int dac )
542 {
543 linear_fit_t l;
544 double a;
545
546 DPRINT(0,"linear fine: %s\n", setup->observables[obs].name);
547 preobserve( setup, obs);
548 check_gain_chan_fine( setup, &l, setup->observables[obs].observe_insn.chanspec,dac);
549 a=linear_fit_func_x(&l,setup->observables[obs].target);
550
551 update_caldac( setup, dac, rint(a) );
552 usleep(caldac_settle_usec);
553
554 DPRINT(0,"caldac[%d] set to %g (%g)\n",dac,rint(a),a);
555 if(verbose>=3){
556 measure_observable( setup, obs);
557 }
558 }
559
560 void peg_binary( calibration_setup_t *setup, int obs, int dac, int maximize )
561 {
562 int x0, x1, x;
563 double y0, y1;
564 new_sv_t sv;
565 unsigned int chanspec = setup->observables[obs].observe_insn.chanspec;
566 int polarity;
567
568 DPRINT(0,"binary peg: %s\n", setup->observables[obs].name);
569 preobserve( setup, obs);
570
571 comedi_set_global_oor_behavior( COMEDI_OOR_NUMBER );
572
573 my_sv_init(&sv, setup, setup->ad_subdev, chanspec);
574
575 x0 = caldac_maxdata(setup->dev, &setup->caldacs[dac]);
576 update_caldac( setup, dac, x0 );
577 usleep(caldac_settle_usec);
578 new_sv_measure( setup->dev, &sv);
579 y0 = sv.average;
580
581 x1 = 0;
582 update_caldac( setup, dac, x1 );
583 usleep(caldac_settle_usec);
584 new_sv_measure( setup->dev, &sv);
585 y1 = sv.average;
586
587 if( (y0 - y1) > 0.0 ) polarity = 1;
588 else polarity = -1;
589
590 if( maximize )
591 {
592 if( polarity > 0 ) x = x0;
593 else x = x1;
594 }else
595 {
596 if( polarity > 0 ) x = x1;
597 else x = x0;
598 }
599 update_caldac( setup, dac, x );
600 DPRINT(0,"caldac[%d] set to %d\n",dac,x);
601 if(verbose>=3){
602 measure_observable( setup, obs);
603 }
604 }
605
606 void cal_binary( calibration_setup_t *setup, int obs, int dac)
607 {
608 int x0, x1, x2, x;
609 unsigned int bit;
610 double y0, y1, y2;
611 new_sv_t sv;
612 double target = setup->observables[obs].target;
613 unsigned int chanspec = setup->observables[obs].observe_insn.chanspec;
614 int polarity;
615
616 DPRINT(0,"binary: %s\n", setup->observables[obs].name);
617 preobserve( setup, obs);
618
619 comedi_set_global_oor_behavior( COMEDI_OOR_NUMBER );
620
621 my_sv_init(&sv, setup, setup->ad_subdev, chanspec);
622
623 x0 = caldac_maxdata(setup->dev, &setup->caldacs[dac]);
624 update_caldac( setup, dac, x0 );
625 usleep(caldac_settle_usec);
626 new_sv_measure( setup->dev, &sv);
627 y0 = sv.average;
628
629 x1 = x2 = 0;
630 update_caldac( setup, dac, x1 );
631 usleep(caldac_settle_usec);
632 new_sv_measure( setup->dev, &sv);
633 y1 = y2 = sv.average;
634
635 if( (y0 - y1) > 0.0 ) polarity = 1;
636 else polarity = -1;
637
638 bit = 1;
639 while((bit << 1) < caldac_maxdata(setup->dev, &setup->caldacs[dac]))
640 bit <<= 1;
641 for( ; bit; bit >>= 1 ){
642 x2 = x1 | bit;
643
644 update_caldac( setup, dac, x2 );
645 usleep(caldac_settle_usec);
646 new_sv_measure( setup->dev, &sv);
647 y2 = sv.average;
648 DPRINT(3,"trying %d, result %g, target %g\n",x2,y2,target);
649
650 if( (y2 - target) * polarity < 0.0 ){
651 x1 = x2;
652 y1 = y2;
653 }
654
655 if(verbose>=3){
656 measure_observable( setup, obs);
657 }
658 }
659
660 // get that least signficant bit right
661 if( fabs( y1 - target ) < fabs( y2 - target ) )
662 x = x1;
663 else x = x2;
664
665 update_caldac( setup, dac, x );
666 DPRINT(0,"caldac[%d] set to %d\n",dac,x);
667 if( x >= caldac_maxdata(setup->dev, &setup->caldacs[dac]) || x <= 0 )
668 DPRINT(0,"WARNING: caldac[%d] pegged!\n", dac );
669 if(verbose>=3){
670 measure_observable( setup, obs);
671 }
672 }
673
674 void cal_postgain_binary( calibration_setup_t *setup, int obs1, int obs2, int dac)
675 {
676 cal_relative_binary( setup, obs1, obs2, dac );
677 }
678
679 void cal_relative_binary( calibration_setup_t *setup, int obs1, int obs2, int dac)
680 {
681 int x0, x1, x2, x, polarity;
682 double y0, y1, y2;
683 new_sv_t sv1, sv2;
684 double target = setup->observables[obs1].target - setup->observables[obs2].target;
685 unsigned int chanspec1 = setup->observables[obs1].observe_insn.chanspec;
686 unsigned int chanspec2 = setup->observables[obs2].observe_insn.chanspec;
687 unsigned int bit;
688
689 DPRINT(0,"relative binary: %s, %s\n", setup->observables[obs1].name,
690 setup->observables[obs2].name);
691
692 comedi_set_global_oor_behavior( COMEDI_OOR_NUMBER );
693
694 x0 = caldac_maxdata(setup->dev, &setup->caldacs[dac]);
695 update_caldac( setup, dac, x0 );
696 usleep(caldac_settle_usec);
697 preobserve( setup, obs1);
698 my_sv_init(&sv1, setup, setup->ad_subdev,chanspec1);
699 new_sv_measure( setup->dev, &sv1);
700
701 preobserve( setup, obs2);
702 my_sv_init(&sv2, setup, setup->ad_subdev,chanspec2);
703 new_sv_measure( setup->dev, &sv2);
704 y0 = sv1.average - sv2.average;
705
706 x1 = x2 = 0;
707 update_caldac( setup, dac, x1 );
708 usleep(caldac_settle_usec);
709 preobserve( setup, obs1);
710 new_sv_measure( setup->dev, &sv1);
711
712 preobserve( setup, obs2);
713 new_sv_measure( setup->dev, &sv2);
714 y1 = y2 = sv1.average - sv2.average;
715
716 if( (y0 - y1) > 0.0 ) polarity = 1;
717 else polarity = -1;
718
719 bit = 1;
720 while((bit << 1) < caldac_maxdata(setup->dev, &setup->caldacs[dac]))
721 bit <<= 1;
722 for( ; bit; bit >>= 1 )
723 {
724 x2 = x1 | bit;
725
726 update_caldac( setup, dac, x2 );
727 usleep(caldac_settle_usec);
728
729 preobserve( setup, obs1);
730 new_sv_measure( setup->dev, &sv1);
731
732 preobserve( setup, obs2);
733 new_sv_measure( setup->dev, &sv2);
734 y2 = sv1.average - sv2.average;
735
736 DPRINT(3,"trying %d, result %g, target %g\n",x2,y2,target);
737
738 if( (y2 - target) * polarity < 0.0 ){
739 x1 = x2;
740 y1 = y2;
741 }
742
743 if(verbose>=3){
744 preobserve( setup, obs1);
745 measure_observable( setup, obs1);
746 preobserve( setup, obs2);
747 measure_observable( setup, obs2);
748 }
749 }
750
751 if( fabs( y1 - target ) < fabs( y2 - target ) )
752 x = x1;
753 else
754 x = x2;
755 update_caldac( setup, dac, x );
756 DPRINT(0,"caldac[%d] set to %d\n",dac,x);
757 if(x >= caldac_maxdata(setup->dev, &setup->caldacs[dac]) || x <= 0 )
758 DPRINT(0,"WARNING: caldac[%d] pegged!\n", dac );
759 if(verbose>=3){
760 preobserve( setup, obs1);
761 measure_observable( setup, obs1);
762 preobserve( setup, obs2);
763 measure_observable( setup, obs2);
764 }
765 }
766
767 void cal_linearity_binary( calibration_setup_t *setup, int obs1, int obs2, int obs3, int dac)
768 {
769 int x0, x1, x2, x, polarity;
770 double y0, y1, y2;
771 new_sv_t sv1, sv2, sv3;
772 double target = ( setup->observables[obs3].target - setup->observables[obs2].target ) /
773 ( setup->observables[obs2].target - setup->observables[obs1].target );
774 unsigned int chanspec1 = setup->observables[obs1].observe_insn.chanspec;
775 unsigned int chanspec2 = setup->observables[obs2].observe_insn.chanspec;
776 unsigned int chanspec3 = setup->observables[obs3].observe_insn.chanspec;
777 unsigned int bit;
778
779 DPRINT(0,"linearity binary: %s,\n%s,\n%s\n", setup->observables[obs1].name,
780 setup->observables[obs2].name,setup->observables[obs3].name);
781
782 comedi_set_global_oor_behavior( COMEDI_OOR_NUMBER );
783
784 x0 = caldac_maxdata(setup->dev, &setup->caldacs[dac]);
785 update_caldac( setup, dac, x0 );
786 usleep(caldac_settle_usec);
787
788 preobserve( setup, obs1);
789 my_sv_init(&sv1, setup, setup->ad_subdev,chanspec1);
790 new_sv_measure( setup->dev, &sv1);
791
792 preobserve( setup, obs2);
793 my_sv_init(&sv2, setup, setup->ad_subdev,chanspec2);
794 new_sv_measure( setup->dev, &sv2);
795
796 preobserve( setup, obs3);
797 my_sv_init(&sv3, setup, setup->ad_subdev,chanspec3);
798 new_sv_measure( setup->dev, &sv3);
799
800 y0 = ( sv3.average - sv2.average ) / ( sv2.average - sv1.average );
801
802 x1 = x2 = 0;
803 update_caldac( setup, dac, x1 );
804 usleep(caldac_settle_usec);
805
806 preobserve( setup, obs1);
807 new_sv_measure( setup->dev, &sv1);
808
809 preobserve( setup, obs2);
810 new_sv_measure( setup->dev, &sv2);
811
812 preobserve( setup, obs3);
813 new_sv_measure( setup->dev, &sv3);
814
815 y1 = y2 = ( sv3.average - sv2.average ) / ( sv2.average - sv1.average );
816
817 if( (y0 - y1) > 0.0 ) polarity = 1;
818 else polarity = -1;
819
820 bit = 1;
821 while((bit << 1) < caldac_maxdata(setup->dev, &setup->caldacs[dac]))
822 bit <<= 1;
823 for( ; bit; bit >>= 1 )
824 {
825 x2 = x1 | bit;
826
827 update_caldac( setup, dac, x2 );
828 usleep(caldac_settle_usec);
829
830 preobserve( setup, obs1);
831 new_sv_measure( setup->dev, &sv1);
832
833 preobserve( setup, obs2);
834 new_sv_measure( setup->dev, &sv2);
835
836 preobserve( setup, obs3);
837 new_sv_measure( setup->dev, &sv3);
838
839 y2 = ( sv3.average - sv2.average ) / ( sv2.average - sv1.average );
840
841 DPRINT(3,"trying %d, result %g, target %g\n",x2,y2,target);
842
843 if( (y2 - target) * polarity < 0.0 ){
844 x1 = x2;
845 y1 = y2;
846 }
847
848 if(verbose>=3){
849 preobserve( setup, obs1);
850 measure_observable( setup, obs1);
851 preobserve( setup, obs2);
852 measure_observable( setup, obs2);
853 preobserve( setup, obs3);
854 measure_observable( setup, obs3);
855 }
856 }
857
858 if( fabs( y1 - target ) < fabs( y2 - target ) )
859 x = x1;
860 else
861 x = x2;
862 update_caldac( setup, dac, x );
863 DPRINT(0,"caldac[%d] set to %d\n",dac,x);
864 if(x >= caldac_maxdata(setup->dev, &setup->caldacs[dac]) || x <= 0 )
865 DPRINT(0,"WARNING: caldac[%d] pegged!\n", dac );
866 if(verbose>=3){
867 preobserve( setup, obs1);
868 measure_observable( setup, obs1);
869 preobserve( setup, obs2);
870 measure_observable( setup, obs2);
871 preobserve( setup, obs3);
872 measure_observable( setup, obs3);
873 }
874 }
875
876 #if 0
877 void chan_cal(int adc,int cdac,int range,double target)
878 {
879 linear_fit_t l;
880 double offset;
881 double gain;
882 double a;
883 char s[32];
884
885 check_gain_chan_x(&l,CR_PACK(adc,range,AREF_OTHER),cdac);
886 offset=linear_fit_func_y(&l,caldacs[cdac].current);
887 gain=l.slope;
888
889 a=caldacs[cdac].current+(target-offset)/gain;
890
891 update_caldac( setup, cdac, rint(a));
892
893 read_chan2(s,adc,range);
894 DPRINT(1,"caldac[%d] set to %g, offset=%s\n",cdac,a,s);
895 }
896 #endif
897
898
899 #if 0
900 void channel_dependence(int adc,int range)
901 {
902 int i;
903 double gain;
904
905 for(i=0;i<n_caldacs;i++){
906 gain=check_gain_chan(adc,range,i);
907 }
908 }
909 #endif
910
911 #if 0
912 void caldac_dependence(int caldac)
913 {
914 int i;
915 double gain;
916
917 for(i=0;i<8;i++){
918 gain=check_gain_chan(i,0,caldac);
919 }
920 }
921 #endif
922
923
924 void setup_caldacs( calibration_setup_t *setup, int caldac_subdev )
925 {
926 int n_chan, i;
927
928 if( caldac_subdev < 0 ){
929 printf("no calibration subdevice\n");
930 return;
931 }
932
933 // XXX check subdevice type is really calibration
934
935 n_chan = comedi_get_n_channels( setup->dev, caldac_subdev );
936 assert(n_chan >= 0);
937 assert(setup->n_caldacs + n_chan < N_CALDACS);
938
939 for(i = 0; i < n_chan; i++){
940 setup->caldacs[ setup->n_caldacs + i ].subdevice = caldac_subdev;
941 setup->caldacs[ setup->n_caldacs + i ].channel = i;
942 setup->caldacs[ setup->n_caldacs + i ].value = 0;
943 }
944
945 setup->n_caldacs += n_chan;
946 }
947
948 void reset_caldac( calibration_setup_t *setup, int caldac_index )
949 {
950 if( caldac_index < 0 ) return;
951 assert( caldac_index < setup->n_caldacs );
952 update_caldac(setup, caldac_index, caldac_maxdata(setup->dev, &setup->caldacs[caldac_index]) / 2);
953 }
954
955 void reset_caldacs( calibration_setup_t *setup )
956 {
957 int i;
958
959 for( i = 0; i < setup->n_caldacs; i++){
960 reset_caldac( setup, i );
961 }
962 }
963
964 void update_caldac( calibration_setup_t *setup, int caldac_index,
965 int value )
966 {
967 int ret;
968 comedi_caldac_t *dac;
969
970 if( caldac_index < 0 ) return;
971 if( caldac_index >= setup->n_caldacs )
972 {
973 fprintf( stderr, "invalid caldac index\n" );
974 return;
975 }
976 if(value < 0){
977 DPRINT(1,"caldac set out of range (%d<0)\n", value);
978 value = 0;
979 }
980 dac = &setup->caldacs[ caldac_index ];
981 if(value > caldac_maxdata(setup->dev, dac)) {
982 DPRINT(1,"caldac set out of range (%d>%d)\n",
983 value, caldac_maxdata(setup->dev, dac));
984 value = caldac_maxdata(setup->dev, dac);
985 }
986 dac->value = value;
987 DPRINT(4,"update %d %d %d\n", dac->subdevice, dac->channel, dac->value);
988
989 ret = comedi_data_write(setup->dev, dac->subdevice, dac->channel, 0, 0,
990 dac->value);
991 if(ret < 0) perror("update_caldac()");
992 }
993
994 #if 0
995 void check_gain(int ad_chan,int range)
996 {
997 int i;
998
999 for(i=0;i<n_caldacs;i++){
1000 check_gain_chan(ad_chan,range,i);
1001 }
1002 }
1003 #endif
1004
1005 #if 0
1006 double check_gain_chan(int ad_chan,int range,int cdac)
1007 {
1008 linear_fit_t l;
1009
1010 return check_gain_chan_x(&l,CR_PACK(ad_chan,range,AREF_OTHER),cdac);
1011 }
1012 #endif
1013
1014
1015 double check_gain_chan_x( calibration_setup_t *setup, linear_fit_t *l,unsigned int ad_chanspec,int cdac)
1016 {
1017 int orig,i,n;
1018 int step;
1019 new_sv_t sv;
1020 double sum_err;
1021 int sum_err_count=0;
1022 char str[100];
1023
1024 n = caldac_maxdata(setup->dev, &setup->caldacs[cdac]) + 1;
1025 memset(l,0,sizeof(*l));
1026
1027 step=n/16;
1028 if(step<1)step=1;
1029 l->n=0;
1030
1031 l->y_data=malloc(n*sizeof(double)/step);
1032 if(l->y_data == NULL)
1033 {
1034 perror( __FUNCTION__ );
1035 exit(1);
1036 }
1037
1038 orig = setup->caldacs[cdac].value;
1039
1040 my_sv_init(&sv, setup, setup->ad_subdev,ad_chanspec);
1041
1042 update_caldac( setup, cdac, 0 );
1043 usleep(caldac_settle_usec);
1044
1045 new_sv_measure( setup->dev, &sv);
1046
1047 sum_err=0;
1048 for(i=0;i*step<n;i++){
1049 update_caldac( setup, cdac, i*step );
1050 usleep(caldac_settle_usec);
1051
1052 new_sv_measure( setup->dev, &sv);
1053
1054 l->y_data[i]=sv.average;
1055 if(!isnan(sv.average)){
1056 sum_err+=sv.error;
1057 sum_err_count++;
1058 }
1059 l->n++;
1060 }
1061
1062 update_caldac( setup, cdac, orig );
1063
1064 l->yerr=sum_err/sqrt(sum_err_count);
1065 l->dx=step;
1066 l->x0=0;
1067
1068 linear_fit_monotonic(l);
1069
1070 if(verbose>=2 || (verbose>=1 && (fabs(l->slope / l->err_slope) > 4.0 || isnan(l->slope / l->err_slope)))){
1071 sci_sprint_alt(str,l->slope,l->err_slope);
1072 printf("caldac[%d] gain=%s V/bit S_min=%g dof=%g\n",
1073 cdac,str,l->S_min,l->dof);
1074 //printf("--> %g\n",fabs(l.slope/l.err_slope));
1075 }
1076
1077 if(verbose>=3)dump_curve(l);
1078
1079 free(l->y_data);
1080
1081 return l->slope;
1082 }
1083
1084
1085 double check_gain_chan_fine( calibration_setup_t *setup, linear_fit_t *l,unsigned int ad_chanspec,int cdac)
1086 {
1087 int orig,i,n;
1088 int step;
1089 new_sv_t sv;
1090 double sum_err;
1091 int sum_err_count=0;
1092 char str[100];
1093 int fine_size = 10;
1094
1095 n=2*fine_size+1;
1096 memset(l,0,sizeof(*l));
1097
1098 step=1;
1099 l->n=0;
1100
1101 l->y_data=malloc(n*sizeof(double)/step);
1102 if(l->y_data == NULL)
1103 {
1104 perror( __FUNCTION__ );
1105 exit(1);
1106 }
1107
1108 orig = setup->caldacs[cdac].value;
1109
1110 my_sv_init(&sv, setup, setup->ad_subdev,ad_chanspec);
1111
1112 update_caldac( setup, cdac, 0 );
1113 usleep(caldac_settle_usec);
1114
1115 new_sv_measure( setup->dev, &sv);
1116
1117 sum_err=0;
1118 for(i=0;i<n;i++){
1119 update_caldac( setup, cdac, i+orig-fine_size );
1120 usleep(caldac_settle_usec);
1121
1122 new_sv_measure( setup->dev, &sv);
1123
1124 l->y_data[i]=sv.average;
1125 if(!isnan(sv.average)){
1126 sum_err+=sv.error;
1127 sum_err_count++;
1128 }
1129 l->n++;
1130 }
1131
1132 update_caldac( setup, cdac, orig );
1133
1134 l->yerr=sum_err/sqrt(sum_err_count);
1135 l->dx=1;
1136 l->x0=orig-fine_size;
1137
1138 linear_fit_monotonic(l);
1139
1140 if(verbose>=2 || (verbose>=1 && fabs(l->slope/l->err_slope)>4.0)){
1141 sci_sprint_alt(str,l->slope,l->err_slope);
1142 printf("caldac[%d] gain=%s V/bit S_min=%g dof=%g\n",
1143 cdac,str,l->S_min,l->dof);
1144 //printf("--> %g\n",fabs(l.slope/l.err_slope));
1145 }
1146
1147 if(verbose>=3)dump_curve(l);
1148
1149 free(l->y_data);
1150
1151 return l->slope;
1152 }
1153
1154
1155
1156 /* helpers */
1157
1158 int is_unipolar( comedi_t *dev, unsigned int subdevice,
1159 unsigned int channel, unsigned int range )
1160 {
1161 comedi_range *range_ptr;
1162
1163 range_ptr = comedi_get_range( dev, subdevice, channel, range );
1164 assert( range_ptr != NULL );
1165 /* This method is better than a direct test, which might fail */
1166 if( fabs( range_ptr->min ) < fabs( range_ptr->max * 0.001 ) )
1167 return 1;
1168 else
1169 return 0;
1170 }
1171
1172 int is_bipolar( comedi_t *dev, unsigned int subdevice,
1173 unsigned int channel, unsigned int range )
1174 {
1175 comedi_range *range_ptr;
1176
1177 range_ptr = comedi_get_range( dev, subdevice, channel, range );
1178 assert( range_ptr != NULL );
1179 /* This method is better than a direct test, which might fail */
1180 if( fabs( range_ptr->max + range_ptr->min ) < fabs( range_ptr->max * 0.001 ) )
1181 return 1;
1182 else
1183 return 0;
1184 }
1185
1186 int get_bipolar_lowgain(comedi_t *dev,int subdev)
1187 {
1188 int ret = -1;
1189 int i;
1190 int n_ranges = comedi_get_n_ranges(dev,subdev,0);
1191 double max = 0;
1192 comedi_range *range;
1193
1194 for(i=0;i<n_ranges;i++){
1195 range = comedi_get_range(dev,subdev,0,i);
1196 if( is_bipolar( dev, subdev, 0, i ) == 0 ) continue;
1197 if(range->unit == UNIT_none ) continue;
1198 if(range->max>max){
1199 ret = i;
1200 max=range->max;
1201 }
1202 }
1203
1204 return ret;
1205 }
1206
1207 int get_bipolar_highgain(comedi_t *dev,int subdev)
1208 {
1209 int ret = -1;
1210 int i;
1211 int n_ranges = comedi_get_n_ranges(dev,subdev,0);
1212 double min = HUGE_VAL;
1213 comedi_range *range;
1214
1215 for(i=0;i<n_ranges;i++){
1216 range = comedi_get_range(dev,subdev,0,i);
1217 if( is_bipolar( dev, subdev, 0, i ) == 0 ) continue;
1218 if(range->unit == UNIT_none ) continue;
1219 if(range->max<min){
1220 ret = i;
1221 min=range->max;
1222 }
1223 }
1224
1225 return ret;
1226 }
1227
1228 int get_unipolar_lowgain(comedi_t *dev,int subdev)
1229 {
1230 int ret = -1;
1231 int i;
1232 int n_ranges = comedi_get_n_ranges(dev,subdev,0);
1233 double max = 0.0;
1234 comedi_range *range;
1235
1236 for(i=0;i<n_ranges;i++){
1237 range = comedi_get_range(dev,subdev,0,i);
1238 if( is_unipolar( dev, subdev, 0, i ) == 0 ) continue;
1239 if(range->unit == UNIT_none ) continue;
1240 if(range->max>max){
1241 ret = i;
1242 max=range->max;
1243 }
1244 }
1245
1246 return ret;
1247 }
1248
1249 int get_unipolar_highgain(comedi_t *dev,int subdev)
1250 {
1251 int ret = -1;
1252 int i;
1253 int n_ranges = comedi_get_n_ranges(dev,subdev,0);
1254 double max = HUGE_VAL;
1255 comedi_range *range;
1256
1257 for(i=0;i<n_ranges;i++){
1258 range = comedi_get_range(dev,subdev,0,i);
1259 if( is_unipolar( dev, subdev, 0, i ) == 0 ) continue;
1260 if(range->unit == UNIT_none ) continue;
1261 if(range->max < max){
1262 ret = i;
1263 max=range->max;
1264 }
1265 }
1266
1267 return ret;
1268 }
1269
1270 int read_eeprom( calibration_setup_t *setup, int addr)
1271 {
1272 lsampl_t data = 0;
1273 int retval;
1274
1275 retval = comedi_data_read( setup->dev, setup->eeprom_subdev, addr,0,0,&data);
1276 if( retval < 0 )
1277 {
1278 perror( "read_eeprom()" );
1279 return retval;
1280 }
1281
1282 return data;
1283 }
1284
1285 double read_chan( calibration_setup_t *setup, int adc,int range)
1286 {
1287 new_sv_t sv;
1288 char str[100];
1289
1290 my_sv_init(&sv, setup, setup->ad_subdev,CR_PACK(adc,range,AREF_OTHER));
1291
1292 new_sv_measure( setup->dev, &sv);
1293
1294 sci_sprint_alt(str,sv.average,sv.error);
1295 printf("chan=%d ave=%s\n",adc,str);
1296
1297 return sv.average;
1298 }
1299
1300 int read_chan2( calibration_setup_t *setup, char *s,int adc,int range)
1301 {
1302 new_sv_t sv;
1303
1304 my_sv_init(&sv, setup, setup->ad_subdev,CR_PACK(adc,range,AREF_OTHER));
1305
1306 new_sv_measure( setup->dev, &sv);
1307
1308 return sci_sprint_alt(s,sv.average,sv.error);
1309 }
1310
1311 #if 0
1312 void set_ao(comedi_t *dev,int subdev,int chan,int range,double value)
1313 {
1314 comedi_range *r = comedi_get_range(dev,subdev,chan,range);
1315 lsampl_t maxdata = comedi_get_maxdata(dev,subdev,chan);
1316 lsampl_t data;
1317
1318 data = comedi_from_phys(value,r,maxdata);
1319
1320 comedi_data_write(dev,subdev,chan,range,AREF_GROUND,data);
1321 }
1322 #endif
1323
1324 int my_sv_init( new_sv_t *sv, const calibration_setup_t *setup, int subdev,
1325 unsigned int chanspec )
1326 {
1327 int retval;
1328 retval = new_sv_init( sv, setup->dev, subdev, chanspec );
1329 sv->settling_time_ns = setup->sv_settling_time_ns;
1330 sv->order = setup->sv_order;
1331 return retval;
1332 }
1333
1334 int new_sv_init(new_sv_t *sv,comedi_t *dev,int subdev,unsigned int chanspec)
1335 {
1336 memset(sv,0,sizeof(*sv));
1337
1338 sv->subd=subdev;
1339 //sv->t.flags=TRIG_DITHER;
1340 sv->chanspec = chanspec;
1341
1342 //sv->chanlist[0]=CR_PACK(chan,range,aref);
1343
1344 sv->maxdata=comedi_get_maxdata(dev,subdev,CR_CHAN(chanspec));
1345 sv->rng=comedi_get_range(dev,subdev,
1346 CR_CHAN(chanspec), CR_RANGE(chanspec));
1347
1348 sv->order=10;
1349
1350 return 0;
1351 }
1352
1353 int new_sv_measure( comedi_t *dev, new_sv_t *sv)
1354 {
1355 lsampl_t *data;
1356 int n,i,ret;
1357
1358 double x,s,s2;
1359
1360 n=1<<sv->order;
1361
1362 data=malloc(sizeof(lsampl_t)*n);
1363 if(data == NULL)
1364 {
1365 perror( __FUNCTION__ );
1366 exit(1);
1367 }
1368
1369 ret = comedi_data_read_hint(dev, sv->subd, sv->chanspec, 0, 0);
1370 if(ret<0){
1371 printf("hint barf\n");
1372 goto out;
1373 }
1374 comedi_nanodelay(dev, sv->settling_time_ns);
1375
1376 ret = comedi_data_read_n(dev, sv->subd, sv->chanspec, 0, 0, data, n);
1377 if(ret<0){
1378 printf("barf\n");
1379 goto out;
1380 }
1381
1382 s=0.0;
1383 s2=0.0;
1384 for(i = 0; i < n; i++){
1385 x = comedi_to_phys(data[i], sv->rng, sv->maxdata);
1386 s += x;
1387 s2 += x * x;
1388 }
1389 s /= n;
1390 s2 /= n;
1391 sv->average=s;
1392 sv->stddev=sqrt( ( ( n + 1 ) / n ) * ( s2 - s * s ) );
1393 sv->error=sv->stddev / sqrt( n );
1394
1395 ret=n;
1396
1397 out:
1398 free(data);
1399
1400 return ret;
1401 }
1402
1403 int new_sv_measure_order( comedi_t *dev, new_sv_t *sv,int order)
1404 {
1405 lsampl_t *data;
1406 int n,i,ret;
1407 double x,s,s2;
1408
1409 n=1<<order;
1410
1411 data=malloc(sizeof(lsampl_t)*n);
1412 if(data == NULL)
1413 {
1414 perror( __FUNCTION__ );
1415 exit(1);
1416 }
1417
1418 ret = comedi_data_read_n(dev, sv->subd, sv->chanspec, 0, 0, data, n);
1419 if(ret<0){
1420 printf("barf order\n");
1421 goto out;
1422 }
1423
1424 s=0;
1425 s2=0;
1426 for(i = 0; i < n; i++){
1427 x = comedi_to_phys(data[i], sv->rng, sv->maxdata);
1428 s += x;
1429 s2 += x * x;
1430 }
1431 s /= n;
1432 s2 /= n;
1433 sv->average = s;
1434 sv->stddev=sqrt( ( ( n + 1 ) / n ) * ( s2 - s * s ) );
1435 sv->error=sv->stddev / sqrt( n );
1436
1437 ret=n;
1438
1439 out:
1440 free(data);
1441
1442 return ret;
1443 }
1444
1445
1446
1447 /* linear fitting */
1448
1449 int calculate_residuals(linear_fit_t *l);
1450
1451 int linear_fit_monotonic(linear_fit_t *l)
1452 {
1453 double x,y;
1454 double sxp;
1455 int i;
1456
1457 l->min=HUGE_VAL;
1458 l->max=-HUGE_VAL;
1459 l->s1=0;
1460 l->sx=0;
1461 l->sy=0;
1462 l->sxy=0;
1463 l->sxx=0;
1464 for(i=0;i<l->n;i++){
1465 x=l->x0+i*l->dx;
1466 y=l->y_data[i];
1467
1468 if(isnan(y))continue;
1469
1470 if(l->y_data[i]<l->min)l->min=l->y_data[i];
1471 if(l->y_data[i]>l->max)l->max=l->y_data[i];
1472 l->s1+=1;
1473 l->sx+=x;
1474 l->sy+=y;
1475 l->sxy+=x*y;
1476 l->sxx+=x*x;
1477 }
1478 sxp=l->sxx-l->sx*l->sx/l->s1;
1479
1480 l->ave_x=l->sx/l->s1;
1481 l->ave_y=l->sy/l->s1;
1482 l->slope=(l->s1*l->sxy-l->sx*l->sy)/(l->s1*l->sxx-l->sx*l->sx);
1483 l->err_slope=l->yerr/sqrt(sxp);
1484 l->err_ave_y=l->yerr/sqrt(l->s1);
1485
1486 calculate_residuals(l);
1487
1488 return 0;
1489 }
1490
1491 int calculate_residuals(linear_fit_t *l)
1492 {
1493 double x,y;
1494 double res,sum_res2;
1495 int i;
1496
1497 sum_res2=0;
1498 for(i=0;i<l->n;i++){
1499 x=l->x0+i*l->dx-l->ave_x;
1500 y=l->y_data[i];
1501
1502 if(isnan(y))continue;
1503
1504 res=l->ave_y+l->slope*x-y;
1505
1506 sum_res2+=res*res;
1507 }
1508 l->S_min=sum_res2/(l->yerr*l->yerr);
1509 l->dof=l->s1-2;
1510
1511 return 0;
1512 }
1513
1514 double linear_fit_func_y(linear_fit_t *l,double x)
1515 {
1516 return l->ave_y+l->slope*(x-l->ave_x);
1517 }
1518
1519 double linear_fit_func_x(linear_fit_t *l,double y)
1520 {
1521 return l->ave_x+(y-l->ave_y)/l->slope;
1522 }
1523
1524 void dump_curve(linear_fit_t *l)
1525 {
1526 static int dump_number=0;
1527 double x,y;
1528 int i;
1529
1530 printf("start dump %d\n",dump_number);
1531 for(i=0;i<l->n;i++){
1532 x=l->x0+i*l->dx-l->ave_x;
1533 y=l->y_data[i];
1534 printf("D%d: %d %g %g %g\n",dump_number,i,y,
1535 l->ave_y+l->slope*x,
1536 l->ave_y+l->slope*x-y);
1537 }
1538 printf("end dump\n");
1539 dump_number++;
1540 }
1541
1542
1543 /* printing of scientific numbers (with errors) */
1544
1545 int sci_sprint(char *s,double x,double y)
1546 {
1547 int errsig;
1548 int maxsig;
1549 int sigfigs;
1550 double mantissa;
1551 double error;
1552
1553 errsig = floor(log10(y));
1554 maxsig = floor(log10(x));
1555
1556 if(maxsig<errsig)maxsig=errsig;
1557
1558 sigfigs = maxsig-errsig+2;
1559
1560 mantissa = x*pow(10,-maxsig);
1561 error = y*pow(10,-errsig+1);
1562
1563 return sprintf(s,"%0.*f(%2.0f)e%d",sigfigs-1,mantissa,error,maxsig);
1564 }
1565
1566 int sci_sprint_alt(char *s,double x,double y)
1567 {
1568 int errsig;
1569 int maxsig;
1570 int sigfigs;
1571 double mantissa;
1572 double error;
1573
1574 errsig = floor(log10(fabs(y)));
1575 maxsig = floor(log10(fabs(x)));
1576
1577 if(maxsig<errsig)maxsig=errsig;
1578 sigfigs = maxsig-errsig+2;
1579
1580 mantissa = x*pow(10,-maxsig);
1581 error = y*pow(10,-errsig+1);
1582
1583 if(isnan(x) || isnan(y)){
1584 return sprintf(s,"%g",x);
1585 }
1586 if(errsig==1 && maxsig<4 && maxsig>1){
1587 return sprintf(s,"%0.0f(%2.0f)",x,error);
1588 }
1589 if(maxsig<=0 && maxsig>=-2){
1590 return sprintf(s,"%0.*f(%2.0f)",sigfigs-1-maxsig,
1591 mantissa*pow(10,maxsig),error);
1592 }
1593 return sprintf(s,"%0.*f(%2.0f)e%d",sigfigs-1,mantissa,error,maxsig);
1594 }
1595
1596 double very_low_target( comedi_t *dev, unsigned int subdevice,
1597 unsigned int channel, unsigned int range )
1598 {
1599 comedi_range *range_ptr;
1600 int max_data;
1601
1602 range_ptr = comedi_get_range( dev, subdevice, channel, range );
1603 assert( range_ptr != NULL );
1604 max_data = comedi_get_maxdata( dev, subdevice, 0 );
1605 assert( max_data > 0 );
1606
1607 return comedi_to_phys( 1, range_ptr, max_data ) / 2.0;
1608 }
1609
1610 double fractional_offset( calibration_setup_t *setup, int subdevice,
1611 unsigned int channel, unsigned int range, int obs )
1612 {
1613 comedi_range *range_ptr;
1614 double target;
1615 double reading;
1616 unsigned int chanspec;
1617 new_sv_t sv;
1618
1619 if( subdevice < 0 || obs < 0 ) return 0.0;
1620
1621 chanspec = setup->observables[obs].observe_insn.chanspec;
1622 target = setup->observables[obs].target;
1623
1624 range_ptr = comedi_get_range( setup->dev, subdevice, channel, range );
1625 assert( range_ptr != NULL );
1626
1627 comedi_set_global_oor_behavior( COMEDI_OOR_NUMBER );
1628 preobserve( setup, obs);
1629
1630 my_sv_init( &sv, setup, setup->ad_subdev, chanspec );
1631 new_sv_measure( setup->dev, &sv );
1632 reading = sv.average;
1633
1634 return ( reading - target ) / ( range_ptr->max - range_ptr->min );
1635 }
1636
1637 double get_tolerance( calibration_setup_t *setup, int subdevice,
1638 double num_bits )
1639 {
1640 int maxdata;
1641
1642 if( subdevice < 0 ) return INFINITY;
1643
1644 maxdata = comedi_get_maxdata( setup->dev, subdevice, 0 );
1645 assert( maxdata > 0 );
1646 return num_bits / maxdata;
1647 }
1648
1649 unsigned caldac_maxdata(comedi_t *dev, const comedi_caldac_t *caldac)
1650 {
1651 unsigned maxdata = comedi_get_maxdata(dev, caldac->subdevice, caldac->channel);
1652 assert(maxdata > 0);
1653 return maxdata;
1654 }