/// <summary> /// Represents a connection to R2D2. On construction, this object creates a two-way /// connection with the bot. To properly shutdown the connection, make sure that /// the Dispose method is called; this is especially important if you wish to create a /// new BotConnection later. /// </summary> /// <param name="botToBase">Object implementing functions that receive from the bot</param> /// <param name="hostname">Hostname of the bo.</param> public BotConnection(BotToBaseDisp_ botToBase, string hostname = "R2D2") { try { Console.WriteLine("Starting server"); communicator = Ice.Util.initialize(); Ice.ObjectAdapter adapter = communicator.createObjectAdapterWithEndpoints( "BotToBaseAdapter", "tcp -p " + BOT_TO_BASE_PORT); adapter.add(botToBase, communicator.stringToIdentity("BotToBaseImpl")); adapter.activate(); Console.WriteLine("Connecting to R2D2"); Ice.ObjectPrx prx = communicator.stringToProxy( string.Format("BaseToBotImpl:tcp -h {0} -p {1}", hostname, BASE_TO_BOT_PORT)); BotProxy = BaseToBotPrxHelper.checkedCast(prx); if (BotProxy == null) { throw new ApplicationException("Invalid proxy"); } // Pings are necessary to ensure that the connection is live. // We want R2D2 to stop immediately if the connection goes dead. new Thread(pinger).Start(); } catch { if (communicator != null) { communicator.destroy(); } throw; } }
static void PlaySound(BaseToBotPrx bot) { Console.WriteLine("Which sound?"); var sounds = Enum.GetValues(typeof(Sound)).Cast <Sound>(); Console.WriteLine(string.Join(", ", sounds.Select((s, i) => i + ". " + s))); string line = Console.ReadLine(); Sound snd = (Sound)Enum.Parse(typeof(Sound), line, true); bot.playSound(snd); }
static void Main(string[] args) { BotConnection botConn = null; while (true) { try { var botToBase = new BotToBaseImpl(); botConn = new BotConnection(botToBase, "r2d2"); BaseToBotPrx bot = botConn.BotProxy; while (true) { Console.WriteLine("Choose a command:"); Console.WriteLine(" 1. Play sound"); Console.WriteLine(" 2. Move using XBox controller"); switch (Console.ReadKey(true).Key) { case ConsoleKey.D1: PlaySound(bot); break; case ConsoleKey.D2: XboxControls(bot, botToBase); break; } } } catch (Exception e) { Console.Error.WriteLine(e.ToString()); } finally { if (botConn != null) { botConn.Dispose(); } } } }
public static void write__(IceInternal.BasicStream os__, BaseToBotPrx v__) { os__.writeProxy(v__); }
static void XboxControls(BaseToBotPrx bot, BotToBaseImpl botToBase) { // Adjustable constants const int commandFrequency = 20; // commands per second const float speedMul = 60; const float headMul = 2.0f; const int moveSpeed = 8; // Mutable state float headAngle = 0; GamePadButtons buttons = GamePad.GetState(PlayerIndex.One).Buttons; int prevLSpeed = 0; int prevRSpeed = 0; Console.WriteLine("Use the left and right thumbsticks to move the wheels"); Console.WriteLine("Use the left and right triggers to rotate the head"); while (true) { GamePadState state = GamePad.GetState(PlayerIndex.One); if (!state.IsConnected) { Console.WriteLine("Gamepad not connected!"); bot.move(0, 0); return; } // Calculate wheel speed int lSpeed, rSpeed; if (state.DPad.Up == ButtonState.Pressed) { lSpeed = rSpeed = moveSpeed; } else if (state.DPad.Down == ButtonState.Pressed) { lSpeed = rSpeed = -moveSpeed; } else if (state.DPad.Left == ButtonState.Pressed) { bot.move(-moveSpeed, moveSpeed); lSpeed = -moveSpeed; rSpeed = moveSpeed; } else if (state.DPad.Right == ButtonState.Pressed) { lSpeed = moveSpeed; rSpeed = -moveSpeed; } else { lSpeed = Convert.ToInt32(speedMul * state.ThumbSticks.Left.Y); rSpeed = Convert.ToInt32(speedMul * state.ThumbSticks.Right.Y); } // Move; make sure we don't send the same speed repeatedly if (!(lSpeed == prevLSpeed && rSpeed == prevRSpeed)) { bot.move(lSpeed, rSpeed); } prevLSpeed = lSpeed; prevRSpeed = rSpeed; // Calculate head angle float headAngleDelta = state.Triggers.Left - state.Triggers.Right; if (headAngleDelta != 0) { headAngle += headMul * headAngleDelta; bot.rotateHead((int)headAngle); } // Play sound? if (state.Buttons.A == ButtonState.Pressed && state.Buttons.A != buttons.A) { bot.playSound(Sound.CHEERFUL); } else if (state.Buttons.B == ButtonState.Pressed && state.Buttons.B != buttons.B) { bot.playSound(Sound.EXCITED); } else if (state.Buttons.X == ButtonState.Pressed && state.Buttons.X != buttons.X) { bot.playSound(Sound.LAUGHING); } else if (state.Buttons.Y == ButtonState.Pressed && state.Buttons.Y != buttons.Y) { bot.playSound(Sound.SAD); } // Move flap? if (state.Buttons.RightShoulder == ButtonState.Pressed && state.Buttons.RightShoulder != buttons.RightShoulder) { bot.setFlapState(FlapState.FLAPOPEN); } else if (state.Buttons.LeftShoulder == ButtonState.Pressed && state.Buttons.LeftShoulder != buttons.LeftShoulder) { bot.setFlapState(FlapState.FLAPCLOSED); } buttons = state.Buttons; Thread.Sleep(1000 / commandFrequency); } }
static void XboxControls(BaseToBotPrx bot, BotToBaseImpl botToBase) { // Adjustable constants const int commandFrequency = 20; // commands per second const float speedMul = 60; const float headMul = 2.0f; const int moveSpeed = 8; // Mutable state float headAngle = 0; GamePadButtons buttons = GamePad.GetState(PlayerIndex.One).Buttons; int prevLSpeed = 0; int prevRSpeed = 0; Console.WriteLine("Use the left and right thumbsticks to move the wheels"); Console.WriteLine("Use the left and right triggers to rotate the head"); while (true) { GamePadState state = GamePad.GetState(PlayerIndex.One); if (!state.IsConnected) { Console.WriteLine("Gamepad not connected!"); bot.move(0, 0); return; } // Calculate wheel speed int lSpeed, rSpeed; if (state.DPad.Up == ButtonState.Pressed) { lSpeed = rSpeed = moveSpeed; } else if (state.DPad.Down == ButtonState.Pressed) { lSpeed = rSpeed = -moveSpeed; } else if (state.DPad.Left == ButtonState.Pressed) { bot.move(-moveSpeed, moveSpeed); lSpeed = -moveSpeed; rSpeed = moveSpeed; } else if (state.DPad.Right == ButtonState.Pressed) { lSpeed = moveSpeed; rSpeed = -moveSpeed; } else { lSpeed = Convert.ToInt32(speedMul * state.ThumbSticks.Left.Y); rSpeed = Convert.ToInt32(speedMul * state.ThumbSticks.Right.Y); } // Move; make sure we don't send the same speed repeatedly if (!(lSpeed == prevLSpeed && rSpeed == prevRSpeed)) { bot.move(lSpeed, rSpeed); } prevLSpeed = lSpeed; prevRSpeed = rSpeed; // Calculate head angle float headAngleDelta = state.Triggers.Left - state.Triggers.Right; if (headAngleDelta != 0) { headAngle += headMul * headAngleDelta; bot.rotateHead((int) headAngle); } // Play sound? if (state.Buttons.A == ButtonState.Pressed && state.Buttons.A != buttons.A) { bot.playSound(Sound.CHEERFUL); } else if (state.Buttons.B == ButtonState.Pressed && state.Buttons.B != buttons.B) { bot.playSound(Sound.EXCITED); } else if (state.Buttons.X == ButtonState.Pressed && state.Buttons.X != buttons.X) { bot.playSound(Sound.LAUGHING); } else if (state.Buttons.Y == ButtonState.Pressed && state.Buttons.Y != buttons.Y) { bot.playSound(Sound.SAD); } // Move flap? if (state.Buttons.RightShoulder == ButtonState.Pressed && state.Buttons.RightShoulder != buttons.RightShoulder) { bot.setFlapState(FlapState.FLAPOPEN); } else if (state.Buttons.LeftShoulder == ButtonState.Pressed && state.Buttons.LeftShoulder != buttons.LeftShoulder) { bot.setFlapState(FlapState.FLAPCLOSED); } buttons = state.Buttons; Thread.Sleep(1000 / commandFrequency); } }
static void PlaySound(BaseToBotPrx bot) { Console.WriteLine("Which sound?"); var sounds = Enum.GetValues(typeof(Sound)).Cast<Sound>(); Console.WriteLine(string.Join(", ", sounds.Select((s, i) => i + ". " + s))); string line = Console.ReadLine(); Sound snd = (Sound)Enum.Parse(typeof(Sound), line, true); bot.playSound(snd); }