Solenoid code not working, compressor issue when solenoid keys are activated?

Our team is figuring out compressor code and the hardware was all set up at the correct ports etc. The main issue is that, upon turning on and enabling the robot, the compressor starts and the valve turns on. So then, naturally, we press the button 11 or 12 to pull out or pull in the hatch. But this basically crashes the roborio and turns off the compressor. We have no idea what is going on, and we’re mostly sure that this is a hardware error. We just want to know if this code works since this is our first year using a pneumatics system.

This is for the ball shooter by the way!

Would really appreciate any help. Program down below:

Compressor compressor;
DoubleSolenoid hatch;
DoubleSolenoid habFront;
DoubleSolenoid habBack;
private Joystick joystick;

public Solenoid() {
    compressor = new Compressor();
    hatch = new DoubleSolenoid(0, 1);	
    habFront = new DoubleSolenoid(2, 3);	
    habBack = new DoubleSolenoid(4, 5);	

public void hatchSolenoid() {
    if(joystick.getRawButton(12) == true && joystick.getRawButton(11) != true)
    else if(joystick.getRawButton(11) == true && joystick.getRawButton(12) != true)

public void habSolenoidFront() {
    if(joystick.getRawButton(6) == true && joystick.getRawButton(4) != true)
    else if(joystick.getRawButton(4) == true && joystick.getRawButton(6) != true)

public void habSolenoidBack() {
    if(joystick.getRawButton(5) == true && joystick.getRawButton(3) != true)
    else if(joystick.getRawButton(3) == true && joystick.getRawButton(5) != true)

ALSO please note that we have no programming mentors this year, so we are trying to figure this out as best as we can but are struggling technical terms and general usage, etc. But thank you.

What error messages do you get from the crash?

There are no error messages but instead the roborio just shuts off for a few seconds and the teleop is disabled. So we manually have to enable it again and the compressor starts up. But after pressing a button, such as 11 or 12, the hatch doesn’t move and the roborio shuts off?

Does the roboRIO actually reboot (status light on the roboRIO will go yellow for a moment), or does the Driver Station Communications light stay lit and the Robot Code light goes red, before coming back to green after a few moments ( a user code crash and restart)?

It sounds like you’ve programmed a crash button.
Usually that’s because the device being referenced in the routine is not within the scope.

Is there any particular reason you’re starting multiple compressors?
Normally, no compressor code is necessary at all.
I’d comment out every compressor statement you have in there.

Yes, it rebooted. The reboots took around 2-5~seconds to happen and we had to completely re-enable the robot to restart everything.

We are basing our code off of some source code found online, and we have 3 pneumatics to control the hatch panel. As for the multiple compressors, what do you exactly mean by that? The code was formatted that way, so we kept the compressors that way.

Also, if we comment out every compressor statement, then how would the compressors start to begin with? And how do we control pressure overload?

Sorry about all of these questions but we really would like to figure pneumatics out to some extent by Wednesday.

Really appreciate the help!

roboRIO reboots take 30 seconds, so what you are seeing is a user code crash and restart.
When the user code crashes, the Driver Station automatically switches into Disable mode.

The compressor calls are not required, because any creation of a solenoid includes starting the compressor deeper within the call.
The pressure overload is handled automatically by the PCM. It doesn’t want to depend on user code operating properly for safety.

Ah, I see. Thank you. But since we have 3 solenoids, does that mean 3 compressors are being started? Also, then how would we ever turn the compressors off, for example for overheating issues or pressure overloads?

Also, the weird thing is that there was no crash error message or warning message

And alright, good to know the pressure overload is not much of an issue

if you call a solenoid command the PCM will automatically turn on an off the compressor to maintain pressure while the robot is enabled, as for presure overload, that is a purely mechanical system, no code is involved, if the pneumatics over-pressurize, then the safety relief should auto bleed or blow off, depending on the type you have, any excess pressure

The library checks to see that a compressor object already exists and doesn’t create additional ones.

You can create a single instance of a compressor if you want to stop it from running, but you don’t directly control when it does run. That parts automatic.

So if I remove the compressor, should the code work find when called by the joysticks?

Sadly, we put away to robot to end the day of testing 30 minutes ago, but hopefully we’ll be able to test soon. Just wanted to know if the code itself seems fine. We really have no experienced programmers to check it for us

Alright, that’s great to hear, thank you

I think your code will still crash as it is.
Your subroutines likely cannot see the solenoid objects you created.

Sorry but could you please elaborate on that? We’re a bit confused as to how to solve that. We’re mostly just instantiating the object and that we’re not really used to other solutions. So far, it seems to have successfully created a solenoid object and we can call methods for it.

Is what you posted all of your code?

If it is, where is your joystick created?

I may have missed it, but it looks like you have not instantiated your joystick object by calling

joystick = new Joystick(<port number>);

I would expect this to crash your code. You should instantiate your joystick in the Solenoid constructor.

Edit: sniped

But your answer was more complete :slight_smile:

1 Like

That is all the code in the class file.

We just realized that we haven’t instantiated a joystick though.

Should we add something like joystick = new Joystick(0); ?
Would this conflict with the same instantiation in the file?

*Initialization ??? Can’t keep up with all of these terms :thinking:

Ah, in that case you should not create a new Joystick in your Solenoid class. There are a couple ways you could access the joystick from your Solenoid class however I would pass the joystick object to the Solenoid object in the constructor.

1 Like

Ah, I see, that would make sense. So use it as a parameter?

Also, would it be better to have all of these methods in a class or would it be better to have everything in the file as its own big method? If that makes sense, this was one of the ideas we had

You could check your joystick buttons in, and then call functions in the Solenoid object, that way you don’t have all your code in a big mess in one place, and also won’t have to worry about passing the joystick around.