Skip to content
Snippets Groups Projects
Commit b8075244 authored by KingMaZito's avatar KingMaZito
Browse files

Build producer/consumer scenario based on mqtt msg and TC Solutions

parent 2b1e4ab0
Branches
No related tags found
No related merge requests found
...@@ -75,7 +75,8 @@ ...@@ -75,7 +75,8 @@
"typeindex": "cpp", "typeindex": "cpp",
"typeinfo": "cpp", "typeinfo": "cpp",
"variant": "cpp", "variant": "cpp",
"future": "cpp" "future": "cpp",
"*.ipp": "cpp"
}, },
"python.analysis.extraPaths": [ "python.analysis.extraPaths": [
"/home/matteo/ccf_sim_workspace/devel/lib/python3/dist-packages", "/home/matteo/ccf_sim_workspace/devel/lib/python3/dist-packages",
......
...@@ -77,6 +77,8 @@ class MGMediator: public AbstractMediator, virtual Controller{ ...@@ -77,6 +77,8 @@ class MGMediator: public AbstractMediator, virtual Controller{
void publishTables(); void publishTables();
moveit::task_constructor::Task pickUp(const std::string& obj, const std::string& robotId); moveit::task_constructor::Task pickUp(const std::string& obj, const std::string& robotId);
moveit::task_constructor::Task place(const std::string& obj, const std::string& robotId, const std::string& target); moveit::task_constructor::Task place(const std::string& obj, const std::string& robotId, const std::string& target);
void processSharedStructure();
}; };
#endif #endif
\ No newline at end of file
#include "mediator/mg_mediator.h" #include "mediator/mg_mediator.h"
std::vector<moveit_task_constructor_msgs::ExecuteTaskSolutionGoal> shared_structure;
std::mutex shared_mutex;
std::condition_variable cv;
void MGMediator::processSharedStructure() {
while (ros::ok()) {
std::vector<moveit_task_constructor_msgs::SubTrajectory> peak;
{
std::unique_lock<std::mutex> lock(shared_mutex);
// Wait until the shared structure is not empty
cv.wait(lock, []{ return !shared_structure.empty(); });
// Iterate over the vectors in shared_structure
for (auto it = shared_structure.begin(); it != shared_structure.end(); ++it) {
if (!it->solution.sub_trajectory.empty()) {
// Get the first entry of the vector and add it to peak
peak.push_back(it->solution.sub_trajectory.front());
it->solution.sub_trajectory.erase(it->solution.sub_trajectory.begin());
}
if (it->solution.sub_trajectory.empty()) {
// Remove the vector if all entries have been processed
it = shared_structure.erase(it);
--it; // Adjust the iterator after erasing
}
}
} // The lock is automatically released here when `lock` goes out of scope
// Print the sizes of the sub_trajectory vectors
// if (!shared_structure.empty()) {
// for (const auto& temp : shared_structure) {
// ROS_INFO("%zu", temp.solution.sub_trajectory.size());
// }
// }
// ROS_INFO("Do something");
peak.clear();
// Sleep or do other work
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
}
MGMediator::MGMediator(std::shared_ptr<ros::NodeHandle> const& nh) MGMediator::MGMediator(std::shared_ptr<ros::NodeHandle> const& nh)
: AbstractMediator(nh) : AbstractMediator(nh)
, Controller(*nh.get()) , Controller(*nh.get())
...@@ -10,13 +56,29 @@ MGMediator::MGMediator(std::shared_ptr<ros::NodeHandle> const& nh) ...@@ -10,13 +56,29 @@ MGMediator::MGMediator(std::shared_ptr<ros::NodeHandle> const& nh)
, planning_scene_diff_publisher_(std::make_shared<ros::Publisher>(nh_->advertise<moveit_msgs::PlanningScene>("planning_scene", 1))){ , planning_scene_diff_publisher_(std::make_shared<ros::Publisher>(nh_->advertise<moveit_msgs::PlanningScene>("planning_scene", 1))){
// MQTT HANDLE // MQTT HANDLE
auto mqtt = std::make_shared<MqttConnection>("localhost", "op");
mqtt->listen("command/place");
mqtt->listen("command/pickup");
addConnection(std::move(mqtt));
addCallback("command/pickup", [&](const std::string& data) { addCallback("command/pickup", [&](const std::string& data) {
std::vector<std::pair<std::string, std::string>> pickup_command{std::make_pair("A", "panda_arm1"), std::make_pair("B", "panda_arm2") }; std::vector<std::pair<std::string, std::string>> pickup_command{std::make_pair("A", "panda_arm1"), std::make_pair("B", "panda_arm2") };
for (const auto& pair: pickup_command) { for (const auto& pair: pickup_command) {
moveit::task_constructor::Task mgt = pickUp(pair.first, pair.second); moveit::task_constructor::Task mgt = pickUp(pair.first, pair.second);
if(mgt.plan(1)) { if(mgt.plan(1)) {
//mgt.introspection().publishSolution(*mgt.solutions().front()); //mgt.introspection().publishSolution(*mgt.solutions().front());
mgt.execute(*mgt.solutions().front()); //mgt.execute(*mgt.solutions().front());
moveit_task_constructor_msgs::ExecuteTaskSolutionGoal e;
mgt.solutions().front()->fillMessage(e.solution, &mgt.introspection());
{
std::lock_guard<std::mutex> lock(shared_mutex);
shared_structure.push_back(std::move(e));
}
// Notify the consuming thread
cv.notify_one();
} }
} }
}); });
...@@ -27,17 +89,28 @@ MGMediator::MGMediator(std::shared_ptr<ros::NodeHandle> const& nh) ...@@ -27,17 +89,28 @@ MGMediator::MGMediator(std::shared_ptr<ros::NodeHandle> const& nh)
moveit::task_constructor::Task mgt = place(std::get<0>(tuple), std::get<1>(tuple), std::get<2>(tuple)); moveit::task_constructor::Task mgt = place(std::get<0>(tuple), std::get<1>(tuple), std::get<2>(tuple));
if(mgt.plan(1)) { if(mgt.plan(1)) {
//mgt.introspection().publishSolution(*mgt.solutions().front()); //mgt.introspection().publishSolution(*mgt.solutions().front());
mgt.execute(*mgt.solutions().front()); //mgt.execute(*mgt.solutions().front());
moveit_task_constructor_msgs::ExecuteTaskSolutionGoal e;
mgt.solutions().front()->fillMessage(e.solution, &mgt.introspection());
{
std::lock_guard<std::mutex> lock(shared_mutex);
shared_structure.push_back(std::move(e));
}
// Notify the consuming thread
cv.notify_one();
} }
} }
}); });
moveit::task_constructor::Task mgt2 = place("A", "panda_arm1", "dafd"); // moveit::task_constructor::Task mgt2 = place("A", "panda_arm1", "dafd");
if(mgt2.plan(1)) { // if(mgt2.plan(1)) {
//mgt.introspection().publishSolution(*mgt.solutions().front()); // //mgt.introspection().publishSolution(*mgt.solutions().front());
mgt2.execute(*mgt2.solutions().front()); // mgt2.execute(*mgt2.solutions().front());
} // }
robot_model_loader::RobotModelLoaderPtr robot_model_loader; robot_model_loader::RobotModelLoaderPtr robot_model_loader;
...@@ -62,18 +135,23 @@ MGMediator::MGMediator(std::shared_ptr<ros::NodeHandle> const& nh) ...@@ -62,18 +135,23 @@ MGMediator::MGMediator(std::shared_ptr<ros::NodeHandle> const& nh)
cartesian_planner_->setMaxAccelerationScaling(1.0); cartesian_planner_->setMaxAccelerationScaling(1.0);
cartesian_planner_->setStepSize(.01); cartesian_planner_->setStepSize(.01);
addConnection(std::make_shared<MqttConnection>("localhost:1883", "asdfadf"));
} }
void MGMediator::mediate(){ void MGMediator::mediate(){
setPanel(); setPanel();
publishTables(); publishTables();
moveit::task_constructor::Task mgt1 = pickUp("A", "panda_arm1"); // moveit::task_constructor::Task mgt1 = pickUp("A", "panda_arm1");
if(mgt1.plan(1)) { // if(mgt1.plan(1)) {
//mgt.introspection().publishSolution(*mgt.solutions().front()); // //mgt.introspection().publishSolution(*mgt.solutions().front());
mgt1.execute(*mgt1.solutions().front()); // mgt1.execute(*mgt1.solutions().front());
} // }
std::thread processThread(&MGMediator::processSharedStructure, this);
// Spin ROS node
processThread.join();
...@@ -252,8 +330,6 @@ moveit::task_constructor::Task MGMediator::pickUp(const std::string& obj, const ...@@ -252,8 +330,6 @@ moveit::task_constructor::Task MGMediator::pickUp(const std::string& obj, const
} }
return task_; return task_;
} }
moveit::task_constructor::Task MGMediator::place(const std::string& obj, const std::string& robotId, const std::string& target){ moveit::task_constructor::Task MGMediator::place(const std::string& obj, const std::string& robotId, const std::string& target){
...@@ -566,8 +642,6 @@ void MGMediator::publishTables(){ ...@@ -566,8 +642,6 @@ void MGMediator::publishTables(){
sendToAll(rss.str(), ss.str()); sendToAll(rss.str(), ss.str());
} }
sendToAll("command/pickup", "");
} }
void MGMediator::setPanel(){ void MGMediator::setPanel(){
......
...@@ -18,7 +18,7 @@ int main(int argc, char **argv){ ...@@ -18,7 +18,7 @@ int main(int argc, char **argv){
ros::init(argc, argv, "mg_routine"); ros::init(argc, argv, "mg_routine");
std::shared_ptr<ros::NodeHandle> n(new ros::NodeHandle); std::shared_ptr<ros::NodeHandle> n(new ros::NodeHandle);
ros::AsyncSpinner spinner(1); ros::AsyncSpinner spinner(4);
spinner.start(); spinner.start();
std::shared_ptr<MGMediator> mediator = std::make_shared<MGMediator>(n); std::shared_ptr<MGMediator> mediator = std::make_shared<MGMediator>(n);
...@@ -34,6 +34,6 @@ int main(int argc, char **argv){ ...@@ -34,6 +34,6 @@ int main(int argc, char **argv){
mediator->mediate(); mediator->mediate();
while (ros::ok()){ while (ros::ok()){
ros::spinOnce(); ros::spin();
} }
} }
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment