Coolones
Coolones

Reputation: 13

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.

Upvotes: 1

Views: 2590

Answers (2)

a3f
a3f

Reputation: 8657

As part of c4ev3, We open-sourced our EV3 uploader, which can also be used to send connection-agnostic commands to the device.

Here is how you would move the motors in Perl (Complete version):

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";

This would probe for an EV3 accessible over USB, Bluetooth or WiFi and connect to it, then send the direct messages associated with turning the motors. For more information on the direct commands protocol check out LEGO's Communication Developer Manual and David Lechner's answer.

Alternatively, you can write a C program for the EV3 with c4ev3 and communicate with that. That way you got a nicer looking C-API you can use.

Upvotes: 0

David Lechner
David Lechner

Reputation: 1824

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.

Upvotes: 2

Related Questions