Note: For modeling and simulating, Stateflow blockset must be installed.
For code generation, Stateflow Coder must be installed.
After we are satisfied with the simulation, we can get ready to generate the source code to our FiO target.
We need to add a Digital Output block from RapidSTM32 On-Chip Peripherals sub-library.
The Digital Output block will output control signal from our target to the real-world.
Set output channel to pin A3, A2, and A1 and connect signal lines together, again, by drag and drop with right mouse button.
Do not forget to update our diagram until all blocks display correct sample time.
In this example, the Digital Output block sample time must be updated.
Before we can proceed, we must connect our FiO Std target to the host PC using the USB port.
Next, set the target in USB In-Application Programming Mode.
By... First, Set jumper J9 to logic High.
Second, set the Program Switch to the right.
Third, press the Reset button.
As shown in the video.
We can check whether the FiO target is properly connected to the host PC by issuing the request_product command.
If every thing is set up correctly, we should get a correct response, which is the name of our target.
You can always type "help rapidstm32_blockset" to display most frequently used commands.
When we are ready, just start the Build process.
Go to Tools, Real-Time Workshop then Build Model.
Matlab will go through the Build process from generating the source code, compiling, and downloading the executable to the target.
The executable code is being downloaded to the target.
Note. Upon completion, all the source files are available in the thai_traffic_light_rapidstm32 folder.
We can run the target by setting it to "Run Custom User Program" Mode.
By... First. Set jumper J9 to logic High.
Second. Set the program switch to the left.
Third. Press the Reset switch.
You should see the LEDs, which simulates the traffic lights, go on and off consecutively as designed.
This picture displays the outputs from pin A3, A2 and A1 as measured by an oscilloscope.
Notice that the positive pulse width are correct and match our settings in our Stateflow model.
A few useful tips & tricks are presented next.
You can use Model Explorer to explore and display system parameters.
With Model Explorer you can view and change various parameters of the model.
For example, click on Chart to display various states, events, and data of our Stateflow chart.
Actions for the Stop State are shown here.
You may wonder with a simple traffic light controller why do we need to go through all the hassel of using Finite State Machine model.
It is true that our traffic light controller is simple, and it is provided just as an introduction to Finite State Machine and Stateflow.
Now, click on the Help menu and go to Stateflow demo.
Go to Stateflow demo. You will find a lot of more advanced real-world examples provided by the Mathworks.
One such example is the Fault-Tolerant Fuel Control System for automotive applications.
Such system must be designed to be robust, and can simultaneously handle combinations of several failures or events.
Using a graphical represetation of a complex system together with the ability to simulate and visualize performance of such system,
is clearly an invaluable analysis tools, if not indispensable.
Lastly, in the same folder, you will find another traffic light Stateflow model for the United Kingdom.
With this model, you will find a slightly more advanced use of Parallel (AND) State Decomposition, where the red and yellow lights
must be turned on simultaneously.
Update the diagram until all blocks display correct sample time.
Then, open the Stateflow model, and run the simulation.
Notice that both the red and yellow lights are now on at the same time.
From this video, you have been introduced to one of the most powerful tools for embedded system development.
It is hoped that you will find opportunities to experiment with Finite State Machine and Stateflow further.
This is the end of this training video. Thanks for watching.