1

我正在使用 Unityscript 在 Unity 中制作赛车游戏,并使用乐高头脑风暴 EV3 机器人进行驾驶。我让机器人通过蓝牙向游戏发送信息,但我找不到如何做到这一点。我已经有了运行和使用 C# 的蓝牙的代码,但我知道我需要知道如何将其转换为 unityscript。我已经尝试在谷歌上找到它,但我似乎只得到了一些软件来破解机器人,而不是在 unityscript 中编写代码来连接转向。

下面是 C# 代码:

        // EV3: The EV3Messenger is used to communicate with the Lego EV3.
private EV3Messenger ev3Messenger;


            // EV3: Create an EV3Messenger object which you can use to talk to the EV3.
            ev3Messenger = new EV3Messenger();

            // EV3: Connect to the EV3 serial port over Bluetooth.
            //      If the program 'hangs' on a call to ev3Messenger.Connect, 
            //      then your EV3 is not paired with your PC yet/anymore. 
            //      To pair: Remove the EV3 from the Windows Bluetooth device list and add it again.
            ev3Messenger.Connect("COM3"); // Hardcoded serial port: put the serial port 
            // of the Bluetooth connection to your EV3 here!
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // Unload any non ContentManager content here

            // EV3: Disconnect
            if (ev3Messenger.IsConnected)
            {
                ev3Messenger.Disconnect();
            }
        }

                // EV3: send Brake message to mailbox with name "MakeNoise"
                if (ev3Messenger.IsConnected)
                {
                    ev3Messenger.SendMessage("MakeNoise", "Brake");
                }


            // Game can be controlled by both the arrow keys and the Steer, gas and brake paddle of                         the connected EV3
            UpdatePaddlePositionUsingKeys();
            UpdatePaddlePositionUsingEV3();

            base.Update(gameTime);
        }
///Steer update
        private void UpdatePaddlePositionUsingEV3()
        {
            if (ev3Messenger.IsConnected)
            {
                // EV3: Receive a new command from mailbox "COMMAND" of the EV3
                // and use it to change the direction of the paddle or to exit the game.
                EV3Message message = ev3Messenger.ReadMessage();
                if (message != null
                    && message.MailboxTitle == "Command")
                {
                    if (message.ValueAsText == "")
                    {
                    }

                    {
                        ev3Messenger.Disconnect();
                        Exit();
                    }
                }
            }
        }

我希望您知道我在哪里可以找到如何做到这一点,甚至可以进一步帮助我。如果您想要我从中获得灵感的小型乒乓球游戏的原始代码,请发表评论。

我希望你能帮助我。

4

2 回答 2

2

以下是一些有用的 EV3 固件文档链接:

特别是,您需要学习如何发送直接命令,然后使用它来读写蓝牙邮箱

对于使用 javascript 与 COM 端口本身进行通信,只需稍作搜索即可。例如,我发现这个 SO question有很多不同的想法。

于 2014-10-25T17:54:47.767 回答
0

作为c4ev3的一部分,我们开源了我们的EV3 上传器,它也可用于向设备发送与连接无关的命令。

以下是在 Perl 中移动电机的方法(完整版):

use IPC::Open2; 
print open2(\*EV3OUT, \*EV3IN, "ev3 tunnel") or die "couldn't find: %!";

print EV3IN "0900xxxx8000 00 A3 00 09 00\n";
print EV3IN "0C00xxxx8000 00 A4 00 09 50 A6 00 09\n";

这将探测可通过 USB、蓝牙或 WiFi 访问的 EV3 并连接到它,然后发送与转动电机相关的直接消息。有关直接命令协议的更多信息,请查看 LEGO 的通信开发人员手册和 David Lechner 的回答。

或者,您可以使用 c4ev3 为 EV3 编写 C 程序并与之通信。这样你就可以使用更好看的 C-API。

于 2016-06-08T11:57:06.460 回答