Ejemplo n.º 1
0
        private void FixedUpdate()
        {
            // brakeTorque
            brakeTorque = wheel.brakeTorque;

            // center
            center = wheel.center;

            // forceAppPointDistance // not implemented

            // forwardFriction
            forwardFriction = wheel.forwardFriction;

            // isGrounded
            isGrounded = wheel.isGrounded;

            // mass
            mass = wheel.mass;

            // motorTorque
            motorTorque = wheel.motorTorque;

            // radius
            radius = wheel.radius;

            // rpm
            rpm = wheel.rpm;

            // sidewaysFriction
            sidewaysFriction = wheel.sideFriction;

            // sprungMass // not implemented, use wheel.suspensionForce instead.

            // steerAngle
            steerAngle = wheel.steerAngle;

            // suspensionDistance
            suspensionDistance = wheel.suspensionDistance;

            // suspensionSpring // not implemented, use wheel.spring instead

            // wheelDampingRate // use damper.reboundForce and damper.bumpForce

            // GetGroundHit()
            wheel.GetGroundHit(out WheelHit hit);

            // GetWorldPose()
            wheel.GetWorldPose(out position, out rotation);
        }
Ejemplo n.º 2
0
        public static void CalculateLateralSlip(float dt, float velocityMagnitude, float angularVelocity,
                                                float loadCoefficient,
                                                float forwardSpeed, ref FrictionPreset frictionPreset, ref Friction friction, bool hasHit,
                                                out float surfaceForce)
        {
            surfaceForce = 0;
            float sideSpeed       = friction.speed;
            float absForwardSpeed = forwardSpeed < 0 ? -forwardSpeed : forwardSpeed;
            float absAngVel       = angularVelocity < 0 ? -angularVelocity : angularVelocity;

            if (hasHit)
            {
                if (velocityMagnitude < 0.35f && absAngVel < 1f)
                {
                    friction.PI_error     = friction.speed;
                    friction.PI_integral += friction.PI_error;
                    friction.slip         = friction.Kp * friction.PI_error
                                            + friction.Ki * friction.PI_integral;
                    friction.slip = friction.slip <-1f ? -1f : friction.slip> 1f ? 1f : friction.slip;
                }
                else
                {
                    if (velocityMagnitude < 0.8f && absAngVel < 6f)
                    {
                        friction.slip = sideSpeed * 0.25f;
                    }
                    else
                    {
                        friction.slip = Mathf.Atan2(sideSpeed, absForwardSpeed) * Mathf.Rad2Deg / 80.0f;
                    }

                    friction.PI_error    = 0;
                    friction.PI_integral = 0;
                }

                friction.slip *= friction.slipCoefficient;
                friction.slip  = friction.slip <-1f ? -1f : friction.slip> 1f ? 1f : friction.slip;
                float absSlip  = friction.slip < 0 ? -friction.slip : friction.slip;
                float slipSign = friction.slip < 0 ? -1f : 1f;
                float curveVal = frictionPreset.Curve.Evaluate(absSlip);
                surfaceForce = slipSign * curveVal * loadCoefficient * friction.forceCoefficient;
            }
        }
Ejemplo n.º 3
0
        public static float CalculateLongitudinalSlip(float torque, float brakeTorque, float dragTorque, float wheelRadius,
                                                      float wheelInertia,
                                                      float dt, float fixedDeltaTime, float loadCoefficient, float BCDEz, ref Friction friction,
                                                      ref float angularVelocity, ref float outSurfaceTorque)
        {
            float speed         = friction.speed;
            float absSpeed      = friction.speed < 0 ? -friction.speed : friction.speed;
            float initialAngVel = angularVelocity;

            angularVelocity += torque / wheelInertia * dt;

            brakeTorque += dragTorque;
            brakeTorque  = brakeTorque * (angularVelocity > 0 ? -1f : 1f);
            float brakeTorqueCap = (angularVelocity < 0 ? -angularVelocity : angularVelocity) * wheelInertia / dt;

            brakeTorque = brakeTorque > brakeTorqueCap ? brakeTorqueCap :
                          brakeTorque < -brakeTorqueCap ? -brakeTorqueCap : brakeTorque;
            angularVelocity += brakeTorque / wheelInertia * dt;

            float freeAngularVelocity  = speed / wheelRadius;
            float errorAngularVelocity = angularVelocity - freeAngularVelocity;
            float errorTorque          = errorAngularVelocity * wheelInertia / dt;
            float maxTorque            = loadCoefficient * BCDEz * friction.forceCoefficient * 0.8f;
            float groundTorque         = errorTorque <-maxTorque? - maxTorque :
                                                      errorTorque> maxTorque ? maxTorque : errorTorque;
            float thresholdVelocity = 0.8f;

            if (absSpeed > thresholdVelocity)
            {
                friction.slip = (speed - angularVelocity * wheelRadius) / absSpeed;
            }
            else
            {
                float Vsx = speed - angularVelocity * wheelRadius;
                friction.slip = 2f * Vsx / (thresholdVelocity + speed * speed / thresholdVelocity);
            }

            friction.slip *= friction.slipCoefficient;
            friction.slip  = friction.slip <-1f ? -1f : friction.slip> 1f ? 1f : friction.slip;

            angularVelocity -= groundTorque / wheelInertia * dt;

            float deltaOmegaTorque = (angularVelocity - initialAngVel) * wheelInertia / dt;

            outSurfaceTorque += groundTorque * (dt / fixedDeltaTime);

            float counterTorque = -groundTorque + brakeTorque - deltaOmegaTorque;

#if NVP_DEBUG_PT
            if (Powertrain.DEBUG)
            {
                Debug.Log($"{name} (SendTorque)\tTreceived = {torque}\tTbrake =\tTreact = {Tground}\tslip = {_slip}\t" +
                          $"W = {angularVelocity}\tIsum = {inertiaSum}\t Returning T = {Treturn}");
            }
#endif

            return(counterTorque * 0.9f);
        }