<div>
<h2>Introduction</h2>
<p>Control theory is an interdisciplinary branch of mathematics and engineering that has the objective of controlling physical systems. A control system is a device or a collection of devices that manage, command, direct or regulate the behavior of other devices or systems. Control systems engineering is a major application of control theory, and involves the study, design/modeling of automatic control systems. Control theory has also been applied to several other fields such as finance, sociology, psychology, physiology and other sciences that analyze dynamic systems.</p>
<p>In recent times, control systems have played a major role in the development and advancement of modern technology and civilization. Control systems are essential in any field of engineering and science, as they are an important and integral part of systems such as space-vehicle systems, robotic systems, modern manufacturing systems, nuclear reactors, navigation systems and industrial operations involving control of temperature, pressure, humidity, flow, speed/velocity, etc.</p>
<div>
<h3>Example of a Control System</h3>
<p>Imagine the operation of an Automatic Electric Iron where its heating elements are controlled by the temperature output of the iron. As long as there is power supply to the electric iron, and a desired heat-level is selected, the electric iron automatically controls at what time to turn-on and switch-off heating so as to maintain that temperature. This type of control system is called a feedback control system: a system in which the output is fed back to the input as a control signal.</p>
<p>Figure 1 shows a simple block diagram depicting a sample feedback control system for a heating system.</p>
<p><img src="https://i0.wp.com/rviews.rstudio.com/post/2017-07-05-control-systems-toolbox_files/Figure1.png?w=456&ssl=1" data-recalc-dims="1"><br>
Figure 1: Block Diagram Model for a Heating System</p>
<p>Another example is the control system for an aircraft or a space shuttle. Truly, such control system would be complex as there are several sub-systems that are interconnected in such transport systems. Therefore, it is very necessary to simulate how these systems could behave using a computer before implementing them physically. This is the reason why computer simulation of control systems have been proliferated.</p>
</div>
<div>
<h3>Control Systems R-GSoC Project</h3>
<p>For this <a rel="nofollow" href="https://github.com/rstats-gsoc/gsoc2017/wiki/control%3A-Control-Systems-toolbox" target="_blank">Google Summer of Code (GSoC) 2017 project</a>, we are working to develop a control system toolbox for the R language. The rationale is that there is a control toolbox for most commercial and open-source technical computing platforms out there, and while the R language is experiencing tremendous growth in its capabilities and user base, it seems necessary to extend the language to serve other scientific and engineering communities. For example, in Python, the development of NumPy and the general SciPy stack, added some dimensions and attracted several scientific domains to the use of Python. This GSoC project hopes to make basic design, analysis and simulation of control systems possible within the R ecosystem.</p>
<p>The major objective of this year’s GSoC project is to try to:</p>
<ul>
<li>Design and develop a framework for the package that defines data structures and creates flexibility of usage and extension for future users and interested contributors to easily adopt the package. The framework is also expected to mimic the I/O style of existing control systems toolkits.</li>
<li>To port a set of functions from the <a rel="nofollow" href="http://rlabplus.sourceforge.net/" target="_blank">RLabPlus</a> control-toolbox and adapt them to the defined data structures and general framework.</li>
<li>To test the functions, create documentation, usage examples and build them for distribution.</li>
</ul>
</div>
<div>
<h3>Benefits of a Control Systems Toolbox in R</h3>
<p>Some benefits of having a control-systems-toolbox in R are:</p>
<ul>
<li>A control package in R could be useful to engineers and students who are looking to migrate to an open-source language like R that has better visualization options and interfaces to several technologies. Models for several dynamic systems could then be analyzed using R.</li>
<li>The package could assist in further developments in areas like optimal control theory which has several applications to economics and management sciences.</li>
</ul>
</div>
<div>
<h3>Current State of the Project</h3>
<p>At the time of writing this article, the first phase of coding for GSoC has elapsed, and we are delighted to meet our target of creating 18 functions that define the general framework and structures. Consequently, a user can, at this stage, perform the following:</p>
<ul>
<li>Create LTI system models of state-space, transfer function and zero-pole classes</li>
<li>Conversions from one system model to other system models</li>
<li>Convert continuous-time system models to discrete-time models</li>
<li>Obtain time response for LTI systems</li>
<li>Install the development version of the package using: <code>devtools::install_github(“benubah/control”)</code></li>
</ul>
</div>
<div>
<h3>Practical Example</h3>
<div>
<h4>Analyzing the Time Response of a Bandpass RLC Filter Circuit</h4>
<p><a rel="nofollow" href="https://en.wikipedia.org/wiki/Band-pass_filter" target="_blank">Wikipedia</a>: “A bandpass filter is a device that passes frequencies within a certain range and rejects (attenuates) frequencies outside that range.” Bandpass filters are commonly used in wireless transmitters and receivers to prevent transmitters from interfering with other stations and to allow signals within selected frequency ranges to be decoded, while preventing signals at unwanted frequencies from passing through to the receiver. In astronomy, bandpass filters are used to allow only certain portion of light spectrum pass through to an instrument. They have several other applications in neuroscience, digital signal processing, atmospheric sciences, etc.</p>
<p>The following example is taken from a <a rel="nofollow" href="https://www.mathworks.com/help/control/examples/analyzing-the-response-of-an-rlc-circuit.html" target="_blank">Mathworks web-page</a> and it shows how to analyze the time response of a common RLC bandpass filter circuit using the current available functions of the Control Systems Toolbox in R. The attenuation properties of the filter circuit, G2 can be ascertained by simulating how this filter transforms sine waves with frequency 0.9, 1, and 1.1 rad/s.</p>
<div>
<img src="https://i0.wp.com/rviews.rstudio.com/post/2017-07-05-control-systems-toolbox_files/Figure2.png?w=456&ssl=1" data-recalc-dims="1">
</div>
<p>Figure 2: Bandpass RLC Filter Circuit.</p>
<p>For this example, we will try to show how the following could be achieved using the developing <code>control</code> library:</p>
<ul>
<li>Creating a transfer function model</li>
<li>Converting a transfer function to state-space representation</li>
<li>Converting a continuous-time model to discrete-time model</li>
<li>Performing Linear simulation using <code>lsim</code></li>
<li>Obtaining step response using <code>lsim</code></li>
</ul>
<p>The following values are given R = 1, L = 1, C = 1, R2 = 20</p>
<p>The transfer function for this circuit is:</p>
<p><span>\[<br>
G(s) = \frac{V_output}{V_input} = \frac{s/(RC)}{s^2 + s/(RC) + 1/(LC)}<br>
\]</span></p>
<p>On the R Console (or in an R script), after installing the <code>control</code> package, the following code obtains the time response of the circuit:</p>
<pre><code>library(control)</code></pre>
<pre><code>## 
## Attaching package: 'control'</code></pre>
<pre><code>## The following object is masked from 'package:base':
## 
##     append</code></pre>
<pre><code>R &lt;- C &lt;- L &lt;- 1
R2  &lt;-  20
t  &lt;-  seq(0,250,0.05)
G1 &lt;-  tf(c(1/(R*C), 0), c(1, 1/(R*C), 1/(L*C) ) )
G2  &lt;-  tf(c(1/(R2*C), 0), c(1, 1/(R2*C), 1/(L*C) ) )
print(G1)</code></pre>
<pre><code>## 
## y1:
##              s 
##   - - - - - - - - -
##      s^2 +  s + 1 
## 
## 
## Transfer Function: Continuous time model</code></pre>
<pre><code>tf2ss(G1) # example: convert the transfer function to state-space representation</code></pre>
<pre><code>## 
##  sys.A = 
## 
##      x1   x2
## x1   -1   -1
## x2    1    0
##    
## sys.B = 
## 
##      u1
## x1    1
## x2    0
##    
## sys.C = 
## 
##      x1   x2
## y1    1    0
##    
## sys.D = 
## 
##      u1
## y1    0
##    
## 
##  State-Space system: Continuous time model</code></pre>
<pre><code>c2d(G1, 0.1) # example: convert the system G1 from continuous-time to discrete-time model</code></pre>
<pre><code>## 
## y1:
##   0.09500408 z^1 - 0.09500408 
##   - - - - - - - - - - - - - - - - -
##      z^2 - 1.895329 z + 0.9048374 
## 
## 
## Sample Time = 0.1 
## Transfer function: Discrete time model</code></pre>
<pre><code>response &lt;- lsim(G2,sin(t),t)
par(mfrow = c(3,1))
plot(t, sin(t), type = &quot;l&quot;, xlab = &quot;Time, secs&quot;, ylab = &quot;Amplitude&quot;, main = &quot;Linear Simulation Response - w = 1&quot;, col = &quot;lightgray&quot;)
lines(t, response$y, type = &quot;l&quot;, col = &quot;blue&quot;, lwd = 2)
grid(5,5, col = &quot;lightgray&quot;)

response2 &lt;- lsim(G2, sin(0.9*t), t)
plot(t, sin(t), type = &quot;l&quot;, xlab = &quot;Time, secs&quot;, ylab = &quot;Amplitude&quot;, main = &quot;Linear Simulation Response - w = 0.9&quot;, col = &quot;lightgray&quot;)
lines(t, response2$y, type = &quot;l&quot;, col = &quot;blue&quot;, lwd = 2)
grid(5,5, col = &quot;lightgray&quot;)

response3 &lt;- lsim(G2, sin(1.1*t), t)
plot(t, sin(t), type = &quot;l&quot;, xlab = &quot;Time, secs&quot;, ylab = &quot;Amplitude&quot;, main = &quot;Linear Simulation Response - w = 1.1&quot;, col = &quot;lightgray&quot;)
lines(t, response3$y, type = &quot;l&quot;, col = &quot;blue&quot;, lwd = 2)
grid(5,5, col = &quot;lightgray&quot;)</code></pre>
<div><span></span><br>
<img alt="Figure 3: R Plot for Linear Simulation Response of RLC bandpass circuit" src="https://i1.wp.com/rviews.rstudio.com/post/2017-07-05-control-systems-toolbox_files/figure-html/unnamed-chunk-1-1.png?w=450&ssl=1" data-recalc-dims="1">
<p>
Figure 1: Figure 3: R Plot for Linear Simulation Response of RLC bandpass circuit
</p>
</div>
<p>The output of the simulation displayed in Figure 3 shows a pure sine wave (in light-gray color) behind the wave response from our linear simulation (in blue). At w = 1 rad/s, after the transients have died off, the wave remains a pure sine wave – no attenuation observed. However, the waves at 0.9 and 1.1 rad/s are significantly attenuated.</p>
<p>Please get the code here: <a rel="nofollow" href="https://github.com/benubah/controldev/blob/master/R/lsimExample.R" target="_blank">https://github.com/benubah/controldev/blob/master/R/lsimExample.R</a></p>
<p>To obtain a <em>step response</em> for the RLC filter circuit using <code>lsim</code> as shown in Figure 4, the following code is used:</p>
<pre><code>dims &lt;- dim(as.matrix(t))
   u &lt;- matrix(rep(1,dims[1]), dims[1], dims[2])
   stepresponse &lt;- lsim(G2, u, t)
plot(t, stepresponse$y, type = &quot;l&quot;, xlab = &quot;Time, secs&quot;, ylab = &quot;Amplitude&quot;, main = &quot;Step Response - G2&quot;, col = &quot;red&quot;, lwd = 2)
grid(7,7, col = &quot;lightgray&quot;, lwd = 2)</code></pre>
<div><span></span><br>
<img alt="Figure 4: R Plot for Step Response of the RLC bandpass circuit" src="https://i0.wp.com/rviews.rstudio.com/post/2017-07-05-control-systems-toolbox_files/figure-html/unnamed-chunk-2-1.png?w=450&ssl=1" data-recalc-dims="1">
<p>
Figure 2: Figure 4: R Plot for Step Response of the RLC bandpass circuit
</p>
</div>
</div>
</div>
<div>
<h3>Next Coding Phase</h3>
<p>In the next coding stage, we plan to attempt creating functions that would perform the following:</p>
<ul>
<li>Evaluate transfer function expressions in the <em>s-domain</em> within R as part of the framework.</li>
<li>Control system block diagram algebra functions – system interconnections</li>
</ul>
</div>
<div>
<h3>Challenges</h3>
<p>The expected challenge for this project is to accurately set up a framework that defines data structures and how functions operate within the framework in such a manner that would be attractive (due to simplicity, reusability and extensibility) to prospective users and contributors.</p>
<p>To overcome this challenge, through the assistance of my mentors, we have examined the data architectures and plotting layout of other open-source scientific languages, and from these, designed a plan and framework that would integrate reasonably with the architecture of the R language while remaining open to improvements.</p>
</div>
<div>
<h3>Dependencies</h3>
<p>The Control Systems Toolbox for the R language is expected to depend on R packages like, <code>pracma</code>, <code>signal</code> and <code>expm</code>.</p>
</div>
<div>
<h3>Feedback</h3>
<p>We would be glad to hear from the R community – feel free to <a rel="nofollow" href="https://github.com/rstats-gsoc/gsoc2017/wiki/control%3A-Control-Systems-toolbox#mentors" target="_blank">get in touch</a> with any suggestions, opinions or comments.</p>
</div>
<div>
<h3>Conclusion</h3>
<p>In this article, we have introduced the Control Systems Toolbox GSoC 2017 project, its current state, benefits and practical examples showing some functions that are currently available. We have also listed the future coding plans for the next GSoC coding phase. We hope to be able to update the R community as things develop.</p>
</div>
</div>
<p> </p>
	<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/3BbuJOmMBJY" height="1">