/// <summary>
        /// Handler for TickEvent of EventLoop. Will update keyframes of any animation and synchronize it with
        /// clients by setting the respective entity attribute
        /// </summary>
        /// <param name="sender">Sender of the event (EventLoop)</param>
        /// <param name="e">TickEvent args</param>
        private void HandleEventTick(Object sender, TickEventArgs e)
        {
            double frameDuration = e.TimeStamp.Subtract(LastTick).TotalMilliseconds;
            LastTick = e.TimeStamp;
            lock (RunningAnimationsForEntities)
            {
                foreach (KeyValuePair<Guid, Dictionary<string, KeyframeAnimation>>
                    animatedEntity in RunningAnimationsForEntities)
                {
                    string animationKeyframes = "";
                    foreach (KeyValuePair<string, KeyframeAnimation> runningAnimation in animatedEntity.Value)
                    {
                        float newKey = PerformTickForEntityAnimation(animatedEntity.Key,
                                                                     runningAnimation.Value,
                                                                     frameDuration);

                        animationKeyframes += runningAnimation.Key + ":" + newKey + ";";
                    }
                    Entity entity = World.Instance.FindEntity(animatedEntity.Key);
                    entity["animation"]["animationKeyframes"].Suggest(animationKeyframes);
                }
            }
            FinalizeFinishedAnimations();
        }
        /// <summary>
        /// Handles a TickFired Evenet of EventLoop. Performs position and orientation updates for all ongoing motions and rotations
        /// </summary>
        /// <param name="sender">Sender of tick event args (EventLoop)</param>
        /// <param name="e">TickEventArgs</param>
        private void HandleEventTick(Object sender, TickEventArgs e)
        {
            lock (ongoingMotion)
            {
                foreach (Entity entity in ongoingMotion)
                {
                    UpdateMotion(entity);
                }
            }

            lock (ongoingSpin)
            {
                foreach (Entity entity in ongoingSpin)
                    UpdateSpin(entity);
            }
        }
        //Stopwatch sw = Stopwatch.StartNew();
        private void HandleEventTick(Object sender, TickEventArgs e)
        {
            //Console.WriteLine(String.Format("Slept for {0}ms.", sw.ElapsedMilliseconds));
            //sw.Restart();

            entRWL.AcquireReaderLock (-1);
            try {
                foreach (KeyValuePair<Entity, NaoConnection> ec in entities)
                {
                    var entity = ec.Key;
                    var connection = ec.Value;
                    lock (connection)
                    {
                        foreach (KeyValuePair<string, float> kv in connection.jointState)
                            entity["nao_posture"][kv.Key].Suggest(kv.Value);

                        entity["nao_posture"]["x"].Suggest((float)connection.positionState.x);
                        entity["nao_posture"]["y"].Suggest((float)connection.positionState.y);
                        entity["nao_posture"]["z"].Suggest((float)connection.positionState.z);
                        entity["nao_posture"]["wx"].Suggest((float)connection.positionState.wx);
                        entity["nao_posture"]["wy"].Suggest((float)connection.positionState.wy);
                        entity["nao_posture"]["wz"].Suggest((float)connection.positionState.wz);

                        /*
                        entity["location"]["position"].Suggest(
                            new Vector((float)connection.positionState.x, (float)connection.positionState.z, (float)-connection.positionState.y));

                        Quat zang = FIVES.Math.QuaternionFromAxisAngle(new Vector(0,1,0),
                            connection.positionState.wz);
                        Quat xang = FIVES.Math.QuaternionFromAxisAngle(new Vector(1,0,0),
                            connection.positionState.wx);
                        Quat yang = FIVES.Math.QuaternionFromAxisAngle(new Vector(0,0,1),
                            connection.positionState.wy);
                        Quat res = FIVES.Math.MultiplyQuaternions(xang, FIVES.Math.MultiplyQuaternions(yang,zang));

                        entity["location"]["orientation"].Suggest(res);
                        */
                        connection.jointState.Clear();
                    }
                }
            } finally {
                entRWL.ReleaseReaderLock ();
                //Console.WriteLine(String.Format("Tick handler took {0}ms", sw.ElapsedMilliseconds));
                //sw.Restart();
            }
            //			doUpdate.Set();
        }
Пример #4
0
        private void HandleEventTick(Object sender, TickEventArgs e)
        {
            //Triggered within fixed time
            lock (RunningAnimationsForEntities)
            {
                foreach (KeyValuePair<string, AnimationInfo>
                    animatedEntity in RunningAnimationsForEntities)
                {
                    loopCounter++;
                    Entity entity = World.Instance.FindEntity(animatedEntity.Key);

                    //check if this is paused entity
                    int pausedFrame = 0;
                    if(this.isPausedEntity.Equals(entity.Guid.ToString("D")))
                    {
                        //we should not send the action string again
                        //we need to take the paused frame
                        pausedFrame = this.PausedFrameForEntities[this.isPausedEntity];
                    }
                    else
                    {
                        //otherwise we send it at the very start
                        string actionString = loopCounter.ToString() + ":" + animatedEntity.Value.bvhActionString;
                        entity["BVHAnimation"]["bvhaction"].Suggest(actionString);

                        //FIXME: we need to wait for answer, but how?
                        System.Threading.Thread.Sleep(500);
                    }

                    int frameNum = 0;
                    foreach (string frame in animatedEntity.Value.bvhAnimationString)
                    {
                        frameNum ++;

                        //firstly check if this is paused entity
                        if (pausedFrame > 0)
                        {
                            if(frameNum < pausedFrame)
                                continue;
                            else
                            {
                                //clear the paused information
                                PausedAnimationsForEntities.Remove(this.isPausedEntity);
                                this.isPaused = false;
                                PausedFrameForEntities.Remove(this.isPausedEntity);
                                this.isPausedEntity = "";
                            }
                        }

                        //Not paused
                        string sendString = frameNum.ToString() + frame;
                        //Console.WriteLine(sendString);
                        entity["BVHAnimation"]["bvhframe"].Suggest(sendString);

                        //FIXME: replace this sleep of pose-to-pose interpolation
                        System.Threading.Thread.Sleep(animatedEntity.Value.frameTime);

                        //if receive the signal of stop, just break
                        if (this.isStoppedEntity.Equals(entity.Guid.ToString("D")))
                        {
                            break;
                        }

                        //if receive the signal of pause, remember the frame number and break
                        if(this.isPausedEntity.Equals(entity.Guid.ToString("D")))
                        {
                            this.PausedFrameForEntities[this.isPausedEntity] = frameNum;

                            break;
                        }

                    }
                }
            }
        }