private void RightHandTracking() { if (viveHandRight != null) { this.transform.position = viveHandRight.position; this.transform.rotation = viveHandRight.rotation; if (rightHandDevice.GetPress(SteamVR_Controller.ButtonMask.Touchpad)) { gesture = GESTURE.POINT; } else if (rightHandDevice.GetPress(SteamVR_Controller.ButtonMask.Trigger)) { gesture = GESTURE.GRAB; } else { gesture = GESTURE.IDLE; } } else { Debug.Log("Looking for RightHand"); var go = GameObject.Find("RightHandPos"); if (go) { viveHandRight = go.transform; // Assigning ViveControllerGrab.leftHandAnimator to be this animator rightHandDevice = viveHandRight.parent.GetComponent<ViveControllerGrab>().device; } } }
private double updateCurrentAngle(GESTURE gesture , GestureDatabase gestureDatabase) { double diffAngle = 0.0; Skeleton lastSkeleton; AppropriateJointInfo lastAptJointInfo; Skeleton lastValidSkeleton; AppropriateJointInfo lastValidAptJointInfo; int indexOfLastValidGesture = (gesture == GESTURE.SWIPE_RIGHT) ? indexOfLastValidSwipeRightFrame : indexOfLastValidSwipeLeftFrame; // compare the current frame with the last valid swipe right frame gestureDatabase.getLastRecord(out lastSkeleton, out lastAptJointInfo); gestureDatabase.getRecord(indexOfLastValidGesture, out lastValidSkeleton, out lastValidAptJointInfo); HCI580_Geometry.Vector2D lastElbowShoulder = new HCI580_Geometry.Vector2D( (lastAptJointInfo.shoulderRightPos.X - lastAptJointInfo.elbowRightPos.X), (lastAptJointInfo.shoulderRightPos.Y - lastAptJointInfo.elbowRightPos.Y)); HCI580_Geometry.Vector2D lastElbowHand = new HCI580_Geometry.Vector2D( (lastAptJointInfo.handRightPos.X - lastAptJointInfo.elbowRightPos.X), (lastAptJointInfo.handRightPos.Y - lastAptJointInfo.elbowRightPos.Y)); HCI580_Geometry.Vector2D lastValidElbowShoulder = new HCI580_Geometry.Vector2D( (lastValidAptJointInfo.shoulderRightPos.X - lastValidAptJointInfo.elbowRightPos.X), (lastValidAptJointInfo.shoulderRightPos.Y - lastValidAptJointInfo.elbowRightPos.Y)); HCI580_Geometry.Vector2D lastValidElbowHand = new HCI580_Geometry.Vector2D( (lastValidAptJointInfo.handRightPos.X - lastValidAptJointInfo.elbowRightPos.X), (lastValidAptJointInfo.handRightPos.Y - lastValidAptJointInfo.elbowRightPos.Y)); diffAngle = lastElbowShoulder.angleTo(lastElbowHand) - lastValidElbowShoulder.angleTo(lastValidElbowHand); // Console.WriteLine("diff Angle " + diffAngle); diffAngle = (gesture == GESTURE.SWIPE_RIGHT) ? (diffAngle < 0 ? 0 : diffAngle) : (diffAngle > 0 ? 0 : diffAngle); // both the angles need to be added , as the diff angle will be negative for left swipes. return ((gesture == GESTURE.SWIPE_RIGHT) ? this.currentSwipeRightAngle + diffAngle : this.currentSwipeLeftAngle + diffAngle); }
/* * <reset param="gesture"> * Takes the type of gesture and resets their apt parameters * 1. resets the indexOfFirstValidGestureFrame , -1 is an invalid index * 2. the indexOfLastValidGestureFrame * 3. the Timers , current and start timer * 4. the Timespan * 5. the gestureStarted parameter to false * 6. the currentAngle parameter to 0. * </reset> */ private void reset(GESTURE gesture) { switch (gesture) { case GESTURE.SWIPE_RIGHT: { indexOfFirstValidSwipeRightFrame = -1; // reset the index indexOfLastValidSwipeRightFrame = -1 ; isSwipeRightGestureStarted = false; // gesture started false currentSwipeRightAngle = 0; // current angle 0 currentSwipeRightTime = new DateTime(1,1,1); // reset the time to some invalid Time startSwipeRightTime = new DateTime(1, 1, 1); ; swipeRightTimeSpan = new TimeSpan(currentSwipeRightTime.Ticks - startSwipeRightTime.Ticks); } break; case GESTURE.SWIPE_LEFT: // same as swipe right { indexOfFirstValidSwipeLeftFrame = -1 ; indexOfLastValidSwipeLeftFrame = -1; isSwipeLeftGestureStarted = false; currentSwipeLeftAngle = 0; currentSwipeLeftTime = new DateTime(1,1,1); startSwipeLeftTime = new DateTime(1,1,1); swipeLeftTimeSpan = new TimeSpan(); } break; } }
private bool isIntermediaryFrameValidGestureFrame(GESTURE gesture, GestureDatabase gestureDatabase) { Skeleton currentSkeleton; AppropriateJointInfo currentAptJointInfo; gestureDatabase.getLastRecord(out currentSkeleton, out currentAptJointInfo); switch (gesture) { case GESTURE.SWIPE_RIGHT: { HCI580_Geometry.Vector2D currentElbowShoulder = new HCI580_Geometry.Vector2D((currentAptJointInfo.shoulderRightPos.X - currentAptJointInfo.elbowRightPos.X), (currentAptJointInfo.shoulderRightPos.Y - currentAptJointInfo.elbowRightPos.Y)); HCI580_Geometry.Vector2D currentElbowHand = new HCI580_Geometry.Vector2D((currentAptJointInfo.handRightPos.X - currentAptJointInfo.elbowRightPos.X), (currentAptJointInfo.handRightPos.Y - currentAptJointInfo.elbowRightPos.Y)); // check if hand Y > elbow Y , angle between elbowShoulder and elbowHand negative if (currentAptJointInfo.handRightPos.Y > currentAptJointInfo.elbowRightPos.Y && currentElbowShoulder.cpsign(currentElbowHand) < 0) { return true; } } break; case GESTURE.SWIPE_LEFT: { HCI580_Geometry.Vector2D currentElbowShoulder = new HCI580_Geometry.Vector2D((currentAptJointInfo.shoulderRightPos.X - currentAptJointInfo.elbowRightPos.X), (currentAptJointInfo.shoulderRightPos.Y - currentAptJointInfo.elbowRightPos.Y)); HCI580_Geometry.Vector2D currentElbowHand = new HCI580_Geometry.Vector2D((currentAptJointInfo.handRightPos.X - currentAptJointInfo.elbowRightPos.X), (currentAptJointInfo.handRightPos.Y - currentAptJointInfo.elbowRightPos.Y)); // check if hand Y > elbow Y , angle between elbowShoulder and elbowHand negative if (currentAptJointInfo.handRightPos.Y > currentAptJointInfo.elbowRightPos.Y && currentElbowShoulder.cpsign(currentElbowHand) < 0) { return true; } } break; } return false; }
private bool isIntermediaryCumulativeAngularMotionValid(GESTURE gesture, GestureDatabase gestureDatabase) { Skeleton currentSkeleton; AppropriateJointInfo currentAptInfo; Skeleton firstValidSkeleton; AppropriateJointInfo firstValidAptInfo; int indexOfFirstValidMotion; indexOfFirstValidMotion = (gesture == GESTURE.SWIPE_RIGHT) ? indexOfFirstValidSwipeRightFrame : indexOfFirstValidSwipeLeftFrame; gestureDatabase.getLastRecord(out currentSkeleton, out currentAptInfo); gestureDatabase.getRecord(indexOfFirstValidMotion,out firstValidSkeleton , out firstValidAptInfo); HCI580_Geometry.Vector2D currentElbowShoulder = new HCI580_Geometry.Vector2D((currentAptInfo.shoulderRightPos.X - currentAptInfo.elbowRightPos.X), (currentAptInfo.shoulderRightPos.Y - currentAptInfo.elbowRightPos.Y)); HCI580_Geometry.Vector2D currentElbowHand = new HCI580_Geometry.Vector2D((currentAptInfo.handRightPos.X - currentAptInfo.elbowRightPos.X), (currentAptInfo.handRightPos.Y - currentAptInfo.elbowRightPos.Y)); HCI580_Geometry.Vector2D firstElbowShoulder = new HCI580_Geometry.Vector2D((firstValidAptInfo.shoulderRightPos.X - firstValidAptInfo.elbowRightPos.X), (firstValidAptInfo.shoulderRightPos.Y - firstValidAptInfo.elbowRightPos.Y)); HCI580_Geometry.Vector2D firstElbowHand = new HCI580_Geometry.Vector2D((firstValidAptInfo.handRightPos.X - firstValidAptInfo.elbowRightPos.X), (firstValidAptInfo.handRightPos.Y - firstValidAptInfo.elbowRightPos.Y)); double diffAngle = currentElbowShoulder.angleTo(currentElbowHand) - firstElbowShoulder.angleTo(firstElbowHand); // Console.WriteLine("cumulative diff " + diffAngle); return (gesture == GESTURE.SWIPE_RIGHT) ? (diffAngle > 0 ? true : false) : (diffAngle < 0 ? true : false); }
// check for too fast and opposite movements private bool isHandAngularChangeWithInBounds(GESTURE gesture , GestureDatabase gestureDatabase) { Skeleton lastSkeleton; AppropriateJointInfo lastAptJointInfo; Skeleton lastValidSkeleton; AppropriateJointInfo lastValidAptJointInfo; int indexOfLastValidGesture = (gesture == GESTURE.SWIPE_RIGHT) ? indexOfLastValidSwipeRightFrame : indexOfLastValidSwipeLeftFrame; if (indexOfLastValidGesture < 0) { return true; } // compare the current frame with the last valid swipe right frame gestureDatabase.getLastRecord(out lastSkeleton , out lastAptJointInfo); gestureDatabase.getRecord(indexOfLastValidGesture , out lastValidSkeleton , out lastValidAptJointInfo); HCI580_Geometry.Vector2D lastElbowShoulder = new HCI580_Geometry.Vector2D( (lastAptJointInfo.shoulderRightPos.X - lastAptJointInfo.elbowRightPos.X) , (lastAptJointInfo.shoulderRightPos.Y - lastAptJointInfo.elbowRightPos.Y)); HCI580_Geometry.Vector2D lastElbowHand = new HCI580_Geometry.Vector2D( (lastAptJointInfo.handRightPos.X - lastAptJointInfo.elbowRightPos.X), (lastAptJointInfo.handRightPos.Y - lastAptJointInfo.elbowRightPos.Y)); HCI580_Geometry.Vector2D lastValidElbowShoulder = new HCI580_Geometry.Vector2D( (lastValidAptJointInfo.shoulderRightPos.X - lastValidAptJointInfo.elbowRightPos.X), (lastValidAptJointInfo.shoulderRightPos.Y - lastValidAptJointInfo.elbowRightPos.Y)); HCI580_Geometry.Vector2D lastValidElbowHand = new HCI580_Geometry.Vector2D( (lastValidAptJointInfo.handRightPos.X - lastValidAptJointInfo.elbowRightPos.X), (lastValidAptJointInfo.handRightPos.Y - lastValidAptJointInfo.elbowRightPos.Y)); if (Math.Abs(lastElbowShoulder.angleTo(lastElbowHand) - lastValidElbowShoulder.angleTo(lastValidElbowHand)) > MAX_RATE_OF_ANGLE_CHANGE) { // Console.WriteLine("angle change too fast"); return false; // moving too fast } // Console.WriteLine("last frame Angle " + lastElbowShoulder.angleTo(lastElbowHand) + " last Valid Frame Angle " + lastValidElbowShoulder.angleTo(lastValidElbowHand) + // " difference " + (lastElbowShoulder.angleTo(lastElbowHand) - lastValidElbowShoulder.angleTo(lastValidElbowHand))); switch (gesture) { case GESTURE.SWIPE_RIGHT: { /* * we are getting only the magnitude of the angles here, so swipe right opposite is moving from larger angle to smaller angle */ if (lastElbowShoulder.angleTo(lastElbowHand) - lastValidElbowShoulder.angleTo(lastValidElbowHand) <= -THRESHOLD_OPPOSITE_ANGLE) { // Console.WriteLine("Gesture : SWIPE RIGHT , OPPOSITE MOTION ...."); return false; } } break; case GESTURE.SWIPE_LEFT: { if (lastElbowShoulder.angleTo(lastElbowHand) - lastValidElbowShoulder.angleTo(lastValidElbowHand) >= THRESHOLD_OPPOSITE_ANGLE) { // Console.WriteLine("Gesture : SWIPE LEFT , OPPOSITE MOTION ...."); return false; } } break; } return true; }
private bool isFirstValidGestureFrame(GESTURE gesture , GestureDatabase gestureDatabase) { Skeleton skeleton; AppropriateJointInfo aptJointInfo; gestureDatabase.getLastRecord(out skeleton ,out aptJointInfo); switch(gesture) { case GESTURE.SWIPE_RIGHT: { HCI580_Geometry.Vector2D elbowShoulder = new HCI580_Geometry.Vector2D((aptJointInfo.shoulderRightPos.X - aptJointInfo.elbowRightPos.X), (aptJointInfo.shoulderRightPos.Y - aptJointInfo.elbowRightPos.Y)); HCI580_Geometry.Vector2D elbowHand = new HCI580_Geometry.Vector2D((aptJointInfo.handRightPos.X - aptJointInfo.elbowRightPos.X), (aptJointInfo.handRightPos.Y - aptJointInfo.elbowRightPos.Y)); // check if hand Y > elbow Y , hand X < elbow X and angle between elbowShoulder and elbowHand negative if(aptJointInfo.handRightPos.Y > aptJointInfo.elbowRightPos.Y && aptJointInfo.handRightPos.X < aptJointInfo.elbowRightPos.X && elbowShoulder.cpsign(elbowHand) < 0) { return true; } } break; case GESTURE.SWIPE_LEFT: { HCI580_Geometry.Vector2D elbowShoulder = new HCI580_Geometry.Vector2D((aptJointInfo.shoulderRightPos.X - aptJointInfo.elbowRightPos.X), (aptJointInfo.shoulderRightPos.Y - aptJointInfo.elbowRightPos.Y)); HCI580_Geometry.Vector2D elbowHand = new HCI580_Geometry.Vector2D((aptJointInfo.handRightPos.X - aptJointInfo.elbowRightPos.X), (aptJointInfo.handRightPos.Y - aptJointInfo.elbowRightPos.Y)); // check if hand Y > elbow Y , hand X < elbow X and angle between elbowShoulder and elbowHand negative if(aptJointInfo.handRightPos.Y > aptJointInfo.elbowRightPos.Y && aptJointInfo.handRightPos.X > aptJointInfo.elbowRightPos.X && elbowShoulder.cpsign(elbowHand) < 0) { return true; } } break; } return false; }
/* * doc: The function takes the first valid gesture frame ,depending upon what the gesture is, and currently added * and compares the shouldercenter position , which is representative of the body , accross these frames to * check for out of range body movements */ private bool isBodyLinearDeflectionWithInBounds(GESTURE gesture , GestureDatabase gestureDatabase) { Skeleton firstValidSkeleton; AppropriateJointInfo firstValidAptInfo; Skeleton currentSkeleton; AppropriateJointInfo currentAptInfo; int indexOfFirstValidGesture = (gesture == GESTURE.SWIPE_LEFT) ? indexOfFirstValidSwipeLeftFrame : indexOfFirstValidSwipeRightFrame; if(indexOfFirstValidGesture < 0) return true; gestureDatabase.getLastRecord(out currentSkeleton, out currentAptInfo); // last added frame gestureDatabase.getRecord(indexOfFirstValidGesture , out firstValidSkeleton , out firstValidAptInfo); // first valid frame // difference between joint positions of shouldercenter in currently added and first valid gesture frame return isJointDelectionValid(currentAptInfo.shoulderCenterPos, firstValidAptInfo.shoulderCenterPos); }
private void reset(GESTURE gesture) { switch (gesture) { case GESTURE.CONST_LEFT: accelerationConstLeftHand = 1; indexOfFirstValidConstLeftGestureFrame = -1; indexOfLastValidConstLeftGestureFrame = -1; isConstLeftHandGestureStarted = false; startTimerConstLeftHand = new DateTime(1, 1, 1); currentTimerConstLeftHand = new DateTime(1, 1, 1); timeElapsedConstLeftHand = new TimeSpan(currentTimerConstLeftHand.Ticks - startTimerConstLeftHand.Ticks); break; case GESTURE.CONST_RIGHT: accelerationConstRightHand = 1; indexOfFirstValidConstRightGestureFrame = -1; indexOfLastValidConstRightGestureFrame = -1; isConstRightHandGestureStarted = false; startTimerConstRightHand = new DateTime(1, 1, 1); currentTimerConstRightHand = new DateTime(1, 1, 1); timeElapsedConstRightHand = new TimeSpan(currentTimerConstRightHand.Ticks - startTimerConstRightHand.Ticks); break; } }
private bool isIntermediaryCumulativeAngleDeflectionValid(GESTURE gesture , GestureDatabase gestureDatabase) { Skeleton firstValidSkeleton; AppropriateJointInfo firstValidAptJointInfo; Skeleton currentSkeleton; AppropriateJointInfo currentAptJointInfo; int indexOfFirstValidGestureFrame = (gesture == GESTURE.CONST_LEFT) ? indexOfFirstValidConstLeftGestureFrame : indexOfFirstValidConstRightGestureFrame; gestureDatabase.getLastRecord(out currentSkeleton , out currentAptJointInfo); gestureDatabase.getRecord(indexOfFirstValidGestureFrame, out firstValidSkeleton , out firstValidAptJointInfo); Vector2D currentElbowShoulder = new Vector2D((currentAptJointInfo.shoulderRightPos.X - currentAptJointInfo.elbowRightPos.X), (currentAptJointInfo.shoulderRightPos.Y - currentAptJointInfo.elbowRightPos.Y)); Vector2D currentElbowHand = new Vector2D((currentAptJointInfo.handRightPos.X - currentAptJointInfo.elbowRightPos.X), (currentAptJointInfo.handRightPos.Y - currentAptJointInfo.elbowRightPos.Y)); Vector2D firstElbowShoulder = new Vector2D((firstValidAptJointInfo.shoulderRightPos.X - firstValidAptJointInfo.elbowRightPos.X), (firstValidAptJointInfo.shoulderRightPos.Y - firstValidAptJointInfo.elbowRightPos.Y)); Vector2D firstElbowHand = new Vector2D((firstValidAptJointInfo.handRightPos.X - firstValidAptJointInfo.elbowRightPos.X), (firstValidAptJointInfo.handRightPos.Y - firstValidAptJointInfo.elbowRightPos.Y)); double angularDeflection = Math.Abs(currentElbowShoulder.angleTo(currentElbowHand) - firstElbowShoulder.angleTo(firstElbowHand)); if (angularDeflection <= THRESHOLD_ANGLE_DEFLECTION) return true; return false; }
private bool isIntermediaryFrameValidGestureFrame(GESTURE gesture, GestureDatabase gestureDatabase) { Skeleton currentSkeleton; AppropriateJointInfo currentAptJointInfo; gestureDatabase.getLastRecord(out currentSkeleton, out currentAptJointInfo); Vector2D currentElbowShoulder = new Vector2D((currentAptJointInfo.shoulderRightPos.X - currentAptJointInfo.elbowRightPos.X), (currentAptJointInfo.shoulderRightPos.Y - currentAptJointInfo.elbowRightPos.Y)); Vector2D currentElbowHand = new Vector2D((currentAptJointInfo.handRightPos.X - currentAptJointInfo.elbowRightPos.X), (currentAptJointInfo.handRightPos.Y - currentAptJointInfo.elbowRightPos.Y)); double angle = currentElbowShoulder.angleTo(currentElbowHand); switch (gesture) { case GESTURE.CONST_LEFT: { if (currentAptJointInfo.shoulderCenterPos.Y > currentAptJointInfo.handRightPos.Y && currentAptJointInfo.shoulderCenterPos.Y > currentAptJointInfo.elbowRightPos.Y && currentAptJointInfo.handRightPos.X < currentAptJointInfo.elbowRightPos.X && currentElbowShoulder.cpsign(currentElbowHand) > 0 && angle >= CONST_LEFT_GESTURE_MIN_THETA - THRESHOLD_ANGLE_DEFLECTION && angle <= CONST_LEFT_GESTURE_MAX_THETA + THRESHOLD_ANGLE_DEFLECTION) { return true; } } break; case GESTURE.CONST_RIGHT: { if ( currentAptJointInfo.shoulderCenterPos.Y > currentAptJointInfo.elbowRightPos.Y && currentAptJointInfo.elbowRightPos.Y > currentAptJointInfo.handRightPos.Y && currentAptJointInfo.handRightPos.X > currentAptJointInfo.elbowRightPos.X && currentElbowShoulder.cpsign(currentElbowHand) < 0 && angle >= CONST_RIGHT_GESTURE_MIN_THETA - THRESHOLD_ANGLE_DEFLECTION && angle <= CONST_RIGHT_GESTURE_MAX_THETA + THRESHOLD_ANGLE_DEFLECTION) { return true; } } break; } return false; }
private bool isHandAngleDeflectionWithInBounds(GESTURE gesture, GestureDatabase gestureDatabase) { Skeleton skeleton; AppropriateJointInfo aptJoint; switch (gesture) { case GESTURE.CONST_LEFT: { gestureDatabase.getLastRecord(out skeleton, out aptJoint); Vector2D elbowShoulder = new Vector2D(aptJoint.shoulderRightPos.X - aptJoint.elbowRightPos.X, aptJoint.shoulderRightPos.Y - aptJoint.elbowRightPos.Y); Vector2D elbowHand = new Vector2D(aptJoint.handRightPos.X - aptJoint.elbowRightPos.X, aptJoint.handRightPos.Y - aptJoint.elbowRightPos.Y); if (elbowShoulder.cpsign(elbowHand) > 0 && elbowShoulder.angleTo(elbowHand) >= CONST_LEFT_GESTURE_MIN_THETA - THRESHOLD_ANGLE_DEFLECTION && elbowShoulder.angleTo(elbowHand) <= CONST_LEFT_GESTURE_MAX_THETA + THRESHOLD_ANGLE_DEFLECTION) { return true; } break; } case GESTURE.CONST_RIGHT: { gestureDatabase.getLastRecord(out skeleton, out aptJoint); Vector2D elbowShoulder = new Vector2D(aptJoint.shoulderRightPos.X - aptJoint.elbowRightPos.X, aptJoint.shoulderRightPos.Y - aptJoint.elbowRightPos.Y); Vector2D elbowHand = new Vector2D(aptJoint.handRightPos.X - aptJoint.elbowRightPos.X, aptJoint.handRightPos.Y - aptJoint.elbowRightPos.Y); if (elbowShoulder.cpsign(elbowHand) < 0 && elbowShoulder.angleTo(elbowHand) >= CONST_RIGHT_GESTURE_MIN_THETA - THRESHOLD_ANGLE_DEFLECTION && elbowShoulder.angleTo(elbowHand) <= CONST_RIGHT_GESTURE_MAX_THETA + THRESHOLD_ANGLE_DEFLECTION) { return true; } break; } } return false; }
private bool isFirstValidGestureFrame(GESTURE gesture, GestureDatabase gestureDatabase) { return isIntermediaryFrameValidGestureFrame(gesture , gestureDatabase); }
private bool isBodyLinearDeflectionsWithInBounds(GESTURE gesture , GestureDatabase gestureDatabase) { if(gesture == GESTURE.CONST_LEFT) if (indexOfFirstValidConstLeftGestureFrame < 0) return true; // this is the start of the new Gesture, first frame of the new gesture if (gesture == GESTURE.CONST_RIGHT) if (indexOfFirstValidConstRightGestureFrame < 0) return true; // this is the start of the new Gesture, first frame of the new gesture int indexOfFirstValidGestureFrame = (gesture == GESTURE.CONST_LEFT) ? indexOfFirstValidConstLeftGestureFrame : indexOfFirstValidConstRightGestureFrame; Skeleton lastValidSkeleton; AppropriateJointInfo lastAptInfo; Skeleton firstValidSkeleton; AppropriateJointInfo firstAptInfo; gestureDatabase.getRecord(gestureDatabase.getTotalSize() - 1, out lastValidSkeleton, out lastAptInfo); gestureDatabase.getRecord(indexOfFirstValidGestureFrame, out firstValidSkeleton, out firstAptInfo); return (isPointWithInThreshold(lastAptInfo.shoulderCenterPos, firstAptInfo.shoulderCenterPos)); }
private void timer_Tick(object sender, EventArgs e) { int data; txtBytesToRead.Text = bytesToRead.ToString(); txtQueueCount.Text = dataQueue.Count.ToString(); Update_btnConnect(); // check if queue is empty if (dataQueue.Count > 0 && serialPort.IsOpen) { dataQueue.TryDequeue(out data); // read a packet of data // first find 255 while (data != 255) { dataQueue.TryDequeue(out data); } // retrieve x, y, z data, show in textbox and add to list int avgCount; // how many to average count++; // for x-axis of plot try { avgCount = int.Parse(txtAvgN.Text); } catch (System.FormatException) { avgCount = 0; } int stddevCount; try { stddevCount = int.Parse(txtStddevN.Text); } catch (System.FormatException) { stddevCount = 0; } for (READ_ACC curr = READ_ACC.X; curr != READ_ACC.OVER; curr++) { dataQueue.TryDequeue(out data); switch (curr) { case READ_ACC.X: txtX.Text = data.ToString(); while (x_acc.Count > stddevCount) { x_acc.RemoveAt(0); } x_acc.Add(data); break; case READ_ACC.Y: txtY.Text = data.ToString(); while (y_acc.Count > stddevCount) { y_acc.RemoveAt(0); } y_acc.Add(data); break; case READ_ACC.Z: txtZ.Text = data.ToString(); while (z_acc.Count > stddevCount) { z_acc.RemoveAt(0); } z_acc.Add(data); break; } } // retreive last x,y,z data int xData = x_acc[x_acc.Count - 1]; int yData = y_acc[y_acc.Count - 1]; int zData = z_acc[z_acc.Count - 1]; if (!bomberGame.GetGameStatus()) { if (grav.Count > avgCount) { grav.RemoveAt(0); } grav.Add(Calculate_grav(xData, yData, zData)); txtGAvg.Text = grav.Average().ToString(); // read threshold values int xThreshold, yThreshold, zThreshold, yThresholdNeg, zThresholdNeg; try { xThreshold = int.Parse(txtXThreshold.Text); yThreshold = int.Parse(txtYThreshold.Text); zThreshold = int.Parse(txtZThreshold.Text); yThresholdNeg = int.Parse(txtYNeg.Text); zThresholdNeg = int.Parse(txtZNeg.Text); } catch (System.FormatException) { xThreshold = 220; yThreshold = 220; zThreshold = 220; yThresholdNeg = 100; zThresholdNeg = 100; } // start gesture reading if ((xData > xThreshold || zData > zThreshold || zData < zThresholdNeg) && gestureState == GESTURE.IDLE) { gestureState = GESTURE.START; } Update_orientation(xData, yData, zData); Calculate_average(avgCount); Calculate_std(stddevCount); Update_gesture(xData, yData, zData, xThreshold, yThreshold, zThreshold, yThresholdNeg, zThresholdNeg); Update_plot(xData, yData, zData, count); } while (dataQueue.Count > itemLimit) { dataQueue.TryDequeue(out int trash); } if (bomberGame.GetGameStatus()) { bomberGame.UpdatePosition(ref bomberGame.bomber, xData, yData, zData); // update bomberman position } else if (btnStart.Text != "Start") { btnStart.Text = "Start"; } } }
private void Update_gesture(int xData, int yData, int zData, int xThreshold, int yThreshold, int zThreshold, int yThresholdNeg, int zThresholdNeg) { switch (gestureState) { case (GESTURE.START): gestureWatch.Start(); txtLED.BackColor = Color.Green; gestureState = GESTURE.ACQUIRE; break; case (GESTURE.ACQUIRE): if (gestureWatch.ElapsedMilliseconds > gestureTimeout) { gestureState = GESTURE.OVER; Console.WriteLine("over 2s, restart"); } else if (gestureWatch.ElapsedMilliseconds > displayTimeout) { txtGesture.Clear(); //Console.WriteLine("over 1s, display clear"); } if (readGesture.Length <= 3) { if (xData > xThreshold && !readGesture.Contains("X") && !readGesture.Contains("Z") && !readGesture.Contains("z")) { readGesture += "X"; txtGesture.Text = readGesture; } if (zData < zThresholdNeg && !readGesture.Contains("z") && !readGesture.Contains("Z") && !readGesture.Contains("X") && !readGesture.Contains("Y")) { readGesture += "z"; txtGesture.Text = readGesture; } if (zData > zThreshold && !readGesture.EndsWith("Z") && !readGesture.EndsWith("z") && !readGesture.EndsWith("X") && !readGesture.EndsWith("Y") && !readGesture.EndsWith("y")) { readGesture += "Z"; txtGesture.Text = readGesture; } if (yData > yThreshold && !readGesture.Contains("Y") && (readGesture.Contains('X') || readGesture.Contains('Z'))) { readGesture += "Y"; txtGesture.Text = readGesture; } if (yData < yThresholdNeg && !readGesture.EndsWith("y") && readGesture.Contains("ZY")) { readGesture += "y"; txtGesture.Text = readGesture; } } switch (readGesture) { case "z": txtGesture.Text = "Free fall"; break; case "XY": txtGesture.Text = "Frisbee throw"; break; case "ZYy": txtGesture.Text = "Wave"; break; } break; case (GESTURE.OVER): txtGesture.Clear(); txtLED.BackColor = Color.Red; gestureWatch.Reset(); gestureState = GESTURE.IDLE; readGesture = ""; break; } }