-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* changed main in manager's tests Signed-off-by: Jakub Delicat <[email protected]> * Changed to AreButtonsPressed Signed-off-by: Jakub Delicat <[email protected]> --------- Signed-off-by: Jakub Delicat <[email protected]>
- Loading branch information
Showing
16 changed files
with
316 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
panther_manager/include/panther_manager/plugins/condition/are_buttons_pressed.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// Copyright 2024 Husarion sp. z o.o. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#ifndef PANTHER_MANAGER_PLUGINS_CONDITION_ARE_BUTTONS_PRESSED_HPP_ | ||
#define PANTHER_MANAGER_PLUGINS_CONDITION_ARE_BUTTONS_PRESSED_HPP_ | ||
|
||
#include <memory> | ||
#include <mutex> | ||
#include <string> | ||
|
||
#include <behaviortree_ros2/bt_topic_sub_node.hpp> | ||
#include <rclcpp/rclcpp.hpp> | ||
|
||
#include <sensor_msgs/msg/joy.hpp> | ||
|
||
#include "panther_manager/behavior_tree_utils.hpp" | ||
|
||
namespace panther_manager | ||
{ | ||
|
||
class AreButtonsPressed : public BT::RosTopicSubNode<sensor_msgs::msg::Joy> | ||
{ | ||
public: | ||
AreButtonsPressed( | ||
const std::string & name, const BT::NodeConfig & conf, const BT::RosNodeParams & params) | ||
: BT::RosTopicSubNode<sensor_msgs::msg::Joy>(name, conf, params) | ||
{ | ||
} | ||
|
||
BT::NodeStatus onTick(const std::shared_ptr<sensor_msgs::msg::Joy> & last_msg); | ||
|
||
static BT::PortsList providedPorts() | ||
{ | ||
return providedBasicPorts( | ||
{BT::InputPort<std::vector<int>>("buttons", "state of buttons to accept a condition")}); | ||
} | ||
|
||
private: | ||
std::vector<int> buttons_; | ||
}; | ||
|
||
} // namespace panther_manager | ||
|
||
#endif // PANTHER_MANAGER_PLUGINS_CONDITION_ARE_BUTTONS_PRESSED_HPP_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
panther_manager/src/plugins/condition/are_buttons_pressed.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// Copyright 2024 Husarion sp. z o.o. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#include "panther_manager/plugins/condition/are_buttons_pressed.hpp" | ||
|
||
#include "panther_manager/behavior_tree_utils.hpp" | ||
|
||
namespace panther_manager | ||
{ | ||
|
||
BT::NodeStatus AreButtonsPressed::onTick(const std::shared_ptr<sensor_msgs::msg::Joy> & last_msg) | ||
{ | ||
getInput<std::vector<int>>("buttons", buttons_); | ||
|
||
if (!last_msg) { | ||
RCLCPP_WARN_STREAM(this->logger(), GetLoggerPrefix(name()) << "There is no joy messages!"); | ||
return BT::NodeStatus::FAILURE; | ||
} | ||
|
||
if (last_msg->buttons.size() < buttons_.size()) { | ||
RCLCPP_ERROR_STREAM( | ||
this->logger(), GetLoggerPrefix(name()) << "Joy message has " << last_msg->buttons.size() | ||
<< " buttons, expected at least " << buttons_.size()); | ||
return BT::NodeStatus::FAILURE; | ||
} | ||
|
||
if (std::equal(buttons_.begin(), buttons_.end(), last_msg->buttons.begin())) { | ||
return BT::NodeStatus::SUCCESS; | ||
} | ||
|
||
return BT::NodeStatus::FAILURE; | ||
} | ||
|
||
} // namespace panther_manager | ||
|
||
#include "behaviortree_ros2/plugins.hpp" | ||
CreateRosNodePlugin(panther_manager::AreButtonsPressed, "AreButtonsPressed"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletions
119
panther_manager/test/plugins/condition/test_are_buttons_pressed.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
// Copyright 2024 Husarion sp. z o.o. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#include <cstdint> | ||
#include <map> | ||
#include <string> | ||
#include <vector> | ||
|
||
#include <gtest/gtest.h> | ||
|
||
#include <behaviortree_cpp/bt_factory.h> | ||
#include <rclcpp/rclcpp.hpp> | ||
|
||
#include <sensor_msgs/msg/joy.hpp> | ||
|
||
#include "panther_manager/plugins/condition/are_buttons_pressed.hpp" | ||
#include "utils/plugin_test_utils.hpp" | ||
|
||
class TestAreButtonsPressed : public panther_manager::plugin_test_utils::PluginTestUtils | ||
{ | ||
public: | ||
TestAreButtonsPressed(); | ||
void PublishJoyMessage(const std::vector<int> & buttons); | ||
|
||
protected: | ||
rclcpp::Publisher<sensor_msgs::msg::Joy>::SharedPtr joy_publisher_; | ||
std::map<std::string, std::string> params_ = {{"topic_name", "joy"}, {"buttons", "0;1;0"}}; | ||
}; | ||
|
||
TestAreButtonsPressed::TestAreButtonsPressed() | ||
{ | ||
RegisterNodeWithParams<panther_manager::AreButtonsPressed>("AreButtonsPressed"); | ||
joy_publisher_ = bt_node_->create_publisher<sensor_msgs::msg::Joy>("joy", 10); | ||
} | ||
|
||
void TestAreButtonsPressed::PublishJoyMessage(const std::vector<int> & buttons) | ||
{ | ||
sensor_msgs::msg::Joy msg; | ||
msg.buttons = buttons; | ||
joy_publisher_->publish(msg); | ||
} | ||
|
||
TEST_F(TestAreButtonsPressed, LoadingAreButtonsPressedPlugin) | ||
{ | ||
ASSERT_NO_THROW({ CreateTree("AreButtonsPressed", params_); }); | ||
} | ||
|
||
TEST_F(TestAreButtonsPressed, NoMessage) | ||
{ | ||
ASSERT_NO_THROW({ CreateTree("AreButtonsPressed", params_); }); | ||
|
||
auto & tree = GetTree(); | ||
auto status = tree.tickWhileRunning(std::chrono::milliseconds(100)); | ||
EXPECT_EQ(status, BT::NodeStatus::FAILURE); | ||
} | ||
|
||
TEST_F(TestAreButtonsPressed, WrongMessageTooFewButtons) | ||
{ | ||
ASSERT_NO_THROW({ CreateTree("AreButtonsPressed", params_); }); | ||
|
||
PublishJoyMessage({0, 1}); | ||
|
||
auto & tree = GetTree(); | ||
auto status = tree.tickWhileRunning(std::chrono::milliseconds(100)); | ||
EXPECT_EQ(status, BT::NodeStatus::FAILURE); | ||
} | ||
|
||
TEST_F(TestAreButtonsPressed, GoodMessageWrongButtonsState) | ||
{ | ||
ASSERT_NO_THROW({ CreateTree("AreButtonsPressed", params_); }); | ||
|
||
PublishJoyMessage({0, 0, 0}); | ||
|
||
auto & tree = GetTree(); | ||
auto status = tree.tickWhileRunning(std::chrono::milliseconds(100)); | ||
EXPECT_EQ(status, BT::NodeStatus::FAILURE); | ||
} | ||
|
||
TEST_F(TestAreButtonsPressed, GoodMessageWithTooMuchButtonsAndGoodButtonsState) | ||
{ | ||
ASSERT_NO_THROW({ CreateTree("AreButtonsPressed", params_); }); | ||
|
||
PublishJoyMessage({0, 1, 0, 0, 0, 1}); | ||
|
||
auto & tree = GetTree(); | ||
auto status = tree.tickWhileRunning(std::chrono::milliseconds(100)); | ||
EXPECT_EQ(status, BT::NodeStatus::SUCCESS); | ||
} | ||
|
||
TEST_F(TestAreButtonsPressed, GoodMessageGoodButtonsState) | ||
{ | ||
ASSERT_NO_THROW({ CreateTree("AreButtonsPressed", params_); }); | ||
|
||
PublishJoyMessage({0, 1, 0}); | ||
|
||
auto & tree = GetTree(); | ||
auto status = tree.tickWhileRunning(std::chrono::milliseconds(100)); | ||
EXPECT_EQ(status, BT::NodeStatus::SUCCESS); | ||
} | ||
|
||
int main(int argc, char ** argv) | ||
{ | ||
testing::InitGoogleTest(&argc, argv); | ||
|
||
auto result = RUN_ALL_TESTS(); | ||
|
||
return result; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.