📄 stepgen.c
字号:
/********************************************************************* Description: stepgen.c* This file, 'stepgen.c', is a HAL component that * provides software based step pulse generation.** Author: John Kasunich* License: GPL Version 2* * Copyright (c) 2003-2007 All rights reserved.** Last change: # $Revision: 1.59 $* $Author: jepler $* $Date: 2007/09/15 21:53:08 $********************************************************************//** This file, 'stepgen.c', is a HAL component that provides software based step pulse generation. The maximum step rate will depend on the speed of the PC, but is expected to exceed 5KHz for even the slowest computers, and may reach 25KHz on fast ones. It is a realtime component. It supports up to 8 pulse generators. Each generator can produce several types of outputs in addition to step/dir, including quadrature, half- and full-step unipolar and bipolar, three phase, and five phase. A 32 bit feedback value is provided indicating the current position of the motor in counts (assuming no lost steps), and a floating point feedback in user specified position units is also provided. The number of step generators and type of outputs is determined by the insmod command line parameter 'step_type'. It accepts a comma separated (no spaces) list of up to 8 stepping types to configure up to 8 channels. A second command line parameter "ctrl_type", selects between position and velocity control modes for each step generator. (ctrl_type is optional, the default control type is position.) So a command line like this: insmod stepgen step_type=0,0,1,2 ctrl_type=p,p,v,p will install four step generators, two using stepping type 0, one using type 1, and one using type 2. The first two and the last one will be running in position mode, and the third one will be running in velocity mode. The driver exports three functions. 'stepgen.make-pulses', is responsible for actually generating the step pulses. It must be executed in a fast thread to reduce pulse jitter. The other two functions are normally called from a much slower thread. 'stepgen.update-freq' reads the position or frequency command and sets internal variables used by 'stepgen.make-pulses'. 'stepgen.capture-position' captures and scales the current values of the position feedback counters. Both 'update-freq' and 'capture-position' use floating point, 'make-pulses' does not. Polarity: All signals from this module have fixed polarity (active high in most cases). If the driver needs the opposite polarity, the signals can be inverted using parameters exported by the hardware driver(s) such as ParPort. Timing parameters: There are five timing parameters which control the output waveform. No step type uses all five, and only those which will be used are exported to HAL. The values of these parameters are in nano-seconds, so no recalculation is needed when changing thread periods. In the timing diagrams that follow, they are identfied by the following numbers: (1): 'stepgen.n.steplen' = length of the step pulse (2): 'stepgen.n.stepspace' = minimum space between step pulses (actual space depends on frequency command, and is infinite if the frequency command is zero) (3): 'stepgen.n.dirhold' = minimum delay after a step pulse before a direction change - may be longer (4): 'stepgen.n.dirsetup' = minimum delay after a direction change and before the next step - may be longer (5): 'stepgen.n.dirdelay' = minimum delay after a step before a step in the opposite direction - may be longer Stepping Types: This module supports a number of stepping types, as follows: Type 0: Step and Direction _____ _____ _____ STEP ____/ \_______/ \_____________/ \______ | | | | | | Time |-(1)-|--(2)--|-(1)-|--(3)--|-(4)-|-(1)-| |__________________ DIR ________________________________/ There are two output pins, STEP and DIR. Step pulses appear on STEP. A positive frequency command results in DIR low, negative frequency command means DIR high. The minimum period of the step pulses is 'steplen' + 'stepspace', and the frequency command is clamped to avoid exceeding these limits. 'steplen' and 'stepspace' must both be non-zero. 'dirsetup' or 'dirhold' may be zero, but their sum must be non-zero, to ensure non-zero low time between the last up step and the first down step. Type 1: Up/Down _____ _____ UP __/ \_____/ \________________________________ | | | | | Time |-(1)-|-(2)-|-(1)-|---(5)---|-(1)-|-(2)-|-(1)-| |_____| |_____| DOWN ______________________________/ \_____/ \____ There are two output pins, UP and DOWN. A positive frequency command results in pulses on UP, negative frequency command results in pulses on DOWN. The minimum period of the step pulses is 'steplen' + 'stepspace', and the frequency command is clamped to avoid exceeding these limits. 'steplen', 'stepspace', and 'dirdelay' must all be non-zero. Types 2 and higher: State Patterns STATE |---1---|---2---|---3---|----4----|---3---|---2---| | | | | | | | Time |--(1)--|--(1)--|--(1)--|--(1+5)--|--(1)--|--(1)--| All the remaining stepping types are simply different repeating patterns on two to five output pins. When a step occurs, the output pins change to the next (or previous) pattern in the state listings that follow. The output pins are called 'PhaseA' thru 'PhaseE'. Timing constraints are obeyed as indicated in the drawing above. 'steplen' must be non-zero. 'dirdelay' may be zero. Because stepspace is not used, state based stepping types can run faster than types 0 and 1. Type 2: Quadrature (aka Gray/Grey code) State Phase A Phase B 0 1 0 1 1 1 2 0 1 3 0 0 0 1 0 Type 3: Three Wire State Phase A Phase B Phase C 0 1 0 0 1 0 1 0 2 0 0 1 0 1 0 0 Type 4: Three Wire HalfStep State Phase A Phase B Phase C 0 1 0 0 1 1 1 0 2 0 1 0 3 0 1 1 4 0 0 1 5 1 0 1 0 1 0 0 Type 5: Unipolar Full Step (one winding on) State Phase A Phase B Phase C Phase D 0 1 0 0 0 1 0 1 0 0 2 0 0 1 0 3 0 0 0 1 0 1 0 0 0 Type 6: Unipolar Full Step (two windings on) State Phase A Phase B Phase C Phase D 0 1 1 0 0 1 0 1 1 0 2 0 0 1 1 3 1 0 0 1 0 1 1 0 0 Type 7: Bipolar Full Step (one winding on) State Phase A Phase B Phase C Phase D 0 1 0 0 0 1 1 1 1 0 2 0 1 1 1 3 0 0 0 1 0 1 0 0 0 Type 8: Bipolar Full Step (two windings on) State Phase A Phase B Phase C Phase D 0 1 0 1 0 1 0 1 1 0 2 0 1 0 1 3 1 0 0 1 0 1 0 1 0 Type 9: Unipolar Half Step State Phase A Phase B Phase C Phase D 0 1 0 0 0 1 1 1 0 0 2 0 1 0 0 3 0 1 1 0 4 0 0 1 0 5 0 0 1 1 6 0 0 0 1 7 1 0 0 1 0 1 0 0 0 Type 10: Bipolar Half Step State Phase A Phase B Phase C Phase D 0 1 0 0 0 1 1 0 1 0 2 1 1 1 0 3 0 1 1 0 4 0 1 1 1 5 0 1 0 1 6 0 0 0 1 7 1 0 0 1 0 1 0 0 0 Type 11: Five Wire Unipolar State Phase A Phase B Phase C Phase D Phase E 0 1 0 0 0 0 1 0 1 0 0 0 2 0 0 1 0 0 3 0 0 0 1 0 4 0 0 0 0 1 0 1 0 0 0 0 Type 12: Five Wire Wave State Phase A Phase B Phase C Phase D Phase E 0 1 1 0 0 0 1 0 1 1 0 0 2 0 0 1 1 0 3 0 0 0 1 1 4 1 0 0 0 1 0 1 1 0 0 0 Type 13: Five Wire Unipolar HalfStep State Phase A Phase B Phase C Phase D Phase E 0 1 0 0 0 0 1 1 1 0 0 0 2 0 1 0 0 0 3 0 1 1 0 0 4 0 0 1 0 0 5 0 0 1 1 0 6 0 0 0 1 0 7 0 0 0 1 1 8 0 0 0 0 1 9 1 0 0 0 1 0 1 0 0 0 0 Type 14: Five Wire Wave HalfStep State Phase A Phase B Phase C Phase D Phase E 0 1 1 0 0 0 1 1 1 1 0 0 2 0 1 1 0 0 3 0 1 1 1 0 4 0 0 1 1 0 5 0 0 1 1 1 6 0 0 0 1 1 7 1 0 0 1 1 8 1 0 0 0 1 9 1 1 0 0 1 0 1 1 0 0 0*//** 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 */#include <float.h>#include "rtapi_math.h"#define MAX_CHAN 8/* module information */MODULE_AUTHOR("John Kasunich");MODULE_DESCRIPTION("Step Pulse Generator for EMC HAL");MODULE_LICENSE("GPL");int step_type[MAX_CHAN] = { -1, -1, -1, -1, -1, -1, -1, -1 };RTAPI_MP_ARRAY_INT(step_type,MAX_CHAN,"stepping types for up to 8 channels");char *ctrl_type[MAX_CHAN] = { "p", "p", "p", "p", "p", "p", "p", "p" };RTAPI_MP_ARRAY_STRING(ctrl_type,MAX_CHAN,"control type (pos or vel) for up to 8 channels");/************************************************************************ STRUCTURES AND GLOBAL VARIABLES *************************************************************************//** This structure contains the runtime data for a single generator. *//* structure members are ordered to optimize caching for makepulses, which runs in the fastest thread */typedef struct { /* stuff that is both read and written by makepulses */ unsigned int timer1; /* times out when step pulse should end */ unsigned int timer2; /* times out when safe to change dir */ unsigned int timer3; /* times out when safe to step in new dir */ int hold_dds; /* prevents accumulator from updating */ long addval; /* actual frequency generator add value */ volatile long long accum; /* frequency generator accumulator */ hal_s32_t rawcount; /* param: position feedback in counts */ int curr_dir; /* current direction */ int state; /* current position in state table */ /* stuff that is read but not written by makepulses */ hal_bit_t *enable; /* pin for enable stepgen */ long target_addval; /* desired freq generator add value */ long deltalim; /* max allowed change per period */ hal_u32_t step_len; /* parameter: step pulse length */ hal_u32_t dir_hold_dly; /* param: direction hold time or delay */ hal_u32_t dir_setup; /* param: direction setup time */ int step_type; /* stepping type - see list above */ int cycle_max; /* cycle length for step types 2 and up */ int num_phases; /* number of phases for types 2 and up */ hal_bit_t *phase[5]; /* pins for output signals */ const unsigned char *lut; /* pointer to state lookup table */ /* stuff that is not accessed by makepulses */ int pos_mode; /* 1 = position mode, 0 = velocity mode */ hal_u32_t step_space; /* parameter: min step pulse spacing */ double old_pos_cmd; /* previous position command (counts) */ hal_s32_t *count; /* pin: captured feedback in counts */ hal_float_t pos_scale; /* param: steps per position unit */ float old_scale; /* stored scale value */ double scale_recip; /* reciprocal value used for scaling */ hal_float_t *vel_cmd; /* pin: velocity command (pos units/sec) */ hal_float_t *pos_cmd; /* pin: position command (position units) */ hal_float_t *pos_fb; /* pin: position feedback (position units) */ hal_float_t freq; /* param: frequency command */ hal_float_t maxvel; /* param: max velocity, (pos units/sec) */ hal_float_t maxaccel; /* param: max accel (pos units/sec^2) */ hal_u32_t old_step_len; /* used to detect parameter changes */ hal_u32_t old_step_space; hal_u32_t old_dir_hold_dly; hal_u32_t old_dir_setup; int printed_error; /* flag to avoid repeated printing */} stepgen_t;/* ptr to array of stepgen_t structs in shared memory, 1 per channel */static stepgen_t *stepgen_array;/* lookup tables for stepping types 2 and higher - phase A is the LSB */static const unsigned char master_lut[][10] = { {1, 3, 2, 0, 0, 0, 0, 0, 0, 0}, /* type 2: Quadrature */ {1, 2, 4, 0, 0, 0, 0, 0, 0, 0}, /* type 3: Three Wire */ {1, 3, 2, 6, 4, 5, 0, 0, 0, 0}, /* type 4: Three Wire Half Step */ {1, 2, 4, 8, 0, 0, 0, 0, 0, 0}, /* 5: Unipolar Full Step 1 */ {3, 6, 12, 9, 0, 0, 0, 0, 0, 0}, /* 6: Unipoler Full Step 2 */ {1, 7, 14, 8, 0, 0, 0, 0, 0, 0}, /* 7: Bipolar Full Step 1 */ {5, 6, 10, 9, 0, 0, 0, 0, 0, 0}, /* 8: Bipoler Full Step 2 */ {1, 3, 2, 6, 4, 12, 8, 9, 0, 0}, /* 9: Unipolar Half Step */ {1, 5, 7, 6, 14, 10, 8, 9, 0, 0}, /* 10: Bipolar Half Step */ {1, 2, 4, 8, 16, 0, 0, 0, 0, 0}, /* 11: Five Wire Unipolar */ {3, 6, 12, 24, 17, 0, 0, 0, 0, 0}, /* 12: Five Wire Wave */ {1, 3, 2, 6, 4, 12, 8, 24, 16, 17}, /* 13: Five Wire Uni Half */ {3, 7, 6, 14, 12, 28, 24, 25, 17, 19} /* 14: Five Wire Wave Half */};static const unsigned char cycle_len_lut[] =
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -