Ejemplo n.º 1
0
 // ------------------------------------------------------------------------
 // Invoked whenever the SerialController gameobject is activated.
 // It creates a new thread that tries to connect to the serial device
 // and start reading from it.
 // ------------------------------------------------------------------------
 void OnEnable()
 {
     serialThread = new SerialThread(portName, baudRate, reconnectionDelay,
                                     maxUnreadMessages);
     thread = new Thread(new ThreadStart(serialThread.RunForever));
     thread.Start();
 }
Ejemplo n.º 2
0
    // Setup the Bicycle Configuration
    void Start()
    {
        // Set component sizes
        const float wheelWidth = 0.01f;
        const float frameWidth = 0.05f;
        Vector3     v          = new Vector3(2 * rR, wheelWidth, 2 * rR);

        rearWheel.transform.localScale = v;
        v = new Vector3(2 * rF, wheelWidth, 2 * rF);
        frontWheel.transform.localScale = v;

        // set camera offset from bicycle origin
        camera.transform.localPosition = new Vector3(0.162f, 0.0f, -1.38f);
        cameraRoll = true;

        q = new VizState();
        SetBicycleTransform(q);
        countdownInfo.text = "";

        if (GamePrefs.device != null)
        {
            serial = new SerialThread(GamePrefs.device, 115200);
        }
        else
        {
            serial = new SerialThread("/dev/tty.usbmodem311", 115200);
        }
        stopwatch = new System.Diagnostics.Stopwatch();
        pose      = new BicyclePose();

        serial.Start();
        timestamp = 0;
        stopwatch.Start();
    }
Ejemplo n.º 3
0
    // ------------------------------------------------------------------------
    // Invoked whenever the SerialController gameobject is deactivated.
    // It stops and destroys the thread that was reading from the serial device.
    // ------------------------------------------------------------------------
    void OnDisable()
    {
        // If there is a user-defined tear-down function, execute it before
        // closing the underlying COM port.
        if (userDefinedTearDownFunction != null)
        {
            userDefinedTearDownFunction();
        }

        // The serialThread reference should never be null at this point,
        // unless an Exception happened in the OnEnable(), in which case I've
        // no idea what face Unity will make.
        if (serialThread != null)
        {
            serialThread.RequestStop();
            serialThread = null;
        }

        // This reference shouldn't be null at this point anyway.
        if (thread != null)
        {
            thread.Join();
            thread = null;
        }
    }
 // ------------------------------------------------------------------------
 // Invoked whenever the SerialController gameobject is activated.
 // It creates a new thread that tries to connect to the serial device
 // and start reading from it.
 // ------------------------------------------------------------------------
 void OnEnable()
 {
     serialThread = new SerialThread(portName, baudRate, reconnectionDelay,
                                     maxUnreadMessages);
     thread = new Thread(new ThreadStart(serialThread.RunForever));
     thread.Start();
     Debug.LogWarning("Hit thread here");
     Console.WriteLine("test");
     Debug.Log("TESTSTST");
 }
    void disconnect()
    {
        // The serialThread reference should never be null at this point,
        // unless an Exception happened in the OnEnable(), in which case I've
        // no idea what face Unity will make.
        if (serialThread != null)
        {
            serialThread.RequestStop();
            serialThread = null;
        }

        // This reference shouldn't be null at this point anyway.
        if (thread != null)
        {
            thread.Join();
            thread = null;
        }
    }
Ejemplo n.º 6
0
    // Use this for initialization
    IEnumerator Start()
    {
        // Connect to the robot and start the serial thread
        var serialThread = new SerialThread(portName, baudRate, delayBeforeReconnecting, maxUnreadMessages);
        var thread       = new Thread(new ThreadStart(serialThread.RunForever));

        thread.Start();

        // Connect to the Tellynet web socket server
        var ws = new WebSocket(tellynetSocketProtocol + tellynetServer + tellynetPort);

        try {
            ws.Connect();
            socket = ws;

            //Send a room specific join message to Tellynet
            ws.Send("join #letsrobot");

            //Grabs messages from TellyNet
            ws.OnMessage += (sender, e) => {
                //Check to see if robot says first.
                string botReply = serialThread.ReadSerialMessage();

                if (botReply != null)
                {
                    Debug.Log("Bot replied: " + botReply);
                }


                Console.WriteLine("Laputa says: " + e.Data);
                string reply = e.Data;
                if (e.IsText)
                {
                    //The messages from TellyNet are JSON objects
                    try {
                        SimpleJSON.JSONNode msg = SimpleJSON.JSON.Parse(reply);

                        //This is a special use case for the bitslapper,
                        //We need to develop a whole new command protocol instead.
                        if (msg ["bits"].Value != "")
                        {
                            Debug.Log(msg ["bits"].Value);
                            var bitAmount = int.Parse(msg ["bits"].Value);
                            if (bitAmount >= 100)
                            {
                                toRobot = "bitslap";
                            }
                            else if (bitAmount <= 99 && bitAmount >= 10)
                            {
                                toRobot = "bittyslap";
                            }
                        }
                        else
                        {
                            //Comment this out if you don't want to send all messages to robot
                            toRobot = msg["message"].Value;
                        }

                        //example of how to grab user chat messages from TellyNet.
                        Debug.Log(msg ["username"].Value + ": " + msg ["message"].Value);
                    } catch (KeyNotFoundException) {
                    }
                    //If there is a message we can send the robot, that happens here.
                    if (toRobot != null)
                    {
                        serialThread.SendSerialMessage(toRobot);
                        Debug.Log(toRobot);
                        toRobot = "";
                    }
                }
            };
        } catch { Debug.Log("Can't connect to TellyNet"); }
        yield return(0);
    }