Friday, November 13, 2009

9.8 and 9.9: Questions and Answers to Self-Test











 < Day Day Up > 











9.8 and 9.9: Questions and Answers to Self-Test



























1. 



Develop a skeleton C++ program that implements the algorithm shown in the flowchart of Figure 9.3. In this initial version, just use comments to indicate where the Initialize, Update, I/O, and real-time clock-related operations would take place.








2. 



Integrate the C++ fixed-point controller code developed in Chapter 8 self-test problem 5 into the code from problem 1 above. This code provides the implementations of the Initialize and Update blocks in Figure 9.3. Note that the sampling interval for this system is 10 milliseconds.








3. 



Implement a MATLAB model of the inverted pendulum as shown in Chapter 7 self-test problem 1. Modify the model's C matrix so that its output is y = [θ x]T, then augment the model to pass its input as an additional output at the end of the output vector. The resulting output vector should be y = [θ x F]T. Execute write_cpp_model() to convert the model to C++ with a first-order hold and a 10-millisecond step time.








4. 







  1. Integrate the pendulum model with the results of problem 2 via the I/O blocks in the flow diagram of Figure 9.3. Use the system configuration shown in Figure 5.1. Set all controller and plant initial conditions to zero. Set up the program to run for 71 time steps (0.7 seconds).




  2. Set up the commanded cart position x as the reference input. Include the feedforward gain value computed in Chapter 7 self-test problem 10. Use the constant value of 1 meter as the commanded cart position.




  3. Add a data logging capability to the simulation. At each controller update, record the time, the cart position x, the pendulum angle θ, and the cart force F. Write this data to a text-format file with each update on one line and spaces separating the values. Don't write a header line or anything else to the file. This format can be read directly into MATLAB.










5. 



You now have a complete simulation of the plant and controller. Execute the simulation to produce the output file. Load the output file into MATLAB and plot each of the signals. Compare this plot to Figure 7.12. Explain any differences.








6. 



The simulation developed in the previous problems used a simplified linear model of the cart and the pendulum. Describe some limitations of this simulation and how you could make the simulation more realistic and useful for testing controller performance.








7. 



Assume you have a prototype cart and inverted pendulum system and a complete implementation of a control system for it. Describe a set of operational tests that will assess the performance of this system under a variety of operational conditions. Indicate how you could use the results of these tests for validating the simulation.







Answers


























1. 



The following skeleton program implements the structure of Figure 9.1.



void main()
{
// Determine initial states

// Call the controller's Initialize function

int n = 0; // Update counter

// Start the real-time clock counting from zero

for (;;)
{
// Read the controller inputs U

// Call the controller's Update function

// Write the controller outputs Y

++n;
// Delay until time = n * h
}
}





2. 



This program adds the inverted pendulum fixed-point controller class to the program and calls its Initialize() and Update() member functions.



#include "pend_dsys_fixpt.cpp"

void main()
{
// Determine initial states
short x0[4] = {0, 0, 0, 0};

// Call the controller's Initialize function
dsys_fixpt controller;
controller.Initialize(x0);

int n = 0; // Update counter
double h = 0.01; // Sampling interval, seconds

// Start the real-time clock counting from zero

for (;;)
{
// Read the controller inputs U
short u[3];

// Call the controller's Update function
controller.Update(u);

// Write the controller outputs Y

++n;
// Delay until time = n * h
}
}





3. 



The following sequence of steps creates the inverted pendulum model and writes it to a C++ file named pend_model.cpp.



>> a = [0 0 1 0; 0 0 0 1; 90.55 0 0 0; -2.26 0 0 0];
>> b = [0; 0; -23.08; 3.08];
>> c = [1 0 0 0; 0 1 0 0];
>> d = 0;
>> ssplant_aug = ss(a, b, [c; zeros(1, 4)], [d; 1]);
>> h = 0.01;
>> pend_model = c2d(ssplant_aug, h, 'foh');
>> write_cpp_model(pend_model, 'pend_model.cpp');






4. 



The following C++ program combines the results of problem 4a through 4c.



#include "pend_dsys_fixpt.cpp"
#include "pend_model.cpp"

#include <cstdio>
#include <cassert>

void main()
{
// Declare the plant and controller objects
dsys_fixpt controller;
pend_model plant;

// Specify the initial states
short controller_x0[4] = {0, 0, 0, 0};
double plant_x0[4] = {0, 0, 0, 0};

// Call the plant and controller intialization functions
controller.Initialize(controller_x0);
plant.Initialize(plant_x0);

// Set up for the dynamic loop
const short *controller_output = controller.Output();
const double *plant_output = plant.Output();

int n = 0; // Loop counter
double h = 0.01; // Sampling interval, seconds
const double r = 0.9; // Reference input
const double N = -223.6; // Feedforward gain
const double short_max = 32768;

const double us[dsys_fixpt::r] = {4, 2, 1000};
const double ys[dsys_fixpt::m] = {1000};

// Start the real-time clock counting from zero

// Open the data logging output file
FILE *iov = fopen("pend.txt", "w");
assert(iov);

while (n < 71)
{
// Set up the controller inputs U
short controller_u[3];
for (int i=0; i<dsys_fixpt::r; i++)
controller_u[i] = short(short_max * plant_output[i] /
us[i]);

// Call the controller's Update function
controller.Update(controller_u);

// Combine the controller output and the reference input
double pend_u = (ys[0]*controller_output[0])/
short_max + r*N;

// Call the plant's Update function
plant.Update(&pend_u);

// Log the current plant and controller outputs
fprintf(iov, "%lf %lf %lf %lf\n", n*h,
plant_output[0], plant_output[1], plant_output[2]);

++n;
// Delay until time = n * h
}

fclose(iov);
}






5. 



The following MATLAB script reads the file created by the C++ program shown above and plots the controller and plant outputs with the plot() command.



>> clear all
>> close all
>> load pend.txt
>> t = pend(:, 1);
>> theta = pend(:, 2);
>> x = pend(:, 3);
>> F = pend(:, 4);
>> subplot(311), plot(t, theta), legend('theta')
>> subplot(312), plot(t, x), legend('x')
>> subplot(313), plot(t, F), legend('F')


The plot created by the above sequence of commands is shown in Figure 9.7. One difference from Figure 7.12 is that the x state does not converge to precisely 1 because of quantization.






Figure 9.7: Plot of controller and plant outputs.




6. 



This plant model is a linear approximation of a nonlinear system. For pendulum angles more than about 5° from the vertical, this assumption becomes less and less reasonable. The angles reached in Figure 9.6 are far outside this limit. A better approach for testing this controller in a simulated environment would be to replace the linear plant model with a nonlinear model that accurately represents the dynamic behavior of the plant for both small and large pendulum angles. Other relevant aspects of the system also should be modeled in the simulation, such as sensor and actuator noise, drive motor dynamics, and friction in the drive system. System simulations commonly grow in complexity with the addition of more realistic models as the system design matures and validation test data becomes available.




7. 



The following list of tests might be appropriate for a system of this type.



  • Step commands into the reference input.




  • Sine wave commands at various frequencies into the reference input.




  • Pseudorandom noise passed through a lowpass filter with a suitable cutoff frequency into the reference input.




  • Parameter variations: increase and decrease the cart and pendulum masses, increase the drive system friction, and alter the motor power supply voltage to test controller performance in the presence of plant variations. These variations should be representative of the parameter ranges the actual system is expected to experience.




To perform simulation validation, set up and execute simulation tests that duplicate the test scenarios described above. Compare the results of the simulation tests to the results of the operational tests. Identify cases where the differences between the results are caused by significant limitations of the simulation. Improve the simulation models to more accurately represent the behavior of the actual system.






















 < Day Day Up > 



No comments: