Pneumatics allocation

my pneumatics is wired correctly but when i try to create a new double solenoid object, it says that the ports that my wires are in are already allocated. can anyone help with this?

Please share your code, ideally a link to github (or similar)

subsystem:

package frc.robot.subsystems;

import edu.wpi.first.wpilibj.Compressor;
import edu.wpi.first.wpilibj.DoubleSolenoid;
import edu.wpi.first.wpilibj.PneumaticsModuleType;
import edu.wpi.first.wpilibj.DoubleSolenoid.Value;
import edu.wpi.first.wpilibj2.command.SubsystemBase;

public class testPiston extends SubsystemBase {
    DoubleSolenoid solenoid = new DoubleSolenoid(PneumaticsModuleType.CTREPCM, 4, 5);
    Compressor compressor = new Compressor(0, PneumaticsModuleType.CTREPCM);


    public testPiston() {
    }

    public void startCompressor() {
        compressor.enableDigital();
    }

    public void setUp() {
        solenoid.set(Value.kForward);
    }

    public void setDown() {
        solenoid.set(Value.kReverse);
    }
}

Command:

package frc.robot.commands;

import java.util.function.BooleanSupplier;

import edu.wpi.first.wpilibj2.command.CommandBase;
import frc.robot.subsystems.testPiston;

public class enablePiston extends CommandBase {
    testPiston tPiston;
    BooleanSupplier aB;
    BooleanSupplier bB;

    public enablePiston(BooleanSupplier a, BooleanSupplier b) {
        aB = a;
        bB = b;
        tPiston = new testPiston();

        addRequirements(tPiston);
    }

    @Override
    public void initialize() {
        tPiston.startCompressor();
    }
    
    @Override
    public void execute() {
        if (aB.getAsBoolean()) {
            tPiston.setUp();
        } else if (bB.getAsBoolean()) {
            tPiston.setDown();
        }
    }
}

this is the error i’m getting:

ERROR  1  Unhandled exception: edu.wpi.first.hal.util.AllocationException: Channels 4 and 5 already allocated 
frc.robot.subsystems.testPiston.<init>(testPiston.java:10) 
ERROR  1  The startCompetition() method (or methods called by it) should have handled the exception above.  

You’re creating an instance of the testPiston subsystem in the command. You’re also likely creating an instance of testPiston subsystem somewhere else (probably in RobotContainer, or possibly another instance of the enablePiston command). This is what causes the exception.

This may be helpful:

1 Like