From ad3b0d1092a4e3ee90f83f17babfad22746f04e4 Mon Sep 17 00:00:00 2001 From: "David.Estevez" Date: Thu, 12 May 2016 22:54:26 +0200 Subject: [PATCH 01/10] Add failing test for new API in RdRobotManager (connection+enable robot) --- .../RdRobotLib/RdMockupRobotManager.cpp | 11 ++++- .../RdRobotLib/RdMockupRobotManager.hpp | 48 ++++++++++--------- src/libraries/RdRobotLib/RdRobotManager.hpp | 9 +++- .../RdRobotLib/RdYarpRobotManager.cpp | 5 +- .../RdRobotLib/RdYarpRobotManager.hpp | 2 +- test/testRdMockupRobotManager.cpp | 27 +++++++++++ 6 files changed, 73 insertions(+), 29 deletions(-) diff --git a/src/libraries/RdRobotLib/RdMockupRobotManager.cpp b/src/libraries/RdRobotLib/RdMockupRobotManager.cpp index b0cfb5fb..0e9dfe71 100644 --- a/src/libraries/RdRobotLib/RdMockupRobotManager.cpp +++ b/src/libraries/RdRobotLib/RdMockupRobotManager.cpp @@ -19,6 +19,7 @@ namespace rd RdMockupRobotManager:: RdMockupRobotManager(const std::string& robotName): RdRobotManager(robotName) { connected = false; + enabled = false; movement_direction = NONE; camera_movement_direction = CAMERA_NONE; } @@ -196,9 +197,10 @@ bool RdMockupRobotManager::test() { return false; } -bool RdMockupRobotManager::ping() { +void RdMockupRobotManager::setEnabled(bool enabled) +{ RD_DEBUG("\n"); - return false; + return; } void RdMockupRobotManager::onDestroy(){ @@ -211,6 +213,11 @@ bool RdMockupRobotManager::isConnected() return connected; } +bool RdMockupRobotManager::isEnabled() +{ + return enabled; +} + bool RdMockupRobotManager::isMoving() { return movement_direction!=NONE; diff --git a/src/libraries/RdRobotLib/RdMockupRobotManager.hpp b/src/libraries/RdRobotLib/RdMockupRobotManager.hpp index 589a4727..c8603361 100644 --- a/src/libraries/RdRobotLib/RdMockupRobotManager.hpp +++ b/src/libraries/RdRobotLib/RdMockupRobotManager.hpp @@ -22,9 +22,9 @@ class RdMockupRobotManager : public RdRobotManager RdMockupRobotManager(const std::string& robotName); - //-- RdRobotManager interface + //-- RdRobotManager interface //----------------------------------------------------- - //-- Robot movement related functions + //-- Robot movement related functions virtual bool moveForward(int velocity = UNUSED); virtual bool moveBackwards(int velocity = UNUSED); virtual bool turnLeft(int velocity = UNUSED); @@ -37,38 +37,40 @@ class RdMockupRobotManager : public RdRobotManager virtual bool panLeft(int velocity = UNUSED); virtual bool panRight(int velocity = UNUSED); virtual bool stopCameraMovement(); - + //-- Robot connection related functions virtual bool connect(); virtual bool disconnect(); virtual bool test(); - virtual bool ping(); + virtual void setEnabled(bool enabled); virtual void onDestroy(); //-- Mockup functionality //----------------------------------------------------- - bool isConnected(); - - bool isMoving(); - int getMovementDirection(); - static const int FORWARD; - static const int BACKWARDS; - static const int LEFT; - static const int RIGHT; - static const int NONE; - - bool isCameraMoving(); - int getCameraMovementDirection(); - static const int CAMERA_UP; - static const int CAMERA_DOWN; - static const int CAMERA_LEFT; - static const int CAMERA_RIGHT; - static const int CAMERA_NONE; + bool isConnected(); + bool isEnabled(); + + bool isMoving(); + int getMovementDirection(); + static const int FORWARD; + static const int BACKWARDS; + static const int LEFT; + static const int RIGHT; + static const int NONE; + + bool isCameraMoving(); + int getCameraMovementDirection(); + static const int CAMERA_UP; + static const int CAMERA_DOWN; + static const int CAMERA_LEFT; + static const int CAMERA_RIGHT; + static const int CAMERA_NONE; private: - bool connected; - int movement_direction; + bool connected; + bool enabled; + int movement_direction; int camera_movement_direction; }; diff --git a/src/libraries/RdRobotLib/RdRobotManager.hpp b/src/libraries/RdRobotLib/RdRobotManager.hpp index bc6a0ec2..8df8e204 100644 --- a/src/libraries/RdRobotLib/RdRobotManager.hpp +++ b/src/libraries/RdRobotLib/RdRobotManager.hpp @@ -42,10 +42,17 @@ class RdRobotManager virtual bool stopCameraMovement() = 0; //-- Robot connection related functions + /// @brief Connect to the remote robot virtual bool connect() = 0; + + /// @brief Disconnect from the remote robot virtual bool disconnect() = 0; + + /// @brief Test connection (not in used yet) virtual bool test() = 0; - virtual bool ping() = 0; + + /// @brief Enable/disable sending commands through the manager + virtual void setEnabled(bool enabled) = 0; //-- Other virtual void onDestroy() = 0; diff --git a/src/libraries/RdRobotLib/RdYarpRobotManager.cpp b/src/libraries/RdRobotLib/RdYarpRobotManager.cpp index 75e2e65b..39d4514c 100644 --- a/src/libraries/RdRobotLib/RdYarpRobotManager.cpp +++ b/src/libraries/RdRobotLib/RdYarpRobotManager.cpp @@ -137,8 +137,9 @@ bool RdYarpRobotManager::test() { return true; } -bool RdYarpRobotManager::ping() { - return true; +void RdYarpRobotManager::setEnabled(bool enabled) +{ + } void RdYarpRobotManager::onDestroy(){ diff --git a/src/libraries/RdRobotLib/RdYarpRobotManager.hpp b/src/libraries/RdRobotLib/RdYarpRobotManager.hpp index b816e5a3..2d2a4d76 100644 --- a/src/libraries/RdRobotLib/RdYarpRobotManager.hpp +++ b/src/libraries/RdRobotLib/RdYarpRobotManager.hpp @@ -42,7 +42,7 @@ class RdYarpRobotManager : public RdRobotManager virtual bool connect(); virtual bool disconnect(); virtual bool test(); - virtual bool ping(); + virtual void setEnabled(bool enabled); virtual void onDestroy(); diff --git a/test/testRdMockupRobotManager.cpp b/test/testRdMockupRobotManager.cpp index 12c093ce..fa100449 100644 --- a/test/testRdMockupRobotManager.cpp +++ b/test/testRdMockupRobotManager.cpp @@ -55,9 +55,34 @@ TEST_F(RdMockupRobotManagerTest, RdMockupRobotCannotMoveIfDisconnected) EXPECT_FALSE(robotManager->isCameraMoving()); } +TEST_F(RdMockupRobotManagerTest, RdMockupRobotCannotMoveIfDisabled) +{ + ASSERT_FALSE(robotManager->isConnected()); + ASSERT_TRUE(robotManager->connect()); + robotManager->setEnabled(false); + ASSERT_FALSE(robotManager->isEnabled()); + + EXPECT_FALSE(robotManager->moveForward()); + EXPECT_FALSE(robotManager->moveBackwards()); + EXPECT_FALSE(robotManager->turnLeft()); + EXPECT_FALSE(robotManager->turnRight()); + EXPECT_FALSE(robotManager->stopMovement()); + + EXPECT_FALSE(robotManager->tiltUp()); + EXPECT_FALSE(robotManager->tiltDown()); + EXPECT_FALSE(robotManager->panLeft()); + EXPECT_FALSE(robotManager->panRight()); + EXPECT_FALSE(robotManager->isCameraMoving()); + + ASSERT_TRUE(robotManager->disconnect()); + ASSERT_FALSE(robotManager->isConnected()); +} + TEST_F(RdMockupRobotManagerTest, RdMockupRobotMoves) { ASSERT_TRUE(robotManager->connect()); + robotManager->setEnabled(true); + ASSERT_TRUE(robotManager->isEnabled()); //-- Moving forward (and stop) EXPECT_TRUE(robotManager->moveForward()); @@ -97,6 +122,8 @@ TEST_F(RdMockupRobotManagerTest, RdMockupRobotMoves) TEST_F(RdMockupRobotManagerTest, RdMockupRobotCameraMoves) { ASSERT_TRUE(robotManager->connect()); + robotManager->setEnabled(true); + ASSERT_TRUE(robotManager->isEnabled()); //-- Tilting up (and stop) EXPECT_TRUE(robotManager->tiltUp()); From 1cd4c6c0f3237f6d636c7f036d9c56b49f320f32 Mon Sep 17 00:00:00 2001 From: "David.Estevez" Date: Thu, 12 May 2016 23:03:22 +0200 Subject: [PATCH 02/10] Add support for enabling/disabling robot manager (in mockup manager) --- .../RdRobotLib/RdMockupRobotManager.cpp | 129 +++++++++++++++--- 1 file changed, 107 insertions(+), 22 deletions(-) diff --git a/src/libraries/RdRobotLib/RdMockupRobotManager.cpp b/src/libraries/RdRobotLib/RdMockupRobotManager.cpp index 0e9dfe71..f04f2946 100644 --- a/src/libraries/RdRobotLib/RdMockupRobotManager.cpp +++ b/src/libraries/RdRobotLib/RdMockupRobotManager.cpp @@ -28,8 +28,16 @@ bool RdMockupRobotManager::moveForward(int velocity) { if (connected) { - movement_direction = FORWARD; - return true; + if (enabled) + { + movement_direction = FORWARD; + return true; + } + else + { + RD_ERROR("Robot is disabled\n"); + return false; + } } else { @@ -42,8 +50,16 @@ bool RdMockupRobotManager::moveBackwards(int velocity) { if (connected) { - movement_direction = BACKWARDS; - return true; + if (enabled) + { + movement_direction = BACKWARDS; + return true; + } + else + { + RD_ERROR("Robot is disabled\n"); + return false; + } } else { @@ -56,8 +72,16 @@ bool RdMockupRobotManager::turnLeft(int velocity) { if (connected) { - movement_direction = LEFT; - return true; + if (enabled) + { + movement_direction = LEFT; + return true; + } + else + { + RD_ERROR("Robot is disabled\n"); + return false; + } } else { @@ -70,8 +94,16 @@ bool RdMockupRobotManager::turnRight(int velocity) { if (connected) { - movement_direction = RIGHT; - return true; + if (enabled) + { + movement_direction = RIGHT; + return true; + } + else + { + RD_ERROR("Robot is disabled\n"); + return false; + } } else { @@ -84,8 +116,16 @@ bool RdMockupRobotManager::stopMovement() { if (connected) { - movement_direction = NONE; - return true; + if (enabled) + { + movement_direction = NONE; + return true; + } + else + { + RD_ERROR("Robot is disabled\n"); + return false; + } } else { @@ -98,8 +138,16 @@ bool RdMockupRobotManager::tiltUp(int velocity) { if (connected) { - camera_movement_direction = CAMERA_UP; - return true; + if (enabled) + { + camera_movement_direction = CAMERA_UP; + return true; + } + else + { + RD_ERROR("Robot is disabled\n"); + return false; + } } else { @@ -112,8 +160,16 @@ bool RdMockupRobotManager::tiltDown(int velocity) { if (connected) { - camera_movement_direction = CAMERA_DOWN; - return true; + if (enabled) + { + camera_movement_direction = CAMERA_DOWN; + return true; + } + else + { + RD_ERROR("Robot is disabled\n"); + return false; + } } else { @@ -126,8 +182,16 @@ bool RdMockupRobotManager::panLeft(int velocity) { if (connected) { - camera_movement_direction = CAMERA_LEFT; - return true; + if (enabled) + { + camera_movement_direction = CAMERA_LEFT; + return true; + } + else + { + RD_ERROR("Robot is disabled\n"); + return false; + } } else { @@ -140,8 +204,16 @@ bool RdMockupRobotManager::panRight(int velocity) { if (connected) { - camera_movement_direction = CAMERA_RIGHT; - return true; + if (enabled) + { + camera_movement_direction = CAMERA_RIGHT; + return true; + } + else + { + RD_ERROR("Robot is disabled\n"); + return false; + } } else { @@ -154,8 +226,16 @@ bool RdMockupRobotManager::stopCameraMovement() { if (connected) { - camera_movement_direction = NONE; - return true; + if (enabled) + { + camera_movement_direction = NONE; + return true; + } + else + { + RD_ERROR("Robot is disabled\n"); + return false; + } } else { @@ -199,8 +279,13 @@ bool RdMockupRobotManager::test() { void RdMockupRobotManager::setEnabled(bool enabled) { - RD_DEBUG("\n"); - return; + this->enabled = enabled; + if (enabled) + { + RD_DEBUG("RdMockupRobotManager enabled\n"); + } + else + RD_DEBUG("RdMockupRobotManager disabled\n"); } void RdMockupRobotManager::onDestroy(){ From b5393fe21bd38338ab8ca839327aaa9411168e73 Mon Sep 17 00:00:00 2001 From: "David.Estevez" Date: Thu, 12 May 2016 23:04:13 +0200 Subject: [PATCH 03/10] Add failing test for RobotManager support in InitState --- test/testInitState.cpp | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/test/testInitState.cpp b/test/testInitState.cpp index ef8bdb49..e2ec5be7 100644 --- a/test/testInitState.cpp +++ b/test/testInitState.cpp @@ -139,7 +139,8 @@ TEST_F(InitStateTest, InitStateWorksCorrectly ) ASSERT_TRUE(mockupNetworkManager->isStopped()); ASSERT_TRUE(mockupImageManager->isStopped()); ASSERT_TRUE(mockupInputManager->isStopped()); -// ASSERT_TRUE(mockupRobotManager->isStopped()); + ASSERT_FALSE(mockupRobotManager->isConnected()); + ASSERT_FALSE(mockupRobotManager->isEnabled()); //-- Start state machine ASSERT_TRUE(fsm->start()); @@ -158,8 +159,8 @@ TEST_F(InitStateTest, InitStateWorksCorrectly ) ASSERT_FALSE(mockupInputManager->isStopped()); ASSERT_EQ(1, mockupInputManager->getNumListeners()); -// ASSERT_FALSE(mockupRobotManager->isStopped()); -// ASSERT_FALSE(mockupRobotManager->isConnected()); + ASSERT_TRUE(mockupRobotManager->isConnected()); + ASSERT_FALSE(mockupRobotManager->isEnabled()); //-- When enter is pressed, the system should log in and go to next state: mockupInputManager->sendKeyPress(MockupKey(RdKey::KEY_ENTER)); @@ -177,7 +178,7 @@ TEST_F(InitStateTest, InitStateWorksCorrectly ) ASSERT_FALSE(mockupInputManager->isStopped()); ASSERT_EQ(0, mockupInputManager->getNumListeners()); -// ASSERT_FALSE(mockupRobotManager->isStopped()); -// ASSERT_TRUE(mockupRobotManager->isConnected()); + ASSERT_TRUE(mockupRobotManager->isConnected()); + ASSERT_FALSE(mockupRobotManager->isEnabled()); } From c99d3e0353a3488453ba66430d0f4d3e1bf8e963 Mon Sep 17 00:00:00 2001 From: "David.Estevez" Date: Thu, 12 May 2016 23:06:23 +0200 Subject: [PATCH 04/10] Add support for Robot Manager in InitState --- src/libraries/GameStatesLib/InitState.cpp | 2 ++ test/testInitState.cpp | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/libraries/GameStatesLib/InitState.cpp b/src/libraries/GameStatesLib/InitState.cpp index fb600402..958e6f76 100644 --- a/src/libraries/GameStatesLib/InitState.cpp +++ b/src/libraries/GameStatesLib/InitState.cpp @@ -44,6 +44,8 @@ bool rd::InitState::loop() { //-- Log in networkManager->login(mentalMap->getMyself()); + robotManager->connect(); + robotManager->setEnabled(false); logged_in = true; } diff --git a/test/testInitState.cpp b/test/testInitState.cpp index e2ec5be7..81e798ba 100644 --- a/test/testInitState.cpp +++ b/test/testInitState.cpp @@ -159,7 +159,7 @@ TEST_F(InitStateTest, InitStateWorksCorrectly ) ASSERT_FALSE(mockupInputManager->isStopped()); ASSERT_EQ(1, mockupInputManager->getNumListeners()); - ASSERT_TRUE(mockupRobotManager->isConnected()); + ASSERT_FALSE(mockupRobotManager->isConnected()); ASSERT_FALSE(mockupRobotManager->isEnabled()); //-- When enter is pressed, the system should log in and go to next state: From 6743ee14ef65f707fcb68c51c83deb83b9d5d327 Mon Sep 17 00:00:00 2001 From: "David.Estevez" Date: Thu, 12 May 2016 23:27:15 +0200 Subject: [PATCH 05/10] Add failing test for new feature (KeyUp/KeyDown emulation) in MockupInputManager --- test/testMockupInputManager.cpp | 44 +++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/test/testMockupInputManager.cpp b/test/testMockupInputManager.cpp index 15393865..a3f6170f 100644 --- a/test/testMockupInputManager.cpp +++ b/test/testMockupInputManager.cpp @@ -167,3 +167,47 @@ TEST_F(MockupInputManagerTest, SeveralPrintableKeyPressesSentAndReceivedCorrectl ASSERT_TRUE(inputManager->stop()); ASSERT_TRUE(inputManager->isStopped()); } + +TEST_F(MockupInputManagerTest, KeyUpAndKeyDownSentAndReceivedCorrectly) +{ + MockupInputEventListener listener; + RdInputEventListener * plistener = (RdInputEventListener *) &listener; + ASSERT_TRUE(((RdInputManager*)inputManager)->addInputEventListener(plistener)); + + ASSERT_TRUE(inputManager->start()); + ASSERT_FALSE(inputManager->isStopped()); + + //-- Fake a key down + MockupKey enter_key(RdKey::KEY_ENTER); + MockupKey d_key('d'); + ASSERT_TRUE(inputManager->sendKeyDown(enter_key)); + ASSERT_TRUE(inputManager->sendKeyDown(d_key)); + + //-- Check if keypress was received + ASSERT_EQ(2,listener.getNumKeyDownPresses()); + ASSERT_EQ(0,listener.getNumKeyUpPresses()); + + //-- Fake a key up + ASSERT_TRUE(inputManager->sendKeyUp(enter_key)); + ASSERT_TRUE(inputManager->sendKeyUp(d_key)); + + //-- Check if keypress was received + ASSERT_EQ(2,listener.getNumKeyDownPresses()); + ASSERT_EQ(2,listener.getNumKeyUpPresses()); + + //-- Check if keypress received is correct + std::vector keys_down_received = listener.getStoredKeyDownPresses(); + std::vector keys_up_received = listener.getStoredKeyDownPresses(); + ASSERT_EQ(2,keys_down_received.size()); + ASSERT_EQ(2,keys_up_received.size()); + + ASSERT_EQ(enter_key.getValue(), keys_down_received[0].getValue()); + ASSERT_EQ(enter_key.getValue(), keys_up_received[0].getValue()); + ASSERT_EQ(d_key.getChar(), keys_down_received[1].getChar()); + ASSERT_EQ(d_key.getChar(), keys_up_received[1].getChar()); + + //-- Cleanup + ASSERT_TRUE(inputManager->removeInputEventListeners()); + ASSERT_TRUE(inputManager->stop()); + ASSERT_TRUE(inputManager->isStopped()); +} From 5ebaf1f1966c6566106abaa15eda71c6fb3f2072 Mon Sep 17 00:00:00 2001 From: "David.Estevez" Date: Thu, 12 May 2016 23:27:43 +0200 Subject: [PATCH 06/10] Add skeleton for new KeyUp/KeyDown API members --- src/libraries/RdInputLib/MockupInputManager.cpp | 10 ++++++++++ src/libraries/RdInputLib/MockupInputManager.hpp | 2 ++ 2 files changed, 12 insertions(+) diff --git a/src/libraries/RdInputLib/MockupInputManager.cpp b/src/libraries/RdInputLib/MockupInputManager.cpp index 7425ca8c..9f29bc81 100644 --- a/src/libraries/RdInputLib/MockupInputManager.cpp +++ b/src/libraries/RdInputLib/MockupInputManager.cpp @@ -18,6 +18,16 @@ bool rd::MockupInputManager::sendKeyPress(RdKey key) return true; } +bool rd::MockupInputManager::sendKeyUp(rd::RdKey key) +{ + return false; +} + +bool rd::MockupInputManager::sendKeyDown(rd::RdKey key) +{ + return false; +} + rd::MockupInputManager::MockupInputManager() { stopped = true; diff --git a/src/libraries/RdInputLib/MockupInputManager.hpp b/src/libraries/RdInputLib/MockupInputManager.hpp index 2571df70..05084404 100644 --- a/src/libraries/RdInputLib/MockupInputManager.hpp +++ b/src/libraries/RdInputLib/MockupInputManager.hpp @@ -19,6 +19,8 @@ class MockupInputManager : public RdInputManager public: //------------------------------ Testing Interface ------------------------------------------------------------// bool sendKeyPress(RdKey key); + bool sendKeyUp(RdKey key); + bool sendKeyDown(RdKey key); //------------------------------ Construction & destruction ---------------------------------------------------// MockupInputManager(); From 3f1345400db3ac8437f2dc296307d1af1692d8ce Mon Sep 17 00:00:00 2001 From: "David.Estevez" Date: Thu, 12 May 2016 23:28:51 +0200 Subject: [PATCH 07/10] Add new KeyUp/KeyDown API members --- src/libraries/RdInputLib/MockupInputManager.cpp | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/libraries/RdInputLib/MockupInputManager.cpp b/src/libraries/RdInputLib/MockupInputManager.cpp index 9f29bc81..d786c1e7 100644 --- a/src/libraries/RdInputLib/MockupInputManager.cpp +++ b/src/libraries/RdInputLib/MockupInputManager.cpp @@ -20,12 +20,24 @@ bool rd::MockupInputManager::sendKeyPress(RdKey key) bool rd::MockupInputManager::sendKeyUp(rd::RdKey key) { - return false; + if (stopped) + return false; + + for ( int i = 0; i < (int)listeners.size(); i++) + listeners.at(i)->onKeyUp(key); + + return true; } bool rd::MockupInputManager::sendKeyDown(rd::RdKey key) { - return false; + if (stopped) + return false; + + for ( int i = 0; i < (int)listeners.size(); i++) + listeners.at(i)->onKeyDown(key); + + return true; } rd::MockupInputManager::MockupInputManager() From 343471cf39fb21a1a3594af0e9342b68c20e3cff Mon Sep 17 00:00:00 2001 From: "David.Estevez" Date: Thu, 12 May 2016 23:39:01 +0200 Subject: [PATCH 08/10] Add support for robot connection and movement in GameState --- src/libraries/GameStatesLib/GameState.cpp | 57 ++++++++++++++++++++- test/testGameState.cpp | 62 ++++++++++++++++++----- 2 files changed, 105 insertions(+), 14 deletions(-) diff --git a/src/libraries/GameStatesLib/GameState.cpp b/src/libraries/GameStatesLib/GameState.cpp index c88749cb..3f29b60e 100644 --- a/src/libraries/GameStatesLib/GameState.cpp +++ b/src/libraries/GameStatesLib/GameState.cpp @@ -67,7 +67,8 @@ bool rd::GameState::setup() return false; } - //-- Robot Startup goes here + //-- Robot Startup + robotManager->setEnabled(true); //-- Show Robot Devastation game screen: //-- Set info elements on GameScreen @@ -103,6 +104,8 @@ bool rd::GameState::cleanup() audioManager->stop(); networkManager->logout(mentalMap->getMyself()); //-- This is kind of weird, but it is supposed to be done like this networkManager->stop(); + robotManager->setEnabled(false); + robotManager->disconnect(); return true; } else @@ -142,11 +145,63 @@ bool rd::GameState::onKeyDown(rd::RdKey k) return true; } + //-- Movement control + if (k.getValue() == RdKey::KEY_ARROW_LEFT) + { + RD_DEBUG("Left arrow was pressed!\n"); + robotManager->turnLeft(); + return true; + } + if (k.getValue() == RdKey::KEY_ARROW_RIGHT) + { + RD_DEBUG("Right arrow was pressed!\n"); + robotManager->turnRight(); + return true; + } + if (k.getValue() == RdKey::KEY_ARROW_UP) + { + RD_DEBUG("Up arrow was pressed!\n"); + robotManager->moveForward(); + return true; + } + if (k.getValue() == RdKey::KEY_ARROW_DOWN) + { + RD_DEBUG("Down arrow was pressed!\n"); + robotManager->moveBackwards(); + return true; + } + return false; } bool rd::GameState::onKeyUp(rd::RdKey k) { + //-- Movement control + if (k.getValue() == RdKey::KEY_ARROW_LEFT) + { + RD_DEBUG("Left arrow was released!\n"); + robotManager->stopMovement(); + return true; + } + if (k.getValue() == RdKey::KEY_ARROW_RIGHT) + { + RD_DEBUG("Right arrow was released!\n"); + robotManager->stopMovement(); + return true; + } + if (k.getValue() == RdKey::KEY_ARROW_UP) + { + RD_DEBUG("Up arrow was released!\n"); + robotManager->stopMovement(); + return true; + } + if (k.getValue() == RdKey::KEY_ARROW_DOWN) + { + RD_DEBUG("Down arrow was released!\n"); + robotManager->stopMovement(); + return true; + } + return false; } diff --git a/test/testGameState.cpp b/test/testGameState.cpp index 3c3ce2c9..0c1526c3 100644 --- a/test/testGameState.cpp +++ b/test/testGameState.cpp @@ -16,6 +16,7 @@ #include "MockupAudioManager.hpp" #include "MockupState.hpp" +#include #include #include @@ -216,8 +217,8 @@ TEST_F(GameStateTest, GameStateGameFlowIsCorrect) ASSERT_TRUE(mockupImageManager->isStopped()); ASSERT_FALSE(mockupInputManager->isStopped()); ASSERT_EQ(0, mockupInputManager->getNumListeners()); -// ASSERT_FALSE(mockupRobotManager->isStopped()); -// ASSERT_TRUE(mockupRobotManager->isConnected()); + ASSERT_TRUE(mockupRobotManager->isConnected()); + ASSERT_FALSE(mockupRobotManager->isEnabled()); //-- Start state machine ASSERT_TRUE(fsm->start()); @@ -232,8 +233,8 @@ TEST_F(GameStateTest, GameStateGameFlowIsCorrect) ASSERT_FALSE(mockupImageManager->isStopped()); ASSERT_FALSE(mockupInputManager->isStopped()); ASSERT_EQ(1, mockupInputManager->getNumListeners()); -// ASSERT_FALSE(mockupRobotManager->isStopped()); -// ASSERT_TRUE(mockupRobotManager->isConnected()); + ASSERT_TRUE(mockupRobotManager->isConnected()); + ASSERT_TRUE(mockupRobotManager->isEnabled()); //-- Testing game flow //----------------------------------------------------------------------------- @@ -246,7 +247,42 @@ TEST_F(GameStateTest, GameStateGameFlowIsCorrect) ASSERT_EQ(50, mentalMap->getMyself().getHealth()); //-- If I send move commands, robot moves -// mockupInputManager->sendKeyPress(MockupKey(RdKey::KEY_ARROW_LEFT)); + //-- Left + mockupInputManager->sendKeyDown(MockupKey(RdKey::KEY_ARROW_LEFT)); + yarp::os::Time::delay(0.5); + ASSERT_TRUE(mockupRobotManager->isMoving()); + ASSERT_EQ(RdMockupRobotManager::LEFT, ((RdMockupRobotManager *)robotManager)->getMovementDirection()); + mockupInputManager->sendKeyUp(MockupKey(RdKey::KEY_ARROW_LEFT)); + yarp::os::Time::delay(0.5); + ASSERT_FALSE(mockupRobotManager->isMoving()); + ASSERT_EQ(RdMockupRobotManager::NONE, ((RdMockupRobotManager *)robotManager)->getMovementDirection()); + //-- Right + mockupInputManager->sendKeyDown(MockupKey(RdKey::KEY_ARROW_RIGHT)); + yarp::os::Time::delay(0.5); + ASSERT_TRUE(mockupRobotManager->isMoving()); + ASSERT_EQ(RdMockupRobotManager::RIGHT,((RdMockupRobotManager *)robotManager)->getMovementDirection()); + mockupInputManager->sendKeyUp(MockupKey(RdKey::KEY_ARROW_RIGHT)); + yarp::os::Time::delay(0.5); + ASSERT_FALSE(mockupRobotManager->isMoving()); + ASSERT_EQ(RdMockupRobotManager::NONE, ((RdMockupRobotManager *)robotManager)->getMovementDirection()); + //-- Forward + mockupInputManager->sendKeyDown(MockupKey(RdKey::KEY_ARROW_UP)); + yarp::os::Time::delay(0.5); + ASSERT_TRUE(mockupRobotManager->isMoving()); + ASSERT_EQ(RdMockupRobotManager::FORWARD, ((RdMockupRobotManager *)robotManager)->getMovementDirection()); + mockupInputManager->sendKeyUp(MockupKey(RdKey::KEY_ARROW_UP)); + yarp::os::Time::delay(0.5); + ASSERT_FALSE(mockupRobotManager->isMoving()); + ASSERT_EQ(RdMockupRobotManager::NONE, ((RdMockupRobotManager *)robotManager)->getMovementDirection()); + //-- Backwards + mockupInputManager->sendKeyDown(MockupKey(RdKey::KEY_ARROW_DOWN)); + yarp::os::Time::delay(0.5); + ASSERT_TRUE(mockupRobotManager->isMoving()); + ASSERT_EQ(RdMockupRobotManager::BACKWARDS, ((RdMockupRobotManager *)robotManager)->getMovementDirection()); + mockupInputManager->sendKeyUp(MockupKey(RdKey::KEY_ARROW_DOWN)); + yarp::os::Time::delay(0.5); + ASSERT_FALSE(mockupRobotManager->isMoving()); + ASSERT_EQ(RdMockupRobotManager::NONE, ((RdMockupRobotManager *)robotManager)->getMovementDirection()); //-- If I shoot with no target in the scope, the enemies life is kept equal mockupImageManager->receiveImage(test_frame_no_target); @@ -300,8 +336,8 @@ TEST_F(GameStateTest, GameStateGameFlowIsCorrect) ASSERT_TRUE(mockupAudioManager->isPlaying("RD_THEME")); ASSERT_FALSE(mockupNetworkManager->isStopped()); ASSERT_TRUE(mockupNetworkManager->isLoggedIn()); - //ASSERT_FALSE(mockupRobotManager->isStopped()); //-- Not correctly implemented - //ASSERT_FALSE(mockupRobotManager->isConnected()); + ASSERT_TRUE(mockupRobotManager->isConnected()); + ASSERT_TRUE(mockupRobotManager->isEnabled()); //-- Check that deadState is active ASSERT_EQ(dead_state_id, fsm->getCurrentState()); @@ -336,8 +372,8 @@ TEST_F(GameStateTest, GameStateQuitsWhenRequested ) ASSERT_TRUE(mockupImageManager->isStopped()); ASSERT_FALSE(mockupInputManager->isStopped()); ASSERT_EQ(0, mockupInputManager->getNumListeners()); -// ASSERT_FALSE(mockupRobotManager->isStopped()); -// ASSERT_TRUE(mockupRobotManager->isConnected()); + ASSERT_TRUE(mockupRobotManager->isConnected()); + ASSERT_FALSE(mockupRobotManager->isEnabled()); //-- Start state machine ASSERT_TRUE(fsm->start()); @@ -352,8 +388,8 @@ TEST_F(GameStateTest, GameStateQuitsWhenRequested ) ASSERT_FALSE(mockupImageManager->isStopped()); ASSERT_FALSE(mockupInputManager->isStopped()); ASSERT_EQ(1, mockupInputManager->getNumListeners()); -// ASSERT_FALSE(mockupRobotManager->isStopped()); -// ASSERT_TRUE(mockupRobotManager->isConnected()); + ASSERT_TRUE(mockupRobotManager->isConnected()); + ASSERT_TRUE(mockupRobotManager->isEnabled()); //-- Testing exiting game //----------------------------------------------------------------------------- @@ -372,8 +408,8 @@ TEST_F(GameStateTest, GameStateQuitsWhenRequested ) ASSERT_FALSE(mockupAudioManager->isPlaying("RD_THEME")); ASSERT_TRUE(mockupNetworkManager->isStopped()); ASSERT_FALSE(mockupNetworkManager->isLoggedIn()); - //ASSERT_FALSE(mockupRobotManager->isStopped()); //-- Not correctly implemented - //ASSERT_FALSE(mockupRobotManager->isConnected()); + ASSERT_FALSE(mockupRobotManager->isConnected()); + ASSERT_FALSE(mockupRobotManager->isEnabled()); //-- Check that end state is active ASSERT_EQ(-1, fsm->getCurrentState()); //-- (When FSM is ended, no state is active, hence -1) From a3b471d13a044345871bac9d21b3cd3b952d41ad Mon Sep 17 00:00:00 2001 From: "David.Estevez" Date: Thu, 12 May 2016 23:43:40 +0200 Subject: [PATCH 09/10] Add failing test for RobotManager support on DeadState --- test/testDeadState.cpp | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/test/testDeadState.cpp b/test/testDeadState.cpp index 07351280..65927e15 100644 --- a/test/testDeadState.cpp +++ b/test/testDeadState.cpp @@ -137,13 +137,13 @@ class DeadStateTest : public testing::Test initState = NULL; //-- Finish setup of the modules that start at game state: + //-- Note: we can change this to use GameState (as previously) if we want someday mockupImageManager->start(); - listener = new MockupInputEventListener; mockupInputManager->addInputEventListener(listener); - audioManager->start(); audioManager->play("RD_THEME", -1); + mockupRobotManager->setEnabled(true); } @@ -233,8 +233,8 @@ TEST_F(DeadStateTest, DeadStateGoesToRespawn) ASSERT_FALSE(mockupAudioManager->isPlaying("RD_DEAD")); ASSERT_FALSE(mockupNetworkManager->isStopped()); ASSERT_TRUE(mockupNetworkManager->isLoggedIn()); - //ASSERT_FALSE(mockupRobotManager->isStopped()); //-- Not correctly implemented - //ASSERT_FALSE(mockupRobotManager->isConnected()); + ASSERT_TRUE(mockupRobotManager->isConnected()); + ASSERT_TRUE(mockupRobotManager->isEnabled()); //-- Start state machine ASSERT_TRUE(fsm->start()); @@ -249,8 +249,8 @@ TEST_F(DeadStateTest, DeadStateGoesToRespawn) ASSERT_TRUE(mockupAudioManager->isPlaying("RD_DEAD")); ASSERT_FALSE(mockupNetworkManager->isStopped()); ASSERT_TRUE(mockupNetworkManager->isLoggedIn()); - //ASSERT_TRUE(mockupRobotManager->isStopped()); //-- Not correctly implemented - //ASSERT_FALSE(mockupRobotManager->isConnected()); + ASSERT_TRUE(mockupRobotManager->isConnected()); + ASSERT_FALSE(mockupRobotManager->isEnabled()); //-- Check that deadState is active ASSERT_EQ(dead_state_id, fsm->getCurrentState()); @@ -278,8 +278,8 @@ TEST_F(DeadStateTest, DeadStateGoesToRespawn) ASSERT_FALSE(mockupAudioManager->isPlaying("RD_DEAD")); ASSERT_FALSE(mockupNetworkManager->isStopped()); ASSERT_TRUE(mockupNetworkManager->isLoggedIn()); - //ASSERT_FALSE(mockupRobotManager->isStopped()); //-- Not correctly implemented - //ASSERT_FALSE(mockupRobotManager->isConnected()); + ASSERT_TRUE(mockupRobotManager->isConnected()); + ASSERT_FALSE(mockupRobotManager->isEnabled()); //-- Check that gameState is active ASSERT_EQ(game_state_id, fsm->getCurrentState()); @@ -319,8 +319,8 @@ TEST_F(DeadStateTest, DeadStateGoesToLogout) ASSERT_FALSE(mockupAudioManager->isPlaying("RD_DEAD")); ASSERT_FALSE(mockupNetworkManager->isStopped()); ASSERT_TRUE(mockupNetworkManager->isLoggedIn()); - //ASSERT_FALSE(mockupRobotManager->isStopped()); //-- Not correctly implemented - //ASSERT_FALSE(mockupRobotManager->isConnected()); + ASSERT_TRUE(mockupRobotManager->isConnected()); + ASSERT_FALSE(mockupRobotManager->isEnabled()); //-- Start state machine ASSERT_TRUE(fsm->start()); @@ -335,8 +335,8 @@ TEST_F(DeadStateTest, DeadStateGoesToLogout) ASSERT_TRUE(mockupAudioManager->isPlaying("RD_DEAD")); ASSERT_FALSE(mockupNetworkManager->isStopped()); ASSERT_TRUE(mockupNetworkManager->isLoggedIn()); - //ASSERT_TRUE(mockupRobotManager->isStopped()); //-- Not correctly implemented - //ASSERT_FALSE(mockupRobotManager->isConnected()); + ASSERT_TRUE(mockupRobotManager->isConnected()); + ASSERT_FALSE(mockupRobotManager->isEnabled()); //-- Check that deadState is active ASSERT_EQ(dead_state_id, fsm->getCurrentState()); @@ -363,8 +363,8 @@ TEST_F(DeadStateTest, DeadStateGoesToLogout) ASSERT_FALSE(mockupAudioManager->isPlaying("RD_DEAD")); ASSERT_TRUE(mockupNetworkManager->isStopped()); ASSERT_FALSE(mockupNetworkManager->isLoggedIn()); - //ASSERT_FALSE(mockupRobotManager->isStopped()); //-- Not correctly implemented - //ASSERT_FALSE(mockupRobotManager->isConnected()); + ASSERT_FALSE(mockupRobotManager->isConnected()); + ASSERT_FALSE(mockupRobotManager->isEnabled()); //-- Check that end state is active ASSERT_EQ(-1, fsm->getCurrentState()); //-- (When FSM is ended, no state is active, hence -1) From 66bf39b95893ae1a2dad26dd6a404a2e6236925c Mon Sep 17 00:00:00 2001 From: "David.Estevez" Date: Thu, 12 May 2016 23:51:59 +0200 Subject: [PATCH 10/10] Add RobotManager suport on DeadState --- src/libraries/GameStatesLib/DeadState.cpp | 5 +++++ test/testDeadState.cpp | 5 ++--- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/libraries/GameStatesLib/DeadState.cpp b/src/libraries/GameStatesLib/DeadState.cpp index d7f3fe55..abe5c6d3 100644 --- a/src/libraries/GameStatesLib/DeadState.cpp +++ b/src/libraries/GameStatesLib/DeadState.cpp @@ -44,6 +44,9 @@ bool rd::DeadState::setup() audioManager->stopMusic(); audioManager->play("RD_DEAD"); + //-- Disable robot controls + robotManager->setEnabled(false); + return true; } @@ -112,6 +115,8 @@ bool rd::DeadState::cleanup() audioManager->stop(); networkManager->logout(mentalMap->getMyself()); //-- This is kind of weird, but it is supposed to be done like this networkManager->stop(); + robotManager->setEnabled(false); + robotManager->disconnect(); return true; } else diff --git a/test/testDeadState.cpp b/test/testDeadState.cpp index 65927e15..a564661c 100644 --- a/test/testDeadState.cpp +++ b/test/testDeadState.cpp @@ -93,7 +93,7 @@ class DeadStateTest : public testing::Test ASSERT_NE((RdMockupImageManager*) NULL, mockupImageManager); //-- Load test image RdImage test_frame; - yarp::sig::file::read(test_frame, "../../share/images/test_frame.ppm"); + yarp::sig::file::read(test_frame, "../../share/images/test_frame_qr.ppm"); mockupImageManager->receiveImage(test_frame); inputManager = RdInputManager::getInputManager("MOCKUP"); @@ -320,7 +320,7 @@ TEST_F(DeadStateTest, DeadStateGoesToLogout) ASSERT_FALSE(mockupNetworkManager->isStopped()); ASSERT_TRUE(mockupNetworkManager->isLoggedIn()); ASSERT_TRUE(mockupRobotManager->isConnected()); - ASSERT_FALSE(mockupRobotManager->isEnabled()); + ASSERT_TRUE(mockupRobotManager->isEnabled()); //-- Start state machine ASSERT_TRUE(fsm->start()); @@ -354,7 +354,6 @@ TEST_F(DeadStateTest, DeadStateGoesToLogout) yarp::os::Time::delay(0.5); //-- Check that it has stopped things and it is in the final state (cleanup): - ASSERT_EQ(0, mentalMap->getMyself().getHealth()); ASSERT_TRUE(mockupImageManager->isStopped()); ASSERT_TRUE(mockupInputManager->isStopped()); ASSERT_EQ(0, mockupInputManager->getNumListeners());