Homing an encoder-equipped stepper motor
to a hard stop
System designers often want to home an axis to a hard stop to avoid the additional complexity and expense of a home sensor. The usual problem with this approach is that the stepper motor produces torque based on its position within its electrical and mechanical cycles. When it stalls, those cycles cause the motor to move back and forth against the stop. When motion is stopped, the motor may come to rest some distance from the stop. This distance is can be random. If this position were used as a home position, poor accuracy would result.
The following sample code uses an iterative approach to try to capture the position closest to home that the motor reaches as it repeatedly hits the hard stop. Since the capture loop in the program runs quickly and asynchronously to the motor’s motion, the lowest value can be captured. This can be tested by stopping the motor, setting the hold current, HC, to zero, and moving the axis manually to the stop and checking the position register (PR P). This is the value we are trying to find. Once we find that value, we move a fixed distance from that value using an absolute move to that value +/- a set amount. We do this because we cannot move to the exact position of the hard stop. Once at this position we either set the position to zero (P=0), or to some offset value X (P=X).
'Test routine for homing to a hard stop. 'Intended for sample purposes only 'The concept is to drive continually toward the hard stop while 'continually testing C2 for A lower value than previously 'found. That value, (not the final rest position), is 'the position of the hard stop. Then move A set distance from 'that position (since moving to the hard stop is impossible). 'Note: To change direction: 'Change sign on both slews 'Change BR G3, C2>=R1 to BR G3, C2<=R1 'Change MA R1-50 to MA R1+50 'Adjust parameter values to suit your particular systems needs. Ee=1 'Enable encoder mode (Best done early in code) Sm=1 'Set stall mode to keep running on a stall Rc=20 'use a good low value for run current Sf=22 'Set the stall factor PG 1 'Enter program mode R1=0 'Initialize R1 to zero R2=0 'Initialize R2 to zero R3=0 'Initialize R3 to zero LB G1 'Home to stop routine St=0 'Clear the stall flag Er=0 'Clear the error flag SL -300 'First find the stop at a good speed St=0 'Clear stall flag LB G2 'First loop PR St," Waiting for 1st Stall" 'Print status BR G2,St=0 'Repeat the first loop if no stall yet SL 0 'Slew to zero speed (stop) Er=0 'Clear the error flag SL -50 'Drive toward the stop slowly LB G3 'Loop start point R2=R2+1 'Increment counter PR St," 2nd Stall: Loop Count= ",R2 'Print status BR G4, R2>=30 'Exit the loop on count of 30 BR G3, C2>=R1 'Test for new lowest value R1=C2 'Store the lowest value found PR "New MAX/MIN reached R1=",R1 'Print status BR G3 'Repeat the loop LB G4 'Loop exit point SL 0 'Slew to zero speed (stop) St=0 'Clear the stall flag H 'Wait until motion completes PR "Last minimum R1=",R1 'Print status MA R1 + 50 'Move to a fixed offset from minimum H 'Wait until motion completes P 0 'Set postion to zero PR "At Home, 50 Counts from stop P=",P E 'End program PG 'Exit program mode