I’m currently working on programming a P-Loop, and intend on working up to a PID-Loop. I have almost completed the P-Loop, but I have run into some problems. My program is included at the bottom of the post. I have two main questions.

What problems are present with my program? I’m 99% sure there are quite a few.

I’m having trouble testing for the kP value. The performance is not consistent. I can have the kP set to one thing, run the program, run the program again without changing anything, and get wildy different results. Is this normal? I can’t find an ideal kP value if it isn’t consistent.

If you’re getting wildly different results with each test, I would recommend you put yourself in the mind of your program, like a line-by-line debugger:

The target ({setpoint}) is a potentiometer reading of -600 ]. The {kP} constant is 0.0289996 ].

The current potentiometer reading is c ], therefore {error} is currently -600 – c ].

{power} must then be set to error × kP ].

Is {power} greater than 127? If so, {power} is now 127.

is the problem, work backwards through the logic of your P-loop:

When the arm is at its lowest point, where the potentiometer value is x ], and the target position is the highest point, with a potentiometer value of y ], the motor should run at power level p ]. Therefore, {kP} should be p ÷ ( y – x ) ].

When the arm is at its highest point, where the potentiometer value is y ], and the target position is the halfway point, with a potentiometer value of z ], the motor should run at power level q ]. Therefore, {kP} should be q ÷ ( z – y ) ].

…and so on.

Case-specific tips:

Use the RobotC debugger or LCD to get realtime sensor values, if not also other data.

If your arm experiences different gravitational effects at different positions, you may want to consider using a modified P-loop instead, with different

kP

values for different cases (going up or down, position thresholds, etc.).

Vex potentiometers in robotC return values from 0 to 4095 while moving through ~265 degrees of rotation. Your target is a negative number, and therefore cannot be attained.

A P Loop is a programmed Proportional controller. It uses the error between a desired and current value to modify a parameter through the mutiplication with a constant ‘Kp’ that is then added to the parameter to help reach that value. In the case of a fly wheel, you try to reach a certain angular velocity, by modifying the ‘power’ the motors get.

An I controller is an integral controller. It takes the previous integral sum from the last loop iteration and adds it to the current loops (error times time passed since the last loop iteration) (dt). A constant ‘Ki’ is then used to modify the controlling parameter through mutiplication with the total error and then added to the parameter.

A D controller is a differential controller, that takes the previous error Minus the current error, divided by the time passed between those two errors to derive the rate of change. The controlling parameter is than changed by multiplying a constant ‘Kd’ to the rate of change and that product to the parameter.

Think of K as a parameter that converts the answer from each controller into a value in the order of magnitude of the controlling variable.

A P Loop is therefore simply a PID loop without an I or D controller.

Thank you for your information, however I don’t seem to understand the significance of “Kp” in the proportion portion of a PID loop. My guess is that it changes speed and accuracy, but that could be wrong.

You’re driving the motor with power proportional to how far off it is. But that doesn’t mean the sensed value is the value you want the motor to have. For example, let’s say you and a friend have identical robots and want them behaving the same. One of you measures a distance in mm and the other in cm. Then the one measuring in cm will get 1/10 the measurement of the other. So the one measuring in cm will have to have a Kp that is 10 times the Kp of the other robot so they’re supplying the same value to be applied to the motor.

More practically, try adjusting it. Find the value that works best.

I’ll add one more point to this. Yes, Kp is a unit conversion. However, it’s important to remember that there may be various desirable values of Kp, depending on preferred behavior: fast convergence, less oscillation, less jerk.