summaryrefslogtreecommitdiff
path: root/src/hal/components/encoder.c
blob: f6b00d6864e24c7851c05b8225492e849b5a06d4 (plain)
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
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
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
/********************************************************************
* Description:  encoder.c
*               This file, 'encoder.c', is a HAL component that 
*               provides software based counting of quadrature 
*               encoder signals.
*
* Author: John Kasunich
* License: GPL Version 2
*    
* Copyright (c) 2003 All rights reserved.
*
* Last change: 
********************************************************************/
/** This file, 'encoder.c', is a HAL component that provides software
    based counting of quadrature encoder signals.  The maximum count
    rate will depend on the speed of the PC, but is expected to exceed
    1KHz for even the slowest computers, and may reach 10KHz on fast
    ones.  It is a realtime component.

    It supports up to eight counters, with optional index pulses.
    The number of counters is set by the module parameter 'num_chan='
    when the component is insmod'ed.  Alternatively, use the
    names= specifier and a list of unique names separated by commas.
    The names= and num_chan= specifiers are mutually exclusive.

    The driver exports variables for each counters inputs and output.
    It also exports two functions.  "encoder.update-counters" must be
    called in a high speed thread, at least twice the maximum desired
    count rate.  "encoder.capture-position" can be called at a much
    slower rate, and updates the output variables.
*/

/** Copyright (C) 2003 John Kasunich
                       <jmkasunich AT users DOT sourceforge DOT net>
*/

/** This program is free software; you can redistribute it and/or
    modify it under the terms of version 2 of the GNU General
    Public License as published by the Free Software Foundation.
    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 General Public License for more details.

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

    THE AUTHORS OF THIS LIBRARY ACCEPT ABSOLUTELY NO LIABILITY FOR
    ANY HARM OR LOSS RESULTING FROM ITS USE.  IT IS _EXTREMELY_ UNWISE
    TO RELY ON SOFTWARE ALONE FOR SAFETY.  Any machinery capable of
    harming persons must have provisions for completely removing power
    from all motors, etc, before persons enter any danger area.  All
    machinery must be designed to comply with local and national safety
    codes, and the authors of this software can not, and do not, take
    any responsibility for such compliance.

    This code was written as part of the EMC HAL project.  For more
    information, go to www.linuxcnc.org.
*/

#include "rtapi.h"		/* RTAPI realtime OS API */
#include "rtapi_app.h"		/* RTAPI realtime module decls */
#include "rtapi_string.h"
#include "hal.h"		/* HAL public API decls */

/* module information */
MODULE_AUTHOR("John Kasunich");
MODULE_DESCRIPTION("Encoder Counter for EMC HAL");
MODULE_LICENSE("GPL");

static int num_chan;
static int default_num_chan=3;
static int howmany;
RTAPI_MP_INT(num_chan, "number of encoder channels");

#define MAX_CHAN 8
char *names[MAX_CHAN] = {0,};
RTAPI_MP_ARRAY_STRING(names, MAX_CHAN, "names of encoder");

/***********************************************************************
*                STRUCTURES AND GLOBAL VARIABLES                       *
************************************************************************/

/* data that is atomically passed from fast function to slow one */

typedef struct {
    char count_detected;
    char index_detected;
    char latch_detected;
    __s32 raw_count;
    __u32 timestamp;
    __s32 index_count;
    __s32 latch_count;
} atomic;

/* this structure contains the runtime data for a single counter
   u:rw means update() reads and writes the
   c:w  means capture() writes the field
   c:s u:rc means capture() sets (to 1), update() reads and clears
*/

typedef struct {
    unsigned char state;	/* u:rw quad decode state machine state */
    unsigned char oldZ;		/* u:rw previous value of phase Z */
    unsigned char Zmask;	/* u:rc c:s mask for oldZ, from index-ena */
    hal_bit_t *x4_mode;		/* u:r enables x4 counting (default) */
    hal_bit_t *counter_mode;	/* u:r enables counter mode */
    atomic buf[2];		/* u:w c:r double buffer for atomic data */
    volatile atomic *bp;	/* u:r c:w ptr to in-use buffer */
    hal_s32_t *raw_counts;	/* u:rw raw count value, in update() only */
    hal_bit_t *phaseA;		/* u:r quadrature input */
    hal_bit_t *phaseB;		/* u:r quadrature input */
    hal_bit_t *phaseZ;		/* u:r index pulse input */
    hal_bit_t *index_ena;	/* c:rw index enable input */
    hal_bit_t *reset;		/* c:r counter reset input */
    hal_bit_t *latch_in;        /* c:r counter latch input */
    hal_bit_t *latch_rising;    /* u:r latch on rising edge? */
    hal_bit_t *latch_falling;   /* u:r latch on falling edge? */
    __s32 raw_count;		/* c:rw captured raw_count */
    __u32 timestamp;		/* c:rw captured timestamp */
    __s32 index_count;		/* c:rw captured index count */
    __s32 latch_count;		/* c:rw captured index count */
    hal_s32_t *count;		/* c:w captured binary count value */
    hal_s32_t *count_latch;     /* c:w captured binary count value */
    hal_float_t *min_speed;     /* c:r minimum velocity to estimate nonzero */
    hal_float_t *pos;		/* c:w scaled position (floating point) */
    hal_float_t *pos_interp;	/* c:w scaled and interpolated position (float) */
    hal_float_t *pos_latch;     /* c:w scaled latched position (floating point) */
    hal_float_t *vel;		/* c:w scaled velocity (floating point) */
    hal_float_t *pos_scale;	/* c:r pin: scaling factor for pos */
    hal_bit_t old_latch;        /* value of latch on previous cycle */
    double old_scale;		/* c:rw stored scale value */
    double scale;		/* c:rw reciprocal value used for scaling */
    int counts_since_timeout;	/* c:rw used for velocity calcs */
} counter_t;

static __u32 timebase;		/* master timestamp for all counters */

/* pointer to array of counter_t structs in shmem, 1 per counter */
static counter_t *counter_array;

/* bitmasks for quadrature decode state machine */
#define SM_PHASE_A_MASK 0x01
#define SM_PHASE_B_MASK 0x02
#define SM_LOOKUP_MASK  0x0F
#define SM_CNT_UP_MASK  0x40
#define SM_CNT_DN_MASK  0x80

/* Lookup table for quadrature decode state machine.  This machine
   will reject glitches on either input (will count up 1 on glitch,
   down 1 after glitch), and on both inputs simultaneously (no count
   at all)  In theory, it can count once per cycle, in practice the
   maximum count rate should be at _least_ 10% below the sample rate,
   and preferrable around half the sample rate.  It counts every
   edge of the quadrature waveform, 4 counts per complete cycle.
*/
static const unsigned char lut_x4[16] = {
    0x00, 0x44, 0x88, 0x0C, 0x80, 0x04, 0x08, 0x4C,
    0x40, 0x04, 0x08, 0x8C, 0x00, 0x84, 0x48, 0x0C
};

/* same thing, but counts only once per complete cycle */

static const unsigned char lut_x1[16] = {
    0x00, 0x44, 0x08, 0x0C, 0x80, 0x04, 0x08, 0x0C,
    0x00, 0x04, 0x08, 0x0C, 0x00, 0x04, 0x08, 0x0C
};

/* look-up table for a one-wire counter */

static const unsigned char lut_ctr[16] = {
   0x00, 0x48, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
};

/* other globals */
static int comp_id;		/* component ID */

/***********************************************************************
*                  LOCAL FUNCTION DECLARATIONS                         *
************************************************************************/

static int export_encoder(counter_t * addr,char * prefix);
static void update(void *arg, long period);
static void capture(void *arg, long period);

/***********************************************************************
*                       INIT AND EXIT CODE                             *
************************************************************************/


int rtapi_app_main(void)
{
    int n, retval, i;
    counter_t *cntr;

    if(num_chan && names[0]) {
        rtapi_print_msg(RTAPI_MSG_ERR,"num_chan= and names= are mutually exclusive\n");
        return -EINVAL;
    }
    if(!num_chan && !names[0]) num_chan = default_num_chan;

    if(num_chan) {
        howmany = num_chan;
    } else {
        howmany = 0;
        for (i = 0; i < MAX_CHAN; i++) {
            if (names[i] == NULL) {
                break;
            }
            howmany = i + 1;
        }
    }

    /* test for number of channels */
    if ((howmany <= 0) || (howmany > MAX_CHAN)) {
	rtapi_print_msg(RTAPI_MSG_ERR,
	    "ENCODER: ERROR: invalid number of channels: %d\n", howmany);
	return -1;
    }
    /* have good config info, connect to the HAL */
    comp_id = hal_init("encoder");
    if (comp_id < 0) {
	rtapi_print_msg(RTAPI_MSG_ERR, "ENCODER: ERROR: hal_init() failed\n");
	return -1;
    }
    /* allocate shared memory for counter data */
    counter_array = hal_malloc(howmany * sizeof(counter_t));
    if (counter_array == 0) {
	rtapi_print_msg(RTAPI_MSG_ERR,
	    "ENCODER: ERROR: hal_malloc() failed\n");
	hal_exit(comp_id);
	return -1;
    }
    /* init master timestamp counter */
    timebase = 0;
    /* export all the variables for each counter */
    i = 0; // for names= items
    for (n = 0; n < howmany; n++) {
	/* point to struct */
	cntr = &(counter_array[n]);
	/* export all vars */
        if(num_chan) {
            char buf[HAL_NAME_LEN + 1];
            rtapi_snprintf(buf, sizeof(buf), "encoder.%d", n);
	    retval = export_encoder(cntr,buf);
        } else {
	    retval = export_encoder(cntr,names[i++]);
        }
	if (retval != 0) {
	    rtapi_print_msg(RTAPI_MSG_ERR,
		"ENCODER: ERROR: counter %d var export failed\n", n);
	    hal_exit(comp_id);
	    return -1;
	}
	/* init counter */
	cntr->state = 0;
	cntr->oldZ = 0;
	cntr->Zmask = 0;
	*(cntr->x4_mode) = 1;
	*(cntr->counter_mode) = 0;
	*(cntr->latch_rising) = 1;
	*(cntr->latch_falling) = 1;
	cntr->buf[0].count_detected = 0;
	cntr->buf[1].count_detected = 0;
	cntr->buf[0].index_detected = 0;
	cntr->buf[1].index_detected = 0;
	cntr->bp = &(cntr->buf[0]);
	*(cntr->raw_counts) = 0;
	cntr->raw_count = 0;
	cntr->timestamp = 0;
	cntr->index_count = 0;
	cntr->latch_count = 0;
	*(cntr->count) = 0;
	*(cntr->min_speed) = 1.0;
	*(cntr->pos) = 0.0;
	*(cntr->pos_latch) = 0.0;
	*(cntr->vel) = 0.0;
	*(cntr->pos_scale) = 1.0;
	cntr->old_scale = 1.0;
	cntr->scale = 1.0;
	cntr->counts_since_timeout = 0;
    }
    /* export functions */
    retval = hal_export_funct("encoder.update-counters", update,
	counter_array, 0, 0, comp_id);
    if (retval != 0) {
	rtapi_print_msg(RTAPI_MSG_ERR,
	    "ENCODER: ERROR: count funct export failed\n");
	hal_exit(comp_id);
	return -1;
    }
    retval = hal_export_funct("encoder.capture-position", capture,
	counter_array, 1, 0, comp_id);
    if (retval != 0) {
	rtapi_print_msg(RTAPI_MSG_ERR,
	    "ENCODER: ERROR: capture funct export failed\n");
	hal_exit(comp_id);
	return -1;
    }
    rtapi_print_msg(RTAPI_MSG_INFO,
	"ENCODER: installed %d encoder counters\n", howmany);
    hal_ready(comp_id);
    return 0;
}

void rtapi_app_exit(void)
{
    hal_exit(comp_id);
}

/***********************************************************************
*            REALTIME ENCODER COUNTING AND UPDATE FUNCTIONS            *
************************************************************************/

static void update(void *arg, long period)
{
    counter_t *cntr;
    atomic *buf;
    int n;
    unsigned char state;
    int latch, old_latch, rising, falling;

    cntr = arg;
    for (n = 0; n < howmany; n++) {
	buf = (atomic *) cntr->bp;
	/* get state machine current state */
	state = cntr->state;
	/* add input bits to state code */
	if (*(cntr->phaseA)) {
	    state |= SM_PHASE_A_MASK;
	}
	if (*(cntr->phaseB)) {
	    state |= SM_PHASE_B_MASK;
	}
	/* look up new state */
	if ( *(cntr->counter_mode) ) {
	    state = lut_ctr[state & (SM_LOOKUP_MASK & ~SM_PHASE_B_MASK)];
	} else if ( *(cntr->x4_mode) ) {
	    state = lut_x4[state & SM_LOOKUP_MASK];
	} else {
	    state = lut_x1[state & SM_LOOKUP_MASK];
	}
	/* should we count? */
	if (state & SM_CNT_UP_MASK) {
	    (*cntr->raw_counts)++;
	    buf->raw_count = *(cntr->raw_counts);
	    buf->timestamp = timebase;
	    buf->count_detected = 1;
	} else if (state & SM_CNT_DN_MASK) {
	    (*cntr->raw_counts)--;
	    buf->raw_count = *(cntr->raw_counts);
	    buf->timestamp = timebase;
	    buf->count_detected = 1;
	}
	/* save state machine state */
	cntr->state = state;
	/* get old phase Z state, make room for new bit value */
	state = cntr->oldZ << 1;
	/* add new value of phase Z */
	if (*(cntr->phaseZ)) {
	    state |= 1;
	}
	cntr->oldZ = state & 3;
	/* test for index enabled and rising edge on phase Z */
	if ((state & cntr->Zmask) == 1) {
	    /* capture counts, reset Zmask */
	    buf->index_count = *(cntr->raw_counts);
	    buf->index_detected = 1;
	    cntr->Zmask = 0;
	}
        /* test for latch enabled and desired edge on latch-in */
        latch = *(cntr->latch_in), old_latch = cntr->old_latch;
        rising = latch && !old_latch;
        falling = !latch && old_latch;

        if((rising && *(cntr->latch_rising))
                || (falling && *(cntr->latch_falling))) {
            buf->latch_detected = 1;
            buf->latch_count = *(cntr->raw_counts);
        }
        cntr->old_latch = latch;

	/* move on to next channel */
	cntr++;
    }
    /* increment main timestamp counter */
    timebase += period;
    /* done */
}


static void capture(void *arg, long period)
{
    counter_t *cntr;
    atomic *buf;
    int n;
    __s32 delta_counts;
    __u32 delta_time;
    double vel, interp;

    cntr = arg;
    for (n = 0; n < howmany; n++) {
	/* point to active buffer */
	buf = (atomic *) cntr->bp;
	/* tell update() to use the other buffer */
	if ( buf == &(cntr->buf[0]) ) {
	    cntr->bp = &(cntr->buf[1]);
	} else {
	    cntr->bp = &(cntr->buf[0]);
	}
	/* handle index */
	if ( buf->index_detected ) {
	    buf->index_detected = 0;
	    cntr->index_count = buf->index_count;
	    *(cntr->index_ena) = 0;
	}
        /* handle latch */
	if ( buf->latch_detected ) {
	    buf->latch_detected = 0;
	    cntr->latch_count = buf->latch_count;
	}

	/* update Zmask based on index_ena */
	if (*(cntr->index_ena)) {
	    cntr->Zmask = 3;
	} else {
	    cntr->Zmask = 0;
	}
	/* done interacting with update() */
	/* check for change in scale value */
	if ( *(cntr->pos_scale) != cntr->old_scale ) {
	    /* save new scale to detect future changes */
	    cntr->old_scale = *(cntr->pos_scale);
	    /* scale value has changed, test and update it */
	    if ((*(cntr->pos_scale) < 1e-20) && (*(cntr->pos_scale) > -1e-20)) {
		/* value too small, divide by zero is a bad thing */
		*(cntr->pos_scale) = 1.0;
	    }
	    /* we actually want the reciprocal */
	    cntr->scale = 1.0 / *(cntr->pos_scale);
	}
        /* check for valid min_speed */
        if ( *(cntr->min_speed) == 0 ) {
            *(cntr->min_speed) = 1;
        }

	/* check reset input */
	if (*(cntr->reset)) {
	    /* reset is active, reset the counter */
	    /* note: we NEVER reset raw_counts, that is always a
		running count of edges seen since startup.  The
		public "count" is the difference between raw_count
		and index_count, so it will become zero. */
	    cntr->raw_count = *(cntr->raw_counts);
	    cntr->index_count = cntr->raw_count;
	}
	/* process data from update() */
	if ( buf->count_detected ) {
	    /* one or more counts in the last period */
	    buf->count_detected = 0;
	    delta_counts = buf->raw_count - cntr->raw_count;
	    delta_time = buf->timestamp - cntr->timestamp;
	    cntr->raw_count = buf->raw_count;
	    cntr->timestamp = buf->timestamp;
	    if ( cntr->counts_since_timeout < 2 ) {
		cntr->counts_since_timeout++;
	    } else {
		vel = (delta_counts * cntr->scale ) / (delta_time * 1e-9);
		*(cntr->vel) = vel;
	    }
	} else {
	    /* no count */
	    if ( cntr->counts_since_timeout ) {
		/* calc time since last count */
		delta_time = timebase - cntr->timestamp;
		if ( delta_time < 1e9 / ( *(cntr->min_speed) * cntr->scale )) {
		    /* not to long, estimate vel if a count arrived now */
		    vel = ( cntr->scale ) / (delta_time * 1e-9);
		    /* make vel positive, even if scale is negative */
		    if ( vel < 0.0 ) vel = -vel;
		    /* use lesser of estimate and previous value */
		    /* use sign of previous value, magnitude of estimate */
		    if ( vel < *(cntr->vel) ) {
			*(cntr->vel) = vel;
		    }
		    if ( -vel > *(cntr->vel) ) {
			*(cntr->vel) = -vel;
		    }
		} else {
		    /* its been a long time, stop estimating */
		    cntr->counts_since_timeout = 0;
		    *(cntr->vel) = 0;
		}
	    } else {
		/* we already stopped estimating */
		*(cntr->vel) = 0;
	    }
	}
	/* compute net counts */
	*(cntr->count) = cntr->raw_count - cntr->index_count;
        *(cntr->count_latch) = cntr->latch_count - cntr->index_count;
	/* scale count to make floating point position */
	*(cntr->pos) = *(cntr->count) * cntr->scale;
	*(cntr->pos_latch) = *(cntr->count_latch) * cntr->scale;
	/* add interpolation value */
	delta_time = timebase - cntr->timestamp;
	interp = *(cntr->vel) * (delta_time * 1e-9);
	*(cntr->pos_interp) = *(cntr->pos) + interp;
	/* move on to next channel */
	cntr++;
    }
    /* done */
}

/***********************************************************************
*                   LOCAL FUNCTION DEFINITIONS                         *
************************************************************************/

static int export_encoder(counter_t * addr,char * prefix)
{
    int retval, msg;

    /* This function exports a lot of stuff, which results in a lot of
       logging if msg_level is at INFO or ALL. So we save the current value
       of msg_level and restore it later.  If you actually need to log this
       function's actions, change the second line below */
    msg = rtapi_get_msg_level();
    rtapi_set_msg_level(RTAPI_MSG_WARN);

    /* export pins for the quadrature inputs */
    retval = hal_pin_bit_newf(HAL_IN, &(addr->phaseA), comp_id,
            "%s.phase-A", prefix);
    if (retval != 0) {
	return retval;
    }
    retval = hal_pin_bit_newf(HAL_IN, &(addr->phaseB), comp_id,
            "%s.phase-B", prefix);
    if (retval != 0) {
	return retval;
    }
    /* export pin for the index input */
    retval = hal_pin_bit_newf(HAL_IN, &(addr->phaseZ), comp_id,
            "%s.phase-Z", prefix);
    if (retval != 0) {
	return retval;
    }
    /* export pin for the index enable input */
    retval = hal_pin_bit_newf(HAL_IO, &(addr->index_ena), comp_id,
            "%s.index-enable", prefix);
    if (retval != 0) {
	return retval;
    }
    /* export pin for the reset input */
    retval = hal_pin_bit_newf(HAL_IN, &(addr->reset), comp_id,
            "%s.reset", prefix);
    if (retval != 0) {
	return retval;
    }
    /* export pins for position latching */
    retval = hal_pin_bit_newf(HAL_IN, &(addr->latch_in), comp_id,
            "%s.latch-input", prefix);
    if (retval != 0) {
	return retval;
    }
    retval = hal_pin_bit_newf(HAL_IN, &(addr->latch_rising), comp_id,
            "%s.latch-rising", prefix);
    if (retval != 0) {
	return retval;
    }
    retval = hal_pin_bit_newf(HAL_IN, &(addr->latch_falling), comp_id,
            "%s.latch-falling", prefix);
    if (retval != 0) {
	return retval;
    }

    /* export parameter for raw counts */
    retval = hal_pin_s32_newf(HAL_OUT, &(addr->raw_counts), comp_id,
            "%s.rawcounts", prefix);
    if (retval != 0) {
	return retval;
    }
    /* export pin for counts captured by capture() */
    retval = hal_pin_s32_newf(HAL_OUT, &(addr->count), comp_id,
            "%s.counts", prefix);
    if (retval != 0) {
	return retval;
    }
    /* export pin for counts latched by capture() */
    retval = hal_pin_s32_newf(HAL_OUT, &(addr->count_latch), comp_id,
            "%s.counts-latched", prefix);
    if (retval != 0) {
	return retval;
    }
    /* export pin for minimum speed estimated by capture() */
    retval = hal_pin_float_newf(HAL_IN, &(addr->min_speed), comp_id,
            "%s.min-speed-estimate", prefix);
    if (retval != 0) {
	return retval;
    }
    /* export pin for scaled position captured by capture() */
    retval = hal_pin_float_newf(HAL_OUT, &(addr->pos), comp_id,
            "%s.position", prefix);
    if (retval != 0) {
	return retval;
    }
    /* export pin for scaled and interpolated position captured by capture() */
    retval = hal_pin_float_newf(HAL_OUT, &(addr->pos_interp), comp_id,
            "%s.position-interpolated", prefix);
    if (retval != 0) {
	return retval;
    }
    /* export pin for latched position captured by capture() */
    retval = hal_pin_float_newf(HAL_OUT, &(addr->pos_latch), comp_id,
            "%s.position-latched", prefix);
    if (retval != 0) {
	return retval;
    }
    /* export pin for scaled velocity captured by capture() */
    retval = hal_pin_float_newf(HAL_OUT, &(addr->vel), comp_id,
            "%s.velocity", prefix);
    if (retval != 0) {
	return retval;
    }
    /* export pin for scaling */
    retval = hal_pin_float_newf(HAL_IO, &(addr->pos_scale), comp_id,
            "%s.position-scale", prefix);
    if (retval != 0) {
	return retval;
    }
    /* export pin for x4 mode */
    retval = hal_pin_bit_newf(HAL_IO, &(addr->x4_mode), comp_id,
            "%s.x4-mode", prefix);
    if (retval != 0) {
	return retval;
    }
    /* export pin for counter mode */
    retval = hal_pin_bit_newf(HAL_IO, &(addr->counter_mode), comp_id,
            "%s.counter-mode", prefix);
    if (retval != 0) {
	return retval;
    }
    /* restore saved message level */
    rtapi_set_msg_level(msg);
    return 0;
}