Stepper Motor Control Function Glossary
C language functions for controlling the position, speed, and acceleration of stepper motors
Click on any of the following links to be taken to the function definition:
Call_Step_Manager
C: void Call_Step_Manager ( void )
4th: Call_Step_Manager ( -- )
Action function invoked by interrupt service routine to service steppers; worst case execution time is approximately 120µs/motor on the QCard, or 20 µs/motor on the PDQ Board.
CCW
C: int CCW
4th: CCW ( -- n )
A constant equal to –1 that indicates the counter-clockwise direction; this constant can be passed as a parameter to the motor control functions.
Change_Speed
C: uint Change_Speed(uint target_speed, int motor)
4th: Change_Speed ( target_speed\motor_index -- numsteps )
This is the most versatile speed control function. It ramps up or down to the specified speed in the current direction; and returns the number of steps taken. If full stepping, speeds are expressed as steps per secondc. If half stepping, speeds are expressed as half steps per second.
Clear_Motor_Port
C: void Clear_Motor_Port(int motor)
4th: Clear_Motor_Port (motor_index -- )
De-energizes the specified motor, removing the holding torque. Writes 00 to the output port that controls the specified stepper motor. To stop a motor while retaining holding torque, use E_Stop
. See also Disable_Motor.
CreateRamp
C: uint CreateRamp(uint start_speed,uint end_speed,uint accel,uint ticks_per_sec, RAMP_ELEMENT* start_ramp_addr, uint speeds_per_ramp)
4th: Create.Ramp ( start.speed\end.speed\accel\ticks/sec\starting.ramp.addr\speeds/ramp--#steps.in.ramp)
A low level primitive that writes the specified speed parameters into the specified motor's ramp array, and returns the number of steps in the ramp. If full stepping, speeds are expressed as steps per second and accelerations/decelerations are expressed as steps/sec/sec. If half stepping, speeds are expressed as half steps per second and accelerations/decelerations are expressed as half steps/sec/sec.
CW
C: int CW
4th: CW ( -- n )
A constant equal to +1 that indicates the clockwise direction; this constant can be passed as a parameter to the motor control functions.
Disable_Motor
C: void Disable_Motor(int motor)
4th: Disable_Motor (motor_index -- )
Disables the specified motor by writing 0 to its Motor_State
and calling Clear_Motor_Port
to de-energize the motor. Calling this function boosts processor efficiency if the specified motor is no longer in use.
Disable_Stepper_IRQ
C: void Disable_Stepper_IRQ(void)
4th: Disable_Stepper_IRQ ( -- )
Locally disables the stepper service interrupt (OC3
in the demo program).
Energize_Stepper
C: void Energize_Stepper(int motor)
4th: Energize_Stepper (motor_index -- )
Writes the next step pattern to the specified motor port, thereby applying holding torque. Executing this command may cause slight motor movement.
E_Stop
C: void E_Stop(int motor)
4th: E_Stop (motor_index -- )
Emergency stop stops a motor without ramp-down leaving it energized with holding torque.
From_Speed_To_Stop
C: uint From_Speed_To_Stop(uint starting_speed, int motor)
4th: From_Speed_To_Stop (staring_speed\motor_index -- num_steps )
Ramps the motor from a constant starting speed down to a stopped (0 speed) state, and returns the number of steps in the resulting ramp. Typically used to create a ramp that ramps to zero speed from a steady starting speed using the default deceleration. If full stepping, speeds are expressed as steps per second. If half stepping, speeds are expressed as half steps per second.
Init_Motor_0
C: void Init_Motor_0 (int use_half_steps)
4th: Init_Motor_0 ( use_half_steps -- )
Defined in the demo program. The use_half_steps parameter is TRUE
(nonzero) for half stepping, FALSE
(=0) for full stepping. This routine uses the default steady speed (typically the maximum speed at which no steps are lost), acceleration, and half-versus-full step specification for each motor as defined in the demo program.
Init_Stepper_Status
C: void Init_Stepper_Status(uint jog_speed, uint steady_speed, uint accel, uint decel, xaddr port_xaddr, char* shadow, uchar nonmotor_mask, int half_steps, int motor)
4th: Init_Stepper_Status (jog/start.speed\steady.speed\accel\decel\port_offset\port.modulenum\motor.mask\Half_Steps?\motor# -- )
An initialization utility that writes the specified speed, acceleration, and step pattern information into the status array used by the step manager. If full stepping, speeds are expressed as steps per second and accelerations/decelerations are expressed as steps/sec/sec. If half stepping, speeds are expressed as half steps per second and accelerations/decelerations are expressed as half steps/sec/sec.
Init_Stepper_IRQ
C: void Init_Stepper_IRQ(void)
4th: Init_Stepper_IRQ ( -- )
Attaches and enables the stepper service interrupt (OC3
in the demo program) and globally enables interrupts.
Init_Steppers
C: void Init_Steppers(int use_half_steps)
4th: Init_Steppers ( use_half_steps -- )
Highest level initialization routine in the demo program. Initializes var_ticks_per_sec to equal the value returned by TICKS_PER_SECOND
as required for proper operation, invokes Disable_Stepper_IRQ
, Zero-Stepper_Arrays
, Init_Motor_x
for each motor (x) in use, clears each motor port and then energizes each stepper in use, and then invokes Initi_Stepper_IRQ
which globally enables interrupts.
Jog_Steps
C: void Jog_Steps(int direction, uint numsteps, int motor)
4th: Jog_Steps ( direction\numsteps\motor_index -- )
Assumes that the specified motor is stopped, takes specified number of steps at the default jog/start speed as defined in the demo program. The jog/start speed is chosen to be slow enough to be abruptly jumped to under load from a stopped state without missing any steps.
MAX_STEPPERS
C: int MAX_STEPPERS
4th: MAX_STEPPERS ( -- n )
A constant that specifies the number of stepper motors in use. Must NOT be greater than 4. Setting this constant to one higher than the highest motor index in use (but never greater than 4) optimizes the performance of the stepper code.
MOTOR_AT_FINAL_SPEED
C: int MOTOR_AT_FINAL_SPEED
4th: MOTOR_AT_FINAL_SPEED ( -- n )
A constant (= –1) that specifies the state returned by Motor_State
when the motor has attained the final turning speed specified by the last function.
Motor_State
C: char Motor_State ( int motor )
4th: MAX_STEPPERS ( motor_index -- byte )
Returns a byte that encodes motor state:
Return Value | Motor state |
---|---|
–1 | MOTOR_AT_FINAL_SPEED |
0 | disabled |
1 | in.ramp |
2 | enter.ramp |
3 | exit.at.current.speed |
4 | exit.and.stop |
5 | MOTOR_STOPPED |
This is a read-only parameter for the application program. Note that a motion command (such as Steps_At_Speed
) finishes well before the stepper motor has finished the physical action. Thus the program must wait until a specified state has been reached before giving the next motor command; otherwise the stepper motor’s physical action is likely to be interrupted. In the demo program, the Motor_State
function is called repeatedly until it returns the MOTOR_STOPPED
value, and only then is the next motor command given.
MOTOR_STOPPED
C: int MOTOR_STOPPED
4th: MOTOR_STOPPED ( -- n )
A constant (= 5) that specifies the state returned by Motor_State
when the motor is stopped.
MSEC_PER_TICK
C: int MSEC_PER_TICK
4th: MSEC_PER_TICK ( -- n)
A constant that sets stepper timebase and sets maximum number of steps or half steps per second. The default value is one millisecond per tick, corresponding to a maximum of 1000 steps per second if full stepping, or 500 steps per second if half stepping. The chosen value should divide evenly into 1000.
One_Step
C: void One_Step(int direction, int motor)
4th: One_Step ( direction\motor_index -- )
Assumes motor is stopped, and takes 1 step.
Ramp_To_Speed
C: uint Ramp_To_Speed(int direction, uint target_speed, int motor)
4th: Ramp_To_Speed ( direction\target_speed\motor_index -- numsteps )
Ramps the specified motor from a stopped state to a specified speed, and returns number of steps in ramp.
Ramp_To_Steady
C: uint Ramp_To_Steady(int direction, int motor)
4th: Ramp_To_Steady ( direction \motor_index -- numsteps )
Ramps the specified motor from a stopped state to the steady speed stored in the status array, which equals the DEFAULT_STEADY_SPEED
defined in the demo program. Returns the number of steps in the ramp.
Set_Stepper_Direction
C: void Set_Stepper_Direction(int direction,int motor)
4th: Set_Stepper_Direction ( direction \motor_index -- )
Writes to the specified direction (CW = +1; CCW = -1) to the status array.
Soft_Stop
C: uint Soft_Stop(int motor)
4th: Soft_Stop ( motor_index -- numsteps)
Ramps the motor down to a stopped state, and returns the number of steps in the ramp. This is the most versatile stopping function.
SpeedToDuty
C: uint SpeedToDuty( uint steps_per_sec, uint ticks_per_sec)
4th: Soft_Stop ( steps_per_second\ticks_per_second – duty_cycle)
A low level primitive that converts speed to the duty cycle representation used by the Call_Step_Manager
function.
Start_Ramp
C: void Start_Ramp(int direction, int motor)
4th: Start_Ramp ( direction\motor_index -- )
Starts a ramp that has already been configured.
Step_Count
C: long* Step_Count(void)
4th: Step_Count (-- xaddr )
Fabius/QCard prototype: xaddr Step_Count(void)
Returns a pointer to a 32-bit signed long integer number of half-steps in the status_array; can be read or written by the application program as illustrated in the demo. Clockwise half-steps increase the step count, and counter-clockwise half-steps decrease it.
StepManager
C: void StepManager ( void )
4th: Step.Manager ( direction\motor_index -- )
Core of interrupt service routine to service steppers. This routine is always invoked by means of Call_Step_Manager
; see its glossary entry.
Steps_At_Speed
C: void Steps_At_Speed(int direction, uint numsteps,uint target_speed, int motor)
4th: Steps_At_Speed ( direction\numsteps\target_speed\motor_index -- )
Assumes the specified motor is stopped. Ramps the motor up to the specified speed and back to zero to achieve the specified number of total steps. Returns an error if the specified number of steps is too small.
Steps_At_Steady
C: void Steps_At_Steady(int direction, uint numsteps, int motor)
4th: Steps_At_Steady ( direction\numsteps\motor_index -- )
Assumes the specified motor is stopped. Ramps the motor up to the steady speed stored in the status array (set to DEFAULT_STEADY_SPEED
in the demo program) and back to zero to achieve the specified number of total steps. Returns an error if the specified number of steps is too small.
TCNTS_PER_TICK
C: int TCNTS_PER_TICK
4th: TCNTS_PER_TICK ( -- n)
A constant defined as TCNTS_PER_MSEC * MSEC_PER_TICK
.
TICKS_PER_SECOND
TCNTS_PER_MSEC
C: int TCNTS_PER_MSEC
4th: TCNTS_PER_MSEC ( -- n)
A system-dependent constant that equals 500 on a default QCard, or 625 on a default PDQ Board. See the comments in the demo code for more information.