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!
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?
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.
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?
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.
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.
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.
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.
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!
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:
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.