Shop OBEX P1 Docs P2 Docs Learn Events
help understanding stepper motor code — Parallax Forums

help understanding stepper motor code

texastigertexastiger Posts: 5
edited 2009-03-10 15:02 in BASIC Stamp
I am new to using the basic stamp and I am still learning the language. I downloaded and ran the stepper motor code for a BS2. It works fine but I would like to understand it fully. I have attached the edited code showing my comments in the code blocks that baffle me. Any help in understanding this would be greatly appreciated.

Comments

  • Lee HarkerLee Harker Posts: 104
    edited 2009-03-09 17:36
    I'll take a stab at it.

    Step_Fwd:
      stpIdx = stpIdx + 1 // 4
    
    Step_Rev:
      stpIdx = stpIdx + 3 // 4
    
    


    This part of the code the math that is being done either counts from 0 to 3 or 3 to 0 one value at a time. I don't know how the author came to this use but it is an elegant approach.

    Do_Step:
      READ (Steps + stpIdx), Phase
    
    



    Here the step index is used to pick the correct phase combination from the list to move the motor.
  • texastigertexastiger Posts: 5
    edited 2009-03-09 17:58
    Lee

    Ok I understand that stpIdx is used as an index therefore it will increment. What I dont know is:

    Why is stpIdx not initialized? I am assuming that each time the program is run stpIdx has no starting value. Seems like bad programming form. I was always instructed to initialize a variable if you want consistent results. Of course I am assuming that an unitialized variable in PBasic has no value?

    If 1//4 always return 1 and stpIdx has a starting value of zero then stpIdx will have the following values: 1 , 2, 3, 4

    If 3//4 always returns 3 and stpIdx has a starting value of zero then stpIdx will have the following values: 3,6,9,12

    What am I missing?

    The next area I am confused about is in the Do_Step block. The Read statement has (Steps + stpIdx), Phase

    What is the result of (Steps + stpIdx)? I know that Steps is a vector with 4 elements and I am assuming that stpIdx is a number so what is the result of the operation within the parenthesis?

    Also what is the role of Phase? I know it is a pointer to a block of memory.

    I would really appreciate an explanation. I am trying to learn but these seemingly simple parts of the program have me stumped.

    thanks
    Douglas
  • Lee HarkerLee Harker Posts: 104
    edited 2009-03-09 23:08
    You are missing part of the math. Without parenthesis, the order is left to right.
    stpIdx = stpIdx + 1 // 4
    
    


    If stpidx is at 0, you add 1 which makes 1 and then you get the remainder of a division by 4 which is 1. You can go throught the rest one by one.

    In this fragment,
    READ (Steps + stpIdx), Phase
    
    


    the value of 'steps' is the address where the data list resides. You add to that the 'stpidx' and you are pointing at the motor coil combination.
    That value is read from the list and put into the variable 'phase' which is the 4 output pins OUTB.
    As far as initializing the variables, they are all reset when the program starts anyway.
    In actuality, the position of the stepper is irrelevant when the program starts because the program has no way of knowing its position.
    The only exception to that would be if you are using some limit switches or homing position switches and the motor had been in a known position before the power was turned off and it hadn't been moved since.
  • texastigertexastiger Posts: 5
    edited 2009-03-10 13:19
    Lee

    Thanks for the explanation. I feel like a total idiot for failing to follow the correct order of operations on the calculation of stpIdx.

    I tried looking in the documentation to see if I could find an example that used the Read command·in the way it is used in this code but couldnt find an example.·The use of Steps + stpIdx is new syntax to me. ·
  • Lee HarkerLee Harker Posts: 104
    edited 2009-03-10 15:02
    If you take a closer look at the DATA and READ commands I think your confusion will vanish. In each of these commands, the location of the data is the operative term.
Sign In or Register to comment.