|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
| Thread Tools | Rate Thread | Display Modes |
|
#1
|
||||
|
||||
|
A good time based way to program
Whenever my mentors ask me if I can program a motor to run for x seconds, I always feel it's not the best way to do that.
For example, press a button and the motor will run for x seconds and then stop. All I can think of to do that is simply using Timer() class, but then I feel using multiple timers, if else's for them is not just the best way to do it. Is there any other better way to do this or is using Timer() fine? All your thoughts are much appreciated! |
|
#2
|
|||
|
|||
|
Re: A good time based way to program
Code:
import edu.wpi.first.wpilibj.Timer;
import edu.wpi.first.wpilibj.SpeedController;
...
/**
* Sets a motor to a certain power value for a certain number of seconds
* @param controller The speed controller of the motor to power.
* @param power The desired power for the motor.
* @param time The time to run in seconds.
*/
public void setForTime(SpeedController controller, double power, double time) {
controller.set(power);
Timer.delay(time);
controller.set(0);
}
|
|
#3
|
||||
|
||||
|
Re: A good time based way to program
Quote:
If setForTime() is called from drive() and when Timer.delay() will be called, would the whole program stop for that much time or just setForTime()? This is what the javadoc says: Quote:
Last edited by neal : 01-26-2012 at 11:21 PM. |
|
#4
|
|||
|
|||
|
Re: A good time based way to program
How about you create a priority queue of sorts?
(Note that the following is a fake priority queue, it is O(n), while a sorted queue should have a much lower runtime cost) Code:
class TimerQueue
{
static class Pair
{
Runnable runnable;
long timeWhenToRunTask;
}
Vector<Pair> myTasks;
public void schedule(Runnable task, long timeTillTask)
{
myTasks.addElement( new Pair(task,timeTillTask + System.currentTimeInMillisecs() ));
}
public void update()
{
for (int i = 0; i < myTasks.size(); i++)
{
Pair p = myTasks.get(i);
if (p.timeWhenToRunTask < System.currentTimeInMillisecs() )
{
p.task.run();
myTasks.remove(i);
i--;
}
}
}
}
Then you could simply schedule tasks and they will run after the certain time has passed, assuming you call update every "tick". I know threading can do the same things, but I do not trust neither myself nor the cRIO with threading. Last edited by Lalaland1125 : 01-26-2012 at 11:42 PM. |
|
#5
|
|||
|
|||
|
Re: A good time based way to program
Quote:
There should be a sample project called GearsBot if you've updated your NetBeans FRC plugins for this year's competition.If you are going to stick with that style, you could try something like this Code:
// Buttons
final int MOVE_ARM = 1;
// Constants
final int ARM_PWM = 1;
final double ARM_SPEED = 1.0;
final int ARM_TIME = 1.0; // seconds
// Should be instance fields
Timer armTimer = new Timer();
Jaguar arm = new Jaguar(ARM_PWM);
Joystick joystick = new Joystick(1);
armTimer.reset();
armTimer.stop();
while (isOperatorControl() && isEnabled()) {
if (joystick.rawButton(MOVE_ARM)) {
armTimer.start();
}
if (armTimer.get() / 1e6 < ARM_TIME) {
arm.set(ARM_SPEED);
} else {
arm.set(0);
armTimer.stop();
armTimer.reset();
}
}
Undocumented and untested quick example: Code:
public class TimedJaguar extends Jaguar{
Timer timer;
public TimedJaguar() {
timer = new Timer();
timer.reset();
}
public void setForTime(double power, double time) {
// Implement here.
}
}
|
|
#6
|
|||
|
|||
|
Re: A good time based way to program
Look at the updated documentation for 2012 WPILib. I think the command based robot is exactly what you are looking for.
|
|
#7
|
|||
|
|||
|
Re: A good time based way to program
Quote:
You might also check out the videos at http://youtube.com/user/bradamiller. It explains how to write the GearsBot sample program step by step. Brad |
|
#8
|
||||
|
||||
|
Re: A good time based way to program
Thanks all for your suggestions. I'm trying to just stick with SimpleRobot for this year since I don't think we have enough time now to write it for CommandBased one, and to test, troubleshoot it.
Brad, does timeouts also work with SimpleRobot, I checked the cookbook and it only shows examples for the command-based bot. |
|
#9
|
|||
|
|||
|
Re: A good time based way to program
Quote:
Brad |
|
#10
|
||||
|
||||
|
Re: A good time based way to program
Thanks!
We'll probably just use Command Based Robot. Just started writing it again and looking through the cookbook and GearsBot. |
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|