Shop OBEX P1 Docs P2 Docs Learn Events
Using an Encoder for Controlling Movement of a Stepper Motor — Parallax Forums

Using an Encoder for Controlling Movement of a Stepper Motor

LucyLucy Posts: 1
edited 2008-12-13 15:24 in General Discussion
I am new to the SX and SX/B, and I need help.· I want to use a 64 PPR quadrature encoder to tell a bipolar stepper motor when to move and in what direction, either clockwise or counterclockwise.· When the encoder stops moving, I want the stepper motor to stop moving.· I know the SX is supposed to be able to keep up, but for some reason, it’s not seeing all of the encoder pulses.
·
I am using a stepper motor driver, so I only send a high or low pulse to its Enable, Step, and Direction pins using the SX.
·
This is my set-up:
Encoder – Phase A:····· PIN RA.1 and pull-up resistor (1k) to VDD
Encoder – Phase B:······ PIN RA.0 and pull-up resistor (1k) to VDD
Driver – Enable:··········· PIN RB.5
Driver – Step:·············· PIN RB.6
Driver – Direction:······· PIN RB.7
·
Does anyone see any problems with the program?· I used the example in the SX/B help file for quadrature input as a starting point.
·
Here is copy of my program.
·
DEVICE········· SX28, OSCHS3, TURBO, STACKX, OPTIONX
FREQ··········· 50_000_000
'

' IO Pins
'

EncPort··································· PIN······ RA
TRIS_Enc······· ··········· ··········· VAR···· TRIS_A
MotorEnable················ PIN······ RB.5··
MotorStep······· ··········· PIN······ RB.6··
MotorDirection····· ······· PIN ······RB.7··
'

' Constants
'

Yes················· CON··· 1
No··················· CON··· 0
'

' Variables
'

encOld········· ··· VAR···· Byte
encNew················· VAR···· Byte
tmpB4························· VAR··· Byte
'

· INTERRUPT
'

ISR_Start:
encNew = EncPort & %00000011········
tmpB4 = encOld XOR encNew
IF tmpB4 > 0 THEN················
··· encOld = encOld << 1
··· encOld = encOld XOR encNew
··· IF encOld.1 = 1 THEN·········
········ INC encVal
··· ELSE········ ·············
········ DEC encVal
··· ENDIF
ENDIF
encOld = encNew
ENDIF
·
ISR_Exit:
· RETURNINT
'=====================================================================
· PROGRAM Start '=====================================================================
·
'

' Program Code
'

Start:
LOW MotorEnable
encNew = EncPort & %00000011
encOld = encNew
Option = $88
·
Main:
IF encOld.1 = 1 THEN
········ HIGH MotorDirection··············· 'clockwise
········ PULSOUT MotorStep, 200
ELSE
········ LOW MotorDirection··················'counterclockwise
········ PULSOUT MotorStep, 200
ENDIF
GOTO Main
END

Comments

  • BeanBean Posts: 8,129
    edited 2008-12-12 17:47
    Try this:

    DEVICE  SX28, OSCHS3, TURBO, STACKX, OPTIONX
    FREQ    50_000_000
     
    ' -------------------------------------------------------------------------
    ' IO Pins
    ' -------------------------------------------------------------------------
    EncPort            PIN       RA    INPUT
    MotorEnable        PIN       RB.5  OUTPUT  
    MotorStep          PIN       RB.6  OUTPUT
    MotorDirection     PIN       RB.7  OUTPUT
     
    ' -------------------------------------------------------------------------
    ' Constants
    ' -------------------------------------------------------------------------
    Yes                CON    1
    No                 CON    0
     
    ' -------------------------------------------------------------------------
    ' Variables
    ' -------------------------------------------------------------------------
    encOld             VAR     BYTE
    encNew             VAR     BYTE
    encVal             VAR     BYTE
    oldEncVal          VAR     BYTE
    tmpB4              VAR     BYTE
     
    ' -------------------------------------------------------------------------
    INTERRUPT 200_000
      encNew = EncPort & %00000011         
      tmpB4 = encOld XOR encNew 
      IF tmpB4 > 0 THEN                 
        encOld = encOld << 1
        encOld = encOld XOR encNew
        IF encOld.1 = 1 THEN          
          INC encVal
          MotorDirection = 1
        ELSE                       
          DEC encVal
          MotorDirection = 0
        ENDIF
      ENDIF
      encOld = encNew
    RETURNINT
     
    '=====================================================================
    PROGRAM Start
    '=====================================================================
     
    ' -------------------------------------------------------------------------
    ' Program Code
    ' -------------------------------------------------------------------------
    Start:
      LOW MotorEnable
      encNew = EncPort & %00000011
      encOld = encNew
      encVal = 128
      oldEncVal = encVal
     
    Main:
      IF encVal <> oldEncVal THEN
        oldEncVal = encVal
        PULSOUT MotorStep, 200
      ENDIF
    GOTO Main
    END
    
    

    Bean


    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

    "The welfare of the people in particular has always been the alibi of tyrants." ~ Camus
    www.iElectronicDesigns.com

    ·
  • RobotWorkshopRobotWorkshop Posts: 2,307
    edited 2008-12-12 18:19
    The SX chips should have NO trouble keeping track of an encoder. If you read SERVO I had an article in the July 2008 issue regarding encoders and I use an SX28 clocked at 20Mhz to keep track of a couple hi-res encoders. You should still be able to get the back issues from T&L publications.

    I've also used an SX48 to watch and encoder and drive a DC motor using the PWM for another project with excellent results.

    The only issue I see that may cause problems with your project is the resolution of your stepper motor in relation to your encoder. If the resolution of them is different then you'll need to account for that in your program. An example would be if the encoder was higher resolution than the stepper and not divisible by the same amount you may get a variation in the number of encoder counts per stepper motor pulse. As long as you are aware of it that may not be a problem.

    Robert
  • Bill ChennaultBill Chennault Posts: 1,198
    edited 2008-12-13 15:24
    Lucy--

    I have little experience with the SX, although I have several.

    However, I use two BS2s to keep track of the encoders on the gear motors which turn the tracks of Ugly Buster. I learned early on that in my configuration when one track falls behind the other it is best to speed the slow one up until it catches or is greater than the other. Thus, it is neat to hear and watch the encoders' BS2s telling the master where they are and having the master send speed instructions to two more BS2s that control two HB-25s which, in turn, drive the gear motors.

    My machine currently has five microcontrollers. I imagine a single SX could easily replace all five.

    --Bill

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    You are what you write.
Sign In or Register to comment.