Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   Java (http://www.chiefdelphi.com/forums/forumdisplay.php?f=184)
-   -   Relay Button Issue (http://www.chiefdelphi.com/forums/showthread.php?t=102669)

Gnome2013 12-02-2012 19:48

Relay Button Issue
 
We have two buttons that control one spike but in the code only one works if the other is commented out. How should we go about fixing this?

Patrick Chiang 12-02-2012 21:09

Re: Relay Button Issue
 
Well, the first thing you could do is show us your code.

NS_Radication 12-02-2012 21:25

Re: Relay Button Issue
 
Well, I coded an on/off switch recently and I came up with a one button toggle after a two button toggle.
Code:

public void teleopPeriodic()
{
    boolean spikeToggle = false;
    spike.set(Relay.Value.kOff);
    if(joy1.getRawButton(BUTTON_NUM)) //set the button to activate motor for the joystick
    {
        if(spikeToggle == false) // if the toggle is off, set to true and activate motor
        {
            spikeToggle = true;
            spike.set(Relay.Value.kOn);
        }
        Timer.delay(0.5); // cannot change the value within the first half second of activating, you can change value but it is here to have a safe toggle without toggling every millisecond
        else if(spikeToggle == true) // if the motor is on, turn it off and it is set to false
        {
            spikeToggle = false;
            spike.set(Relay.Value.kOff);
        }
        Timer.delay(0.5); // again it stalls the change for convenience and safety
    }
}

Two buttons were crazy, but if you want two buttons, just replace the else if with the if statement and add it to the second button loop. Hope this helped.

If you have any questions and comments, just reply or post, etc.

Happy Competitions!

BurtGummer 14-02-2012 19:55

Re: Relay Button Issue
 
So, what is the problem? I assume you fixed it? Regardless, you might want to create a separate thread in teleOp periodic since you have the 0.5s delays in there.

gixxy 15-02-2012 10:50

Re: Relay Button Issue
 
Here is how I did one button with CommandBased


Use something like (its explained in the OI class):
Code:

ButtonObject.whenPressed(new ToggleSpike());
then inside the command my code:

Code:

private boolean state = false;
    private boolean previousState = true;
   
   
    public SetFeedBelt() {
        // Use requires() here to declare subsystem dependencies
        // eg. requires(chassis);
        requires(feedBelt);
    }

    // Called just before this Command runs the first time
    protected void initialize() {
    }

    // Called repeatedly when this Command is scheduled to run
    protected void execute() {
        state = !state;
        feedBelt.setBelt(state);
    }

    // Make this return true when this Command no longer needs to run execute()
    protected boolean isFinished() {
        if(state != previousState) {
            return true;
        } else {
            return false;
        }
    }

    // Called once after isFinished returns true
    protected void end() {
        previousState = state;
    }

So with two buttons make it two button.whenPressed(new onOrOffCommand()); and two commands, and if they require the spike subsystem it will handle the interrupts for you.


All times are GMT -5. The time now is 22:41.

Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi