Lesson 8: Potentiometers and Analog Input
Let's learn about about analog inputs. A common use of an analog input is potentiometers (informally called a pot). In the context of robotics, a pot is usually used to determine what angle a mechanism is at. Another really cool use of a pot is in a string potentiometer. Code Red has used these for the past several years, and we highly recommend your team look into them if they haven't already. You can purchase them on AndyMark, or make them yourself for a little bit cheaper. Basically they are 3D printed enclosures with a retractable key chain wound around inside + a spring + a potentiometer. You can use the potentiometer to measure how far the string is pulled out.
We can use the
AnalogPotentiometer class to read from a
potentiometer. I'm only going to go over the basics of this object, but you can
read the full documentation here
if you want. Let's get down and dirty... declare and instantiate an
Just like a
DigitalInput, we can use the
method to get the state of the potentiometer. This time,
going to return a double instead of a boolean. This double represents the
current reading of the potentiometer. Say that we have a pot attached to an
arm, and are using it to get the angle. We could do something like this:
If you need to know where a certain manipulator is, a limit switch is not always practical. In these cases, a potentiometer is usually needed, so I want you to practice with a real situation. I've actually already covered all I need to say for potentiometers. I am going to leave an exersise below, that you should really try, that modifies the above code.
It's worth noting that an
get() method does not return an angle when it is setup like we
have set it up above. Rather, it just returns the value of that Potentiometer's
analog input. Usually this value ranges from 0 - 5ish. You actually can setup a
potentiometer to return an angle, but you need to use a different constructor.
Measure the analog value of your potentiometer when it is at full range. You
can then use this constructor which will make the
scale its output to the appriate degree value:
official documentation for the
AnalogPotentiometer class for
I do want to mention the
AnalogInput class that the WPI Java
Library offers. You should use that if you need to get analog input but are not
using a potentiometer. I'm actually going to talk about that more in the next
lesson if you need more detail.
- As mentioned earlier, a potentiometer can be used to track the position of an object. Setup a potentiometer that will only allow a motor to move within a predetermined boundary.
- Setup a potentiometer that can adjust a motor's speed output.
Using Code Red's Robot Library
Refer to the last lesson's "Using Code Red's Robot
Library. The answer here is exactly the same, just use a
VirtualizableAnalogInput to replace