Pneumatics system code

From all of us here at Team 4939, hopefully everybody had a good time. As a rookie team I know we did.

During the off-season we decided to incorporate a very basic pneumatics system into our robot, so that we would be more experienced for next. We were able to figure out where to connect everything (or so we think) according to the instructions, but when it comes to the code, we are just feeling very lost.

As a rookie team most of out programmers including myself, have no clue where to go from here. We found a couple resources online, but are just feeling very confused. We believe we have all the parts we just have to get them to work. The system works manually, but when doing it automatically with the joystick we have no clue where to go. Here is our current code

package edu.wpi.first.wpilibj.templates;
 
import edu.wpi.first.wpilibj.Jaguar;
import edu.wpi.first.wpilibj.Joystick;
import edu.wpi.first.wpilibj.RobotDrive;
import edu.wpi.first.wpilibj.SimpleRobot;
import edu.wpi.first.wpilibj.Timer;
 
public class RobotTemplate extends SimpleRobot {
    
    RobotDrive chassis = new RobotDrive(1,2);
    Joystick mainStick = new Joystick(1);
    Jaguar jaguar = new Jaguar(3);
    Jaguar jag = new Jaguar(4);

    
    public void autonomous(){ 
        chassis.setSafetyEnabled(false);
        chassis.drive (-0.5, 0.08);
        Timer.delay(2.0);
        chassis.drive (0, 0.0);
    }
    
    public void operatorControl() {
        chassis.setSafetyEnabled(true);
        while (isOperatorControl() && isEnabled()) {
            double speed;
            double rot;
            speed = mainStick.getY();
            rot = -mainStick.getX();
            chassis.arcadeDrive (speed, rot);
            if (mainStick.getRawButton(3)){
                jag.set(1);
                jaguar.set(-1);
            }
            else if (mainStick.getRawButton(4)){
                jaguar.set(-1);
                jag.set(1);
            }
            else{
                jaguar.set(0);
                jag.set(0);
            }
    }
    }
    
    public void test() {
       
    }
}   

All help is greatly appreciated since it is the end of the FRC season.

I will try and upload a picture of how we have the system set up tomorrow.

Thanks in Advance

The compressor is controlled automatically through the Compressor class. Simply pass the Pressure Switch and Relay channels to the constructor and call Compressor#start() in robotInit() and you should be good to go. If desired, it can be disabled with Compressor#stop().

Solenoids come in two varieties: Solenoids and DoubleSolenoids. Single solenoids can be set true or false, while double solenoids are typically set to DoubleSolenoid.Value.kForward, DoubleSolenoid.Value.kReverse, or DoubleSolenoid.Value.kOff.

A simple example incorporating your code:


package edu.wpi.first.wpilibj.templates;
 
import edu.wpi.first.wpilibj.Jaguar;
import edu.wpi.first.wpilibj.Joystick;
import edu.wpi.first.wpilibj.RobotDrive;
import edu.wpi.first.wpilibj.SimpleRobot;
import edu.wpi.first.wpilibj.Timer;
 
public class RobotTemplate extends SimpleRobot {
    
    RobotDrive chassis = new RobotDrive(1,2);
    Joystick mainStick = new Joystick(1);
    Jaguar jaguar = new Jaguar(3);
    Jaguar jag = new Jaguar(4);

    // Pneumatics
    Compressor compressor = new Compressor(1, 1);
    Solenoid solenoid = new Solenoid(1);
    DoubleSolenoid doubleSolenoid = new DoubleSolenoid(2, 3);
    
    public void robotInit() {
        compressor.start();
    }

    public void autonomous(){ 
        chassis.setSafetyEnabled(false);
        chassis.drive (-0.5, 0.08);
        Timer.delay(2.0);
        chassis.drive (0, 0.0);
    }
    
    public void operatorControl() {
        chassis.setSafetyEnabled(true);
        while (isOperatorControl() && isEnabled()) {
            double speed;
            double rot;
            speed = mainStick.getY();
            rot = -mainStick.getX();
            chassis.arcadeDrive (speed, rot);
            if (mainStick.getRawButton(3)){
                jag.set(1);
                jaguar.set(-1);
            }
            else if (mainStick.getRawButton(4)){
                jaguar.set(-1);
                jag.set(1);
            }
            else{
                jaguar.set(0);
                jag.set(0);
            }

            // Solenoid
            solenoid.set(mainStick.getRawButton(1);
            if (mainStick.getRawButton(2))
                doubleSolenoid.set(DoubleSolnoid.Value.kForward);
            else
                doubleSolenoid.set(DoubleSolenoid.Value.kOff);
        }
    }
    
    public void test() {
       
    }
}

Also, you might want to check out these articles from the WPILib docs at ScreenSteps Live:
http://wpilib.screenstepslive.com/s/3120/m/7912/l/85779-operating-a-compressor-for-pneumatics
http://wpilib.screenstepslive.com/s/3120/m/7912/l/132407-operating-pneumatic-cylinders-solenoids

I would start by making a list of what all the connected pneumatic components are and what joystick inputs they’re supposed to be controlled by. This will make it easier to help you.

By the way, the following two code snipets are identical:

int x;
x=4;
int x=4;

Since you are just starting out, I want to point out a tiny error in Domenic Rodriguez ’ s code. He named a variable “double” since it represents a double solenoid. However, you can’t name a variable “double” in Java as it is a reserved word. Change the name to doubleSol or something else and you will be fine. Be sure to only change the name of the variable and not other occurrences of “double”.

Sorry…on mobile or I would repost the code with the fixes in place.

Fixed, my bad. Now I feel silly; I guess that’s what I get for writing code at 12:30 AM :rolleyes:

Sorry for not replying earlier, but I just haven’t been able to test the code that you all helped out with yet, due to exams and such. I should be able to test the system out tomorrow and I will post a reply letting you know how it worked out. I was able to get a picture: http://postimg.org/image/wvu8lykol/ . If that makes a difference to the code.

Also I was wondering something about where to plug the spike in, I pluged one end to the solenoid, and the other end into port 5 on my Digital side car.

I noticed in the code that Domenic Rodriguez posted :

 // Pneumatics
    Compressor compressor = new Compressor(1, 1);
    Solenoid solenoid = new Solenoid(1);
    DoubleSolenoid doubleSolenoid = new DoubleSolenoid(2, 3);

Ports 1, 2, and 3 are labeled.

Where should I plug my Spike into?

Thank You to all that helped me get this far, just a little more help is needed.

To clarify, are you planning on controlling your solenoid with a Spike Relay, or with the solenoid module on the cRIO?

The Solenoid and DoubleSolenoid classes only work for controlling solenoids through the solenoid module. If you are controlling your solenoid through a relay, you will need to use the Relay class instead.

Judging by your picture, it looks like you are using a double solenoid. I’ve never controlled a solenoid through Spikes before, [strike]but I believe you will need two Spikes, one for each side.[/strike] Edit: Nope, see Mark McLeod’s post below.

Updated code:


package edu.wpi.first.wpilibj.templates;
 
import edu.wpi.first.wpilibj.Jaguar;
import edu.wpi.first.wpilibj.Joystick;
import edu.wpi.first.wpilibj.RobotDrive;
import edu.wpi.first.wpilibj.SimpleRobot;
import edu.wpi.first.wpilibj.Timer;
import edu.wpi.first.wpilibj.Compressor;
import edu.wpi.first.wpilibj.Relay;
 
public class RobotTemplate extends SimpleRobot {
    
    RobotDrive chassis = new RobotDrive(1,2);
    Joystick mainStick = new Joystick(1);
    Jaguar jaguar = new Jaguar(3);
    Jaguar jag = new Jaguar(4);

    // Pneumatics
    Compressor compressor = new Compressor(1, 1);
    Relay spike = new Relay(2);
    
    public void robotInit() {
        compressor.start();
    }

    public void autonomous(){ 
        chassis.setSafetyEnabled(false);
        chassis.drive (-0.5, 0.08);
        Timer.delay(2.0);
        chassis.drive (0, 0.0);
    }
    
    public void operatorControl() {
        chassis.setSafetyEnabled(true);
        while (isOperatorControl() && isEnabled()) {
            double speed = mainStick.getY();
            double rot = -mainStick.getX();
            chassis.arcadeDrive (speed, rot);
            if (mainStick.getRawButton(3)){
                jag.set(1);
                jaguar.set(-1);
            }
            else if (mainStick.getRawButton(4)){
                jaguar.set(-1);
                jag.set(1);
            }
            else{
                jaguar.set(0);
                jag.set(0);
            }

            // Solenoid
            if (mainStick.getRawButton(1))
                spike.set(Relay.Value.kForward);
            else if (mainStick.getRawButton(2))
                spike.set(Relay.Value.kReverse);
        }
    }
}

Only one spike per double solenoid.
The code for a double would use kForward for one direction and kReverse for the other direction. Don’t use kOff for this purpose.
Wiring a spike to control a 12v-only solenoid via a Relay output on the Digital Sidecar looks like this:

24v solenoids are usable only through the cRIO Solenoid Module/Breakout, not a 12v Spike relay.

By the picture that I posted earlier this evening could you tell me if it is a 24v solenoid or a 12v solenoid. If not I will just ask my electrical team members.

I suspect from your photo that it is a 24v solenoid just because that’s what has been offered in the KOP, but that style can be either voltage rating. The black end caps will have a voltage printed on them.

If it is a 24v solenoid, then it gets wired to two sets of pins on the Solenoid Breakout and the Solenoid Breakout must get 24v power from the empty connectors of the Power Distribution’s special 24v output.

Yes you were right it was a 24v solenoid, but I was able to find a different 12v double solenoid in a couple of spare parts sitting around, but unfortunately due to exams and such our team will not be able to switch the solenoids out until next week this time. So I would just like to thank you for all the help you have offered.

I should be able to use the code above posted by Domenic Rodriguez correct?

Correct, that code looks good.

I tried out the code but it did not work out.

If I am right the spike has a light that is supposed to flash when I pressed the assigned button on my joystick. Also on my driver station when a activate any of the other functions such as (movement, or intake motors) the voltage spikes, is that not supposed to happen when a press the button assigned to the pneumatics spike.

None of the things is happening, so I don’t know what to exactly do.

I am sitting here with my team so any immediate help would be greatly appreciated.

Please provide a copy of your code for this.

Also, what is the LED on the spike doing? (Blink Status & Color)

I am using the code that Domenic Rodriguez posted:

The light of the spike is red-ish/orange and it is steady.

Here are some pics of the wiring at the moment:

http://tinypic.com/view.php?pic=11qpkjs&s=8#.U6m8OyWmfIU

Any solutions would be appreciated

Ok, to do some quick troubleshooting:

First, for my sanity, please add brackets

// Solenoid
if (mainStick.getRawButton(1))
{
   spike.set(Relay.Value.kForward);
}
else {
  if (mainStick.getRawButton(2))
 {
   spike.set(Relay.Value.kReverse);
 }
}

If problem persists, comment out the code and use only

spike.set(Relay.Value.kForward);

If the spike LED is not solid Green, you have a wiring issue. Either your wire is bad or the spike is on the wrong port. If that does work, it becomes a code issue.

“If the spike LED is not solid Green, you have a wiring issue. Either your wire is bad or the spike is on the wrong port. If that does work, it becomes a code issue.”

We tried switching all the wires, but not change in the light.

Here is the updated code:

package edu.wpi.first.wpilibj.templates;
 
import edu.wpi.first.wpilibj.Jaguar;
import edu.wpi.first.wpilibj.Joystick;
import edu.wpi.first.wpilibj.RobotDrive;
import edu.wpi.first.wpilibj.SimpleRobot;
import edu.wpi.first.wpilibj.Timer;
import edu.wpi.first.wpilibj.Compressor;
import edu.wpi.first.wpilibj.Relay;
 
public class RobotTemplate extends SimpleRobot {
    
    RobotDrive chassis = new RobotDrive(1,2);
    Joystick mainStick = new Joystick(1);
    Jaguar jaguar = new Jaguar(3);
    Jaguar jag = new Jaguar(4);
    Compressor compressor = new Compressor(1, 1);
    Relay spike = new Relay(4);
    
    public void robotInit() {
        compressor.start();
    }

    public void autonomous(){ 
        chassis.setSafetyEnabled(false);
        chassis.drive (-0.5, 0.08);
        Timer.delay(2.0);
        chassis.drive (0, 0.0);
    }
    
    public void operatorControl() {
        chassis.setSafetyEnabled(true);
        while (isOperatorControl() && isEnabled()) {
            double speed = mainStick.getY();
            double rot = -mainStick.getX();
            chassis.arcadeDrive (speed, rot);
            if (mainStick.getRawButton(3)){
                jag.set(1);
                jaguar.set(-1);
            }
            else if (mainStick.getRawButton(4)){
                jaguar.set(-1);
                jag.set(1);
            }
            else{
                jaguar.set(0);
                jag.set(0);
            }

            // Solenoid
            {if (mainStick.getRawButton(7))
                spike.set(Relay.Value.kForward);}
        }
    }
}

Did you try it without any case statements around the relay set? I’m trying to remove the controller from this as well.

package edu.wpi.first.wpilibj.templates;
 
import edu.wpi.first.wpilibj.Jaguar;
import edu.wpi.first.wpilibj.Joystick;
import edu.wpi.first.wpilibj.RobotDrive;
import edu.wpi.first.wpilibj.SimpleRobot;
import edu.wpi.first.wpilibj.Timer;
import edu.wpi.first.wpilibj.Compressor;
import edu.wpi.first.wpilibj.Relay;
 
public class RobotTemplate extends SimpleRobot {
    
    RobotDrive chassis = new RobotDrive(1,2);
    Joystick mainStick = new Joystick(1);
    Jaguar jaguar = new Jaguar(3);
    Jaguar jag = new Jaguar(4);
    Compressor compressor = new Compressor(1, 1);
    Relay spike = new Relay(4);
    
    public void robotInit() {
        compressor.start();
    }

    public void autonomous(){ 
        chassis.setSafetyEnabled(false);
        chassis.drive (-0.5, 0.08);
        Timer.delay(2.0);
        chassis.drive (0, 0.0);
    }
    
    public void operatorControl() {
        chassis.setSafetyEnabled(true);
        while (isOperatorControl() && isEnabled()) {
            double speed = mainStick.getY();
            double rot = -mainStick.getX();
            chassis.arcadeDrive (speed, rot);

            // Solenoid
                spike.set(Relay.Value.kForward);
        }
    }
}

I tried it with the new code that you posted but that did not work out either. No change in light on the spike or change in the system

In this pic it looks an awful lot like the PWM cable into the Spike is backwards. Make sure the black wire is near the “B” on the casing. Also check the Digital Sidecar end of this wire, to make sure Black lines up with (-) (Red with + and White with SIG).

Edit: According to your code, it should be in Relay output 4. Also note: the socket in Spikes and Victors can be a little funny - make sure the connector is properly and snuggly seated all the way down into the socket.

The solid orange light on the spike means it’s being commanded to do nothing, which is certainly the case if there is no proper signal coming in.