Chief Delphi

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

Bennett 01-15-2013 01:37 PM

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.
Code:

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


Code:

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?

Joe Ross 01-15-2013 01:54 PM

Re: Relay/Spike Problems?
 
Please post your SpikeOn and SpikeOff commands.

Ether 01-15-2013 01:57 PM

Re: Relay/Spike Problems?
 
Quote:

Any Ideas?
What happens when you press and hold both buttons?

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




Bennett 01-15-2013 02:00 PM

Re: Relay/Spike Problems?
 
SpikeOn:

Code:

/*
 * 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:

Code:

/*
 * 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:

Code:

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


Bennett 01-15-2013 02:01 PM

Re: Relay/Spike Problems?
 
Ether, When I press both buttons nothing happens, but I can't test swapping the code right now...

Joe Ross 01-15-2013 02:15 PM

Re: Relay/Spike Problems?
 
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).

Bennett 01-15-2013 02:17 PM

Re: Relay/Spike Problems?
 
Thanks!

Joe Ross 01-15-2013 02:27 PM

Re: Relay/Spike Problems?
 
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


All times are GMT -5. The time now is 07:43 AM.

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