Shop OBEX P1 Docs P2 Docs Learn Events
Arduino Command - millis(); — Parallax Forums

Arduino Command - millis();

John BoardJohn Board Posts: 371
edited 2012-05-14 19:49 in Propeller 1
Hi everyone,

I've just been doing quite a bit of exciting stuff with my robot - the robot is now looking like it might be able to get finished on schedule :P It's following lines well using the QTI Sensors. So, now it comes to my problem, I've been trying to figure out what things I should have turned on while my robot is follow a like - i.e. should I have info debugging to the LCD? These are a few questions that are pretty much directly related to the speed of the code - if my LCD takes too long to display things while running the line following, my robot will skip over the line because the motors couldn't update fast enough. So, basically, I need a way of finding out how long each main loop cycle took. I was thinking along the lines of this:

(Psudo Code)
PUB Main | oldtime, newtime

repeat
  newtime := cnt
  LCD.Dec(newtime-oldtime)
  oldtime := cnt


Not perfect.. but you get the picture. Only problem is, I'm not sure if that's quite how I should do it, and also that the result will probably not be in milliseconds. Also, I have no idea how to convert the result into MS. Also the cnt seems to be all over the place :blank: Any help is appreciated!

P.S. This is kind of based of the arduino's "Millis" function: http://arduino.cc/en/Reference/Millis

-John

Comments

  • JonnyMacJonnyMac Posts: 9,195
    edited 2012-05-13 18:48
    I use the counter to time code quite frequently -- here's a shell you can use:
    pub main | t
    
      term.start(RX1, TX1, %0000, 115_200)
      waitcnt((clkfreq >> 9) + cnt)
    
      ' ------------------------------------------------------------
      
      t := -cnt
    
      ' test code here
    
      t += cnt - 544
    
      ' ------------------------------------------------------------
    
      term.dec(t)
      term.tx(CR)
      term.dec(t / (clkfreq / 1_000))
      term.tx(CR)
    
      repeat
        waitcnt(0)
    


    This displays the code duration in ticks and in milliseconds. If you get 0 milliseconds then divide clkfreq by 1_000_000 to get microseconds.

    I tend to start a new cog just for spitting debug info to a screen or LCD so that the time required to do that does not interfere with other processes. It's quite easy to do.
  • John BoardJohn Board Posts: 371
    edited 2012-05-13 19:30
    Thanks - It's not perfect, but it's good enough - I can make something usable out of it ;)

    -John
  • John BoardJohn Board Posts: 371
    edited 2012-05-13 19:56
    This is what I have, it doesn't seem to work - probably just my code... but....

    pub main | t 
    
    t := cnt 
    
    waitcnt(clkfreq/10+cnt) 'Waits 100ms - code should return 100ms 
    
    LCD.dec(cnt-t / (clkfreq / 1_000)) 
    
    repeat
    
    
  • kuronekokuroneko Posts: 3,623
    edited 2012-05-13 20:29
    pub main | t 
    
    t := cnt 
    
    waitcnt(clkfreq/10+cnt) 'Waits 100ms - code should return 100ms 
    
    LCD.dec([COLOR="#FF0000"]([/COLOR]cnt-t[COLOR="#FF0000"])[/COLOR] / (clkfreq / 1_000)) 
    
    repeat
    
    
  • JonnyMacJonnyMac Posts: 9,195
    edited 2012-05-13 20:44
    How in the world is it not perfect? -- it gives you timing to the system tick for Pete's sake!

    What you fail to understand is that the setup for waitcnt takes time. What you also fail to understand is that putting the timing calculation inside your reporting statement is also artificially adding time. The -544 section in my code snippet actually corrects for the setup of the Spin statements in front of and just after the test code; with no test code the program will report 0 ticks.

    It's not perfect because you fouled it up! ;)
  • John BoardJohn Board Posts: 371
    edited 2012-05-14 17:07
    Hi,

    Sorry, I did put it a bit odly, and I can see how you would get anoyed at my remark - what I meant is, it is good code, but I need to tweak it slightly to put it into my robot - sorry for putting it like that. Well anyway, I'll put it in the robot today, and it should be a good adition to it ;)

    Sorry again,

    -John
  • JonnyMacJonnyMac Posts: 9,195
    edited 2012-05-14 18:07
    You didn't state how you want to use the code -- with much detail, anyway -- just that you wanted to be able to time code fragments. Here's another way to do it that may be more flexible and allows you to have multiple timers (long variable) running at the same time. Note that the limit is going to be about 26.8 seconds at 80MHz (POSX / 80_000_000).
    pub main | t
    
      term.start(RX1, TX1, %0000, 115_200)
      pause(1)
    
      ' ------------------------------------------------------------
    
      start_timer(@t)
    
      ' test code here
     
      stop_timer(@t)
    
      ' ------------------------------------------------------------
    
      term.tx(CLS)
      term.dec(t)
    
      repeat
        waitcnt(0)
    
    
    pub start_timer(pntr)
    
      long[pntr] := -cnt
    
    
    pub stop_timer(pntr)
    
    '' Stops timer and returns corrected value in system ticks
    
      long[pntr] += cnt - 1520
    
      return long[pntr]
    
  • John BoardJohn Board Posts: 371
    edited 2012-05-14 19:49
    Hi,

    Thanks! I got it working! Sorry about me mixing my words up a bit earlier - this code will be perfect for what I need ;)

    -John
Sign In or Register to comment.