# Programming the Vex Potentiometer

I was writing some code for a robot that I am building, and then I realized that in order for my robot to work I needed a potentiometer. I don’t have one and I was looking at all of the instructions and some stuff online and then I realized that I have no clue how to write code for this thing. I then went into EasyC and I didn’t have an idea of how to write something for it.

So, could someone please show me some examples of code? Or at least point me in the right direction? Thanks!

I would love to help you but I need a little more info. What are you trying to use the potentiometer for and do you need code for the potentiometer or something else.

You can buy the VEX Potentiometer or for quick experimentation you can get something like a Radio Shack 271-1715 (see post Vex Analog Inputs).

Read the Potentiometer just like any other Analog Device, turning the Potentiometer full one direction will report an Analog Value of ~0, full the other direction will report an Analog Value of ~1023.

The Radio Shack Potentiometer will not Turn 360 Degrees, so don’t use it in that sort of application. The VEX Potentiometer might be limited as well, but I don’t have one to test.

I think it would be similar to any analog sensor coding with some differences. If you explain the reason behind using it, we might be able to help you writing the code !!

What I am trying to do is get my robot to drive in a straight line, so if it veers off course the robot would correct itself. What I really need to do is learn how to read what the potentiometer spits out at me. So let’s say that the robot turns 45 degrees. What would the reading be, and how would I tell the motors to correct for that by going 45 degrees in the opposite direction?

Thanks for all of your help.

Ahh, so you are basically using it to measure an angle.

Plug the potentiometer into an analog port, and when you want to know the current direction, just read the analog value. You’ll get a value from 0 to 1023 that you can easily convert to an angle.

Since you need to measure both degrees-right and degrees-left, you’ll need to subtract 512 from the analog value. This will give you a value from -512 (hard left), to 0 (going straight) to 511 (hard right).

Now to convert that to degrees: The potentiometer has about 250 degrees of measurement, so a very rough approximation is just to divide the value by four. This should give you degrees (+/- about 20%).

So now we have a formula that looks like:

[INDENT]degrees = (analog value - 512) / 4[/INDENT]

If you are going straight, the analog value will be 512 and so the formula above will give you an angle of 0 degrees. It’ll twitter around a bit, but it’ll stay near zero.

If your bot turns by 45 degrees, the analog reading would change to something around 696, which gives and answer of 46 degrees. If you turned 45 degrees the other way, the analog reading would be around 328, yielding an answer of -46 degrees. Not bad. If you need something more precise, you can calibrate your code using a particular potentiometer and get a more exact value.

Once you know what direction you are going, adjusting course is a more challenging problem. This will depend a great deal on your exact drive train and how perfect you need to steering to be. In many ways, the logic resembles what you would do for a line-tracker.

An easy first attempt would be to look at the steering angle to see which side is being driven too fast. Then just slow that side down by some % based on how many degrees of error you are seeing.

This should keep you going approximately straight. If it curves to one side or the other, you can tweak the 512 constant in the formula above, or you can realign the potentiometer itself - it mounts such that you can adjust it mechanically +/- 45 degrees.

More advanced navigation might try to correct for an error by steering to the opposite side for a while to try to counteract the effects of the initial error.

Hope this helps,

• Dean

The Potentiometer (pot) does not measure the direction something is pointed (the way a compass does).

If the pot is mounted on one item and a Vex axle running through the center of the pot is connected to another item. You can tell the angle between the first and second item.

If you mount a pot on your robot and if you want to use it to cause the robot to drive in a straight line; the pot can’t… unless you connect it to another item that corresponds to the second item in my paragraph above.

It seems like you need to think about using a gyroscope, an accelerometer, or a compass; not a potentiometer.

Does this make sense? Did I misunderstand your plans?

Blake

I am curious about how you will connect the Potentiometer.

Since the Vex Potentiometer (like most Potentiometers) can not turn in a continuous direction, you will need to stop it and reverse it at some point.

With Ackermann Steering, the Tie Rod can only move so far each direction, and would be a fine candidate for something like a Slide Potentiometer.

I just assumed he would hook it to a trailing idler wheel (like a traditional caster). As the robot steered off the straight line, this caster would pan left or right a bit to follow. I assumed he would hook the potentiometer up to measure that angle.

Of course, this only works if the robot never backs up, since as MarkO points out, it can’t handle continuous rotation.

• Dean

EUREKA!!!

Look at the “Articulated Steering” section, in this Steering article. That can be done with a Potentiometer.

I understand, but my hunch was that dragging a tail wouldn’t work very well over non-trivial distances.

Agreed. I think he would be better off with a shaft encoder on each drive wheel, and then speeding/slowing the motors to try to keep the counts as close as possible. I was just fixated on using a potentiometer, since that seemed to be the focus of the post.

Cheers,

• Dean

Ah, Quazar… The Nobel Engineer. https://vexforum.com/images/icons/icon6.gif

Trying to solve someones problem, with the Potentiometer at hand, rather than suggest a Shaft Encoder that might be more appropriate for the task before them.

I agree though… “Use the Right Tool for the Job” (and when you don’t have it, then improvise)

If Angus MacGyver didn’t have shaft encoders, he’d use a potentiometer, and if he didn’t have a potentiometer, he’d use some fishing poles. If he didn’t have any fishing poles, he’d, well, you get the idea…

Actually, there are probably cases where a trailing wheel might do better than monitoring the drive wheels. For surfaces where the drive wheels might slip, or perhaps when using tank tracks which can slip a bit during normal operation.

Cheers,

• Dean

I had assumed he was going to mount the potentiometer on top of the steering point. I had done that on a crab drive where the potentiometer was on the swerve drive pivot point. It would work on both the Ackerman and Articulated steering setups.

The orginal post was help on how “steer in a straight line” the bane of ALL robot builders. I’ve never had good luck on matching the two wheel encoders to see which wheel was turning more and driving the robot off track.

We were trying to be helpful as possible. You just never know what the limitations are in creating a solution to a project…

I can relate. Even if your Wheels are the same size and each of them has an Encoder, if there is slippage for some reason, you can actually be off, even when your Encoders say your on.

A secondary feedback reference would be a highly desirable thing, like an Accelerometer or a Compass, or even a Beacon and Sensor to detect the Beacon and direct the Robot toward it.

Quazar and MarkO thank you both so very much! I think that I will just use encoders, but your explanation of how the potentiometer works was extremely helpful. Thanks again!

Just one more question:

I think that I read right that I could not back up because the potentiometer cannot handle continuous motion. Why is this true? Could I just reset the potentiometer every time it corrected? For example; when it goes 45 degrees to the right, and then the robot goes 45 degrees in the other direction could the potentiometer reset? Thanks again!

Boy oh Boy!! Your making me feel OLD!!

Carefully turn the Vex potentiometer or most any potentiometer contentiously one direction, and it will stop (see Cut Away of a Potentiometer ). If you have a chance, open a BAD Potentiometer, and see how it works inside…

If the Potentiometer were connected to a Castor, and the Castor was at a Single Point on the back of the Robot, when you turned the Robot, the Castor would rotate Clockwise when Turning Left and Counterclockwise when Turning Right (as Viewed from above). For the most part, the Castor never makes a complete Turn one direction or the other. When you stop Turning, the Castor then reverses direction so that its direction of Travel is the same as the Front Wheels. This motion would be fine with a Potentiometer.
But if you decided to Back Up, the Castor would try to Turn 180 Degrees (Clockwise or Counterclockwise), and JAM and slide(or Break the Potentiometer).

With Articulated Steering, the rotation at the Pivot Point would less than the Maximum of the Potentiometer. This motion would be fine with a Potentiometer too.

So the Short Answer is no, you can not reset it, it is a Physical Limitation of the Potentiometer.

That was my assertion, and it was based on the idea that you would use a trailing rear-wheel to measure direction. Just so you know what I’m talking about, imagine something like this, but made out of Vex:

http://www.walterproducts.com/casters/Caster_swivel_top_plate.jpg

The potentiometer would mount on the top so that as the wheel changed direction, it would turn the potentiometer.

The issue is that the potentiometer is mechanically prevented from rotation all the way around. It can only turn 250 degrees or so before it hits a mechanical limit and stops turning.

A trailing wheel will normally swivel all the way around (possibly multiple times) as you drive the bot backwards and forwards. Since this would peg the potentiometer to its mechanical limits, it would not give you an accurate reading and it would not be good for the pot.

However, if you never let either drive wheel go into reverse, then the trailing wheel will only ever steer from -90 to 90 degrees. This is within the potentiometer’s mechanical range and should work fine.

Hope this makes things clearer…

• Dean

Ah… I get it now. Sorry if I made you feel old, MarkO. Thanks again, your information helped me tremendously.