We are using pneumatics with our robot this year and want to know how/if we can use a timer to tell the solenoid to stay open for a certain amount of time.
We have historically used timestamps:
package edu.wpi.first.wpilibj.templates;
import edu.wpi.first.wpilibj.Joystick;
import edu.wpi.first.wpilibj.SimpleRobot;
import edu.wpi.first.wpilibj.Timer;
// Boolean used to determine whether or not the frisby indexer is on
boolean indexerActive = false;
/* Timestamp variables for identifying the robots current cycle time
* This is to be used most primarily for user intarction via button
* presses. Prevents the button from being repeatedly cycled.
*/
long CPUlastTime;
long CPUcurrentTime;
long CPUtimeDifference;
public void robotInit() {
}
public void operatorControl() {
if(xboxControl.getRawButton(1)){
CPUcurrentTime = System.currentTimeMillis();
CPUtimeDifference = CPUcurrentTime - CPUlastTime;
if(indexerActive == true) {
if(CPUtimeDifference > 500) {
indexerActive = false;
CPUlastTime = System.currentTimeMillis();
}
}
else {
if(CPUtimeDifference > 500) {
indexerActive = true;
CPUlastTime = System.currentTimeMillis();
}
}
System.out.println("HID: XBox|A pressed");
}
}
Pardon the mess. Let me know how that works for you. Alternatively, you can start learning about the iterativeRobot structure to do it, which shoots it off into separate threads in separate files. Someone correct me on any of this if it sounds off…
There are a few methods. You could used command based programming, and then use it as such (assuming you’re using a spike relay, aka bridge driver to control the solenoid):
public ShootFrisbee() {
// Use requires() here to declare subsystem dependencies
// eg. requires(chassis);
requires(shooterSubsystem);
setTimeout(0.25);
}
// Called just before this Command runs the first time
protected void initialize() {
}
// Called repeatedly when this Command is scheduled to run
protected void execute() {
shooterSubsystem.shootSolenoid(Relay.Value.kOn);
}
// Make this return true when this Command no longer needs to run execute()
protected boolean isFinished() {
return isTimedOut();
}
// Called once after isFinished returns true
protected void end() {
shooterSubsystem.shootSolenoid(Relay.Value.kOff);
}
// Called when another command which requires one or more of the same
// subsystems is scheduled to run
protected void interrupted() {
}
If you want to just use the SimpleRobot, then using java’s built in delay command should work (solenoid is the relay object in this block)
solenoid.set(Relay.Value.kOn);
wait(0.25);
solenoid.set(Relay.Value.kOff);
Of course, the second example’s block of code should be run in a separate thread so the wait(0.25) function does not clog up your robot’s “docket”.