MCARDMotor Control With Arduino - A Case Study in Data-Driven Modeling and Control Design - MATLAB & Simulink
MCARDMotor Control With Arduino - A Case Study in Data-Driven Modeling and Control Design - MATLAB & Simulink
1 of 10 17-12-20, 5:24 PM
We connect the host PC to the Arduino board using a Simulink® capability that lets you generate an executable and run it on selected hardware. Figure 2
shows the Simulink library for use with Arduino hardware.
To collect the data, the Arduino board will send voltage commands to the motor and measure the resulting motor angles. We create a Simulink model to
enable data collection. The host machine must communicate with the Arduino board to send voltage commands and receive back the angle data. We
create a second model to enable this communication.
In the model that will run on the Arduino Uno board (Figure 3), the MATLAB® Function block Voltage Command To Pins reads from the serial port and
routes the voltage commands to the appropriate pins. We use serial communication protocol to enable the host computer to communicate with the
Arduino board. In the CreateMessage subsystem, a complete serial message is generated from the motor position data obtained from one of the
analog input pins on the board.
We send various voltage profiles to excite the system, and record and log the corresponding position data. At the end of the simulation, the signal
logging feature in Simulink will create a Simulink data set object in the workspace containing all the logged signals as time-series objects.
Next, we prepare the collected data for estimation and validation. Using the following commands, we convert the data into iddata objects for import into
the System Identification Tool in System Identification Toolbox™.
>> logsout
logsout =
Simulink.SimulationData.Dataset
Package: Simulink.SimulationData
Characteristics:
Name: 'logsout'
Total Elements: 2
Elements:
1: 'Voltage'
2: 'Angle'
‐Use getElement to access elements by index or name.
‐Use addElement or setElement to add or modify elements.
Methods, Superclasses
>> u = logsout.getElement(1).Values.Data;
>> y = logsout.getElement(2).Values.Data;
Developing plant models using system identification techniques involves a tradeoff between model fidelity and modeling effort. The more accurate the
model, the higher the cost in terms of effort and computational time. The goal is to find the simplest model that will adequately capture the dynamics of
the system.
We follow the typical workflow for system identification: We start by estimating a simple linear system and then estimate a more detailed nonlinear model
that is a more accurate representation of the motor and captures the nonlinear behavior. While a linear model might suffice for most controller design
applications, a nonlinear model enables more accurate simulations of the system behavior and controller design over a range of operating points.
Using the iddata objects, we first estimate a linear dynamic model for the plant as a continuous-time transfer function. For this estimation, we specify the
number of poles and zeros. System Identification Toolbox then automatically determines their locations to maximize the fit to the selected data sets.
>> ident
We can import the data sets into the tool from the base workspace using the Import Data pull-down menu (Figure 5). We also have the option to
preprocess the imported data. To start the estimation process, we select the working data that will be used to estimate a model and the validation data
against which the estimated model will be tested. We can use the same data set for both estimation and validation initially, and then use other data sets
to confirm our results. Figure 5 shows the System Identification Tool with the data set imported. The estimation data set, data set 11, comes from an
experiment designed to avoid exciting nonlinearities in the system.
We can now estimate a continuous transfer function for this data. In our example, we estimate a 2-pole, no-zero, continuous-time transfer function (Figure
6).
To ensure that the estimated transfer function represents the motor dynamics, we must validate it against an independent data set. For this purpose we
select data set 12, where the motor operates linearly as our validation data. We achieve a reasonably accurate fit (Figure 8).
While the fit is not perfect, the transfer function that we identified does a good job of capturing the dynamics of the system. We can use this transfer
function to design a controller for the system.
We can also analyze the effect of plant uncertainty. Models obtained with System Identification Toolbox contain information not only about the nominal
parameter values but also about parameter uncertainty encapsulated by the parameter covariance matrix. A measure of the reliability of the model, the
computed uncertainty is influenced by external disturbances affecting the system, unmodeled dynamics, and the amount of collected data. We can
Motor Control with Arduino: A Case Study in Data-Driven Mo... 5 of 10 17-12-20, 5:24 PM
visualize the uncertainty by plotting its effect on the model’s response. For example, we can generate the Bode plot of the estimated transfer function
showing 1 standard deviation confidence bound around the nominal response (Figure 9).
A linear model of the motor dynamics, created by using data collected from a linear region of its operation, is useful for designing an effective controller.
However, this plant model cannot capture nonlinear behavior exhibited by the motor. For example, data set 2 shows that the motor’s response saturates
at about 100°, and data set 3 shows that the motor is not responsive to small command voltages, perhaps owing to dry friction.
In this step, we will create a higher-fidelity model of the DC motor. To do that, we estimate a nonlinear model for the DC motor. A closer inspection of the
data reveals that the change in the slope of the response is not linearly related to the change in voltage. This trend suggests nonlinear, hysteresis-like
behavior. Nonlinear ARX (NLARX) models offer considerable flexibility, enabling us to capture such behavior using a rich set of nonlinear functions, such
as wavelets and sigmoid networks. Furthermore, these models let us incorporate what we have discovered about the system nonlinearities using custom
regressors.
For the NLARX modeling to be effective, we need data that is rich in information about the nonlinearities. We merge three data sets to create the
estimation data. We merge five other data sets to create a larger, multi-experiment, validation data set.
Motor Control with Arduino: A Case Study in Data-Driven Mo... 6 of 10 17-12-20, 5:24 PM
The nonlinear model had various adjustable components. We adjusted the model orders, delays, type of nonlinear function, and the number of units in the
nonlinear function. We added regressors that represent saturation and dead-zone behavior. After several iterations, we chose a model structure that
employed a sigmoid network with a parallel linear function and used a subset of regressors as its inputs. The parameters of this model were estimated
to achieve the best possible simulation results (Figure 10).
The resulting model has an excellent fit of >90% for the estimation data as well as for the validation data. This model can be used for controller design as
well as for analysis and prediction.
We are now ready to design a PID controller for the higher-fidelity nonlinear model. We linearize the estimated nonlinear model at an operating point of
interest and then design a controller for this linearized model.
We tune the PID controller and then select its parameters (Figure 11).
Motor Control with Arduino: A Case Study in Data-Driven Mo... 7 of 10 17-12-20, 5:24 PM
We also check how this controller performs on the nonlinear model. Figure 12 shows the Simulink model that we use to obtain the simulation response of
the nonlinear ARX model.
We then compare the linearized and nonlinear model closed-loop step responses for a desired reference position of 60° (Figure 13).
We create a Simulink model with the controller and place it on the Arduino Uno board using Simulink built-in support for deploying models to target
h
We designed a controller by linearizing the estimated nonlinear ARX model about a certain operating point. The results for this controller show that the
hardware response is quite close to the simulation results (Figure 15).
We also tested how well the controller tracks a random reference command. We see that the hardware tracking performance is also very close to that
obtained during simulation (Figure 16).
Motor Control with Arduino: A Case Study in Data-Driven Mo... 9 of 10 17-12-20, 5:24 PM
This example, while simple, captures the essential steps for data-driven control. We collected input/output data from a given hardware target and used
System Identification Toolbox to construct models of the system. We showed how you can create models of lower and higher fidelity and design
controllers using these estimated models. We then verified the performance of our controllers on the actual hardware.