📜 ⬆️ ⬇️

Model-oriented design, or continue taming the Cortex M3 with Matlab / Simulink


The image is taken, and slightly edited, from Anton Lopatin 's blog. Well, very inspiring.

Good afternoon, dear community!

In the past, I tried to make an introductory excursion into model-oriented design of embedded systems using ARM microprocessors Cortex M3 and MATLAB / Simulink. We continue immersion, we complicate the task. It is a pity that the concept in the title illustration is still far away, but I will try to show the process of developing one of its most important nodes, namely the turret homing system.
')
There are quite a few images below, but without them it's boring and not visual.


Hardware


Unlike the first topic, we now have in our hands:

Another difference from the beautiful concept is that for our crafts the concept of “collected on the knee” is as close to reality as possible. A vivid example of rapid development. Fast, cheap, workable.

Task setting and subtasks


The main task is to implement the functions of capturing and tracking the target. To achieve the intended we have to:


Host-side model


The collapsed model, on which the functions of video processing and issuing control signals are assigned, looks like this:
The video signal divided into channels is transmitted to the detection and processing subsystems. After them, the output signals are transmitted via USB to the control board.

Suppose our object is illuminated by a laser with a specific wavelength. The main task of the video recognition subsystem will be the selection of a given color shade, filtering possible interference and issuing a concise signal about the location of the object. For our "bull" we will not use a red scarf, but a green one, which, again, is not so important. You can tie up the movement here ...
Detection subsystems look like this:

Filtering subsystems look like this.
I became familiar with video recognition during the development process, so I chose the simplest algorithm. The split video signal passes through the condition branches that return true or false (instead of the RGB percentage) depending on the specified conditions. As can be seen from the numerical values, I was guided by the green color.

At the output of the systems, instead of video, we get a matrix from the field of zeros and closed islands of ones. The processing subsystem of the received information determines the areas and centers of the received "islands", outlines them with boundaries and equivalent circles, and also determines the distance of the center of the object from the center of the matrix. An error is transmitted to the side of iron with the rows and columns for the largest island, and the number of islands. If this is the most num == 0, then the turret goes into standby mode and performs a scheduled scan of the area horizontally. Counting errors is not from the absolute center of the image, and from the boundaries of a rectangular "zero zone". Inside this area, the object is considered "captured" and the camera fixes its position. All additional constructions are being adjusted to the original video, complementing the gray and gloomy wall of my room with the reality of our vigorous rectangles and circles.



The subsystems for drawing additional graphics and error definitions look like this:
Simulink politely and reminds us in advance that the blocks are marked with a “Reglace” mark should be replaced before the release of the new version of the program. Otherwise, the model will not be compatible with it.

It should be noted that binding to running Simulink is not required. Using the same Simulink Coder, you can generate C ++ code to build a standalone application for both Windows and Linux. I am not strong in this yet, so I limited myself to a working Simulink model in Accelerator mode, that is, in partial compilation mode to increase execution speed:


Iron side model


The Simulink model from which the code for filling in the microcontroller is generated looks as shown in the illustration below: It is executed in uniform clocking, as it is visible on identical color illumination for all blocks of model after updating. As mentioned earlier, in addition to elementary units of the type of addition or multiplication, the model contains two PID regulators. Rather, even PI, because the coefficient of the differentiating component is zero.

Parameters were selected manually by experiment. But for more serious systems, MATLAB has a special Control System Toolbox tool for “tuning” linear control systems based on experimental data with the ability to set the required control parameters.
Small proportional coefficient and zero differential due to a rather noticeable time delay, in which each element of the system contributes.

The assignments of the Saturation and Dead Zone blocks are clear from the graphs in their icons.

Initially, the implementation of the “duty” and “combat” modes was conceived using finite automata, but then it turned out that the issue could be solved more easily. Therefore, FSM is still ahead. This concept is implemented inside Simulink using the elegant Stateflow tool, and just as easily poured into the MC.

Generation, compilation and code fill is performed entirely from under MATLAB. Keil doesn't even need to be run separately.

The actual development process



All of the above is mostly already the results than the development stages.

While there was no serv (read the absence of the hardware component as such) the algorithms of the system were worked out on such an ugly, monstrous draft model:

Then she became like this:

The main distinctive part of these models is the mechanical subsystem (highlighted in blue in the previous illustration), which made it possible to evaluate the performance of the system exclusively using a PC. In my case, it was important to optimize the angular velocity of rotation of the serv, and the coefficients of the PI regulators, but a complete mechanical analysis of the system is also possible.
This is how the assembly in Solidworks looked:



And this is how the simulation of assembly motion looks already inside the Simulink model:



There is a very serious tool for the simulation of dynamics and statics inside Solidworks, but working with MATLAB is more familiar to me. The assembly is exported using the SimMechanics Link utility. Here a lot of things are written about her.
In a word, when the “iron” arrived, I had almost everything ready. I simply deleted all unnecessary and divided the model into two components: Host and Target.

Test results


Finally, a field test video:



Yes, pretty ugly assembly. For a premium super direct hands just do not claim.

The following is happening on the laptop screen:



findings



Positive:

  1. Development took a minimum of time and resources. Everything works satisfactorily and is easy to upgrade. The implementation of the "trigger" - there is nothing easier. There are still 100,500 ports left on the board. You just need to add a condition branch when the center of the object is in the capture zone that will log. 1 to a specific port and unload the rail gun to simulate a shot.


Instructive negative:
  1. Use the cat as a target did not work. Gray camouflage is not detectable, and the attempt to fasten a tag on it was crowned with failure. In addition, he is also terribly afraid of buzzing serv. I had to experience everything for myself.
  2. To increase the speed and stability of work, there is still a lot to be improved. The mechanical part was the most whimsical. Calibration of serv took most of the time allotted for commissioning. With the further development of the project, it will be necessary to switch to higher-quality devices, possibly with the subsequent revision and the introduction of your own feedback.
  3. I could not cope with automatically changing the brightness settings of the camera depending on the lighting and the surrounding background. It is clear that this influenced the selectivity of the recognition system. In the future, it is planned to use a separate camera module (possibly with a focus on the IR range with the appropriate backlight) with direct connection to the control board.
  4. With great pleasure and boundless gratitude I will take note of the reasonable criticism in the comments. And here I will add!


Updates:
UPD # 1 project sources
MATLAB models for Host & Target
Build in Solidworks

UPD # 2 Domestic manufacturer
Who does not have a patriotic idea (and who does not have Simmechanics in the delivery), for the mat mechanics modeling before production can use the domestic (if not less powerful) tool UM "Universal Mechanism" , which is successfully used by robotics from MSTU. And not only they. Demonstrations here .
Can talk to Simulink. For details, you can contact the habraiser yupych .

Source: https://habr.com/ru/post/133911/


All Articles