Go to Post "And that right there is the Sharpie line between right and wrong. Guess which side you cut on?" - Cypher [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 Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 12-02-2013, 11:37
mrklempae mrklempae is offline
Registered User
no team
 
Join Date: Jan 2013
Location: Montana
Posts: 19
mrklempae is an unknown quantity at this point
Wait() issue

We're having trouble with out robot losing control every time Wait() is used. We need to delay part of our code from executing for 1/10th or 1/100th second. That part works (we can't notice the delay), but in other parts of the code (where we need 1.5 second delay) we completely lose control for that period of time. Someone mentioned that we could use the Timer() class for this, but we weren't sure how accurate it is, having to Start and Stop it in a very short period of time. Here's part of out code that causes the most issues:
Code:
			if ((LeftBumper)&&(RightBumper))  //xBox controller
			{
				front->Set(-speed); //Talons
				back-> Set(speed);
				if (ButtonA)
				{
					Forward->Set(1); //solenoids
					Reverse->Set(0);
					Wait(0.1);
					Forward->Set(0);
					Reverse->Set(1);
					Wait(1.5);
				}
			}
Has anyone else encountered this problem?
Reply With Quote
  #2   Spotlight this post!  
Unread 12-02-2013, 11:39
Ether's Avatar
Ether Ether is offline
systems engineer (retired)
no team
 
Join Date: Nov 2009
Rookie Year: 1969
Location: US
Posts: 8,089
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() issue

Quote:
Originally Posted by mrklempae View Post
We're having trouble with out robot losing control every time Wait() is used.
This is a classic problem that new teams (or programmers) struggle with every season.

You need to either

a) use a state machine, or

b) put your wait code in a separate thread where it can run concurrently.


Here's a generic explanation of state machine.



Last edited by Ether : 12-02-2013 at 11:54. Reason: added link
Reply With Quote
  #3   Spotlight this post!  
Unread 12-02-2013, 12:37
kenfox kenfox is offline
Registered User
FRC #3322 (Eagle Imperium)
Team Role: Mentor
 
Join Date: Jan 2013
Rookie Year: 2013
Location: Ann Arbor, MI
Posts: 52
kenfox is a glorious beacon of lightkenfox is a glorious beacon of lightkenfox is a glorious beacon of lightkenfox is a glorious beacon of lightkenfox is a glorious beacon of light
Re: Wait() issue

Quote:
Originally Posted by Ether View Post
a) use a state machine
I agree with trying a state machine first.

Hand-coded state machines are a bit tedious. If you have more than a couple places you need them, the command-based robot approach works better. Robot builder will generate the basic code for your robot after you describe the subsystems and commands.

Here's an example of what a hand-coded state machine looks like.

Code:
// declare the timeout variables at the top of your Robot.cpp
static double waitForwardTimeout = 0.0;
static double waitReverseTimeout = 0.0;

...

if (waitForwardTimeout > 0.0) {
    if (Timer::GetPPCTimestamp() >= waitForwardTimeout) {
        waitForwardTimeout = 0.0;
        // DO SOMETHING AFTER WAITING FOR FORWARD TIMEOUT
        Forward->Set(0);
        Reverse->Set(1);
        waitReverseTimeout = Timer::GetPPCTimestamp + 1.5;
    }
}
else if (waitReverseTimeout > 0.0) {
    if (Timer::GetPPCTimestamp() >= waitReverseTimeout) {
        waitReverseTimeout = 0.0;
        // DO SOMETHING AFTER WAITING FOR REVERSE TIMEOUT
    }
}
else if (LeftBumper && RightBumper) {  // xBox controller
    front->Set(-speed); // Talons
    back->Set(speed);
    if (ButtonA) {
        Forward->Set(1); // solenoids
        Reverse->Set(0);
        waitForwardTimeout = Timer::GetPPCTimestamp + 0.1;
    }
}
There are many ways to implement a state machine, but that code is probably the easiest to fit into your existing code.

In case you can't recognize your code in that example, here's your code with a couple comments added to match the comments in my example:

Code:
if ((LeftBumper)&&(RightBumper)) { // xBox controller
    front->Set(-speed); // Talons
    back-> Set(speed);
    if (ButtonA) {
        Forward->Set(1); // solenoids
        Reverse->Set(0);
        Wait(0.1);
        // DO SOMETHING AFTER WAITING FOR FORWARD TIMEOUT
        Forward->Set(0);
        Reverse->Set(1);
        Wait(1.5);
        // DO SOMETHING AFTER WAITING FOR REVERSE TIMEOUT
    }
}
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 13:57.

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