Skip to content

Commit

Permalink
RSDK-3531 - Motion service wrappers (#141)
Browse files Browse the repository at this point in the history
  • Loading branch information
stuqdog authored Aug 18, 2023
1 parent 5b8d57a commit e92e449
Show file tree
Hide file tree
Showing 24 changed files with 1,820 additions and 76 deletions.
6 changes: 6 additions & 0 deletions codesamples/apis.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@
"func": "get_properties",
"args": []
},
"motion": {
"client": "MotionClient",
"func": "get_pose",
"args": ["{{\"rdk\", \"component\", \"fake\"}, \"\", \"component\"}", "<destination-frame>", "{}", "nullptr"]
"comment": "Update with correct component name, destination frame, supplemental transforms, and extra params"
},
"motor": {
"client": "MotorClient",
"func": "is_moving",
Expand Down
8 changes: 8 additions & 0 deletions src/viam/api/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,10 @@ if (VIAMCPPSDK_USE_DYNAMIC_PROTOS)
${PROTO_GEN_DIR}/service/mlmodel/v1/mlmodel.grpc.pb.h
${PROTO_GEN_DIR}/service/mlmodel/v1/mlmodel.pb.cc
${PROTO_GEN_DIR}/service/mlmodel/v1/mlmodel.pb.h
${PROTO_GEN_DIR}/service/motion/v1/motion.grpc.pb.cc
${PROTO_GEN_DIR}/service/motion/v1/motion.grpc.pb.h
${PROTO_GEN_DIR}/service/motion/v1/motion.pb.cc
${PROTO_GEN_DIR}/service/motion/v1/motion.pb.h
${PROTO_GEN_DIR}/tagger/v1/tagger.grpc.pb.cc
${PROTO_GEN_DIR}/tagger/v1/tagger.grpc.pb.h
${PROTO_GEN_DIR}/tagger/v1/tagger.pb.cc
Expand Down Expand Up @@ -234,6 +238,8 @@ target_sources(viamapi
${PROTO_GEN_DIR}/robot/v1/robot.pb.cc
${PROTO_GEN_DIR}/service/mlmodel/v1/mlmodel.grpc.pb.cc
${PROTO_GEN_DIR}/service/mlmodel/v1/mlmodel.pb.cc
${PROTO_GEN_DIR}/service/motion/v1/motion.grpc.pb.cc
${PROTO_GEN_DIR}/service/motion/v1/motion.pb.cc
${PROTO_GEN_DIR}/tagger/v1/tagger.grpc.pb.cc
${PROTO_GEN_DIR}/tagger/v1/tagger.pb.cc
PUBLIC FILE_SET viamapi_includes TYPE HEADERS
Expand Down Expand Up @@ -268,6 +274,8 @@ target_sources(viamapi
${PROTO_GEN_DIR}/../../viam/api/tagger/v1/tagger.grpc.pb.h
${PROTO_GEN_DIR}/../../viam/api/service/mlmodel/v1/mlmodel.grpc.pb.h
${PROTO_GEN_DIR}/../../viam/api/service/mlmodel/v1/mlmodel.pb.h
${PROTO_GEN_DIR}/../../viam/api/service/motion/v1/motion.grpc.pb.h
${PROTO_GEN_DIR}/../../viam/api/service/motion/v1/motion.pb.h
${PROTO_GEN_DIR}/../../viam/api/tagger/v1/tagger.pb.h
)

Expand Down
10 changes: 10 additions & 0 deletions src/viam/sdk/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,10 @@ target_sources(viamsdk
# be initialized within main before anything else happens?
registry/registry.cpp
common/linear_algebra.cpp
common/pose.cpp
common/proto_type.cpp
common/utils.cpp
common/world_state.cpp
components/base/base.cpp
components/base/client.cpp
components/base/server.cpp
Expand Down Expand Up @@ -83,6 +85,9 @@ target_sources(viamsdk
services/mlmodel/mlmodel.cpp
services/mlmodel/private/proto.cpp
services/mlmodel/server.cpp
services/motion/client.cpp
services/motion/motion.cpp
services/motion/server.cpp
services/service.cpp
spatialmath/geometry.cpp
spatialmath/orientation_types.cpp
Expand All @@ -92,8 +97,10 @@ target_sources(viamsdk
../..
FILES
../../viam/sdk/common/linear_algebra.hpp
../../viam/sdk/common/pose.hpp
../../viam/sdk/common/proto_type.hpp
../../viam/sdk/common/utils.hpp
../../viam/sdk/common/world_state.hpp
../../viam/sdk/components/base/base.hpp
../../viam/sdk/components/base/client.hpp
../../viam/sdk/components/base/server.hpp
Expand Down Expand Up @@ -131,6 +138,9 @@ target_sources(viamsdk
../../viam/sdk/services/mlmodel/client.hpp
../../viam/sdk/services/mlmodel/mlmodel.hpp
../../viam/sdk/services/mlmodel/server.hpp
../../viam/sdk/services/motion/client.hpp
../../viam/sdk/services/motion/motion.hpp
../../viam/sdk/services/motion/server.hpp
../../viam/sdk/services/service.hpp
../../viam/sdk/spatialmath/geometry.hpp
../../viam/sdk/spatialmath/orientation_types.hpp
Expand Down
48 changes: 48 additions & 0 deletions src/viam/sdk/common/pose.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include <viam/sdk/common/pose.hpp>

#include <common/v1/common.pb.h>

namespace viam {
namespace sdk {

common::v1::PoseInFrame pose_in_frame::to_proto() const {
common::v1::PoseInFrame pif;
*pif.mutable_reference_frame() = reference_frame;
common::v1::Pose proto_pose;
proto_pose.set_x(pose.coordinates.x);
proto_pose.set_y(pose.coordinates.y);
proto_pose.set_z(pose.coordinates.z);
proto_pose.set_o_x(pose.orientation.o_x);
proto_pose.set_o_y(pose.orientation.o_y);
proto_pose.set_o_z(pose.orientation.o_z);
proto_pose.set_theta(pose.theta);
*pif.mutable_pose() = std::move(proto_pose);
return pif;
};

pose_in_frame pose_in_frame::from_proto(const common::v1::PoseInFrame& proto) {
pose_in_frame pif;
pif.reference_frame = proto.reference_frame();
const auto& proto_pose = proto.pose();
pif.pose.orientation.o_x = proto_pose.o_x();
pif.pose.orientation.o_y = proto_pose.o_y();
pif.pose.orientation.o_z = proto_pose.o_z();
pif.pose.coordinates.x = proto_pose.x();
pif.pose.coordinates.y = proto_pose.y();
pif.pose.coordinates.z = proto_pose.z();
pif.pose.theta = proto_pose.theta();

return pif;
}

bool operator==(const pose_in_frame& lhs, const pose_in_frame& rhs) {
return lhs.pose == rhs.pose && lhs.reference_frame == rhs.reference_frame;
}
std::ostream& operator<<(std::ostream& os, const pose_in_frame& v) {
os << "{ pose: " << v.pose << ",\n"
<< " reference_frame: " << v.reference_frame << "}";
return os;
}

} // namespace sdk
} // namespace viam
44 changes: 44 additions & 0 deletions src/viam/sdk/common/pose.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#pragma once

#include <common/v1/common.pb.h>

namespace viam {
namespace sdk {

struct coordinates {
double x, y, z;
friend bool operator==(const coordinates& lhs, const coordinates& rhs);
};

struct pose_orientation {
double o_x, o_y, o_z;
friend bool operator==(const pose_orientation& lhs, const pose_orientation& rhs);
};

struct pose {
struct coordinates coordinates;
pose_orientation orientation;
double theta;

static pose from_proto(const viam::common::v1::Pose& proto);
viam::common::v1::Pose to_proto() const;

friend bool operator==(const pose& lhs, const pose& rhs);
friend std::ostream& operator<<(std::ostream& os, const pose& v);
};

struct pose_in_frame {
viam::common::v1::PoseInFrame to_proto() const;
static pose_in_frame from_proto(const viam::common::v1::PoseInFrame& proto);
pose_in_frame(std::string reference_frame_, struct pose pose_)
: reference_frame(std::move(reference_frame_)), pose(std::move(pose_)) {}
pose_in_frame() {}

std::string reference_frame;
struct pose pose;
friend bool operator==(const pose_in_frame& lhs, const pose_in_frame& rhs);
friend std::ostream& operator<<(std::ostream& os, const pose_in_frame& v);
};

} // namespace sdk
} // namespace viam
4 changes: 4 additions & 0 deletions src/viam/sdk/common/proto_type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ using google::protobuf::Value;
// NOLINTNEXTLINE(misc-no-recursion)
Struct map_to_struct(AttributeMap dict) {
Struct s;
if (!dict) {
return s;
}

for (const auto& key_and_value : *dict) {
const std::string key = key_and_value.first;
const Value value = key_and_value.second->proto_value();
Expand Down
98 changes: 98 additions & 0 deletions src/viam/sdk/common/world_state.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
#include <viam/sdk/common/world_state.hpp>

#include <common/v1/common.pb.h>

#include <viam/sdk/common/pose.hpp>

namespace viam {
namespace sdk {

WorldState::geometries_in_frame WorldState::geometries_in_frame::from_proto(
const common::v1::GeometriesInFrame& proto) {
geometries_in_frame gif;
for (const auto& geo : proto.geometries()) {
gif.geometries.push_back(GeometryConfig::from_proto(geo));
}
gif.reference_frame = proto.reference_frame();

return gif;
}

common::v1::GeometriesInFrame WorldState::geometries_in_frame::to_proto() const {
common::v1::GeometriesInFrame proto;

*proto.mutable_reference_frame() = reference_frame;
for (const auto& geometry : geometries) {
*proto.mutable_geometries()->Add() = geometry.to_proto();
}

return proto;
}

WorldState WorldState::from_proto(const common::v1::WorldState& ws) {
const auto& proto_obstacles = ws.obstacles();
std::vector<geometries_in_frame> obstacles;
for (const auto& po : proto_obstacles) {
obstacles.push_back(geometries_in_frame::from_proto(po));
}

const auto& proto_transforms = ws.transforms();
std::vector<transform> transforms;
for (const auto& pt : proto_transforms) {
transforms.push_back(WorldState::transform::from_proto(pt));
}

return {obstacles, transforms};
}

common::v1::WorldState WorldState::to_proto() const {
common::v1::WorldState proto_ws;
for (const auto& obstacle : obstacles_) {
*proto_ws.mutable_obstacles()->Add() = obstacle.to_proto();
}
for (const auto& transform : transforms_) {
*proto_ws.mutable_transforms()->Add() = transform.to_proto();
}

return proto_ws;
}

WorldState::transform WorldState::transform::from_proto(const common::v1::Transform& proto) {
WorldState::transform transform;
transform.reference_frame = proto.reference_frame();
transform.pose_in_observer_frame = pose_in_frame::from_proto(proto.pose_in_observer_frame());
if (proto.has_physical_object()) {
transform.physical_object =
std::make_shared<GeometryConfig>(GeometryConfig::from_proto(proto.physical_object()));
}

return transform;
}

common::v1::Transform WorldState::transform::to_proto() const {
common::v1::Transform proto;
*proto.mutable_reference_frame() = reference_frame;
*proto.mutable_pose_in_observer_frame() = pose_in_observer_frame.to_proto();
if (physical_object) {
*proto.mutable_physical_object() = physical_object->to_proto();
}

return proto;
}
bool operator==(const WorldState::geometries_in_frame& lhs,
const WorldState::geometries_in_frame& rhs) {
return lhs.reference_frame == rhs.reference_frame && lhs.geometries == rhs.geometries;
}
bool operator==(const WorldState::transform& lhs, const WorldState::transform& rhs) {
return lhs.reference_frame == rhs.reference_frame &&
(lhs.physical_object == rhs.physical_object ||
*lhs.physical_object == *rhs.physical_object) &&
lhs.pose_in_observer_frame == rhs.pose_in_observer_frame;
}

bool operator==(const WorldState& lhs, const WorldState& rhs) {
return lhs.obstacles_ == rhs.obstacles_ && lhs.transforms_ == rhs.transforms_;
}

} // namespace sdk
} // namespace viam
48 changes: 48 additions & 0 deletions src/viam/sdk/common/world_state.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#pragma once

#include <common/v1/common.pb.h>

#include <viam/sdk/common/pose.hpp>
#include <viam/sdk/spatialmath/geometry.hpp>

namespace viam {
namespace sdk {

// TODO(RSDK-4553) Add documentation for these types
class WorldState {
public:
struct geometries_in_frame {
std::vector<GeometryConfig> geometries;
std::string reference_frame;
common::v1::GeometriesInFrame to_proto() const;
static geometries_in_frame from_proto(const common::v1::GeometriesInFrame& proto);
};

struct transform {
std::string reference_frame;
pose_in_frame pose_in_observer_frame;
std::shared_ptr<GeometryConfig> physical_object;

common::v1::Transform to_proto() const;
static transform from_proto(const common::v1::Transform& proto);
transform() {}
};

common::v1::WorldState to_proto() const;
static WorldState from_proto(const common::v1::WorldState& ws);

WorldState() {}
WorldState(std::vector<geometries_in_frame> obstacles, std::vector<transform> transforms)
: obstacles_(obstacles), transforms_(transforms) {}

friend bool operator==(const WorldState& lhs, const WorldState& rhs);
friend bool operator==(const geometries_in_frame& lhs, const geometries_in_frame& rhs);
friend bool operator==(const transform& lhs, const transform& rhs);

private:
std::vector<geometries_in_frame> obstacles_;
std::vector<transform> transforms_;
};

} // namespace sdk
} // namespace viam
Loading

0 comments on commit e92e449

Please sign in to comment.