Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Connecting the LEGO EV3 Bot as gamecontroller

Discussion in 'Scripting' started by BigPapie, Oct 28, 2014.

  1. BigPapie

    BigPapie

    Joined:
    Oct 28, 2014
    Posts:
    5
    Hi,

    I'm new to Unity. For a school project we need to make a game that that you need to control with a LEGO EV3 Robot.
    I've got the code to connect and use the EV3 as a controller. But the problem is that I've got a Parsing error that I don't understand:

    PlayerControl.cs(5,14): error CS8025: Parsing Error

    Our teachers don't work with Unity and nobody on school is able to help me since nobody used Unity. Therefore I'm here to ask you the more experienced/ pro's for help.

    EDIT:
    Code cleanup after replys

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using EV3MessengerLib;
    5.  
    6. // EV3: Create an EV3Messenger object which you can use to talk to the EV3.
    7.  
    8. //EV3Messenger ev3Messenger = new EV3Messenger();
    9.  
    10. // EV3: Connect to the EV3 serial port over Bluetooth.
    11. //      If the program 'hangs' on a call to ev3Messenger.Connect,
    12. //      then your EV3 is not paired with your PC yet/anymore.
    13. //      To pair: Remove the EV3 from the Windows Bluetooth device list and add it again.
    14.  // Hardcoded serial port: put the serial port
    15. // of the Bluetooth connection to your EV3 here!
    16. public class PlayerControl : MonoBehaviour {
    17.  public GameControlScript control;
    18.  CharacterController controller;
    19.  bool isGrounded= false;
    20.  public float speed = 6.0f;
    21.  public float jumpSpeed = 8.0f;
    22.  public float gravity = 20.0f;
    23.  private Vector3 moveDirection = Vector3.zero;
    24. // EV3: The EV3Messenger is used to communicate with the Lego EV3
    25. private EV3Messenger ev3Messenger;
    26.  //start
    27.  void Start () {
    28.   controller = GetComponent<CharacterController>();
    29.         ev3Messenger = new EV3Messenger();
    30.         ev3Messenger.Connect("COM03");
    31.  }
    32.     //// Game can be controlled by the connected EV3
    33.     //UpdateUsingEV3();
    34.  
    35.  
    36.  // Update is called once per frame
    37.  void Update (){
    38.   if (controller.isGrounded) {
    39.    animation.Play("run");            //play "run" animation if spacebar is not pressed
    40.    moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, 0);  //get keyboard input to move in the horizontal direction
    41.    moveDirection = transform.TransformDirection(moveDirection);  //apply this direction to the character
    42.    moveDirection *= speed;            //increase the speed of the movement by the factor "speed"
    43.        
    44.             if (ev3Messenger.IsConnected)
    45.             {
    46.                 // EV3: Receive a new command from mailbox "COMMAND" of the EV3
    47.                 // and use it to change the direction of the paddle or to exit the game.
    48.                 EV3Message message = ev3Messenger.ReadMessage();
    49.                 if (message != null
    50.                    && message.MailboxTitle == "Command")
    51.                 {
    52.                     if (message.ValueAsText == "Right")
    53.                     {
    54.                         transform.position += transform.right * speed;
    55.                     }
    56.                     else if (message.ValueAsText == "Left")
    57.                     {
    58.                         transform.position += -transform.right * speed;
    59.                     }
    60.                     else if (message.ValueAsText == "Exit")
    61.                     {
    62.                         ev3Messenger.Disconnect();
    63.                         //Exit();
    64.                     }
    65.                 }
    66.             }
    67.      
    68.    //if (Input.GetButton ("Jump")) {          //play "Jump" animation if character is grounded and spacebar is pressed
    69.     //animation.Stop("run");
    70.     //animation.Play("jump_pose");
    71.     //moveDirection.y = jumpSpeed;         //add the jump height to the character
    72.    //}
    73.    if(controller.isGrounded)           //set the flag isGrounded to true if character is grounded
    74.     isGrounded = true;
    75.   }
    76.   moveDirection.y -= gravity * Time.deltaTime;       //Apply gravity  
    77.   controller.Move(moveDirection * Time.deltaTime);      //Move the controller
    78.  }
    79.  //check if the character collects the powerups or the snags
    80.  void OnTriggerEnter(Collider other)
    81.  {              
    82.   if(other.gameObject.name == "Powerup(Clone)")
    83.   {
    84.    control.PowerupCollected();
    85.             //EV3 sends Message MakeNoise so it will play the sound Hit
    86.             ev3Messenger.SendMessage("MakeNoise", "Hit");
    87.   }
    88.   else if(other.gameObject.name == "Obstacle(Clone)" && isGrounded == true)
    89.   {
    90.    control.AlcoholCollected();
    91.             //EV3 sends Message MakeNoise so it will play the sound Crash
    92.             ev3Messenger.SendMessage("MakeMoreNoise", "Crash");
    93.   }
    94.    
    95.   Destroy(other.gameObject);
    96.    
    97.  }
    98. }
    99. }
    I can explain things if the code if not clear enough.

    Thank you in advance for looking into it!
     
    Last edited: Oct 28, 2014
  2. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    Line 5 needs a variable type, and possibly can't initialise outside of a function.
    So you need line 5 to read
    EV3Messenger ev3Messenger;
    to create the typed variable, then inside start;
    ev3Messenger = new EV3Messenger();
    to populate it
     
  3. BigPapie

    BigPapie

    Joined:
    Oct 28, 2014
    Posts:
    5
    Thank you for your reply. And you where right. I cleaned/shifted the code.

    Only problem now is that I've got a Internal compiler error that gives me this message:

    Internal compiler error. See the console log for more information. output was:
    Unhandled Exception: System.Reflection.ReflectionTypeLoadException: The classes in the module cannot be loaded.

    at (wrapper managed-to-native) System.Reflection.Assembly:GetTypes (bool)

    at System.Reflection.Assembly.GetTypes () [0x00000] in <filename unknown>:0

    at Mono.CSharp.RootNamespace.ComputeNamespaces (System.Reflection.Assembly assembly, System.Type extensionType) [0x00000] in <filename unknown>:0


    Well that is as you can see very unclear. If you got a few minutes is it possible to check my project? http://athena.fhict.nl/users/i322877/Temple_run.zip

    Thnak you very much for the help so far!
     
  4. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    What's this EV3Messenger class you're using? From the error, I'm guessing that it's not included in the built app. Where'd you get it, and are you sure it's compatible with Unity?
     
  5. BigPapie

    BigPapie

    Joined:
    Oct 28, 2014
    Posts:
    5
    The class is used to create the function to send a Message in the EV3 Robot. The robot has a program with instructions that it needs to do when EV3Messenger is called.

    I changed the code above to the one I've now.
     
  6. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    I figured that much, but you haven't answered the important questions. Where'd you get it, and are you sure it's compatible with Unity?
     
  7. BigPapie

    BigPapie

    Joined:
    Oct 28, 2014
    Posts:
    5
    I got the EV3 code from school. We only needed to implement it correctly.
    But I heard that Unity uses Net 2.0 and EV3 uses NET 4.0. So if that is true then it's not possible to connect it??
     
  8. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Yes, that could be possible. That's why it's important whether the library has been verified for use with Unity. If not, this may not be something you can fix in a reasonable amount of time. :(
     
  9. BigPapie

    BigPapie

    Joined:
    Oct 28, 2014
    Posts:
    5
    Well In the passed other students where able to connect Unity with the EV3 Robot. Only these students are nowhere to be find. Maybe there is a short term solution? Any you know?

    Thanks for your help so far!
     
  10. janoonk

    janoonk

    Joined:
    Dec 3, 2008
    Posts:
    79
  11. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Welcome to the world of pioneers and early adopters. A quick google search brings up nothing. So you are on your own unless you can find someone who has solved this before.

    It's worth questioning why you want to do this. If you have to ask about parsing errors you are not a competent coder. (At least in the Unity environment). If the teacher is not familiar with unity then it shouldn't be an absolute requirement. You've picked a hard topic that will require an intimate knowledge of Unity, C# and ev3 to solve. It's doable, but will take you a long time.
     
  12. janoonk

    janoonk

    Joined:
    Dec 3, 2008
    Posts:
    79
    The original library/source at codeplex uses SerialPort (.NET 4) and ConcurrentQueue(.NET 4.5/4.6). All other stuff should be Unity compatible I think/hope.

    I looked at the plugin documentation http://docs.unity3d.com/Manual/Plugins.html
    Tried the managed plugin example. I had to select .NET 3.5 to get the ultra simple plugin to work so I guess you need to find a replacement for SerialPort and ConcurrentQueue:
    -I found a Unity compatible ConcurrentQueue at https://gist.github.com/jaredjenkins/5421892. Only TryDequeue method, which is used by the EV3 source, is missing. Don't know how difficult it's to implement this yourself.
    -For SerialPort you could use an third party non-free DLL: http://www.activexperts.com/serial-port-component/
    Then you have to rewrite all original SerialPort references to use this new third party component.


    Other option is to leave the EV3 sources alone, and build some functionality on top of it to support a connection from Unity for example sockets.

    If EV3 messenger program and Unity runs on the same machine you could look into inter-process communication (IPC). There are many IPC methods (pipes, files, sockets...) and the right choice depends on your specific requirements. For node.js IPC, see for instance http://stackoverflow.com/questions/...js-inter-process-communication-library-method

    If you read http://stackoverflow.com/questions/...js-inter-process-communication-library-method
    you will notice ZeroMQ which can superfast send/receive messages (from Unity to EV3 connection program) and is now .NET 3.5 compatible: https://github.com/zeromq/netmq/issues/98
    To support bidirectional messaging you could make both a server and client in both Unity and EV3 messenger.
     
    Last edited: Sep 14, 2015
    Kiwasi and JoeStrout like this.