Low-Cost, Flexible Controls Lab Platform using Matlab/Simulink and Non-Uniform Sampling John Miller, Ian Gravagne, Scott Koziol Department of Electrical and Computer Engineering Baylor University John Miller1@baylor.edu, Ian Gravagne@baylor.edu, Scott Koziol@baylor.edu Abstract There is a movement in undergraduate labs toward smaller, more mobile, more modular equipment for the purposes of engaging students and managing higher enrollments. This paper presents a relatively low-cost, flexible, modular platform for automatic control system lab experiments. The hardware consists of a custom circuit board that provides amplification and some signal processing, a National Instruments USB DAQ unit, and one of several peripheral experiment modules (DC motor, pendulum, thermal tunnel, etc.). The NI DAQ unit can communicate directly to MATLAB/Simulink via the Data Acquisition Toolbox, which allows students to implement closed-loop control algorithms directly in the ML/SL environment with no concern for the ”target platform” or code compilation. The control loop sampling rate is randomly distributed with a mean around 40-50 Hz, depending on the host computer’s speed and operating system. While this is too slow for critical control applications, it is more than sufficient for many basic lab experiments, as will be shown by experimental results. Furthermore, the authors provide mathematical conditions under which a non-uniformly sampled system can be controlled. This is supported by experimental data, which also demonstrates that from the student’s perspective, the system can be expected to behave according to the continuous-time model. This allows the system to be used by undergraduate students early in their first control systems course, without the need to introduce the concept of discretization or discrete-time control theory. Introduction An effective method of delivering laboratory content is to provide students with equipment they can operate independently, on their own time, and even in a location of their choosing. Most commercially availble controls lab experiment platforms are too expensive for this purpose, and a quick survey of recent articles reveals a number of people trying to address this need by designing custom systems [1–3]. These systems can be divided into two general catagories: hardware-in-the-loop (HIL) and embedded. HIL systems (see Figure 1) utilize a microcontroller that communicates sensor data to a Proceedings of the 2015 ASEE Gulf-Southwest Annual Conference Organized by the University of Texas at San Antonio c Copyright 2015, American Society for Engineering Education Microcontroller + Hardware PC/Control Algorithm DAC/Amp/ H-bridge Plant (e.g. motor) ADC/Signal Conditioning Figure 1: Block diagram of a generic HIL system. The PC performs the control algorithm calculations. Microcontroller + Hardware Control Algorithm PC “Hybrid”: Sensor data back to PC DAC/Amp/ H-bridge Plant (e.g. motor) ADC/Signal Conditioning Figure 2: Block diagram of a generic embedded system. The embedded microcontroller performs the control algorithm calculations. In ”hybrid” mode, the sensor data is communicated back to the PC for user visualization. personal computer (PC), receives control data from the computer, and converts the control values into the signals that drive the physical plant. An embedded system (see Figure 2) performs the control algorithm calculations on the microprocessor. [2] provides an additional scenario, termed ”hybrid,” whereby the microcontroller communicates sensor data back to the PC for off-line analysis and visualization, while still performing the control calculations locally. Each of these systems have their positive and negative characteristics. The HIL system requires a communications protocol (often RS-232, sometimes converted to USB) between the PC and hardware, but students do not have to program the microcontroller. This can be done for them. The embedded system requires knowledge of the microcontroller, its programming language, and the hardware to which it is connected (if any; e.g. H-bridge IC’s or other signal conditioning hardware). It also requires an understanding of digital control algorithms. The benefit is typically a speed increase for the control loop timing. This paper provides an alternate HIL system, replacing the microcontroller with an off-the-shelf (OTS) data acqusition (DAQ) unit, which is intended to be: • Relatively low cost: much less expensive than commercial units, but more than the examples Proceedings of the 2015 ASEE Gulf-Southwest Annual Conference Organized by the University of Texas at San Antonio c Copyright 2015, American Society for Engineering Education referenced above • Flexible: able to perform multiple types of experiments • Transparent: very little is hidden from students • Target-free: does not require compiling code or an external microcontroller The last item is the key factor that differentiates this design from others. All algorithms are run within MATLAB/Simulink (hereafter abbreviated ML/SL), which communicates sensor and control data with the DAQ unit via USB. This avoids the need for a) writing code in another language or b) writing ML code, compiling it, and downloading it to a target processor, which often requires multiple software applications (ML, compilers, drivers, etc.) to operate together and appropriate programming hardware. It also avoids custom communication protocols and embedded processor timing issues. Another advantage of OTS hardware and software is that it is likely to be wellsupported for an extended period of time. (For example, NI just released an updated set of USB600x DAQ’s that are compatible with the previous versions.) The downside is that the sampling rate is relatively slow, averaging around 40-50 Hz, depending on the host computer. This issue is discussed in depth below. System Design The system can be divided into four main parts, as shown in Figure 3: • ML/SL software - functions as both the user interface and control algorithm computation engine • National Instruments (NI) USB-600x DAQ unit - analog and digital signal interface • A custom PCB - provides output signal amplification, sensors, and some sensor signal processing • A peripheral (system) to be controlled - the present use is for DC motor control, but addiitional peripherals are discussed in the Future Development section Hardware A picture of the hardware (custom PCB and DAQ) and a corresponding block diagram are shown in Figures 4 and 5. The NI USB-6008 DAQ unit was chosen for its low cost, simplicity, appropriate I/O channels, and direct connectivity to ML (via the Data Acquisition Toolbox). NI sells an ”OEM” version, without the standard casing, which allows it to easily mount to the custom PCB. (Note: The new USB6000/1/2/3 series is pin and connector compatible with the 6008/9 versions. Only mount spacings will have to change.) The current version of the hardware uses the following DAQ I/O channels: • Two analog outputs for driving the amplifiers, which in turn drive the motor (or other load). Proceedings of the 2015 ASEE Gulf-Southwest Annual Conference Organized by the University of Texas at San Antonio c Copyright 2015, American Society for Engineering Education Custom PCB MATLAB/ Simulink USB-600x DAQ Amplifiers Plant (e.g. motor) Sensor Signal Conditioning Figure 3: Block diagram of the system showing the four main components. • One analog input for measuring output (motor) voltage. • Two analog inputs for measuring output (motor) current. Future revisions will have only one curent sensor. • One digital counter input for measuring motor speed by counting pulses from the encoderto-counter decoder. • One analog input for determining motor direction. (This function could be performed by a digital pin, if available.) • Twelve digital inputs driven by a set of hardware binary counters for determining motor position. • One analog input for measuring temperature, which is not currently being used. The two analog outputs from the DAQ unit are the inputs to two independent amplifiers. Both amplfiers are powered from a single 15 V dc power supply. Their outputs can be used either as one differential output or as two single-ended outputs. As a differential output, they can produce a ±12 V signal (but not a 24 V differential signal because neither amplifier can be less than 0 V), which allows for bidirectional motor control. This control scheme is used primarily because of the presence of analog outputs and lack of digital pulse-width modulated (PWM) outputs on the DAQ unit. (The digital I/O channels can produce a PWM signal, but they are timed in software, meaning the modulation frequency would be far too slow.) An optical rotary encoder is mounted on the motor and is connected to a quadrature clock converter IC that converts the quadrature encoder signal pair into two seperate signals—a pulse train that can drive a standard binary counter and a direction bit. These signals actually drive two different counters. One is onboard the DAQ unit and is used to determine motor speed. The onboard counter simply counts the number of pulses, regardless of whether the motor is turning clockwise or counter clockwise. The direction signal is also input to the DAQ and can be used to determine the direction Proceedings of the 2015 ASEE Gulf-Southwest Annual Conference Organized by the University of Texas at San Antonio c Copyright 2015, American Society for Engineering Education Thermocouple solder point and IC (not present) Current sensors NI USB DAQ Motor output Position reset Encoder input Hardware counters (position) Amplifiers Quadrature clock converter IC Figure 4: Custom PCB and associated hardware components. 12-25 VDC input Op-amp 1 USB-600x DAQ Current sensor(s) Op-amp 2 Motor output (USB bus-powered, provides +5V to other components) Count and direction Position binary counters (x3) Quadrature clock converter IC Encoder input Position counter reset button Note: only some connections are shown Figure 5: Block diagram of custom PCB and hardware components. Proceedings of the 2015 ASEE Gulf-Southwest Annual Conference Organized by the University of Texas at San Antonio c Copyright 2015, American Society for Engineering Education the motor is turning. However, if the motor changes direction too quickly and/or frequently, it is possible for the software to miss these changes and not account for them. (Recall that the sampling rate is approximately 40-50 Hz.) This is not a significant issue as long as there is a reasonable load (moment of inertia) on the motor shaft and speed is the variable of interest (i.e. speed control). Where it becomes a problem is trying to accurately position the motor (i.e. position control). For this application, a set of three four-bit binary counters (for a total of twelve bits) is added. The binary counter IC’s have inputs for both the pulse signal and the direction signal. Thus, they track the bidirectional movement of the motor (unlike the onboard counter). Software Since the hardware is based on an NI DAQ, a natural choice for the software would be NI’s Labview. While this would certainly work, the authors chose to use ML/SL because it was already in use by the control systems courses at the university and they felt that being able to integrate the lab platform with the mathematical analysis already being done in ML/SL would be beneficial. The Data Acquisition Toolbox (DAQ Toolbox) contains functions for setting up the DAQ unit (specifying which I/O channels are being used and their properties), reading input values, and writing output values. The DAQ Toolbox includes two interfaces for communicating with hardware: ”legacy” and ”session-based.” The older USB-6008/9 units supported both interfaces, but the newly-released USB-600x (0,1,2,3) series only supports the session-based interface. It is also worth noting that the legacy interface has to operate under the 32-bit version of ML. For all of these reasons, the current platform uses the session-based interface. The reader is referred to the Mathworks website for more information[4]. The reading and writing functions access all of the configured inputs and outputs together. Thus, for example, the more input channels that are configured, the longer the read operation takes. Listing 1 shows a simple setup and single read operation for the DAQ unit. Listing 1: Basic USB DAQ session % Create DAQ session object s = daq . createSession ( ’ ni ’ ); % Add input channel & set as " single ended " ( ref to ground ) s . addA n a l o g I n p u t C h a n n e l ( ’ Dev2 ’ ,0 , ’ Voltage ’ ); set ( s . Channels (1) , ’ TerminalConfig ’ , ’ SingleEnded ’ , ’ Range ’ ,[ -10 10]) % Read ( measure voltage ) s . inputSingleScan The session-based interface does not have native SL commands (blocks); however, the ML commands can be called from ”MATLAB Function” blocks within SL. There are some potential limProceedings of the 2015 ASEE Gulf-Southwest Annual Conference Organized by the University of Texas at San Antonio c Copyright 2015, American Society for Engineering Education itations (e.g. SL cannot generate code for these functions; they have to be handled as ”extrinsic” functions), but since the intent is to operate within the ML/SL environment (and not compile to another target), these do not present a real problem. It is expected that these issues will be hidden from the students and that the students will be provided SL blocks/ML functions that they can use to interface with the DAQ unit. The session-based read and write commands are single, blocking operations. Thus, all of the communication with the DAQ unit is initiated within ML/SL, and the timing of these operations depends on ML, the operating system, the USB connection, and the DAQ’s embedded processor. Non-uniform Sampling Closing a feedback loop with a non-deterministic digital controller naturally raises two issues. The first has to do with how much timing variance (sometimes known as ”jitter”) the closed-loop system can accept while still yielding acceptable performance. The second is whether a controller ought, in some way, to be designed to ameliorate or at least better tolerate the negative effects of timing variance. Fortunately, recent theoretical advances using a field of mathematics known as dynamic equations on time scales can shed some light on these questions. Figure 6 illustrates a typical timing histogram of a closed-loop controller executed in ML running on a non-real time operating system. The histogram approximates the timing statistics for a controller running as fast as possible, where timing is not regulated by the use of timers or interrupt routines. From it, two facts are clear: there is a minimum sampling time (or total loop time) of around 13 ms, such that the system simply will not run faster than that; and the distribution of sampling times permits occasional outliers: loop times that are substantially longer than the mean but statistically rare. In the parlance of dynamic equations on time scales, the sampling times form an ordered discrete subset of the real numbers known as a time scale T ⊂ R, and the sampling periods are known as the time scale graininess µ(tn ) = tn+1 − tn where all ti ∈ T. The basics of time scale theory appear in a volume by Bohner and Petersen [6], and recents works on the application of time scale theory to discrete-time control appear in the bibliography[5, 7–9]. The upshot of the time scale analytical framework is that any linear, time-invariant, open-loop system expressed in continuous-time state space form ẋ(t) = Ax(t) + Bu(t), with t ∈ R, can be discretized onto an arbitrary time scale and expressed as a closed-loop time scale state space system x∆ (t) = A (µ(t))x(t); t ∈ T, (1) where x∆ is the Hilger dynamic derivative (similar to a forward difference operator). Here A (µ(t)) = expc(µ(t)A)Acl (2) where expc(X) = I + 21 X + 16 X 2 + ... + n!1 X n−1 is a convergent power series and Acl is the closedloop system matrix.; for example Acl = A − BK for a state feedback controller. (Readers will Proceedings of the 2015 ASEE Gulf-Southwest Annual Conference Organized by the University of Texas at San Antonio c Copyright 2015, American Society for Engineering Education 160 140 occurances 120 100 80 outliers 60 40 20 0 0.013 0.014 0.015 0.016 0.017 0.018 Period µ (seconds) 0.019 0.02 0.021 Figure 6: A histogram of 1000 ADC/DAC conversions executed in a ML loop running in Windows 7. The hardware interface is a NI USB-6009. Period µ(t) is clearly non-constant, with some statistically rare outliers. (Taken from [5]) note we are assuming the closed-loop system is autonomous and neglecting any reference inputs without loss of generality.) The new time scale state space representation is no longer time invariant in general; however, if the timing variance is small compared to the mean, as is the case in Figure 6, it is reasonable to use a constant coefficient system x∆ (t) = Āx(t); t ∈ T, (3) where Ā = E[A (µ(t))] is the expected value of A considering the graininess as a random variable. With this in mind, Poulsen, et. al., showed that the discretized closed-loop system will uniformly Proceedings of the 2015 ASEE Gulf-Southwest Annual Conference Organized by the University of Texas at San Antonio c Copyright 2015, American Society for Engineering Education Figure 7: The complex plane showing the circular region Hδ (T) for a time scale with gammadistributed graininess. The larger shaded area is the largest region of stability that is possible for a constant-coefficient system. This region is difficult to compute and does not guarantee uniform exponential stability. (Taken from [7].) exponentially stabilize if its eigenvalues fall within the region Hδ (T) , where ) ( 2 1 1 Ha = λ ∈ C : λ + < 2 , a a (4) Z T µ(t)∆t δ (T) = lim sup T →∞ t0 T − t0 . If we are armed with knowledge of a fixed statistical distribution for graininess µ, then δ (T) simplifies to E[µ 2 ] δ (T) = , (5) E[µ] which is very easy to calculate. Regions of the form Ha are known as ”Hilger circles”. In particular, the specific region Hδ (T) is the largest Hilger circle that can be subsumed by the (generally non-circular) region of convergence for a system of the form of (3). Figure 7 illustrates this concept, and leads to a key observation: loop timing may be best modeled using a ”long-tail” distribution function, for example a gamma or exponential distribution, because of the possibility of outliers as illustrated earlier. As long as Proceedings of the 2015 ASEE Gulf-Southwest Annual Conference Organized by the University of Texas at San Antonio c Copyright 2015, American Society for Engineering Education these are statistically rare – such that δ (T) exists – a closed loop system will tolerate them and maintain uniform exponential stability. This is not a guarantee of ”nice” performance, but simply a way to ensure that the controller will in fact work in the presence of variable timing. Returning the discussion to its pedagogical purposes, there are two main takeaways. First, instructors will need to ensure that the dynamics of the plants and controllers chosen for student experiments place the closed-loop eigenvalues of (3) within Hδ (T) , or there will be no guarantee of control stability. Second, some knowledge of the worst-case statistical distribution of the system is needed, which will require tests across a variety of computing platforms likely to be used by students. Weighed against ease and simplicity of operation (no target microcontrollers or compilers, no real-time system concerns, use of off-the-shelf and widely supported USB interface hardware, portability to any sufficiently fast computer running MATLAB), the need for careful experiment design may be a good tradeoff. Experimental Results Figure 8 demonstrates the capabilities of the current system. The plots will be explained moving from left to right, top to bottom. The first plot shows the DAQ ouput/amplifier input and amplifier output/motor input voltages. The DAQ output voltage is specified in ML and is not measured. The motor voltage is being measured using two DAQ analog inputs in a differential configuration. The amplifiers are setup in a non-inverting configuration with a nominal gain of 3. The DAQ output voltage is set at ±3 V, and it can be seen that the motor voltage is approximately ±9 V, as expected. Next is motor speed. A basic backward difference is being used to compute speed. Rotations are being measured by the internal DAQ counter driven by the quadrature clock converter. ML’s ”tic” and ”toc” functions are being used to measure actual sample time, which is then used in the speed calculation. For this example, the signal is not being filtered. In the middle are two different plots of motor position. On the left is the actual binary count value being stored on the external binary counters (and read periodically by the DAQ unit). One can see that the counters ”wrap around” when they reach a maximum value of 212 = 4096. On the right is an ”unwrapped” version which has also been converted to angular position in degrees. The speed plot appears to be the derivative of this position plot, as would be expected. On the bottom left is a plot of the two current sensors presently on the unit. There are two for the sake of comparison. The final design will have only one. There are several issues with the current sensors which will be resolved in the future, including the difference in polarity, the amount of noise present in the ”Vac” sensor, and verifying the accuracy of the measurements. On the bottom right is a histogram plot of the sample times, exactly like Figure 6. As stated previously, on these DAQ units, the sampling time increases as more I/O channels are included. Thus, Proceedings of the 2015 ASEE Gulf-Southwest Annual Conference Organized by the University of Texas at San Antonio c Copyright 2015, American Society for Engineering Education the mean of this distribution is higher than the previous one, but the general character remains the same—most of the samples are gouped around the mean with a few outliers. Figure 9 shows a SL diagram with a control loop that includes the hardware and motor (top) and a loop that is simulation only (bottom) using a first-order model of the motor. Zero-order hold blocks have been placed on either side of the simulation model to imitate the discrete behavior of the DAQ system. Figure 10 shows the motor and DAQ voltages (left) and the actual and simulated speed cuves (right). The performance of the motor is reasonable; however, there is some discrepancy in the transient response, the cause of which is still to be determined. Future Development The next step is to continue to improve the hardware design to a state where it is ready to provide to students. The students will be asked to try some basic DC motor experiments with the new system and provide feedback about their experience. Moving forward, additional peripherals can be developed to expand the types of experiments that can be performed. For example, it would be possible to do a temperature control system with a small enclosure, resistive heating element, fan, and one or more thermocouples. It would also be possible to use this system with the ”aeropendulum” referenced in [1]. Another idea for future development is to integrate this platform into other undergraduate engineering classes in addition to introductory controls. The hardware system’s modularity (DAQ unit/amplification and signal processing board/peripheral plant modules) is one of the key features that makes this platform so versatile for multi-class use. As was stated in the introduction, one of the benefits of using this system for teaching a controls course is that it is target-free and does not require compiling code or an external microcontroller. For a controls course, this is very appropriate because it keeps the students focused on applying the theoretical aspects of controls. For other classes, however, a different approach can be taken. For example, when teaching a microprocessors class, the students are focused on learning how to write and compile code and not focused on learning control theory. The system could be used for a microprocessors class by having the students replace the DAQ unit with the microcontrollers that they are learning to program. They could then apply their microprocessor programming skills to implement either an HIL or embedded control system. In addition to learning how to compile code, this would also be a good application of learning how to process microprocessor input/output data. Processing sensor feedback, for example, would teach them about working with microprocessor input data and creating an output signal for motor control could teach them about output. Because they would be interacting with the amplification and signal processing board, they would also learn about methods to interface a microprocessor with mechatronic systems. Another course that the authors are involved in is called Avionics Systems Design. Here, students Proceedings of the 2015 ASEE Gulf-Southwest Annual Conference Organized by the University of Texas at San Antonio c Copyright 2015, American Society for Engineering Education Motor Speed Motor Voltage 4000 10 Motor DAQ 3000 2000 Speed (RPM) Voltage (V) 5 0 1000 0 −1000 −2000 −5 −3000 −10 0 1 2 3 Time (s) 4 5 −4000 6 0 1 2 Motor Position As Counts 16000 4000 14000 2500 2000 1500 6 5 6 10000 8000 6000 4000 1000 2000 500 0 1 2 3 Time (s) 4 5 0 6 0 1 2 3 Time (s) 4 Sample Timing Measured Current 60 1.5 Vac T60404 HW CLSW6B5 1 50 # of samples 0.5 Current (A) 5 12000 3000 Angle (deg) Position (cnts) 3500 0 40 30 −0.5 20 −1 10 −1.5 4 Motor Position As Angle 4500 0 3 Time (s) 0 1 2 3 Time (s) 4 5 6 0 25 30 35 40 45 50 Elapsed Time (ms) 55 60 65 Figure 8: Example 1: Open-loop operation of DC motor showing sensor measurements. Proceedings of the 2015 ASEE Gulf-Southwest Annual Conference Organized by the University of Texas at San Antonio c Copyright 2015, American Society for Engineering Education Figure 9: Example 2: Simulink diagram. The upper loop interfaces with the DAQ unit. The lower loop is a simulation using a first-order plant model with parameters from the motor’s data sheet. Motor Voltage Motor Speed 4 1400 3.5 1200 1000 Speed (RPM) Voltage (V) 3 2.5 2 1.5 600 400 1 0.5 0 800 200 Motor DAQ 0 1 2 3 4 Time (s) 5 6 7 8 0 Actual Simulated 0 1 2 3 4 Time (s) 5 6 Figure 10: Example 2: Closed-loop operation of DC motor in Simulink. Proceedings of the 2015 ASEE Gulf-Southwest Annual Conference Organized by the University of Texas at San Antonio c Copyright 2015, American Society for Engineering Education 7 8 can benefit from this modular system’s ability to interact with peripheral modules. Avionics is a senior elective so the students should already have experience in both microprocessors and control theory. They can apply this knowledge to avionics specific controls and signal processing problems. Example problems might be applying a real time operating system (RTOS) to a control problem, learning about and applying advanced control concepts such as digital control, state space concepts, and advanced signal processing algorithms such as Kalman filters. They could even develop their own peripheral modules as part of their required major class projects. These two course examples illustrate how this board can be re-configured in the future to help accomplish different pedagogical goals of various classes. References [1] Eniko Enikov, Vasco Polyzoev, and Joshua Gill. “Low-Cost Take-Home Experiment on Classical Control Using Matlab/Simulink Real-Time Windows Target”. In: Proceedings of the 2010 ASEE Zone IV Conference (2010). [2] Ryan W. Krauss. “Evaluation of a Low-Cost Microcontroller for Real-Time Control Education and Prototyping”. In: Proceedings of the ASME 2014 Dynamic Systems and Control Conference (DSCC) DSCC2014-5846 (2014). [3] Rebecca M. Reck and Ramavarapu S. Sreenivas. “Developing an Affordable Laboratory Kit for Undergraduate Controls Education”. In: Proceedings of the ASME 2014 Dynamic Systems and Control Conference (DSCC) DSCC2014-6046 (2014). [4] Data Acquisition Toolbox. http://www.mathworks.com/products/daq/. [5] Dylan Poulsen, Ian Gravagne, and John M. Davis. “Is Deterministic Real Time Control Always Necessary? A Time Scales Perspective”. In: Proceedings of the ASME 2014 Dynamic Systems and Control Conference (DSCC) DSCC2014-6093 (2014). [6] Martin Bohner and Allan Peterson. Dynamic Equation on Time Scales. Birkhauser, 2001. [7] Dylan Poulsen, John M. Davis, and Ian Gravagne. “Stochastic time scales: quadratic Lyapunov functions and probablistic regions of stability”. In: Proceedings of the IEEE Southeastern Symposium on System Theory (SSST) (2013), pp. 98–103. [8] Dylan Poulsen, John M. Davis, and Ian Gravagne. “Observer based feedback controllers on stochastic time scales”. In: Proceedings of the IEEE Southeastern Symposium on System Theory (SSST) (2013), pp. 104–107. [9] Ian Gravagne, John M. Davis, and Dylan Poulsen. “Time scale-based observer design for battery state-of-charge estimation”. In: Proceedings of the IEEE Southeastern Symposium on System Theory (SSST) (2012), pp. 12–17. Proceedings of the 2015 ASEE Gulf-Southwest Annual Conference Organized by the University of Texas at San Antonio c Copyright 2015, American Society for Engineering Education