コード例 #1
0
        public override void OnBridgeSetup(BridgeInstance bridge)
        {
            if (bridge.Plugin.GetBridgeNameAttribute().Type == "ROS")
            {
                bridge.AddSubscriber <VehicleControlData>(Topic, data =>
                {
                    if (Time.timeScale == 0f)
                    {
                        return;
                    }

                    controlData       = data;
                    LastControlUpdate = SimulatorManager.Instance.CurrentTime;

                    float wheelAngle = data.SteerAngle.GetValueOrDefault();
                    wheelAngle       = UnityEngine.Mathf.Clamp(wheelAngle, -Dynamics.MaxSteeringAngle, Dynamics.MaxSteeringAngle);
                    var k            = (float)(wheelAngle + Dynamics.MaxSteeringAngle) / (Dynamics.MaxSteeringAngle * 2);

                    ADSteerInput = UnityEngine.Mathf.Lerp(-1f, 1f, k);
                    ADAccelInput = data.Acceleration.GetValueOrDefault() - data.Braking.GetValueOrDefault();
                });
            }
            else if (bridge.Plugin.GetBridgeNameAttribute().Type == "ROS2")
            {
                bridge.AddSubscriber <VehicleControlData>(Topic, data =>
                {
                    if (Time.timeScale == 0f)
                    {
                        return;
                    }

                    controlData       = data;
                    LastControlUpdate = SimulatorManager.Instance.CurrentTime;

                    float wheelAngle = data.SteerAngle.GetValueOrDefault();

                    ADSteerInput = wheelAngle;
                    ADAccelInput = data.Acceleration.GetValueOrDefault() - data.Braking.GetValueOrDefault();

                    if (data.TargetGear == GearPosition.Reverse)
                    {
                        Dynamics.ShiftReverseAutoGearBox();
                    }
                    else if (data.TargetGear == GearPosition.Drive)
                    {
                        Dynamics.ShiftFirstGear();
                    }
                });
            }
        }
コード例 #2
0
        public override void OnBridgeSetup(BridgeInstance bridge)
        {
            var subscriber = BridgeMessageDispatcher.Instance.GetSynchronousSubscriber <VehicleControlData>(data =>
            {
                if (!IsControlReceived)
                {
                    IsControlReceived = true;

                    if (ApiManager.Instance != null)
                    {
                        var jsonData = new JSONObject();
                        ApiManager.Instance.AddCustom(transform.parent.gameObject, "checkControl", jsonData);
                    }
                }

                controlData       = data;
                LastControlUpdate = SimulatorManager.Instance.CurrentTime;

                if (double.IsInfinity(data.Acceleration.GetValueOrDefault()) || double.IsInfinity(data.Braking.GetValueOrDefault()) ||
                    double.IsNaN(data.Acceleration.GetValueOrDefault()) || double.IsNaN(data.Braking.GetValueOrDefault()))
                {
                    return;
                }

                var timeStamp = data.TimeStampSec.GetValueOrDefault();
                var dt        = (float)(timeStamp - LastTimeStamp);
                LastTimeStamp = timeStamp;

                Debug.Assert(data.Acceleration.GetValueOrDefault() >= 0 && data.Acceleration.GetValueOrDefault() <= 1);
                Debug.Assert(data.Braking.GetValueOrDefault() >= 0 && data.Braking.GetValueOrDefault() <= 1);
                var linearAccel = AccelerationInputCurve.Evaluate(data.Acceleration.GetValueOrDefault()) - BrakeInputCurve.Evaluate(data.Braking.GetValueOrDefault());

                var steeringTarget = -data.SteerTarget.GetValueOrDefault();
                var steeringAngle  = Controller.SteerInput;
                var sgn            = Mathf.Sign(steeringTarget - steeringAngle);
                var steeringRate   = data.SteerRate.GetValueOrDefault() * sgn;

                steeringAngle += steeringRate * dt;
                if (sgn != steeringTarget - steeringAngle)  // prevent oversteering
                {
                    steeringAngle = steeringTarget;
                }

                ADSteerInput = steeringAngle;
                ADAccelInput = linearAccel;

                if (data.CurrentGear == GearPosition.Reverse)
                {
                    Dynamics.ShiftReverseAutoGearBox();
                }
                else if (data.CurrentGear == GearPosition.Drive)
                {
                    Dynamics.ShiftFirstGear();
                }
            });

            bridge.AddSubscriber(Topic, subscriber);
        }
コード例 #3
0
        public override void OnBridgeSetup(BridgeInstance bridge)
        {
            Bridge = bridge;
            Bridge.AddSubscriber <VehicleControlData>(Topic, data =>
            {
                if (Time.timeScale == 0f)
                {
                    return;
                }

                LastControlUpdate = SimulatorManager.Instance.CurrentTime;
                ADSteerInput      = data.SteerInput.GetValueOrDefault();
                InferenceTime     = data.TimeStampSec.GetValueOrDefault();

                ControlMessages++;
                TotalInferenceTime  += InferenceTime;
                AverageInferenceTime = TotalInferenceTime / (float)ControlMessages;
            });
        }
コード例 #4
0
        public override void OnBridgeSetup(BridgeInstance bridge)
        {
            bridge.AddSubscriber <VehicleStateData>(Topic, data =>
            {
                if (Time.timeScale == 0f)
                {
                    return;
                }

                if (data != null)
                {
                    if (StateData.Blinker != data.Blinker)
                    {
                        if (data.Blinker == 0)
                        {
                            if (Actions.LeftTurnSignal)
                            {
                                Actions.LeftTurnSignal = false;
                            }
                            if (Actions.RightTurnSignal)
                            {
                                Actions.RightTurnSignal = false;
                            }
                            if (Actions.HazardLights)
                            {
                                Actions.HazardLights = false;
                            }
                        }
                        else if (data.Blinker == 1)
                        {
                            Actions.LeftTurnSignal = true;
                        }
                        else if (data.Blinker == 2)
                        {
                            Actions.RightTurnSignal = true;
                        }
                        else if (data.Blinker == 3)
                        {
                            Actions.HazardLights = true;
                        }
                    }
                    if (StateData.HeadLight != data.HeadLight)
                    {
                        if (data.HeadLight == 0)
                        {
                            Actions.CurrentHeadLightState = HeadLightState.OFF;
                        }
                        else if (data.HeadLight == 1)
                        {
                            Actions.CurrentHeadLightState = HeadLightState.LOW;
                        }
                        else if (data.HeadLight == 2)
                        {
                            Actions.CurrentHeadLightState = HeadLightState.HIGH;
                        }
                    }
                    if (StateData.Gear != data.Gear)
                    {
                        if (data.Gear == (byte)GearPosition.Reverse)
                        {
                            Dynamics.ShiftReverseAutoGearBox();
                        }
                        else if (data.Gear == (byte)GearPosition.Drive)
                        {
                            Dynamics.ShiftFirstGear();
                        }
                    }
                    if (StateData.HandBrake != data.HandBrake)
                    {
                        if (data.HandBrake == true)
                        {
                            Dynamics.SetHandBrake(true);
                        }
                        else
                        {
                            Dynamics.SetHandBrake(false);
                        }
                    }

                    StateData = data;
                }
            });
        }
コード例 #5
0
 public override void OnBridgeSetup(BridgeInstance bridge)
 {
     bridge.AddSubscriber <Detected3DObjectArray>(Topic, data => Detected = data.Data);
 }
コード例 #6
0
        public override void OnBridgeSetup(BridgeInstance bridge)
        {
            bridge.AddSubscriber <VehicleControlData>(Topic, data =>
            {
                controlData       = data;
                LastControlUpdate = SimulatorManager.Instance.CurrentTime;

                if (data.Velocity.HasValue) // autoware
                {
                    controlType = ControlType.AutowareAi;
                    if (data.ShiftGearUp || data.ShiftGearDown)
                    {
                        if (data.ShiftGearUp)
                        {
                            Dynamics.GearboxShiftUp();
                        }
                        if (data.ShiftGearDown)
                        {
                            Dynamics.GearboxShiftDown();
                        }

                        ADAccelInput = data.Acceleration.GetValueOrDefault() - data.Braking.GetValueOrDefault(); // converted from lin accel
                        ADSteerInput = data.SteerAngle.GetValueOrDefault();                                      // angle should be in degrees
                    }
                    else
                    {
                        if (Dynamics.Reverse)
                        {
                            return;                   // TODO move?
                        }
                        var linMag   = Mathf.Clamp(Mathf.Abs(data.Velocity.GetValueOrDefault() - ActualLinVel), 0f, 1f);
                        ADAccelInput = ActualLinVel < data.Velocity.GetValueOrDefault() ? linMag : -linMag;
                        ADSteerInput = -Mathf.Clamp(data.SteerAngularVelocity.GetValueOrDefault() / (29 * (float)System.Math.PI / 180), -1f, 1f);
                        //ADASTEC 0.506 radian for 29 degrees
                        //Dividing with 0.506 radian is for normalizing -0.506,+0.506 radians to -1,1 unity values.
                    }
                }
                else if (data.SteerRate.HasValue) // apollo
                {
                    if (double.IsInfinity(data.Acceleration.GetValueOrDefault()) || double.IsInfinity(data.Braking.GetValueOrDefault()) ||
                        double.IsNaN(data.Acceleration.GetValueOrDefault()) || double.IsNaN(data.Braking.GetValueOrDefault()))
                    {
                        return;
                    }

                    controlType   = ControlType.Apollo;
                    var timeStamp = data.TimeStampSec.GetValueOrDefault();
                    var dt        = (float)(timeStamp - LastTimeStamp);
                    LastTimeStamp = timeStamp;

                    Debug.Assert(data.Acceleration.GetValueOrDefault() >= 0 && data.Acceleration.GetValueOrDefault() <= 1);
                    Debug.Assert(data.Braking.GetValueOrDefault() >= 0 && data.Braking.GetValueOrDefault() <= 1);
                    var linearAccel = AccelerationInputCurve.Evaluate(data.Acceleration.GetValueOrDefault()) - BrakeInputCurve.Evaluate(data.Braking.GetValueOrDefault());

                    var steeringTarget = -data.SteerTarget.GetValueOrDefault();
                    var steeringAngle  = Controller.SteerInput;
                    var sgn            = Mathf.Sign(steeringTarget - steeringAngle);
                    var steeringRate   = data.SteerRate.GetValueOrDefault() * sgn;
                    steeringAngle     += steeringRate * dt;

                    if (sgn != steeringTarget - steeringAngle) // to prevent oversteering
                    {
                        steeringAngle = steeringTarget;
                    }

                    ADSteerInput = steeringAngle;
                    ADAccelInput = linearAccel;

                    if (data.CurrentGear == GearPosition.Reverse)
                    {
                        Dynamics.ShiftReverseAutoGearBox();
                    }
                    else if (data.CurrentGear == GearPosition.Drive)
                    {
                        Dynamics.ShiftFirstGear();
                    }
                }
                else if (data.SteerInput.HasValue) // lgsvl
                {
                    controlType  = ControlType.LGSVL;
                    ADSteerInput = data.SteerInput.GetValueOrDefault();
                }
                else if (data.Acceleration.HasValue)
                {
                    controlType  = ControlType.AutowareAuto;
                    ADAccelInput = data.Acceleration.GetValueOrDefault() - data.Braking.GetValueOrDefault();
                    ADSteerInput = data.SteerAngle.GetValueOrDefault();
                }
                else
                {
                    controlType = ControlType.None;
                }
            });
        }