Relay/Spike Problems?

I have some code to turn on a spike that is connected to a compressor, and I can turn it on but not off, so I made a second button running a command to turn it off. Problem is, when I added the second command, nothing happens. If I comment it out, It works fine.

public class OI {
    
    Joystick Joystick = new Joystick(1); //Xbox 360 Controller.
    Button button1 = new JoystickButton(Joystick,1);
   // Button button2 = new JoystickButton(Joystick,4);
    public OI(){
        button1.whenPressed(new SpikeOn()); //Runs command to turn it on.
      //  button2.whenPressed(new SpikeOff()); //Runs command to turn it off.
    }
    

^^ Works Fine

public class OI {
    
    Joystick Joystick = new Joystick(1); //Xbox 360 Controller.
    Button button1 = new JoystickButton(Joystick,1);
    Button button2 = new JoystickButton(Joystick,4);
    public OI(){
        button1.whenPressed(new SpikeOn()); //Runs command to turn it on.
        button2.whenPressed(new SpikeOff()); //Runs command to turn it off.
    }
    

^^ It won’t do anything
Any Ideas?

Please post your SpikeOn and SpikeOff commands.

Any Ideas?

What happens when you press and hold both buttons?

What happens if you swap the SpikeOn() and SpikeOff() lines?

SpikeOn:


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.wpi.first.wpilibj.templates.commands;

import edu.wpi.first.wpilibj.Relay;
import edu.wpi.first.wpilibj.templates.subsystems.Spike;
import edu.wpi.first.wpilibj.templates.commands.CommandBase;

/**
 *
 * @author Bennett
 */
public class SpikeOn extends CommandBase {
    Spike spike = new Spike();
    public SpikeOn() {
        
        
        // Use requires() here to declare subsystem dependencies
        // eg. requires(chassis);
    }

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

    // Called repeatedly when this Command is scheduled to run
    protected void execute() {
        spike.spikeon();
        //setTimeout(10.0);
    }

    // Make this return true when this Command no longer needs to run execute()
    protected boolean isFinished() {
       return false;
        // return isTimedOut();
    }

    // Called once after isFinished returns true
    protected void end() {
    }

    // Called when another command which requires one or more of the same
    // subsystems is scheduled to run
    protected void interrupted() {
    }
}

SpikeOff:


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.wpi.first.wpilibj.templates.commands;

import edu.wpi.first.wpilibj.templates.subsystems.Spike;
import edu.wpi.first.wpilibj.templates.commands.SpikeOff;

/**
 *
 * @author Bennett
 */
public class SpikeOff extends CommandBase {
    Spike spike = new Spike();
    
    
    public SpikeOff() {
        // Use requires() here to declare subsystem dependencies
        // eg. requires(chassis);
    }

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

    // Called repeatedly when this Command is scheduled to run
    protected void execute() {
        spike.spikestop();
    }

    // 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() {
    }

    // Called when another command which requires one or more of the same
    // subsystems is scheduled to run
    protected void interrupted() {
    }
}

Here is my spike class:


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.wpi.first.wpilibj.templates.subsystems;

import edu.wpi.first.wpilibj.command.Subsystem;
import edu.wpi.first.wpilibj.Relay;
/**
 *
 * @author Bennett
 */
public class Spike extends Subsystem {
    Relay spike = new Relay(8);
    
  public void spikeon(){
    spike.set(Relay.Value.kForward);
  }
  
  public void spikestop(){
      spike.set(Relay.Value.kOff);
  }
    // Put methods for controlling this subsystem
    // here. Call these from Commands.

    public void initDefaultCommand() {
        // Set the default command for a subsystem here.
        //setDefaultCommand(new MySpecialCommand());
    }
}

Ether, When I press both buttons nothing happens, but I can’t test swapping the code right now…

You’re creating your Spike Subsystem in both of your commands. This is creating a relay object twice, both referencing the same hardware relay port. The second instance is throwing an exception. I suspect if you scroll up in the output window you will see a stack trace.

You should create a static instance of your subsystem in commandbase, and then reference the subsystem in your commands (without creating new objects).

Thanks!

Using Robot Builder makes it easier to avoid these types of errors, because it will will create all the objects for you in the correct places.

http://wpilib.screenstepslive.com/s/3120/m/7882