|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
|
|
Thread Tools | Rate Thread | Display Modes |
|
|
|
#1
|
||||
|
||||
|
Re: Timer Question
That is an excellent idea. Here's how I've been implementing it for the last few years; first the code, then an explination:
First off, I have two macros, all defined in auto_next.h ("DEBUG" is a macro defined elsewhere which will either call printf, or do nothing) Code:
#ifndef __AUTO_NEXT_H__
#define __AUTO_NEXT_H__
#define AUTO_NEXT_STEP(x,name) \
if (auto_counter >= x) \
{ \
DEBUG((name "\r\n")); \
DEBUG(("auto_counter(%d) >= %d ", auto_counter, x)); \
++auto_step; \
DEBUG(("advancing to state %d\r\n", auto_step)); \
auto_counter = 0; \
}
#define AUTO_NEXT_STEP_COND(x,y,name) \
AUTO_NEXT_STEP(x,name) \
else if (y) \
{ \
DEBUG((name "\r\n")); \
DEBUG((#y)); \
DEBUG((" (%d) ", auto_counter)); \
++auto_step; \
DEBUG(("advancing to state %d\r\n", auto_step)); \
auto_counter = 0; \
}
#endif // __AUTO_NEXT_H__
Code:
// These are used for state in autonomous mode static unsigned int auto_counter = 0; static char auto_step = 0; And finally, in your slow loop autonomous function, do something like this: Code:
void User_Autonomous_Slow_Loop(void)
{
auto_counter++;
switch (auto_step)
{
case 0:
robot_drive(127,192);
AUTO_NEXT_STEP(120, "drive forward");
break;
case 1:
{
bool turn;
turn = robot_turn_to_angle(90);
AUTO_NEXT_STEP_COND(400, turn, "turn robot");
}
break;
case 2:
robot_all_stop();
break;
}
}
The two variables, auto_step and auto_counter keep track of the state. The macros help manipulate them. AUTO_NEXT_STEP two paramaters, a time, and a string which is the name of the current mode. Each time through the slow loop, the auto_counter gets incremented. If it reaches the time paramater of AUTO_NEXT_STEP, the counter gets reset to 0 and the auto_step gets incremented. The advantage to doing it this way over something like a bunch of chained if statements Code:
if (timer < 120)
{
// drive forward
} else if (timer < 400)
{
// turn
}
The AUTO_NEXT_STEP_COND macro is like AUTO_NEXT_STEP, except that it also takes a condition. If you have a function like "robot_turn(90)" which returns a boolean value of TRUE when it is completed and FALSE when it is not, then you can use AUTO_NEXT_STEP_COND to advance to the next state as soon as a condition is met, rather than waiting for a time. This is particularly useful if you have functions to drive a certain distance based on encoder counts, or move an arm to a certain position, etc. Good luck! --AJY |
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Timer questions | Rick TYler | Programming | 3 | 12-02-2006 19:24 |
| Quick Timer Question | et1337 | Programming | 3 | 02-02-2006 15:19 |
| Accelerometer Timer Question | psquared | Programming | 3 | 12-02-2005 01:34 |
| timer | omega | Programming | 3 | 30-03-2004 18:52 |
| Timer Preview | Nate Smith | General Forum | 2 | 05-03-2003 12:25 |