Qin Rong
Yiqi Ju
Ying Shen
We aim to learn how to use PID controller to ensure a robot car run a specified degree with
PID controller. And the inbuilt PID controller is disabled.
1. Build a robotic car
2. Understand PID control design for robots
3. Design a PID controller. Tune a PID controller
LEGOS NXT Robotic kit
PC to NXT USB cable
Computer with RobotC installed
The car robot we are using is a differential drive car robot as it has two motorized wheels that
provide drive and steering functions. There are some basic problems with this program. The car
may not go straight as the two motors may not have the same speed even though we have
commanded both of them to be at a specified power. This can be due to the different construction
of the two motors as well as different outside environment for the two wheels. One motor my
rotate faster than the other causing the car to drift. To counter these effects, we can use
ROBOTC’s inbuilt PID control function or we can build our own PID controller to ensure the car
will run a specified degree.
A proportional–integral–derivative controller (PID controller) is a generic control loop
feedback mechanism (controller) widely used in industrial control systems – a PID is the most
commonly used feedback controller. A PID controller calculates an "error" value as the difference
between a measured process variable and a desired setpoint. The controller attempts to minimize
the error by adjusting the process control inputs.
Figure 1: PID control for general system
The PID controller calculation (algorithm) involves three separate constant parameters, and is
accordingly sometimes called three-term control: the proportional, the integral and derivative
values, denoted P, I, and D. Heuristically, these values can be interpreted in terms of time: P
depends on the present error, I on the accumulation of past errors, and D is a prediction of future
errors, based on current rate of change. The weighted sum of these three actions is used to adjust
the process via a control element such as the position of a control valve or the power supply of a
heating element.
In the absence of knowledge of the underlying process, a PID controller is the best controller.
By tuning the three parameters in the PID controller algorithm, the controller can provide control
action designed for specific process requirements. The response of the controller can be described
in terms of the responsiveness of the controller to an error, the degree to which the
controller overshoots the setpoint and the degree of system oscillation. Note that the use of the
PID algorithm for control does not guarantee optimal control of the system or system stability.
As for LEGO NXT, the motors have rotation sensors, and therefore can measure the speed. A
speed rating 100 corresponds to the power of 100%. The max speed of the motor in ideal
conditions is 1000 degrees per second.
The closed loop PID control algorithm uses feedback from the motor encoder to adjust the
raw power to provide consistent speed. Closed loop control continuously adjusts the motor raw
power to maintain a motor speed relative to the maximum regulated speed.
To set the maximum regulated speed we use the command nMaxRegulatedSpeedNxt, which
is the max regulated speed level in degrees per second. We can use any value lower than 1000.
Usually, the batteries may not be 100% charged, so we will not be able to get the max speed and
therefore a lower value is recommended. Here we set the max speed as 500.
We will use nMotorPIDSpeedCtrl[motorB] = mtrNoReg and nSyncedMotors = synchNone to
disable the inbuilt PID controller. And we will write our own PID controller.
PID tuning
Tuning a control loop is the adjustment of its control parameters (gain/proportional band,
integral gain/reset, derivative gain/rate) to the optimum values for the desired control response.
Stability (bounded oscillation) is a basic requirement, but beyond that, different systems have
different behavior, different applications have different requirements, and requirements may
conflict with one another.
PID tuning is a difficult problem, even though there are only three parameters and in
principle is simple to describe, because it must satisfy complex criteria within the limitations of
PID control. There are accordingly various methods for loop tuning, and more sophisticated
techniques are the subject of patents.
Ziegler–Nichols method
This method was developed by John G. Ziegler and Nathaniel B. Nichols in the 1940’s. Here
we will discuss the Z-M method for those systems that can become unstable by using proportional
control only. The basic steps in Z-M method are
1. Set K I and K D equal to zero.
2. Slowly increase K p to a value K u at which we see sustained oscillations (constant
amplitude and periodic).
3. Note the period of oscillation. We will denote it by Tu .
4. Use the following values as the initial tuning constants
Z-M model
P controller
0.5 K u
PI controller
0.455 K u
0.833 Tu
PID controller
0.588 K u
0.5 Tu
0.125 Tu
Table 1: Z-M table for PID tuning
1. Build a LEGO NXT Robot car. Instructions are provided in the LEGO MINDSTORM’s
instruction manual. For testing, keep the robotic car upside down for design. Keep the
robot fully charged, as design solution may change if the battery power changes.
set point
NXT Servo
Figure 2: PID control for NXT position control
2. In ROBOTC, write a code for the PID controller. 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 should run for 10s.
It is important to disable all inbuilt ROBOTC PID control functions. This can be
done using the commands shown below. Do not use any inbuilt ROBOTC PID
control function.
nMotorPIDSpeedCtrl[motorA] = mtrNoReg;//disable NXT inbuilt PID
nMotorPIDSpeedCtrl[motorB] = mtrNoReg;//disable NXT inbuilt PID
nSyncedMotors = synchNone;//disable NXT inbuilt PID
b. Provide a soft copy (.c) of the code as well as put in the project report. The code
should not be unnecessary complex and should be well commented explaining
the functioning of the program.
3. Tune the PID controller by following the steps below
Use the Ziegler–Nichols (Z-N) method to find K u and Tu . Draw a plot
showing sustained oscillations and period of the oscillations. An example plot is
shown in figure 3.5. Provide the values of K u and Tu
b. Using the values of K u and Tu , show the initial tuned performance of the PID
controller. Provide the rise time, percentage overshoot and settling time (± 5%)
Tune the PID values further for a maximum percent overshoot Mp < 6% , settling
time < 1.2 sec (2% scenario) and steady state error of < 1.3 %.
4. Design a PID controller for speed control using the set point speed to 250 degrees per
second. Tune the PID values for a percent overshoot < 7%, settling time < 1.5 s (5%
scenario) and steady state error of < 10%.
Figure 3: Robot Car Model based on LEGO NXT
#define Kp 0.05292 // Kp
#define Ki 0.0000000075 // Ki
#define Kd 0.0375 //Kd
task main()
nMaxRegulatedSpeedNxt=500; // set the max speed as 500
nMotorEncoder[motorB]=0; // initialize encoder of motor B
nMotorEncoder[motorC]=0; // initialize encoder of motor C
nMotorPIDSpeedCtrl[motorB] = mtrNoReg;//disable NXT inbuilt PID
nMotorPIDSpeedCtrl[motorC] = mtrNoReg;//disable NXT inbuilt PID
nSyncedMotors = synchNone;//disable NXT inbuilt PID
motor[motorB] = 50;//set speed to 500/2=250 degrees per second
motor[motorC] = 50;//set speed to 500/2=250 degrees per second
int setPoint=500; // target degrees
// initialize all variables for both motor B and motor C
int actualDegreesB=0;
int actualDegreesC=0;
int errorB=0;
int errorC=0;
int pre_errB=0;
int pre_errC=0;
int IB=0;
int IC=0;
int DB=0;
int DC=0;
float outputB=0;
float outputC=0;
// motor B
actualDegreesB = nMotorEncoder[motorB]; // find actual degrees of motor B
errorB=setPoint-actualDegreesB; // find error of motor B
IB=IB+errorB; // find value of integral
DB=errorB-pre_errB; // find value of derivative
outputB=Kp*errorB+Ki*IB+Kd*DB; // pid contorl for motor B
pre_errB=errorB; // store the error
// motor C
actualDegreesC = nMotorEncoder[motorC]; // find actual degrees of motor C
errorC=setPoint-actualDegreesC; // find error of motor B
IC=IC+errorC; // find value of integral
DC=errorC-pre_errC; // find value of derivative
outputC=Kp*errorC+Ki*IC+Kd*DC; // pid contorl for motor C
pre_errC=errorC; // store the error
// record the value of actual degree of motor C for further research.
AddToDatalog(1, actualDegreesC);
// wait for 50 ms
// store all data into a file.
When we set Ku as 0.09, the robot starts to oscillate:
Figure 4: PID tuning. Z-N oscillations
Figure 5: Zoom in the plot of PID tuning. Z-N oscillations
Tu is easily to be found as 0.3s. So when we set Kp=0.588*Ku=0.05292, Ki=0.5*Tu=0.15,
Kd=0.125*Tu=0.0375. We find it still oscillates. The plot diagram is shown as below:
Figure 6: A test example PID response
We can find the Ki is too big from this example. So we low Ki until the PID response is
proper. When we try about 70 times, we find when Ki is equal to 0.0000000075 the response is
very good. The plot diagram is shown below:
Figure 7: A good PID response
Though we want to let the robot run only 500 degrees, the steady state we get is 519.
The steady state error is
Figure 8: Zoom in for the good PID response
We don’t know why there is such a tolerance. But when we try different target degrees, like
480, 400, and 300, we get the plot diagram below:
Figure 9: An example PID response for target degree as 480
Figure 10: An example PID response for target degree as 400
Figure 11: An example PID response for target degree as 300
If the tolerance is generated by algorithm, the tolerance should be changed proportionally to
the target degree. But we found the steady states are always 20 bigger than the target states. So we
believe the tolerance may be generated by the robot itself, not algorithm.
Overshoot is when a signal or function exceeds its target. It arises especially in the step
response of bandlimited systems such as low-pass filters.
Figure 12: The highest value the system reached
So the overshot in this plot diagram is
Rise time refers to the time required for a signal to change from a specified low value to a
specified high value. Typically, these values are 10% and 90% of the step height.
Since the steady degree is 519, the rise time is the time that values rise from 10% to 90% of
the step height, which is about 51.9-467.1.
Figure 13: The trend line for the rise part
We use the rise part of the plot to determine the formula for that part. The formula is
Since we know the formula and values, we can find the rise time easily.
If y=51.9, which means 51.9=0.2842x2+36x-57.622
If y=467.1, which means 467.1=0.2842x2+36x-57.622
So the rise time is
The settling time of an amplifier or other output device is the time elapsed from the
application of an ideal instantaneous step input to the time at which the amplifier output has
entered and remained within a specified error band, usually symmetrical about the final value.
Figure 14: The lowest value after highest value
After the system reaches the highest value, the lowest value is 513.
When the specified error band is ± 5%, the error band will be about 493.05-544.95. Since the
highest value is 526, so we just find when the system reaches 493.05.
If y=493.05, which means 493.05=0.2842x2+36x-57.622
So the settling time is
When the specified error band is ± 2%, the error band will be about 508.62-529.38. Since the
highest value is 526, so we just find when the system reaches 493.05.
If y=508.62, which means 508.62=0.2842x2+36x-57.622
So the settling time is
There exists a tolerance generated by the system itself, not my program. All values we got are
very good. The plot diagram we got looks beautiful.
From this project, we know how to use ROBOTC. And we know more about PID controller,
especially why we need PID controller.
Wikipedia. (2011, April 30). PID controller. Retrieved from
Wikipedia. (2011, April 29). Rise time. Retrieved from
Wikipedia. (2011, April 28). Settling time. Retrieved from
Wikipedia. (2011, April 29). Ziegler–Nichols method. Retrieved from
Wikipedia. (2011, April 28). Overshoot_(signal). Retrieved from