Thanks to visit codestin.com
Credit goes to sg.erturk.me

My blog

Using floats with sprintf/snprintf in embedded C

In my situation, I’ve following configuration

image

Somewhere in project, I need to use float formatting

This works with integer formatting

    char s[24];
    snprintf(s, sizeof(s), "%05u", 32767);

However it doesn’t work with float formatting

    char s[24];
    snprintf(s, sizeof(s), "%06.3f", 32.767);

Then everything works with memory allocation as follow

    char *s;

    s = (char*) malloc(24*sizeof(char));
    memset((void *)s, 0xff, 24*sizeof(char))

    snprintf(s, 24, "%05u", 32767);
    snprintf(s, 24, "%06.3f", 32.767);

What’s wrong with the code without memory allocation?

Share this
Share this
Permalink

ARM CMSIS DSP Software Library - Complex FFT Frequency Bin Example

In my recent project, I’m using STM32F429i Cortex M4 with FPU, and I need a Signal Processing Library. So I decided to use ARM CMSIS DSP Software Library, and try Complex FFT Frequency Bin Example. My configuration as follow.

image



- STM32F429i Discover Board
- Emblocks IDE 2.30
- ARM GCC Compiler 4.7 (Emblocks - bare-metal)
- Compiler options with harf-FPU (-mfloat-abi=hard)
- CMSIS library “libarm_cortexM4lf_math.a”

Unfortunately, the original example did not terminated successfully, and falled into hard fault. After some investigation, I figured out the problem is related with bit reversing flag. When the example without bit reversing terminates successfuly, ofcource it is not useful.

The reason of this problem is the pre-built GCC library has not the function “arm_bitreversal_32”, because a reason regarding the source written in assembly language. So in my situation following workaround succeeded.

  • Re implement the function “arm_bitreversal_32” with another name like that “sg_bitreversal_32” for avoiding to call original bit reversing function, the code as follow
;/**
;  ******************************************************************************
;  * @file    sg_bitreversal_32.S
;  * @author  ierturk @ StarGate
;  * @version V1.0.0
;  * @date    24-May-2014
;  * @brief   Re implement bit reversal
;  ******************************************************************************
;  * @attention
;  *
;  * <h2><center>&copy; COPYRIGHT 2014 SarGate Inc</center></h2>
;  *
;  * Unless required by applicable law or agreed to in writing, software
;  * distributed under the License is distributed on an "AS IS" BASIS,
;  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
;  * See the License for the specific language governing permissions and
;  * limitations under the License.
;  *
;  ******************************************************************************
;*/
    .syntax unified
    .thumb

    .global sg_bitreversal_32

sg_bitreversal_32:
    ADDS     r3,r1,#1
    CMP      r3,#1
    IT       LS
    BXLS     lr
    PUSH     {r4-r9}
    ADDS     r1,r2,#2
    LSRS     r3,r3,#2
sg_bitreversal_32_0:
    LDRH     r8,[r1,#4]
    LDRH     r9,[r1,#2]
    LDRH     r2,[r1,#0]
    LDRH     r12,[r1,#-2]
    ADD      r8,r0,r8
    ADD      r9,r0,r9
    ADD      r2,r0,r2
    ADD      r12,r0,r12
    LDR      r7,[r9,#0]
    LDR      r6,[r8,#0]
    LDR      r5,[r2,#0]
    LDR      r4,[r12,#0]
    STR      r6,[r9,#0]
    STR      r7,[r8,#0]
    STR      r5,[r12,#0]
    STR      r4,[r2,#0]
    LDR      r7,[r9,#4]
    LDR      r6,[r8,#4]
    LDR      r5,[r2,#4]
    LDR      r4,[r12,#4]
    STR      r6,[r9,#4]
    STR      r7,[r8,#4]
    STR      r5,[r12,#4]
    STR      r4,[r2,#4]
    ADDS     r1,r1,#8
    SUBS     r3,r3,#1
    BNE      sg_bitreversal_32_0
    POP      {r4-r9}
    BX       lr

;/************************ (C) COPYRIGHT StarGate Inc *****END OF FILE****/
  • Declare the new function in the C code as follow
extern void sg_bitreversal_32(
        uint32_t * pSrc,
        const uint16_t bitRevLen,
        const uint16_t * pBitRevTable);

  • do FFT without bit reversing as follow
    ....
    uint32_t doBitReverse = 0;
    ....
    arm_cfft_f32(&arm_cfft_sR_f32_len1024, testInput_f32_10khz, ifftFlag, doBitReverse);
  • do bit reversing as follow
    sg_bitreversal_32((uint32_t *)fftBuf, arm_cfft_sR_f32_len1024.bitRevLength, arm_cfft_sR_f32_len1024.pBitRevTable);


If everything went well so far, modified example as described above should run as the original one with prebuilt library.

Please feel free to contact us if you have any further question.

References

All rights reserved. COPYRIGHT 2014 StarGate Inc.

Share this
Share this
Permalink

Getting Started Code Generation With Epsilon

While evaluating code generation tools under Eclipse, I tried Xpand and Acceleo but I didn’t succeed with them for Papyrus UML models. So recently, I decided to try Epsilon Modeling Tools. I tried it for GMF tools generation with Emfatic and EuGENia. Epsilon is a family of languages and tools for code generation, model-to-model transformation, model validation, comparison, migration and refactoring that work out of the box with EMF and other types of models.

The example below is inspired from this article. In this example, we’ll try to generate an HTML Form model which is prepared with Papyrus UML. Papyrus is a tool for modeling with UML, SysML and ModelicaML.

Modeling HTML Form
We assumed that you are familiar wit Papyrus. An HTML form Class is shown in the image below. It is a Papyrus Class Diagram. Model has a Form Class with two type of fields as TextField and MultiLİneTextField which is derived from TextFiled.

image

In the image below, an a HTML form design with the instance of the Class elements in Class diagram. The form has three instance, a Name TextField, an Email TextField, and a Message MultiLineTextField.

image

Creating Epsilon Generator Language Templates
We have two EGL file to generate HTML code, “create.egl” and “uml2html.egl” as follow. The first one is used to call converter, and the second one is used to generate.
create.egl

[%
	var t := TemplateFactory.load('uml2html.egl');
	t.populate ('dummy', null);	
	t.generate ('../out/contact.html');    	
%]

uml2html.egl

[%
var FormAttr : Set;
var FormFields : Set;
var name : String;
var label : String;

for (p in Package.all.select(t|t.type.name.equals("Package"))) {    
    if (p.name.equals("System")==true){
        for (i in p.packagedElement.select(e|e.isTypeOf(InstanceSpecification))) {
            if (i.classifier.first.type.name == "Class") {
                if (i.classifier.first.name == "Form") {
                    for (s in i.slot) {
                        if (s.value.first.isTypeOf(LiteralString)) {
                            FormAttr.add(s);
                        }
                        else if (s.value.first.isTypeOf(InstanceValue)) {
                            FormFields.add(s);
                        }
                    }
                }
            }
        }
    }
}
%]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <title>[% for (attr in FormAttr) { if (attr.definingFeature.name.equals("title")) {
      %][%=attr.value.first.value
      %][%}}%]</title>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
    <div id="page-wrap">
        <form method="post" action="form.php">
[%
    for (field in FormFields) {
        for (s in field.value.first.instance.slot) {
            if (s.definingFeature.name.equals("name")) {
                name = s.value.first.value;
            } else if (s.definingFeature.name.equals("label")) {
                label = s.value.first.value;
            }
        }%]
            <label for="[%=name%]">[%=label%]:</label>
        [% if (field.definingFeature.name.equals("textField")) {%]
            <input type="text" name="[%=name%]" id="[%=name%]" />
        [%} else {%]
            <textarea name="[%=name%]" id="[%=name%]" rows="20" cols="20"></textarea>
    [%}}%]
        <input type="submit" name="submit" value="Submit" class="submit-button" />
      </form>
      <div style="clear: both;"></div>
    </div>
  </div>
</body>
</html>

Launcher Configuration
To initiate launcher is done as follow.

  • Rigt-click on the file “create.egl” in “Project Explorer” and select “Run As > Run Configuration…”. You’ll reach the window below.

image
  • Click “Models” tab. Add the new one as shown the image below.

image
  • To save your run configuration in your project, click the tab “Common”, then click “Shared file:” radio button and select your project location as shown in the image below

image
  • Then click Run, then you’ll have your HTML file in folder “out”. The form looks like as follow.

image


Please feel free to ask any question. The complete Eclipse project can be downloaded from here.

References


All rights reserved. COPYRIGHT 2014 StarGate Inc.

Share this
Share this
Permalink

Continious-Time Simulation on Eclipse with ModelicaML/MDT Plugin

In previous blog post, First Experience on Modelica and ModelicaML, I tried to show how to simulate a discrete time model on OMEdit (Open Modelica Connection editor) and on Eclipse with ModelicaML plugin.

In this post you’ll find simulation of a continious-time model. For this purpose we’ll us a Permanent Magnet DC Machine with a load. Model includes only standart Modelica Blocks. We assume that you have OpenModelica and Eclipse with ModelicaML/MDT plugin installations, and you are familiar with creating Modelica models on OMEdit and ModelicaML model Eclipse/Papyrus.

So, for now, we can skip the steps regarding creations model, then just download the models previously created from the links below. However, please feel free to contact us for furher information.

On OpenModelica OMedit

  • Download the model from here.
  • Open the model on OMEdit
  • Just click simulate
  • Then you’ll get the results
  • Click checkbox the graph you want to see like that below

image

image

On Eclipse/Papyrus with ModelicaML/MDT Plugin

  • Download the model from here.
  • Import project from the archive
  • Switct perspective to ModelicaML Modeling
  • Open the model file
  • Find SimulationModel item in tree on Model Explorer View
  • Right click and select simulation from pop-up menu
  • Then you’ll get the results
  • Click checkbox the graph you want to see like that below

image

image

image

image

All rights reserved. COPYRIGHT 2014 StarGate Inc.

Share this
Share this
Permalink

First Experience on Modelica and ModelicaML

What is Modelica?
Modelica is an object-oriented, declarative, multi-domain modeling language for component-oriented modeling of complex systems, e.g., systems containing mechanical, electrical, electronic, hydraulic, thermal, control, electric power or process-oriented subcomponents. The free Modelica language is developed by the non-profit Modelica Association. The Modelica Association also develops the free Modelica Standard Library that contains about 1360 generic model components and 1280 functions in various domains, as of version 3.2.1.

What is ModelicaML?
Modelica Modeling Language (ModelicaML) is a graphical modeling language for the description of time-continuous and time-discrete/event-based system dynamics. ModelicaML is defined as an extended subset of the OMG Unified Modeling Language (UML). This subset enables the generation of executable Modelica code.

ModelicaML extends the graphical modeling capabilities of Modelica by providing more diagrams (UML diagrams for presenting the composition, connection, inheritance or behavior of classes) for graphical model definition or documentation. Moreover, ModelicaML supports a method for formalizing and evaluating system requirements using simulations.

What is UML?
The Unified Modeling Language (UML) is a general-purpose modeling language in the field of software engineering, which is designed to provide a standard way to visualize the design of a system.It was created and developed by Grady Booch, Ivar Jacobson and James Rumbaugh at Rational Software during 1994–95 with further development led by them through 1996. In 1997 it was adopted as a standard by the Object Management Group (OMG), and has been managed by this organization ever since. In 2000 the Unified Modeling Language was also accepted by the International Organization for Standardization (ISO) as an approved ISO standard. Since then it has been periodically revised to cover the latest revision of UML.

Simulation on Eclipse
When I look for an Eclipse based tools for my sgJavaSci Code Generation project, I tried many basic tools for Modeling like that EMF, GMF, Graphiti, Spray and so on… However Eclipse has more powerful tools based UML on top of basic tools, Papyrus, Sirius for System Engineer. It also has tools for code generation Acceleo and Xtext.

Regarding UML, it can be extended for using on various domain. For example one of them is SysML for system engineering and developing Embedde systems. However, this modeling languages are not for simulation.

Regarding ModelicaML, it can be used for code generation and simulation. A Modelica open source project, OpenModelica already has tools for development graphical and textual, Connection Editor (OMEdit) and NoteBook (OMNoteBook) applications. But output of this tools can not be used for code generation because they are not XML based. That is why ModelicaML was selected for code generation with GeneAuto. Also all of the tools to be used are open source.

You’ll find an example model on OpenModelica Connection editor, and an equivalent model on ModelicaML on Eclipse Kepler below. Also you can download source code of example below.

Example is a discrete model of simple up-counter

Modelica Model on OpenModelica Connection Editor
You should have OpenModelica installation to try this model


image


image


ModelicaML Model on Eclipse Kepler
You should have OpenModelica installation and Eclipse Kepler with ModelicaML plugin to try this model

image

image

image

Sources

References


All rights reserved. COPYRIGHT 2014 StarGate Inc.

Share this
Share this
Permalink

sgJavaSci - Java Based Scilab/Xcos Diagram Importer and C-Code Generator for GeneAuto

What is Scilab ?
Scilab is free and open source software for numerical computation providing a powerful computing environment for engineering and scientific applications.

What is Scilab/Xcos ?
Scilab includes a toolbox called Xcos which is based on Scicos.

What is Scicos ?
Scicos is a graphical dynamical system modeler and simulator. Users can create block diagrams to model and simulate the dynamics of hybrid dynamical systems (both continuous and discrete time) and compile these models into executable code. Applications include signal processing, systems control, queuing systems, and the of study physical and biological systems.

What is GeneAuto ?
Gene-Auto is an open-source toolset for real-time embedded systems. The toolset takes as input a functional description of an application specified in a high-level modelling language (Simulink/Stateflow/Scicos) and produces C (in close future also Ada) code as output.

What is sgJavaSci ?
sgJavaSci is a tool for binding Scilab and Java. The first module if the toolbox is C Code Generator by using above tools.

Currently it has the features as follow

Supported Xcos Blocks

  • SUPER_f defines a system
  • CONST_m defines a constant
  • DOLLAR_m defines a unit delay
  • IFTHEL_f calls a system block according to condition based on an input value (u>0)
  • SUMMATION adds or subtracts inputs
  • SWITCH2_m selects an inputs from two according to conditions based on an input value and a trishold value (u>a, u>=a, u~=a)
  • OUT_f defines an output variable
  • CLKINV_f defines an event or action input


Supported Data Types

  • Double


Supported Block Parameter Types

  • Real Expression
  • Integer Expression
  • Binary Expression
  • String Expression


Features

  • Block Names


Usage

Before using the tool you shoul have programs as follow

  • Eclipse Java Development Environment
  • Java RTE 1.7
  • Java JDK 1.7
  • Scilab


There is no published binaries or Java Byte Code of the tool. You can get it as Eclipse project as follow.

  • You can clone the tool from Git repository

git clone https://bitbucket.org/ierturk/sgjavasci.git

  • You can download as zip from

https://bitbucket.org/ierturk/sgjavasci


Then you can import the project into your Eclipse workspace. If you have Git plugin with Eclipse you can directly import project from above git repository.

Creating Scilab/Xcos diagram
We assume that you are familiar with Scilab/Xcos diagrams. Diagram should be created by using only supported blocks. Parameters should be in type of double and scalar. An example diagram are given below.

image

image

image

Code Generation Process
After creating diagram or already you have a diagram which comply with the condition above, you can run the tool within Eclipse. The window will be displayed as follow.

image

At this stage, the process is as simple as press button and select input diagram and output folder by means menu or toolbar buttons. The steps should be in sequences as in the image below.

image


In the end everything were go well. You will be found C Files in output folder “cfiles”, and GeneAuto XML file in output folder “XML”. Output folder is the one you were select at the step 2.

Other windows you will see as follow along with process

image

image

image

image

After all, c files in output folder, trees views of input diagram XML file and GeneAuto XML file in main window of the tool and a generated C file in Eclipse will be shown as follow

image


I hope that the tool is useful and look for your valued feedback.

References

All rights reserved. COPYRIGHT 2014 StarGate Inc.

Share this
Share this
Permalink

sgJavaSci Scilab/Xcos Tools

image

We have started up a new project sgJavaSci Scilab/Xcos tools to manage Scilab from Java environment. Initially, JavaSci example was imported from Scilab installation. Also a class was created to display Xcos diagram in XML format as SWT/JFace treeviewer.

Current development environment on Windows 7 / 64bit machine as follow

  • Eclipse Luna Release (4.4.0) x86_64
  • IBM 64-bit SDK for Windows AMD64/EM64T architecture,
    Java Technology Edition, Version 7
  • Scilab-5.5.0 64bit

Source code can be downloaded from BitBucket repository.

Share this
Share this
Permalink

Scilab/Xcos Toolbox for Power Electronics and Electrical Machines

Toolbox was updated… It can be downloaded also from BitBucket

SciPowerLab (0.2)
    * Update blocks to comply Scilab API
    * Toolbox title is changed as SciPowerLab
    * Source tree was moved to BitBucket
 – Ibrahim ERTURK <[email protected]> Mon, 25 Aug 2014

XCPL (0.1)
    * Repacked for ATOMS Module
 – Ibrahim ERTURK <[email protected]> Fri, 02 Mar 2012

XCPL_toolbox (0.0)
    * Initial Release
 – Ibrahim ERTURK <[email protected]> Wed, 01 Jun 2011

Previously developed blocks under ScicosLab has been ported to Scilab/Xcos. It can be downloaded from link below. We will be glad to receive your valued feedback.

Usage

  • Run builder.sce in root folder
  • Run loader.sce created by builder macro

Available Blocks

  • xcpl_BLDC
    Brushless DC Machine in abc frame
  • xcpl_BLSC
    Brushless DC Machine Sensored Controller
  • xcpl_PMSM
    Permanent Magnet Synchronous Machine in dq frame
  • xcpl_PMSC
    Permanent Magnet Synchronous Machine FOC Controller
  • xcpl_IMPU
    Induction Machine Per-Unit Model in dq frame
  • xcpl_SVPWM
    Space Vector PWM generator
  • xcpl_PSDINV
    Pseudo Inverter (It is not support three-state switching case)

Available Demos

  • Field Oriented Controlling Permanent Magnet Synchronous Machine

image

Download Links

Or it can be installed on Scilab console as follow

  • atomsInstall(‘XCPL’)

Then restart Scilab after installing.

(via stargatetr)

Share this
Share this
Permalink

Enjoy Scilab and Python - Binding Scilab and Python - SciPyLab

Scilab is a powerful computing platform for engineers and academicians. Python has many scientific tools. While I look for a tools for binding Python and Scilab for a project, I found one on ScilabForge.

Sciscipy: A Scilab API for Python


Unfortunately, it doesn’t work with recent version of Scilab and Python under Windows. The original was coded as pure C extension.

Whatever, I decided to simplify it for my purpose. At the moment, the simplified code is sufficient for my purpose. However, it will be developed. Initial version was uploaded onto BitBucket as Git repository.

Installation and Usage are as follow,

  • Clone git repository onto your computer from

https://bitbucket.org/ierturk/scipylab.git

  • Start a command window, then go to root of your local copy, and issue command below

python setup.py build_ext –inplace

  • Open your favorite Python console then issue commands below

>> from SciPyLab import Scilab
>> sci=Scilab()
>> sci.<any Scilab function>


for example


>> sci.sin(3.14/2)
>> sci.plot([1, 2, 3])


The code tested under following condition

  • Python 3.4 (Anaconda Python distribution from Continuum Analytics)
  • Cython 0.20.2
  • Spyder 2.3.0
  • MSVC 2010 Express
  • Scilab-5.5.0
  • Windows 7 x64
  • Numpy 1.8.2

image
Share this
Share this
Permalink

Drawings of the LHC in the style of Leonardo da Vinci

Dr. Sergio Cittolin has worked at CERN for the past 30 years as a research physicist. He has also made several drawings of the Large Hadron Collider in the style of Leonardo da Vinci.

image

image

Symmetry magazine profiled Cittolin a few years ago.

As a naturalist, da Vinci probed, prodded, and tested his way to a deeper understanding of how organisms work and why, often dissecting his object of study with this aim. “I thought, why not present the idea of data analysis to the world within the naturalist world of Leonardo?” Cittolin says. In the drawing below, the CMS detector is the organism to be opened; the particles passing through it and the tracks they leave behind are organs exposed for further investigation.

Cittolin brings a sense of humor to his work. For example, after betting CMS colleague Ariella Cattai that he could produce a quality drawing for the cover of the CMS tracker technical proposal by a given deadline, he included in the drawing a secret message in mirror-image writing-which was also a favorite of da Vinci’s. The message jokingly demanded a particular reward for his hard work. The completed picture was delivered on time and within a few hours Cattai cleverly spotted and deciphered the message. She promptly presented him with the requested bottle of wine.

Source: kottke.org

(via mrnstrm)

Share this
Share this
Permalink