Go to Post In Dave We Trust :) - JohnBoucher [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 01-02-2016, 18:31
LFRobotics's Avatar
LFRobotics LFRobotics is offline
Registered User
FRC #4623
 
Join Date: Jan 2014
Location: Little Falls, MN
Posts: 95
LFRobotics is on a distinguished road
Pnuematics Toggle Code Issue

Okay so when I press the button associated with the toggleSolenoid command the cylinder occasionally actually toggles back and forth at the touch of the button - but more often than not it just spazzes back and forth repeatedly.

What is wrong??

Pnuematics Subsystem:

Code:
package org.usfirst.frc.team4623.robot.subsystems;

import edu.wpi.first.wpilibj.Compressor;
import edu.wpi.first.wpilibj.DoubleSolenoid;
import edu.wpi.first.wpilibj.command.Subsystem;

/**
 *
 */
public class Pnuematics extends Subsystem {
    
    // Put methods for controlling this subsystem
    // here. Call these from Commands.
	
	Compressor comp = new Compressor(0);

       DoubleSolenoid ds1 = new DoubleSolenoid(0, 1);
	
	public Pnuematics() {
		
		comp.setClosedLoopControl(true);
		
	}

    public void initDefaultCommand() {
        // Set the default command for a subsystem here.
        //setDefaultCommand(new MySpecialCommand());
    }
    
    public void toggleSolenoid() {
    	
       if(ds1.get() == DoubleSolenoid.Value.kForward) {
    	   ds1.set(DoubleSolenoid.Value.kReverse);
       }
       
       else {
    	   ds1.set(DoubleSolenoid.Value.kForward);
       }
    	
    }
    
    public void stop1() {
    	
    	ds1.set(DoubleSolenoid.Value.kOff);
    	
    }

}
toggleSolenoid Command:

Code:
package org.usfirst.frc.team4623.robot.commands;

import org.usfirst.frc.team4623.robot.Robot;

import edu.wpi.first.wpilibj.command.Command;

/**
 *
 */
public class toggleSolenoid extends Command {

    public toggleSolenoid() {
        // Use requires() here to declare subsystem dependencies
        // eg. requires(chassis);
    	
    	requires(Robot.pnue);
    	
    }

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

    // Called repeatedly when this Command is scheduled to run
    protected void execute() {
    	Robot.pnue.toggleSolenoid();
    }

    // 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() {
    	Robot.pnue.stop1();
    }

    // Called when another command which requires one or more of the same
    // subsystems is scheduled to run
    protected void interrupted() {
    	end();
    }
}
Connection between Button and Command:

Code:
toggleSolenoid.whileActive(new toggleSolenoid());
Thanks so much for the help!
Reply With Quote
  #2   Spotlight this post!  
Unread 01-02-2016, 18:46
TGurlik's Avatar
TGurlik TGurlik is offline
Scouting Co-Leader
AKA: Travis Gurlik
FRC #0706 (Cyberhawks)
Team Role: Scout
 
Join Date: Oct 2015
Rookie Year: 2014
Location: Hartland, WI
Posts: 24
TGurlik is an unknown quantity at this point
Re: Pnuematics Toggle Code Issue

My team has just switched over to Java from C++ this year, so I'm a little unsure of myself, but I think I know what your issue is. Using whileActive to toggle your solenoid will result in the toggleSolenoid() function running repeatedly as long as the button is held (assuming I'm understanding this right). If there's a function you can use that will only run once when the button is pressed instead of whileActive, that would be best. Otherwise, you can look into using a timer to run the function at most once every half second or so, which would keep it from freaking out.
__________________
2014 - Programmer:
Lake Superior - Finalist (with teams 3313 and 3883) and Industrial Design Award
Wisconsin - Finalist (with teams 3018 and 1259) and Quality Award
2015 - Programmer:
Wisconsin - Quality Award
Queen City - Creativity Award
Tesla Division

2015-2017 - Programming Mentor for FTC Team #9956 The Knack
2016 - Scouting Co-Leader:
Buckeye - Finalist (with teams 27, 379, and 1014)
Wisconsin - Finalist (with teams 5903 and 537), Excellence in Engineering Award, and Industrial Safety Award
2017 - Scouting Co-Leader:
Central Illinois - ???
Wisconsin - ???
Reply With Quote
  #3   Spotlight this post!  
Unread 01-02-2016, 20:39
Joe Derrick's Avatar
Joe Derrick Joe Derrick is offline
Mentor - Programming
FRC #0319
 
Join Date: Feb 2012
Rookie Year: 2011
Location: Prospect Mountain High School
Posts: 44
Joe Derrick is a glorious beacon of lightJoe Derrick is a glorious beacon of lightJoe Derrick is a glorious beacon of lightJoe Derrick is a glorious beacon of lightJoe Derrick is a glorious beacon of lightJoe Derrick is a glorious beacon of light
Re: Pnuematics Toggle Code Issue

Hey there, try adding or modifying your subsystem to this

Code:
public class Pnuematics extends Subsystem {

public boolean toggle;
	
    public void solenoidIn() {
    	   ds1.set(DoubleSolenoid.Value.kReverse);
	toggle = true;
    }
       
    public void solenoidOut(){
    	   ds1.set(DoubleSolenoid.Value.kForward);
toggle = false;
       }
    	
}
these are two methods which when called will set the solenoid in a direction then flip the state of the boolean

then your Command to this

Code:
public class toggleSolenoid extends Command {

    public toggleSolenoid() {	
    	requires(Robot.pnue);    	
    }

protected void initialize() {
	if (Robot.pnue.toggle == true)
    	{
    	Robot.pnue.solenoidOut();
    	}
    	else if(Robot.pnue.toggle == false){
    	Robot.pnue.solenoidIn();
    	}
    }

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

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

    // Called once after isFinished returns true
    protected void end() {
    
    }
protected void interrupted() {
    	end();
    }
}
since solenoids retain their "state" when switched, they do not need to be told continuously what do do. they are set to forward or reverse one time in the "initialize" part of the command depending on the state of the boolean. Then the command finishes immediately.

the OI should be

Code:
toggleSolenoid.whenPressed(new toggleSolenoid());
Best of luck, hope this helps. Once mastered, command based is the way to go!

Also there may be other ways to acheive a "toggle" but this has worked for us in the past. I did see a "toggle option" in the OI this year but have yet to test it.
__________________
2010-2017 Mentor Team 319
2012 - Rockwell Automation Award Winner
2014 - Xerox Creativity in Engineering Winner, Archimedes Division
2015 - Rockwell Automation and Gracious Proffesionalism Winner, Tesla Division
2016 - North Shore and UNH District Event Winner, Carson Division
http://www.frc319.com
Reply With Quote
  #4   Spotlight this post!  
Unread 02-02-2016, 16:10
LFRobotics's Avatar
LFRobotics LFRobotics is offline
Registered User
FRC #4623
 
Join Date: Jan 2014
Location: Little Falls, MN
Posts: 95
LFRobotics is on a distinguished road
Re: Pnuematics Toggle Code Issue

Okay using your code I got it to work - thanks so much!
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 10:51.

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