0

What I am trying to do is enter a number into the function move and get movement that is up if the argument is greater than the previous argument or move down if the argument is lower than the previous argument. But as shown in the commented code if starting after moving from .5 to .125 it should move down the difference of .375. What I am getting is the movement always going up and and it's not modifying the variable 'oldSteps'. Can I get some fresh eyes and help me figure out what I'm doing wrong?

def move(n):
    numberEntered = float(n)
    stepPerRev = 400
    TPI = 4
    steps = int(( float( stepPerRev) * float( TPI ) ) * numberEntered)
    oldSteps = 0 # place holder for oldSteps
    if steps > oldSteps:    #turn ccw
        for i in range(steps - oldSteps):
            print i # turning code
        oldSteps -= steps
        print 'did %s steps up' % int(steps - oldSteps)
    if steps < oldSteps:    # turn cw        
        for i in range( oldSteps - steps ):
            print i # turning code
        oldSteps -= steps
        print 'did %s steps down' % int(oldSteps - steps)
    return 0
move(.5)    # move up 1/2 of inch
move(.125)    # move down from 1/2 to 1/8 of inch ( move 3/8 of inch)
1

2 Answers 2

3

Your question isn't really clear. But looking at your code, it seems like you're expecting if steps < oldSteps to be true, when you call move(.125) after move(.5). If that's the case, then that won't happen, because oldSteps is always reset to 0 with each fresh call to move(). Variables defined inside a function do not persist their values across function calls. If you want this behaviour, you'll have to store that state somewhere, ideally outside of the function.

I've modified your code to give you an example of what you could do, but ideally, just avoid the global statement and store your state in an object of some, and either pass that in to move, or make move() a method of that object.

# Define your globals/Constants up top
stepPerRev = 400
TPI = 4
oldSteps = 0

def move(n):
    # Statment to specify you're modifying the /global/ oldSteps in this scope
    global oldSteps

    steps = stepPerRev * TPI * n

    if steps > oldSteps:    #turn ccw
        # Code for counterclockwise
        oldSteps -= steps
        print 'did %s steps up' % int(steps - oldSteps)

    if steps < oldSteps:    # turn cw        
        # Code for clockwise
        oldSteps -= steps
        print 'did %s steps down' % int(oldSteps - steps)

    return 0

# Test calls
move(.5)    # move up 1/2 of inch
move(.125)    # move down from 1/2 to 1/8 of inch ( move 3/8 of inch)

Here's how you'd do it if you were refactoring it to make it cleaner and avoid using the global statement entirely:

class Thing(object):
    def __init__(self):
        # Constants
        self.STEP_PER_REV = 400
        self.TPI = 4

        # state
        self.oldSteps = 0

    # Define other movements/more complex functions
    # ...

    def move(n):

        steps = self.stepPerRev * self.TPI * n

        if steps > self.oldSteps:    #turn ccw
            # Code for counterclockwise
            self.oldSteps -= steps
            print 'did %s steps up' % int(steps - oldSteps)

        if steps < self.oldSteps:    # turn cw        
            # Code for clockwise
            self.oldSteps -= steps
            print 'did %s steps down' % int(oldSteps - steps)


t = Thing()
# Test calls
t.move(.5)    # move up 1/2 of inch
t.move(.125)    # move down from 1/2 to 1/8 of inch ( move 3/8 of inch)
Sign up to request clarification or add additional context in comments.

8 Comments

I've done this and made 'oldSteps' global but I run into the error:
Traceback (most recent call last): File "stepdebug.py", line 24, in <module> move(.5) # move 1/2 of inch File "stepdebug.py", line 11, in move if steps > oldSteps: #turn ccw UnboundLocalError: local variable 'oldSteps' referenced before assignment
That's because global variables are treated differently in python. Simply moving a variable out of scope does make it global, but you have to use a global variable explicitly by using the global keyword.
Thanks. Let me try this. Sorry for the confusion I'm not sure how to define this problem.
Hey I appreciate your help. I'm still having trouble getting it to print 'down'. To me it looks solid. I took a break but i got nothing. If you could take another look i would appreciate it.
|
0
THREADS_PER_INCH = 4
STEPS_PER_REV = 400

Current_steps = 0

def move(n):
    """
    Given an input, n, measured in inches of position from zero, 
    compute the number of steps required to move the {WHAT IS IT?}
    from its current position to the target position. Print the 
    number of clockwise/counterclockwise steps required.
    """
    global Current_steps
    target_inches = float(n)
    target_steps = int(target_inches * THREADS_PER_INCH * STEPS_PER_REV)

    if target_steps == Current_steps:
        print("Already here! Nothing to do...")
    elif target_steps > Current_steps:
        print("Turn CCW, {} steps".format(target_steps - Current_steps))
    else: # target_steps < Current_steps
        print("Turn CW, {} steps".format(Current_steps - target_steps))

    Current_steps = target_steps

move(.5)    # move up 1/2 of inch
move(.125)  

3 Comments

In your code it prints CCW both times. It should have went one way then the other. I know what I was asking was not clear but I'm still having trouble getting it to go down or CW. Still have time to work on this with me?
My bad, forgot to update Current_steps. See edit (added last line of move).
It seems so obvious now. I will try that when I get off work. Thanks .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.