![]() |
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?
|
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.
|
Re: Wait(); function
Quote:
|
Re: Wait(); function
Quote:
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 |
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! |
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 file1. 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. |
Re: Wait(); function
Here's some simple timer code that you can apply to this situation as well.
Code:
Timer* t; |
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!
|
Re: Wait(); function
Quote:
[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