File: calib.c

package info (click to toggle)
comedilib 0.11.0%2B5-1.3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 8,544 kB
  • sloc: xml: 19,779; ansic: 14,719; sh: 5,679; cpp: 2,211; ruby: 1,658; perl: 700; makefile: 596; yacc: 439; lex: 86; python: 17
file content (357 lines) | stat: -rw-r--r-- 9,580 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/*
    lib/calib.c
    functions for setting calibration

    Copyright (C) 2003 Frank Mori Hess <fmhess@users.sourceforge.net

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation, version 2.1
    of the License.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
    USA.
*/

#define _GNU_SOURCE

#include <assert.h>
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/sysmacros.h>
#include <unistd.h>

#include "libinternal.h"

static int set_calibration( comedi_t *dev, const comedi_calibration_t *parsed_file,
	unsigned int cal_index );

static int check_cal_file( comedi_t *dev, const comedi_calibration_t *parsed_file )
{
	if( strcmp( comedi_get_driver_name( dev ), parsed_file->driver_name ) )
	{
		COMEDILIB_DEBUG( 3, "driver name does not match '%s' from calibration file\n",
			parsed_file->driver_name );
		return -1;
	}

	if( strcmp( comedi_get_board_name( dev ), parsed_file->board_name ) )
	{
		COMEDILIB_DEBUG( 3, "board name does not match '%s' from calibration file\n",
			parsed_file->board_name );
		return -1;
	}

	return 0;
}

static inline int valid_channel( const comedi_calibration_t *parsed_file,
	unsigned int cal_index, unsigned int channel )
{
	int num_channels, i;

	num_channels = parsed_file->settings[ cal_index ].num_channels;
	if( num_channels == 0 ) return 1;
	for( i = 0; i < num_channels; i++ )
	{
		if( parsed_file->settings[ cal_index ].channels[ i ] == channel )
			return 1;
	}

	return 0;
}

static inline int valid_range( const comedi_calibration_t *parsed_file,
	unsigned int cal_index, unsigned int range )
{
	int num_ranges, i;

	num_ranges = parsed_file->settings[ cal_index ].num_ranges;
	if( num_ranges == 0 ) return 1;
	for( i = 0; i < num_ranges; i++ )
	{
		if( parsed_file->settings[ cal_index ].ranges[ i ] == range )
			return 1;
	}

	return 0;
}

static inline int valid_aref( const comedi_calibration_t *parsed_file,
	unsigned int cal_index, unsigned int aref )
{
	int num_arefs, i;

	num_arefs = parsed_file->settings[ cal_index ].num_arefs;
	if( num_arefs == 0 ) return 1;
	for( i = 0; i < num_arefs; i++ )
	{
		if( parsed_file->settings[ cal_index ].arefs[ i ] == aref )
			return 1;
	}

	return 0;
}

static int apply_calibration( comedi_t *dev, const comedi_calibration_t *parsed_file,
	unsigned int subdev, unsigned int channel, unsigned int range, unsigned int aref )
{
	int num_cals, i, retval;
	int found_cal = 0;

	num_cals = parsed_file->num_settings;

	for( i = 0; i < num_cals; i++ )
	{
		if( parsed_file->settings[ i ].subdevice != subdev ) continue;
		if( valid_range( parsed_file, i, range ) == 0 ) continue;
		if( valid_channel( parsed_file, i, channel ) == 0 ) continue;
		if( valid_aref( parsed_file, i, aref ) == 0 ) continue;

		retval = set_calibration( dev, parsed_file, i );
		if( retval < 0 ) return retval;
		found_cal = 1;
	}
	if( found_cal == 0 )
	{
		COMEDILIB_DEBUG( 3, "failed to find matching calibration\n" );
		return -1;
	}

	return 0;
}

static int set_calibration( comedi_t *dev, const comedi_calibration_t *parsed_file,
	unsigned int cal_index )
{
	int i, retval, num_caldacs;

	num_caldacs = parsed_file->settings[ cal_index ].num_caldacs;
	COMEDILIB_DEBUG( 4, "num_caldacs %i\n", num_caldacs );

	for( i = 0; i < num_caldacs; i++ )
	{
		comedi_caldac_t caldac;

		caldac = parsed_file->settings[ cal_index ].caldacs[ i ];
		COMEDILIB_DEBUG( 4, "subdev %i, ch %i, val %i\n", caldac.subdevice,
			caldac.channel,caldac.value);
		retval = comedi_data_write( dev, caldac.subdevice, caldac.channel,
			0, 0, caldac.value );
		if( retval < 0 ) return retval;
	}

	return 0;
}

EXPORT_ALIAS_DEFAULT(_comedi_apply_parsed_calibration,comedi_apply_parsed_calibration,0.7.20);
int _comedi_apply_parsed_calibration( comedi_t *dev, unsigned int subdev, unsigned int channel,
	unsigned int range, unsigned int aref, const comedi_calibration_t *calibration )
{
	int retval;

	if(!valid_dev(dev)) return -1;
	retval = check_cal_file( dev, calibration );
	if( retval < 0 ) return retval;

	retval = apply_calibration( dev, calibration, subdev, channel, range, aref );
	return retval;
}

/* munge characters in board name that will cause problems with file paths */
static void fixup_board_name( char *name )
{
	while( ( name = strchr( name, '/' ) ) )
	{
		if( name )
		{
			*name = '-';
			name++;
		}
	}
}

EXPORT_ALIAS_DEFAULT(_comedi_get_default_calibration_path,comedi_get_default_calibration_path,0.7.20);
char* _comedi_get_default_calibration_path( comedi_t *dev )
{
	struct stat file_stats;
	char *file_path;
	const char *temp;
	char *board_name;
	const char *driver_name;
	int err;

	if(!valid_dev(dev)) return NULL;
	if( fstat( comedi_fileno( dev ), &file_stats ) < 0 )
	{
		COMEDILIB_DEBUG( 3, "failed to get file stats of comedi device file\n" );
		return NULL;
	}

	driver_name = comedi_get_driver_name( dev );
	if( driver_name == NULL )
	{
		return NULL;
	}
	temp = comedi_get_board_name( dev );
	if( temp == NULL )
	{
		return NULL;
	}
	board_name = strdup( temp );

	fixup_board_name( board_name );
	err = asprintf( &file_path, LOCALSTATEDIR "/lib/comedi/calibrations/%s_%s_comedi%li",
		driver_name, board_name, ( unsigned long ) minor( file_stats.st_rdev ) );

	free( board_name );
	if( err < 0 )
	{
		return NULL;
	}
	return file_path;
}

EXPORT_ALIAS_DEFAULT(_comedi_apply_calibration,comedi_apply_calibration,0.7.20);
int _comedi_apply_calibration( comedi_t *dev, unsigned int subdev, unsigned int channel,
	unsigned int range, unsigned int aref, const char *cal_file_path )
{
	char file_path[ 1024 ];
	int retval;
	comedi_calibration_t *parsed_file;

	if( cal_file_path )
	{
		strncpy( file_path, cal_file_path, sizeof( file_path ) );
	}else
	{
		char *temp;

		temp = comedi_get_default_calibration_path( dev );
		if( temp == NULL ) return -1;
		strncpy( file_path, temp, sizeof( file_path ) );
		free( temp );
	}

	parsed_file = comedi_parse_calibration_file( file_path );
	if( parsed_file == NULL )
	{
		COMEDILIB_DEBUG( 3, "failed to parse calibration file\n" );
		return -1;
	}

	retval = comedi_apply_parsed_calibration( dev, subdev, channel, range, aref, parsed_file );

	comedi_cleanup_calibration( parsed_file );

	return retval;
}

EXPORT_ALIAS_DEFAULT(_comedi_get_hardcal_converter, comedi_get_hardcal_converter, 0.8.0);
int _comedi_get_hardcal_converter(
	comedi_t *dev, unsigned subdevice, unsigned channel, unsigned range,
	enum comedi_conversion_direction direction,
	comedi_polynomial_t* polynomial)
{
	comedi_range *range_ptr = comedi_get_range(dev, subdevice, channel, range);
	lsampl_t maxdata;

	if(range_ptr == NULL)
	{
		return -1;
	}
	maxdata = comedi_get_maxdata(dev, subdevice, channel);
	if(maxdata == 0)
	{
		return -1;
	}
	polynomial->order = 1;
	switch(direction)
	{
	case COMEDI_TO_PHYSICAL:
		polynomial->expansion_origin = 0.;
		polynomial->coefficients[0] = range_ptr->min;
		polynomial->coefficients[1] = (range_ptr->max - range_ptr->min) / maxdata;
		break;
	case COMEDI_FROM_PHYSICAL:
		polynomial->expansion_origin = range_ptr->min;
		polynomial->coefficients[0] = 0.;
		polynomial->coefficients[1] =  maxdata / (range_ptr->max - range_ptr->min);
		break;
	}
	return 0;
}

EXPORT_ALIAS_DEFAULT(_comedi_get_softcal_converter, comedi_get_softcal_converter, 0.8.0);
int _comedi_get_softcal_converter(
	unsigned subdevice, unsigned channel, unsigned range,
	enum comedi_conversion_direction direction,
	const comedi_calibration_t *calibration, comedi_polynomial_t* polynomial)
{
	unsigned i;

	for(i = 0; i < calibration->num_settings; ++i)
	{
		if(calibration->settings[i].subdevice != subdevice) continue;
		if(valid_channel(calibration, i, channel) == 0) continue;
		if(valid_range(calibration, i, range) == 0) continue;
		switch(direction)
		{
		case COMEDI_TO_PHYSICAL:
			if(calibration->settings[i].soft_calibration.to_phys == NULL)
			{
				continue;
			}
			*polynomial = *calibration->settings[i].soft_calibration.to_phys;
			break;
		case COMEDI_FROM_PHYSICAL:
			if(calibration->settings[i].soft_calibration.from_phys == NULL)
			{
				continue;
			}
			*polynomial = *calibration->settings[i].soft_calibration.from_phys;
			break;
		}
		return 0;
	}
	return -1;
}

static double apply_polynomial(const comedi_polynomial_t *polynomial, double input)
{
	double value = 0.;
	double term = 1.;
	unsigned i;
	assert(polynomial->order < COMEDI_MAX_NUM_POLYNOMIAL_COEFFICIENTS);
	for(i = 0; i <= polynomial->order; ++i)
	{
		value += polynomial->coefficients[i] * term;
		term *= input - polynomial->expansion_origin;
	}
	return value;
}

EXPORT_ALIAS_DEFAULT(_comedi_to_physical, comedi_to_physical, 0.8.0);
double _comedi_to_physical(lsampl_t data,
	const comedi_polynomial_t *conversion_polynomial)
{
	return apply_polynomial(conversion_polynomial, data);
}

EXPORT_ALIAS_DEFAULT(_comedi_from_physical, comedi_from_physical, 0.8.0);
lsampl_t _comedi_from_physical(double data,
	const comedi_polynomial_t *conversion_polynomial)
{
	return nearbyint(apply_polynomial(conversion_polynomial, data));
}