Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   Programming (http://www.chiefdelphi.com/forums/forumdisplay.php?f=51)
-   -   Last minute autonomous programming! (http://www.chiefdelphi.com/forums/showthread.php?t=65737)

ZP8892 11-03-2008 19:43

Last minute autonomous programming!
 
We need help using a counting variable for programming autonomous. The counter should be incrementing but its not. It sorta looks like this:

unsigned counter = 0;

counter++;

printf ( "counter value: ", counter);

Any suggestions would be very much appreciated!

tdlrali 11-03-2008 19:45

Re: Last minute autonomous programming!
 
your variable declaration is executed every loop, meaning it will be initialized to 0 every loop. declare it as a static unsigned, and your counter will work.

scottanderson 11-03-2008 19:59

Re: Last minute autonomous programming!
 
Actual code would help...

I see several problems with what you posted:

No type on counter:
unsigned int counter = 0;

No spec in the printf to actually print the value:
printf( "counter value: %d\n", (int) counter);

As was pointed out, is this all in the same loop?

slavik262 12-03-2008 01:17

Re: Last minute autonomous programming!
 
If it is all within the loop, to prevent the counter from being reset every iteration of the loop, declare it in the following manner:

Code:

static unsigned char counter;

Guy Davidson 12-03-2008 01:20

Re: Last minute autonomous programming!
 
I'd referain from making it a char. A char will overflow after 256 cycles. Running around 26.2hz, the main loop will make 256 cycles in around 9.8 seconds. After that, it will drop to zero and start rising again. I'd suggest you make it

Code:

static unsigned int counter;

slavik262 12-03-2008 01:21

Re: Last minute autonomous programming!
 
True. It depends how fast this loop is running. If it is running constantly, then a larger type should be used.

Loubear 16-03-2008 21:04

Re: Last minute autonomous programming!
 
Autonomous only lasts 15 seconds. And teleop is 2 minutes. With about only 39 iterations per second, an unsigned int is more than enough to hold the tick counter.

Qbranch 16-03-2008 22:43

Re: Last minute autonomous programming!
 
Quote:

Originally Posted by sumadin (Post 716942)
I'd referain from making it a char. A char will overflow after 256 cycles. Running around 26.2hz, the main loop will make 256 cycles in around 9.8 seconds. After that, it will drop to zero and start rising again. I'd suggest you make it

Code:

static unsigned int counter;

26.2ms/packet is actually about 38.17Hz.

Also, I believe this variable would overflow in about 6.7 seconds. I suggest you blow the extra byte of memory and go for an unsigned integer just to cover everything, as this won't overflow until after a whole 28 minutes at the 26.2ms rate.

I suggest a switch statement to run your time based autonomous.

If you are going to Cleveland, i'd be happy to give you some 'pointers'. :]

-q


All times are GMT -5. The time now is 01:00.

Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi