Go to Post If chairs really inspired me that much I would be in woodcrafting club and not robotics - CJ_Elliott [more]
Go Back   Chief Delphi > Competition > OCCRA
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 10-07-2009, 09:26 AM
parakeets11 parakeets11 is offline
Registered User
None #1213
Join Date: Oct 2009
Location: Bloomfield TWP, MI
Posts: 1
parakeets11 can only hope to improve
[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.
Reply With Quote
  #2   Spotlight this post!  
Unread 10-08-2009, 06:09 PM
apalrd's Avatar
apalrd apalrd is offline
More Torque!
AKA: Andrew Palardy
FRC #0033
Team Role: Mentor
Join Date: Mar 2009
Rookie Year: 2009
Location: Clarkston, MI
Posts: 1,352
apalrd has a reputation beyond reputeapalrd has a reputation beyond reputeapalrd has a reputation beyond reputeapalrd has a reputation beyond reputeapalrd has a reputation beyond reputeapalrd has a reputation beyond reputeapalrd has a reputation beyond reputeapalrd has a reputation beyond reputeapalrd has a reputation beyond reputeapalrd has a reputation beyond reputeapalrd has a reputation beyond repute
Re: [OCCRA]: Potentiometer Code

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: ");
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.
Kettering University - Computer Engineering, 2018
Williams International - Commercial Turbofan Engines - Controls and Accessories
FRC 33 - The Killer Bees - 2009-2012 Student, 2013-2014 Advisor, 2018+ Mentor
VEX IQ 3333 - The Bumble Bees - 2014+ Mentor

"Sometimes, the elegant implementation is a function. Not a method. Not a class. Not a framework. Just a function." ~ John Carmack

Last edited by apalrd : 10-08-2009 at 10:04 PM.
Reply With Quote

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
[OCCRA]: VEX Master Code apalrd OCCRA 6 10-10-2009 10:31 AM
Code for use of a potentiometer wonderdog5 OCCRA 2 11-06-2002 11:15 PM
Code for Potentiometer wonderdog5 Programming 4 11-06-2002 03:19 PM

All times are GMT -5. The time now is 07:16 PM.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.

Powered by vBulletin®
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi