From 48ce7b38e227762f1c3c853c9625ccf8e2a707b2 Mon Sep 17 00:00:00 2001 From: Cosmoslip <37982167+Cosmoflips@users.noreply.github.com> Date: Wed, 17 May 2023 10:28:49 +0800 Subject: [PATCH] interface optimization and rpc unify (#45) * fix: get_payload invalid object * feat: unify pause_task arguments * fix: bugfix and overload * fix:bugfix * fix:bugfix * fix:bugfix * doc: version update * fix:bugfix --------- Co-authored-by: lebai --- CMakeLists.txt | 2 +- Doxyfile | 2 +- sdk/include/lebai/robot.hh | 27 +++++++------ sdk/python/jsonbase.i | 2 +- sdk/src/protos/control.cc | 2 +- sdk/src/protos/dynamic.cc | 81 +++++++++++++++++++++++++------------- sdk/src/protos/dynamic.hh | 34 ++++++++++++---- sdk/src/robot.cc | 54 +++++++++++++++---------- sdk/src/robot_impl.cc | 8 ++++ sdk/src/robot_impl.hh | 2 + 10 files changed, 142 insertions(+), 72 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index c206a2c..709fe7f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,7 +7,7 @@ list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake") option(CMAKE_EXPORT_COMPILE_COMMANDS "Export compile command" TRUE) -project(lebai VERSION 1.1.0 LANGUAGES CXX) +project(lebai VERSION 1.1.1 LANGUAGES CXX) set(PROJECT_NAMESPACE lebai) message(STATUS "${PROJECT_NAME} version: ${PROJECT_VERSION}") # message(STATUS "major: ${PROJECT_VERSION_MAJOR}") diff --git a/Doxyfile b/Doxyfile index 7914cd9..8509d09 100644 --- a/Doxyfile +++ b/Doxyfile @@ -38,7 +38,7 @@ PROJECT_NAME = "lebai sdk" # could be handy for archiving the generated documentation or if some version # control system is used. -PROJECT_NUMBER = 1.1.0 +PROJECT_NUMBER = 1.1.1 # Using the PROJECT_BRIEF tag one can provide an optional one line description # for a project that appears at the top of each page and should give viewer a diff --git a/sdk/include/lebai/robot.hh b/sdk/include/lebai/robot.hh index 184ada3..1f6ae57 100644 --- a/sdk/include/lebai/robot.hh +++ b/sdk/include/lebai/robot.hh @@ -693,16 +693,7 @@ namespace lebai * @param dir: 调用场景所在的文件夹名 * @param params: 其他参数 */ - unsigned int start_task(const std::string &name,bool is_main,unsigned int loop_to,const std::string &dir,const std::vector & params); - /** - * @brief 调用场景 - * - * @param name: 调用场景的名字 - * @param is_main: 是否以主任务方式运行(主任务会排队执行,子任务会并发执行) - * @param loop_to: 循环次数(默认0永久循环) - * @param dir: 调用场景所在的文件夹名 - */ - unsigned int start_task(const std::string &name,bool is_main,unsigned int loop_to,const std::string &dir); + unsigned int start_task(const std::string &name,const std::vector & params,const std::string & dir, bool is_parallel,unsigned int loop_to); /** * @brief 调用场景 * @@ -721,6 +712,7 @@ namespace lebai * @param wait: 是否等待 */ void pause_task(unsigned int id,unsigned long time,bool wait); + void pause_task(unsigned int id); /** * @brief 恢复任务与运动 * @@ -896,12 +888,25 @@ namespace lebai * @param cog 质心相对于TCP坐标系的偏移. */ void set_payload(double mass, std::map cog); + /** + * @brief 设置机器人末端负载. + * + * @param mass 末端负载的质量(kg). + */ + void set_payload(double mass); + /** + * @brief 设置机器人末端负载. + * + * @param mass 末端负载的质量(kg). + * @param cog 质心相对于TCP坐标系的偏移. + */ + void set_payload(std::map cog); /** * @brief 获取末端负载设置. * * @return 由负载质量mass和负载偏移组成的元组. */ - std::tuple> get_payload(); + std::map get_payload(); /** * @brief 设置机器人重力加速度方向. * diff --git a/sdk/python/jsonbase.i b/sdk/python/jsonbase.i index 9872b1b..80a8683 100755 --- a/sdk/python/jsonbase.i +++ b/sdk/python/jsonbase.i @@ -84,7 +84,7 @@ %template(StrVector) std::vector; %template(IntVector) std::vector; %template(BVector) std::vector; -// %template(DoubleVector) std::vector; +%template(DoubleVector) std::vector; %template(BSVector) std::vector>; %std_tuple(TupleDDB, double, double, bool); %std_tuple(TupleIntStr, int, std::string); diff --git a/sdk/src/protos/control.cc b/sdk/src/protos/control.cc index 01ac3d1..b8cc79c 100644 --- a/sdk/src/protos/control.cc +++ b/sdk/src/protos/control.cc @@ -198,7 +198,7 @@ namespace lebai bool TaskIndex::Serialize(rapidjson::Writer *writer) const { writer->StartObject(); - writer->Key("ids"); + writer->Key("id"); writer->Uint(id_); writer->EndObject(); return true; diff --git a/sdk/src/protos/dynamic.cc b/sdk/src/protos/dynamic.cc index caa017d..10f31f7 100644 --- a/sdk/src/protos/dynamic.cc +++ b/sdk/src/protos/dynamic.cc @@ -6,6 +6,41 @@ namespace lebai { namespace dynamic { + void SetMassRequest::set_mass(double mass) + { + mass_ = mass; + } + double SetMassRequest::mass() const + { + return mass_; + } + double *SetMassRequest::mutable_mass() + { + return &mass_; + } + bool SetMassRequest::Deserialize(const rapidjson::Value &obj) + { + if(obj.HasMember("mass")) + { + double mass = (double)(obj["mass"].GetDouble()); + mass_ = mass; + } + return true; + } + bool SetMassRequest::Serialize(rapidjson::Writer *writer) const + { + writer->StartObject(); + writer->Key("mass"); + writer->Double(mass_); + writer->EndObject(); + return true; + } + bool SetMassRequest::IsNullJSONData() const + { + return false; + } + + void SetPayloadRequest::set_mass(double mass) { mass_ = mass; @@ -19,15 +54,15 @@ namespace lebai return &mass_; } - void SetPayloadRequest::set_cog(std::vector cog) + void SetPayloadRequest::set_cog(posture::Position cog) { cog_ = cog; } - std::vector SetPayloadRequest::cog() const + posture::Position SetPayloadRequest::cog() const { return cog_; } - std::vector *SetPayloadRequest::mutable_cog() + posture::Position *SetPayloadRequest::mutable_cog() { return &cog_; } @@ -41,11 +76,10 @@ namespace lebai } if(obj.HasMember("cog")) { - std::vector cog; - for(auto c = obj["cog"].GetArray().Begin();c != obj["cog"].GetArray().End();c++) - { - cog.push_back(c->GetDouble()); - } + posture::Position cog; + cog.set_x(obj["cog"].GetObject()["x"].GetDouble()); + cog.set_y(obj["cog"].GetObject()["y"].GetDouble()); + cog.set_z(obj["cog"].GetObject()["z"].GetDouble()); cog_ = cog; } return true; @@ -56,12 +90,7 @@ namespace lebai writer->Key("mass"); writer->Double(mass_); writer->Key("cog"); - writer->StartArray(); - for(auto c:cog_) - { - writer->Double(c); - } - writer->EndArray(); + cog_.Serialize(writer); writer->EndObject(); return true; } @@ -69,6 +98,8 @@ namespace lebai { return false; } + + void Payload::set_mass(double mass) { mass_ = mass; @@ -82,15 +113,15 @@ namespace lebai return &mass_; } - void Payload::set_cog(std::vector cog) + void Payload::set_cog(posture::Position cog) { cog_ = cog; } - std::vector Payload::cog() const + posture::Position Payload::cog() const { return cog_; } - std::vector *Payload::mutable_cog() + posture::Position *Payload::mutable_cog() { return &cog_; } @@ -104,11 +135,10 @@ namespace lebai } if(obj.HasMember("cog")) { - std::vector cog; - for(auto c = obj["cog"].GetArray().Begin();c != obj["cog"].GetArray().End();c++) - { - cog.push_back(c->GetDouble()); - } + posture::Position cog; + cog.set_x(obj["cog"].GetObject()["x"].GetDouble()); + cog.set_y(obj["cog"].GetObject()["y"].GetDouble()); + cog.set_z(obj["cog"].GetObject()["z"].GetDouble()); cog_ = cog; } return true; @@ -119,12 +149,7 @@ namespace lebai writer->Key("mass"); writer->Double(mass_); writer->Key("cog"); - writer->StartArray(); - for(auto c:cog_) - { - writer->Double(c); - } - writer->EndArray(); + cog_.Serialize(writer); writer->EndObject(); return true; } diff --git a/sdk/src/protos/dynamic.hh b/sdk/src/protos/dynamic.hh index 9f2dadc..4733bd1 100644 --- a/sdk/src/protos/dynamic.hh +++ b/sdk/src/protos/dynamic.hh @@ -4,6 +4,7 @@ #include #include #include +#include "posture.hh" namespace lebai { @@ -16,13 +17,30 @@ namespace lebai double mass() const; double *mutable_mass(); - void set_cog(std::vector cog); - std::vector cog() const; - std::vector * mutable_cog(); + void set_cog(posture::Position cog); + posture::Position cog() const; + posture::Position * mutable_cog(); + + protected: + double mass_; + posture::Position cog_; + // These methods are used to serialize and deserialize the class. + // They will not be wrapped in the SDK. + public: + virtual bool Deserialize(const rapidjson::Value &obj); + virtual bool Serialize(rapidjson::Writer *writer) const; + virtual bool IsNullJSONData() const; + }; + + class SetMassRequest : public JSONBase + { + public: + void set_mass(double mass); + double mass() const; + double *mutable_mass(); protected: double mass_; - std::vector cog_; // These methods are used to serialize and deserialize the class. // They will not be wrapped in the SDK. public: @@ -38,13 +56,13 @@ namespace lebai double mass() const; double *mutable_mass(); - void set_cog(std::vector cog); - std::vector cog() const; - std::vector * mutable_cog(); + void set_cog(posture::Position cog); + posture::Position cog() const; + posture::Position * mutable_cog(); protected: double mass_; - std::vector cog_; + posture::Position cog_; // These methods are used to serialize and deserialize the class. // They will not be wrapped in the SDK. public: diff --git a/sdk/src/robot.cc b/sdk/src/robot.cc index 00eae35..a68f670 100644 --- a/sdk/src/robot.cc +++ b/sdk/src/robot.cc @@ -921,7 +921,7 @@ void Robot::add_signal(unsigned int index,int value) impl_->addSignal(req); } -unsigned int Robot::start_task(const std::string &name,bool is_parallel,unsigned int loop_to,const std::string & dir,const std::vector & params) +unsigned int Robot::start_task(const std::string &name,const std::vector & params,const std::string & dir, bool is_parallel,unsigned int loop_to) { control::StartTaskRequest req; req.set_name(name); @@ -932,16 +932,6 @@ unsigned int Robot::start_task(const std::string &name,bool is_parallel,unsigned control::TaskIndex resp = impl_->scene(req); return resp.id(); } -unsigned int Robot::start_task(const std::string &name,bool is_parallel,unsigned int loop_to,const std::string & dir) -{ - control::StartTaskRequest req; - req.set_name(name); - req.set_is_parallel(is_parallel); - req.set_loop_to(loop_to); - req.set_dir(dir); - control::TaskIndex resp = impl_->scene(req); - return resp.id(); -} unsigned int Robot::start_task(const std::string &name) { control::StartTaskRequest req; @@ -956,6 +946,14 @@ std::vector Robot::load_task_list() control::TaskIds resp = impl_->loadTaskList(); return resp.ids(); } +void Robot::pause_task(unsigned int id) +{ + control::PauseRequest req; + req.set_id(id); + req.set_time(0); + req.set_wait(false); + impl_->pauseTask(req); +} void Robot::pause_task(unsigned int id,unsigned long time,bool wait) { control::PauseRequest req; @@ -1315,23 +1313,37 @@ void Robot::set_payload(double mass, std::map cog) { dynamic::SetPayloadRequest req; req.set_mass(mass); - std::vector c; - c.push_back(cog.at("x")); - c.push_back(cog.at("y")); - c.push_back(cog.at("z")); + posture::Position c; + c.set_x(cog.at("x")); + c.set_y(cog.at("y")); + c.set_z(cog.at("z")); req.set_cog(c); impl_->setPayload(req); } +void Robot::set_payload(double mass) +{ + dynamic::SetMassRequest req; + req.set_mass(mass); + impl_->setPayload(req); +} +void Robot::set_payload(std::map cog) +{ + posture::Position req; + req.set_x(cog.at("x")); + req.set_y(cog.at("y")); + req.set_z(cog.at("z")); + impl_->setPayload(req); +} -std::tuple> Robot::get_payload() +std::map Robot::get_payload() { dynamic::Payload resp = impl_->getPayload(); std::map cog; - cog["x"] = resp.cog()[0]; - cog["y"] = resp.cog()[1]; - cog["Z"] = resp.cog()[2]; - std::tuple> ret = std::make_tuple(resp.mass(),cog); - return ret; + cog["x"] = resp.cog().x(); + cog["y"] = resp.cog().y(); + cog["Z"] = resp.cog().z(); + cog["mass"] = resp.mass(); + return cog; } void Robot::set_gravity(std::map gravity) diff --git a/sdk/src/robot_impl.cc b/sdk/src/robot_impl.cc index 04bae10..b20a602 100644 --- a/sdk/src/robot_impl.cc +++ b/sdk/src/robot_impl.cc @@ -495,6 +495,14 @@ namespace lebai { json_rpc_connector_->CallRpc("set_payload",req.ToJSONString(),nullptr); } + void Robot::RobotImpl::setPayload(const posture::Position &req) + { + json_rpc_connector_->CallRpc("set_payload",req.ToJSONString(),nullptr); + } + void Robot::RobotImpl::setPayload(const dynamic::SetMassRequest &req) + { + json_rpc_connector_->CallRpc("set_payload",req.ToJSONString(),nullptr); + } dynamic::Payload Robot::RobotImpl::getPayload() { std::string resp_str; diff --git a/sdk/src/robot_impl.hh b/sdk/src/robot_impl.hh index 34f2df6..fea8c3e 100644 --- a/sdk/src/robot_impl.hh +++ b/sdk/src/robot_impl.hh @@ -108,6 +108,8 @@ namespace lebai void unzip(const file::UnzipRequest & req); file::LoadZipListResponse loadZipList(const file::LoadZipListRequest & req); void setPayload(const dynamic::SetPayloadRequest & req); + void setPayload(const posture::Position & req); + void setPayload(const dynamic::SetMassRequest & req); dynamic::Payload getPayload(); void setGravity(const posture::Position & req); posture::Position getGravity();