Go to Post They have a Woodie Flowers Finalist, Outstanding Volunteer and numerous UFH Awards to demonstrate this as well. You do not receive any of these accolades without doing something right. - J Flex 188 [more]
Home
Go Back   Chief Delphi > Technical > Programming > Java
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Reply
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 15-01-2013, 13:37
Bennett Bennett is offline
Registered User
FRC #2977 (Sir Lancer Bots)
Team Role: Mentor
 
Join Date: Jan 2012
Rookie Year: 2012
Location: Minnesota
Posts: 26
Bennett is an unknown quantity at this point
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?
Reply With Quote
  #2   Spotlight this post!  
Unread 15-01-2013, 13:54
Joe Ross's Avatar Unsung FIRST Hero
Joe Ross Joe Ross is offline
Registered User
FRC #0330 (Beachbots)
Team Role: Engineer
 
Join Date: Jun 2001
Rookie Year: 1997
Location: Los Angeles, CA
Posts: 8,572
Joe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond repute
Re: Relay/Spike Problems?

Please post your SpikeOn and SpikeOff commands.
Reply With Quote
  #3   Spotlight this post!  
Unread 15-01-2013, 13:57
Ether's Avatar
Ether Ether is offline
systems engineer (retired)
no team
 
Join Date: Nov 2009
Rookie Year: 1969
Location: US
Posts: 8,089
Ether has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond reputeEther has a reputation beyond repute
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?



Reply With Quote
  #4   Spotlight this post!  
Unread 15-01-2013, 14:00
Bennett Bennett is offline
Registered User
FRC #2977 (Sir Lancer Bots)
Team Role: Mentor
 
Join Date: Jan 2012
Rookie Year: 2012
Location: Minnesota
Posts: 26
Bennett is an unknown quantity at this point
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());
    }
}
Reply With Quote
  #5   Spotlight this post!  
Unread 15-01-2013, 14:01
Bennett Bennett is offline
Registered User
FRC #2977 (Sir Lancer Bots)
Team Role: Mentor
 
Join Date: Jan 2012
Rookie Year: 2012
Location: Minnesota
Posts: 26
Bennett is an unknown quantity at this point
Re: Relay/Spike Problems?

Ether, When I press both buttons nothing happens, but I can't test swapping the code right now...
Reply With Quote
  #6   Spotlight this post!  
Unread 15-01-2013, 14:15
Joe Ross's Avatar Unsung FIRST Hero
Joe Ross Joe Ross is offline
Registered User
FRC #0330 (Beachbots)
Team Role: Engineer
 
Join Date: Jun 2001
Rookie Year: 1997
Location: Los Angeles, CA
Posts: 8,572
Joe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond repute
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).
Reply With Quote
  #7   Spotlight this post!  
Unread 15-01-2013, 14:17
Bennett Bennett is offline
Registered User
FRC #2977 (Sir Lancer Bots)
Team Role: Mentor
 
Join Date: Jan 2012
Rookie Year: 2012
Location: Minnesota
Posts: 26
Bennett is an unknown quantity at this point
Re: Relay/Spike Problems?

Thanks!
Reply With Quote
  #8   Spotlight this post!  
Unread 15-01-2013, 14:27
Joe Ross's Avatar Unsung FIRST Hero
Joe Ross Joe Ross is offline
Registered User
FRC #0330 (Beachbots)
Team Role: Engineer
 
Join Date: Jun 2001
Rookie Year: 1997
Location: Los Angeles, CA
Posts: 8,572
Joe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond reputeJoe Ross has a reputation beyond repute
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
Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -5. The time now is 11:49.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


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