Go to Post FIRST isn't chaotic; the rest of the world just moves in slow motion! - StephLee [more]
Home
Go Back   Chief Delphi > Technical > Programming > C/C++
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Reply
 
Thread Tools Rating: Thread Rating: 3 votes, 5.00 average. Display Modes
  #1   Spotlight this post!  
Unread 05-09-2011, 03:52 PM
krudeboy51's Avatar
krudeboy51 krudeboy51 is offline
Only Programmer
AKA: kory
FRC #0369 (369)
Team Role: Programmer
 
Join Date: Mar 2010
Rookie Year: 2010
Location: brooklyn
Posts: 151
krudeboy51 is a glorious beacon of lightkrudeboy51 is a glorious beacon of lightkrudeboy51 is a glorious beacon of lightkrudeboy51 is a glorious beacon of lightkrudeboy51 is a glorious beacon of light
Send a message via AIM to krudeboy51
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?
Reply With Quote
  #2   Spotlight this post!  
Unread 05-09-2011, 04:02 PM
Kevin Sevcik's Avatar
Kevin Sevcik Kevin Sevcik is online now
(Insert witty comment here)
FRC #0057 (The Leopards)
Team Role: Mentor
 
Join Date: Jun 2001
Rookie Year: 1998
Location: Houston, Texas
Posts: 3,569
Kevin Sevcik has a reputation beyond reputeKevin Sevcik has a reputation beyond reputeKevin Sevcik has a reputation beyond reputeKevin Sevcik has a reputation beyond reputeKevin Sevcik has a reputation beyond reputeKevin Sevcik has a reputation beyond reputeKevin Sevcik has a reputation beyond reputeKevin Sevcik has a reputation beyond reputeKevin Sevcik has a reputation beyond reputeKevin Sevcik has a reputation beyond reputeKevin Sevcik has a reputation beyond repute
Send a message via AIM to Kevin Sevcik Send a message via Yahoo to Kevin Sevcik
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.
__________________
The difficult we do today; the impossible we do tomorrow. Miracles by appointment only.

Lone Star Regional Troubleshooter
Reply With Quote
  #3   Spotlight this post!  
Unread 05-09-2011, 04:11 PM
Ether's Avatar
Ether Ether is offline
systems engineer (retired)
no team
 
Join Date: Nov 2009
Rookie Year: 1969
Location: US
Posts: 8,002
Ether has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond repute
Re: Wait(); function

Quote:
Originally Posted by krudeboy51 View Post
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?


Reply With Quote
  #4   Spotlight this post!  
Unread 05-09-2011, 04:36 PM
krudeboy51's Avatar
krudeboy51 krudeboy51 is offline
Only Programmer
AKA: kory
FRC #0369 (369)
Team Role: Programmer
 
Join Date: Mar 2010
Rookie Year: 2010
Location: brooklyn
Posts: 151
krudeboy51 is a glorious beacon of lightkrudeboy51 is a glorious beacon of lightkrudeboy51 is a glorious beacon of lightkrudeboy51 is a glorious beacon of lightkrudeboy51 is a glorious beacon of light
Send a message via AIM to krudeboy51
Re: Wait(); function

Quote:
Originally Posted by Ether View Post
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
Reply With Quote
  #5   Spotlight this post!  
Unread 05-09-2011, 05:17 PM
TofuRama TofuRama is offline
01000110 01010010 01000011
AKA: Matt
FRC #2484 (Team Implosion)
Team Role: Programmer
 
Join Date: Apr 2011
Rookie Year: 2009
Location: Woods Cross Utah
Posts: 17
TofuRama is an unknown quantity at this point
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!
Reply With Quote
  #6   Spotlight this post!  
Unread 05-09-2011, 05:24 PM
Dave Scheck's Avatar
Dave Scheck Dave Scheck is offline
Registered User
FRC #0111 (WildStang)
Team Role: Engineer
 
Join Date: Feb 2003
Rookie Year: 2002
Location: Arlington Heights, IL
Posts: 574
Dave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond repute
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.
Reply With Quote
  #7   Spotlight this post!  
Unread 05-09-2011, 05:39 PM
Dave Scheck's Avatar
Dave Scheck Dave Scheck is offline
Registered User
FRC #0111 (WildStang)
Team Role: Engineer
 
Join Date: Feb 2003
Rookie Year: 2002
Location: Arlington Heights, IL
Posts: 574
Dave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond reputeDave Scheck has a reputation beyond repute
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);
Reply With Quote
  #8   Spotlight this post!  
Unread 05-09-2011, 06:44 PM
krudeboy51's Avatar
krudeboy51 krudeboy51 is offline
Only Programmer
AKA: kory
FRC #0369 (369)
Team Role: Programmer
 
Join Date: Mar 2010
Rookie Year: 2010
Location: brooklyn
Posts: 151
krudeboy51 is a glorious beacon of lightkrudeboy51 is a glorious beacon of lightkrudeboy51 is a glorious beacon of lightkrudeboy51 is a glorious beacon of lightkrudeboy51 is a glorious beacon of light
Send a message via AIM to krudeboy51
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!
Reply With Quote
  #9   Spotlight this post!  
Unread 05-09-2011, 06:59 PM
Ether's Avatar
Ether Ether is offline
systems engineer (retired)
no team
 
Join Date: Nov 2009
Rookie Year: 1969
Location: US
Posts: 8,002
Ether has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond repute
Re: Wait(); function

Quote:
Originally Posted by Ether View Post
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 View Post
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]




Last edited by Ether : 05-09-2011 at 07:02 PM.
Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


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

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


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