encoder.c

来自「CNC 的开放码,EMC2 V2.2.8版」· C语言 代码 · 共 537 行 · 第 1/2 页

C
537
字号
/********************************************************************* 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: # $Revision: 1.29.4.1 $* $Author: jmkasunich $* $Date: 2008/06/30 00:13:17 $********************************************************************//** 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.    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.*/#ifndef RTAPI#error This is a realtime component only!#endif#include "rtapi.h"		/* RTAPI realtime OS API */#include "rtapi_app.h"		/* RTAPI realtime module decls */#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 = 3;	/* number of channels - default = 3 */RTAPI_MP_INT(num_chan, "number of channels");/************************************************************************                STRUCTURES AND GLOBAL VARIABLES                       *************************************************************************//* data that is atomically passed from fast function to slow one */typedef struct {    char count_detected;    __s32 raw_count;    __u32 timestamp;    char index_detected;    __s32 index_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 */    __s32 raw_count;		/* c:rw captured raw_count */    __u32 timestamp;		/* c:rw captured timestamp */    __s32 index_count;		/* c:rw captured index count */    hal_s32_t *count;		/* c:w captured binary count value */    hal_float_t *pos;		/* c:w scaled position (floating point) */    hal_float_t *vel;		/* c:w scaled velocity (floating point) */    hal_float_t pos_scale;	/* c:r parameter: scaling factor for pos */    float 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_counter(int num, counter_t * addr);static void update(void *arg, long period);static void capture(void *arg, long period);/************************************************************************                       INIT AND EXIT CODE                             *************************************************************************/#define MAX_CHAN 8int rtapi_app_main(void){    int n, retval;    counter_t *cntr;    /* test for number of channels */    if ((num_chan <= 0) || (num_chan > MAX_CHAN)) {	rtapi_print_msg(RTAPI_MSG_ERR,	    "ENCODER: ERROR: invalid num_chan: %d\n", num_chan);	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(num_chan * 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 */    for (n = 0; n < num_chan; n++) {	/* point to struct */	cntr = &(counter_array[n]);	/* export all vars */	retval = export_counter(n, cntr);	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->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->count) = 0;	*(cntr->pos) = 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", num_chan);    hal_ready(comp_id);    return 0;}void rtapi_app_exit(void){    hal_exit(comp_id);}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?