示例#1
0
 void DepthFrameReady(object sender, DepthImageFrameReadyEventArgs e)
 {
     if (frameCount % 3 == 0) // increase the number after the mod to increase speed. in its current setup it skips 2 out of 3 frames and only processes the 3rd.
     {
         DepthImageFrame imageFrame = e.OpenDepthImageFrame();
         if (imageFrame != null)                                        // makes sure a frame exists.
         {
             short[] pixelData = new short[imageFrame.PixelDataLength]; // Creates an array the size of the image frame pixel length
             imageFrame.CopyPixelDataTo(pixelData);                     // copies pixel data to the pixel data array.
             int temp = 0;                                              // initalizes depth.
             int i    = 0;                                              // intializes the count.
             for (int y = 0; y < 480; y += pointDen)                    // iterates through each row.
             {
                 for (int x = 0; x < 640; x += pointDen)                //iterates through each column in the row.
                 {
                     temp = ((ushort)pixelData[x + y * 640]) >> 4;
                     ((TranslateTransform3D)points[i].Transform).OffsetZ = temp; // sets the triangles z offset.
                     if (temp < 400 && x >= 200 && x <= 400)
                     {
                         points[i].Material = new DiffuseMaterial(new SolidColorBrush(Colors.Red)); // throws a read warning.
                         rotate             = -.2f;
                         speed    = .0f;
                         REDALERT = true;
                     }
                     else if (temp < 500 && x >= 200 && x <= 400 && !REDALERT)
                     {
                         points[i].Material = new DiffuseMaterial(new SolidColorBrush(Colors.Yellow)); // sets it to be yellow when it is in a cetain range.
                         objectDetected     = true;
                         av += x;
                         count++;
                         // 3 triagles that are yellow, what x value? x  value is on left side, rotate right.
                     }
                     else
                     {
                         points[i].Material = new DiffuseMaterial(new SolidColorBrush(Colors.Blue)); // paints everyhting else blue.
                     }
                     i++;
                 }
             }
             if (objectDetected)
             {
                 av = av / count;
                 av = av - 320;
                 if (av >= 0 && count > 5)
                 {
                     rotate = -.2f;
                     speed  = .5f;
                 }
                 else if (count > 5)
                 {
                     rotate = .2f;
                     speed  = .5f;
                 }
             }
             if (pathChecked == true)
             {
                 if (mp == null)
                 {
                     mp = new MotionProxy(ipBox.Text, 9559);
                 }
                 if (mp != null)
                 {
                     mp.setWalkTargetVelocity(speed, 0, rotate, .5f);
                 }
             }
             else if (pathChecked == false)
             {
                 if (mp == null)
                 {
                     mp = new MotionProxy(ipBox.Text, 9559);
                 }
                 if (mp != null)
                 {
                     mp.stopMove();
                 }
             }
         }
     }
     speed          = .6f;
     rotate         = 0;
     av             = 0;
     count          = 0;
     objectDetected = false;
     REDALERT       = false;
     pathChecked    = false;
     frameCount++;
 }