IEnumerator<ITask> JoystickUpdateButtonsHandler(game.UpdateButtons update) { if (_driveControl != null) { WinFormsServicePort.FormInvoke( delegate() { _driveControl.UpdateJoystickButtons(update.Body); } ); } yield break; }
// TT Dec-2006 - Updated for V1.0 IEnumerator<ITask> JoystickReplaceHandler(game.Replace replace) { if (_driveControl != null) { WinFormsServicePort.FormInvoke( delegate() { _driveControl.UpdateJoystickButtons(replace.Body.Buttons); _driveControl.UpdateJoystickAxes(replace.Body.Axes); } ); } yield break; }
public void UpdateJoystickButtons(joystick.Buttons buttons) { if (buttons.Pressed != null && buttons.Pressed.Count > 0) { string[] buttonString = buttons.Pressed.ConvertAll<string>( delegate(bool button) { return button ? "X" : "O"; } ).ToArray(); lblButtons.Text = string.Join(" ", buttonString); if (chkStop.Checked && buttons.Pressed.Count > 2) { if (buttons.Pressed[2] == true) { chkStop.Checked = false; } } else if (buttons.Pressed[1] == true && buttons.Pressed.Count > 1) { chkStop.Checked = true; } if (buttons.Pressed[0] != chkDrive.Checked) { chkDrive.Checked = buttons.Pressed[0]; } } }
/// <summary> /// Handle updates to the buttons on the Gamepad /// </summary> /// <param name="update">The parameter is not used.</param> /// <returns>An Iterator</returns> private IEnumerator<ITask> JoystickUpdateButtonsHandler(game.UpdateButtons update) { if (this.obstacleAvoidanceForm != null) { //WinFormsServicePort.FormInvoke(() => this.obstacleAvoidanceForm.UpdateJoystickButtons(update.Body)); } yield break; }
/* public void ReplaceJoystickList(joystick.StateType data) { cbJoystick.BeginUpdate(); try { cbJoystick.Items.Clear(); foreach (joystick.JoystickInstance instance in data.Available) { cbJoystick.Items.Add(instance.Name); if (data.Current != null && instance.Guid == data.Current.Guid) { cbJoystick.SelectedIndex = cbJoystick.Items.Count - 1; } } cbJoystick.Tag = data.Available; } finally { cbJoystick.EndUpdate(); } } */ public void UpdateJoystickAxes(joystick.Axes axes) { int x = axes.X; int y = -axes.Y; lblX.Text = x.ToString(); lblY.Text = y.ToString(); lblZ.Text = axes.Z.ToString(); DrawJoystick(x, y); /* if (!chkStop.Checked) { int left; int right; if (chkDrive.Checked == true) { if (y > -100) { left = y + axes.X / 4; right = y - axes.X / 4; } else { left = y - axes.X / 4; right = y + axes.X / 4; } } else { left = right = 0; } _eventsPort.Post(new OnMove(this, left, right)); } */ // TT - Version 2 - New code if (!chkStop.Checked) { double left; double right; if (chkDrive.Checked == true) { //double x, y; //this is the raw length of the vector double magnitude = Math.Sqrt(x * x + y * y); //x = data.X; //y = data.Y; // Check for the "dead zone" // TT - Version 3 // Added some code so that the speed values would // not suddenly jump after leaving the Dead Zone if (Math.Abs(x) < options.DeadZoneX) x = 0; else { // Subtract off the dead zone value so that the // coord starts from zero if (x > 0) x -= (int) options.DeadZoneX; else x += (int) options.DeadZoneX; } if (Math.Abs(y) < options.DeadZoneY) y = 0; else { if (y > 0) y -= (int) options.DeadZoneY; else y += (int) options.DeadZoneY; } if (x == 0 && y == 0) { // Totally dead in the middle! left = right = 0; } else { //angle of the vector double theta = Math.Atan2(y, x); //this is the maximum magnitude for a given angle // double maxMag; double scaledMag = 1.0; /* // Sorry Ben, I did not understand why you did this // and the 1000 cancels out anyway if you look // carefully at the code. if (Math.Abs(data.X) > Math.Abs(data.Y)) scaledMag = magnitude * Math.Abs(Math.Cos(theta)); // maxMag = Math.Abs(1000 / Math.Cos(theta)); else scaledMag = magnitude * Math.Abs(Math.Sin(theta)); // maxMag = Math.Abs(1000 / Math.Sin(theta)); */ //a scaled down magnitude according to above // double scaledMag = magnitude * 1000 / maxMag; scaledMag = magnitude; //decompose the vector into motor components // What is the significance of 150? The cross-hairs // cross over at 0, so this just meant that the // cross-over point was slightly below the center // of the yoke ... // NOTE: There is a peculiar problem that if you // try to rotate the robot on the spot, and you // are not careful to keep the cursor on the // horizontal center line, then the robot might // suddenly start rotating in the OPPOSITE direction! // This is a quirk of the maths. It happens if the // cursor moves outside the dead zone on the bottom // (negative) side. This might be the reason for the // -150 in the original code, i.e. to try to avoid // this problem. //if (data.Y > -150) if (y >= 0) { left = scaledMag * options.TranslateScaleFactor * Math.Sin(theta) + scaledMag * options.RotateScaleFactor * Math.Cos(theta); right = scaledMag * options.TranslateScaleFactor * Math.Sin(theta) - scaledMag * options.RotateScaleFactor * Math.Cos(theta); } else { left = scaledMag * options.TranslateScaleFactor * Math.Sin(theta) - scaledMag * options.RotateScaleFactor * Math.Cos(theta); right = scaledMag * options.TranslateScaleFactor * Math.Sin(theta) + scaledMag * options.RotateScaleFactor * Math.Cos(theta); } } } else { left = right = 0; } //cap at 1000 left = Math.Min(left, 1000); right = Math.Min(right, 1000); left = Math.Max(left, -1000); right = Math.Max(right, -1000); // Quick and dirty way to display results for debugging - // Uncomment the two lines below // Console.WriteLine("Joy: " + data.X + ", " + data.Y // + " => " + left + ", " + right); _eventsPort.PostUnknownType(new OnMove(this, (int)Math.Round(left), (int)Math.Round(right))); } // End of changes }