Working with Potentiometers

Our team tried to test operating a van door motor using two pots – one for feedback and one for control. Unfortunately, we couldn’t get the motor to drive in the direction of decreasing the error. No one can figure out what’s wrong. Here’s the code:
:confused:
if stack_pos - sw_stacker > 10 then pos_error
if abs(stack_pos - sw_stacker) < 10 then done_stack
neg_error:
error = (sw_stacker - stack_pos)
sw_stacker = 127 - (4 * error) / 10)
pos_error:
error = (stack_pos - sw_stacker)
sw_stacker = 127 + (4 * error) / 10)
:confused:

Here are our pots:
PN 3825A-282-104A

sw_stacker = 127 + (4 * error) / 10) has parentheses problems :frowning:

Could you elaborate how the pots are connected physically and electrically?

Also, PBASIC does not handle negative numbers as you might expect. Pad your values so that you don’t perform math in the negative range.

For starters, try putting a “goto done_stack” immediately before the pos_error label. Otherwise, the code in the neg_error section will “fall-through” and execute the pos_error code also. However, without knowing what variables correspond to what, there’s not a whole lot I can tell you.

Believe it or not, the lack of parens was actually a copying error. I overlooked one.

Pertaining to negative numbers, I believe switching the sensor value and target value takes care of that problem.

The sensor pot is hooked up to the analog inputs, aliased to sensor1. The pot controlling the motor is on port 4, aliased to p4_x. All wires are 24 gauge.

stack_pos – sensor potentiometer
sw_stacker – OI input pot

See Rob’s previous post…There’s still the fall-through issue.

I might be misunderstanding the issue, but it seems you are trying to make a servo, i.e. the motor turns until the feedback pot and the control pot give the same value.

[edit]Your code seems to be unnecessarily complex, [/edit]
…wouldn’t

if feedback_pot < control_pot then [turn motor one way]
if feedback_pot > control_pot then [turn motor other way]

work for this?

[edit]
You could tweak the if…then statements a bit to allow slight error:

if feedback_pot+255 < control_pot +245 then [one way]
if feedback_pot+255 > control_pot +265 then [other way]
[/edit]

With regards to your code, though, does sw_stacker get sent to the right PWM by the end of the loop?

[edit - I’m bad with the edits today…]

Let’s say stack_pos=1 and sw_stacker=100

if stack_pos - sw_stacker > 10 then pos_error

Will this go to pos_error or not?
1-100= -99, which to the controller might be -99 or 157, depending on how it handles negative numbers in this situation.

[/edit]

It will be interpreted as being 157, not -99.

** if stack_pos - sw_stacker > 10 then pos_error**
This statement is fine as long as stack_pos is greater than sw_stacker; and the jump will be executed when stack_pos is more than 10 greater than sw_stacker. BUT if stack_pos is less than sw_stacker, then the result of the subtraction will be negative, and since the BASIC Stamp does UNSIGNED comparisons, THE JUMP PATH WILL ALWAYS BE TAKEN. It would be safer to say “if stack_pos > sw_stacker + 10 then pos_error”.

** if abs(stack_pos - sw_stacker) < 10 then done_stack**
To reiterate, this test will only be made when stack_pos is between sw_stacker and sw_stacker +10. And this jump will ALWAYS be taken UNLESS stack_pos EQUALS sw_stacker + 10.

Thus, the following section will only be executed when stack_pos EQUALS sw_stacker + 10:
**
neg_error:
error = (sw_stacker - stack_pos)
sw_stacker = 127 - (4 * error) / 10)
**
… and this, only when stack_pos IS GREATER THAN sw_stacker + 10:
**
pos_error:
error = (stack_pos - sw_stacker)
sw_stacker = 127 + (4 * error) / 10)
**
I don’t think this is what you intended.:wink:

This should do more like what you expected:


	if abs(stack_pos - sw_stacker) < 10 then done_stack
	if stack_pos > sw_stacker + 10 then pos_error
neg_error:
	error = (sw_stacker - stack_pos)
	sw_stacker = 127 - (4 * error) / 10)
pos_error:
	error = (stack_pos - sw_stacker)
	sw_stacker = 127 + (4 * error) / 10)

Now, I’m not sure whether your code for neg_error and pos_error is correct, but this should get you closer, at least.

If the code that everyone has suggested does not fix it ask yourself, are the pots are wired correctly? We ran into this problem last year when the pot on OI had wires to all three of the tabs, when only two should be wired. Refer to pages 13 and 15 in the Robot section of the manual.
Good Luck

*Originally posted by n[ate]vw *
**Let’s say stack_pos=1 and sw_stacker=100

if stack_pos - sw_stacker > 10 then pos_error

Will this go to pos_error or not?
1-100= -99, which to the controller might be -99 or 157, depending on how it handles negative numbers in this situation.
**

why dont you just take a page out of innovationfirst’s book and their basic programming to avoid negatives. try something like this:

if (((stack_pos + 100 - sw_stacker) Min 100) - 100) > 10 then {whatever}

anything that would be a negative number is instead turned into a zero, and all this code is looking at is whether it is less than 10 or not, not by how much it is less than ten.

-Anthony

P.S. i dont know the range of your values, so iff 100 is too little or too much, you can change it accordingly