Hands-on Lab PID Closed-Loop Control Adding feedback improves performance. Unity feedback was examined to serve as a motivating example. Lectures derived the power of adding proportional, integral and derivatives (PID) of the error. Such additions result in further moving the poles and zeros of the closed-loop system to more desirable locations (as seen by root locus plots); the designer tunes PID gains in order to achieve desired performance (i.e. rise time) and stability (i.e. overshoot). In essence, PID adds or decreases the closed-loop system type (e.g. Type 0). Depending on the input (e.g. a step or ramp signal), one needs to tune PID gains accordingly. Preamble: Step Response of NXT DC motor with PID Continuing our example, the NXT DC motor, the open-loop transfer function (OLTF) was given by 7.83 8.33 (1) The rise time was observed to be 0.12sec. Commanding the motor at 75%, yielding a steady-state velocity is 70.5 RPM. We also see that the NXT Motor is a Type 0 system. From lecture, recall that a PID controller is depicted in Figure A Figure A: Block diagram of a typical PID controller with a plant (denoted by the open-loop transfer function From lecture, we said the following: For Type 0 systems (like DC motor speed): Hypothesis A: For step response, if 1 and 0 and 0 there will always be a discrete error. This is because simply adding proportional gain does NOT increase system type. Hypothesis B: For step response, if 0, then we expect zero error (but may have stability issues)
Concept 1: Use Simulink to test Hypotheses A and B for NXT DC motor Step 1: Launch Simulink and create the following model given in Figure 1-1 Figure 1-1: Baseline model of PID control with NXT motor using equation (1) Exercises: 1-1. Execute the Simulink simulation with 1, 10, 100 and 1000 (and make sure 0 Contrast the plots for error, motor command and motor speed. What do you notice and what can you say about Hypothesis A? Hint: What is the impact of on rise time and error? 1-2. Execute the Simulink simulation with 0 but now set 1, 10, 100. Contrast plots for error, motor command and motor speed. What do you notice and what can you say about Hypothesis B? Hint: What is the impact of on rise time and error? 1-3. Execute the Simulink simulation with various combinations of,, and (example, try 100, 10and 10. What do you notice about the rise time and error? From your motor command scope observations, can such a controller be implemented for real? 1-4. Repeat 1-1, 1-2 and 1-3 but replace the plant with your own open-loop transfer function of the NXT DC motor.
Concept 2: Implement PID in NXC Step 1: Re-run an open-loop step response (motor command of 75%) and determine the steadystate velocity (recall code from past: nxtmotorolsr1_0.nxc) Make a note of the steady-state RPM and the rise-time Step 2: Write NXC code to implement PID control. Define some PID related variables // PID related variables float Kp, Ki, Kd; // PID gains float desiredrpm; // desired motor speed (e.g. open-loop steady-state RPM) float error; // difference between desired RPM and actual RPM float preverror; // needed for derivative control float deltaerror; // needed for derivative control float derivativeoferror; float integraloferror; float motorcommand; // calculated PID motor command Step 3: Initialize desired RPM (to perhaps your open-loop steady-state value) and error-related variables. // Initialize variables elapsedtimeinseconds = 0.0; // set elapsed time to zero prevangleindegrees = 0; // motor initially motionless so set angle to zero motorrpm = 0.0; desiredrpm = 70.5; // this was our motor's open-loop steady-state RPM error = 0.0; deltaerror = 0.0; preverror = 0.0; derivativeoferror = 0.0; integraloferror = 0.0; Step 4: Set PID gains // Set PID gains Kp = 100.0; Ki = 0.0; Kd = 0.0; Step 5: Calculate error, its derivative and integral. Calculate resulting motor command // Calculate error error = desiredrpm - motorrpm; deltaerror = error - preverror; derivativeoferror = (deltaerror)/elapsedtimeinseconds; integraloferror = preverror + error; // Update motor command motorcommand = Kp*error + Ki*integralOfError + Kd*derivativeOfError; if(motorcommand >= 100.0) motorcommand = 100.0; if(motorcommand <= 0.0) motorcommand = 0.0; charmotorcommand = motorcommand; OnFwd(MOTOR, charmotorcommand);
NXT DC Motor PID Last updated 11/17/12 Step 6: Update variables // Update current tic value and angle prevtick = curtick; prevangleindegrees = curangleindegrees; preverror = integraloferror; Wait(25); // update loop every 25 milliseconds } while( elapsedtimeinseconds <= 2.0); // collect 2 sec of data Sample Results Figure 2A: OLSR shows steady-state RPM of about 54 RPM Figure 2B: 1; 0; 0 to 70.5 desired RPM. Illustrates that will have discrete error Figure 2C: 100; 0; 0 to 70.5 desired RPM. Illustrates discrete error 1 decreases if 100; 10; 10 to 70.5 Figure 2D: desired RPM. Illustrates discrete error going to zero.
Figure 2E: 0; 1; 1 to 70.5 desired RPM. Illustrates discrete error going to zero. Exercises: 2-1 Execute your NXC PID program and capture your own versions of Figure 2A-2E 2-2 Based on Figure 2A, calculate rise-time and steady-state velocity. Implement the resulting transfer function in your Simulink model (i.e. Figure 1-1). Run simulations and capture figures for the various gains 2-3 Compare experimental results (2-1) and Simulink simulations (2-2). Show that the results are similar. Try to explain any differences.