Exemplo n.º 1
0
    public int maxSpeed = 100; //0-100

    public void Update()
    {
        //See if remote control request has been made yet:
        if (remoteReceiveTimer > 0)
        {
            remoteReceiveTimer -= Time.deltaTime;
        }

        //Check if left or right turn buttons are pressed:
        float horizontal, vertical = 0;

        horizontal = joystick.GetX(); vertical = joystick.GetY();
        if (leftRotateButton != null && leftRotateButton.pressed)
        {
            horizontal = 1; vertical = 0;
        }
        else if (rightRotateButton != null && rightRotateButton.pressed)
        {
            horizontal = -1; vertical = 0;
        }

        //Update tank every time serial is ready to push another message
        if (serialTimer > 0)
        {
            serialTimer -= Time.deltaTime;
        }
        else
        {
            serialTimer = serialTime;
            //If USB Serial, let incoming Bluetooth/UDP messages override joystick
            if (sendMode == SendMode.SERIAL)
            {
                if (remoteReceiveTimer > 0)
                {
                    controlTank(remoteX, remoteY);
                }
                else
                {
                    controlTank(horizontal, vertical);
                }
            }
            //If Bluetooth or UDP, send data from Joystick to face device.
            else if (sendMode == SendMode.BLUETOOTH || sendMode == SendMode.UDP)
            {
                controlTank(horizontal, vertical);
            }
            //If Misty, send data from Joystick to Misty device.
            else if (sendMode == SendMode.MISTY)
            {
                if (leftRotateButton.pressed || rightRotateButton.pressed)
                {
                    misty.DriveTrack((int)(vertical * maxSpeed) - (int)(horizontal * maxSpeed), (int)(vertical * maxSpeed) + (int)(horizontal * maxSpeed));
                    //misty.DriveTime((int)(vertical*maxSpeed), -(int)(horizontal*maxSpeed), 1000);
                    //misty.Drive((int)(vertical*maxSpeed), -(int)(horizontal*maxSpeed));
                }
                else if (new Vector2(joystick.GetX(), joystick.GetY()).magnitude > 0.05f)
                {
                    //misty.DriveTrack((int)(vertical*maxSpeed) - (int)(horizontal*maxSpeed), (int)(vertical*maxSpeed) + (int)(horizontal*maxSpeed));
                    //misty.DriveTime((int)(vertical*maxSpeed), -(int)(horizontal*maxSpeed), 1000);
                    misty.Drive((int)(vertical * maxSpeed), -(int)(horizontal * maxSpeed));
                }
                else
                {
                    misty.Stop();
                }
            }
        }
    }
Exemplo n.º 2
0
    public void Update()
    {
        //See if remote control request has been made yet:
        if (remoteReceiveTimer > 0)
        {
            remoteReceiveTimer -= Time.deltaTime;
        }

        //Check if left or right turn buttons are pressed:
        float horizontal, vertical = 0;

        horizontal = joystick.GetX(); vertical = joystick.GetY();
        if (leftRotateButton != null && leftRotateButton.pressed)
        {
            horizontal = -1; vertical = 0;
        }
        else if (rightRotateButton != null && rightRotateButton.pressed)
        {
            horizontal = 1; vertical = 0;
        }

        //Update tank every time serial is ready to push another message
        if (serialTimer > 0)
        {
            serialTimer -= Time.deltaTime;
        }
        else
        {
            serialTimer = serialTime;
            //If USB Serial, let incoming Bluetooth/UDP messages override joystick

            /* if(sendMode == SendMode.SERIAL) {
             * if(remoteReceiveTimer > 0) { controlTank(remoteX, remoteY); }
             * else { controlTank(horizontal, vertical); }
             * }
             * //If Bluetooth or UDP, send data from Joystick to face device.
             * else if(sendMode == SendMode.BLUETOOTH || sendMode == SendMode.UDP) {
             * controlTank(horizontal, vertical);
             * } */
            //If Misty, send data from Joystick to Misty device.
            if (sendMode == SendMode.MISTY)
            {
                if (leftRotateButton.pressed || rightRotateButton.pressed)
                {
                    //misty.DriveTrack((int)(vertical*maxSpeed) - (int)(horizontal*maxSpeed), (int)(vertical*maxSpeed) + (int)(horizontal*maxSpeed));
                    //misty.DriveTime((int)(vertical*maxSpeed), -(int)(horizontal*maxSpeed), (int)(serialTime*2000));
                    misty.Drive((int)(vertical * maxSpeed), -(int)(horizontal * maxSpeed));
                    stopped = false;
                }
                else if (new Vector2(joystick.GetX(), joystick.GetY()).magnitude > 0.05f)
                {
                    //misty.DriveTrack((int)(vertical*maxSpeed) - (int)(horizontal*maxSpeed), (int)(vertical*maxSpeed) + (int)(horizontal*maxSpeed));
                    //misty.DriveTime((int)(vertical*maxSpeed), -(int)(horizontal*maxSpeed), (int)(serialTime*2000));
                    misty.Drive((int)(vertical * maxSpeed), -(int)(horizontal * maxSpeed));
                    stopped = false;
                }
                else
                {
                    //misty.Halt();
                    //Stop misty immediately
                    if (!stopped)
                    {
                        misty.Drive(0, 0);
                        misty.Stop();
                        misty.DisableHazardSensors(); //also disable hazard sensors
                        stopped = true;
                    }
                    //If for whatever reason Misty didn't stop, stop again every x counts to be safe.
                    stopCounter++;
                    if (stopCounter == 30)
                    {
                        stopCounter = 0;
                        misty.Drive(0, 0);
                        misty.Stop();
                        misty.DisableHazardSensors(); //also disable hazard sensors
                    }
                }
            }
        }
    }