protected override void OnMessage(MessageEventArgs e) { // read operation var bytes = e.RawData; var operation = (Operation)bytes[0]; if (operation == Operation.Ping) { return; } if (operation == Operation.Move) { var leftSpeedByte = bytes[1]; var rightSpeedByte = bytes[2]; if (leftSpeedByte == 0 && rightSpeedByte == 0) { robot.Stop(); base.OnMessage(e); return; } var leftSpeed = Convert.ToSingle((sbyte)leftSpeedByte); var rightSpeed = Convert.ToSingle((sbyte)rightSpeedByte); robot.Forward(leftSpeed, rightSpeed); } else if (operation == Operation.CameraMove) { var movement = (CameraMovement)bytes[1]; var degrees = Convert.ToInt32((sbyte)bytes[2]); if (movement == CameraMovement.Pan) { robot.CameraChangePanPosition(degrees); } else { robot.CameraChangeTiltPosition(degrees); } } base.OnMessage(e); }
static void ManualControl() { const int speedStep = 10; int speed = 80; const int cameraStep = 5; int cameraPanPosition = 0; int cameraTiltPosition = 0; Console.WriteLine("press q to quit."); // move camera to initial position robot.CameraChangePanPosition(cameraPanPosition); robot.CameraChangeTiltPosition(cameraTiltPosition); // light off //robot.LedFront.Set(false); //robot.LedRear.Set(false); bool ledFront = false, ledRear = false; ConsoleKeyInfo key; while ((key = Console.ReadKey(true)).Key != ConsoleKey.Q) { // speed control if (key.Key == ConsoleKey.H && speed < 100) { speed += speedStep; Console.WriteLine("speed is now at {0}%", speed); } if (key.Key == ConsoleKey.N && speed > 0) { speed -= speedStep; Console.WriteLine("speed is now at {0}%", speed); } var hasShift = (key.Modifiers & ConsoleModifiers.Shift) != 0; // if shift is not pressed, control wheel if (!hasShift) { if (key.Key == ConsoleKey.UpArrow) { robot.Forward(speed); Console.WriteLine("forward"); } if (key.Key == ConsoleKey.DownArrow) { robot.Reverse(speed); Console.WriteLine("reverse"); } if (key.Key == ConsoleKey.RightArrow) { robot.SpinRight(speed); Console.WriteLine("spin right"); } if (key.Key == ConsoleKey.LeftArrow) { robot.SpinLeft(speed); Console.WriteLine("spin left"); } if (key.Key == ConsoleKey.Spacebar) { robot.Stop(); Console.WriteLine("stop"); } if (key.Key == ConsoleKey.D) { //Console.WriteLine("distance = {0}cm", robot.Sonar.Distance()); } } else { Console.WriteLine("shift + " + key.Key); // if shift move camera if (key.Key == ConsoleKey.A || key.Key == ConsoleKey.UpArrow) { cameraTiltPosition += cameraStep; robot.CameraChangeTiltPosition(cameraTiltPosition); Console.WriteLine("camera up"); } if (key.Key == ConsoleKey.B || key.Key == ConsoleKey.DownArrow) { cameraTiltPosition -= cameraStep; robot.CameraChangeTiltPosition(cameraTiltPosition); Console.WriteLine("camera down"); } if (key.Key == ConsoleKey.C || key.Key == ConsoleKey.RightArrow) { cameraPanPosition += cameraStep; robot.CameraChangePanPosition(cameraPanPosition); Console.WriteLine("camera right"); } if (key.Key == ConsoleKey.D || key.Key == ConsoleKey.LeftArrow) { cameraPanPosition -= cameraStep; robot.CameraChangePanPosition(cameraPanPosition); Console.WriteLine("camera left"); } } if (key.Key == ConsoleKey.F) { ledFront = !ledFront; //robot.LedFront.Set(ledFront); } if (key.Key == ConsoleKey.R) { ledRear = !ledRear; //robot.LedRear.Set(ledRear); } } }