Solenoids in Java

I can’t really find anything on how to properly code solenoids in java all ive got for solenoids is

m_solenoid.set(m_driverController.getRawButton(kSolenoidButton));

if (m_driverController.getRawButton(kDoubleSolenoidForward)) {

  m_doubleSolenoid.set(DoubleSolenoid.Value.kForward);

} else if (m_driverController.getRawButton(kDoubleSolenoidReverse)) {

  m_doubleSolenoid.set(DoubleSolenoid.Value.kReverse);

}

Seems like a reasonable start.

If you are looking for some documentation or small examples…
Have you looked through the WPIlib docs on Double solenoid’s?

would something like this possibly work

if (m_driverController.getRawButton(kDoubleSolenoid)) {

  m_doubleSolenoid.toggle();

}

if (m_driverController.getRawButton(kSolenoidButton)) {

  m_solenoid.toggle();

}
1 Like

Depends on what you want the solenoid to do, and whether this code is getting run periodically or not.

Keep in mind every time toggle() is called, the solenoid will change state. If this code is being called in teleopPeriodic, and a button is pushed, this will switch the cylinder position every 20ms. I suspect this is not the behavior you are looking for

However, general advice: always work methodically. Start by defining what you want the behavior to be, find functions which help you get closer to that behavior, and check whether the code you write matches your requirements. Guess and check has its place, but is often inneficent to quickly write functional code.

2 Likes

ok how would i go about fixing it i just need the solenoids to fire on and off with a button like press the button and they stay either open or closed like a pinch and the single to lift

1 Like

So the next steps I would take:

Start by reading the docs and examples to figure out what “tools” wpilib provides you to solve the problem.

For example, calling the .set() method will change the state of the solenoid. Not calling any .set() method will leave the solenoid in its previous state.

With that in mind, how might you use if statements and button states to only call .set() when appropriate?

so this is what i currently have for the solenoids

  // Solenoids
  // Defaults Double Solenoid Pinch Closed
  m_doubleSolenoid.set(Value.kReverse);
  // Defaults Single Solenoid Lift Up
  m_solenoid.set(true);
  // Opens Double Solenoid Pinch
if (m_driverController.getRawButton(5)) {
  m_doubleSolenoid.set(Value.kForward);
}
  // Closes Double Solenoid Pinch
 if (m_driverController.getRawButton(3)) {
   m_doubleSolenoid.set(Value.kReverse);
 }
  // Raises Single Solenoid Lift
if (m_driverController.getRawButton(1)) {
  m_solenoid.set(true);
}
  // Lowers Single Solenoid Life
if (m_driverController.getRawButton(2)) {
  m_solenoid.set(false);
}

will this work properly???

Presumably this code will end up inside teleopPeriodic?

If that’s the case, this code will be getting run ~50x per second.

So it’s good to keep in mind that the “default” positions you have identified will be re-asserted many times a second.

I would say if you move the code that asserts default positions off into the robotinit method, and leave the remaining code in teleopperiodic, your code would make sense to allow toggling the solenoid positions with the different buttons referenced.

The question of will it work is harder to answer because we don’t have the complete robot code or info on how the robot is wired/plumbed, nor a notion of what behaviors/controls you would deem acceptable to solve your problem.

One way you can answer the question for yourself is to execute your code in simulation mode right on your computer. This will allow you to check if what you expect should happen actually does once the code is running.

There’s info here on how to setup and execute your code in sim:

You won’t see a robot do stuff, but you can verify there are no compilation/runtime errors, and then see what happens to your solenoid output states whe. You press the buttons on your keyboard or a real joystick plugged into your computer. It a great stepping stone to verifying your expectations when you don’t have a robot sitting in front of you to deploy code to

so the double solenoid appears to have them set so one opens and one is closed when the button is pressed how would i fix this so either both open or both close

I think what you’re saying is that when you press button 5 and 3 you see the two outputs for the double solenoid toggling, but only one output is energized at a time?

The above is how kForward and kReverse states of a double solenoid are intended to work. There are two other states the dounle solenoid can be in, kOff (where both solenoid channels are powered off) and kOn (where both solenoid channels are powered on).
Aside: It looks like kOn isn’t a valid value for a doublesilenoid, it is for a relay device, I may be misremembering it as a possible output option or it was a modification to the library to discourage people using it (see below).

This video may be helpful for visualizing how a pneumatic 5/3 double solenoid valve physically works to port the air to the cylinder:

Note that if you energized both of the coils in the double solenoid, you wouldnt actually cause motion of the valve cartridge. Air wouldn’t be routed (ported) differently, so no resulting motion of the cylinder would occur.

If this is the kind of pneumatic valve you’re using on your robot, you probably don’t actually want both outputs to be energized on simultaneously.

tomorrow morning I’ll be able to test the code on the robot so I’ll do that then get back to you because i dont really know exactly what kinda valves we got going on.

This video might be a better one to follow.

I’d forgottent that center blocked valves are not typically used (against rules iirc) in FRC, the valve on your robot likely only has two states it can be in (vs the 3 positions illustrated in the video above)

If you want to take pictures of the mechanism, valve(s), plumbing, I’m sure myself/others can help explain what’s going on further.

Here are the solenoids on the robot the pinch are both attached to the same solenoid

They look like they are both double solenoids to me. The way you can tell is a double solenoid would have coils (where the wires plug in/connect to the valve assembly) on both ends. A single sole oid just has one coil on one end, which makes it look asymmetrical.

In that style of SMC valve,
A single solenoid looks something like this:

A double solenoid looks like this:

Can you explain why you were using single vs double in your code?

It was just an error I realized after looking they were both double and fixed the code accordingly rn I have to rewire the pneumatic controller

Pneumatics are working flawlessly now

1 Like