Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   C/C++ (http://www.chiefdelphi.com/forums/forumdisplay.php?f=183)
-   -   Wait/Pause function? (http://www.chiefdelphi.com/forums/showthread.php?t=105010)

thecakeisalie 24-03-2012 21:44

Wait/Pause function?
 
While working out some of the kinks in our code, i stumbled upon a problem. That problem being that I could not have the robot wait for a certain period of time before continuing with its code. So now my question is this: Is there any function that can be used to pause the robot for a certain amount of time?
Thanks.

Ether 24-03-2012 21:46

Re: Wait/Pause function?
 
Quote:

Originally Posted by thecakeisalie (Post 1148577)
While working out some of the kinks in our code, i stumbled upon a problem. That problem being that I could not have the robot wait for a certain period of time before continuing with its code. So now my question is this: Is there any function that can be used to pause the robot for a certain amount of time?

When you say "I could not have the robot wait for a certain period of time before continuing with its code" did you mean precisely that, or did you mean when you tried to do it, the robot would stop responding to driver commands while it was waiting?



thecakeisalie 24-03-2012 21:53

Re: Wait/Pause function?
 
I tried to do it with a simple for loop:
for (int x= 0; x< 500; x++)
this was made in autonomous periodic, which I believed would count up one every 20 miliseconds, which should equal around 5 seconds. But this did not work and did not delay the robot at all.

rbmj 24-03-2012 22:04

Re: Wait/Pause function?
 
You can try Wait(double) in Timer.h

And that's just a simple two instruction loop. It won't occupy the processor for long (though it will waste computing power when compared to a yield AFAIK), it won't be reliable (timing could change very easily based on process scheduling), and it may be optimized out by the compiler.

thecakeisalie 24-03-2012 22:09

Re: Wait/Pause function?
 
Quote:

Originally Posted by rbmj (Post 1148589)
You can try Wait(double) in Timer.h

And that's just a simple two instruction loop. It won't occupy the processor for long (though it will waste computing power when compared to a yield AFAIK), it won't be reliable (timing could change very easily based on process scheduling), and it may be optimized out by the compiler.

Thanks, I shall try your suggestion. :)

Jay Meldrum 25-03-2012 12:04

Re: Wait/Pause function?
 
Something like this maybe useful for pausing. This example is directed towards auton, but can be applied in any instance of pausing

Code:

m_autoPeriodicLoops++;
//Make sure in your init you set m_autonomousCase = 1;
switch(m_autonomousCase)
{
case 1:
{
    //Reset auto counter
    m_autoPeriodicLoops = 0;
    m_autonomousCase = 2;
}
case 2:
{
    if(m_autoPeriodicLoops < 50)
    {
        //Wait for 50 loops 50 * ~20ms = ~1 second
        robotDrive->TankDrive(0.0 , 0.0);
    }
    else
    {
        //Robot will drive after the 50 loops have completed
        robotDrive->TankDrive(0.5 , 0.5);
      }
}

This will pause the robot for about one second (this can vary depending on how much other code is running, and what you have your Periodic loop set to refresh at). Default is about 20 ms.

By using this method you can have other things in the background running in your loops but you are not "stopping" the cRio completely by using a Wait function.

Travis Hoffman 25-03-2012 12:14

Re: Wait/Pause function?
 
We use a timer to implement delays. Here is our bread and butter 2-ball front of key autonomous code - this is called from AutonomousPeriodic:

Code:

void XM15::AutoThree1(void)
{
        //printf("3-PT 1 - Front of Key\n");
       
        #define        TH1_READYDWELL        4.0                //Dwell time to permit to move to shot position
        #define TH1_SHOTDELAY        0.5                //Short pause between oktoshoot signal and feeding ball
        #define        TH1_SHOT1DWELL        0.4                //0.5 OK - Feed time for first ball
        #define TH1_SHOT2DWELL        1.0                //Feed time for second ball
        #define        TH1_REGENDWELL        1.5                //Wait for shooter to regain speed = originally 2.0
       
        switch (autonStep)
        {
                case 1: //Select Front Key Three Arm Position & Spin Up Shooter
                {

                        autofrontkeythree_sw = 1;
                       
                        shooterstate = ON;
                       
                        if (delaytimer->Get() < delayset)        //Wait until the timer times past the preset delay
                        {

                                //Waiting...
                               
                        }
                        else
                        {
                                delaytimer->Stop();
                               
                                autotimer->Reset();
                               
                                autonStep = 2;                       
                        }       
                       
                        break;
                }
                case 2: //Wait for arm and shooter to be ready (or watchdog timeout), then fire a ball
                {                               
                       
                        if ( (oktoshoot == 1) || (autotimer->Get() > TH1_READYDWELL) )
                        {       
                                autotimer->Reset();

                                autonStep = 3;
                        }
               
                        break;
                }
                case 3: //Wait for a short moment after arm in position before firing a ball
                {                               
                       
                        if (autotimer->Get() > TH1_SHOTDELAY)
                        {
                                autocagefeed_sw = 1;
                               
                                autotimer->Reset();

                                autonStep = 4;
                        }
               
                        break;
                }                       
                case 4: //Stop firing after timeout
                {
                        if (autotimer->Get() > TH1_SHOT1DWELL)
                        {
                                autocagefeed_sw = 0;
                               
                                autotimer->Reset();
                               
                                autonStep = 5;
                                //autonStep = 7;                //Debug - permits tuning of initial shot timing
                        }
                                               
                        break;
                }
                case 5: //Wait for shooter to regain speed, then fire again
                {                       
                        if (autotimer->Get() > TH1_REGENDWELL)
                        {
                                if (speedok == 1)
                                {

                                        autocagefeed_sw = 1;
                                       
                                        autotimer->Reset();
                                       
                                        autonStep = 6;
                                       
                                }
                       
                        }
                       
                        break;
                }
                case 6: //Turn shooter off after 2nd ball is fired
                {               
                        if (autotimer->Get() > TH1_SHOT2DWELL)
                        {
                                autocagefeed_sw = 0;
                               
                                autotimer->Reset();
                               
                                autonStep = 7;
                        }
                                                               
                        break;
                }       
                case 7: //Program is done - turn off the shooter, place arm in load position
                {
                       
                        shooterstate = OFF;
                       
                        //autofrontkeythree_sw = 0;
                       
                        //autoloadball_sw = 1;
               
                        break;
                }
               
        }
       
}


adein 26-03-2012 10:25

Re: Wait/Pause function?
 
I use a state system and a Timer object inside of an 'iterable' function. The function returns false until the operation is complete. This works well if you use the 'Iterable' robot template that comes with WindRiver.

The state variable 'state_' must be set to kPhase1 before the function is called the first time (make sure you don't keep setting the state to kPhase1 each time through the control loop). The timer will have a resolution equal to the duration of the calling control loop.

Code:

bool TechnoJays::SomeFunction() {
        double time_left = 0.0;
        // Get the timer value since the last event
        double elapsed_time = timer_->Get();

        switch (state_) {
        // Start the timer and move to phase 2
        case kPhase1:
                timer_->Stop();
                timer_->Reset();
                timer_->Start();
                elapsed_time = timer_->Get();
                state_ = kPhase2;
                // Fall through into kPhase2
        // Do nothing until the time has elapsed
        case kPhase2:
                // Calculate time left
                time_left = delay_time_ - elapsed_time;
                // If enough time has passed, continue
                if (time_left <= 0.0) {
                        timer_->Stop();
                        state_ = kPhase3;
                        // Fall through to kPhase3
                } else {
                        break;
                }
        // The time delay is finished, continue on
        case kPhase3:
                // Return true indicating that the function is complete
                state_ = kNotRunning;
                return true;
        default:
                state_ = kNotRunning;
                return true;
        }

        return false;
}


enrique 04-04-2012 13:27

Re: Wait/Pause function?
 
Quote:

Originally Posted by thecakeisalie (Post 1148577)
While working out some of the kinks in our code, i stumbled upon a problem. That problem being that I could not have the robot wait for a certain period of time before continuing with its code. So now my question is this: Is there any function that can be used to pause the robot for a certain amount of time?
Thanks.

Wait(1.0);


All times are GMT -5. The time now is 13:24.

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