P-Loop programming

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.

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

  2. 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.

My program:

#pragma config(Sensor, in2, potent, sensorPotentiometer)
#pragma config(Motor, port2, arm, tmotorVex393_MC29, openLoop)
//!!Code automatically generated by ‘ROBOTC’ configuration wizard !!//

task main()
{

int setpoint;
setpoint = -600;
float error;
float power;
float kP;
kP = 0.0289996;


while(1==1)
{

	error = setpoint - SensorValue[potent];
	power = error*kP;
	if(power > 127)
	{
		power = 127;
	}
	if(power < -127)
	{
		power = 127;
	}
	motor[arm] = power;

	wait1Msec(25);
}

}

One thing that leaped out at me:


if(power < -127)
{
power = 127;
}

Did you leave out a negative sign?

That is um… That is…

Oops.

I’ll fix that right away. Thanks for pointing it out.

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:

  1. The target ({setpoint}) is a potentiometer reading of -600 ]. The {kP} constant is 0.0289996 ].
  2. The current potentiometer reading is c ], therefore {error} is currently -600 – c ].
  3. {power} must then be set to error × kP ].
  4. Is {power} greater than 127? If so, {power} is now 127.
  5. Is {power} less than -127? If so, {power} is now 127.
  6. Run the arm motor at power ].
  7. After a short delay, repeat steps 2-7.

If you determine that your


kP

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.

Hello,

Can someone please explain to me what P loops and PID loops are, and what difference exists between them? Any help will be appreciated.

Here is a google search of this site that will give you a bunch of answers much faster than I can explain it:

https://www.google.com/search?source=hp&ei=6EJvW8SjH6Hm_QbYvZqQCA&q=pid+loop+explanation+site%3Avexforum.com&oq=pid+loop+explanation+site%3Avexforum.com&gs_l=psy-ab.3...1268.16773.0.17469.44.40.1.0.0.0.256.3304.33j3j2.38.0..2..0...1.1.64.psy-ab..5.24.2306.0..0j0i131k1j0i10k1j0i22i30k1j33i21k1j33i160k1.0.i17rn3da4CA

Maybe start by having a look at this:

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.

@FullMetalMentor @Alex241N

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.

Any help will be appreciated

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.

Exactly. It’s a unit conversion from encoder units to motor units. The example you gave I find is the best way to get everything to click.

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.