Difference between revisions of "Matlab/Simulink files"

From OpenMBD
Jump to: navigation, search
(sfcn_signal_tower_chk.m)
 
(4 intermediate revisions by the same user not shown)
Line 9: Line 9:
 
Each layer of the signal tower requires three input ports: one for each RGB color.<br>
 
Each layer of the signal tower requires three input ports: one for each RGB color.<br>
 
As there are four layers, a total of twelve input ports are created in the setup function.<br>
 
As there are four layers, a total of twelve input ports are created in the setup function.<br>
Notice that the DataTtypeID of each port is set to 3, which means that the ports requires data of type uint8_T.<br>
+
Notice that the DatatypeID of each port is set to 3, which means that the ports requires data of type uint8_T. The range of a uint8_T variable is 0-255.<br>
The range of a uint8_T variable is 0-255.<br>
 
 
<br>
 
<br>
 
The S-function requires two dialog parameters:
 
The S-function requires two dialog parameters:
*The selected hardware target, which in this example project will always be the Olimexino STM32
+
*The sample time
 
*The selected SPI channel.
 
*The selected SPI channel.
This number of parameters is set by the following line of code:<br>
+
This number of dialog parameters is set by the following line of code:<br>
 
'''block.NumDialogPrms = 2;'''
 
'''block.NumDialogPrms = 2;'''
  
 
== sfcn_signal_tower_chk.m ==
 
== sfcn_signal_tower_chk.m ==
 
'''File location: .\blockset\toolbox\STM32\blocks\sfcn_signal_tower_chk.m'''<br>
 
'''File location: .\blockset\toolbox\STM32\blocks\sfcn_signal_tower_chk.m'''<br>
This function checks if there is more than one signal tower block in the model and will generate an error message.<br>
+
This function checks if there is more than one signal tower block in the model.<br>
  
 
== sfcn_signal_tower_mcb.m ==
 
== sfcn_signal_tower_mcb.m ==
Line 28: Line 27:
 
*'''hardwareID''': which in this example project is always equal to the Olimexino STM32
 
*'''hardwareID''': which in this example project is always equal to the Olimexino STM32
 
*'''channel''': selected SPI channel
 
*'''channel''': selected SPI channel
The function returns a human readable string with information about the each channel.<br>
+
The function returns a human readable string with information about each channel.<br>
Information about the hardware_ID is omitted, because this is always the same for this example project.<br>
+
Information about the hardwareID is omitted, because this is always the same for this example project.<br>
 
The value of the variable '''channelStr''' is what will be used in the C-code. The value of channelStr depends on '''channel''', which is selected by the user in the block's dialog.
 
The value of the variable '''channelStr''' is what will be used in the C-code. The value of channelStr depends on '''channel''', which is selected by the user in the block's dialog.
  
Line 36: Line 35:
 
This file is the target language compiler (TLC) file that contains the code generation specifics.<br>
 
This file is the target language compiler (TLC) file that contains the code generation specifics.<br>
 
The file starts by including the header file ws2812b.h<br>
 
The file starts by including the header file ws2812b.h<br>
This file contains two functions:
+
Furthermore, two functions are implemented:
 
*Start<br>
 
*Start<br>
 
This function is called once when the model starts. In this function the ws2812b_init() function is called for initializing the target hardware.<br>
 
This function is called once when the model starts. In this function the ws2812b_init() function is called for initializing the target hardware.<br>
Line 42: Line 41:
 
*Update
 
*Update
 
This function is called whenever the block must be updated.<br>
 
This function is called whenever the block must be updated.<br>
The function reads the value of each of the twelve input ports by using a MatLAB macro, such as '''%<LibBlockInputSignal(0, "", "", 0)>'''.<br>
+
The function reads the value of each of the twelve input ports by using MatLAB macros, such as '''%<LibBlockInputSignal(0, "", "", 0)>'''.<br>
The rest of the function is implemented in plain C-code. The port values are shifted to the correct positions and finally the ws2812b_write() function is called for assigning the new values to the LEDs.
+
The rest of the function is implemented in C-code. The port values are bit-shifted to the correct positions and finally the ws2812b_write() function is called for assigning the new values to the LEDs.
 +
 
 +
----
 +
Part 1: [[C source code]] - '''Previous''' | '''Next''' - Part 3: [[Creating a block in Simulink]]

Latest revision as of 20:01, 28 February 2019

Creating a custom S-function block requires the addition of four files to the MatLAB/Simulink project.

  • sfcn_<name>.m: A function written in MatLAB code describing the S-function block's basic characteristics, such as the number of input ports and the umber of dialog parameters.
  • sfcn_<name>_chk.m: A function written in MatLAB code for checking prerequisites and/or dependencies of using the block, such as that only a single block might be present in the model.
  • sfcn_<name>_mcb.m: A function written in MatLAB code that is used for setting up the block's parameters.
  • sfcn_<name>.tlc: A function written in both MatLAB code and C-code, that transfers the block's settings, input ports and output ports to the C-code and vice versa.

sfcn_signal_tower.m

File location: ./blockset/toolbox/STM32/blocks/sfcn_signal_tower.m
Each layer of the signal tower requires three input ports: one for each RGB color.
As there are four layers, a total of twelve input ports are created in the setup function.
Notice that the DatatypeID of each port is set to 3, which means that the ports requires data of type uint8_T. The range of a uint8_T variable is 0-255.

The S-function requires two dialog parameters:

  • The sample time
  • The selected SPI channel.

This number of dialog parameters is set by the following line of code:
block.NumDialogPrms = 2;

sfcn_signal_tower_chk.m

File location: .\blockset\toolbox\STM32\blocks\sfcn_signal_tower_chk.m
This function checks if there is more than one signal tower block in the model.

sfcn_signal_tower_mcb.m

File location: .\blockset\toolbox\STM32\blocks\sfcn_signal_tower_mcb.m

The function sfcn_signal_tower_mcb takes two parameters:

  • hardwareID: which in this example project is always equal to the Olimexino STM32
  • channel: selected SPI channel

The function returns a human readable string with information about each channel.
Information about the hardwareID is omitted, because this is always the same for this example project.
The value of the variable channelStr is what will be used in the C-code. The value of channelStr depends on channel, which is selected by the user in the block's dialog.

sfcn_signal_tower.tlc

File location: .\blockset\toolbox\STM32\blocks\tlc_csfcn_signal_tower.tlc
This file is the target language compiler (TLC) file that contains the code generation specifics.
The file starts by including the header file ws2812b.h
Furthermore, two functions are implemented:

  • Start

This function is called once when the model starts. In this function the ws2812b_init() function is called for initializing the target hardware.
The channel parameter is read from the block by using the MatLAB macro %<block.RTWdata.channel>.

  • Update

This function is called whenever the block must be updated.
The function reads the value of each of the twelve input ports by using MatLAB macros, such as %<LibBlockInputSignal(0, "", "", 0)>.
The rest of the function is implemented in C-code. The port values are bit-shifted to the correct positions and finally the ws2812b_write() function is called for assigning the new values to the LEDs.


Part 1: C source code - Previous | Next - Part 3: Creating a block in Simulink