// Update is called once per frame
    void Update()
    {
        DirectInputWrapper.Update();
        DeviceState state = DirectInputWrapper.GetStateManaged(0);

        if (Input.GetKeyDown(KeyCode.P)) {
            //Debug.Log("PLAY: " + DirectInputWrapper.PlaySpringForce(0, 0, sat, coeff));
            //Debug.Log("PLAY: " + DirectInputWrapper.PlayConstantForce(0, constant));
            Debug.Log("PLAY: " + DirectInputWrapper.PlayDamperForce(0, damper));
            playing = true;
        }

        if (Input.GetKeyDown(KeyCode.S))
        {
            //Debug.Log("STOP:" + DirectInputWrapper.StopSpringForce(0));
            //Debug.Log("STOP:" + DirectInputWrapper.StopConstantForce(0));
            Debug.Log("STOP:" + DirectInputWrapper.StopDamperForce(0));
            playing = false;
        }
        
        if(playing)
        {
         //   DirectInputWrapper.UpdateConstantForce(0, constant);
        }

    }
    // Update is called once per frame
    void Update()
    {
        DirectInputWrapper.Update();

        DeviceState rec = DirectInputWrapper.GetStateManaged(wheelIndex);

        //    steerInput = rec.lX / 32768f;
        //      accelInput = rec.rglSlider[0] / -32768f;
        x  = rec.lX;
        y  = rec.lY;
        z  = rec.lZ;
        s0 = rec.rglSlider[0];
        s1 = rec.rglSlider[1];

        Debug.Log("d0 = " + DirectInputWrapper.GetProductNameManaged(wheelIndex));
        Debug.Log("ff0 = " + DirectInputWrapper.HasForceFeedback(wheelIndex));

        if (forceFeedbackPlaying)
        {
            DirectInputWrapper.PlayConstantForce(wheelIndex, constant);
        }


        if (DirectInputWrapper.DevicesCount() > 1)
        {
            Debug.Log("d1 = " + DirectInputWrapper.GetProductNameManaged(pedalIndex));
            Debug.Log("ff1 = " + DirectInputWrapper.HasForceFeedback(pedalIndex));
            DeviceState rec2 = DirectInputWrapper.GetStateManaged(pedalIndex);
            //     accelInput = ((32768 + rec2.lZ) - (rec2.lY + 32768f)) / 65535f;
            x2  = rec2.lX;
            y2  = rec2.lY;
            z2  = rec2.lZ;
            s02 = rec2.rglSlider[0];
            s12 = rec2.rglSlider[1];
        }
    }
Exemple #3
0
    public void OnUpdate()
    {
        if (workingInstance != this || !available || !enable)
        {
            return;
        }

        float accelInput;
        uint  pov;

        byte[] buttons;

        if (SystemInfo.operatingSystemFamily == OperatingSystemFamily.Windows)
        {
            if (DirectInputWrapper.DevicesCount() == 0)
            {
                timeAccumulator += UnityEngine.Time.deltaTime;
                if (timeAccumulator >= 1.0f)
                {
                    Init();
                    timeAccumulator = 0.0f;
                }
            }

            DirectInputWrapper.Update();

            DeviceState state;
            if (!DirectInputWrapper.GetStateManaged(wheelIndex, out state))
            {
                var error = $"Can not get valid device state for steering wheel {wheelIndex}, try reconnect and restart game to fix";
                Debug.Log(error);
                stateFail = error;
                available = false;
                return;
            }
            SteerInput = state.lX / 32768f;
            accelInput = (state.lY - state.lRz) / -32768f;

            if (!useFeedback || !useGroundFeedbackForce)
            {
                DirectInputWrapper.PlayConstantForce(wheelIndex, 0);
                DirectInputWrapper.PlayDamperForce(wheelIndex, Mathf.Clamp(Mathf.RoundToInt(damper * forceFeedbackGain), -10000, 10000));
                DirectInputWrapper.PlaySpringForce(wheelIndex, 0, 0, springCoefficient);
            }

            if (useFeedback && (useGroundFeedbackForce || (autonomousBehavior != SteerWheelAutonomousFeedbackBehavior.None)))
            {
                float totalConstForce = 0.0f;
                if (useGroundFeedbackForce)
                {
                    totalConstForce += constant * forceFeedbackGain;
                }

                if (autonomousBehavior != SteerWheelAutonomousFeedbackBehavior.None)
                {
                    totalConstForce += autoforce * autoForceGain;
                }

                //applying effects to steering wheel
                var value = Mathf.RoundToInt(totalConstForce);
                if (value < -100)
                {
                    value = Mathf.Clamp(value, -10000, -800);
                }
                if (value > 100)
                {
                    value = Mathf.Clamp(value, 800, 10000);
                }

                DirectInputWrapper.PlayConstantForce(wheelIndex, Mathf.Clamp(value, -10000, 10000));
                if (useGroundFeedbackForce)
                {
                    DirectInputWrapper.PlayDamperForce(wheelIndex, Mathf.Clamp(Mathf.RoundToInt(damper * forceFeedbackGain), -10000, 10000));
                    DirectInputWrapper.PlaySpringForce(wheelIndex, 0, Mathf.Clamp(Mathf.RoundToInt(springSaturation * forceFeedbackGain), 0, 10000), springCoefficient);
                }
            }

            pov     = state.rgdwPOV[0];
            buttons = state.rgbButtons;
        }
        else
        {
            SteerInput = Input.GetAxis("Steering");

            // pedal range is -1 (not pressed) to +1 (pressed)
            // but by default when user has not pressed pedal the Unity reports value 0
            float accel = Input.GetAxis("Acceleration");
            float brake = Input.GetAxis("Braking");
            if (accel != 0.0f)
            {
                accelPressedOnce = true;
            }
            if (brake != 0.0f)
            {
                brakePressedOnce = true;
            }
            if (!accelPressedOnce)
            {
                accel = -1.0f;
            }
            if (!brakePressedOnce)
            {
                brake = -1.0f;
            }
            accelInput = (accel - brake) / 2.0f;

            pov = 0;
            // TODO
            // if (Input.GetButton("SelectUp")) pov = 0;
            // else if (Input.GetButton("SelectRight")) pov = 9000;
            // else if (Input.GetButton("SelectDown")) pov = 18000;
            // else if (Input.GetButton("SelectLeft")) pov = 27000;

            buttons = new byte [32];
            // TODO: here fill buttons according to buttonMapping array above
            buttons[0]  = (byte)(Input.GetButton("TurnOffAutonomousMode") ? 1 : 0);
            buttons[1]  = (byte)(Input.GetButton("ToggleMainCamera") ? 1 : 0);
            buttons[8]  = (byte)(Input.GetButton("ShiftUp") ? 1 : 0);
            buttons[9]  = (byte)(Input.GetButton("ShiftDown") ? 1 : 0);
            buttons[10] = (byte)(Input.GetButton("EngineStartStop") ? 1 : 0);
        }

        if (accelInput >= 0)
        {
            AccelBrakeInput = pedalInput == null ? accelInput : pedalInput.throttleInputCurve.Evaluate(accelInput);
        }
        else
        {
            AccelBrakeInput = pedalInput == null ? accelInput: -pedalInput.brakeInputCurve.Evaluate(-accelInput);
        }

        foreach (var m in buttonMapping)
        {
            if (buttons[m.Key] != 0)
            {
                TriggerDown(m.Value);
            }

            if (oldButtons[m.Key] == 0 && buttons[m.Key] != 0)
            {
                TriggerPress(m.Value);
            }
            else if (oldButtons[m.Key] != 0 && buttons[m.Key] == 0)
            {
                TriggerRelease(m.Value);
            }
        }

        System.Array.Copy(buttons, oldButtons, oldButtons.Length);

        Action <uint, Action <InputEvent> > povAction = (uint value, Action <InputEvent> action) =>
        {
            switch (value)
            {
            case 0:
                action(InputEvent.SELECT_UP);
                break;

            case 9000:
                action(InputEvent.SELECT_RIGHT);
                break;

            case 18000:
                action(InputEvent.SELECT_DOWN);
                break;

            case 27000:
                action(InputEvent.SELECT_LEFT);
                break;

            default:
                break;
            }
        };

        povAction(pov, x => TriggerDown(x));

        if (pov != oldPov)
        {
            povAction(oldPov, x => TriggerRelease(x));
            povAction(pov, x => TriggerPress(x));
        }

        oldPov = pov;
    }
    public override void OnUpdate()
    {
        if (inited != this)
        {
            return;
        }

        //check for SelectLeft/right actions
        if (currentSelectState == SelectState.REST && GetSteerInput() < -selectThreshold)
        {
            currentSelectState = SelectState.LEFT;
            TriggerEvent(EventType.SELECT_CHOICE_LEFT);
        }
        else if (currentSelectState == SelectState.LEFT && GetSteerInput() > -selectThreshold)
        {
            currentSelectState = SelectState.REST;
        }
        else if (currentSelectState == SelectState.REST && GetSteerInput() > selectThreshold)
        {
            currentSelectState = SelectState.RIGHT;
            TriggerEvent(EventType.SELECT_CHOICE_RIGHT);
        }
        else if (currentSelectState == SelectState.RIGHT && GetSteerInput() < selectThreshold)
        {
            currentSelectState = SelectState.REST;
        }

        //Check for Throttle confirm
        if (isConfirmDown && GetAccelBrakeInput() < selectThreshold)
        {
            isConfirmDown = false;
        }
        else if (!isConfirmDown && GetAccelBrakeInput() > selectThreshold)
        {
            isConfirmDown = true;
            if (Time.timeSinceLevelLoad > confirmTimeout)
            {
                TriggerEvent(EventType.SELECT_CHOICE_CONFIRM);
            }
        }



        DirectInputWrapper.Update();

        {
            DeviceState state = DirectInputWrapper.GetStateManaged(wheelIndex);
            if (state.lX == 0 && state.lY == 0 && state.lRz == 0)
            {
                return;
            }
            //steerInput = state.lX / 32768f;
            steerInput = Mathf.Clamp(state.lX / 32200f, -1f, 1f);

            //accelInput = state.rglSlider[0] / -32768f;
            //ly accel, lrz freno
            //float gas = (float) state.lY / 32768;
            //float indiceGas = (float(state.lY);
            float gas   = ((float)state.lY - 32767) / -64378f;
            float brake = ((float)state.lRz - 32767) / 65403f;
            //float brake = (float) state.lRz / 32768;

            accelInput = gas + brake;

            /*float totalGas = (maxGas - minGas);
             * float totalBrake = (maxBrake - minBrake);
             *
             * accelInput = (gas - minGas) / totalGas - (brake - minBrake) / totalBrake;*/

            /* x = state.lX;
             * y = state.lY;
             * z = state.lZ;
             * s0 = state.rglSlider[0];
             * s1 = state.rglSlider[1];*/
            /*if (forceFeedbackPlaying)
             * {*/

            if (!guidaAutomatica)
            {
                DirectInputWrapper.PlayConstantForce(wheelIndex, Mathf.RoundToInt(constant * FFBGain));
                DirectInputWrapper.PlayDamperForce(wheelIndex, Mathf.RoundToInt(damper * FFBGain));
                DirectInputWrapper.PlaySpringForce(wheelIndex, 0, Mathf.RoundToInt(springSaturation * FFBGain), springCoefficient);
            }
            //DirectInputWrapper.PlayConstantForce(wheelIndex, Mathf.RoundToInt(constant * FFBGain));

            /*DirectInputWrapper.PlayDamperForce(wheelIndex, Mathf.RoundToInt(damper * FFBGain));
             * DirectInputWrapper.PlaySpringForce(wheelIndex, 0, Mathf.RoundToInt(springSaturation * FFBGain), springCoefficient);*/
            //}


            /*if(DirectInputWrapper.DevicesCount() > 1)
             * {
             *  DeviceState state2 = DirectInputWrapper.GetStateManaged(pedalIndex);
             *  int gas = 0;
             *  int brake = 0;
             *
             * /* x2 = state2.lX;
             *  y2 = state2.lY;
             *  z2 = state2.lZ;
             *  s02 = state2.rglSlider[0];
             *  s12 = state2.rglSlider[1];*//*
             *
             *  switch (gasAxis) {
             *      case "X":
             *          gas = state2.lX;
             *          break;
             *      case "Y":
             *          gas = state2.lY;
             *          break;
             *      case "Z":
             *          gas = state2.lZ;
             *          break;
             *  }
             *
             *  switch (brakeAxis)
             *  {
             *      case "X":
             *          brake = state2.lX;
             *          break;
             *      case "Y":
             *          brake = state2.lY;
             *          break;
             *      case "Z":
             *          brake = state2.lZ;
             *          break;
             *  }
             *
             *
             *  float totalGas = (maxGas - minGas);
             *  float totalBrake = (maxBrake - minBrake);
             *
             *  accelInput = (gas - minGas) / totalGas - (brake - minBrake) / totalBrake;
             * }/*/
        }
    }
Exemple #5
0
    public override void OnUpdate()
    {
        if (inited != this)
        {
            return;
        }

        //check for SelectLeft/right actions
        if (currentSelectState == SelectState.REST && GetSteerInput() < -selectThreshold)
        {
            currentSelectState = SelectState.LEFT;
            TriggerEvent(EventType.SELECT_CHOICE_LEFT);
        }
        else if (currentSelectState == SelectState.LEFT && GetSteerInput() > -selectThreshold)
        {
            currentSelectState = SelectState.REST;
        }
        else if (currentSelectState == SelectState.REST && GetSteerInput() > selectThreshold)
        {
            currentSelectState = SelectState.RIGHT;
            TriggerEvent(EventType.SELECT_CHOICE_RIGHT);
        }
        else if (currentSelectState == SelectState.RIGHT && GetSteerInput() < selectThreshold)
        {
            currentSelectState = SelectState.REST;
        }

        //Check for Throttle confirm
        if (isConfirmDown && GetAccelBrakeInput() < selectThreshold)
        {
            isConfirmDown = false;
        }
        else if (!isConfirmDown && GetAccelBrakeInput() > selectThreshold)
        {
            isConfirmDown = true;
            if (Time.timeSinceLevelLoad > confirmTimeout)
            {
                TriggerEvent(EventType.SELECT_CHOICE_CONFIRM);
            }
        }



        DirectInputWrapper.Update();

        {
            DeviceState state = DirectInputWrapper.GetStateManaged(wheelIndex);
            steerInput = state.lX / 32768f;
            accelInput = state.rglSlider[0] / -32768f;

            /* x = state.lX;
             * y = state.lY;
             * z = state.lZ;
             * s0 = state.rglSlider[0];
             * s1 = state.rglSlider[1];*/
            if (forceFeedbackPlaying)
            {
                DirectInputWrapper.PlayConstantForce(wheelIndex, Mathf.RoundToInt(constant * FFBGain));
                DirectInputWrapper.PlayDamperForce(wheelIndex, Mathf.RoundToInt(damper * FFBGain));
                DirectInputWrapper.PlaySpringForce(wheelIndex, 0, Mathf.RoundToInt(springSaturation * FFBGain), springCoefficient);
            }


            if (DirectInputWrapper.DevicesCount() > 1)
            {
                DeviceState state2 = DirectInputWrapper.GetStateManaged(pedalIndex);
                int         gas    = 0;
                int         brake  = 0;

                /* x2 = state2.lX;
                 * y2 = state2.lY;
                 * z2 = state2.lZ;
                 * s02 = state2.rglSlider[0];
                 * s12 = state2.rglSlider[1];*/

                switch (gasAxis)
                {
                case "X":
                    gas = state2.lX;
                    break;

                case "Y":
                    gas = state2.lY;
                    break;

                case "Z":
                    gas = state2.lZ;
                    break;
                }

                switch (brakeAxis)
                {
                case "X":
                    brake = state2.lX;
                    break;

                case "Y":
                    brake = state2.lY;
                    break;

                case "Z":
                    brake = state2.lZ;
                    break;
                }


                float totalGas   = (maxGas - minGas);
                float totalBrake = (maxBrake - minBrake);

                accelInput = (gas - minGas) / totalGas - (brake - minBrake) / totalBrake;
            }
        }
    }
    public override void OnUpdate()
    {
        if (inited != this)
        {
            return;
        }

        //check for SelectLeft/right actions
        if (currentSelectState == SelectState.REST && GetSteerInput() < -selectThreshold)
        {
            currentSelectState = SelectState.LEFT;
            TriggerEvent(EventType.SELECT_CHOICE_LEFT);
        }
        else if (currentSelectState == SelectState.LEFT && GetSteerInput() > -selectThreshold)
        {
            currentSelectState = SelectState.REST;
        }
        else if (currentSelectState == SelectState.REST && GetSteerInput() > selectThreshold)
        {
            currentSelectState = SelectState.RIGHT;
            TriggerEvent(EventType.SELECT_CHOICE_RIGHT);
        }
        else if (currentSelectState == SelectState.RIGHT && GetSteerInput() < selectThreshold)
        {
            currentSelectState = SelectState.REST;
        }

        //Check for Throttle confirm
        if (isConfirmDown && GetAccelBrakeInput() < selectThreshold)
        {
            isConfirmDown = false;
        }
        else if (!isConfirmDown && GetAccelBrakeInput() > selectThreshold)
        {
            isConfirmDown = true;
            if (Time.timeSinceLevelLoad > confirmTimeout)
            {
                TriggerEvent(EventType.SELECT_CHOICE_CONFIRM);
            }
        }



        if (Application.platform != RuntimePlatform.OSXEditor)
        {
            DirectInputWrapper.Update();

            {
                DeviceState state;
                DeviceState slaveState;


                if (MasterSteeringWheel)
                {
                    state         = DirectInputWrapper.GetStateManaged(masterIndex);
                    slaveState    = DirectInputWrapper.GetStateManaged(wheelIndex);
                    slaveSteering = slaveState.lX / 32768f;
                }
                else
                {
                    state = DirectInputWrapper.GetStateManaged(wheelIndex);
                }

                steerInput = state.lX / 32768f;
                accelInput = state.rglSlider [0] / -32768f;


                // Debug.Log("Device One: \tlRx: " + state.lRx + "\tlRy: " + state.lRy + "\tlRz: " + state.lRz + "\tlX: " + state.lX + "\tlY: " + state.lY + "\tlZ: " + state.lZ);


                /* x = state.lX;
                 * y = state.lY;
                 * z = state.lZ;
                 * s0 = state.rglSlider[0];
                 * s1 = state.rglSlider[1];*/
                if (forceFeedbackPlaying)
                {
                    if (MasterSteeringWheel)
                    {
                        DirectInputWrapper.PlayConstantForce(masterIndex, Mathf.RoundToInt(constant * FFBGain));
                        DirectInputWrapper.PlayDamperForce(masterIndex, Mathf.RoundToInt(damper * FFBGain));
                        DirectInputWrapper.PlaySpringForce(masterIndex, 0, Mathf.RoundToInt(springSaturation * FFBGain), springCoefficient);

                        DirectInputWrapper.PlayConstantForce(wheelIndex, Mathf.RoundToInt(slaveConstant * FFBGain));
                        DirectInputWrapper.PlayDamperForce(wheelIndex, Mathf.RoundToInt(slaveDamper * FFBGain));
                        DirectInputWrapper.PlaySpringForce(wheelIndex, 0, Mathf.RoundToInt(slaveSpringSaturation * FFBGain), slaveSpringCoefficient);
                    }
                    else
                    {
                        DirectInputWrapper.PlayConstantForce(wheelIndex, Mathf.RoundToInt(constant * FFBGain));
                        DirectInputWrapper.PlayDamperForce(wheelIndex, Mathf.RoundToInt(damper * FFBGain));
                        DirectInputWrapper.PlaySpringForce(wheelIndex, 0, Mathf.RoundToInt(springSaturation * FFBGain), springCoefficient);
                    }
                }
                if (DirectInputWrapper.DevicesCount() > 1 || MasterSteeringWheel)
                {
                    int gas   = 0;
                    int brake = 0;
                    if (DirectInputWrapper.DevicesCount() > 1 && !MasterSteeringWheel)
                    {
                        DeviceState state2 = DirectInputWrapper.GetStateManaged(pedalIndex);

                        /* x2 = state2.lX;
                         * y2 = state2.lY;
                         * z2 = state2.lZ;
                         * s02 = state2.rglSlider[0];
                         * s12 = state2.rglSlider[1];*/
                        switch (gasAxis)
                        {
                        case "X":
                            gas = state2.lX;
                            break;

                        case "Y":
                            gas = state2.lY;
                            break;

                        case "Z":
                            gas = state2.lZ;
                            break;
                        }

                        switch (brakeAxis)
                        {
                        case "X":
                            brake = state2.lX;
                            break;

                        case "Y":
                            brake = state2.lY;
                            break;

                        case "Z":
                            brake = state2.lZ;
                            break;
                        }
                    }
                    if (MasterSteeringWheel)
                    {
                        brake = state.lRz;
                        gas   = state.lY;
                    }
                    //Debug.Log(brake.ToString() + " break and gas" + gas.ToString());
                    float totalGas   = (maxGas - minGas);
                    float totalBrake = (maxBrake - minBrake);

                    accelInput = (gas - minGas) / totalGas - (brake - minBrake) / totalBrake;
                }
            }
        }
    }