Ejemplo n.º 1
0
        /// <summary>
        /// Suspends the listening for incoming PDU's.
        /// </summary>
        /// <param name="pdu">The <see cref="DIS.Dis1998.IMarshallable"/> PDU to be transmitted</param>
        /// <param name="group">The <see cref="DIS.Utilties.Networking.MulticastGroup"/> to send this PDU to.</param>
        public void sendPDU(IMarshallable pdu, MulticastGroup group)
        {
            DataOutputStream dos = new DataOutputStream(Endian.Big);

            pdu.MarshalAutoLengthSet(dos);
            byte[] data = dos.ConvertToBytes();
            UdpClient.Send(data, data.Length, new IPEndPoint(group.Address, Port));
        }
Ejemplo n.º 2
0
    //update is called once per frame
    private void Update()
    {
        //if the agent is crossing a road, they can choose a random faster speed - to simulat a human running
        NavMeshHit hit;

        if (!agent.SamplePathPosition(NavMesh.AllAreas, 0.0f, out hit))
        {
            if ((hit.mask & crossingMask) != 0)
            {
                int random = UnityEngine.Random.Range(8, 15);
                agent.speed = random; //run across roads
            }
            else
            {
                agent.speed = 5; //walk around the paths
            }
        }

        //if the agent is close to their waypoint, choose another at random
        if (agent.remainingDistance < 0.5)
        {
            int d = UnityEngine.Random.Range(0, wps.Length);
            agent.SetAreaCost(4, 20);
            agent.SetDestination(wps[d].transform.position);
        }
        //change the acceleration if the agent is turning corners to help navigate
        else if (agent.hasPath)
        {
            agent.isStopped = false;

            Vector3 toTarget  = agent.steeringTarget - this.transform.position;
            float   turnAngle = Vector3.Angle(this.transform.forward, toTarget);
            agent.acceleration = turnAngle * agent.speed;
        }

        // Simple dead reckoning algorithm below; checking to see if distance moved since last espdu (change) > threshold value
        change[0]      += Mathf.Abs(this.agent.transform.position.x - prev_location.x);
        prev_location.x = this.agent.transform.position.x;
        change[1]      += Mathf.Abs(this.agent.transform.position.y - prev_location.y);
        prev_location.y = this.agent.transform.position.y;

        if ((change[0] > threshold) | (change[1] > threshold))
        {
            change[0]         = 0;
            change[1]         = 0;
            this.sendNewEspdu = true;
        }
        else
        {
            this.sendNewEspdu = false;
        }

        // Sending the new Espdu if necessary (if Dead Reckoning threshold passed)
        if (this.sendNewEspdu)
        {
            // Declaring the position of the Bot (in WSP - World Space Position)
            Vector3Double loc = espdu.EntityLocation;                                   // Issues here

            loc.X = this.agent.transform.position.x;
            loc.Y = this.agent.transform.position.y;
            loc.Z = 0.0;


            if (espdu.EntityLocation.X.Equals(null) | espdu.EntityLocation.Y.Equals(null))
            {
                Debug.LogError("Espdu's location value is NULL!!!");
            }

            // Declaring the Bot's velocity
            Vector3Float vel = espdu.EntityLinearVelocity;

            vel.X = this.agent.velocity.x;
            vel.Y = this.agent.velocity.y;
            vel.Z = 0.0f;

            if (espdu.EntityLinearVelocity.X.Equals(null) | espdu.EntityLinearVelocity.Y.Equals(null))
            {
                Debug.LogError("Espdu's linear velocity value is NULL!!!");
            }

            // Declaring the DeadReckoning Algorithm to be used (R, P, W)
            espdu.DeadReckoningParameters.DeadReckoningAlgorithm = (byte)2;

            // Sending the Espdu
            espdu.Timestamp = DisTime.DisRelativeTimestamp;

            // Prepare output
            DataOutputStream dos = new DataOutputStream(Endian.Big);
            espdu.MarshalAutoLengthSet(dos);

            // Transmit broadcast messages
            Sender.SendMessages(dos.ConvertToBytes());
            string mess = string.Format("Message sent with TimeStamp [{0}] Time Of[{1}]", espdu.Timestamp, (espdu.Timestamp >> 1));
            Debug.Log(mess);
            this.sendNewEspdu = false;
        }
    }
Ejemplo n.º 3
0
    // Update is called once per frame
    void Update()
    {
        goalPosition = manager.transform.position;
        transform.up = this.velocity;

        if (this.velocity.magnitude == 0)
        {
            this.GetComponent <Rigidbody2D>().velocity =
                new Vector2(Random.Range(-5f, 5f), Random.Range(-5f, 5f));
        }

        flock();
        goalPosition = manager.transform.position;
        stayInBorder();

        this.GetComponent <Rigidbody2D>().rotation =
            this.GetComponent <Rigidbody2D>().angularVelocity *Time.deltaTime;

        if (
            (this.location.x - this.prev_location.x) > threshold // If position in x axis > threshold value, send Espdu
            )
        {
            this.prev_location = this.location;
            this.sendNewEspdu  = true;
        }
        else if (
            (this.location.y - this.prev_location.y) > threshold // If position in y axis > threshold value, send Espdu
            )
        {
            this.prev_location = this.location;
            this.sendNewEspdu  = true;
        }
        else
        {
            this.sendNewEspdu = false;
        }

        for (int i = 0; i < 1; i++)
        {
            // Declaring the position of the Bird (in WSP - World Space Position)
            Vector3Double loc = espdu.EntityLocation;
            loc.X = this.location.x;
            loc.Y = this.location.y;
            loc.Z = 0.0;

            // Declaring the Bird's velocity
            Vector3Float vel = espdu.EntityLinearVelocity;
            vel.X = this.velocity.x;
            vel.Y = this.velocity.y;
            vel.Z = 0.0f;

            // Declaring the DeadReckoning Algorithm to be used (R, P, W)
            espdu.DeadReckoningParameters.DeadReckoningAlgorithm = (byte)2;

            //// THIS IS IN TESTING PHASE --------------------------------------------------------------------
            //// Setting the angular velocity of the Bird (as above)
            //Vector3Float angvelo = new Vector3Float
            //{
            //    X = this.GetComponent<Rigidbody2D>().angularVelocity,
            //    Y = 0.0f,
            //    Z = 0.0f
            //};
            //espdu.DeadReckoningParameters.EntityAngularVelocity = angvelo;
            //// END OF TESTING BLOCK ------------------------------------------------------------------------
            if (this.sendNewEspdu)
            {
                espdu.Timestamp = DisTime.DisRelativeTimestamp;

                // Prepare output
                DataOutputStream dos = new DataOutputStream(Endian.Big);
                espdu.MarshalAutoLengthSet(dos);

                // Transmit broadcast messages
                Sender.SendMessages(dos.ConvertToBytes());
                string mess =
                    string
                    .Format("Message sent with TimeStamp [{0}] Time Of[{1}]",
                            espdu.Timestamp,
                            (espdu.Timestamp >> 1));
                Debug.Log(mess);
            }
        }
    }
Ejemplo n.º 4
0
        public static void Main(string[] args)
        {
            mcastAddress  = IPAddress.Parse("239.1.2.3");
            mcastPort     = 62040;
            broadcastPort = 62040;            //3000 for DisMapper default
            var espdu = new EntityStatePdu(); //Could use factory but easier this way

            //Alcatraz
            const double lat = 37.827;
            double       lon = -122.425;

            // Configure socket.
            //JoinMulticast();   //Used to talk to C# receiver.  No need to connect as we are just sending multicast
            //StartMulticast();  //Least preffered
            StartBroadcast();      //Used for DisMapper

            //Setup EntityState PDU
            espdu.ExerciseID = 1;
            var eid = espdu.EntityID;

            eid.Site        = 0;
            eid.Application = 1;
            eid.Entity      = 2;
            // Set the entity type. SISO has a big list of enumerations, so that by
            // specifying various numbers we can say this is an M1A2 American tank,
            // the USS Enterprise, and so on. We'll make this a tank. There is a
            // separate project elsehwhere in this project that implements DIS
            // enumerations in C++ and Java, but to keep things simple we just use
            // numbers here.
            var entityType = espdu.EntityType;

            entityType.EntityKind  = 1;     // Platform (vs lifeform, munition, sensor, etc.)
            entityType.Country     = 255;   // USA
            entityType.Domain      = 1;     // Land (vs air, surface, subsurface, space)
            entityType.Category    = 1;     // Tank
            entityType.Subcategory = 1;     // M1 Abrams
            entityType.Specific    = 3;     // M1A2 Abrams

            for (int i = 0; i < 100; i++)
            {
                lon += (i / 1000.0);

                double[] disCoordinates = CoordinateConversions.getXYZfromLatLonDegrees(lat, lon, 0.0);

                var location = espdu.EntityLocation;
                location.X      = disCoordinates[0];
                location.Y      = disCoordinates[1];
                location.Z      = disCoordinates[2];
                espdu.Timestamp = DisTime.DisRelativeTimestamp;

                //Prepare output
                var dos = new DataOutputStream(Endian.Big);
                espdu.MarshalAutoLengthSet(dos);

                // Transmit broadcast messages
                SendMessages(dos.ConvertToBytes());
                Console.Write("Message sent with TimeStamp [{0}] Time Of[{1}]", espdu.Timestamp, espdu.Timestamp >> 1);

                //Thread.Sleep(1000);
                Console.Write("Hit Enter for Next PDU.  Ctrl-C to Exit");
                Console.ReadLine();
            }

            mcastSocket.Close();
        }