From 4a28456b8d8006e963ba8c55bee03a878a36318a Mon Sep 17 00:00:00 2001 From: Frank Mori Hess Date: Tue, 6 May 2003 01:24:50 +0000 Subject: [PATCH] added options for specifying which subdev, channel, range, aref you want comedi_calibrate to apply calibration for before exiting --- comedi_calibrate/calib.h | 3 - comedi_calibrate/cb.c | 10 +- comedi_calibrate/cb64.c | 2 +- comedi_calibrate/comedi_calibrate.c | 208 +++++++++++++++++----------- 4 files changed, 139 insertions(+), 84 deletions(-) diff --git a/comedi_calibrate/calib.h b/comedi_calibrate/calib.h index 38efe55..1a714c6 100644 --- a/comedi_calibrate/calib.h +++ b/comedi_calibrate/calib.h @@ -75,9 +75,6 @@ struct calibration_setup_struct { void *private_data; }; -extern char *devicename; -extern char *drivername; - extern int verbose; enum { diff --git a/comedi_calibrate/cb.c b/comedi_calibrate/cb.c index 39cd54f..3ac026a 100644 --- a/comedi_calibrate/cb.c +++ b/comedi_calibrate/cb.c @@ -97,7 +97,7 @@ int cb_setup( calibration_setup_t *setup, const char *device_name ) for( i = 0; i < num_boards; i++ ) { - if( !strcmp( devicename, boards[i].name ) ) + if( !strcmp( device_name, boards[i].name ) ) { setup->status = boards[i].status; return boards[i].setup( setup ); @@ -348,6 +348,7 @@ static int init_observables_1xxx( calibration_setup_t *setup ) return 0; } +#if 0 static int init_observables_1602_16( calibration_setup_t *setup ) { comedi_insn tmpl;//, po_tmpl; @@ -409,6 +410,7 @@ static int init_observables_1602_16( calibration_setup_t *setup ) return 0; } +#endif enum cal_knobs_1xxx { @@ -524,6 +526,12 @@ static int cal_cb_pci_1602_16( calibration_setup_t *setup ) { generic_layout_t layout; + if( comedi_get_version_code( setup->dev ) <= COMEDI_VERSION_CODE( 0, 7, 66 ) ) + { + DPRINT(0, "WARNING: you need comedi driver version 0.7.67 or later\n" + "for this calibration to work properly\n" ); + } + init_generic_layout( &layout ); layout.adc_gain = adc_gain_1602_16; layout.adc_offset = adc_pregain_offset_1602_16; diff --git a/comedi_calibrate/cb64.c b/comedi_calibrate/cb64.c index 8d06676..f487859 100644 --- a/comedi_calibrate/cb64.c +++ b/comedi_calibrate/cb64.c @@ -133,7 +133,7 @@ int cb64_setup( calibration_setup_t *setup, const char *device_name ) for( i = 0; i < num_boards; i++ ) { - if( !strcmp( devicename, boards[i].name ) ) + if( !strcmp( device_name, boards[i].name ) ) { setup->status = boards[i].status; return boards[i].setup( setup ); diff --git a/comedi_calibrate/comedi_calibrate.c b/comedi_calibrate/comedi_calibrate.c index 57bb5ab..7d0dfb3 100644 --- a/comedi_calibrate/comedi_calibrate.c +++ b/comedi_calibrate/comedi_calibrate.c @@ -43,10 +43,6 @@ #include "calib.h" /* global variables */ - -char *drivername = NULL; -char *devicename = NULL; - int verbose = 0; /* */ @@ -73,27 +69,6 @@ static int do_calibrate = 1; static int do_results = 0; static int do_output = 1; -struct option options[] = { - { "verbose", 0, 0, 'v' }, - { "quiet", 0, 0, 'q' }, - { "file", 1, 0, 'f' }, - { "save-file", 1, 0, 's' }, - { "help", 0, 0, 'h' }, - { "driver-name", 1, 0, 0x1000 }, - { "device-name", 1, 0, 0x1001 }, - { "reset", 0, &do_reset, 1 }, - { "no-reset", 0, &do_reset, 0 }, - { "calibrate", 0, &do_calibrate, 1 }, - { "no-calibrate", 0, &do_calibrate, 0 }, - { "dump", 0, &do_dump, 1 }, - { "no-dump", 0, &do_dump, 0 }, - { "results", 0, &do_results, 1 }, - { "no-results", 0, &do_results, 0 }, - { "output", 0, &do_output, 1 }, - { "no-output", 0, &do_output, 0 }, - { 0 }, -}; - void help(void) { printf("comedi_calibrate [options] - autocalibrates a Comedi device\n"); @@ -101,7 +76,7 @@ void help(void) printf(" --quiet, -q \n"); printf(" --help, -h \n"); printf(" --file, -f [/dev/comediN] \n"); - printf(" --save-file, -s [filepath] \n"); + printf(" --save-file, -S [filepath] \n"); printf(" --driver-name [driver] \n"); printf(" --device-name [device] \n"); printf(" --[no-]reset \n"); @@ -111,27 +86,63 @@ void help(void) printf(" --[no-]output \n"); } -int main(int argc, char *argv[]) +typedef struct { - char *fn = NULL; - int c; - int i; - struct board_struct *this_board; - int index; - int device_status = STATUS_UNKNOWN; - calibration_setup_t setup; - comedi_t *dev; - int ad_subdev; - int da_subdev; - int eeprom_subdev; - int caldac_subdev; - int retval; + int verbose; + char *file_path; + char *save_file_path; + char *driver_name; + char *device_name; + int do_reset; + int do_dump; + int do_calibrate; + int do_results; + int do_output; + unsigned int subdevice; + unsigned int channel; + unsigned int range; + unsigned int aref; +} parsed_options_t; - memset( &setup, 0, sizeof( setup ) ); +void parse_options( int argc, char *argv[], parsed_options_t *settings ) +{ + int c, index; - fn = "/dev/comedi0"; + struct option options[] = { + { "verbose", 0, 0, 'v' }, + { "quiet", 0, 0, 'q' }, + { "file", 1, 0, 'f' }, + { "save-file", 1, 0, 'S' }, + { "help", 0, 0, 'h' }, + { "driver-name", 1, 0, 0x1000 }, + { "device-name", 1, 0, 0x1001 }, + { "reset", 0, &settings->do_reset, 1 }, + { "no-reset", 0, &settings->do_reset, 0 }, + { "calibrate", 0, &settings->do_calibrate, 1 }, + { "no-calibrate", 0, &settings->do_calibrate, 0 }, + { "dump", 0, &settings->do_dump, 1 }, + { "no-dump", 0, &settings->do_dump, 0 }, + { "results", 0, &settings->do_results, 1 }, + { "no-results", 0, &settings->do_results, 0 }, + { "output", 0, &settings->do_output, 1 }, + { "no-output", 0, &settings->do_output, 0 }, + { "subdevice", 1, 0, 's' }, + { "channel", 1, 0, 'c' }, + { "range", 1, 0, 'r' }, + { "aref", 1, 0, 'a' }, + { 0 }, + }; + + memset( settings, 0, sizeof( *settings ) ); + settings->do_dump = 0; + settings->do_reset = 1; + settings->do_calibrate = 1; + settings->do_results = 0; + settings->do_output = 1; + + settings->file_path = "/dev/comedi0"; while (1) { - c = getopt_long(argc, argv, "f:s:vq", options, &index); + c = getopt_long(argc, argv, "f:S:vqs:c:r:a:", options, &index); if (c == -1)break; switch (c) { case 0: @@ -141,66 +152,94 @@ int main(int argc, char *argv[]) exit(0); break; case 'f': - fn = optarg; + settings->file_path = optarg; break; - case 's': - setup.cal_save_file_path = optarg; + case 'S': + settings->save_file_path = optarg; break; case 'v': - verbose++; + settings->verbose++; break; case 'q': - verbose--; + settings->verbose--; break; case 0x1000: - drivername = optarg; + settings->driver_name = optarg; break; case 0x1001: - devicename = optarg; + settings->device_name = optarg; + break; + case 's': + settings->subdevice = strtoul( optarg, NULL, 0 ); + break; + case 'c': + settings->channel = strtoul( optarg, NULL, 0 ); + break; + case 'r': + settings->range = strtoul( optarg, NULL, 0 ); + break; + case 'a': + settings->aref = strtoul( optarg, NULL, 0 ); break; default: help(); exit(1); } } +} - dev = comedi_open(fn); - if (dev == NULL ) { - fprintf( stderr, "comedi_open() failed, with device file name: %s\n", fn ); +int main(int argc, char *argv[]) +{ + int i; + struct board_struct *this_board; + int device_status = STATUS_UNKNOWN; + calibration_setup_t setup; + int retval; + parsed_options_t options; + + memset( &setup, 0, sizeof( setup ) ); + setup.settling_time_ns = 99999; + + parse_options( argc, argv, &options ); + setup.cal_save_file_path = options.save_file_path; + do_reset = options.do_reset; + do_dump = options.do_dump; + do_calibrate = options.do_calibrate; + do_results = options.do_results; + do_output = options.do_output; + + setup.dev = comedi_open( options.file_path ); + if( setup.dev == NULL ) { + fprintf( stderr, "comedi_open() failed, with device file name: %s\n", + options.file_path ); comedi_perror("comedi_open"); exit(0); } - if(!drivername) - drivername=comedi_get_driver_name(dev); - if(!devicename) - devicename=comedi_get_board_name(dev); + if(!options.driver_name) + options.driver_name=comedi_get_driver_name( setup.dev ); + if(!options.device_name) + options.device_name=comedi_get_board_name( setup.dev ); - ad_subdev=comedi_find_subdevice_by_type(dev,COMEDI_SUBD_AI,0); - da_subdev=comedi_find_subdevice_by_type(dev,COMEDI_SUBD_AO,0); - caldac_subdev=comedi_find_subdevice_by_type(dev,COMEDI_SUBD_CALIB,0); - eeprom_subdev=comedi_find_subdevice_by_type(dev,COMEDI_SUBD_MEMORY,0); + setup.ad_subdev=comedi_find_subdevice_by_type( setup.dev,COMEDI_SUBD_AI,0); + setup.da_subdev=comedi_find_subdevice_by_type( setup.dev,COMEDI_SUBD_AO,0); + setup.caldac_subdev=comedi_find_subdevice_by_type( setup.dev,COMEDI_SUBD_CALIB,0); + setup.eeprom_subdev=comedi_find_subdevice_by_type( setup.dev,COMEDI_SUBD_MEMORY,0); for(i=0;iinit_setup( &setup, devicename ); + retval = this_board->init_setup( &setup, options.device_name ); if( retval < 0 ){ - fprintf(stderr, "init_setup() failed for %s\n", devicename ); + fprintf(stderr, "init_setup() failed for %s\n", options.device_name ); return 1; } device_status = setup.status; @@ -235,18 +274,18 @@ ok: char *s = "$Id$"; printf("%.*s\n",(int)strlen(s)-2,s+1); - printf("Driver name: %s\n",drivername); - printf("Device name: %s\n",devicename); + printf("Driver name: %s\n", options.driver_name); + printf("Device name: %s\n", options.device_name); printf("%.*s\n",(int)strlen(this_board->id)-2,this_board->id+1); printf("Comedi version: %d.%d.%d\n", - (comedi_get_version_code(dev)>>16)&0xff, - (comedi_get_version_code(dev)>>8)&0xff, - (comedi_get_version_code(dev))&0xff); + (comedi_get_version_code(setup.dev)>>16)&0xff, + (comedi_get_version_code(setup.dev)>>8)&0xff, + (comedi_get_version_code(setup.dev))&0xff); } setup.do_reset = do_reset; setup.do_output = do_output; - + if(do_reset)reset_caldacs( &setup ); if(do_dump) observe( &setup ); if(do_calibrate && setup.do_cal) @@ -260,9 +299,20 @@ ok: } if(do_results) observe( &setup ); - comedi_close(dev); + retval = comedi_apply_calibration( setup.dev, options.subdevice, + options.channel, options.range, options.aref, setup.cal_save_file_path ); + if( retval < 0 ) + { + DPRINT( 0, "Failed to apply " ); + }else + { + DPRINT( 0, "Applied " ); + } + DPRINT( 0, "calibration for subdevice %i, channel %i, range %i, aref %i\n", + options.subdevice, options.channel, options.range, options.aref ); + comedi_close(setup.dev); - return 0; + return retval; } void set_target( calibration_setup_t *setup, int obs,double target)