Skip to content

Commit c354615

Browse files
committed
Deleting unnecessary complicated tests, adding necessary condition nodes and their tests
Signed-off-by: silanus23 <berkantali23@outlook.com>
1 parent 6f72e48 commit c354615

24 files changed

Lines changed: 435 additions & 80 deletions

nav2_behavior_tree/include/nav2_behavior_tree/bt_action_node.hpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,9 @@ class BtActionNode : public BT::ActionNodeBase
7777
if (getInput("server_name", remapped_action_name)) {
7878
action_name_ = remapped_action_name;
7979
}
80-
is_global_ = config().blackboard->template get<bool>("global_mode");
80+
if (!config().blackboard->template get<bool>("is_global", is_global_)) {
81+
is_global_ = false;
82+
}
8183

8284
createActionClient(action_name_);
8385

@@ -212,7 +214,7 @@ class BtActionNode : public BT::ActionNodeBase
212214
new_run_id = config().blackboard->get<std::string>("run_id");
213215
} catch (const std::exception & e) {
214216
throw std::runtime_error(
215-
"global_mode=true requires 'run_id' to be set on the blackboard for action: " +
217+
"is_global=true requires 'run_id' to be set on the blackboard for action: " +
216218
action_name_ + ". Error: " + e.what());
217219
}
218220

nav2_behavior_tree/include/nav2_behavior_tree/bt_action_server_impl.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,8 @@ bool BtActionServer<ActionT, NodeT>::on_configure()
171171
node->declare_or_get_parameter("wait_for_service_timeout", 1000));
172172

173173
// Get parameter for global node reinitialization behavior
174-
is_global_ =
175-
node->declare_or_get_parameter("global_mode", true);
174+
is_global_ =
175+
node->declare_or_get_parameter("is_global", true);
176176

177177
always_reload_bt_ = node->declare_or_get_parameter(
178178
"always_reload_bt_xml", false);
@@ -190,7 +190,7 @@ bool BtActionServer<ActionT, NodeT>::on_configure()
190190
blackboard_ = BT::Blackboard::create();
191191

192192
// Put items on the blackboard
193-
blackboard_->template set<bool>("global_mode", is_global_); // NOLINT
193+
blackboard_->template set<bool>("is_global", is_global_); // NOLINT
194194
blackboard_->template set<nav2::LifecycleNode::SharedPtr>("node", client_node_); // NOLINT
195195
blackboard_->template set<std::chrono::milliseconds>("server_timeout", default_server_timeout_); // NOLINT
196196
blackboard_->template set<std::chrono::milliseconds>("cancel_timeout", default_cancel_timeout_); // NOLINT
@@ -384,7 +384,7 @@ bool BtActionServer<ActionT, NodeT>::loadBehaviorTree(const std::string & bt_xml
384384
blackboard->template set<std::chrono::milliseconds>(
385385
"wait_for_service_timeout",
386386
wait_for_service_timeout_);
387-
blackboard->template set<bool>("global_mode", is_global_);
387+
blackboard->template set<bool>("is_global", is_global_);
388388
}
389389
} catch (const std::exception & e) {
390390
setInternalError(

nav2_behavior_tree/include/nav2_behavior_tree/plugins/condition/distance_traveled_condition.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ class DistanceTraveledCondition : public BT::ConditionNode
8181
double distance_;
8282
double transform_tolerance_;
8383
std::string global_frame_, robot_base_frame_;
84+
85+
bool is_global_;
86+
std::string current_run_id_;
8487
};
8588

8689
} // namespace nav2_behavior_tree

nav2_behavior_tree/include/nav2_behavior_tree/plugins/condition/goal_updated_condition.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ class GoalUpdatedCondition : public BT::ConditionNode
5353
*/
5454
BT::NodeStatus tick() override;
5555

56+
/**
57+
* @brief Function to read parameters and initialize class variables
58+
*/
59+
void initialize();
60+
5661
/**
5762
* @brief Creates list of BT ports
5863
* @return BT::PortsList Containing node-specific ports
@@ -74,6 +79,8 @@ class GoalUpdatedCondition : public BT::ConditionNode
7479
private:
7580
geometry_msgs::msg::PoseStamped goal_;
7681
nav_msgs::msg::Goals goals_;
82+
bool is_global_;
83+
std::string current_run_id_;
7784
};
7885

7986
} // namespace nav2_behavior_tree

nav2_behavior_tree/include/nav2_behavior_tree/plugins/condition/is_battery_charging_condition.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ class IsBatteryChargingCondition : public BT::ConditionNode
8686
std::string battery_topic_;
8787
bool is_battery_charging_;
8888
std::chrono::milliseconds bt_loop_duration_;
89+
90+
bool is_global_;
91+
std::string current_run_id_;
8992
};
9093

9194
} // namespace nav2_behavior_tree

nav2_behavior_tree/include/nav2_behavior_tree/plugins/condition/is_battery_low_condition.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,9 @@ class IsBatteryLowCondition : public BT::ConditionNode
9494
bool is_voltage_;
9595
bool is_battery_low_;
9696
std::chrono::milliseconds bt_loop_duration_;
97+
98+
bool is_global_;
99+
std::string current_run_id_;
97100
};
98101

99102
} // namespace nav2_behavior_tree

nav2_behavior_tree/include/nav2_behavior_tree/plugins/condition/time_expired_condition.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ class TimeExpiredCondition : public BT::ConditionNode
6969
nav2::LifecycleNode::SharedPtr node_;
7070
rclcpp::Time start_;
7171
double period_;
72+
73+
bool is_global_;
74+
std::string current_run_id_;
7275
};
7376

7477
} // namespace nav2_behavior_tree

nav2_behavior_tree/include/nav2_behavior_tree/plugins/condition/transform_available_condition.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ class TransformAvailableCondition : public BT::ConditionNode
8181

8282
std::string child_frame_;
8383
std::string parent_frame_;
84+
85+
bool is_global_;
86+
std::string current_run_id_;
8487
};
8588

8689
} // namespace nav2_behavior_tree

nav2_behavior_tree/package.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
<depend>action_msgs</depend>
1515
<depend>backward_ros</depend>
16+
<depend>libboost-dev</depend>
1617
<depend>behaviortree_cpp</depend>
1718
<depend>geometry_msgs</depend>
1819
<depend>nav2_common</depend>

nav2_behavior_tree/plugins/condition/distance_traveled_condition.cpp

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ DistanceTraveledCondition::DistanceTraveledCondition(
2929
const BT::NodeConfiguration & conf)
3030
: BT::ConditionNode(condition_name, conf),
3131
distance_(1.0),
32-
transform_tolerance_(0.1)
32+
transform_tolerance_(0.1),
33+
is_global_(false),
34+
current_run_id_("")
3335
{
3436
}
3537

@@ -45,19 +47,45 @@ void DistanceTraveledCondition::initialize()
4547
node_, "global_frame", this);
4648
robot_base_frame_ = BT::deconflictPortAndParamFrame<std::string>(
4749
node_, "robot_base_frame", this);
50+
is_global_ = node_->declare_or_get_parameter("is_global", false);
4851
}
4952

5053
BT::NodeStatus DistanceTraveledCondition::tick()
5154
{
5255
if (!BT::isStatusActive(status())) {
5356
initialize();
54-
if (!nav2_util::getCurrentPose(
55-
start_pose_, *tf_, global_frame_, robot_base_frame_,
56-
transform_tolerance_))
57-
{
58-
RCLCPP_DEBUG(node_->get_logger(), "Current robot pose is not available.");
57+
if (!is_global_) {
58+
if (!nav2_util::getCurrentPose(
59+
start_pose_, *tf_, global_frame_, robot_base_frame_,
60+
transform_tolerance_))
61+
{
62+
RCLCPP_DEBUG(node_->get_logger(), "Current robot pose is not available.");
63+
}
64+
return BT::NodeStatus::FAILURE;
65+
} else if (start_pose_.header.frame_id.empty()) {
66+
if (!nav2_util::getCurrentPose(
67+
start_pose_, *tf_, global_frame_, robot_base_frame_,
68+
transform_tolerance_))
69+
{
70+
RCLCPP_DEBUG(node_->get_logger(), "Current robot pose is not available.");
71+
}
72+
}
73+
}
74+
75+
// Global mode: on RunID change fall through without resetting start_pose_
76+
if (is_global_) {
77+
std::string new_run_id;
78+
try {
79+
new_run_id = config().blackboard->template get<std::string>("run_id");
80+
} catch (const std::exception & e) {
81+
throw std::runtime_error(
82+
"is_global=true requires 'run_id' to be set on the blackboard for condition: " +
83+
std::string(name()));
84+
}
85+
if (new_run_id != current_run_id_) {
86+
current_run_id_ = new_run_id;
87+
// Do not reset start_pose_ to preserve distance measurement across runs
5988
}
60-
return BT::NodeStatus::FAILURE;
6189
}
6290

6391
// Determine distance travelled since we've started this iteration

0 commit comments

Comments
 (0)