How to program double solenoids

I tried a few different code to try to run my double solenoids. My compressor is working fine but I don’t know why my solenoids are not working. I might have problems in my code too but I couldn’t figure it out. Here is my code:

import edu.wpi.first.wpilibj.Compressor;
import edu.wpi.first.wpilibj.DoubleSolenoid;
import edu.wpi.first.wpilibj.Joystick;
import edu.wpi.first.wpilibj.TimedRobot;

public class Robot extends TimedRobot {
private static final String kDefaultAuto = “Default”;
private static final String kCustomAuto = “My Auto”;
private String m_autoSelected;
private final SendableChooser m_chooser = new SendableChooser<>();

private final Joystick Drivestick = new Joystick(0);
private static DoubleSolenoid solenoid_Double = new DoubleSolenoid(0, 1);
private static Compressor compressor = new Compressor(1);

solenoid_Double.set(DoubleSolenoid.Value.kOff); solenoid_Double.set(DoubleSolenoid.Value.kForward);
solenoid_Double.set(DoubleSolenoid.Value.kReverse);

boolean enabled = compressor.enabled();
boolean pressureSwitch = compressor.getPressureSwitchValue();
double current = compressor.getCompressorCurrent();

public void teleopPeriodic() {
if(Drivestick.getRawButtonPressed(4) == true) {
solenoid_Double.set(DoubleSolenoid.Value.kForward);
}

else if(Drivestick.getRawButtonPressed(2) == true) {
  solenoid_Double.set(DoubleSolenoid.Value.kReverse);
}

else {
  solenoid_Double.set(DoubleSolenoid.Value.kOff);
}

Anyone has any idea? Thank you!

It might be because you are originally setting the Solenoid to kOff. Maybe take out that line and try again.

Also, I noticed your if statement is a bit redundant.
Instead of (Drivestick.getRawButtonPressed(4) == true) you can just put
if (Drivestick.getRawButtonPressed(4)) { and that will also work.
The .getRawButtonPressed function returns a boolean value so there is no need to test it against another boolean value

It’s still not working after I deployed the code. Do you think I have any other issue in my code?

Off the top of my head, you need to set it to kForward and kReverse as your two options. kOff will just disable both solenoids. Based on the way a double solenoid works, if neither solenoid is powered it will stay in the previously commanded state (kReverse or kForward). So when you released the button, it will just stay in the same position.

Try deleting this:

else {
solenoid_Double.set(DoubleSolenoid.Value.kOff);
}

Not sure if it’ll work but it’s worth a shot.

It could also be an electrical problem, do you have a picture of your wiring?

Is your compressor on a PCM with CAN ID 1? I think the default is 0.

(You don’t actually need to instantiate the Compressor object – the DoubleSolenoid will take care of the compressor loop for you under the hood)

If so, your DoubleSolenoids are using the default PCM ID of 0.

I think the whole business of setting the solenoid to kOff is fine. Activating the solenoid is like pressing a toggle switch. You can press kForward or kReverse and leave them that way, or press kForward and then flip the switch back to kOff. Both will have the same result. The piston will activate on the transition, and nothing will happen whether you keep the solenoid activated or toggle it back to off.

However, if you do set it to kForward and then flip it to kOff again, you may need to add a little delay in the code to give the electronics time to react. That might be why you don’t see the pneumatics activating. Set it to kForward or kReverse and leave it for a good half second or so, see if that helps.

Do you see the LEDs on the PCM solenoid outputs light up?


Here is the pictures of my wiring. I tried to take it as clear as possible but if any part you can’t see I can retake it

1 Like

I tried switching Compressor(1) to (0) but it did not run so I’m not sure what happened. Also, I’m not sure what LEDs are you talking about. I have some pictures in the previous reply. Can you take a look at it and show me which one is it? Thank you.

I’m referring to the red LEDs on the PCM. There’s one over each of the 8 sets of solenoid output wire connectors. Your two solenoid connections are wired to outputs 0 and 1. When your code sets the output to kForward, you should see a red LED light up right above the pair of wires at either output 0 or 1. When your code sets the output to kReverse, the other one should light up. When your code sets the output to kOff, none of them light up.

You say that when you change Compressor(1) to Compressor(0), it doesn’t work. This suggests that maybe your PCM is set to respond to CAN ID 1 instead of the default of CAN ID 0. If that’s the case, then you need to change your DoubleSolenoid instantiation to also reflect that.

Change “new DoubleSolenoid(0, 1)” to “new DoubleSolenoid(1, 0, 1)” and see if that helps. The constructor with only 2 parameters uses CAN ID 0 by default.

You should use the Phoenix Tuner app on your PC to verify what CAN ID the PCM reports itself to be. Make sure, whether it’s 0 or 1, that your code is consistent with the hardware.

I suspect this will resolve your problem. Good luck :slight_smile:

2 Likes