Shop OBEX P1 Docs P2 Docs Learn Events
Spin code issue - Repeat loop only repeats once — Parallax Forums

Spin code issue - Repeat loop only repeats once

Ran into a problem where a repeat loop (repeat index from 1 to stepIncrement) should have looped 20 times but it stops after completing the first loop. Its acting like it is hanging up as no debug statements past the initial loop are displayed. Hoping someone else can see what is going on here. The second repeat loop for elapsed time is working correctly. I am using the current version of Spin Tools and no errors are indicated prior to running code.

  debug("-------- start runTest5 ----------")
  'repeat loop to get each x,y,z position along stride path
  'elapsedtix is timer output counting 10msec segments
  time := elapsedtix
  'initialize step movement
  legGaitX[n] := legXActual[n]
  legGaitY[n] := legYActual[n]
  legGaitZ[n] := legZActual[n]
  debug("Leg ", sdec_(n), sdec(bodystride), sdec(legangle[n]), sdec(resolution), sdec(stepincrement))
  debug("Step distance: ", sdec(legXIncrement[n]), sdec(legYIncrement[n]), sdec(legZIncrement[n]))
  debug("Leg start position: ", sdec_(n), sdec(legGaitX[n]), sdec(legGaitY[n]), sdec(legGaitZ[n]))
  debug(" ")

  repeat index from 1 to stepIncrement
    debug("---------- Start timed movement  Step: ", sdec_(index), " ----------")
    'use msec delay to slow down movement and not use motor stop flags
    'add distance to move in x,y,z
    legGaitX[n] := legGaitX[n] + legXIncrement[n]
    legGaitY[n] := legGaitY[n] + legYIncrement[n]
    legGaitZ[n] := legGaitZ[n] + legZIncrement[n]
    debug("Next Position Leg ", sdec_(n), sdec(legGaitX[n]), sdec(legGaitY[n]), sdec(legGaitZ[n]))

    'move leg to new coordinates using IK for motor angles
    fangle, tangle, cangle := moveToCoordinate(n, legGaitX[n],legGaitY[n],legGaitZ[n])

    'extra time to take up backlash on first iteration of loop
    if index == 1
      backlash := 50
    else
      backlash := msecdelay

    debug("Move cycle timer interval: ", sdec_(backlash))

    'timed delay for smoother motor operations
    etime := elapsedtix - time
    repeat while elapsedtix - time < backlash
    time := elapsedtix
    debug("Code timer (msec): ", sdec_(etime))
    debug("------- End Intermediate Step -------")
    debug(" ")

  debug("------- Complete Movement Steps -------")

Debug code for above code

Cog0  -------- start runTest5 ----------
Cog0  Leg 1, bodystride = 200, legangle[n] = 450, resolution = 10, stepincrement = 20
Cog0  Step distance: legXIncrement[n] = 7, legYIncrement[n] = 7, legZIncrement[n] = 0
Cog0  Leg start position: 1, legGaitX[n] = 21, legGaitY[n] = 214, legGaitZ[n] = 634
Cog0   
Cog0  ---------- Start timed movement  Step: 1 ----------
Cog0  Next Position Leg 1, legGaitX[n] = 28, legGaitY[n] = 221, legGaitZ[n] = 634
Cog0  ---------- moveToCoordinate ----------
Cog0  Leg 1 New angle positions: Femur: 899  Tibia: 4  Coxa: 827
Cog0  Transmited command: $,1,899,4,827,1
Cog0  Move cycle timer interval: 50
Cog0  Code timer (msec): 5
Cog0  ------- End Intermediate Step -------
Cog0   

Comments

  • JonnyMacJonnyMac Posts: 9,102

    Is stepIncrement a global that could be modified outside of this code?

  • Tapping the usual sign: You should isolate the problematic code so you can post a full compile-able example. (Though many a time you will find your problem while doing that)

    My money is on the moveToCoordinate call corrupting RAM somewhere.

  • DiverBobDiverBob Posts: 1,108

    @JonnyMac said:
    Is stepIncrement a global that could be modified outside of this code?

    stepIncrement global variable is set in an earlier routine and not modified during this routine. this is the only place I’m using the local variable ‘index’.

    @Wuerfel_21 said:
    Tapping the usual sign: You should isolate the problematic code so you can post a full compile-able example. (Though many a time you will find your problem while doing that)

    My money is on the moveToCoordinate call corrupting RAM somewhere.

    That why I added debug code at the start of the loop, the end of the loop and a debug statement should be shown when the loop completes. What gets me is that the debug statement at the end of the loop executes as expected but index doesn’t go to the next value of 2. The loop isn’t being aborted as the debug statement after the loop isn’t running. It looks like the repeat command is getting corrupted and so the program just hangs up at this point.
    I will look at moveToCoordinate() a bit closer and make sure there isn’t anything in there that could impact the loop.

  • DiverBobDiverBob Posts: 1,108

    Update: I copied an older version of the moveToCoordinate() routine into the code and the repeat loop started to work again. Then I changed one line of code at a time and tested each change to see which code line would cause it to stop working. By the time I was done the replacement code matched the code that doesn't work but the repeat loop is still working.
    So I'm still not sure why caused the repeat loop to stop but at least I can continue working

  • @DiverBob said:
    Update: I copied an older version of the moveToCoordinate() routine into the code and the repeat loop started to work again. Then I changed one line of code at a time and tested each change to see which code line would cause it to stop working. By the time I was done the replacement code matched the code that doesn't work but the repeat loop is still working.
    So I'm still not sure why caused the repeat loop to stop but at least I can continue working

    Gremlins...it was gremlins.

    On a more serious note, I would be tempted to do a file compare between the two routines and see if there is a difference that I missed.

Sign In or Register to comment.