Hands-on Lab PID Closed-Loop Control Preamble: Step ...pyo22/mem639Fall2012-2013/... · PID...

5
NXT DC Motor PID – Last updated 11/17/12 © Copyright Paul Oh 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 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 From lecture, we said the following: Figure A: Block diagram of a typical PID controller with a plant (denoted by the open-loop transfer function ܩ ݏ(1) 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) ܩ ݏ 7.83 ݏ 8.33

Transcript of Hands-on Lab PID Closed-Loop Control Preamble: Step ...pyo22/mem639Fall2012-2013/... · PID...

Page 1: Hands-on Lab PID Closed-Loop Control Preamble: Step ...pyo22/mem639Fall2012-2013/... · PID Closed-Loop Control ... Lectures derived the power of adding proportional, integral and

NXT DC Motor PID – Last updated 11/17/12

© Copyright Paul Oh

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 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 From lecture, we said the following:

Figure A: Block diagram of a typical PID controller with a plant (denoted by the open-loop transfer function

(1)

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)

7.838.33

Page 2: Hands-on Lab PID Closed-Loop Control Preamble: Step ...pyo22/mem639Fall2012-2013/... · PID Closed-Loop Control ... Lectures derived the power of adding proportional, integral and

NXT DC Motor PID – Last updated 11/17/12

© Copyright Paul Oh

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.

Page 3: Hands-on Lab PID Closed-Loop Control Preamble: Step ...pyo22/mem639Fall2012-2013/... · PID Closed-Loop Control ... Lectures derived the power of adding proportional, integral and

NXT DC Motor PID – Last updated 11/17/12

© Copyright Paul Oh

Concept 2: Implement PID in NXC Step 1: Re-run an open-loop step response (motor command of 75%) and determine the steady-state 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 Step 3: Initialize desired RPM (to perhaps your open-loop steady-state value) and error-related variables. Step 4: Set PID gains Step 5: Calculate error, its derivative and integral. Calculate resulting motor command

// 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

// 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;

// Set PID gains Kp = 100.0; Ki = 0.0; Kd = 0.0;

// 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);

Page 4: Hands-on Lab PID Closed-Loop Control Preamble: Step ...pyo22/mem639Fall2012-2013/... · PID Closed-Loop Control ... Lectures derived the power of adding proportional, integral and

NXT DC Motor PID – Last updated 11/17/12

© Copyright Paul Oh

Step 6: Update variables Sample Results

// 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

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 decreases if ≫ 1

Figure 2D: 100; 10; 10 to 70.5 desired RPM. Illustrates discrete error going to zero.

Page 5: Hands-on Lab PID Closed-Loop Control Preamble: Step ...pyo22/mem639Fall2012-2013/... · PID Closed-Loop Control ... Lectures derived the power of adding proportional, integral and

NXT DC Motor PID – Last updated 11/17/12

© Copyright Paul Oh

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.