# Category Archives: Blogs EN

## The Limitations of Classical PID Controller and Its Advanced Derivations

Since founded by N. Weiner in 1947, the control theory has been evolved for more than 60 years and is still full of challenges and opportunities. The most important principle of the control theory, in my opinion, is the feedback mechanism. Without feedback and closed-loop, almost no algorithm and control technique can be implied. The idea of feedback is that by comparing the reference input and the actual output, an error signal can be obtained and then can be used by the controller to trace and eliminate the difference between the input and the output. Apart from Watt’s steam engine, one could say that the first formally implication of (negative) feedback is the amplifier invented by H.S. Black. It is a genius idea when first came out in 1927 and was proved to be an extremely useful way to solve electronic and control problems. The idea of output feedback has also been extended to state feedback and error feedback to achieve state control and estimation in more advanced control techniques.

Classical control is the foundation of control theory and it is more concentrated on analysing the stability and performance of a controlled plant. However, only linear and SISO systems have been discussed in classical control theory. Although traditional control techniques such as PID controller are still widely used in industry, they cannot handle more complex engineering scenarios such as aerospace, chemistry and biology. Another problem of classical control is that all parameters are designed and tuned based on the current system model, in which case the system will be more vulnerable to further disturbance and parameters varying.

In order to solve these problems of classical PID controller which mentioned before, more advanced approaches have been derived nowadays. If using classical approach to control a MIMO system, one should divide the system into different modes and control each mode separately. However if the system inputs and outputs are coupled with each other, it cannot be decoupled and this method will not be practicable anymore. Here comes the state-space method, which solved the limitation of classical control by using state variables. The advantage of state-space is that it can be represented by matrices and such is very computer-friendly. State-space representation is actually defined in time domain instead of frequency domain and every state can have some extend of physical meaning which gives some clues about what is happening inside a controlled plant. One milestone which makes the state-space method more practicable is the invention of Kalman filter. Kalman filter uses a series of history measurements in the presence of noise to estimate the current state of the system. Kalman filter can work as a state estimator or simply a special filter which uses the physical system model to remove the process and the measurement noise.

Optimal control method such as MPC and LQR is another derivation of classical control. In most circumstances, there are more than one possible control inputs which can drive the system to work properly, but we need is to find the optimal one. Optimal control actually transforms the control problem into an optimal problem which tries to minimise an objective function to get the best outcome. Another advantage of optimal control is that it can take constraints into consideration. One defect of PID controller is that it cannot handle system constraints like actuator saturation or output limitation. In the optimal case control, design a controller with constraints could be feasible.

It is also known that no system is constant and some parameters are likely to vary with time or to the working condition. In classical control, the controller is designed just for the current system model and thus may loss performance or even be unstable due to the system change and uncertainties. In such aspect, adaptive control or robust control may be more applicable. Both adaptive control and robust control are designed to cope with uncertainties. The difference is that adaptive control identifies the system model and changes its parameters in real-time, but robust control fixed its parameters after deployed to the plant. For the truth that adaptive control has to calculate the system model every few periods, it needs much more computational time. What’s more, since the control parameters in the adaptive controller are changing every time, it may be difficult to prove its stability.  On the other hand, the gain of robust controller has already been designed before applied to the system, so it doesn’t need to do additional calculation during the operation. Since robust controller is globally optimised and especially designed to handle uncertainties, it may not have a performance as good as other controllers. But since the real control problems are always not ideal, it is meaningful to take uncertainties and disturbance into the system model.

Some more advanced control techniques such as neural network and expert control are being discussed today. In my opinion, these new approaches have the potential to be the next generation of control theory. With the developing of computer science, it is now possible to model extremely complex networks. This kind of controller can actually take all the possible system states and its corresponding solutions into a database and each time just search for the best solution according to the current system data.  New techniques such as machine learning can also be absorbed into the controller and make the controller more flexible which can handle different control problems using a same configuration.

However, no matter how powerful the control method is, there are rarely situations where we do not need to make trade-offs. As human-beings, we always need to make decisions and balance the income and the expense. Being too greedy is like giving an infinite gain to a helicopter, which may work at the beginning but will suddenly crash whenever there is any disturbance. So push yourself while keep in mind that you have limitation. Take it easy, be adaptive to the environment and always try to get the optimal solution of your life.

## REFERENCES

[1] R.C. Dorf & R.H. Bishop, Modern Control Systems (Twelfth Edition), Pearson, USA.

[2] Wikipedia, Harold Stephen Black. Available at: http://en.wikipedia.org/wiki/Harold_Stephen_Black. Last accessed 26th Mar 2014

[3] E.F. Camacho and C. Bordons, Modern Predictive Control, Springer, London, 2003

## Py | Weather Report and Forecasting with Python

OpenWeatherMap.org is a free weather forecast website, where you can get up-to-date weather reports. One nice thing about this website is that it does provide weather APIs which you can use to fetch current weather data, and use them to build your own weather-forecasting program. In this post, I will describe how to use python to access the weather APIs and fetch the current weather information.

The full documentation of the APIs can be find here: http://openweathermap.org/api. To access the APIs, you have to register a new account to obtain an API key. There are two categories of APIs that can be used for free: Current and 5 day forecast. The Current API provides up-to-date weather data, which includes current temperature, humidity, wind, etc. The 5 day forecast gives information of the weather data for the next 5 days in every 3 hours. So you will get 40 forecasts in total. The usage of the APIs is simple, just send request to specific URLs, and get data in return given in the form of json or XML.

Here I give an example of how to use the Current API:

a) Fetch data from the URL provided by the document:

# this is where you should put your OpenWeatherMap API key
# get your own key from http://openweathermap.org/
API_KEY = "5eb19937b8ba9aa2029fe7e290b8d799"

# this is the city you want to enquiry
CITY_NAME = "York,uk"

# get weather data from weather.com
try:
f = urllib2.urlopen('http://api.openweathermap.org/data/2.5/weather?q=' \
+ CITY_NAME + '&units=metric' + '&APPID=' +  API_KEY)
except:
print 'Execption: URL is not correct!'
raise


b) The default data format is json, we can unpack it with the json library:

# unpack json package into python dictionary

# print raw data with pprint
pp.pprint(weather_dict)


what you will get is something like this:

{   u'base': u'cmc stations',
u'clouds': {   u'all': 36},
u'cod': 200,
u'coord': {   u'lat': 53.96, u'lon': -1.08},
u'dt': 1447431162,
u'id': 2633352,
u'main': {   u'grnd_level': 1016.58,
u'humidity': 89,
u'pressure': 1016.58,
u'sea_level': 1027.66,
u'temp': 6.99,
u'temp_max': 6.99,
u'temp_min': 6.99},
u'name': u'York',
u'sys': {   u'country': u'GB',
u'message': 0.0106,
u'sunrise': 1447399715,
u'sunset': 1447430881},
u'weather': [   {   u'description': u'scattered clouds',
u'icon': u'03n',
u'id': 802,
u'main': u'Clouds'}],
u'wind': {   u'deg': 257.001, u'speed': 10.62}}


c) Once you have the data stored in a python dictionary, you can generate something more readable and customized:

# print human-readable weather message
print('\n\n' + weather_dict['name'] + ' is ' + \
weather_dict['weather'][0]['description'] + \
', temperature: ' + str(weather_dict['main']['temp']) + \
', humidity: ' + str(weather_dict['main']['humidity']) + '%.' )


which produces:

York is scattered clouds, temperature: 6.99, humidity: 89%.


Accessing 5 days forecast is very similar. But personally I think that API is not very useful, because it packs every 3 hour weather data in a single entity. This means in order to give a day-wide forecast, you have to process the data a little bit (day info is consisted of 8 entities) to find out the maximum and minimum temperatures. Also if you get more than one forecasts spreading in different times of the day (e.g., sunshine between 13:00-18:00 but raining between 10:00-13:00), you have to consider how to fairly describe the weather on that particular day . Due to these issues, I didn't spent more efforts on that but instead switch to other weather providers. If you are more interested in forecasting, just have a try and let me know if it works.

### Appendix. Full Python Code

weather_from_openweather.py

#!/usr/bin/python

import os
import urllib2
import json
import pprint

pp = pprint.PrettyPrinter(indent=4)

# this is where you should put your OpenWeatherMap API key
# get your own key from http://openweathermap.org/
API_KEY = "5eb19937b8ba9aa2029fe7e290b8d799"

# this is the city you want to enquiry
CITY_NAME = "York,uk"

# get weather data from weather.com
try:
f = urllib2.urlopen('http://api.openweathermap.org/data/2.5/weather?q=' \
+ CITY_NAME + '&units=metric' + '&APPID=' +  API_KEY)
except:
print 'Execption: URL is not correct!'
raise

# unpack json package into python dictionary

# print raw data with pprint
pp.pprint(weather_dict)

print('\n\n' + weather_dict['name'] + ' is ' + \
weather_dict['weather'][0]['description'] + \
', temperature: ' + str(weather_dict['main']['temp']) + \
', humidity: ' + str(weather_dict['main']['humidity']) + '%.' )


## Configure OpenCV-Python development environment in Windows

In older days, it is relative complex to configure a OpenCV development environment. You have to install an IDE, e.g. Mircosoft Visual Studio, and have to set many configurations of the project, such as the include folder, the header folder and where the IDE can find the third-party OpenCV library. But after Python becomes one of the main streams, things has changed. Python has proven to be a better and easier way to program and test computer vision programs, compared with C++. In this article, I will show you the simple steps that could help you to configure an OpenCV-Python ready environment in Windows.

1. First, you have to download and install Python 2.7 and Numpy. Install them to their default locations, in order to avoid any further issues. The reason to use Python 2.7 instead of Python 3.x is that Python 2.7 is best supported by most third-party libraries, while Python 3 is too new for all the scientific libraries to be fully supported.

2. Download the latest OpenCV release from Sourceforge and extract it into a system folder, e.g., C:\Program Files\opencv.

3. Goto $(opencv folder)\build\python\2.7\x86 or \x64 based on your machine type. Copy cv2.pyd to$(python folder)\lib\site-packages.

That's all you have to do. Really easy and will only cost you 10 minutes. Now let's open the Python IDLE and test if the environment is working:

import numpy
import cv2

print cv2.__version__


Run the code and if you got the version printed and no error message, then congratulations: you have succeed. But if you did get error message, then you should check the version of both the Python and the OpenCV is correct. If you have multiple versions of Python, then check if you have run the right version of IDLE.

Before we try more complicated vision algorithms, let's simply connect a USB camera first and try to get the video stream from the camera and display it on the screen:

import cv2

cap = cv2.VideoCapture(0)

while(True):
cv2.imshow('camera', frameInput)

if cv2.waitKey(33) == 27:
break

cv2.destroyAllWindows()
cap.release()


What you should got is a pop-up window displaying the current image captured from the camera. You don't have to understand this code at the moment, but the thing is once you got the live video stream from the USB camera, further steps can be done to process, modify and save it. More details and examples will be discussed later in my further blogs.

References
[1] OpenCV-Python Tutorials, Install OpenCV-Python in Windows, available at: http://opencv-python-tutroals.readthedocs.org/en/latest/py_tutorials/py_setup/py_setup_in_windows/py_setup_in_windows.html

## Hard v.s. Soft real-time systems

When you study your first lecture in Embedded Systems, you will definitely hear the term 'hard' real-time and 'soft' real-time. Systems such as avionic systems, automobile engine control systems and cardiac pacemakers could be included in the category of hard real-time systems, while web servers, human-machine interfaces and multimedia systems are soft real-time systems. By intuition, we can infer that these hard real-time systems are more critical and important than the soft ones. But what is the really distinction between them?

To answer this question, we should first introduce the notion of 'timing requirements'. Timing requirements, which is also known as timing constraints, are metrics that specify the temporal behaviors of tasks during run-time. Two most important parameters when defining timing requirements are the response time (R) and the deadline (D). The deadline of a task is the time instant by which the task should be completed and the response time is the execution time between the release and the completion of the task.

Now we can give a formal definition of hard real-time: a hard real-time system is a system where all tasks in that system should be completed before their own pre-defined deadlines, i.e., $R_i <= D_i$ . Any failure to meet the deadline could possibly lead to disastrous consequence, e.g., chemical leakage, airplane crash or life risk.

When it comes to soft real-time systems, the deadline could be missed occasionally with the only consequence that the usefulness of the result could be degraded. Take the MP3 player system as an example. The failure of delivering the audio steam in time will only cause some annoying lags, but will not do any harm to the person who is listening to it.

Since hard real-time systems require a high level of predictability, the specification is often given in a deterministic way. On the other hand, the timing specification of a soft real-time system is often defined in a probabilistic form, e.g., the probabilistic of deadline misses in every 5 minutes is less than 1%.

The high requirement of temporal behavior of hard real-time systems poses a great challenge in designing such systems. Many restrictions should be imposed on the design of the software, as well as the hardware architecture. Formal methods should also be used to prove the system could work properly under any possible conditions without any deadline miss.

In reality, not all systems can be identified as either soft or hard real-time systems. In these circumstances, the system can be separated into the hard part and the soft part sub-systems and be designed and verified separately, in order to meet their own specifications.

Reference
[1] Jane W. S. Liu, Real-time Systems, Prentice Hall, 2000, page 26-33

## Smart Home | Update the smart home architecture to v1.2

The architecture of the Yunfei Smart Home project is changed again. This time the system adopt the new bought HP ProLiant Microserver Gen 8 as the main server, instead of using an ARM-based embedded system. This evolution improves both the storage and computation capacity of the server, which makes it possible to deploy more complex nodes in the system and to provide a better user interface. The 12TB maximum storage capacity of the server also makes it an ideal NAS server for storing personal photos and videos!

## Real-time Scheduling in a big picture

Real-time Scheduling Theory has been developed over last 40 years, since the first published work of Liu and Layland in 1973. Before that, real-times systems were designed with cyclic executives, which is in a ad-hoc manner and very difficult to maintain. In general, the theory is consist of task modelling, scheduling policy and schedulability test. Some other issues, e.g., resource management, response time analysis, worst-case execution time analysis, are also involved in the design of real-time systems. Here is a big picture of the real-time theory, based on my knowledge so far:

## How to Make a Halloween Pumpkin (with Arduino)

It's Halloween time again! Today I gonna make a Halloween pumpkin!

Wait... As a geek, why not make a Arduino powered pumpkin with wield lighting effect?

Sounds great, so let's start.

1. Firstly, prepare a medium size pumpkin, of course. Open the top and make it as a lid.

2. Then clean the inside and sketch the pattern you want with a mark pen. Carve the pattern with a knife. It is not as difficult as it looks like and it only took me 10 minutes. This is what I have done here:

3. Prepare your Arduino (I used UNO here), LED source (I used LoL led shield) and power. Program the Arduino with the LED pattern you like. I wrote a program with the LED fading in and fading out like breathing and it turned out to be great.

4. To protect the circuit, I suggest you use a film to cover the Arduino before put it into the pumkin:

5. Power the Arduino and now you got a really scary Arduino pumpkin now. Happy Halloween!

## Started to learn Ada and Real-Time Java

Having been mainly using C/C++ in real-time systems for many years, this is my first time to seriously consider other languages which has the capability and primitives to handle concurrency and meet the requirement of hard real-time systems.

During my time looking for a research position in Real-time Systems Group at University of York, I firstly realized there is another language - Ada - which has been used in military, aerospace and industrial systems for more than three decades. It is a shame that I didn't get a chance to know its name, even I was working in industrial and automotive filed. Ada has a fruitful environment for real-time systems design and was first introduced by the US Defense. It has advanced features such as rum-time checkingparallel processing and OOP support.

To be honest, according to my previous experience with the Java language, I never expect it to be used in real-time systems. However, things are changing and there is a trend to use RTSJ (Real-Time Specification JAVA) in control and military peripheral systems (but not core systems). I think it will take a long time before RTSJ displaced the dominant position of C in embedded systems, but I think it is worthy to learn the language features that Sun and IBM are trying to change with current Java language.