Skip to content

Commit

Permalink
fix(wait_for_task_loaded): modify return condition - even if 'return_…
Browse files Browse the repository at this point in the history
…on_error' is true, when receiving the error message (load nofile), the function returns error
  • Loading branch information
keunjun-choi committed Mar 26, 2024
1 parent d7e1cbf commit 80028b5
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 5 deletions.
29 changes: 29 additions & 0 deletions examples/task_load.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include <iostream>
#include "rbpodo/rbpodo.hpp"

using namespace std;
using namespace rb::podo;

int main() {
try {
auto robot = Cobot("10.0.2.7");
auto rc = ResponseCollector();

robot.task_load(rc, "default");
if(robot.wait_for_task_loaded(rc).type() == ReturnType::Success) {
std::cout << "'default' wsl exists" << std::endl;
} else {
std::cerr << "Error" << std::endl;
}
rc = rc.error().throw_if_not_empty();

robot.task_load(rc, "default_does_not_exist");
if(robot.wait_for_task_loaded(rc).type() == ReturnType::Error) {
std::cout << "'default_dose_not_exist' wsl does not exist" << std::endl;
}
rc = rc.error().throw_if_not_empty();
} catch (const std::exception& e) {
std::cerr << e.what() << std::endl;
}
return 0;
}
22 changes: 17 additions & 5 deletions include/rbpodo/cobot.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ class ReturnType {
public:
enum Type { Undefined, Success, Timeout, Error };

explicit ReturnType(Type type = Undefined, double remain_time = 0.) : type_(type), remain_time_(remain_time) {}
ReturnType(Type type = Undefined, double remain_time = 0.) : type_(type), remain_time_(remain_time) {}

[[nodiscard]] Type type() const { return type_; }

Expand Down Expand Up @@ -478,8 +478,8 @@ class Cobot {
ReturnType set_user_coordinate(ResponseCollector& response_collector, int id, PointConstRef point,
double timeout = -1., bool return_on_error = false) {
std::stringstream ss;
ss << "set rb_manual_user_coord_6d " << id << ",1," << point[0] << "," << point[1] << "," << point[2] << "," << point[3]
<< "," << point[4] << "," << point[5];
ss << "set rb_manual_user_coord_6d " << id << ",1," << point[0] << "," << point[1] << "," << point[2] << ","
<< point[3] << "," << point[4] << "," << point[5];
sock_.send(ss.str());
return wait_until_ack_message(response_collector, timeout, return_on_error);
}
Expand Down Expand Up @@ -1371,12 +1371,24 @@ class Cobot {
return wait_until_ack_message(response_collector, timeout, return_on_error);
}

/// Even if 'return_on_error' is true, when receiving the error message (load nofile), this function returns error
ReturnType wait_for_task_loaded(ResponseCollector& response_collector, double timeout = -1.,
bool return_on_error = true) {
const auto& check = [=](const Response& res) {
const auto check_done = [=](const Response& res) {
return res.type() == Response::Type::Info && res.category() == "load" && res.msg() == "done";
};
return wait_until(response_collector, check, timeout, return_on_error);
const auto check_no_file = [=](const Response& res) {
return res.type() == Response::Type::Error && res.category() == "load" && res.msg() == "nofile";
};

const auto& check = [=](const Response& res) {
return check_done(res) || check_no_file(res);
};
auto res = wait_until(response_collector, check, timeout, return_on_error);
if (res.is_success() && check_no_file(response_collector.back())) {
return {ReturnType::Error, res.remain_time()};
}
return res;
}

ReturnType wait_for_task_started(ResponseCollector& response_collector, double timeout = -1.,
Expand Down

0 comments on commit 80028b5

Please sign in to comment.