Go to Post Your students are my idols. - Al Skierkiewicz [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 09-05-2011, 17:24
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
  #2   Spotlight this post!  
Unread 09-05-2011, 17:39
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
  #3   Spotlight this post!  
Unread 09-05-2011, 18:44
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
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:22.

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