2f5d thomas' tech blog thomas' tech blog - all about nerd stuff @ thomashauer.at

# PI – controller’s and LTSpice

December 16th, 2012 by Thomas

As a reader of my blog you know I’m a very intense user of LTSpice. This time I want to write about controllers in LTSpice and one of the most common and versatile controller is the PI – controller.

// PI – controller theory

The PI-controller consists of a very simple formula:

$V_{(t)} = K*(e_{(t)}+\frac{1}{K_1}*\int \! e_{(t)} \, \mathrm{d} x)$

In this formula the $e_{(t)}$ represents the error function. This error function is the deviance between the given value and the output value of your system. In simpler words when you want to have 5V as output of your system but the actual output is 7V the $e_{(t)}$ has the value of -2.

So the e(t) formula has to like this:

$e_{(t)} = V_{ref} - V_{out}$

The rest of the components setting the behaviour of the controller and are very well described at the wikipedia article about PID-controllers i want to refer to.

// PI – controller at LTSpice

Controllers are not implemented in LTSpice by default, you have to create them by your own. LTspice offers a voltage (or current) source what is called “arbitrary behaviour voltage (or current) source” short “bv” or “bi”. This source can be every thing you want and in our case its a PI-controller. To be able to create a controller type

V=K*((V(out)-V(ref))+(1/K1)*int(V(out)-V(ref)))

in the value field of the source. As you can see there is a “V(out)” and a “V(ref)” parameter in the formula. The “V(ref)” represents the controller input and in LTSpice it can be a voltage source with the label net “ref”. The “V(out)” represents the output of your system, this is also a label on a net you want to control with the PI-controller, in my case here the net is called “out”. The values K and K1 are the property values of the controller and have to be replaced by a number or a LTSpice parameter.

// LTSpice – file and hints

Like in the last LTspice tutorial “Monte Carlo Simulation” i did an example file to make your start with PI-controllers in LTSpice easy!

In this file I created a linear voltage controller based on a PI-controller. Feel free to play with it, improve it and create something totally new with it!

A important HINT at the end, be careful with creating PI-Controlled BUCK-converters. Adding a PI (or PID) – controller to a buck converters causes some instabilities and the result of your system will not be the result that you expected! For buck-converter controlling other controllers have to be used!

2670

# electronica fair 2012 munich – my first visit

November 16th, 2012 by Thomas

This week the electronica fair took place in Munich and it was my first visit there and my very first exhibition visit with RECOM.

// First impressions
In my preparation for this exhibition I already found out that this fair is not a small one, but at the arriving I saw how huge it is in reality! I was very impressed (still impressed to be honest).

// The exhibition
The first day i had no meetings and I used it to take a walk around the exhibition and do purchasing. Normally i hate purchasing, browsing through the web, across websites, never sure if I’ve overseen something. Purchasing on exhibitions is the opposite – it’s fun! You can directly talk to the people of the company and they can show their products and can provide additional ideas to make the product better. Another reason for purchasing at electronica is that there are all who play a role in the market so you are able to do your purchasing stuff in 2 hours instead of a day of work.

// The exhibitors
The whole world enjoyed the electronica in Munich. Almost all global big players were there, except Xilinx was missing for some reason – if you know why, please leave a comment below! I would really like to know why they were not there!
A thing that I really liked, was that the US government helped smaller local companies to get there. There was a section with smaller companies from the US including an US lounge for meetings and talks.

// What was missing
Not much was missing for me, except a free Wi-Fi access for all. In times like these it’s not complicated to serve free Wi-Fi for all. I really wanted to tweet live from the exhibition but without Wi-Fi it’s a problem. There was a LAN – lounge but sadly my smartphone has no LAN-connector and starting my Netbook for every tweet is more than silly.

# Monte Carlo Simulations With LTSpice

August 30th, 2012 by Thomas

LTSpice is as a free available software a very complete and mighty tool but for me there is an important part missing – the Monte Carlo Simulation. Here is a short introduction how to add a Monte Carlo Simulation to your LTSpice simulation.

// What is it good for

Schematic simulators like LTSpice simulate your schematic with exact that value you define at the part in the schematic. In real life no component has exact that value you want. For example a standard SMD – resistor i use can have a deviation of 1% of the nominal value. Same with capacitors, inductors and fuses (the can have deviation up to 20% (!) ). This means for you and your schematic, when you build for example 10 amplifier pcb’s every single pcb will have a small random deviation in its amplification to each other – welcome to the analogue world.

Monte Carlo simulation can help you to figure out the deviation you will get in your schematic in real live and you can determine the parts which have a major impact to the deviation and based on this you can choose a type with a greater accuracy.

// How to do in  LTSpice

First think of we have to do to to achieve Monte Carlo simulation.

1. Define a function which delivers the component value with random deviation (for a better usability)
2. Give the parts with deviation the random deviation value
3. Simulate
4. Repeat steps 2 and 3 many times

This seems to be magic in LTSpice but it isn’t.

First the  function. Below you can find out how you can define a function in LTSpice. The function is called “normal” and has the arguments “nom” for nominal value and “tol” for tolerance. In the body of the function is the expression “nom*(1+gauss(tol))”. Gauss(tol) deliveres a random value from -1 to 1 based on the tolerance value. This is how we get our random component value based on the nominal value and the tolerance of the component.

.function normal(nom,tol) nom*(1+gauss(tol))

To set the component value the “.param” value has to be used. As you can see below i use the function i defined above.

.param L1=normal(100m,0.01)

To make LTSpice repeat the simulation several times with new random values add the line below to you simulation. It is like a for – loop with the variable “run”, it starts at 1 and stops at 50. In every repetition LTSpice sets new component values with a random deviation, so in every repetition the solution of the simulation will be a different one.

.step param run 1 50 1

As you can see the LTSpice Monte Carlo simulation is pretty easy. Have fun!

Here you can download a LTSpice file i made during my experiments with the Monte Carlo simulation.

3337

# basic vector and matrix operations tutorial in GNU Octave

August 11th, 2012 by Thomas

In my last blog entry i introduced octave to you. This time i want to introduce the first basic vector and matrix operations.

// Vector

To create a column vector just type in

v0 = [1;2;3];

To create a row vector just type in

v1 = [1 2 3];

// Matrix

A matrix can be defined in some different ways. First you can type in every single number

m0 = [1 2 3; 4 5 6; 7 8 9];

A matrix can also consists of vectors

m1 = [v0 v0 v0]; %for a matrix consists if column vectors
m2 = [v1; v1; v1]; %for a matrix consists if row vectors

Gnu Octave has some predefined matrices to reduce the amount of work for the engineer. Here is a list of some of them. There are more predefined but this list of it i used by myself and i know that they are working.

m3 = eye(row, column); %creates a diagonal matrix m4 = zeros(row, column); %creates a matrix with zeros m5 = ones(row, column); %creates a matrix with ones m6 = rand(row, column); %creates a matrix with random numbers

// Operations

The most basic operation is multiplication of a matrix with a vector. It can be written like below.
If the formats of the matrix or the vector are not compatible for some reason octave responds with
an error – message. This operation can also be made with matrices but pay attention to the formats of
the matrices!

v2 = m0*v0;

There are some basic matrix operations which are built in in octave. Here is a (not complete) list of it.

> m7 = inv(m0); %inverts the matrix m8 = m0'; %transposes the matrix</span> m8 = transpose(m0); %transposes the matrix</span>

// Future outlook
In this blog i made a review of some basic operations and pre-built-functions. I will try to continue that
and provide more detailed information in numerical calculation with Octave.
If you have some questions just comment!

# First Glance At GNU Octave

July 11th, 2012 by Thomas

// What is GNUOctave
GNU Octave is an open source alternative to Matlab. Its made for numerical calculations in linear and non linear problems and as a alternative to Matlab its able to read m-files written in Matlab.
Octave is not only available for Linux, there is also a Windows version named GUIOctave, more information about that you can read in the point “Octave For Windows” below.

// First Steps
First of all you have to install Octave to your system. With Linux it is available at the Synaptic Package Manager and the Ubuntu Software Center. I installed it via the Synaptic Package Manager because there are a lot of additional packages available.

When Octave is installed you can start it normally and in Linux a terminal window will appear. It should look like this:

Octave Start Screen

Now you can type in different math operations like:

First Inputs

Of course Octave can also provide plotting functions. The input can look like this:

Plot Input

and the resulting plot looks like this:

Plot Output

When you want to execute more than one command at once just hit shift-enter for a new line for commands and when the input is finished hit enter and the Octave input is going to be executed.
If you want to hide the result of every line, because its to much or not from interest just place a “;” at the end of the line.

// Run Scripts
If you are an experienced Matlab user you know the m-miles or script files that Matlab can execute – Octave can serve this functionality too.
To write scripts you can use any notepad or text editor you want. For octave I use emacs, because it has syntax highlighting for octave. Emacs can be also installed very easy through the software centers at Linux. To start emacs just write “edit” in octave and hit enter. It will start emacs and when you are finished with coding, just close it and you get back to Octave and run the script.
Maybe you give it a try and write the commands above to a script and try to execute it in Octave – keep in mind that Octave has to work in the folder in which the file is and you only have to enter the name of the file not the ending (for test.m just write test for example).

// Conclusion
I hope i could serve you a little help for the first steps in Octave. I will do some more tutorials for Octave to help you to get started with it. If you have some detailed questions or problems with Octave, just write a comment or a mail and I will try to solve it.

// Outlook
In some of my next blogs i will introduce some packages which are available for Octave users.

// Octave For Windows
For the Microsoft Windows users there is a Octave version for Windows unfortunately it is not open source but i works like the linux version with a GUI. The original download site is still down and the people who are responsible for that site seem to be gone through a black hole so i decided to serve an own download opportunity.

3783

# Store Matlab plot’s as *.jpg, *.png, *.eps or other formats

May 21st, 2012 by Thomas

In a previous post i’ve shown how to generate proper plot’s in Matlab. In many cases in university and working life there is the need to store plots in a certain format to embed them into a document or paper, so it’s not done with displaying proper plots.
In this case Matlab helps the engineers a lot with store functions for their Matlab – plots or code.
To prepare a Matlab plot for printing we have to do a plot first with the “plot” command like i’ve shown in the previous post.
Then we create the figure object to get access to the current plot properties. “gcf” is an object which holds the properties of the current plot.

fig = gcf;

As next step i define the plot size, in my case the size of the A4 paper format

width = 29.7; % cm height = 21; % cm

The paper properties can be set very simple. In the code above i set paper size and the background color.

set(fig, 'Color', 'White'); papersize = get(fig, 'PaperSize'); left = (papersize(1) - width)/2; bottom = (papersize(2) - height)/2; my_size = [left, bottom, width, height]; set(fig, 'PaperPosition', my_size);

Matlab serves a lot of different file formats for output. Two of the i’ve shown below. These are my common outputs, the *.eps output i mostly use in latex – documents and *.jpg output is my format of choice when i use it in word documents.

print('-deps', 'output_files/plotDoc.eps') print('-djpeg', 'output_files/plotDoc.jpg')

If you need other file formats just look at the Matlab online documentation. There is a full list of it.

HINT: When you are going to use a “output_files” – folder like me in this case ensure that the folder has been created before the script runs the first time. Matlab is able to create a file but not to create a folder, so an execution without this folder created will cause an error.

# Energy Micro Gecko EFM32 Starter Kit

April 21st, 2012 by Thomas

This week i had the honour to participate on a workshop with the guys from Energy Micro. At this workshop the guy’s presented their company, their products and they’ve shown some demos of what is possible with their MCU’s.

At the end of the workshop every participant get it’s own EFM32 Starter Kit. In this blog entry I will take an introduction to this Starter Kit and what its all possible with it.

Dave Jones (@eevblog) already did a very good review of the starter kit in his video blog post at youtube. He had a strong focus at the Simplicity Studio and its functions that make it to a very powerful tool. I will take a closer look at the Gecko at the Starter Kit and the power management.

// The Starter Kit

Above you can see a picture of the Starter Kit board.

In the middle of the board, below the LCD is the Gecko. On this board is the EFM32TG840F32 in the QFN64 housing mounted.
This EFM32 comes with:

• 32 KB Flash + 4Kb Ram
• 56 GPIO Pins
• 8×20 LCD driver
• 2 USART SPI I2S Channels
• 1 I²C Channel
• 2 Timers (PWM)
• 1 ADC (on 8 Pins)
• 2 DAC’s
• 3 OPAMPS
• AES
• LESENSE

At the upper right corner you can find the debug port. This port is twice interesting. First with this port you can program the mounted Gecko with an external programmer if you want. Second and the most interesting part is, that you can program other Geckos with this port. For example, you have built your own prototype-board. It is not neccessary to buy a programmer or debugger, you can do it with this board. You only have to set the “Debug Mode” in the energy Aware Commander to “out” and the board will act as programmer to other Energy Micro devices on your prototype-board.

At the lower right side there is the capacitor slider. This slider is used in the demo application as shown in the video – blog of Dave Jones. I made a short video with my where i play around a little bit to show the functionality – OK i played a half a hour but the video only shows 10 seconds of it.

// The Energy Modes

The picture above is showing the components that can be found in an EFM32.

This color marks the ENERGY MODE 4 also named “Shutoff Mode”, as you can see above very few components are working in this mode. The pin reset and the Power-on reset are working in this mode. There you can reset the EFM32 and throw interrupts

This color marks the ENERGY MODE 3 also named “Stop Mode”. In this mode the real time counter, watchdog, external interrupts and some more features are working, and of course all components which are working in the ENERGY MODE 4

This color marks the ENERGY MODE 2 also named “Deep Sleep”. In this mode the LCD controller, the DMA controller are working and of course all components which are working in the ENERGY MODE 3 & 4

This color marks the ENERGY MODE 1 also named “Sleep”. This is the first mode in which the ADC & DAC are working. The analog components of this MCU’s are totally new designed for the Geckos to ensure a minimum of power consumption. Of course here are all components working that are working at EM 4 to 2.

This color marks the ENERGY MODE 0 also named “Run Mode”. As you can see in the picture above this mode is the only mode in which the CPU is running. Due to the fact that the EFM32 has built in a very powerful DMA – controller the CPU can be turned off and kept turned off for a very long time.

// Conclusion

For me the guys of Energy Micro did a great job with this microcontroller series. But the really impressive thing is the simplicity studio. This software tool serves a lot of functions that make engineers live easier and more productive.

4f7f

# LTSpice and Matlab

April 7th, 2012 by Thomas

The studies at a university sometimes serve the students some unwanted surprises. I have to do some homework for a lecture in LTSpice. LTSpice is a wonderful tool for modeling and simulation of electrical circuits, but the plot-export for example, papers is not really satisfying.
To create plots for papers and homework my tool of choice is Matlab, it has the advantage that the programmer can completely configure all plot attributes and use custom values.

Due to the fact that LTSpice has the functionality to export data in a *.txt – file I decided to create a Matlab function for my plot’s and below you can find how I did it.

// LTSpice data export

The data export in LTSpice is very simple. When the plot window is checked, choose file and then export. The appearing window shows the possible plot data’s that can be exported. Just select the data that should be exported and LTSpice does the rest.

The exported file looks like this.

time V(vbiastest) 0.000000000000000e+000 8.199954e-001 2.256562492220837e-006 8.341131e-001 4.209687492220837e-006 8.461137e-001 6.162812492220837e-006 8.577214e-001 8.115937492220837e-006 8.687617e-001 1.006906249222084e-005 8.790685e-001

// Reading the data with Matlab

First of all it is important to read in the data so you can edit it in Matlab. To read in from a file it is necessary to open that file, like in any other programming language too. In Matlab there is the function “fopen(‘Filename’)” which does the work for you. This function returns the file – ID which is necessary for further processing. To read in the file I used the function “textscan(fileID, ‘%s’)”. This function reads the whole file into a string and returns the content of the file in a cell – object. That was the whole magic of reading in a file to Matlab and at the end it is always important to close the file when the read/write process is done so the function “fclose(fileID)” should always be at the end of the read in process.

Here below is the whole commented source code of file reading

fileID = fopen('input_files/opv_input_stage_n_m_test_transient.txt'); fContent = textscan(fileID, '%s'); % reads the whole file into fContent fclose(fileID); fContent = fContent{1}; % making fContent to an n x 1 - Matrix

// Expanding and re-modeling the data

The main and more interesting part is to extract the data from the string array so Matlab can plot it.
As we’ve seen above the file starts with the name of the axis. In the code below you can find the code in which the axis names were stored in the variables, very trivial as you can see.

%% create axis and plot names title = fContent(2);   if strcmp(fContent(1), 'Freq.') xAxis = 'Frequ'; end   if strcmp(fContent(1), 'time') xAxis = 'Time'; end

LTSpice offers the engineer the possibility to simulate an electronic circuit with a parameter sweep. This sweep can be made with the “.step param” – command in LTSpice. In this case the LTSpice-file holds more than one plot. For this case we add a variable to the program for checking if there are more plots to show.
In the source code below the check of the parameter sweep simulation is shown and also the calculation of the step size on the x-axis. For the calculation of the time step the “str2double” – function can be used; it does all the type conversion for you.

%% create axis and plot names %% determine if parameter step simulation if strcmp(fContent(3), 'Step') stepSim = 1; end   %% determine x - axis step size str1 = fContent(3); str2 = fContent(5);   if stepSim str1 = fContent(8); str2 = fContent(10); end   xStepsize = str2double(str2) - str2double(str1);

At this point the Matlab – code has extracted all parameters it needs to store all variables into an array or matrix for the plot.
In the code below the first thing I’ve done is to set the counter variable to the first value that holds a value for the plot. Mind that there is an if-query to check if there are more plots than one to show. In this case the first value is on a different place.
After that Matlab can start to convert all the plot variables from the input matrix into a new plot matrix. In case that there is only one plot to show the while-loop ends with the a-array full of double-values that is at the end of the loop stored to A.

In case that there are more plot’s to show the while loop has to do a lot more work than for one plot. If there is no fixed step size set in LTSpice, it’s a common case that LTSpice changes the step size between the simulations and the simulation-output has different length’s. Arrays with different lengths cannot be stored in the same matrix and it’s also not possible to display it in the same plot. Matlab serves the functionality of linear interpolation in the function “interp1″.
The “interp1″-function does the linear interpolation for us and gives us the array with the length we need for our matrix and we can expand the matrix with the vector.

For those of you that are more interested in this topic i would please you to read the Matlab website at this topic, the link can be found at the end of this blog.

In the source code below all the functionality described above is posted.

i = 4;   if stepSim i = 9; end   while( i

// The plot

The plot is another mighty tool of Matlab. It serves a lot of properties that can be modified or changed. In the source code my most used properties were shown.

I think the source code speaks for it selves and no much explanation needs to be taken. In case you need some, let me know I will help!

%plot figure();   for i=1:length(A(:,1)); plot(A(i,:)); hold on; fig = gcf; end hold off;   grid on;   maxVal = A(1,1); for i=1:length(A(:,1)) if maxVal &lt; max(A(i,:)) maxVal = max(A(i,:)); end end   minVal = A(1,1); for i=1:length(A(:,1)) if minVal &gt; min(A(i,:)) minVal = min(A(i,:)); end end   % Axes propertiesset axis([0 length(A(1,:)) minVal maxVal*1.1]) set(gca, 'Box', 'off', ... 'FontSize', 25, ... 'LineWidth', 2, ... 'TickDir', 'out', ... 'XTickLabel',0:length(A(i,:))/10:length(A(i,:)), ... 'XTick', 0:length(A(i,:))/10:length(A(i,:)), ... 'XGrid', 'on', ... 'XTickMode','man',... 'YTickLabel',0:(maxVal/10):maxVal*1.1, ... 'YTick', 0:(maxVal/10):maxVal*1.1);   xlabel(xAxis, 'FontSize', 60) ylabel(yAxis, 'FontSize', 60) % Line properties line_handles = findobj('Type','Line');   set(line_handles(1), 'LineStyle', '-', ... 'LineWidth', 4, ... 'Color', 'Black');

// Conclusio

This code above shows up how easy and fast a plot routine in Matlab can be written. If you like the code and it helped you feel free to copy it, rewrite it or change it.

Matlab Interpolation

251f

# Android developement boards

March 23rd, 2012 by Thomas

// Android – the playground

At the moment, Android is one of the most popular fastest changing OS at the market and it is open source. But Android is no longer reserved for mobile phones or tab’s only. Due to the fact that this OS is open source it can be ported to many hardware platforms and this is what makes Android to a very powerful tool.

// Android dev – boards

One of the interesting boards that I’ve found was the Atmel SAM9 Board. This board comes with a display, Ethernet interface, two high speed USB – hosts, composite video out and a camera interface. Atmel it selves posted a video on youtube where they present their board with an in house automation software demo.

The Atmel board is one opportunity to work with Android. Due to the fact that it is developed, tested and manufactured by Atmel it has the advantage for beginners of this topic to be save that the combination of hard and software is working.

Alternatively to the Atmel there should be named the pandaboard and the beagleboard.

The pandaboard is based on TI’s OMAP4460 dual core processor. The website of the pandaboard – community serves a lot of informations about running Android on this board including a mailinglist. There is also information about running Ubuntu on this board.

With beagleboard the engineer has the opportunity to choose between either the xM or the BeagleBone. These boards are based on TI ARM Cortex A8 processors.

// The Android breakout

A very interesting tool on the market is the breakout – board for Android devices. The IOIO (pronounced “yo-yo”) enable the engineer to expand its Android device. This board serves a lot of IO ports with dig IO, PWM I²C, SPI and UART functions. It can be connected via USB to your Android device and has a Java API that makes it easy to work with, not only for experienced microcontroller programmers. This board can be controlled by an app on the Android device.

This board is available at the web shop of sparkfun electronics. At the product site is a description of this board, link to a blog post with documented example projects and a lot of documents like the schematics, eagle files, beginners guide and the IOIO – wiki home.

// Summary

This was a summary focused on four boards for android development.  Like Linux Android can pe ported to many different platforms and  the market of such board is fastly growing. Nearly every week a new board appears with some special features. Engineers has the spoilt of choice between all these boards but there is also the advantage that engineers have the opportunity to find a perfect fitting board for their application.

# raspberryPI sale start

February 29th, 2012 by Thomas

Today the raspberry PI foundation made the announcement that there was made a sale and manufacturing partnership and the first units are on the way from china to britain and ready for sale.

The raspberry Pi is a single-board-computer which runs on linux. The goal of the project is to promote studies like computer science and related topics at school level and bring more fun into computing.

Spec’s
There are two versions of this single-board-computer, an A and a B version. Both versions have as main part a broadcom chip, the BCM2835. This chip includes an ARM11 processor with 700MHz, a video core IV GPU and 256MB of Ram.

Model B has

• IO – Ports (UART, I²C, SPI or GPIO)
• HDMI out
• 1 USB – Port
• RCA – Video out
• 3,5mm Audio Jack
• SD/MMC/SDIO Card Slot

Model A has

• same as Model B but including
• 2nd USB – Port
• Ethernet – Port