Student Lounge

Sharing technical and real-life examples of how students can use MATLAB and Simulink in their everyday projects #studentsuccess

代码生成:在任何地方运行MATLAB代码和SIMULINK模型!金宝app

我想首先祝你们所有人都度过新年快乐!在新的一年的精神,尝试我们想要向您介绍的新事物our latest series of student competition tutorials on code generation。Connell, our regular guest blogger (… we should call him co-author of this blog …) wrote this article.

What is Code Generation?

代码是直接从高级编程语言或建模环境生成低级代码的过程。但是,为什么这是重要的?Matlab是一种多范式高级编程语言,由全球200多万工程师和科学家使用。世界各地有超过5000所大学,Matlab向来自科学,技术,工程和数学学科的学生教授。在我与Matlab用户的许多对话中,最重复的主题始终是“Matlab,Simulink是我的转移到原型和模拟算法”。金宝app但下一个什么?

As a student competition team there is a good chance that you are working with embedded systems – tiny computers that are the brains of your racecars, airplanes, boats, robots or the various other thingamajigs that are trying to make this world a better place. So how do you take your algorithms from MATLAB desktop to these embedded systems? The answer is code generation. Through these video tutorials we hope to teach you how to convert your MATLAB and Simulink algorithms in low-level C/C++ source code that can be integrated into other independent code bases or executables that can be deployed directly to an embedded system.

它如何帮助你?

本教程系列将教您如何通过简单自主系统的镜头从Matlab和Simulink生成低级代码。金宝app我们创建了一个球跟踪系统,其中安装在伺服电机上的相机用于跟踪绿色球。嵌入式计算机用于从传感器(摄像机)中的数据流识别球位置并计算执行器命令以将物体保持在相机图像的中心。这是许多自治系统背后的原则 - 使用传感器信息计算控制命令。一些常见的例子是Lane保持辅助系统在自动驾驶车中,其中计算摄像机和转向命令,或者在巡航模式中计算自动驾驶仪,其中,用于估计方向和翼襟命令的巡航模式以保持期望的方向。

模拟系统

Simulation is the cheapest way to test algorithms. MATLAB and Simulink provide many different tools to design simulations so you can test different scenarios and edge cases enabling you to build robust code.

These tutorials follow the same approach. We started by trying to identify the ball. Once you download the freeMATLAB Support package for USB webcams那sampling data from a camera, in MATLAB, is as easy as

vidobj = imaq.videodevice('winvideo'那1); vidObj.ReturnedColorSpace ='ycbcr';Vidobj.ReturnedDatatype =.'uint8';preview(vidObj);

Now that we have a data stream from the sensor, we tried a few different algorithms to achieve our objective – locate the ball in the image. After a few simulations we settled on a simple color thresholding algorithm that is efficient and serves our purpose.

接下来,我们搬到了我们问题的控制方面。我们决定PID控制器是电机低电平控制的最佳控制方案。为了模拟该系统,我们在Simulink中创建了电机的数学模型,并使用随机啁啾信号,并且PID块的自动调谐功能到达该模型的一组P,I和D增益。金宝appWatch this tech talk series to learn about the basics of PID control.

To define the input to our PID controller, or what I like to call the high-level control, we used Stateflow to define the different modes of operation – a seeking mode where there is no object in the camera frame and the motor will turn the camera to find the object, a tracking state where the object is detected and the motor will try to keep the object in the center of the camera frame and a default waiting mode to enable smooth transition between the other two modes.

在硬件上实施

Now that we have 3 independently developed and tested modules, we need to implement the system on hardware. This is where code generation comes in. Working with hardware is usually the most challenging task for student competition teams. Selecting the right hardware is key. We decided on a Raspberry Pi Model 3 to process the sensor data and select the operating mode and an Arduino Uno for the low-level motor control. The Raspberry Pi and the Arduino are connected to each other via a serial connection through the USB port. Now we understand that for most teams MATLAB and Simulink are two tools in your toolbox and you may be using other programming languages to program your hardware but bear with me, these tutorials will not only teach you how to deploy code directly onto target hardware, but also how to generate code for individual modules that can be taken into a different programming environment and integrated independently of MATLAB or Simulink.

我们将使用仿真软件,仿真金宝app软件编码器和具体driver libraries for theArduinoandRaspberry Pi名为硬件支持包,将代码部署到硬件金宝app中的硬件。Hardware support packages are available for several hardware boards and can be found here(如果您正在寻找目前不支持的理由的支持,金宝app按照此页面上的步骤操作与我们的开发团队取得联系)。免费下载库,请遵循设置步骤并为您的应用程序使用它!一旦您下载了库,您将找到一些块,帮助您与电路板上的不同输入和输出端口接口。

MATLAB code can be used in Simulink in a MATLAB Function block, so we created 2 Simulink models – the first with the 2 modules to be deployed to the Raspberry Pi and connected the input to the From Video Capture block and the outputs to the SDL Video Display block to visualize the camera feed and Serial Write block to communicate with the Arduino. If you followed the set-up steps correctly, hitting the “Deploy to Hardware” button will build this code directly onto the Raspberry Pi. The Arduino serial bus can only transmit data of “uint8” data type, so make sure you convert to the required data-type. This is where knowledge of the hardware you are working with is important.

The second model contains the low-level control module with the inputs Serial Receive and outputs to the Continuous Servo Write and Digital Output blocks from the Arduino Hardware Support Package. Plug the Arduino into your computer and the build the code. Simple ?

Conclusion

本教程系列还将教您如何优化生成的速度,内存使用,生成的文件数等代码等。这允许您生成针对您的应用程序优化的代码。一旦代码构建到硬件上,您可以使用称为外部模式的强大的Simulink功能。金宝app外部模式如下视频所示,允许您在硬件目标上运行算法,同时仍然与Simulink与其交互。金宝app您可以可视化输出和算法的行为,以确保它正在执行您想要的操作。

We hope that this series will help you shorten your development time and enable you to use algorithms developed in MATLAB or Simulink without having to rewrite them in a low-level language.

So, what are you waiting for?Download the files观看视频并告诉我们你的想法!同时,不要忘记check out the othercomplimentary video tutorial seriescreated for student competition teams!

|

Comments

要发表评论,请点击here要登录您的MathWorks帐户或创建新的。