主要内容

开始使用鹦鹉键盘控制

This example shows you how to create a Simulink® model that uses the keyboard of the host computer to control the motors of a Parrot minidrone.

介绍

The Simulink Support Package for Parrot Minidrones provides a Keyboard Read block that reads the keys pressed on the keyboard of the host computer, when the Simulink model is running on the drone. The output of this Keyboard Read block is the corresponding ASCII code of the character pressed, which you can use to trigger any control logic.

键盘读取块可以在Simulink库中找到。万博1manbetx要从simulink库浏览器访问块:万博1manbetx

1.EnterslLibraryBrowserat the MATLAB prompt.

2.In the Simulink Library Browser, navigate toSimulink Support Package for Parrot Minidrones.

open_system('parrotlib');

In this example, you will learn how to use the Keyboard Read block in the代码生成模板to set the speed of one of the motors on Parrot minidrone, using two different keys on the keyboard.

Prerequisites

Required Hardware

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

  • Parrot Rolling Spider or Parrot Mambo with a fully charged battery, and propellers connected to the motors.

  • Bluetooth Low Energy (BLE) 4.0 support on the host computer

任务1-添加键盘读取块并建模逻辑以检测接收的字符

1.在Simuli万博1manbetxnk启动页面上,导航到Simulink Support Package for Parrot Minidrones和select代码生成模板.

2.在Simuli万博1manbetxnk模型中,导航到飞行控制系统,然后插入Keyboard Read距离库。

3.Add twoCompare to Constantblocks in the model, and connect the output of Keyboard Read block to the inputs of the two Compare to Constant blocks.

In this example, we assume that the character 'a' on the keyboard will be used to start a motor, and the character 's' will be used to stop the motor. You can use one of the Compare to Constant blocks to compare the received character with the ASCII code of 'a' (which is97),并使用其他块将接收的字符与“ S”的ASCII代码进行比较(即115).

4.双击该比较与恒定块相比,设置Operatorvalue to==, 和持续的value to97115respectively.

任务2-添加用于控制电动机的其他逻辑

In this task, you will perform the following:

  • 使用从映射到“ a”的恒定块中的输出来运行鹦鹉微酮的电动机1

  • 使用比较恒定的输出块mapped to 's' to stop Motor 1 of Parrot minidrone.

1.插入nAddblock, and combine the two outputs (from the two Compare to Constant blocks) using the Add block.

2.插入Switch块,并将添加块的输出连接到开关块的输入2。

3.添加一个具有价值的增益块400, 和connect it to the output of Compare to Constant block configured for character 'a'. Connect the output of the Gain block to Input 1 of the Switch block.

4.对于开关块的输入3,我们将获得从开关块的输出派生的延迟反馈。插入delayblock and connect it to Input 3.

5.Connect the output of Switch block to Motor 1.

6.Add a持续的block with value200和connect it to other motors.

The completed logic will look like this:

A pre-configured model(parrot_keyboard_control)还包括为了方便您。双击此模型中的飞行控制系统子系统以查看控制逻辑。

open_system('parrot_keyboard_control');

Task 3 - Deploy the model and use the keyboard to control the drone

In this task, you will deploy the Simulink model to Parrot minidrone, start the model, and use the keyboard of the host computer to control the speed of Motor 1.

1.在Simuli万博1manbetxnk工具栏上,单击Deploy to hardware按钮。该模型已部署到鹦鹉的极小性。

If you have selected the构建后自动启动鹦鹉飞行控制接口option in the Configuration Parameters dialog box (硬件实现pane >Target Hardware Resources>Build Options), the Parrot Flight Control Interface opens automatically after the build goes through successfully.

You can also launch the interface by executing the commandParrot_FlightInterfaceat the MATLAB command prompt.

2.On the Parrot Flight Control Interface, clickStart. The deployed model is now ready to accept keyboard key actions.

3.On the Parrot Flight Control Interface, click显示键盘控制面板.

4.Click启用键盘控制

5.Place the cursor in the field below the启用键盘控制button, and type the charactera在主机计算机的键盘上。

The Motor 1 starts running.

6.Type the characters在主机计算机的键盘上。The Motor 1 stops running.

7ClickSTOP在飞行控制接口上,以阻止在无人机上运行的模型。

For more details, seeKeyboard Control of Parrot Minidrones.

Use MATLAB script to control the drone

可以使用MATLAB脚本复制上述行为。有关更多详细信息,请参阅该主题Using MATLAB Scripts for Keyboard Control.

下面提供了一个示例脚本,该脚本在电动机上打开3秒钟,然后将其关闭3秒钟,以进行10个周期。

您需要首先将Simulink模型部署到无人机,使用飞行控制万博1manbetx接口启动模型,然后在MATLAB命令提示符下运行以下脚本。

尝试tcphandle = tcpclient('192.168.3.1',26061);
对于i = 1:10写(tcphandle,uint8('a'));暂停(3);写(tcphandle,uint8('s'));暂停(3);end Catch ex if(iSequal(ex.Identifier,'matlab:networklib:tcpclient:contecteeObject'))错误('包括键盘读取块万博1manbetx的simulink模型必须在无人机中运行以启用键盘控制。”);否则错误(“无法将键盘字符传输到无人机。检查Simulink模型是否在无人机中运行。”);万博1manbetx结尾

ClickSTOP在飞行控制接口上,以阻止在无人机上运行的模型。