Shop OBEX P1 Docs P2 Docs Learn Events
FOR...NEXT question... — Parallax Forums

FOR...NEXT question...

nightridernightrider Posts: 9
edited 2007-03-13 19:31 in BASIC Stamp
Hello all, someone help me understand this please.· I'm working through Robotic with the Boe-Bot manual and I'm on page 82.· Here is the exerpt that I do not understand.


Here is an example of a FOR…NEXT loop that will make the servo turn for a few seconds:
······· FOR counter = 1 TO 100
········· PULSOUT 13, 850
········· PAUSE 20
······· NEXT
Let’s figure out the exact length of time this code would cause the servo to turn. Each
time through the loop, the PULSOUT command lasts for 1.7 ms, the PAUSE command lasts
for 20 ms, and it takes around 1.3 ms for the loop to execute.
One time throught the loop=1.7ms+20ms+1.3ms = 23.0ms


I don't understand the statements in red.· 1.3ms? what loop is the author talking about? The FOR-NEXT, loop? If that's the case, I don't understand how it can take 1.3ms to execute.
Can someone explain this to me?

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Thank you,


Alex Cordero
San Diego, Ca.

robots: AIBO ERS-220, Robosapien, Roomba, Boe-bot (basic), a bunch of other little robots and toys.

"Fill your boots, man!"
-Irish saying

Comments

  • Bruce BatesBruce Bates Posts: 3,045
    edited 2007-03-13 09:03
    Alex -

    The timing in question regards ONLY the instructions involved specifically with FOR and NEXT, and nothing else in between. NEXT begins the loop process. FOR either ends it, or continues it.

    If you want more PBASIC instruction timing information, here is a resource:
    http://www.emesystems.com/BS2speed.htm

    Regards,

    Bruce Bates

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
  • ZootZoot Posts: 2,227
    edited 2007-03-13 14:10
    What Bruce is referring to is that the instructions themselves actually take time to execute. The Basic Stamp is (relatively) slow, and so the actual time for instructions to execute needs to be taken into account for time sensitive applications (like servo control).

    In other words, if you piled up instructions in your program between pulsouts to the servos, you would quickly exceed the reasonable limit of 20-30ms between pulses.

    In the code above, you will get about 23ms between pulses, which is OK for most servos. The 20ms is a little flexible. If anything, driving the servos too often (<20ms between pulses) is more problematic, but intervals that are too long will underdrive the servos.

    Longer and more complex programs that drive servos often do something like the following to be able to run all the code and still refresh the servos every 20ms or so, e.g.


    - run task 1
    - refresh servo
    - run task 2
    - refresh servo
    - run task 3
    - refresh servo
    - run task 4
    - refresh servo
    - do it all again

    As opposed to:

    - run task 1
    - run task 2
    - run task 3
    - run task 4
    - refresh servo
    - do it all again

    If you want to "see" how slow instructions can be when they are piled up, hook up an LED that is flashed on then turned off instead of pulsing out to servos. With nothing else going on, the LED will blink so fast you can barely see it blink. As you pile instructions in between the blinks, you will start to see it blink slower and sloower and slooooower...

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    When the going gets weird, the weird turn pro. -- HST
  • nightridernightrider Posts: 9
    edited 2007-03-13 19:19
    ok, thanks both of you. So, it looks like that 1.3ms is an estimate (varies)--sort of.·Bruce, I read the information on the link you sent me and I think I understand now.· I'll go through http://www.emesystems.com/BS2speed.htm#timing·a little more tonight to get more accurate information--but I see what you guys are saying.· You feel sort of dumb when you realize something like this...

    "oh yea!·all things happen instantaneously... NOT!"
    -- "I want it NOW!" -- geez...

    *there's so much to learn... [noparse]:([/noparse]



    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Thank you,


    Alex Cordero
    San Diego, Ca.

    robots: AIBO ERS-220, Robosapien, Roomba, Boe-bot (basic), a bunch of other little robots and toys.

    "Fill your boots, man!"
    -Irish saying

    Post Edited (nightrider) : 3/13/2007 7:35:31 PM GMT
  • FranklinFranklin Posts: 4,747
    edited 2007-03-13 19:31
    Bruce had a link in his post, go there read it, you'll probably have more questions to ask then.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - Stephen
Sign In or Register to comment.