-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #411 from agri-gaia/example/cppTfQuery
add cpp example for tf query
- Loading branch information
Showing
3 changed files
with
152 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#ifndef SEEREP_CPP_EXAMPLES_TF_QUERY_H_ | ||
#define SEEREP_CPP_EXAMPLES_TF_QUERY_H_ | ||
|
||
#include <flatbuffers/grpc.h> | ||
#include <grpcpp/grpcpp.h> | ||
#include <seerep_com/meta_operations.grpc.fb.h> | ||
#include <seerep_com/tf_service.grpc.fb.h> | ||
|
||
#include <optional> | ||
|
||
namespace seerep_cpp_examples | ||
{ | ||
|
||
class TfQuery | ||
{ | ||
public: | ||
TfQuery(std::shared_ptr<grpc::Channel> channel); | ||
~TfQuery() = default; | ||
void getTf(const std::string& projectUUID, const std::string& childFrame, | ||
const std::string& parentFrame, const int& seconds, | ||
const int& nanos); | ||
|
||
private: | ||
std::optional<std::string> getProjectUUID(const std::string& projectName); | ||
flatbuffers::grpc::Message<seerep::fb::TransformStampedQuery> | ||
createQuery(const std::string& projectUUID, const std::string& childFrame, | ||
const std::string& parentFrame, const int& seconds, | ||
const int& nanos); | ||
|
||
std::shared_ptr<grpc::Channel> channel_; | ||
flatbuffers::grpc::MessageBuilder fbb_; | ||
}; | ||
|
||
} // namespace seerep_cpp_examples | ||
|
||
#endif // SEEREP_CPP_EXAMPLES_TF_QUERY_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
|
||
#include "../include/tf_query.h" | ||
|
||
namespace seerep_cpp_examples | ||
{ | ||
|
||
TfQuery::TfQuery(std::shared_ptr<grpc::Channel> channel) | ||
: channel_(channel), fbb_(flatbuffers::grpc::MessageBuilder()) | ||
{ | ||
} | ||
|
||
std::optional<std::string> | ||
TfQuery::getProjectUUID(const std::string& projectName) | ||
{ | ||
auto metaStub = seerep::fb::MetaOperations::NewStub(channel_); | ||
auto requestOffset = seerep::fb::CreateEmpty(fbb_); | ||
fbb_.Finish(requestOffset); | ||
auto requestMsg = fbb_.ReleaseMessage<seerep::fb::Empty>(); | ||
|
||
grpc::ClientContext context; | ||
flatbuffers::grpc::Message<seerep::fb::ProjectInfos> responseMsg; | ||
auto status = metaStub->GetProjects(&context, requestMsg, &responseMsg); | ||
|
||
if (status.ok()) | ||
{ | ||
auto response = responseMsg.GetRoot(); | ||
for (const auto& project : *response->projects()) | ||
{ | ||
if (project->name()->str() == projectName) | ||
{ | ||
return project->uuid()->str(); | ||
} | ||
} | ||
} | ||
else | ||
{ | ||
throw std::runtime_error(status.error_message()); | ||
} | ||
return std::nullopt; | ||
} | ||
|
||
flatbuffers::grpc::Message<seerep::fb::TransformStampedQuery> | ||
TfQuery::createQuery(const std::string& projectUUID, | ||
const std::string& childFrame, | ||
const std::string& parentFrame, const int& seconds, | ||
const int& nanos) | ||
{ | ||
auto projectUUIDOffst = fbb_.CreateString(projectUUID); | ||
auto childFrameOffset = fbb_.CreateString(childFrame); | ||
auto parentFrameOffset = fbb_.CreateString(parentFrame); | ||
|
||
seerep::fb::TimestampBuilder timestampBuilder(fbb_); | ||
timestampBuilder.add_seconds(seconds); | ||
timestampBuilder.add_nanos(nanos); | ||
auto timestampOffset = timestampBuilder.Finish(); | ||
|
||
seerep::fb::HeaderBuilder headerBuilder(fbb_); | ||
headerBuilder.add_frame_id(parentFrameOffset); | ||
headerBuilder.add_stamp(timestampOffset); | ||
headerBuilder.add_uuid_project(projectUUIDOffst); | ||
auto headerOffset = headerBuilder.Finish(); | ||
|
||
seerep::fb::TransformStampedQueryBuilder queryBuilder(fbb_); | ||
queryBuilder.add_header(headerOffset); | ||
queryBuilder.add_child_frame_id(childFrameOffset); | ||
/* add extra query parameters here */ | ||
auto requestOffset = queryBuilder.Finish(); | ||
fbb_.Finish(requestOffset); | ||
return fbb_.ReleaseMessage<seerep::fb::TransformStampedQuery>(); | ||
} | ||
|
||
void TfQuery::getTf(const std::string& projectName, | ||
const std::string& childFrame, | ||
const std::string& parentFrame, const int& seconds, | ||
const int& nanos) | ||
{ | ||
auto tfStub = seerep::fb::TfService::NewStub(channel_); | ||
std::optional<std::string> projectUUID = getProjectUUID(projectName); | ||
if (projectUUID.has_value()) | ||
{ | ||
grpc::ClientContext context; | ||
flatbuffers::grpc::Message<seerep::fb::TransformStamped> responseMsg; | ||
tfStub->GetTransformStamped(&context, | ||
createQuery(projectUUID.value(), childFrame, | ||
parentFrame, seconds, nanos), | ||
&responseMsg); | ||
|
||
auto response = responseMsg.GetRoot(); | ||
/* do something usefull with the images matching the query */ | ||
std::cout << "Received tf with childframe: " | ||
<< response->child_frame_id()->str() << std::endl; | ||
} | ||
else | ||
{ | ||
throw std::runtime_error("Project not found"); | ||
} | ||
} | ||
|
||
} // namespace seerep_cpp_examples | ||
|
||
int main() | ||
{ | ||
std::shared_ptr<grpc::Channel> grpcChannel = | ||
grpc::CreateChannel("localhost:9090", grpc::InsecureChannelCredentials()); | ||
seerep_cpp_examples::TfQuery TfQuery(grpcChannel); | ||
TfQuery.getTf("testproject", "map", "camera", 1661336508, 0); | ||
} |