private static void HandleXboxInputPanTilt(MainWindow mainWindow, PanTiltController panTilt, FiringController firingController, XboxController handHeldController) { try { var convertedXboxInputX = ConvertXInputCoordinateToVelocity(handHeldController.LeftThumbStick.X); var convertedXboxInputY = ConvertXInputCoordinateToVelocity(handHeldController.LeftThumbStick.Y); Console.WriteLine("X Value Converted - " + convertedXboxInputX); //Pan Up & Pan Down for Manual Tracking if (convertedXboxInputY <= PAN_TILT_SPEED_LIMIT && !s_PanTiltTooFarDown && mainWindow.TrackingMode == TrackingMode.MANUAL) { s_PanTiltTooFarUp = false; mainWindow.CannonYVelocity = convertedXboxInputY; panTilt.PanY(mainWindow.CannonYVelocity); } if (convertedXboxInputY >= -1 * PAN_TILT_SPEED_LIMIT && !s_PanTiltTooFarUp && mainWindow.TrackingMode == TrackingMode.MANUAL) { mainWindow.CannonYVelocity = convertedXboxInputY; //set too far to false. if its stil too far the next key event handler will set to true s_PanTiltTooFarDown = false; panTilt.PanY(mainWindow.CannonYVelocity); } if (convertedXboxInputY <= PAN_TILT_SPEED_LIMIT && !s_PanTiltTooFarDown && mainWindow.TrackingMode == TrackingMode.AUDIBLE) { s_PanTiltTooFarUp = false; mainWindow.CannonYVelocity = convertedXboxInputY; panTilt.PanY(mainWindow.CannonYVelocity); } if (convertedXboxInputY >= -1 * PAN_TILT_SPEED_LIMIT && !s_PanTiltTooFarUp && mainWindow.TrackingMode == TrackingMode.AUDIBLE) { mainWindow.CannonYVelocity = convertedXboxInputY; //set too far to false. if its stil too far the next key event handler will set to true s_PanTiltTooFarDown = false; panTilt.PanY(mainWindow.CannonYVelocity); } if (mainWindow.CannonXVelocity >= -1 * PAN_TILT_SPEED_LIMIT && !s_PanTiltTooFarRight && mainWindow.TrackingMode == TrackingMode.MANUAL) { mainWindow.CannonXVelocity = convertedXboxInputX; //set too far to false. if its stil too far the next key event handler will set to true s_PanTiltTooFarLeft = false; panTilt.PanX(mainWindow.CannonXVelocity); } if (mainWindow.CannonXVelocity <= PAN_TILT_SPEED_LIMIT && !s_PanTiltTooFarLeft && mainWindow.TrackingMode == TrackingMode.MANUAL) { mainWindow.CannonXVelocity = convertedXboxInputX; //set too far to false. if its stil too far the next key event handler will set to true s_PanTiltTooFarRight = false; panTilt.PanX(mainWindow.CannonXVelocity); } } //anything goes wrong, stop pantilt and crash the app catch (Exception ex) { panTilt.Disengage(); throw ex; } }
/// <summary> /// Returns the singleton instance. We will always have only 1 pan/tilt controller /// </summary> /// <returns></returns> public static PanTiltController GetOrCreatePanTiltController() { if (s_Singleton == null) { s_Singleton = new PanTiltController(); } return s_Singleton; }
/// <summary> /// Returns the singleton instance. We will always have only 1 pan/tilt controller /// </summary> /// <returns></returns> public static PanTiltController GetOrCreatePanTiltController() { if (s_Singleton == null) { s_Singleton = new PanTiltController(); } return(s_Singleton); }
private static void HandlePanTilt(MainWindow mainWindow, PanTiltController panTilt, FiringController firingController, Key key) { try { //Pan Up if (key == System.Windows.Input.Key.Down && !s_PanTiltTooFarDown && mainWindow.TrackingMode == TrackingMode.MANUAL) { if (mainWindow.CannonYVelocity <= PAN_TILT_SPEED_LIMIT) { mainWindow.CannonYVelocity += 20; } //set too far to false. if its stil too far the next key event handler will set to true s_PanTiltTooFarUp = false; panTilt.PanY(mainWindow.CannonYVelocity); } else if (key == System.Windows.Input.Key.Up && !s_PanTiltTooFarUp && mainWindow.TrackingMode == TrackingMode.MANUAL) { if (mainWindow.CannonYVelocity >= -1 * PAN_TILT_SPEED_LIMIT) { mainWindow.CannonYVelocity -= 20; } //set too far to false. if its stil too far the next key event handler will set to true s_PanTiltTooFarDown = false; panTilt.PanY(mainWindow.CannonYVelocity); } else if (key == System.Windows.Input.Key.Right && !s_PanTiltTooFarRight && mainWindow.TrackingMode == TrackingMode.MANUAL) { if (mainWindow.CannonXVelocity >= -1 * PAN_TILT_SPEED_LIMIT) { mainWindow.CannonXVelocity -= 20; } //set too far to false. if its stil too far the next key event handler will set to true s_PanTiltTooFarLeft = false; panTilt.PanX(mainWindow.CannonXVelocity); } else if (key == System.Windows.Input.Key.Left && !s_PanTiltTooFarLeft && mainWindow.TrackingMode == TrackingMode.MANUAL) { if (mainWindow.CannonXVelocity <= PAN_TILT_SPEED_LIMIT) { mainWindow.CannonXVelocity += 20; } //set too far to false. if its stil too far the next key event handler will set to true s_PanTiltTooFarRight = false; panTilt.PanX(mainWindow.CannonXVelocity); } } //anything goes wrong, stop pantilt and crash the app catch (Exception ex) { panTilt.Disengage(); throw ex; } }
public MainWindow() { // one sensor is currently supported this.kinectSensor = KinectSensor.GetDefault(); // get the coordinate mapper this.coordinateMapper = this.kinectSensor.CoordinateMapper; // open the reader for the body frames this.bodyFrameReader = this.kinectSensor.BodyFrameSource.OpenReader(); this.colorFrameReader = this.kinectSensor.ColorFrameSource.OpenReader(); this.audioReader = this.kinectSensor.AudioSource.OpenReader(); // get the depth (display) extents FrameDescription jointFrameDescription = this.kinectSensor.DepthFrameSource.FrameDescription; FrameDescription colorFrameDescription = this.kinectSensor.ColorFrameSource.CreateFrameDescription(ColorImageFormat.Bgra); FrameDescription infraredFrameDescription = this.kinectSensor.InfraredFrameSource.FrameDescription; colorRenderer = new ColorFrameRenderer(colorFrameDescription.Width, colorFrameDescription.Height, jointFrameDescription.Width, jointFrameDescription.Height, infraredFrameDescription.Width, infraredFrameDescription.Height); var drawingGroup = new DrawingGroup(); var drawingImage = new DrawingImage(drawingGroup); hudRenderer = new HudRenderer(drawingGroup, drawingImage, colorFrameDescription.Width, colorFrameDescription.Height); AudioSource audioSource = this.kinectSensor.AudioSource; // Allocate 1024 bytes to hold a single audio sub frame. Duration sub frame // is 16 msec, the sample rate is 16khz, which means 256 samples per sub frame. // With 4 bytes per sample, that gives us 1024 bytes. this.audioBuffer = new byte[audioSource.SubFrameLengthInBytes]; this.colorFrameReader.FrameArrived += this.Reader_ColorFrameArrived; this.audioReader.FrameArrived += audioReader_FrameArrived; //on startup hide the audio meter AudioMeterVisibility = Visibility.Hidden; this.infraredFrameReader = this.kinectSensor.InfraredFrameSource.OpenReader(); //Infrared // open the reader for the depth frames this.infraredFrameReader = this.kinectSensor.InfraredFrameSource.OpenReader(); // wire handler for frame arrival this.infraredFrameReader.FrameArrived += this.colorRenderer.Reader_InfraredFrameArrived; // set IsAvailableChanged event notifier this.kinectSensor.IsAvailableChanged += this.Sensor_IsAvailableChanged; // open the sensor this.kinectSensor.Open(); // set the status text TODO: change namespace name in resources this.StatusText = this.kinectSensor.IsAvailable ? Microsoft.Samples.Kinect.BodyBasics.Properties.Resources.RunningStatusText : Microsoft.Samples.Kinect.BodyBasics.Properties.Resources.NoSensorStatusText; // use the window object as the view model in this simple example this.DataContext = this; // initialize the components (controls) of the window this.InitializeComponent(); //register the code which will tell the system what to do when keys are pressed SetupKeyHandlers(); //initialize panTilt = PanTiltController.GetOrCreatePanTiltController(); firingControl = FiringController.GetOrCreateFiringController(); var panTiltErr = panTilt.TryInitialize(); var firingErr = firingControl.TryInitialize(); if (panTiltErr != null) { //crash the app. we can't do anything if it doesn't intialize throw panTiltErr; } if (firingErr != null) { //crash the app. we can't do anything if it doesn't intialize throw firingErr; } string safetyText; if (this.firingControl.VirtualSafetyOn) { safetyText = Microsoft.Samples.Kinect.BodyBasics.Properties.Resources.SafetyDisengagedText; } else { safetyText = Microsoft.Samples.Kinect.BodyBasics.Properties.Resources.SafetyEngagedText; } panTilt.TryInitialize(); //draw the headsup display initially this.hudRenderer.RenderHud(new HudRenderingParameters() { CannonX = this.CannonX, CannonY = this.CannonY, //CannonTheta = this.CannonTheta, StatusText = this.statusText, SystemReady = (this.kinectSensor.IsAvailable && this.kinectSensor.IsOpen && this.panTilt.IsReady), FrameRate = this.FrameRate, TrackingMode = this.trackingMode, FiringSafety = this.firingControl.VirtualSafetyOn, FiringSafetyText = safetyText }); //set voice synth to Hazel this.voiceSynth.SelectVoice("Microsoft Hazel Desktop"); this.voiceSynth.SpeakAsync("Kinect Cannon Fully Initialized"); //debug start frame rate counter FPSTimerStart(); // Try to use the controller }
//Static for now... not sure if we actually need to make this class instatiable public static async Task HandleInput(MainWindow mainWindow, PanTiltController panTilt, FiringController firingController, Key?key, XboxController handHeldController) { //////////////////////////////////////////////////////////////////////////// //Manual Control Logic //////////////////////////////////////////////////////////////////////////// if (panTilt.IsReady) { if (key != null) { HandlePanTilt(mainWindow, panTilt, firingController, (Key)key); } else { HandleXboxInputPanTilt(mainWindow, panTilt, firingController, handHeldController); } } if (key == Key.NumPad1 || key == Key.D1 || handHeldController.IsXPressed) { mainWindow.TrackingMode = TrackingMode.MANUAL; mainWindow.AudioMeterVisibility = Visibility.Hidden; } else if (key == Key.NumPad2 || key == Key.D2 || handHeldController.IsAPressed) { mainWindow.TrackingMode = TrackingMode.SKELETAL; mainWindow.AudioMeterVisibility = Visibility.Hidden; } else if (key == Key.NumPad3 || key == Key.D3 || handHeldController.IsBPressed) { mainWindow.TrackingMode = TrackingMode.AUDIBLE; mainWindow.AudioMeterVisibility = Visibility.Visible; } //The ordering rational of the key assignments is based on //the bottom row of the keyboard else if (mainWindow.TrackingMode == TrackingMode.SKELETAL && key == Key.Z) { mainWindow.RequestedTrackedSkeleton = SkeletalLetter.A; } else if (mainWindow.TrackingMode == TrackingMode.SKELETAL && key == Key.X) { mainWindow.RequestedTrackedSkeleton = SkeletalLetter.B; } else if (mainWindow.TrackingMode == TrackingMode.SKELETAL && key == Key.C) { mainWindow.RequestedTrackedSkeleton = SkeletalLetter.C; } else if (mainWindow.TrackingMode == TrackingMode.SKELETAL && key == Key.V) { mainWindow.RequestedTrackedSkeleton = SkeletalLetter.D; } else if (mainWindow.TrackingMode == TrackingMode.SKELETAL && key == Key.B) { mainWindow.RequestedTrackedSkeleton = SkeletalLetter.E; } else if (mainWindow.TrackingMode == TrackingMode.SKELETAL && key == Key.N) { mainWindow.RequestedTrackedSkeleton = SkeletalLetter.F; } else if (key == Key.NumPad4 || key == Key.D4 || handHeldController.IsYPressed) { //toggle 'night vision' mode if (mainWindow.DisplayMode == DisplayMode.INFRARED) { mainWindow.DisplayMode = DisplayMode.COLOR; } else { mainWindow.DisplayMode = DisplayMode.INFRARED; } //check to see if this is the UI thread, xbox controller uses different thread if (!mainWindow.Dispatcher.CheckAccess()) { await mainWindow.Dispatcher.BeginInvoke((Action)(() => { mainWindow.CameraFeedImageSource.Source = mainWindow.ImageSource; })); } else { //we're on the UI thread directly change the UI element image source, keyboard stroke hits this path mainWindow.CameraFeedImageSource.Source = mainWindow.ImageSource; } } if (handHeldController.IsDPadRightPressed) { mainWindow.RequestedTrackedSkeleton++; //Console.WriteLine(mainWindow.RequestedTrackedSkeleton); } if (handHeldController.IsDPadLeftPressed) { mainWindow.RequestedTrackedSkeleton--; } if (handHeldController.IsDPadDownPressed) { mainWindow.RequestedTrackedSkeleton = SkeletalLetter.A; } ///////////////////////////////////////////////////////// //Rage Safteys - Makey Makey Board Provides These Events ///////////////////////////////////////////////////////// try { //mainWindow.Cannon is too high if (key == Key.A && !s_PanTiltTooFarUp) { s_PanTiltTooFarUp = true; mainWindow.CannonYVelocity = 0; panTilt.PanY(mainWindow.CannonYVelocity); } //mainWindow.Cannon is too low else if (key == Key.S && !s_PanTiltTooFarDown) { s_PanTiltTooFarDown = true; mainWindow.CannonYVelocity = 0; panTilt.PanY(mainWindow.CannonYVelocity); } //mainWindow.Cannon is too far right else if (key == Key.D && !s_PanTiltTooFarRight) { s_PanTiltTooFarRight = true; mainWindow.CannonXVelocity = 0; panTilt.PanX(mainWindow.CannonXVelocity); } //mainWindow.Cannon is too far left else if (key == Key.F && !s_PanTiltTooFarLeft) { s_PanTiltTooFarLeft = true; mainWindow.CannonXVelocity = 0; panTilt.PanX(mainWindow.CannonXVelocity); } } //if anything at all goes wrong we gotta stop the pantilt and crash the app catch (Exception ex) { panTilt.Disengage(); throw ex; } if ((key == Key.P || (handHeldController.IsLeftShoulderPressed & handHeldController.IsRightShoulderPressed)) && firingController.VirtualSafetyOn) { //keep safety from being toggled too fast if ((DateTime.Now - s_LastDisarmTime > s_DisarmWaitTime) || s_LastDisarmTime == null) { s_LastDisarmTime = DateTime.Now; //toggle the safety firingController.VirtualSafetyOn = !firingController.VirtualSafetyOn; s_VoiceSynth.SelectVoice("Microsoft Hazel Desktop"); s_VoiceSynth.SpeakAsync("System Armed, Pull both triggers simultaneously to fire!"); } //handHeldController.Vibrate(40.0, 40.0, 10.0); } else if ((key == Key.P || (handHeldController.IsLeftShoulderPressed & handHeldController.IsRightShoulderPressed)) && !firingController.VirtualSafetyOn) { //keep safety from being toggled too fast if (DateTime.Now - s_LastDisarmTime > s_DisarmWaitTime) { s_LastDisarmTime = DateTime.Now; //toggle the safety firingController.VirtualSafetyOn = !firingController.VirtualSafetyOn; s_VoiceSynth.SelectVoice("Microsoft Hazel Desktop"); s_VoiceSynth.SpeakAsync("System Disarmed!"); } } //MAYBE WE SHOULD PICK A HARDER TO PRESS KEY THAN THE SPACE BAR? if (key == Key.Space || (handHeldController.RightTrigger > 250 && handHeldController.LeftTrigger > 250)) { handHeldController.Vibrate(VIBRATION_INTENSITY, VIBRATION_INTENSITY, new TimeSpan(1)); await firingController.Fire(350); } }
//Static for now... not sure if we actually need to make this class instatiable public static async Task HandleInput(MainWindow mainWindow, PanTiltController panTilt, FiringController firingController, Key? key, XboxController handHeldController){ //////////////////////////////////////////////////////////////////////////// //Manual Control Logic //////////////////////////////////////////////////////////////////////////// if (panTilt.IsReady) { if (key != null) { HandlePanTilt(mainWindow, panTilt, firingController, (Key)key); } else { HandleXboxInputPanTilt(mainWindow, panTilt, firingController, handHeldController); } } if (key == Key.NumPad1 || key == Key.D1 || handHeldController.IsXPressed) { mainWindow.TrackingMode = TrackingMode.MANUAL; mainWindow.AudioMeterVisibility = Visibility.Hidden; } else if (key == Key.NumPad2 || key == Key.D2 || handHeldController.IsAPressed) { mainWindow.TrackingMode = TrackingMode.SKELETAL; mainWindow.AudioMeterVisibility = Visibility.Hidden; } else if (key == Key.NumPad3 || key == Key.D3 || handHeldController.IsBPressed) { mainWindow.TrackingMode = TrackingMode.AUDIBLE; mainWindow.AudioMeterVisibility = Visibility.Visible; } //The ordering rational of the key assignments is based on //the bottom row of the keyboard else if (mainWindow.TrackingMode == TrackingMode.SKELETAL && key == Key.Z) { mainWindow.RequestedTrackedSkeleton = SkeletalLetter.A; } else if (mainWindow.TrackingMode == TrackingMode.SKELETAL && key == Key.X) { mainWindow.RequestedTrackedSkeleton = SkeletalLetter.B; } else if (mainWindow.TrackingMode == TrackingMode.SKELETAL && key == Key.C) { mainWindow.RequestedTrackedSkeleton = SkeletalLetter.C; } else if (mainWindow.TrackingMode == TrackingMode.SKELETAL && key == Key.V) { mainWindow.RequestedTrackedSkeleton = SkeletalLetter.D; } else if (mainWindow.TrackingMode == TrackingMode.SKELETAL && key == Key.B) { mainWindow.RequestedTrackedSkeleton = SkeletalLetter.E; } else if (mainWindow.TrackingMode == TrackingMode.SKELETAL && key == Key.N) { mainWindow.RequestedTrackedSkeleton = SkeletalLetter.F; } else if (key == Key.NumPad4 || key == Key.D4 || handHeldController.IsYPressed) { //toggle 'night vision' mode if (mainWindow.DisplayMode == DisplayMode.INFRARED) { mainWindow.DisplayMode = DisplayMode.COLOR; } else{ mainWindow.DisplayMode = DisplayMode.INFRARED; } //check to see if this is the UI thread, xbox controller uses different thread if (!mainWindow.Dispatcher.CheckAccess()) { await mainWindow.Dispatcher.BeginInvoke((Action)(()=>{ mainWindow.CameraFeedImageSource.Source = mainWindow.ImageSource; })); } else { //we're on the UI thread directly change the UI element image source, keyboard stroke hits this path mainWindow.CameraFeedImageSource.Source = mainWindow.ImageSource; } } if (handHeldController.IsDPadRightPressed) { mainWindow.RequestedTrackedSkeleton++; //Console.WriteLine(mainWindow.RequestedTrackedSkeleton); } if (handHeldController.IsDPadLeftPressed) { mainWindow.RequestedTrackedSkeleton--; } if (handHeldController.IsDPadDownPressed) { mainWindow.RequestedTrackedSkeleton = SkeletalLetter.A; } ///////////////////////////////////////////////////////// //Rage Safteys - Makey Makey Board Provides These Events ///////////////////////////////////////////////////////// try { //mainWindow.Cannon is too high if (key == Key.A && !s_PanTiltTooFarUp) { s_PanTiltTooFarUp = true; mainWindow.CannonYVelocity = 0; panTilt.PanY(mainWindow.CannonYVelocity); } //mainWindow.Cannon is too low else if (key == Key.S && !s_PanTiltTooFarDown) { s_PanTiltTooFarDown = true; mainWindow.CannonYVelocity = 0; panTilt.PanY(mainWindow.CannonYVelocity); } //mainWindow.Cannon is too far right else if (key == Key.D && !s_PanTiltTooFarRight) { s_PanTiltTooFarRight = true; mainWindow.CannonXVelocity = 0; panTilt.PanX(mainWindow.CannonXVelocity); } //mainWindow.Cannon is too far left else if (key == Key.F && !s_PanTiltTooFarLeft) { s_PanTiltTooFarLeft = true; mainWindow.CannonXVelocity = 0; panTilt.PanX(mainWindow.CannonXVelocity); } } //if anything at all goes wrong we gotta stop the pantilt and crash the app catch(Exception ex) { panTilt.Disengage(); throw ex; } if ((key == Key.P || (handHeldController.IsLeftShoulderPressed & handHeldController.IsRightShoulderPressed)) && firingController.VirtualSafetyOn) { //keep safety from being toggled too fast if ((DateTime.Now - s_LastDisarmTime > s_DisarmWaitTime) || s_LastDisarmTime == null) { s_LastDisarmTime = DateTime.Now; //toggle the safety firingController.VirtualSafetyOn = !firingController.VirtualSafetyOn; s_VoiceSynth.SelectVoice("Microsoft Hazel Desktop"); s_VoiceSynth.SpeakAsync("System Armed, Pull both triggers simultaneously to fire!"); } //handHeldController.Vibrate(40.0, 40.0, 10.0); } else if ((key == Key.P || (handHeldController.IsLeftShoulderPressed & handHeldController.IsRightShoulderPressed)) && !firingController.VirtualSafetyOn) { //keep safety from being toggled too fast if (DateTime.Now - s_LastDisarmTime > s_DisarmWaitTime) { s_LastDisarmTime = DateTime.Now; //toggle the safety firingController.VirtualSafetyOn = !firingController.VirtualSafetyOn; s_VoiceSynth.SelectVoice("Microsoft Hazel Desktop"); s_VoiceSynth.SpeakAsync("System Disarmed!"); } } //MAYBE WE SHOULD PICK A HARDER TO PRESS KEY THAN THE SPACE BAR? if (key == Key.Space || (handHeldController.RightTrigger > 250 && handHeldController.LeftTrigger > 250)) { handHeldController.Vibrate(VIBRATION_INTENSITY, VIBRATION_INTENSITY, new TimeSpan(1)); await firingController.Fire(350); } }