![]() |
Set command to run for a certain amount of time
I need a motor to run for a short duration when I click a button. I already have it working to run continuously when I hit the button but im not sure how to get it to only run for like half a second or something.
I'm sure this is a simple fix I'm just a rookie programmer so could anyone walk me through this? FYI, I'm using the command based template from WPI. |
Re: Set command to run for a certain amount of time
Here is a command that uses a timer. When you create an instance of the command you pass in the time (seconds) you want it to run.
package edu.rhhs.frc.commands; /** * @author rhhs */ public class ConveyorTimed extends CommandBase { private double m_timeout; public ConveyorTimed(double timeout) { m_timeout = timeout; requires(m_conveyor); } protected void initialize() { setTimeout(m_timeout); m_conveyor.start(); } protected void execute() { } protected boolean isFinished() { return isTimedOut(); } protected void end() { m_conveyor.stop(); } protected void interrupted() { m_conveyor.stop(); } } |
Re: Set command to run for a certain amount of time
If you don't want something big and fancy like ^ that guy suggested :), I would use the FPGA time method in the Utility class.
Code:
long millisecondsToRun = 1000; // This should run 1000ms = 1 s.Utility.getFPGATime() gives you a time in milliseconds, say 123400 ms. Now, you want to run your code for a continuous 2 seconds. You would then set the while loop to run your code UNTIL 2 seconds or 2000 ms has passed. So, the loop would be to run until the FPGA time is equals or greater than 125400 ms. And then, it stops running because the conditional is no longer true. |
Re: Set command to run for a certain amount of time
Quote:
not the best way to do things. http://en.wikipedia.org/wiki/Busy_waiting http://citeseerx.ist.psu.edu/viewdoc...=rep1&type=pdf |
Re: Set command to run for a certain amount of time
Quote:
Well, he could put that in its own thread implementation, but they are first year programmers after all... |
Re: Set command to run for a certain amount of time
Also if you don't need interrupts it may be helpful to have a timed command class:
This class is designed for commands that don't have interrupts and have static motor speeds. If you wanted dynamic motor speeds you wouldn't want to override execute. Code:
public abstract class TimedCommand extends Command{Code:
public class RunMotor extends TimedCommand{ |
Re: Set command to run for a certain amount of time
Quote:
|
Re: Set command to run for a certain amount of time
Or you could try this:
The nice thing about this, for all of your timed commands you only have to define what its doing when its running. Code:
public abstract class Mechanism extends Subsystem{Code:
public abstract class TimedCommand extends Command{Code:
public class RunMotor extends TimedCommand{ |
Re: Set command to run for a certain amount of time
You could try this. This worked for me when I had your EXACT problem.
Code:
public void autonomous() |
| All times are GMT -5. The time now is 09:56. |
Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi