|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
| Thread Tools | Rate Thread | Display Modes |
|
#1
|
||||
|
||||
|
Is there a built in tick/millisecond counter?
My first question is, are there any counters built into the main unit?
If there is, how would i access it? |
|
#2
|
||||
|
||||
|
Re: Is there a built in tick/millisecond counter?
Quote:
|
|
#3
|
||||
|
||||
|
Re: Is there a built in tick/millisecond counter?
Quote:
Code:
/******************************************************************************* * * TITLE: clock.c * * VERSION: 0.3 (Beta) * * DATE: 18-Dec-2003 * * AUTHOR: R. Kevin Watson * kevinw@jpl.nasa.gov * * COMMENTS: This demonstrates the use of a timer and associated interrupt. * In this example we'll setup a timer to generate an interrupt * every millisecond (= 1000Hz rate). In response to this interrupt, * the microcontroller will execute a specific piece of code called * an interrupt handler (see user_routines_fast.c). This interrupt * handler will simply increment the global variables "msClock" and * "Clock". High-level user code can then access these variables to * create sophisticated state machines and highly accurate sequencers. * We'll just keep the time. * * ******************************************************************************** * * Change log: * * DATE REV DESCRIPTION * ----------- --- ---------------------------------------------------------- * 14-Dec-2003 0.1 RKW Original * 16-Dec-2003 0.2 Accuracy bug: Changed the value of PR2 to 249 (was 250) * 18-Dec-2003 0.3 Fixed 25 hour/day and 366 day/year bugs * ******************************************************************************* -Kevin |
|
#4
|
|||
|
|||
|
Re: Is there a built in tick/millisecond counter?
Quote:
There is a hazard associated with reading a counter value that is maintained by the interrupt handler, but this is easily dealt with. See the section on interrupt programming in "An Introduction to C Programming for FIRST Robotics Applications," you will want the latest version from www.srvhsrobotics.org, in the Technical page. Have fun! |
|
#5
|
||||
|
||||
|
Re: Is there a built in tick/millisecond counter?
Quote:
Another possible problem is when the variable can be modified through some process the compiler doesn't know about. You can inform the compiler of this possibility by using the keyword "volatile" in the variable's declaration. There's also much information about the volatile keyword on the web. -Kevin |
|
#6
|
|||
|
|||
|
Re: Is there a built in tick/millisecond counter?
Quote:
How often you get a corrupt value depends on your usage. If you busy poll the clock value in the "fast loop", you will get a bad value every few seconds. |
|
#7
|
||||
|
||||
|
Re: Is there a built in tick/millisecond counter?
Quote:
Quote:
Quote:
-Kevin |
|
#8
|
|||||
|
|||||
|
Re: Is there a built in tick/millisecond counter?
Isn't there a time mark on the dashboard port data stream?
|
|
#9
|
||||
|
||||
|
Re: Is there a built in tick/millisecond counter?
Quote:
|
|
#10
|
|||
|
|||
|
Re: Is there a built in tick/millisecond counter?
Quote:
#include "printf_lib.h" void Process_Data_From_Local_IO(void) { /* oldmsClock must be saved between calls. */ static unsigned long oldmsClock = 0; unsigned long newmsClock; newmsClock = msClock; /* If we have a little byte trip from the devil that we weren't expecting... */ if(newmsClock < oldmsClock) { printf("Hit: old = %lx, new = %lx\n", oldmsClock, newmsClock); } oldmsClock = newmsClock; } Hit output will be of the form: Hit: old = 0dff new = 0d00 Hit: old = 020ff new = 02000 Hit: old = 02eff new = 02e00 Hit: old = 040ff new = 04000 That the interrupt is splitting the read of the low order byte, and the next one up, is clear. Last edited by eugenebrooks : 02-12-2004 at 22:35. |
|
#11
|
||||
|
||||
|
Re: Is there a built in tick/millisecond counter?
Quote:
1) You're using the variable 'msClock' outside of the interrupt service routine without using the the techniques I described above. I did not do this in my example code. But because you did, the likelyhood of getting corrupted data is one thousand times greater when compared to how I used the 'Clock' variable. 2) The function Display_Time() in my example is only called every seventeen milliseconds, which means the variable 'Clock' is only being accessed around fifty-nine times a second. In addition to what you did above, you've also moved error checking code into the Process_Data_From_Local_IO() loop which executes, oh, one-hundred thousand times a second, which increases the likelyhood of creating an error by an additional factor of seventeen-hundred times. Combined, these changes increase the likelyhood of creating a race condition by a factor of roughly a million. If you're seeing an error every, say, two seconds when you increased the likelyhood of generating one by over a million, is what you've done really meaningful? I wrote that example in the clearest, most concise way I know how to. My goal is to "set the hook" and get folks interested in investigating other cool things their robot controller can do. Introducing the concept of task synchronization is unnecessary and counterproductive for such an example. Quote:
-Kevin |
|
#12
|
|||
|
|||
|
Re: Is there a built in tick/millisecond counter?
Quote:
great contribution to the community, and I refer to it as such. I am not trying to poke holes in it at all... I only pointed out that the hazard exists, and can be run into depending on how, and how often, you read the counter. |
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Multiple Auton's | Joe Clohessy | Programming | 17 | 11-02-2005 18:46 |
| User Activity Counter | JVN | CD Forum Support | 14 | 04-10-2004 14:33 |
| Offloading intterupts to a counter | Max Lobovsky | Control System | 14 | 30-03-2004 21:07 |
| Who built your robot? | Wayne Doenges | General Forum | 101 | 14-02-2003 23:24 |
| Has anyone built a segway? | Moshingkow | Dean Kamen's Inventions | 29 | 27-11-2002 07:26 |