# PID Controller based on NXT using ROBOTC

Technology 26231浏览

PID controller knowledge and basic ROBOTC is required for this post.
PID controller information can be acquired  here: http://en.wikipedia.org/wiki/PID_controller
ROBOTC knowledge is available here: http://www.robotc.net

In ROBOTC, we’ll write a code for our own PID controller to ensure the robot car will run a specified target. We’ll define the error signal as the actual angle of the encoder and angle set point (target angle). The angle set point is 500 degrees. The motor will run for 10s. Figure 1: PID control for general system

Since we want to use our own PID controller, we have to disable all inbuilt ROBOTC PID control functions. This can be done using the commands shown below.

nMotorPIDSpeedCtrl[motorA] = mtrNoReg;//disable NXT inbuilt PID

nMotorPIDSpeedCtrl[motorB] = mtrNoReg;//disable NXT inbuilt PID

nSyncedMotors = synchNone;//disable NXT inbuilt PID

We tune the PID controller by Ziegler-Nichols method to find Ku and Tu. It’s a little difficult to find perfect Kp, Ki and Kd though when you know Ku and Tu. As for us, we get the value of Ku and Tu, but we try many times to find good Ki and Kd to make the system run stably.

```previous_error = 0
proportional = 0
integral = 0
derivative = 0
loop:
error = setpoint - actual_position
proportional = error
integral = integral + (error*dt)
derivative = (error - previous_error)/dt
output = (Kp*proportional) + (Ki*integral) + (Kd*derivative)
previous_error = error
wait(dt)
loop end;```

In the pseudocode above, Kp, Ki, Kd and dt are not shown how to be got. Because they are constant values. Value of dt is dependent by you. Kp Ki and Kd could be determined by trial and error. The reason why we tune the system is to find Ku and Tu, so we will know approximate values of the three coefficients.

I post the source file here. The code is well commented  and easy to be understood.

ROBOTC Source File : PIDController.c Figure 2: PID tuning. Z-N oscillations

When Ku is set to 0.09, the system starts to oscillate. Figure 3: Zoom in the plot of PID tuning. Z-N oscillations

When we try about 70 times, we find when Ki is equal to 0.0000000075 the response is very good. Figure 4: A good PID response

It seems there is a tolerance, since our target degree is 500. I explain and give examples in my report you can find in the end of this post.

The overshot for my example is 1.349%. The rise time is 0.511s. The settling time for ± 5% error band is 0.6897s, and for ± 2% is 0.7074s

Dependent on the value we got, I believe the project we did is successful. The values and plot are beautiful.

You can find more information in my report. BTW, excuse my English both in this post and report.

Report: PIDController.docx

ROBOTC Source File : PIDController.c

### 网友最新评论 (6)

1. You are welcome to contact me, if you have any suggestion or question.