Search code examples
bluetoothunity-game-engineunityscriptlego-mindstorms

How can I connect a EV3 mindstorms via bluotooth to a unity game using unityscript?


I am making a race game in Unity with Unityscript and I made a steer with the lego mindstorms EV3 robot. I let te robot send information by bluetooth to the game, but I can't find how I can do that. I already have the code for the bluetooth running and working a C#, but know I need to know how to translate it to unityscript. I already tried to find it on google, but I only seem to get some software to hack the robot, but not to make code in unityscript for connecting the steer.

Under here stands the C# code:

        // 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();
                    }
                }
            }
        }

I hope that you know where I can find how I can do this or even help me further. If you want the original code for a small pong game where I got my inspiration from, just comment it.

I hope you can help me.


Solution

  • Here are some useful links with documentation on the EV3 firmware :

    In particular, you need to learn how to send direct commands and then use that to read and write bluetooth mailboxes.

    For communicating with the COM port itself using javascript, just do a little searching. For example, I found this SO question which has quite a few different ideas.