Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ros2 bt manager to hpp #461

Merged
merged 6 commits into from
Dec 11, 2024
Merged

Ros2 bt manager to hpp #461

merged 6 commits into from
Dec 11, 2024

Conversation

delihus
Copy link
Contributor

@delihus delihus commented Dec 11, 2024

Description

  • Moved definitions of BehaviorTreeManager to .hpp to use this class in other packages
  • the tests of the managers pass,
  • moved test_shutdown_single_host_node because it needs ssh configuration.

Requirements

  • Code style guidelines followed
  • Documentation updated

Tests 🧪

  • Robot
  • Container
  • Simulation

Summary by CodeRabbit

  • New Features

    • Added a "Unit testing" section to the README, detailing instructions for building and testing the husarion_ugv package.
  • Bug Fixes

    • Improved error handling for invalid blackboard entry types in the BehaviorTree configuration process.
  • Documentation

    • Updated README.md with new section and minor formatting adjustments.
  • Refactor

    • Integrated initialization logic directly into method definitions of the BehaviorTreeManager class.
  • Chores

    • Removed obsolete source files and updated test definitions accordingly.

@delihus delihus requested a review from KmakD December 11, 2024 12:16
Copy link
Contributor

coderabbitai bot commented Dec 11, 2024

Walkthrough

The pull request introduces several changes to the Husarion UGV project. A new "Unit testing" section has been added to the README.md, providing guidance on building and testing the husarion_ugv package. In the CMakeLists.txt for husarion_ugv_manager, multiple source files have been removed from executable and test definitions. Additionally, the BehaviorTreeManager class has undergone significant modifications, with methods converted to inline definitions and enhanced error handling. The file behavior_tree_manager.cpp has been deleted, consolidating the class's functionality into the header file.

Changes

File Path Change Summary
README.md Added "Unit testing" section with instructions for building and testing the husarion_ugv package. Minor formatting adjustments made.
husarion_ugv_manager/CMakeLists.txt Removed src/behavior_tree_manager.cpp from add_executable commands and test definitions. Introduced TEST_INTEGRATION option.
husarion_ugv_manager/include/husarion_ugv_manager/behavior_tree_manager.hpp Converted Initialize and CreateBTConfig methods to inline definitions with enhanced error handling.
husarion_ugv_manager/src/behavior_tree_manager.cpp Deleted file containing implementation of the BehaviorTreeManager class.

Possibly related PRs

  • Panther manager refactor #301: Modifications to behavior tree management related to the new "Unit testing" section for the husarion_ugv package.
  • Fix safety_manager path #411: Improvements in the SafetyManagerNode, enhancing type safety and clarity, aligning with the testing instructions introduced.

Suggested reviewers

  • rafal-gorecki

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Experiment)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@delihus delihus changed the base branch from ros2 to ros2-devel December 11, 2024 12:16
Signed-off-by: Jakub Delicat <jakub.delicat@husarion.com>
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 533f520 and 8d02a31.

📒 Files selected for processing (4)
  • README.md (1 hunks)
  • husarion_ugv_manager/CMakeLists.txt (7 hunks)
  • husarion_ugv_manager/include/husarion_ugv_manager/behavior_tree_manager.hpp (2 hunks)
  • husarion_ugv_manager/src/behavior_tree_manager.cpp (0 hunks)
💤 Files with no reviewable changes (1)
  • husarion_ugv_manager/src/behavior_tree_manager.cpp
🔇 Additional comments (1)
husarion_ugv_manager/CMakeLists.txt (1)

64-65: CMakeLists.txt updates correctly reflect source file changes

The removal of src/behavior_tree_manager.cpp from the executable and test definitions appropriately aligns with the relocation of the BehaviorTreeManager implementation to the header file.

Also applies to: 80-81, 154-162, 184-185, 193-194, 230-231, 250-251

README.md Outdated Show resolved Hide resolved
Signed-off-by: Jakub Delicat <jakub.delicat@husarion.com>
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Outside diff range and nitpick comments (3)
husarion_ugv_manager/include/husarion_ugv_manager/behavior_tree_manager.hpp (3)

110-113: Use string_view for the error message literal

For better performance, consider using string_view for the error message literal since it's a compile-time constant string.

-        throw std::invalid_argument(
-          "Invalid type for blackboard entry. Valid types are: bool, int, unsigned, float, double, "
-          "const char*, std::string");
+        static constexpr std::string_view error_msg = 
+          "Invalid type for blackboard entry. Valid types are: bool, int, unsigned, float, double, "
+          "const char*, std::string";
+        throw std::invalid_argument(std::string(error_msg));

93-114: Consider using a type map for more maintainable type checking

The current implementation uses a series of if-else statements for type checking. Consider using a map of type handlers for better maintainability and extensibility.

+  template<typename T>
+  void SetBlackboardValue(
+    BT::Blackboard::Ptr blackboard, const std::string & name, const std::any & value) const
+  {
+    blackboard->set<T>(name, std::any_cast<T>(value));
+  }

   inline BT::NodeConfig CreateBTConfig(const std::map<std::string, std::any> & bb_values) const
   {
     BT::NodeConfig config;
     config.blackboard = BT::Blackboard::create();

+    static const std::map<const std::type_info *, 
+      std::function<void(BT::Blackboard::Ptr, const std::string &, const std::any &)>> type_handlers = {
+      {&typeid(bool), [this](auto bb, auto & name, auto & val) { 
+         SetBlackboardValue<bool>(bb, name, val); }},
+      {&typeid(int), [this](auto bb, auto & name, auto & val) { 
+         SetBlackboardValue<int>(bb, name, val); }},
+      // Add other types similarly...
+    };

     for (auto & [name, value] : bb_values) {
-      const std::type_info & type = value.type();
-      if (type == typeid(bool)) {
-        config.blackboard->set<bool>(name, std::any_cast<bool>(value));
-      } else if (type == typeid(int)) {
-        // ... existing type checks
+      auto handler = type_handlers.find(&value.type());
+      if (handler != type_handlers.end()) {
+        handler->second(config.blackboard, name, value);
+      } else {
         throw std::invalid_argument("Invalid type for blackboard entry...");
       }
     }
     return config;
   }

Line range hint 71-79: Consider thread safety for concurrent access

The methods TickOnce, TickExactlyOnce, TickWhileRunning, and GetTreeStatus access shared state (tree_status_) without synchronization. If these methods might be called from different threads, consider adding thread safety mechanisms.

Consider:

  1. Adding mutex protection for shared state
  2. Documenting thread safety guarantees in the class documentation
  3. Making tree_status_ atomic if simple atomic operations are sufficient
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 8d02a31 and b316d97.

📒 Files selected for processing (2)
  • README.md (1 hunks)
  • husarion_ugv_manager/include/husarion_ugv_manager/behavior_tree_manager.hpp (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • README.md

@KmakD KmakD merged commit bdbfd7f into ros2-devel Dec 11, 2024
1 check passed
@KmakD KmakD deleted the ros2-bt-manager-to-hpp branch December 11, 2024 13:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants