| 1 |
/*************************************************************************** |
| 2 |
* * |
| 3 |
* This program is free software; you can redistribute it and/or modify * |
| 4 |
* it under the terms of the GNU Lesser General Public License as * |
| 5 |
* published by * |
| 6 |
* the Free Software Foundation; either version 2.1 of the License, or * |
| 7 |
* (at your option) any later version. * |
| 8 |
* * |
| 9 |
***************************************************************************/ |
| 10 |
|
| 11 |
#ifndef __CALIB_H_ |
| 12 |
#define __CALIB_H_ |
| 13 |
|
| 14 |
#include "comedilib.h" |
| 15 |
#include "../libcomedi_calibrate/comedi_calibrate_shared.h" |
| 16 |
#if 0 |
| 17 |
#include <stdio.h> |
| 18 |
#include <fcntl.h> |
| 19 |
#include <unistd.h> |
| 20 |
#include <errno.h> |
| 21 |
#include <getopt.h> |
| 22 |
#include <ctype.h> |
| 23 |
#include <math.h> |
| 24 |
#include <stdlib.h> |
| 25 |
#include <string.h> |
| 26 |
#endif |
| 27 |
|
| 28 |
#define DPRINT(level,fmt,args...) do{if(verbose>=level)printf(fmt, ## args);}while(0) |
| 29 |
|
| 30 |
#define N_CALDACS 64 |
| 31 |
#define N_OBSERVABLES 128 |
| 32 |
#define PREOBSERVE_DATA_LEN 10 |
| 33 |
|
| 34 |
static const int caldac_settle_usec = 100000; |
| 35 |
|
| 36 |
typedef struct{ |
| 37 |
char *name; |
| 38 |
|
| 39 |
comedi_insn preobserve_insn; |
| 40 |
lsampl_t preobserve_data[ PREOBSERVE_DATA_LEN ]; |
| 41 |
|
| 42 |
comedi_insn observe_insn; |
| 43 |
|
| 44 |
int reference_source; |
| 45 |
double target; |
| 46 |
}observable; |
| 47 |
|
| 48 |
typedef struct calibration_setup_struct calibration_setup_t; |
| 49 |
struct calibration_setup_struct { |
| 50 |
comedi_t *dev; |
| 51 |
int ad_subdev; |
| 52 |
int da_subdev; |
| 53 |
int eeprom_subdev; |
| 54 |
int caldac_subdev; |
| 55 |
int status; |
| 56 |
unsigned int sv_settling_time_ns; |
| 57 |
unsigned int sv_order; |
| 58 |
observable observables[ N_OBSERVABLES ]; |
| 59 |
unsigned int n_observables; |
| 60 |
comedi_caldac_t caldacs[ N_CALDACS ]; |
| 61 |
unsigned int n_caldacs; |
| 62 |
int (*do_cal) ( calibration_setup_t *setup ); |
| 63 |
const char *cal_save_file_path; |
| 64 |
unsigned do_output : 1; |
| 65 |
comedi_calibration_t *old_calibration; |
| 66 |
comedi_calibration_t *new_calibration; |
| 67 |
void *private_data; |
| 68 |
}; |
| 69 |
|
| 70 |
extern int verbose; |
| 71 |
|
| 72 |
enum { |
| 73 |
STATUS_UNKNOWN = 0, |
| 74 |
STATUS_GUESS, |
| 75 |
STATUS_SOME, |
| 76 |
STATUS_DONE |
| 77 |
}; |
| 78 |
|
| 79 |
/* high level */ |
| 80 |
|
| 81 |
void observe( calibration_setup_t *setup ); |
| 82 |
int preobserve( calibration_setup_t *setup, int obs); |
| 83 |
void observable_dependence( calibration_setup_t *setup, int obs); |
| 84 |
void measure_observable( calibration_setup_t *setup, int obs); |
| 85 |
void reset_caldac( calibration_setup_t *setup, int caldac_index ); |
| 86 |
void reset_caldacs( calibration_setup_t *setup); |
| 87 |
|
| 88 |
/* drivers */ |
| 89 |
|
| 90 |
extern char ni_id[]; |
| 91 |
extern char cb_id[]; |
| 92 |
extern char cb64_id[]; |
| 93 |
extern char ni_labpc_id[]; |
| 94 |
|
| 95 |
int ni_setup( calibration_setup_t*, const char *device_name ); |
| 96 |
int cb_setup( calibration_setup_t*, const char *device_name ); |
| 97 |
int cb64_setup( calibration_setup_t*, const char *device_name ); |
| 98 |
int ni_labpc_setup( calibration_setup_t*, const char *device_name ); |
| 99 |
|
| 100 |
/* low level */ |
| 101 |
|
| 102 |
void set_target( calibration_setup_t *setup, int obs,double target); |
| 103 |
void update_caldac( calibration_setup_t *setup, int caldac_index, int value ); |
| 104 |
void setup_caldacs( calibration_setup_t *setup, int caldac_subdev); |
| 105 |
void postgain_cal( calibration_setup_t *setup, int obs1, int obs2, int dac); |
| 106 |
void cal1( calibration_setup_t *setup, int obs, int dac); |
| 107 |
void cal1_fine( calibration_setup_t *setup, int obs, int dac); |
| 108 |
void cal_binary( calibration_setup_t *setup, int obs, int dac); |
| 109 |
void cal_postgain_binary( calibration_setup_t *setup, int obs1, int obs2, int dac); |
| 110 |
void cal_relative_binary( calibration_setup_t *setup, int obs1, int obs2, int dac); |
| 111 |
void cal_linearity_binary( calibration_setup_t *setup, int obs1, int obs2, int obs3, int dac); |
| 112 |
void peg_binary( calibration_setup_t *setup, int obs, int dac, int maximize ); |
| 113 |
|
| 114 |
/* misc and temp */ |
| 115 |
|
| 116 |
void channel_dependence(int adc,int range); |
| 117 |
void caldac_dependence(int caldac); |
| 118 |
void chan_cal(int adc,int caldac,int range,double target); |
| 119 |
int read_eeprom( calibration_setup_t *setup, int addr); |
| 120 |
|
| 121 |
double read_chan( calibration_setup_t *setup, int adc,int range); |
| 122 |
int read_chan2( calibration_setup_t *setup, char *s,int adc,int range); |
| 123 |
void set_ao(comedi_t *dev,int subdev,int chan,int range,double value); |
| 124 |
void check_gain(int ad_chan,int range); |
| 125 |
double check_gain_chan(int ad_chan,int range,int cdac); |
| 126 |
|
| 127 |
int cb_actual_source_voltage( comedi_t *dev, unsigned int subdevice, |
| 128 |
unsigned int eeprom_channel, float *voltage); |
| 129 |
|
| 130 |
/* helper functions */ |
| 131 |
|
| 132 |
int get_bipolar_lowgain(comedi_t *dev,int subdev); |
| 133 |
int get_bipolar_highgain(comedi_t *dev,int subdev); |
| 134 |
int get_unipolar_lowgain(comedi_t *dev,int subdev); |
| 135 |
int get_unipolar_highgain(comedi_t *dev,int subdev); |
| 136 |
double very_low_target( comedi_t *dev, unsigned int subdevice, |
| 137 |
unsigned int channel, unsigned int range ); |
| 138 |
int is_bipolar( comedi_t *dev, unsigned int subdevice, |
| 139 |
unsigned int channel, unsigned int range ); |
| 140 |
int is_unipolar( comedi_t *dev, unsigned int subdevice, |
| 141 |
unsigned int channel, unsigned int range ); |
| 142 |
|
| 143 |
double fractional_offset( calibration_setup_t *setup, int subdevice, |
| 144 |
unsigned int channel, unsigned int range, int obs ); |
| 145 |
double get_tolerance( calibration_setup_t *setup, int subdevice, |
| 146 |
double num_bits ); |
| 147 |
|
| 148 |
/* other */ |
| 149 |
|
| 150 |
void comedi_nanodelay(comedi_t *dev, unsigned int delay); |
| 151 |
unsigned caldac_maxdata(comedi_t *dev, const comedi_caldac_t *caldac); |
| 152 |
|
| 153 |
/* printing scientific numbers */ |
| 154 |
|
| 155 |
int sci_sprint(char *s,double x,double y); |
| 156 |
int sci_sprint_alt(char *s,double x,double y); |
| 157 |
|
| 158 |
/* linear fitting */ |
| 159 |
|
| 160 |
typedef struct { |
| 161 |
int n; |
| 162 |
|
| 163 |
double *y_data; |
| 164 |
double *yerr_data; |
| 165 |
double *x_data; |
| 166 |
|
| 167 |
double x0; |
| 168 |
double dx; |
| 169 |
double yerr; |
| 170 |
|
| 171 |
/* stats */ |
| 172 |
double s1,sx,sy,sxy,sxx; |
| 173 |
|
| 174 |
double min,max; |
| 175 |
|
| 176 |
/* results */ |
| 177 |
double ave_x; |
| 178 |
double ave_y; |
| 179 |
double slope; |
| 180 |
double err_slope; |
| 181 |
double err_ave_y; |
| 182 |
double S_min; |
| 183 |
double dof; |
| 184 |
|
| 185 |
}linear_fit_t; |
| 186 |
int linear_fit_monotonic(linear_fit_t *l); |
| 187 |
double linear_fit_func_y(linear_fit_t *l,double x); |
| 188 |
double linear_fit_func_x(linear_fit_t *l,double y); |
| 189 |
double check_gain_chan_x( calibration_setup_t *setup, linear_fit_t *l,unsigned int ad_chanspec,int cdac); |
| 190 |
double check_gain_chan_fine( calibration_setup_t *setup, linear_fit_t *l,unsigned int ad_chanspec,int cdac); |
| 191 |
void dump_curve(linear_fit_t *l); |
| 192 |
|
| 193 |
/* slowly varying measurements */ |
| 194 |
|
| 195 |
typedef struct{ |
| 196 |
comedi_t *dev; |
| 197 |
|
| 198 |
int maxdata; |
| 199 |
int order; |
| 200 |
int subd; |
| 201 |
unsigned int chanspec; |
| 202 |
unsigned int settling_time_ns; |
| 203 |
|
| 204 |
comedi_range *rng; |
| 205 |
|
| 206 |
double average; |
| 207 |
double stddev; |
| 208 |
double error; |
| 209 |
}new_sv_t; |
| 210 |
|
| 211 |
int new_sv_measure(comedi_t *dev, new_sv_t *sv); |
| 212 |
int new_sv_init(new_sv_t *sv,comedi_t *dev,int subdev,unsigned int chanspec); |
| 213 |
int my_sv_init( new_sv_t *sv, const calibration_setup_t *setup, int subdev, |
| 214 |
unsigned int chanspec ); |
| 215 |
|
| 216 |
/* generic calibration support */ |
| 217 |
typedef struct |
| 218 |
{ |
| 219 |
int (*adc_offset)( unsigned int channel ); |
| 220 |
int (*adc_offset_fine)( unsigned int channel ); |
| 221 |
int (*adc_postgain_offset)( unsigned int channel ); |
| 222 |
int (*adc_gain)( unsigned int channel ); |
| 223 |
int (*adc_gain_fine)( unsigned int channel ); |
| 224 |
int (*dac_linearity)( unsigned int channel ); |
| 225 |
int (*dac_linearity_fine)( unsigned int channel ); |
| 226 |
int (*dac_offset)( unsigned int channel ); |
| 227 |
int (*dac_offset_fine)( unsigned int channel ); |
| 228 |
int (*dac_gain)( unsigned int channel ); |
| 229 |
int (*dac_gain_fine)( unsigned int channel ); |
| 230 |
int (*adc_high_observable)( const calibration_setup_t *setup, |
| 231 |
unsigned int channel, unsigned int range ); |
| 232 |
int (*adc_ground_observable)( const calibration_setup_t *setup, |
| 233 |
unsigned int channel, unsigned int range ); |
| 234 |
int (*dac_high_observable)( const calibration_setup_t *setup, |
| 235 |
unsigned int channel, unsigned int range ); |
| 236 |
int (*dac_mid_observable)( const calibration_setup_t *setup, |
| 237 |
unsigned int channel, unsigned int range ); |
| 238 |
int (*dac_ground_observable)( const calibration_setup_t *setup, |
| 239 |
unsigned int channel, unsigned int range ); |
| 240 |
double adc_fractional_tolerance; |
| 241 |
double dac_fractional_tolerance; |
| 242 |
unsigned do_adc_unipolar_postgain : 1; |
| 243 |
} generic_layout_t; |
| 244 |
void init_generic_layout( generic_layout_t *layout ); |
| 245 |
int generic_cal_by_channel_and_range( calibration_setup_t *setup, |
| 246 |
const generic_layout_t *layout ); |
| 247 |
int generic_cal_by_range( calibration_setup_t *setup, |
| 248 |
const generic_layout_t *layout ); |
| 249 |
int generic_cal_ao(calibration_setup_t *setup, |
| 250 |
const generic_layout_t *layout ); |
| 251 |
void generic_do_cal( calibration_setup_t *setup, |
| 252 |
comedi_calibration_setting_t *saved_cal, int observable, int caldac ); |
| 253 |
void generic_do_relative( calibration_setup_t *setup, |
| 254 |
comedi_calibration_setting_t *saved_cal, int observable1, int observable2, int caldac ); |
| 255 |
void generic_do_linearity( calibration_setup_t *setup, |
| 256 |
comedi_calibration_setting_t *saved_cal, int observable1, int observable2, |
| 257 |
int observable3, int caldac ); |
| 258 |
void generic_prep_adc_caldacs( calibration_setup_t *setup, |
| 259 |
const generic_layout_t *layout, unsigned int channel, unsigned int range ); |
| 260 |
void generic_prep_dac_caldacs( calibration_setup_t *setup, |
| 261 |
const generic_layout_t *layout, unsigned int channel, unsigned int range ); |
| 262 |
void generic_peg( calibration_setup_t *setup, int observable, int caldac, |
| 263 |
int maximize ); |
| 264 |
|
| 265 |
#endif |
| 266 |
|