# Tutorial: Installing scicoslab and generating code from a Scicos diagram

(Difference between revisions)
 Revision as of 10:16, 29 September 2011 (view source)Erikadds (Talk | contribs) (→Scicoslab 4.4.1 and ScicosLab Pack 10.0: code generator for FLEX - Installation procedure)← Older edit Revision as of 10:33, 13 October 2011 (view source) (→Your first Scicos application)Newer edit → Line 30: Line 30: of a first simple Scicos example on a FLEX Demo board. The example of a first simple Scicos example on a FLEX Demo board. The example created in this tutorial can be found in the directory created in this tutorial can be found in the directory - scicos_examples/led_sin. + scicos_ee/examples/scicos_flex/Led_sin. + inside scicos pack base directory. If you are looking for a prebuilt example, go directly to the next Section. If you are looking for a prebuilt example, go directly to the next Section.

## Revision as of 10:33, 13 October 2011

This is the Wiki page dedicated to the Scicos code generator originally developed by Roberto Bucher from SUPSI Lugano.

The code of the code generator is distributed under GPL2. The project is currently lead by Roberto Bucher, from SUPSI Lugano, and is hosted on the Evidence web site.

# Scicoslab 4.4.1 and ScicosLab Pack 10.0: code generator for FLEX - Installation procedure

Steps to set up the Scicoslab code generation for FLEX

1. Download and install Cygwin from the Cygwin site. Here is a minimal installation pack. Cygwin is required to compile the code generated from your Scicos diagram.
2. Download Microsoft Visual C++ 2008 from the Microsoft Visual C++ site and install it. It is required from the Evidence Scicoslab pack.
4. Download Microchip MPLAB IDE from the MPLAB IDE site and install it. It is required to program the dsPIC microcontroller mounted on the FLEX board. Then download a C30 compiler, for example from the Microchip MPLAB C30 compiler site and install it. A compiler is required to compile your control application. To program the dsPIC microcontroller you need a programmer. You can buy a programmer for dsPIC from Microchip site. Programmers suggested are: Microchip MPLAB ICD2 or Microchip MPLAB ICD3. See Microchip for more informations:Microchip programmers site
5. Download the latest Scicoslab pack from the ScicosLab pack download page. Unzip the pack and install it. To install the pack execute the installer.sce script file in ScicosLab (File -> Exec...). (If needed, run ScicosLab with administrator privileges). At the end of the installation restart ScicosLab for the changes to take effect.
6. Create and compile your first Scicos diagram (as shown here: An example of code-generation), flash the microcontroller, test your application and ... enjoy!

# Scicoslab 4.4b7 code generator for FLEX - Installation procedure

Steps to set up the Scicoslab code generation for FLEX

1. Download Erika Enterprise 1.5.1 (the executable contains: Erika Enterprise kernel, RT-Druid and Cygwin) from Erika installer site and install it. Then launch the program and update the software with the nightly build version (with Help->Software Updates...) from Erika update site It is recommended to update periodically the program. Update is needed only if you want to use last version of Scicoslab pack.
2. Download Microsoft Visual C++ 2008 from Microsoft Visual C++ site and install it. It is required from the Evidence Scicoslab pack.
4. Download Microchip MPLAB IDE from MPLAB IDE site and install it. It is required to program the dsPIC microcontroller mounted on the FLEX board. Then download a C30 compiler, for example from Microchip MPLAB C30 compiler site and install it. A compiler is required to compile your control application. To program the dsPIC microcontroller you need a programmer. You can buy a programmer for dsPIC from Microchip site. Programmers suggested are: Microchip MPLAB ICD2 or Microchip MPLAB ICD3. See Microchip for more informations:Microchip programmers site
5. Compile you first Scicos diagram, flash the microcontroller, test your application and ... enjoy!

This Chapter will guide you to the creation, compilation and execution of a first simple Scicos example on a FLEX Demo board. The example created in this tutorial can be found in the directory scicos_ee/examples/scicos_flex/Led_sin. inside scicos pack base directory.

If you are looking for a prebuilt example, go directly to the next Section.

## Creating the Scicos example files

(Note the screenshow may refer to old versions of ScicosLab or Scilab 4.1.2).

1. Please start ScicosLab from the Start menu. The ScicosLab window appears.
2. Type scicos(); as showed below in the Figure, and press Enter.
The Scilab splash screen. Type scicos(); to start Scicos.
3. The Scicos windows appears, as showed in Figure
The Scicos splash screen.
4. You need to compile the Scicos blocks, if you didn't do it yet, in order to get the system working. To do that type these commands in ScicosLab window:
cd ("%SCICOSLAB_HOME%\contrib\dspic"); // (%SCICOSLAB_HOME% means the installation path of ScicosLab...)
exec ("builder.sce");
IMPORTANT NOTE: please check the correctness of this path. Newer Scicos Packs are installed under ...\scicoslab-4.4.1\contrib\scicos_ee\scicos_flex\dspic
5. Select Palettes from the Palette menu, as showed in Figure
The Palettes.
6. A little list appear in place of the menu. Select FLEX, as showed in Figure
The Palette list.
7. A windows appears, with some sink blocks specific for the FLEX boards
The dsPIC Palette.
8. Single click on the FLEX-LED block. The window selection moves to the Scicos window. The mouse now becomes a white rectangle of the dimension of the LED block. Single click somewhere in the white part of the window. A LED block is dropped in the diagram, like in Figure
The LED block is dropped in the design window.
• Note: If you need to move a block, go over it with the mouse, press m, then move the block and click on the new position!
• Note: If you need to delete a block or a line, go over it with the mouse, then press d!
• Note: If some garbage appears on the diagram windos, don't panic! Just press r!
9. Open the MCHP16-Sources palette, and repeat the same with the Sine block, placing it on the left of the LED block, as in Figure
Place the Sine block to the left of the LED block.
10. Link the black triangle of the Sine block to the black triangle of the LED block. To do that, press l, then single click on the triangle of the Sine block (the source), then click again on the triangle of the LED block (the sink). See Figure
Sine and LED are now linked.
11. From the MCHP16-Sources Palette, which can be found il the palette list, choose the red clock, and put it on the diagram as shown in Figure
Put the Clock block over the Sine and LED blocks.
12. Now connect the clock signal to the two blocks. To do that, single click on the red triangle of the clock block, then single click below it, then single click over the Sine block, then click on the red triangle of the Sine block. After that, single click on the line below the clock block, hit the key 'L' of keyboard, then over the LED block, then on the red triangle of the LED block. The result is shown in Figure
The Clock block is connected to the Sine and LED blocks.
13. Single click on the Clock block. Its properties window appears. Leave them untouched, and press OK. You can do the same on the Sine block. The two Figures below show these windows.
The Clock block properties.
The Sine block properties.
14. The code generator can produce code which only comes from a special block named Super Block. For this reason, we need to create a Super Block enclosing the Sine and the LED blocks. To do that, select the Region to Super Block menu item from the Diagram menu (see Figure below).
The Region to Super Block menu item.
15. Then, draw a selection which includes the Sine, the LED, and the red lines in a way that only one red line exits the selection, as shown in Figure
The selection made to create a Super Block.
16. As a result, a Super Block is created (see Figure)
The Super Block.
Which contains the Sine and LED blocks. To see these blocks, just single click on the Super Block, and another window will appear (see Figure).
The contents of the Super Block.
17. Please note that this window is very similar to the previous one except that the clock object is substituted by a placeholder signed with the number 1.
• Note: The Diagram containing the Super Block is disabled when the Super Block diagram is displayed. Only one window can be enabled at a time in Scicos. The limitation will be removed in the next version of Scicos.
18. It is now time to save the two diagrams. From the File menu, choose Save as. Save the diagram containing the Super Block as led_sin.cos.

## Generating dsPIC code from a Scicos Diagram

It is now time to generate the code for the example we just created.

Note: A copy of the file created in the previous steps is included inside the scicos_examples/led_sin directory. To open it, double click on the scicos_examples/led_sin/led_sin.cos file.

1. Select EmbCodeGen from the CodeGen menu (see Figure).
2. A window appear, like the one in Figure
The CodeGen dialog box.
3. You can specify the directory where all the files will be created by modifying the Created files Path textbox. Please leave the other options unchanged.
• IMPORTANT NOTE FOR WINDOWS USERS: Always create directories under your User home directory. DO NOT USE other directories (such as C:\xxx), strange problems may happen. Note: If you are using Windows Vista, then the workspace directory c:/Users/<username>/workspace works.
4. Press Ok. As a result, a set of files are generated in the output directory.
5. Then, Scicos automatically opens a console window, running in it the following commands:
• the RT-Druid template generator to instantiate the Scicos template application;
• the RT-Druid standalone code generator to produce the ERIKA Enterprise configuration files from the generated OIL file;
• the make application to compile the code.
6. The result of the code generation process is depicted in Figure \ref{fig:console}.
The compilation console.
7. The executable file is named pic30.elf and it is located inside the Debug directory as usual for all the ERIKA Enterprise applications.
8. You can now program your application on your FLEX board. To do that, you need to open MPLABIDE as you usually do to program other ERIKA Enterprise applications. Please refer to the ERIKA Enterprise tutorial for dsPIC for more information.
9. Running the code on your FLEX board has the following behavior: the system led on the board flashes with a period of 20 seconds, and a duty cycle of around 6 seconds over 20. The explanation is the following:
• The system works like a synchronous control system, with a sampling frequency of 0.1 secs
• The Sine block output is a sinus with a frequency of 0.05, which correspond to a period of 20 s
• The LED block is directly linked to the system led, and is programmed to put on the system led when its input is greater than 0.5.
• Looking at the Figure below, it is clear that the sinus has a value greater than 0.5 for around a third of its period. Given that, the system led is on for around 6 seconds over 20.
A graphic of a Sine and of a constant value 0.5.

# Internals of the genareted code

## Templates and customization of the generated application

The default application wich is generated by the Scicos embedded code generator for dsPIC generates a basic application which uses ERIKA Enterprise with the FP kernel, a periodic task and an Alarm triggered by a timer interrupt to activate it.

In general, it is likely that advanced users would like to customize the application which is generated by the code generator, to add other activities to be executed concurrently with the code generated from the Scicos design. Examples of this activities could be for example background activities for reporting, supervision, display, debug, and so on.

Implementing such variations is very easy, because the application scheleton used by the code generator is contained inside a RT-Druid template. In particular, the default template is the pic30_empty_scicos template stored inside the examples/pic30/pic30_scicos directory under the dsPIC examples plugin in the Eclipse installation. The user can add a new template using the following steps:

1. Copy the examples/pic30/pic30_scicos directory in another location under the examples directory;
2. Change the ID of the template by modifying the template.xml file contained inside the directory. The ID is specified in the second line of the XML file as follows:
 <evidence_example version="1" ID="pic30_empty_scicos">
3. Change the files included in the new template. If you need to add a new file, please remember to add it in the corresponding list in the template.xml file.

Finally, specify the new template when generating the code in the Template textbox.

## Assumptions of the default template

The code generated by the Scilab/Scicos code generator for FLEX uses the template named pic30_empty_scicos, and has the following symplifing assumptions:

1. There is a single sampling time T_s in the system;
2. T_s is forced to 1 ms;
3. Every sampling time specified by the user under the Scicos design will be rounded to a multiple of a millisecond;
4. An ERIKA Enterprise counter is linked to the a periodic timer;
5. The periodic timer used in the dsPIC hardware is set to raise an interrupt every 1 ms;
6. An ERIKA Enterprise alarm is attached to the counter, to periodically activate a task;
7. The task body just calls the routines generated by the Scicos code generator. Which executes the functions you specified in the design;
8. The PWM object has a fixed period of 1 ms. This means that if the sampling period is a multiple of T_s, then the PWM will repeat the same duty cycle until the PWN value is changed;
9. The A/D converter always works on demand, meaning it always executes the following steps:
• selects a channel;
• starts the conversion;
• waits for the end of the conversion (typically max 10 usec)
• it converts the result in a value from 0.0 V and 3.3 V