Limit an action in Java

I’m trying to set it in code so if the lift is up and the pinch is open the lift cant go down to protect some cables heres the code i was wanting to try and use

 if (m_liftSolenoid = kForward) && (m_pinchSolenoid = kForward); {

I think your problem is just the if statement syntax, try:

if (m_liftSolenoid == kForward &&      
m_pinchSolenoid == kForward) {

so the issue with that is kForward cant be resloved to a variable

Three things:

  • Notice how you specify kForward in the set()… Value.kForward
  • The semicolon after the “if” is errant… “if (condition) do nothing” is what your code essentially says
  • Juicestus is correct… your use of the single “=” is to use it as an assignment operation not a comparison. It always returns true.

I hope this helps!

1 Like

import static edu.wpi.first.wpilibj.DoubleSolenoid.Value.*;

Use this line to import kForward and others.
The static is optional, but without it you have to call the using Value.kForward

now i have an Incompatible operand types DoubleSolenoid and DoubleSolenoid.Value error on the solenoid == kForward portion

For the future:
When you get a _______ can't be resolved to a variable error in java, it means that variable you are trying to use does not currently exist. This could mean a few things:

  1. You never instantiated the variable you meant to use
  2. You instantiated the variable, but used the wrong name
  3. You instantiated the variable, but within a different scope, so garbage collections deleted it too soon.

Not all variables are accessible throughout an entire program. Scope is how long a variable is “alive for” and can be accessed. In java, a variables scope is often within the coding block (denoted by { }). Once the coding block is finished, the variable is no long accessible. There is more to it than that, but that’s a high level overview.

Garbage Collection

Java automatically deletes variables that should no longer be in scope via a process called garbage collection. It’s beneficial because you don’t have to think about it (like you do when writing in C) but it also take extra time than if you had written it only when you needed to use it. This is one of the trade-offs between specific languages, and why some may choose to write in on over another.

In this case, you used the incorrect naming convention, calling it just kForward rather than Value.kForward

What do you think could have caused this issue? Have you tried googling related issues to see what causes incompatible operand types?

Edit: I do know what your issue it. I’m 99% sure there is someone else who knows too. However, since you don’t, I want to walk you through the process of figuring it out so you can learn how to debug for the future.

1 Like

Remember m_liftSolenoid is an object of type DoubleSolenoid.

You probably want to use one of it’s methods that will get it’s state. You can them compare the state that it’s in, against the state you care about (DoubleSolenoid.Value.kForward).

The javadocs for the DoubleSolenoid class are here: DoubleSolenoid (WPILib API 2020.3.2-60-g3011ebe)

That said, going from code you’ve posted in other threads, I think to achieve the behavior you’re after (prevent an action when a component is in a state) would be more cleanly achieved by adding some logic into the if statement that lowers the lift solenoid.

Presently it is something like

If button is pressed
  Lower lift solenoid

But you could change that to something like the following to create the behavior you’ve described in the first post.

If button is pressed and pinch solenoid is not open
  Lower lift solenoid

ok how would i figure it out?

how do set it so it properly gets the state of the solenoid

Read the docs. Here’s a link to the get() function: DoubleSolenoid (WPILib API 2021.3.1)

i have i cant find the values im trying to find

What values? You’re being really vague.

Go to the link I posted and look for the method that gets the solenoids state. You’re looking for a method that returns to you a value of type DoubleSolenoid.Value. you can then compare that in your if statement with operators like== or !=

You’ll get better feedback if you take the time to explain what you’ve tried, what you’re confused about, what errors you’re getting, etc. People are more willing to put the effort into providing you help if they can see you’re putting in the effort on your end. Not saying you aren’t, it’s just hard to sus out if you post brief one-liners.

so then something like this would work to prevent the lift from lowering while the pinch is open

if ((m_liftSolenoid.get() == kForward) && (m_pinchSolenoid.get() == kForward)); {

There are lots of basic syntax errors and mistakes in your code. Are you familiar with Java programming already?

Trying to program an FRC robot without knowing the language can be really difficult and lead to you being unable to fix crucial parts of your code at a competition field, leading to a robot dead on the field.

You can’t build a robot without first knowing how to use a hammer, or a drill, or a lot of other tools. You get the gist.

Zero to Robot introduction offers some links to learning the language. I’d give them a look.

1 Like

What is the difference between kForward and Value.kForward?

Well, it is kinda like giving your street number (123) without giving the street address (Cherry Lane). 123 = kForward, Cherry Lane = Value and Value.kForward = 123 Cherry Lane.

Well now… I left out the city, state and zipcode. Remember the imports at the top of the Java file? They allow the compiler to find/imply the “city, state and zipcode” in this case the fully qualified address is “edu.wpi.first.wpilibj.DoubleSolenoid.Value.kForward”.

Just a suggestion… the following is a great way to learn Java if you don’t have mentor readily available to help you.

yea i know the point of value before hand and i know pretty basic code like im pretty good at reading and understanding and debugging code the thing is this is my first attempt at fully building code from scratch and i just dont know all of the smaller things and doing this has been really helpful to figuring my way around the frc coding

Hey! you are doing an amazing job and I applaud your dedication and resourcefulness. We are all here to support you.

I can see that you are a “go fast” person and I can respect that. I also see you are a hands on learner and I can relate to that…

I think you are missing a few fundamentals and think if you invest a few cycles you’ll end up going even faster…

1 Like