Quote:
Originally Posted by Inverted
I've forgotten most of the C that I learned a few years ago, and my mind has been clouded with the InteractiveC commands that we use in my robotics class (stupid LEGO RCX). I know about incrementing timers and doing, for example:
Code:
timer1++;
if (timer1 < 200){
blah blah;
}
My question is does a C command exist that is the equivalent of InteractiveC's "sleep()" function? We want to have a dead reckoning backup and it would be a lot easier for me to train newbies if we could do the following instead of the former, since they already know how to write those type of programs.
Code:
pwm13 = pwm14 = 167;
pwm14 = pwm15 = 87;
sleep(2.0); //drive forward for 2 seconds
pwm07 = 205;
sleep(.5); //raise arm
blah blah
Is that possible or am I just out of luck with doing things the easy way?
|
You have full control over the code that gets installed in the user processor. The good news is that you can do pretty much whatever you want. The bad news is, you can do pretty much whatever you want.
It appears that you are probably wanting to do this for autonomous mode, but the same thing will work for both.
Structure your code so that it is state driven and always exits. Whether in autonomous or teleoperated, setup your statemachine code to run every SPIData receive event (~38Hz).
You can then setup timer's like this:
Code:
if (TimerDrive) { // if timer running
if (!(--TimerDrive)) { // decrement one tick, and when it hits zero
// do whatever you need to do to advance to the next step in your state machine
}
}
if (TimerSomethingElse) { // if timer running
if (!(TimerSomethingElse)) { // decrement one tick, and when it hits zero
// do something else
}
}
If you really do want to impliment a sleep function, and if this is for autonomous, keep in mind how the default autonomous mode software is structured, and make sure you'll exit at the proper time... in other words, you'll probably need to sprinkle tests at each step along the way.
That said, to impliment a sleep function, I'd suggest setting up one of the unused hardware timers to interrupt and reload at rate equal to whatever you need your timer resolution to be, say 100ms.
In the interrupt service routine, in addition to servicing the interrupt, do this:
Code:
if (TimerSleep) {
--TimerSleep;
}
Then your sleep function can be setup like this:
Code:
volatile unsigned char TimerSleep;
void Sleep(unsigned char timer) {
TimerSleep = timer;
while (TimerSleep);
}
If you need more than 8 bits of dynamic range for your timer and move up to a 16 or 32 bit value, then you should disable interrupts, move the TimerSleep variable into a local temp, re-enable interrupts, test the local variable, and loop until it times out.
The protection is necessary because the interrupt routine would update one byte of the variable at a time, and it creates a critical region for your foreground test.