[OCCRA]: Potentiometer Code

Hey I was wondering if anyone had some basic code for the use of potentiometer, or else some basic assistance would be greatly appreciated.
Thanks

Is this for a big robot or a VEX robot? What do you want to do with the potentiometer?

I assume that you want to use a motor to go to a certain position. In that case, the code would go something like: (in MPLAB C)


//Do not simply use this code. 
//Implement things one at a time, starting with joystick=>motor and outputting the sensor to the serial port
//Then see if a positive motor = positive sensor, or if the sensor is inverted
//Then try the code here. Make sure to invert the sensor if it is inversely related to the motor movement.
//After that, add the checking code to prevent something catastrophic from going on.

//Create a "gain"
int gain = 1;

//Get the sensor variable and save it
int pot = Get_Analog_Value(rc_ana_01);
//Convert it from 0-1023 to 0-254 (10-bit to 8-bit)
pot = pot / 4;

//Get the position we want to go to from the first joystick axis
//big robot:
int joystick = p1_x;
//VEX robot:
int joystick = Pwm_in1;

//This is really really simple: 
//we take what we want and subtract what we have to get error
//We set this to our motor. 
//If they are the same, then the motor will not move. 
//If they are close, the motor will go slow.
//If they are very close, they will be with in the "deadband" and the motor will not move.
//The only trick is if the joystick or sensor is inverted. Then just one is 255-var to invert it
//create an int called "error" to store this
int error = joystick - pot;

//multiply by the gain
error *= gain;

//Add 127 to convert this from a -127-+127 to a 0-255 variable
error += 127

//Limit it to 0-254 (two 255's in a row can cause the OI to freak out)
if(error > 254)
{
error = 254;
}
else if(error < 0)
{
error = 0;
}

//set the motor to the error
pwm01 = error;

Basically what I did is I got the joystick and the potentiometer, made them the same range, and subtracted the pot from the joystick. Then I added 127, limited it to the available motor range, and set the motor. The purpose of the gain is so you can turn it up (slowly starting at one; do not increment more than 2 each time you test) to make it go faster. If you turn it up too high, you will overshoot and it will have to go back.

Before you do this, always use the serial output to check and see that moving the motor forward causes the sensor to move in the same direction.


printf("Pot: ");
printf(pot);
printf("/n");

If it does not, invert the sensor after dividing it by 4:

pot = 255 - pot;

This is what is most commonly used with the potentiometers. This is called “Proportional” or “P” control. It is a part of the more-advanced PID control (Proportional/Integral/Derivative).

It would be good programming practice to write checking code that sees if the sensor is out of bounds and limits the motor to go in one direction only, and stops it all together or switches control directly to the joystick if the sensor is too far out of bounds. When we wrote the crab-drive code for our FIRST robot, we did not include this in our early test versions, and the motors were inverted, and all of the pods started spinning around and around, breaking all of the sensors. If the sensor never physically gets to 0, you can assume 0 means the sensor is unplugged. That can happen.

If you went to the workshop (the regular workshop, not the VEX workshop), Jim Zondag taught a class on this. This is the same principle. It would be much easier to explain if I had an example to show. It took me about half an hour to understand once it was explained to me (and I drew some pictures on the whiteboard to understand it).

Any questions? Don’t forget semicolons if you are using C. If you are using EasyC, then the same principle still applies. You just need to apply it.