Electrical machinery = Limit switch , Pbasic = ? HELP
Jeff Boertjes
Posts: 3
I am setting up a stepper motor to rotate another component in porportion to a distance variable input, under a range of·distances the stepper rotates forwards, under·another range of distance values it rotates·reverse. my problem is that the additional component which the stepper motor is rotating has a limited range of rotation. I was wondering if anyone could show me how to design into my program a variable to keep track of the position of the motor ( of its 48 steps ) and implement somekind of Pbasic "limit switch", possibly in some kind of For..next type of solution·so that it will only rotate forwards to a certain step and then be unable to rotate further forward despite the distance variable being at a range that would otherwise make the stepper continue to rotate forwards. and vice versa so that once at the minimum it can no longer continue to step in reverse.
' {$STAMP BS2}
' {$PBASIC 2.5}
DIST VAR Byte·················································· ·'variable used to store DIRRS result
T VAR Byte························································ 'variable used for 30ms warm-up/wait time of DIRRS
OUTPUT 0························································· ·'pin 0 connects to pin 2 (Vin: Output pin of Micro-controller.) of DIRRS
INPUT 1···························································· ·'pin 1 connects to pin 4 (Vout: Input pin of micro-controller.) of DIRRS
Phase VAR OUTB··············································· ·· ' phase control outputs variable, nibble size
idx VAR Byte······················································ ·' loop counter
stpIdx VAR Nib··················································· ·' step pointer
Steps DATA %0011, %0110, %1100, %1001············· 'stepping data directive stored in eeprom
Setup:······························································ ·'
DIRB = %1111····················································· ' makes P4..P7 outputs
start:································································· "
HIGH 0······························································ ·'vin high-low transition
PAUSE 3···························································· ·'waiting for high to be recognized
LOW 0······························································ ·'
FOR T= 1 TO 30················································· ·'begin 30ms wait
PAUSE 1·························································· · 'pauses 1 ms
IF IN1=1 THEN·················································· · 'vout is high, dist data available
ENDIF······························································ ·'no endif, DIRRS continuously monitors dist
NEXT······························································· ·'
SHIFTIN 1,0,2, [noparse][[/noparse]dist\8]········································ 'shift 8 bits into dist via pin p1, dist value=0-255
······································································ ·'clocking with p0, msb first
DEBUG "distance= ", DEC4 dist, HOME····················· 'display dist's 4 most significant digits in DEBUG window, not scrolling
main:
IF (dist <=50 ) THEN GOTO start···························· 'If dist is less then or equal to 50 do nothing, continues monitoring dist
IF (dist· <=175 ) THEN· FOR idx = 1 TO 2················· 'if dist is less then or equal to 175 step forward by 1 step
GOSUB Step_Fwd··············································· ·' rotate clockwise 1 step
NEXT······························································· · '
IF (dist >=176 ) THEN FOR idx = 1 TO 2···················· ·'if dist is greater then or equal to 176 step reverse by 1 step
NEXT······························································ ·· '
GOSUB Step_Rev················································ ·' rotate counter clockwise 1 step
GOTO start······················································· ·'loop program
END·································································· 'no end command, program loops indeffinitley
Step_Fwd:
stpIdx = stpIdx + 1 // 4······································· ·' point to next step by step pointer= step pointer + 1 modulus 4
GOTO Do_Step
Step_Rev:
stpIdx = stpIdx + 3 // 4········································ ·' point to previous step by step pointer= step pointer + 3 modulus 4
GOTO Do_Step
Do_Step:
READ (Steps + stpIdx), Phase································· 'output new phase data based on current data directive in use + StpIdx
RETURN······························································ ·'loop to beginning of program
' {$STAMP BS2}
' {$PBASIC 2.5}
DIST VAR Byte·················································· ·'variable used to store DIRRS result
T VAR Byte························································ 'variable used for 30ms warm-up/wait time of DIRRS
OUTPUT 0························································· ·'pin 0 connects to pin 2 (Vin: Output pin of Micro-controller.) of DIRRS
INPUT 1···························································· ·'pin 1 connects to pin 4 (Vout: Input pin of micro-controller.) of DIRRS
Phase VAR OUTB··············································· ·· ' phase control outputs variable, nibble size
idx VAR Byte······················································ ·' loop counter
stpIdx VAR Nib··················································· ·' step pointer
Steps DATA %0011, %0110, %1100, %1001············· 'stepping data directive stored in eeprom
Setup:······························································ ·'
DIRB = %1111····················································· ' makes P4..P7 outputs
start:································································· "
HIGH 0······························································ ·'vin high-low transition
PAUSE 3···························································· ·'waiting for high to be recognized
LOW 0······························································ ·'
FOR T= 1 TO 30················································· ·'begin 30ms wait
PAUSE 1·························································· · 'pauses 1 ms
IF IN1=1 THEN·················································· · 'vout is high, dist data available
ENDIF······························································ ·'no endif, DIRRS continuously monitors dist
NEXT······························································· ·'
SHIFTIN 1,0,2, [noparse][[/noparse]dist\8]········································ 'shift 8 bits into dist via pin p1, dist value=0-255
······································································ ·'clocking with p0, msb first
DEBUG "distance= ", DEC4 dist, HOME····················· 'display dist's 4 most significant digits in DEBUG window, not scrolling
main:
IF (dist <=50 ) THEN GOTO start···························· 'If dist is less then or equal to 50 do nothing, continues monitoring dist
IF (dist· <=175 ) THEN· FOR idx = 1 TO 2················· 'if dist is less then or equal to 175 step forward by 1 step
GOSUB Step_Fwd··············································· ·' rotate clockwise 1 step
NEXT······························································· · '
IF (dist >=176 ) THEN FOR idx = 1 TO 2···················· ·'if dist is greater then or equal to 176 step reverse by 1 step
NEXT······························································ ·· '
GOSUB Step_Rev················································ ·' rotate counter clockwise 1 step
GOTO start······················································· ·'loop program
END·································································· 'no end command, program loops indeffinitley
Step_Fwd:
stpIdx = stpIdx + 1 // 4······································· ·' point to next step by step pointer= step pointer + 1 modulus 4
GOTO Do_Step
Step_Rev:
stpIdx = stpIdx + 3 // 4········································ ·' point to previous step by step pointer= step pointer + 3 modulus 4
GOTO Do_Step
Do_Step:
READ (Steps + stpIdx), Phase································· 'output new phase data based on current data directive in use + StpIdx
RETURN······························································ ·'loop to beginning of program
Comments
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
- Stephen
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
- Stephen
Motor_Position VAR Byte
Step_Fwd:
Motor_Position = Motor_Position +1
IF Motor_Position =49 THEN Motor_Position=48 : RETURN
stpIdx = stpIdx + 1 // 4········································ ' point to next step by step pointer= step pointer + 1 modulus 4
GOTO Do_Step
Step_Rev:
Motor_Position = Motor_Position -1
IF Motor_Position =255 THEN Motor_Position=0 : RETURN
stpIdx = stpIdx + 3 // 4········································· ' point to previous step by step pointer= step pointer + 3 modulus 4
GOTO Do_Step
a homing switch at one of the extremes would be a nice feature
Jeff T.