Skip to content

Commit d7369ab

Browse files
completed prompt-testing
1 parent 41a5a43 commit d7369ab

File tree

13 files changed

+102
-42
lines changed

13 files changed

+102
-42
lines changed

capabilities2_fabric/include/capabilities2_fabric/capabilities_fabric.hpp

Lines changed: 75 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ class CapabilitiesFabric : public rclcpp::Node
9696

9797
feedback_msg = std::make_shared<Plan::Feedback>();
9898
result_msg = std::make_shared<Plan::Result>();
99+
100+
need_reset_ = false;
99101
}
100102

101103
private:
@@ -141,7 +143,10 @@ class CapabilitiesFabric : public rclcpp::Node
141143
status_->error("Received the request to cancel the plan");
142144
(void)goal_handle;
143145

144-
bond_client_->stop();
146+
for (auto& [bond_id, bond_client] : bond_client_cache_)
147+
{
148+
bond_client->stop();
149+
}
145150

146151
return rclcpp_action::CancelResponse::ACCEPT;
147152
}
@@ -173,6 +178,17 @@ class CapabilitiesFabric : public rclcpp::Node
173178

174179
status_->info("Plan after adding closing event :\n\n " + modified_plan);
175180

181+
// if (need_reset_)
182+
// {
183+
// free_capability_all(connection_map);
184+
// need_reset_ = false;
185+
// }
186+
187+
interface_list.clear();
188+
providers_list.clear();
189+
rejected_list.clear();
190+
connection_map.clear();
191+
176192
expected_providers_ = 0;
177193
completed_providers_ = 0;
178194

@@ -181,6 +197,7 @@ class CapabilitiesFabric : public rclcpp::Node
181197

182198
expected_capabilities_ = 0;
183199
completed_capabilities_ = 0;
200+
freed_capabilities_ = 0;
184201

185202
expected_configurations_ = 0;
186203
completed_configurations_ = 0;
@@ -406,7 +423,7 @@ class CapabilitiesFabric : public rclcpp::Node
406423
process_feedback("Connection extraction successful");
407424

408425
// estasblish the bond with the server
409-
establish_bond();
426+
request_bond();
410427
}
411428

412429
/**
@@ -419,10 +436,6 @@ class CapabilitiesFabric : public rclcpp::Node
419436
auto feedback = std::make_shared<Plan::Feedback>();
420437
auto result = std::make_shared<Plan::Result>();
421438

422-
// intialize a vector to accomodate elements from both
423-
std::vector<std::string> tag_list(interface_list.size() + control_tag_list.size());
424-
std::merge(interface_list.begin(), interface_list.end(), control_tag_list.begin(), control_tag_list.end(), tag_list.begin());
425-
426439
// verify whether document got 'plan' tags
427440
if (!xml_parser::check_plan_tag(document))
428441
{
@@ -449,10 +462,10 @@ class CapabilitiesFabric : public rclcpp::Node
449462
}
450463

451464
/**
452-
* @brief establish the bond with capabilities2 server
465+
* @brief Request the bond from the capabilities2 server
453466
*
454467
*/
455-
void establish_bond()
468+
void request_bond()
456469
{
457470
process_feedback("Requesting bond id");
458471

@@ -469,18 +482,40 @@ class CapabilitiesFabric : public rclcpp::Node
469482

470483
auto response = future.get();
471484
bond_id_ = response->bond_id;
472-
473485
process_feedback("Received the bond id : " + bond_id_);
474486

475-
bond_client_ = std::make_unique<BondClient>(shared_from_this(), bond_id_);
476-
bond_client_->start();
487+
establish_bond();
488+
});
489+
}
477490

478-
expected_capabilities_ = connection_map.size();
491+
/**
492+
* @brief establish the bond with capabilities2 server
493+
*
494+
*/
495+
void establish_bond()
496+
{
497+
bond_client_cache_[bond_id_] = std::make_unique<BondClient>(shared_from_this(), bond_id_);
498+
bond_client_cache_[bond_id_]->start();
479499

480-
process_feedback("Requsting start of " + std::to_string(expected_capabilities_) + " capabilities");
500+
process_feedback("Bond sucessfully established with bond id : " + bond_id_);
481501

482-
use_capability(connection_map);
483-
});
502+
if (bond_client_cache_.size() > 1)
503+
{
504+
for (auto& [old_bond_id, bond_client] : bond_client_cache_)
505+
{
506+
if (old_bond_id != bond_id_)
507+
{
508+
bond_client->stop();
509+
process_feedback("Stopping and removing old bond with id : " + old_bond_id);
510+
}
511+
}
512+
}
513+
514+
expected_capabilities_ = connection_map.size();
515+
516+
process_feedback("Requsting start of " + std::to_string(expected_capabilities_) + " capabilities");
517+
518+
use_capability(connection_map);
484519
}
485520

486521
/**
@@ -511,17 +546,17 @@ class CapabilitiesFabric : public rclcpp::Node
511546
process_result("Failed to Use capability " + capability + " from " + provider + ". Server Execution Cancelled");
512547

513548
// release all capabilities that were used since not all started successfully
514-
for (const auto& [key, value] : connection_map)
549+
free_capability_all(connection_map);
550+
551+
for (auto& [bond_id, bond_client] : bond_client_cache_)
515552
{
516-
process_feedback("Freeing capability of Node " + std::to_string(key) + " named " + value.source.runner);
517-
free_capability(value.source.runner);
553+
bond_client->stop();
518554
}
519-
520-
bond_client_->stop();
521555
return;
522556
}
523557

524558
completed_capabilities_++;
559+
need_reset_ = true;
525560

526561
auto response = future.get();
527562

@@ -546,12 +581,14 @@ class CapabilitiesFabric : public rclcpp::Node
546581
}
547582

548583
/**
549-
* @brief Request use of capability from capabilities2 server
584+
* @brief Free all started capabilities in the capabilities map
550585
*
551-
* @param capability capability name to be started
586+
* @param capabilities map of capabilities to be freed
552587
*/
553-
void free_capability(const std::string& capability)
588+
void free_capability_all(std::map<int, capabilities2::node_t>& capabilities)
554589
{
590+
std::string capability = capabilities[freed_capabilities_].source.runner;
591+
555592
auto request_free = std::make_shared<FreeCapability::Request>();
556593
request_free->capability = capability;
557594
request_free->bond_id = bond_id_;
@@ -566,6 +603,18 @@ class CapabilitiesFabric : public rclcpp::Node
566603

567604
auto response = future.get();
568605
process_feedback("Successfully freed capability " + capability, true);
606+
607+
freed_capabilities_++;
608+
609+
// Check if all expected calls are completed before calling verify_plan
610+
if (freed_capabilities_ == completed_capabilities_)
611+
{
612+
process_feedback("All started capabilities have been freed.");
613+
}
614+
else
615+
{
616+
free_capability_all(connection_map);
617+
}
569618
});
570619
}
571620

@@ -754,6 +803,7 @@ class CapabilitiesFabric : public rclcpp::Node
754803

755804
/** flag to select loading from file or accepting via action server */
756805
bool read_file;
806+
bool need_reset_;
757807

758808
int expected_interfaces_;
759809
int completed_interfaces_;
@@ -763,6 +813,7 @@ class CapabilitiesFabric : public rclcpp::Node
763813

764814
int expected_capabilities_;
765815
int completed_capabilities_;
816+
int freed_capabilities_;
766817

767818
int expected_configurations_;
768819
int completed_configurations_;
@@ -771,7 +822,7 @@ class CapabilitiesFabric : public rclcpp::Node
771822
std::string bond_id_;
772823

773824
/** Manages bond between capabilities server and this client */
774-
std::shared_ptr<BondClient> bond_client_;
825+
std::map<std::string, std::shared_ptr<BondClient>> bond_client_cache_;
775826

776827
/** Handles status message sending and printing to logging */
777828
std::shared_ptr<StatusClient> status_;

capabilities2_fabric/include/capabilities2_fabric/capabilities_fabric_client.hpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -176,11 +176,11 @@ class CapabilitiesFabricClient : public rclcpp::Node
176176

177177
if (result.result->success)
178178
{
179-
status_->info("Plan executed successfully");
179+
status_->info("Plan launched successfully");
180180
}
181181
else
182182
{
183-
status_->error("Plan failed to complete");
183+
status_->error("Plan failed to launch");
184184

185185
if (result.result->failed_elements.size() > 0)
186186
{
@@ -199,6 +199,7 @@ class CapabilitiesFabricClient : public rclcpp::Node
199199
{
200200
if (fabric_state == Status::RUNNING)
201201
{
202+
status_->info("Plan canncelling requested");
202203
this->planner_client_->async_cancel_goal(goal_handle_);
203204
}
204205

@@ -208,7 +209,7 @@ class CapabilitiesFabricClient : public rclcpp::Node
208209
void setCompleteCallback(const std::shared_ptr<CompleteFabric::Request> request, std::shared_ptr<CompleteFabric::Response> response)
209210
{
210211
fabric_state = Status::COMPLETED;
211-
212+
status_->info("Plan completed successfully");
212213
completed_ = true;
213214
cv_.notify_all();
214215
}
@@ -267,7 +268,9 @@ class CapabilitiesFabricClient : public rclcpp::Node
267268

268269
plan_queue.push_back(request->plan);
269270

270-
if (fabric_state == Status::RUNNING)
271+
status_->info("Plan queued and waiting for execution");
272+
273+
if ((fabric_state == Status::RUNNING) or (fabric_state == Status::LAUNCHED))
271274
{
272275
status_->info("Prior plan under exeution. Will defer the new plan");
273276
}

capabilities2_fabric/include/capabilities2_fabric/utils/bond_client.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#pragma once
2+
#include <string>
23
#include <bondcpp/bond.hpp>
34
#include <rclcpp/rclcpp.hpp>
45

@@ -7,7 +8,7 @@ class BondClient
78
public:
89
BondClient(rclcpp::Node::SharedPtr node, const std::string& bond_id, const std::string& bonds_topic = "/capabilities/bond")
910
{
10-
bonds_topic_ = bonds_topic;
11+
topic_ = bonds_topic;
1112
bond_id_ = bond_id;
1213
node_ = node;
1314
}
@@ -16,8 +17,7 @@ class BondClient
1617
{
1718
RCLCPP_INFO(node_->get_logger(), "[BondClient] creating bond to capabilities server");
1819

19-
bond_ =
20-
std::make_unique<bond::Bond>(bonds_topic_, bond_id_, node_, std::bind(&BondClient::on_broken, this), std::bind(&BondClient::on_formed, this));
20+
bond_ = std::make_unique<bond::Bond>(topic_, bond_id_, node_, std::bind(&BondClient::on_broken, this), std::bind(&BondClient::on_formed, this));
2121

2222
bond_->setHeartbeatPeriod(0.10);
2323
bond_->setHeartbeatTimeout(10.0);
@@ -59,7 +59,7 @@ class BondClient
5959
std::string bond_id_;
6060

6161
/** Bond topic to be published */
62-
std::string bonds_topic_;
62+
std::string topic_;
6363

6464
/** Heart beat bond with capabilities server */
6565
std::shared_ptr<bond::Bond> bond_;

capabilities2_fabric/plans/prompt_4.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<Control name="sequential">
44
<Event name="std_capabilities/CapabilityGetRunner" provider="std_capabilities/CapabilityGetRunner"/>
55
<Event name="std_capabilities/PromptCapabilityRunner" provider="std_capabilities/PromptCapabilityRunner" />
6-
<Event name="std_capabilities/PromptPlanRunner" provider="std_capabilities/PromptPlanRunner" replan="false" task="move to x:1 and y:2 position" />
6+
<Event name="std_capabilities/PromptPlanRunner" provider="std_capabilities/PromptPlanRunner" replan="false" task="move to x:0.5 and y:1 position" />
77
<Event name="std_capabilities/FabricSetPlanRunner" provider="std_capabilities/FabricSetPlanRunner"/>
88
</Control>
99
</Plan>

capabilities2_runner/include/capabilities2_runner/action_runner.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,8 @@ class ActionRunner : public RunnerBase
115115
throw runner_exception(e.what());
116116
}
117117
}
118+
119+
info_("stopping runner");
118120
}
119121

120122
/**

capabilities2_runner/include/capabilities2_runner/launch_runner.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@ class LaunchRunner : public RunnerBase
124124
RCLCPP_INFO(node_->get_logger(), "Request to launch %s from %s succeeded ", launch_name.c_str(),
125125
package_name.c_str());
126126
});
127+
128+
info_("stopping runner");
127129
}
128130

129131
// throw on trigger function

capabilities2_runner/include/capabilities2_runner/service_runner.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,8 @@ class ServiceRunner : public RunnerBase
143143
info_("on_stopped", -1, events[execute_id].on_stopped, EventType::STOPPED);
144144
triggerFunction_(events[execute_id].on_stopped, update_on_stopped(events[execute_id].on_stopped_param));
145145
}
146+
147+
info_("stopping runner");
146148
}
147149

148150
protected:

capabilities2_runner/include/capabilities2_runner/topic_runner.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ class TopicRunner : public RunnerBase
122122
info_("on_stopped", -1, events[execute_id].on_stopped, EventType::STOPPED);
123123
triggerFunction_(events[execute_id].on_stopped, update_on_stopped(events[execute_id].on_stopped_param));
124124
}
125+
126+
info_("stopping runner");
125127
}
126128

127129
protected:

capabilities2_runner_prompt/include/capabilities2_runner_prompt/prompt_plan_runner.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class PromptPlanRunner : public PromptServiceRunner
4343
if (!replan)
4444
{
4545
prompt = "Build a xml plan based on the availbale capabilities to acheive mentioned task of " + taskString +
46-
". Return only the xml plan without explanations or comments";
46+
". Return only the xml plan without explanations or comments.";
4747

4848
flush = true;
4949
}

capabilities2_server/include/capabilities2_server/bond_cache.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ class BondCache
7171
{
7272
// remove bond id from capability entry
7373
auto it = std::find(bond_cache_[capability].begin(), bond_cache_[capability].end(), bond_id);
74+
7475
if (it != bond_cache_[capability].end())
7576
{
7677
bond_cache_[capability].erase(it);

0 commit comments

Comments
 (0)