Chief Delphi

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

krudeboy51 05-09-2011 03:52 PM

Wait(); function
 
hi I need a little assistance, is there a way to use something else other than the function "Wait();"? something like a counter?

Kevin Sevcik 05-09-2011 04:02 PM

Re: Wait(); function
 
You can make your own counter by incrementing a variable every time your run through your periodic loop. The timing is usually precise enough for most tasks.

Ether 05-09-2011 04:11 PM

Re: Wait(); function
 
Quote:

Originally Posted by krudeboy51 (Post 1060435)
hi I need a little assistance, is there a way to use something else other than the function "Wait();"? something like a counter?

Is there a specific reason why you want to do this? i.e. Is there a problem you are trying to solve?



krudeboy51 05-09-2011 04:36 PM

Re: Wait(); function
 
Quote:

Originally Posted by Ether (Post 1060444)
Is there a specific reason why you want to do this? i.e. Is there a problem you are trying to solve?


yes!, the problem is that whenever I use wait, we would lose control of our robot until what ever it is doing is finished.

I tried this:

if(Thirdstick.GetTrigger())
{
int timer = 0;
timer++;
motor->Set(1.0);
if(timer >= 1000)
{
motor->Set(0.0);
}
}
but it never worked

TofuRama 05-09-2011 05:17 PM

Re: Wait(); function
 
I looked at your code and there are a few issues with it. One is that the timer will never get past 1. This is because it sets it to 0 within the if statement but also increments it only in the if statement as well. Another issue is that you're only checking to see if the motors should stop in the if statement. You can try this code and see if it should work:

//declare the timer as static to keep the value from the last loop
static int timer = 0;

//check if the trigger was pushed
if(Thirdstick.GetTrigger())
{
//if so, start the motors and reset the timer
timer = 0;
motor->Set(1.0);
}
//increment the timer
timer++;
if(timer >= 1000)
{
//stop the motors
motor->Set(0.0);
}

Also, as for an actual timer there is a timer class. Below is the syntax to use it:

//create a new timer class
Timer *timer = new Timer();
//start the timer
timer->Start();
//reset the timer, call this anytime that you want the timer to be set at zero
timer->Reset();

I hope this helps!

Dave Scheck 05-09-2011 05:24 PM

Re: Wait(); function
 
TofuRama replied while I was composing this, but I'll post everything for completeness. One thing to be careful of with the code that was suggested is that the timer variable can roll over when it hits 2147483647. This is probably larger than you'll ever hit with an FRC robot, but I wanted to point it out so you understood that there could be a potential problem (think about if you did that in an airplane that had its computers running for hours at a time).

Based on your code, I assume you want this behavior

When the trigger is pressed, run the motor for one second then turn off.

There are multiple reasons that your code doesn't work.
1. Your timer variable is reset every time you run this code because your variable declaration is within the IF block. To do what you're trying to do you need some form of persistent memory that holds its value between loops. You can accomplish this with a global variable, a static variable within your function, or a member variable in your class depending on how you want to code it up.

2. Your timer and motor control code only get run when the trigger is pressed.

Here's some quick code that should do what you're looking for. I didn't compile/run/test it, but the concepts are there.
Code:

// Declare some global variables at the top of the cpp file
unsigned int timerCounter = 0;
bool processingTriggerPress = false;

....

// Store the trigger value into a local variable so it can be used multiple times
bool triggerPressed = Thirdstick.GetTrigger();

// Create a motor speed variable and default it to 0 (off)
float motorSpeed = 0.0;

// This checks to see if the button is pressed but we're not
// processing the press yet.  This condition causes the timer
// counter to clear and the processing state to change.  The
// reason that it is needed is to protect against the timer from
// clearing when the trigger is held down.
if(triggerPressed == true &&
  processingTriggerPress == false)
{
    timerCounter = 0;
    processingTriggerPress = true;
}

//Check to see if we're in a processing state
if(processingTriggerPress == true)

    if(timerCounter < 1000)
    {
        // The timer counter is within range, so assign a motor speed
        motorSpeed = 1.0;
    }
    else
    {
        // The timer has expired.  Clear the processing state
        processingTriggerPress = false;
    }
}

// Assign the calculated motor speed to the motor.
motor->Set(motorSpeed);

This code illustrates a few important concepts that I teach to our students every year.

1. You have a giant loop around your code. You can use this to your advantage by setting states and using them in the next loop to make decisions

2. Read your inputs into variables at the top of your code, calculate the output values based on the inputs and store the values in local variables, then write your outputs once at the end. This really makes your life easier and makes your code cleaner. For one, this method protects against inputs changing from read to read within the same loop iteration. Second, as your code gets more and more complex, if you have motor assignments (calls to motor->Set()) strewn throughout your code, you could potentially be writing multiple values to your speed controllers within the same loop. This could cause some strange PWM output if you're setting your motors to 0, then to 1 every loop.

Dave Scheck 05-09-2011 05:39 PM

Re: Wait(); function
 
Here's some simple timer code that you can apply to this situation as well.

Code:

Timer* t;
bool running = false;
bool driveState = false;
...
// In constructor
t = new Timer();
t->Reset();
...
// In periodic function
int motorSpeed = 0.0;
bool triggerPressed = Thirdstick.GetTrigger();

// When the trigger is pressed, the desired behavior is
// 1. Run the motor forward at half speed for 1 second.
// 2. After one second has passed, turn the motor off
// 3. When another second has passed, return to #1
if(triggerPressed == true)
{
    if(running == false)
    {
        // We aren't running yet.  Start the timer and set the flag
        t->Start();
        running = true;
        driveState = true;
    }

    if(t->HasPeriodPassed(1.0))
    {
        // The one second timer has passed.  Change the drive state
        driveState = !driveState;
        // Reset the timer
        t->Reset();
    }

    // Select a motor speed based on the drive state
    if(driveState == true)
    {
        motorSpeed = 0.5;   
    }
    else
    {
        motorSpeed = 0.0;
    }
}
else
{
    // The trigger is not pressed. 

    // Stop it and reset it
    t->Stop();
    t->Reset();

    // Clear the global variables so that they're ready to
    // go when the button is pressed again
    running = false;
    driveState = false;
}

// Set the motor output
motor->Set(motorSpeed);


krudeboy51 05-09-2011 06:44 PM

Re: Wait(); function
 
ok thanks guys i finally got it!, I did a test where I ran the motor for 10 seconds while driving with the timer() class and it worked!

Ether 05-09-2011 06:59 PM

Re: Wait(); function
 
Quote:

Originally Posted by Ether (Post 1060444)
Is there a specific reason why you want to do this? i.e. Is there a problem you are trying to solve?

Quote:

Originally Posted by krudeboy51 (Post 1060448)
yes!, the problem is that whenever I use wait, we would lose control of our robot until what ever it is doing is finished.



I started to type a reply, but then I noticed you got several responses. Did you get your question answered?

[edit] Never mind. I see your latest post says you got it working. [/edit]





All times are GMT -5. The time now is 10:20 AM.

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