View Single Post
  #2   Spotlight this post!  
Unread 18-01-2014, 10:34
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: Pneumatics Coding

Here's some code to run a spike relay to control your compressor, and it uses a pressure sensor to turn it off.
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.RobotBase;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
import edu.wpi.first.wpilibj.templates.RobotTemplate;

/**
 *
 */
public class ManualRunSpikes extends CommandBase {
    Relay ms1 = compressor.compressorOne();
    boolean thatBoolean = false;
    boolean isFlipped;
    
    public ManualRunSpikes() {
        // Use requires() here to declare subsystem dependencies
        // eg. requires(chassis);
        requires(compressor);
    }

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

    // Called repeatedly when this Command is scheduled to run
    protected void execute() {
        if (oi.getB() == false) {
            isFlipped = false;
        }
        
        if (oi.getB() == true & isFlipped == false) {
            thatBoolean = !thatBoolean;
            isFlipped = true;
        }
        
        if (compressor.getCompressorSwitch() == false & thatBoolean == true) {
            ms1.set(Relay.Value.kForward);
        }
        
        if (compressor.getCompressorSwitch() == false & thatBoolean == false) {
            ms1.set(Relay.Value.kOff);
        }
        
        if (compressor.getCompressorSwitch() == true) {
            ms1.set(Relay.Value.kOff);
        }
        
        SmartDashboard.putBoolean("asdf", thatBoolean);
        SmartDashboard.putBoolean("switch", compressor.getCompressorSwitch());
    }

    // 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() {
            ms1.set(Relay.Value.kOff);
            thatBoolean = false;
    }

    // Called when another command which requires one or more of the same
    // subsystems is scheduled to run
    protected void interrupted() {
            ms1.set(Relay.Value.kOff);
    }
}
Here is our compressor subsystem:
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.DigitalInput;
import edu.wpi.first.wpilibj.Relay;
import edu.wpi.first.wpilibj.command.Subsystem;
import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
import edu.wpi.first.wpilibj.templates.commands.ManualRunSpikes;
import edu.wpi.first.wpilibj.templates.commands.RunSpikes;

/**
 *
 */
public class Compressor extends Subsystem {
    // Put methods for controlling this subsystem
    // here. Call these from Commands.
    Relay s1 = new Relay(1);
    DigitalInput comSwitch = new DigitalInput(1);

    public void initDefaultCommand() {
        // Set the default command for a subsystem here.
        //setDefaultCommand(new RunSpikes());
        setDefaultCommand(new ManualRunSpikes());
    }
    
    public Relay compressorOne() {
        return s1;
    }
    
    public boolean getCompressorSwitch() {
        return comSwitch.get(); 
    }
}
You use Solenoids to control pneumatic cylinders, and here is some code for them:
Code:
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.wpi.first.wpilibj.templates.subsystems;

import edu.wpi.first.wpilibj.Solenoid;
import edu.wpi.first.wpilibj.Talon;
import edu.wpi.first.wpilibj.command.Subsystem;
import edu.wpi.first.wpilibj.templates.RobotMap;


/**
 *
 */
public class Intake extends Subsystem {
    // Put methods for controlling this subsystem
    // here. Call these from Commands.
    
    
    Solenoid shoe = new Solenoid(1, RobotMap.sole); // creates Solenoid
    Solenoid spirit = new Solenoid(1, RobotMap.soul); // creates Solenoid


    public void initDefaultCommand() {
        // Set the default command for a subsystem here.
        //setDefaultCommand(new MySpecialCommand());
    }
    
    
    public void push () {
        shoe.set(true);
        spirit.set(false);
    }
    
    public void pull () {
        shoe.set(false);
        spirit.set(true);
    }
    

}
Hopefully that isn't too confusing, feel free to ask if you need any more help.
__________________
2013 Lake Superior Regional SemiFinalists
2013 Team Spirit Award Winners
2013 MSHSL State Competition SemiFinalists / Third Place
Reply With Quote