Java Potentiometer Start Help

Our team is new to Java. We have a dart with a potentiometer that at a specific value would stop the dart from going farther. I have been reading documentation, but don’t know where to start. Does anyone have code examples similar to this that I could look at?

1 Like

If asking questions about a specific piece of hardware, it’s generally best practice to link to such hardware or at least provide a model number so everyone is on the same page.

Speaking genetically, potentiometers are typically simple analog sensors, so you can wire it to an Analog IO port then use an AnalogPotentiometer object to read the value.

Here’s the relevant page in the WPILib docs as well: Analog Potentiometers - Software — FIRST Robotics Competition documentation

1 Like

Fletch covers the Potentiometer itself. To cover using a sensor to control a mechanism’s position, velocity, etc., look at the PID Controller in WPILib PID Control in WPILib — FIRST Robotics Competition documentation

3 Likes

Right. Made me guess what in the world OP means.

If I guess right what OP wants, that is what they should look at.

The dart travel of 0 to 12 inches doesn’t spin the pot its whole range so adjust the constructor accordingly.

From the specs: The potentiometer will turn 8.76 times during a 12 inch travel)

If it starts at 0 volts is 0 inches, then the constructor would look something like:

AnalogPotentiometer pot = new AnalogPotentiometer(0, 12./8.76, 0.);

// Then OP can code something like:

if(pot < 2. || pot > 10.) stop the motor

If the pot doesn’t start at 0 volts then adjust the 3rd parameter of the constructor.

The 1st parameter of the constructor is the analog input port you are using.

3 Likes

Oh, thank you. I’m sorry my question wasn’t clear, but I think I can do it now. The main issue was that I didn’t have any reference to start coding the potentiometer, and was confused because I don’t have any prior java experience. You all have been very helpful

1 Like

You are welcome. Sorry I forgot the factor of 10. turns for the pot in the constructor. 8.76/10 is 12 inches so divide by that factor to scale it to the full range because 10 turns of the pot would be (almost) 5 volts of the sensor supply voltage.
I also forgot the get()

pot.get()

1 Like

The AnalogPotentiometer class does the units conversion from volts for you by assuming a 0 to 5 volt range and using your 2 arguments on the constructor which are your units value at 5 volts and your units value at 0 volts.

Your pot uses .876 of the 5 volt input to register 12 inches. How many inches would it be at 5 volts? 5./.876

For good style leave the values of conversion constants as raw as possible to avoid them being “magic numbers.” Something like this:

// actuator moves 12 inches to its limit at which point the pot turns 8.76 of its 10 turn limit
double kActuatorConversion = 12. / (8.76/10.); // number of inches if pot turned to its full range

My team does not use this class. We use AnalogInput and do our own units conversion or don’t bother converting - use the volts. For our typical use of a small portion of a multi-turn pots we use a “range sensitivity expander” resistor and use the non-linear range’s most sensitive part. This actuator uses most of the pot’s range so you don’t need to adjust the sensitivity.

Check your system when you code it. I’m just going off the documentation and don’t know sure how this works.

1 Like

Interesting. I will see what I can do with all this information. It may take some time to figure out the analog port though, my only reference at this moment is my teams old labview code.

What potentiometer are you using? I have been kicking around the idea of a string pot for elevators, but I will probably end up sticking with encoders

At this moment, I’m not sure. I don’t have access to our robot until tomorrow. I’m just fishing around for help with general ideas to try to understand. I’m doing all of this without actually knowing Java yet. Most of what I do is based on theories and advice from people that do understand the docs more than me.

1 Like

OP never said but didn’t correct my assumptions. I’m guessing it’s the blue one on the end of this device that they have.

2 Likes

That does look like it, so it is a safe bet

All the onboard analog inputs are marked with the port number so you can use any unused one. If the device is already wired you can trace the circuit from the roboRIO to the dart. If you mean what exactly is old LV code using, I’d have to guess since I’ve never touched LV. LV has menus for devices that you select and then right click to pick the port? You should be able to click on the device and see what was assigned. Sorry, I’m guessing about something you already know but the Java port number isn’t tricky - it’s what’s printed on the roboRIO onboard just like LV uses. Now if you are using the MXP that’s an entirely different matter.

1 Like

I have found that declaring an Analog potentiometer this way leads to the potentiometer “pot” not closing. So a data leak.

I also found that when making an if statement
if(pot>0.36) in particular. I get an error something like “Pot does not transfer to boolean”
Do you know how to fix issues like this?

I had tried to fix my typo with this update.

1 Like

Oh, I see. Sorry, initially I misunderstood

There are several resources that do not close. Since the robot is supposed to run forever and it stops only on fatal error or power off there is no need or good way to close those resources. Heed any messages but you have to decide if the problem can be fixed which in this case it cannot be.
Not sure if you have followed the navigation through the Java code but a better formed Java program not associated with the robot might be like:
Main.java

  • open resources and return to main
  • use resources and return to main
  • close resources and return to main

That’s the compiler’s understanding of normal that it’s basing it’s error message to you.

Our robot code is like:
Main.java

  • open resource, use resources but never return to main
2 Likes

I understand now, sorry to make this post so long

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.