|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
| Thread Tools | Rate Thread | Display Modes |
|
#1
|
||||||
|
||||||
|
whileHeld() Motor Program Issue
Okay - I am programming a command based java and wrote a couple of commands defined by a subsystem called ForkMotor. One command was supposed to move a motor forward(1) while a button was pressed. The other was supposed to move a motor backwards(-1) while a button is pressed. When I hold the button down that is supposed to move it forward the motor glitches off and on but does stop when I let go - the motor is fine I tested it. The button that SHOULD turn it in the other direction doesn't work at all. If anyone knows how to fix this that would be AWESOME.
Here it is: Subsystem(called ForkMotor): Quote:
Quote:
Second Command(to move it in reverse -- called LowerForks): Quote:
Quote:
Quote:
Quote:
Last edited by LFRobotics : 02-02-2014 at 17:03. |
|
#2
|
||||
|
||||
|
Re: whileHeld() Motor Program Issue
Check out the javadoc for the whileHeld method. It might not be doing what you expect.
whileHeld will repeatedly call the command's start method (50 times a second). The start method will add the command to the scheduler. In the command based project, the scheduler is the bit of code that is responsible for stepping all active commands through their steps of execution [initialize(), execute(), isfinished(), end()]. The comment on the start() method is worth reading: Quote:
Code:
ForksUp.whenPressed(new LiftForks()); ForksUp.whenReleased(new StopForks()); ForksDown.whenPressed(new LowerForks()); ForksDown.whenReleased(new StopForks()); Other than the use of whileHeld, I don't see any other glaring errors in your code. |
|
#3
|
||||
|
||||
|
Re: whileHeld() Motor Program Issue
Okay - THANKS a ton!
|
|
#4
|
||||
|
||||
|
Re: whileHeld() Motor Program Issue
whileHeld() works as advertised, you just need to remember to shut down things in end() and interrupted()...
Our experience last summer was whileHeld called init() once, (execute(), getFinished()) repeatedly, and interrupted() is called when the button is released. We used whileHeld reliably with commands like this (note that we stop the motor from end() and interrupted(). We would see multiple init() if several whileHelds fired off commands that required the same subsystem(s) and those buttons were held at the same time. I don't remember the exact details. Code:
package org.usfirst.frc3620.FRC36202013RobotRedo.commands;
import edu.wpi.first.wpilibj.command.Command;
import org.usfirst.frc3620.FRC36202013RobotRedo.Robot;
/**
*
*/
public class FlipperBackwardCommand extends Command {
public FlipperBackwardCommand() {
// Use requires() here to declare subsystem dependencies
// eg. requires(chassis);
// BEGIN AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=REQUIRES
requires(Robot.flipperSubsystem);
// END AUTOGENERATED CODE, SOURCE=ROBOTBUILDER ID=REQUIRES
}
// Called just before this Command runs the first time
protected void initialize() {
}
// Called repeatedly when this Command is scheduled to run
protected void execute() {
Robot.flipperSubsystem.flipperBackward();
}
// Make this return true when this Command no longer needs to run execute()
protected boolean isFinished() {
return false;
}
// Called once after isFinished returns true
protected void end() {
Robot.flipperSubsystem.flipperStop();
}
// Called when another command which requires one or more of the same
// subsystems is scheduled to run
protected void interrupted() {
end();
}
}
|
|
#5
|
||||
|
||||
|
Re: whileHeld() Motor Program Issue
I went through the code more closely than last time...
JoystickButton inherits whileHeld from Button. Button extends Trigger. whileHeld calls Trigger's whileActive method From Javadoc: Code:
whileActive public void whileActive(Command command) Constantly starts the given command while the button is held. Command.start() will be called repeatedly while the trigger is active, and will be canceled when the trigger becomes inactive. Parameters: command - the command to start Command.start() adds the command to a vector of command that should be added to the list of commands that the scheduler will process. Here's the catch, the scheduler doesn't do anything with the command if the command is already in the list. See line 145. So I agree. whileHeld should work as advertised. I know that I've had trouble with it in the past, and have always implemented commands as I outlined above. Maybe the problems I had with whileHeld were actually with something else, I haven't gone back to investigate. |
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|