Home > Mobile >  Nested loop provides unwanted action
Nested loop provides unwanted action

Time:11-09

I am trying make a nested loop so a while loop gets repeated n times. An object is supposed to drive a distance in i steps for n repetitions. Axis and speed are irrelevant for the loop. What I don't understand is why the first i in i = i 1 is not used, since just i 1 doesn't seem to do the job either.

def steptofull(reps, steps, speed, axis, distance):
    i = 0
    n = 0
    strecke = distance
    # rdistance = 0
    while i < reps:
        while n < steps:
            distance = strecke // steps  # division ohne rest

            # mod = strecke % steps # rest der division, wird evtl addiert als fehler/rest
            fahren(speed, axis, distance)
            time.sleep(1)
            # rdistance = rdistance   rmoved
            flushbuffer()
            n = n   1
            print(n)
        distance = strecke
        fahren(speed, axis, -distance)
        time.sleep(1)
    i = i   1
    

My current output is:

50000625 nm Gefahren
1
50000625 nm Gefahren
2
-99999375 nm Gefahren
-9768750 nm Gefahren


Whereas Gefahren equals the distance driven and is returned by the fahren() function. So the inner while loop works just as intended since the distance is divided into steps and whichi are driven correctly, but the outer while loop doesn't seem to work as it's just trying to drive the whole way back again and again.

I'm using a stepmotor and I'm trying to divide a distance it has to drive into steps and let it drive those steps until the original distance is reached and then let it drive the whole distance back without those steps. I added the outer while loop to add repetitions since I want to to this process i times to measure inaccuracy of the step motor.

CodePudding user response:

Your i = i 1 needs to be indented to run within the while loop. That way, i will increase until it exceeds reps and your outer while loop will stop.

  • Related