Main Content

Communicating with Arduino Hardware

This example shows how to tune the parameters and monitor the signals of an algorithm running on Arduino® board.

金宝app支持的硬件:

  • Arduino Mega 2560

  • Arduino Mega Adk

  • Arduino应得

  • Arduino Leonardo

  • Arduino微型

  • Arduino Uno

  • Arduino MKR1000

  • Arduino MKR WiFi 1010

  • Arduino MKR ZERO

  • Arduino Nano 3.0

  • Arduino Robot Control Board

  • Arduino Robot Motor Board

  • Arduino Nano 33 IoT

  • Arduino Nano 33 BLE Sense

Available versions of this example:

Arduino Mega 2560董事会:arduino_communication

The provided model is pre-configured for Arduino Mega 2560 and can be run on any of the board listed in the "Supported Hardware" section, by changing the "Hardware board" parameter in the configuration parameters dialog box of the model as described in Task 2 of this example.

Introduction

金宝appArdui金宝appno硬件的Simulink®支持软件包使您可以从相同的Simulink模型中监视和调整Arduino板上运行的算法,从而从中开发了算法。

在此示例中,您将学习如何在执行时实时调整和监视算法。当您开发算法时,通常有必要以迭代方式确定关键算法参数的适当值。例如,测量房间运动能量的监视算法可以使用阈值来确定在存在环境噪声的情况下的入侵者。如果设置阈值太低,则该算法可能会错误地将任何运动解释为入侵者。如果设置阈值太高,则该算法可能根本无法检测到任何运动。在这种情况下,可以通过尝试不同的值来获得正确的阈值值,直到达到所需的算法性能为止。这个迭代过程称为参数调整。

The External mode (Monitor and Tune) feature enables you to accelerate the process of parameter tuning by letting you change certain parameter values while the model is running on target hardware, without stopping the model. When you change parameter values from within Simulink, the modified parameter values are communicated to the target hardware immediately. The effects of the parameters tuning activity may be monitored by viewing algorithm signals on scopes or displays in Simulink.

This example introduces the SimulinkExternal mode通过向您展示如何:

  • Set up communication between Simulink and Arduino board.

  • Use a Simulink model to tune the parameters of an algorithm that is running on Arduino board.

  • Use Simulink scopes to monitor the state of an algorithm running on Arduino board.

先决条件

Before you start with this example, we recommend you complete theGetting Started with Arduino Hardwareexample.

Required Hardware

要运行此示例,您将需要以下硬件:

  • Supported Arduino board

  • USB电缆

  • LED

  • 220 Ohm resistor

  • Breadboard wires

  • Small breadboard (recommended)

Model

下图显示了示例模型:

任务1-连接导致Arduino输出引脚

在此任务中,您将将LED连接到Arduino数字输出引脚,以便您可以看到PIN的逻辑状态的变化。

1。Attach one end of the 220 Ohm resistor to digital output pin 9 on the Arduino board. Use the recommended breadboard and the breadboard wires.

2。把长腿(积极)导致了resistor. Attach the short leg (negative) to the ground pin on the Arduino board.

任务2-为支持的Arduino硬件配置金宝appSimulink模型金宝app

In this task, you will configure the model for the supported Arduino board.

1。打开Communicating with Arduino Hardware模型。

2。在您的Simul金宝appink型号中,单击Simulation > Model Configuration Parametersto open配置参数dialog.

3。Select the硬件Implementation窗格并从中选择您所需的Arduino硬件硬件board参数列表。不要更改任何其他设置。

4。点击OK

Task 3 - Simulate Model

要模拟模型,请按照以下步骤:

1。观察该模型播放记录在房间中的运动能,并将其与阈值进行比较以检测入侵。

2。In the model, change theSimulation mode在工具栏上普通的。This tells Simulink to run the model on the host computer. See Task 4 below to run the model on the Arduino target hardware.

3。在模型中,单击button in the Simulink toolbar.

4。点击the范围堵塞。观察到该算法检测到多个侵入。

5。点击停止Simulink模型中的按钮金宝app。

任务4-运行Simulin金宝appk模型

1。将USB电缆从计算机连接到Arduino板。

2。硬件tab of the Simulink model, in theMode部分,选择在船上运行然后单击监视和调子

任务5-通过串行与Simulink模型通信金宝app

At this point, your model is running on Arduino board. As the model runs on hardware, it communicates with Simulink model in External mode using serial connection over USB.

1。请注意,连接到引脚9的LED几乎不断发光。这意味着所选阈值太低,并且算法误解甚至是较小的运动能量变化作为入侵。您需要找到阈值的最佳值。

2。双击Thresholdblock in the model, increase its value, and clickOKor申请。这会改变板上运行的模型中的阈值。

3。检查LED的发光模式是否已更改。LED应以正确调整的算法每10秒和11.5秒点亮一次。

4。如果LED光模式没有变化,请重复步骤2和3,直到找到阈值的正确值。

5。点击停止在Simuli金宝appnk模型中。

Troubleshoot Serial Communication

1。一个错误指出“试图通过端口建立与主机名127.0.0.1建立连接”,指示本地回环上的TCP/IP端口17725由某些其他应用程序持有。外部模式在后台使用过程来缓冲数据并改善数据记录。此过程需要端口17725。

2。High base rates may cause the time in the scope window to freeze. This is a limitation of the firmware on Arduino boards. Using an FTDI chip as the USB-Serial bridge resolves this issue.

任务6-配置Simulink模型以通过金宝appTCP/IP以外部模式运行

Refer to the documentation section:Configure Model to Run in External Mode over Ethernet

任务7-配置Simulink模型以在W金宝appiFi上以外部模式运行

Refer to the documentation section:Configure Model to Run in External Mode over WiFi or Serial

Other Things to Try

  • 监视模型中的其他信号。例如,添加另一个范围来监视记录的运动能量的值。

  • 改进检测算法,以滤除比0.2秒短的任何运动能量变化。