Shop OBEX P1 Docs P2 Docs Learn Events
The math of making a motor spin — Parallax Forums

The math of making a motor spin

AcadianAcadian Posts: 16
edited 2004-11-11 22:19 in Learn with BlocklyProp
I was just wondering if someone could explain to me the math of making a stepper motor spin.

I am looking at the stepper motor example that you have on your BS2 examples.

This line makes the stepper go forward

stpindex = stpindex + 1 // 4 ' point to next step
READ (Step1 + stpindex), coils

, but this line makes it go in reverse

stpindex = stpindex + 3 // 4 ' point to previous step
READ (Step1 + stpindex), Coils

I was just wondering how adding 3 and dividing by 4 makes the motor spin in reverse and adding 1 and dividing by 4 makes it go forward?


Step1··········· DATA··· %0101
Step2··········· DATA··· %0110
Step3··········· DATA··· %1010
Step4··········· DATA··· %1001

How does the read command increment to Step2 then Step3, then Step4
stpindex = stpindex + 1 // 4 ' point to next step
READ (Step1 + stpindex), Coils
We are 3 guys who just can't get our heads around how this is being acomplished.


Thanks

Post Edited (Acadian) : 11/11/2004 7:40:38 PM GMT

Comments

  • Jon WilliamsJon Williams Posts: 6,491
    edited 2004-11-11 21:43
    Keep in mind that the coil data is coming form an EEPROM table, so what each of those subroutines wants to do is point to the next step, or the previous if going in reverse.· Since there are only four steps in the sequence, the valid index values for the steps are 0 - 3.·

    When we do this:

    · stpIdx = stpIdx + 1 // 4

    It is the same thing, albeit more concise, as doing this:

    · stpIdx = stpIdx + 1
    · IF (stpIdx = 4) THEN stpIdx = 0

    The first line works because the modulus operator (//) returns the remainder of a division.· As 4 / 4 is 1 with no remainder, when the value of stpIdx reaches 4 the modulus operator gives us a quick way of moving it to zero.

    Going the other direction works the same, but is a little less obvious.· When going forward we added one to our current index; so when going backward we want to subract one from our current index.· But ... we can't simply do that because PBASIC represents negative values in 2's-compliment form.· So what we do, then, is add one less than the total loop value.· What we want is 3, 2, 1, 0, 3, 2, 1 ...

    Here's how we get there using the modulus operator:

    3 + 3 = 6, 6 // 4 = 2
    2 + 3 = 5, 5 // 4 = 1
    1 + 3 = 4, 4 // 4 = 0
    0 + 3 = 3, 3 // 4 = 3

    If we didn't have the modulus operator, we could do this using PBASIC 2.5:

    · IF (stpIdx·> 0) THEN
    ··· stpIdx = stpIdx - 1
    · ELSE
    ··· stpIdx = 3
    · ENDIF

    I hope this helps.


    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
    Dallas Office
  • AcadianAcadian Posts: 16
    edited 2004-11-11 22:19
    Thanks Jon that makes a little more sense.
Sign In or Register to comment.