diff --git a/src/viam/examples/dial/example_dial.cpp b/src/viam/examples/dial/example_dial.cpp index 8f18e264b..cae58e0b3 100644 --- a/src/viam/examples/dial/example_dial.cpp +++ b/src/viam/examples/dial/example_dial.cpp @@ -13,16 +13,11 @@ #include #include -#include -#include -#include - #include #include #include #include -using viam::robot::v1::Status; using namespace viam::sdk; int main() { diff --git a/src/viam/examples/dial_api_key/example_dial_api_key.cpp b/src/viam/examples/dial_api_key/example_dial_api_key.cpp index d0a5b7dac..d67efbb4f 100644 --- a/src/viam/examples/dial_api_key/example_dial_api_key.cpp +++ b/src/viam/examples/dial_api_key/example_dial_api_key.cpp @@ -14,10 +14,6 @@ #include #include -#include -#include -#include - #include #include #include diff --git a/src/viam/examples/modules/complex/client.cpp b/src/viam/examples/modules/complex/client.cpp index 894fd5c11..6ded78722 100644 --- a/src/viam/examples/modules/complex/client.cpp +++ b/src/viam/examples/modules/complex/client.cpp @@ -1,5 +1,4 @@ #include -#include #include #include #include @@ -12,10 +11,6 @@ #include #include -#include -#include -#include - #include #include #include diff --git a/src/viam/examples/modules/complex/main.cpp b/src/viam/examples/modules/complex/main.cpp index 9b746bfff..59f590733 100644 --- a/src/viam/examples/modules/complex/main.cpp +++ b/src/viam/examples/modules/complex/main.cpp @@ -5,10 +5,6 @@ #include #include -#include -#include -#include - #include #include #include diff --git a/src/viam/examples/modules/complex/proto/buf.lock b/src/viam/examples/modules/complex/proto/buf.lock index 603482e23..1f8e2b014 100644 --- a/src/viam/examples/modules/complex/proto/buf.lock +++ b/src/viam/examples/modules/complex/proto/buf.lock @@ -4,5 +4,5 @@ deps: - remote: buf.build owner: googleapis repository: googleapis - commit: e7f8d366f5264595bcc4cd4139af9973 - digest: shake256:e5e5f1c12f82e028ea696faa43b4f9dc6258a6d1226282962a8c8b282e10946281d815884f574bd279ebd9cd7588629beb3db17b892af6c33b56f92f8f67f509 + commit: 553fd4b4b3a640be9b69a3fa0c17b383 + digest: shake256:e30e3247f84b7ff9d09941ce391eb4b6f04734e1e5fae796bfc471f167e6f90813630cc39397ee46b8bc0ea7d6935c416d15c219cc5732d9778cbfdf73a1ed6e diff --git a/src/viam/examples/modules/complex/test_complex_module.cpp b/src/viam/examples/modules/complex/test_complex_module.cpp index 9845ab5c0..4984df8f0 100644 --- a/src/viam/examples/modules/complex/test_complex_module.cpp +++ b/src/viam/examples/modules/complex/test_complex_module.cpp @@ -7,8 +7,6 @@ #include -#include - #include #include diff --git a/src/viam/examples/modules/simple/main.cpp b/src/viam/examples/modules/simple/main.cpp index edc400347..c19904a57 100644 --- a/src/viam/examples/modules/simple/main.cpp +++ b/src/viam/examples/modules/simple/main.cpp @@ -6,10 +6,6 @@ #include #include -#include -#include -#include - #include #include #include diff --git a/src/viam/sdk/CMakeLists.txt b/src/viam/sdk/CMakeLists.txt index fd38c196e..9f1a70442 100644 --- a/src/viam/sdk/CMakeLists.txt +++ b/src/viam/sdk/CMakeLists.txt @@ -50,6 +50,7 @@ target_sources(viamsdk common/exception.cpp common/linear_algebra.cpp common/pose.cpp + common/private/proto_conversions.cpp common/proto_value.cpp common/service_helper.cpp common/utils.cpp @@ -76,6 +77,7 @@ target_sources(viamsdk components/private/board_server.cpp components/private/camera_client.cpp components/private/camera_server.cpp + components/private/encoder.cpp components/private/encoder_client.cpp components/private/encoder_server.cpp components/private/gantry_client.cpp diff --git a/src/viam/sdk/common/client_helper.hpp b/src/viam/sdk/common/client_helper.hpp index d8db1f338..97ca0edef 100644 --- a/src/viam/sdk/common/client_helper.hpp +++ b/src/viam/sdk/common/client_helper.hpp @@ -4,6 +4,7 @@ #include #include +#include #include #include #include diff --git a/src/viam/sdk/common/linear_algebra.cpp b/src/viam/sdk/common/linear_algebra.cpp index afd7c5b8c..f9fe64d84 100644 --- a/src/viam/sdk/common/linear_algebra.cpp +++ b/src/viam/sdk/common/linear_algebra.cpp @@ -2,8 +2,6 @@ #include -#include - namespace viam { namespace sdk { @@ -45,17 +43,5 @@ std::array& Vector3::data() { return this->data_; } -viam::common::v1::Vector3 Vector3::to_proto() const { - viam::common::v1::Vector3 result; - result.set_x(x()); - result.set_y(y()); - result.set_z(z()); - return result; -}; - -Vector3 Vector3::from_proto(const viam::common::v1::Vector3& vec) { - return {vec.x(), vec.y(), vec.z()}; -} - } // namespace sdk } // namespace viam diff --git a/src/viam/sdk/common/linear_algebra.hpp b/src/viam/sdk/common/linear_algebra.hpp index 6af24fe1b..02133f8ea 100644 --- a/src/viam/sdk/common/linear_algebra.hpp +++ b/src/viam/sdk/common/linear_algebra.hpp @@ -5,7 +5,6 @@ #include #include -#include namespace viam { namespace sdk { @@ -30,8 +29,6 @@ class Vector3 { const std::array& data() const; std::array& data(); - viam::common::v1::Vector3 to_proto() const; - static Vector3 from_proto(const viam::common::v1::Vector3& vec); private: std::array data_; diff --git a/src/viam/sdk/common/pose.cpp b/src/viam/sdk/common/pose.cpp index 521d3127e..34db5bf39 100644 --- a/src/viam/sdk/common/pose.cpp +++ b/src/viam/sdk/common/pose.cpp @@ -1,40 +1,8 @@ #include -#include - 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; } diff --git a/src/viam/sdk/common/pose.hpp b/src/viam/sdk/common/pose.hpp index b97353dc8..b6663693c 100644 --- a/src/viam/sdk/common/pose.hpp +++ b/src/viam/sdk/common/pose.hpp @@ -1,6 +1,6 @@ #pragma once -#include +#include namespace viam { namespace sdk { @@ -20,16 +20,11 @@ struct pose { 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() {} diff --git a/src/viam/sdk/common/private/client_helper.hpp b/src/viam/sdk/common/private/client_helper.hpp new file mode 100644 index 000000000..2a77eba31 --- /dev/null +++ b/src/viam/sdk/common/private/client_helper.hpp @@ -0,0 +1 @@ +#include diff --git a/src/viam/sdk/common/private/proto_conversions.cpp b/src/viam/sdk/common/private/proto_conversions.cpp new file mode 100644 index 000000000..2f3a99a1d --- /dev/null +++ b/src/viam/sdk/common/private/proto_conversions.cpp @@ -0,0 +1,651 @@ +#include + +#include +#include +#include + +#include +#include + +namespace viam { +namespace sdk { +namespace impl { + +viam::common::v1::Vector3 to_proto(const Vector3& v3) { + viam::common::v1::Vector3 result; + result.set_x(v3.x()); + result.set_y(v3.y()); + result.set_z(v3.z()); + return result; +}; + +Vector3 from_proto(const viam::common::v1::Vector3& vec) { + return {vec.x(), vec.y(), vec.z()}; +} + +response_metadata from_proto(const viam::common::v1::ResponseMetadata& proto) { + response_metadata metadata; + metadata.captured_at = timestamp_to_time_pt(proto.captured_at()); + return metadata; +} + +viam::common::v1::ResponseMetadata to_proto(const response_metadata& metadata) { + viam::common::v1::ResponseMetadata proto; + google::protobuf::Timestamp ts = time_pt_to_timestamp(metadata.captured_at); + *proto.mutable_captured_at() = std::move(ts); + return proto; +} + +std::chrono::microseconds from_proto(const google::protobuf::Duration& proto) { + namespace sc = std::chrono; + const sc::seconds seconds_part{proto.seconds()}; + const sc::nanoseconds nanos_part{proto.nanos()}; + + const sc::microseconds from_seconds = sc::duration_cast(seconds_part); + sc::microseconds from_nanos = sc::duration_cast(nanos_part); + + if ((nanos_part.count() < 0) && (from_nanos > nanos_part)) { + from_nanos -= sc::microseconds(1); + } else if ((nanos_part.count() > 0) && (from_nanos < nanos_part)) { + from_nanos += sc::microseconds(1); + } + return from_seconds + from_nanos; +} + +google::protobuf::Duration to_proto(const std::chrono::microseconds& duration) { + namespace sc = std::chrono; + + const sc::seconds seconds = sc::duration_cast(duration); + const sc::nanoseconds nanos = duration - seconds; + + google::protobuf::Duration proto; + proto.set_nanos(static_cast(nanos.count())); + proto.set_seconds(seconds.count()); + return proto; +} + +viam::module::v1::HandlerMap to_proto(const HandlerMap_& hm) { + viam::module::v1::HandlerMap proto; + for (const auto& h : hm.handles()) { + viam::module::v1::HandlerDefinition hd; + for (const auto& model : h.second) { + const std::string m = model.to_string(); + *hd.mutable_models()->Add() = m; + } + viam::robot::v1::ResourceRPCSubtype rpc_subtype; + const Name name(h.first.api(), "", ""); + const viam::common::v1::ResourceName resource_name = to_proto(name); + *rpc_subtype.mutable_subtype() = resource_name; + *rpc_subtype.mutable_proto_service() = h.first.proto_service_name(); + *hd.mutable_subtype() = rpc_subtype; + *proto.add_handlers() = hd; + } + + return proto; +}; + +// NOLINTNEXTLINE(readability-const-return-type) +HandlerMap_ from_proto(const viam::module::v1::HandlerMap& proto) { + HandlerMap_ hm; + + const google::protobuf::RepeatedPtrField& handlers = + proto.handlers(); + + for (const auto& handler : handlers) { + const viam::common::v1::ResourceName name = handler.subtype().subtype(); + const API api(name.namespace_(), name.type(), name.subtype()); + const google::protobuf::DescriptorPool* pool = + google::protobuf::DescriptorPool::generated_pool(); + const google::protobuf::ServiceDescriptor* sd = pool->FindServiceByName(name.type()); + const RPCSubtype handle(api, *sd); + for (const auto& mod : handler.models()) { + try { + const Model model = Model::from_str(mod); + hm.add_model(model, handle); + } catch (std::string error) { // NOLINT + BOOST_LOG_TRIVIAL(error) << "Error processing model " + mod; + } + } + } + + return hm; +} + +viam::app::v1::ComponentConfig to_proto(const ResourceConfig& cfg) { + viam::app::v1::ComponentConfig proto_cfg; + const google::protobuf::RepeatedPtrField + service_configs; + + for (const auto& svc_cfg : cfg.service_config()) { + viam::app::v1::ResourceLevelServiceConfig cfg; + *cfg.mutable_type() = svc_cfg.type; + *cfg.mutable_attributes() = map_to_struct(svc_cfg.attributes); + *proto_cfg.mutable_service_configs()->Add() = cfg; + } + + *proto_cfg.mutable_name() = cfg.name(); + *proto_cfg.mutable_namespace_() = cfg.namespace_(); + *proto_cfg.mutable_type() = cfg.type(); + *proto_cfg.mutable_api() = cfg.api().to_string(); + *proto_cfg.mutable_model() = cfg.model().to_string(); + *proto_cfg.mutable_attributes() = map_to_struct(cfg.attributes()); + for (const auto& dep : cfg.depends_on()) { + *proto_cfg.mutable_depends_on()->Add() = dep; + } + *proto_cfg.mutable_frame() = to_proto(cfg.frame()); + + return proto_cfg; +} + +ResourceConfig from_proto(const viam::app::v1::ComponentConfig& proto_cfg) { + LinkConfig frame; + if (proto_cfg.has_frame()) { + frame = from_proto(proto_cfg.frame()); + } + + return ResourceConfig(proto_cfg.type(), + proto_cfg.name(), + proto_cfg.namespace_(), + struct_to_map(proto_cfg.attributes()), + proto_cfg.api(), + Model::from_str(proto_cfg.model()), + frame); +}; + +WorldState::geometries_in_frame from_proto(const common::v1::GeometriesInFrame& proto) { + WorldState::geometries_in_frame gif; + for (const auto& geo : proto.geometries()) { + gif.geometries.push_back(from_proto(geo)); + } + gif.reference_frame = proto.reference_frame(); + + return gif; +} + +common::v1::GeometriesInFrame to_proto(const WorldState::geometries_in_frame& gif) { + common::v1::GeometriesInFrame proto; + + *proto.mutable_reference_frame() = gif.reference_frame; + for (const auto& geometry : gif.geometries) { + *proto.mutable_geometries()->Add() = to_proto(geometry); + } + + return proto; +} + +WorldState::transform from_proto(const common::v1::Transform& proto) { + WorldState::transform transform; + transform.reference_frame = proto.reference_frame(); + transform.pose_in_observer_frame = from_proto(proto.pose_in_observer_frame()); + if (proto.has_physical_object()) { + transform.physical_object = + std::make_shared(from_proto(proto.physical_object())); + } + + return transform; +} + +common::v1::Transform to_proto(const WorldState::transform& transform) { + common::v1::Transform proto; + *proto.mutable_reference_frame() = transform.reference_frame; + *proto.mutable_pose_in_observer_frame() = to_proto(transform.pose_in_observer_frame); + if (transform.physical_object) { + *proto.mutable_physical_object() = to_proto(*transform.physical_object); + } + + return proto; +} + +WorldState from_proto(const common::v1::WorldState& ws) { + const auto& proto_obstacles = ws.obstacles(); + std::vector obstacles; + for (const auto& po : proto_obstacles) { + obstacles.push_back(from_proto(po)); + } + + const auto& proto_transforms = ws.transforms(); + std::vector transforms; + for (const auto& pt : proto_transforms) { + transforms.push_back(from_proto(pt)); + } + + return {obstacles, transforms}; +} + +common::v1::WorldState to_proto(const WorldState& ws) { + common::v1::WorldState proto_ws; + for (const auto& obstacle : ws.obstacles()) { + *proto_ws.mutable_obstacles()->Add() = to_proto(obstacle); + } + for (const auto& transform : ws.transforms()) { + *proto_ws.mutable_transforms()->Add() = to_proto(transform); + } + + return proto_ws; +} + +viam::common::v1::Pose to_proto(const pose& pose) { + viam::common::v1::Pose proto; + proto.set_x(pose.coordinates.x); + proto.set_y(pose.coordinates.y); + proto.set_z(pose.coordinates.z); + proto.set_o_x(pose.orientation.o_x); + proto.set_o_y(pose.orientation.o_y); + proto.set_o_z(pose.orientation.o_z); + proto.set_theta(pose.theta); + + return proto; +} + +pose from_proto(const viam::common::v1::Pose& proto) { + struct pose pose; + pose.coordinates.x = proto.x(); + pose.coordinates.y = proto.y(); + pose.coordinates.z = proto.z(); + pose.orientation.o_x = proto.o_x(); + pose.orientation.o_y = proto.o_y(); + pose.orientation.o_z = proto.o_z(); + pose.theta = proto.theta(); + + return pose; +} + +common::v1::PoseInFrame to_proto(const pose_in_frame& pif) { + common::v1::PoseInFrame proto_pif; + *proto_pif.mutable_reference_frame() = pif.reference_frame; + common::v1::Pose proto_pose; + proto_pose.set_x(pif.pose.coordinates.x); + proto_pose.set_y(pif.pose.coordinates.y); + proto_pose.set_z(pif.pose.coordinates.z); + proto_pose.set_o_x(pif.pose.orientation.o_x); + proto_pose.set_o_y(pif.pose.orientation.o_y); + proto_pose.set_o_z(pif.pose.orientation.o_z); + proto_pose.set_theta(pif.pose.theta); + *proto_pif.mutable_pose() = std::move(proto_pose); + return proto_pif; +}; + +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; +} + +viam::common::v1::Sphere sphere_proto(const GeometryConfig& cfg) { + try { + viam::common::v1::Sphere sphere; + const auto sphere_specifics = boost::get(cfg.get_geometry_specifics()); + sphere.set_radius_mm(sphere_specifics.radius); + return sphere; + } catch (...) { + throw Exception( + "Couldn't convert geometry config to sphere proto; sphere specifics not found"); + } +} + +viam::common::v1::RectangularPrism box_proto(const GeometryConfig& cfg) { + try { + const auto box_specifics = boost::get(cfg.get_geometry_specifics()); + viam::common::v1::RectangularPrism box; + viam::common::v1::Vector3 vec3; + vec3.set_x(box_specifics.x); + vec3.set_y(box_specifics.y); + vec3.set_z(box_specifics.z); + *box.mutable_dims_mm() = vec3; + return box; + } catch (...) { + throw Exception("Couldn't convert geometry config to box proto; box specifics not found"); + } +} + +viam::common::v1::Capsule capsule_proto(const GeometryConfig& cfg) { + try { + const auto capsule_specifics = boost::get(cfg.get_geometry_specifics()); + viam::common::v1::Capsule capsule; + capsule.set_radius_mm(capsule_specifics.radius); + capsule.set_length_mm(capsule_specifics.length); + return capsule; + } catch (...) { + throw Exception( + "Couldn't convert geometry config to capsule proto; capsule specifics not found"); + } +} + +GeometryConfig from_proto(const viam::common::v1::Geometry& proto) { + GeometryConfig cfg; + const auto& pose = proto.center(); + cfg.set_pose(::viam::sdk::impl::from_proto(pose)); + cfg.set_label(proto.label()); + + switch (proto.geometry_type_case()) { + case viam::common::v1::Geometry::GeometryTypeCase::kBox: { + cfg.set_geometry_type(GeometryType::box); + struct box box; + box.x = proto.box().dims_mm().x(); + box.y = proto.box().dims_mm().y(); + box.z = proto.box().dims_mm().z(); + cfg.set_geometry_specifics(box); + return cfg; + } + case viam::common::v1::Geometry::GeometryTypeCase::kSphere: { + auto r = proto.sphere().radius_mm(); + if (r == 0) { + cfg.set_geometry_type(GeometryType::point); + } else { + cfg.set_geometry_type(GeometryType::sphere); + } + struct sphere sphere({r}); + cfg.set_geometry_specifics(sphere); + return cfg; + } + case viam::common::v1::Geometry::GeometryTypeCase::kCapsule: { + cfg.set_geometry_type(GeometryType::capsule); + struct capsule capsule; + capsule.radius = proto.capsule().radius_mm(); + capsule.length = proto.capsule().length_mm(); + cfg.set_geometry_specifics(capsule); + return cfg; + } + case viam::common::v1::Geometry::GeometryTypeCase::GEOMETRY_TYPE_NOT_SET: + default: { + throw Exception(ErrorCondition::k_not_supported, "Geometry type is not supported"); + } + } +} +std::vector from_proto(const viam::common::v1::GetGeometriesResponse& proto) { + std::vector response; + for (const auto& geometry : proto.geometries()) { + response.push_back(from_proto(geometry)); + } + return response; +} + +viam::common::v1::Geometry to_proto(const GeometryConfig& cfg) { + viam::common::v1::Geometry geometry_; + *geometry_.mutable_label() = cfg.get_label(); + *geometry_.mutable_center() = to_proto(cfg.get_pose()); + switch (cfg.get_geometry_type()) { + case GeometryType::box: { + *geometry_.mutable_box() = box_proto(cfg); + return geometry_; + } + case GeometryType::sphere: { + *geometry_.mutable_sphere() = sphere_proto(cfg); + return geometry_; + } + case point: { + viam::common::v1::Sphere sphere; + sphere.set_radius_mm(0); + *geometry_.mutable_sphere() = sphere; + return geometry_; + } + case capsule: { + *geometry_.mutable_capsule() = capsule_proto(cfg); + return geometry_; + } + case unknown: + default: { + auto pose = cfg.get_pose(); + if (pose.coordinates.x == 0 && pose.coordinates.y == 0 && pose.coordinates.z == 0) { + *geometry_.mutable_box() = box_proto(cfg); + } else { + *geometry_.mutable_sphere() = sphere_proto(cfg); + } + return geometry_; + } + } +} + +common::v1::GeoPoint to_proto(const geo_point& gp) { + common::v1::GeoPoint proto; + proto.set_latitude(gp.latitude); + proto.set_longitude(gp.longitude); + + return proto; +} + +geo_point from_proto(const common::v1::GeoPoint& proto) { + struct geo_point geo_point; + geo_point.latitude = proto.latitude(); + geo_point.longitude = proto.longitude(); + + return geo_point; +} + +common::v1::GeoGeometry to_proto(const geo_geometry& gg) { + common::v1::GeoGeometry proto; + *proto.mutable_location() = to_proto(gg.location); + + for (const auto& geometry : gg.geometries) { + *proto.mutable_geometries()->Add() = to_proto(geometry); + } + + return proto; +} + +geo_geometry from_proto(const common::v1::GeoGeometry& proto) { + struct geo_geometry geo_geometry; + + geo_geometry.location = from_proto(proto.location()); + for (const auto& proto_geometry : proto.geometries()) { + auto geometry = from_proto(proto_geometry); + geo_geometry.geometries.push_back(std::move(geometry)); + } + + return geo_geometry; +} + +OrientationConfig from_proto(const viam::app::v1::Orientation& proto) { + namespace p = viam::app::v1; + switch (proto.type_case()) { + case p::Orientation::TypeCase::kAxisAngles: { + auto type = AxisAngles; + axis_angles aa; + aa.x = proto.axis_angles().x(); + aa.y = proto.axis_angles().y(); + aa.z = proto.axis_angles().z(); + aa.theta = proto.axis_angles().theta(); + return {type, {}, std::move(aa)}; + } + case p::Orientation::TypeCase::kEulerAngles: { + auto type = EulerAngles; + euler_angles ea; + ea.yaw = proto.euler_angles().yaw(); + ea.pitch = proto.euler_angles().pitch(); + ea.roll = proto.euler_angles().roll(); + return {type, {}, std::move(ea)}; + } + case p::Orientation::TypeCase::kQuaternion: { + auto type = Quaternion; + quaternion quat; + quat.w = proto.quaternion().w(); + quat.x = proto.quaternion().x(); + quat.y = proto.quaternion().y(); + quat.z = proto.quaternion().z(); + return {type, {}, std::move(quat)}; + } + case p::Orientation::TypeCase::kVectorDegrees: { + auto type = OrientationVectorDegrees; + orientation_vector_degrees ovd; + ovd.x = proto.vector_degrees().x(); + ovd.y = proto.vector_degrees().y(); + ovd.z = proto.vector_degrees().z(); + ovd.theta = proto.vector_degrees().theta(); + return {type, {}, std::move(ovd)}; + } + case p::Orientation::TypeCase::kVectorRadians: { + auto type = OrientationVector; + orientation_vector ov; + ov.x = proto.vector_radians().x(); + ov.y = proto.vector_radians().y(); + ov.z = proto.vector_radians().z(); + ov.theta = proto.vector_radians().theta(); + return {type, {}, std::move(ov)}; + } + case p::Orientation::TypeCase::kNoOrientation: { + // if type is NoOrientation, we put a sentinel + // orientation that indicates no rotation + auto type = Quaternion; + quaternion quat; + quat.w = 1; + quat.x = 0; + quat.y = 0; + quat.z = 0; + return {type, {}, std::move(quat)}; + } + case p::Orientation::TypeCase::TYPE_NOT_SET: + default: { + throw Exception(ErrorCondition::k_not_supported, "orientation type not known"); + } + } + return OrientationConfig{}; +} + +viam::app::v1::Orientation to_proto(const OrientationConfig& cfg) { + namespace p = viam::app::v1; + p::Orientation orientation; + switch (cfg.get_type()) { + case AxisAngles: { + p::Orientation_AxisAngles aa; + const axis_angles a = boost::get(cfg.get_orientation()); + aa.set_x(a.x); + aa.set_y(a.y); + aa.set_z(a.z); + aa.set_theta(a.theta); + *orientation.mutable_axis_angles() = std::move(aa); + return orientation; + }; + case OrientationVector: { + p::Orientation_OrientationVectorRadians ovec; + const orientation_vector ov = boost::get(cfg.get_orientation()); + ovec.set_x(ov.x); + ovec.set_y(ov.y); + ovec.set_z(ov.z); + ovec.set_theta(ov.theta); + *orientation.mutable_vector_radians() = std::move(ovec); + return orientation; + }; + case OrientationVectorDegrees: { + p::Orientation_OrientationVectorDegrees ovec; + const orientation_vector_degrees ovd = + boost::get(cfg.get_orientation()); + ovec.set_x(ovd.x); + ovec.set_y(ovd.y); + ovec.set_z(ovd.z); + ovec.set_theta(ovd.theta); + *orientation.mutable_vector_degrees() = std::move(ovec); + return orientation; + }; + case EulerAngles: { + p::Orientation_EulerAngles euler; + const euler_angles ea = boost::get(cfg.get_orientation()); + euler.set_pitch(ea.pitch); + euler.set_roll(ea.roll); + euler.set_yaw(ea.yaw); + *orientation.mutable_euler_angles() = std::move(euler); + return orientation; + }; + case Quaternion: { + p::Orientation_Quaternion quat; + const quaternion q = boost::get(cfg.get_orientation()); + quat.set_w(q.w); + quat.set_x(q.x); + quat.set_y(q.y); + quat.set_z(q.z); + *orientation.mutable_quaternion() = std::move(quat); + return orientation; + }; + default: { + throw Exception(ErrorCondition::k_not_supported, "orientation type not known"); + } + } +} + +viam::app::v1::Frame to_proto(const LinkConfig& cfg) { + viam::app::v1::Frame frame; + + *frame.mutable_parent() = cfg.get_parent(); + *frame.mutable_geometry() = to_proto(cfg.get_geometry_config()); + *frame.mutable_orientation() = to_proto(cfg.get_orientation_config()); + *frame.mutable_translation() = to_proto(cfg.get_translation()); + return frame; +}; + +LinkConfig from_proto(const viam::app::v1::Frame& proto) { + translation ts; + ts.x = proto.translation().x(); + ts.y = proto.translation().y(); + ts.z = proto.translation().z(); + + OrientationConfig ocfg; + if (proto.has_orientation()) { + ocfg = impl::from_proto(proto.orientation()); + } + + GeometryConfig gcfg; + if (proto.has_geometry()) { + gcfg = from_proto(proto.geometry()); + } + + return {std::move(ts), std::move(ocfg), std::move(gcfg), proto.parent()}; +}; + +viam::app::v1::Translation to_proto(const translation& ts) { + viam::app::v1::Translation t; + t.set_x(ts.x); + t.set_y(ts.y); + t.set_z(ts.z); + return t; +} + +viam::common::v1::ResourceName to_proto(const Name& name) { + viam::common::v1::ResourceName rn; + *rn.mutable_namespace_() = name.api().type_namespace(); + if (name.remote_name().empty()) { + *rn.mutable_name() = name.name(); + } else { + *rn.mutable_name() = name.remote_name() + ":" + name.name(); + } + *rn.mutable_type() = name.api().resource_type(); + *rn.mutable_subtype() = name.api().resource_subtype(); + return rn; +} + +Name from_proto(const viam::common::v1::ResourceName& proto) { + const API api(proto.namespace_(), proto.type(), proto.subtype()); + auto name_parts = long_name_to_remote_and_short(proto.name()); + + return Name( + {proto.namespace_(), proto.type(), proto.subtype()}, name_parts.first, name_parts.second); +} + +using time_point = std::chrono::time_point; + +time_point timestamp_to_time_pt(const google::protobuf::Timestamp& timestamp) { + const std::chrono::seconds seconds(timestamp.seconds()); + const std::chrono::nanoseconds nanos(timestamp.nanos()); + return time_point(std::chrono::duration_cast(seconds) + + nanos); +} + +google::protobuf::Timestamp time_pt_to_timestamp(const time_point& time_pt) { + const std::chrono::seconds duration_s = + std::chrono::duration_cast(time_pt.time_since_epoch()); + const std::chrono::nanoseconds duration_ns = time_pt.time_since_epoch() - duration_s; + google::protobuf::Timestamp timestamp; + timestamp.set_seconds(duration_s.count()); + timestamp.set_nanos(static_cast(duration_ns.count())); + return timestamp; +} +} // namespace impl +} // namespace sdk +} // namespace viam diff --git a/src/viam/sdk/common/private/proto_conversions.hpp b/src/viam/sdk/common/private/proto_conversions.hpp new file mode 100644 index 000000000..30f3344b7 --- /dev/null +++ b/src/viam/sdk/common/private/proto_conversions.hpp @@ -0,0 +1,85 @@ +#pragma once + +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace viam { +namespace sdk { +namespace impl { + +viam::common::v1::Vector3 to_proto(const Vector3& v3); +Vector3 from_proto(const viam::common::v1::Vector3& vec); + +viam::common::v1::ResponseMetadata to_proto(const response_metadata& metadata); +response_metadata from_proto(const viam::common::v1::ResponseMetadata& proto); + +google::protobuf::Duration to_proto(const std::chrono::microseconds& duration); +std::chrono::microseconds from_proto(const google::protobuf::Duration& proto); + +viam::module::v1::HandlerMap to_proto(const HandlerMap_& hm); +HandlerMap_ from_proto(const viam::module::v1::HandlerMap& proto); + +ResourceConfig from_proto(const viam::app::v1::ComponentConfig& proto_cfg); +viam::app::v1::ComponentConfig to_proto(const ResourceConfig& cfg); + +WorldState::geometries_in_frame from_proto(const common::v1::GeometriesInFrame& proto); +common::v1::GeometriesInFrame to_proto(const WorldState::geometries_in_frame& gif); + +common::v1::Transform to_proto(const WorldState::transform& transform); +WorldState::transform from_proto(const common::v1::Transform& proto); + +common::v1::WorldState to_proto(const WorldState& ws); +WorldState from_proto(const common::v1::WorldState& ws); + +pose from_proto(const viam::common::v1::Pose& proto); +viam::common::v1::Pose to_proto(const pose& pose); + +pose_in_frame from_proto(const viam::common::v1::PoseInFrame& proto); +viam::common::v1::PoseInFrame to_proto(const pose_in_frame& pif); + +GeometryConfig from_proto(const viam::common::v1::Geometry& proto); +std::vector from_proto(const viam::common::v1::GetGeometriesResponse& proto); +viam::common::v1::Geometry to_proto(const GeometryConfig& cfg); +viam::common::v1::RectangularPrism box_proto(const GeometryConfig& cfg); +viam::common::v1::Sphere sphere_proto(const GeometryConfig& cfg); +viam::common::v1::Capsule capsule_proto(const GeometryConfig& cfg); + +common::v1::GeoPoint to_proto(const geo_point& gp); +geo_point from_proto(const common::v1::GeoPoint& proto); + +common::v1::GeoGeometry to_proto(const geo_geometry& gg); +geo_geometry from_proto(const common::v1::GeoGeometry& proto); + +viam::app::v1::Orientation to_proto(const OrientationConfig& cfg); +OrientationConfig from_proto(const viam::app::v1::Orientation& proto); + +viam::app::v1::Frame to_proto(const LinkConfig& cfg); +LinkConfig from_proto(const viam::app::v1::Frame& proto); + +viam::app::v1::Translation to_proto(const translation& ts); + +viam::common::v1::ResourceName to_proto(const Name& name); +Name from_proto(const viam::common::v1::ResourceName& proto); + +std::chrono::time_point timestamp_to_time_pt( + const google::protobuf::Timestamp& timestamp); + +google::protobuf::Timestamp time_pt_to_timestamp( + const std::chrono::time_point& time_pt); + +} // namespace impl +} // namespace sdk +} // namespace viam diff --git a/src/viam/sdk/common/proto_value.hpp b/src/viam/sdk/common/proto_value.hpp index 61bd43ea1..6724de384 100644 --- a/src/viam/sdk/common/proto_value.hpp +++ b/src/viam/sdk/common/proto_value.hpp @@ -1,10 +1,8 @@ #pragma once -#include #include #include #include -#include #include namespace google { diff --git a/src/viam/sdk/common/utils.cpp b/src/viam/sdk/common/utils.cpp index b3fec14c9..88770f63f 100644 --- a/src/viam/sdk/common/utils.cpp +++ b/src/viam/sdk/common/utils.cpp @@ -4,6 +4,7 @@ #include #include +#include #include #include #include @@ -11,8 +12,6 @@ #include #include -#include - #include #include #include @@ -21,8 +20,6 @@ namespace viam { namespace sdk { -using time_point = std::chrono::time_point; - std::vector string_to_bytes(const std::string& s) { std::vector bytes(s.begin(), s.end()); return bytes; @@ -33,64 +30,6 @@ std::string bytes_to_string(const std::vector& b) { return img_string; }; -time_point timestamp_to_time_pt(const google::protobuf::Timestamp& timestamp) { - const std::chrono::seconds seconds(timestamp.seconds()); - const std::chrono::nanoseconds nanos(timestamp.nanos()); - return time_point(std::chrono::duration_cast(seconds) + - nanos); -} - -google::protobuf::Timestamp time_pt_to_timestamp(const time_point& time_pt) { - const std::chrono::seconds duration_s = - std::chrono::duration_cast(time_pt.time_since_epoch()); - const std::chrono::nanoseconds duration_ns = time_pt.time_since_epoch() - duration_s; - google::protobuf::Timestamp timestamp; - timestamp.set_seconds(duration_s.count()); - timestamp.set_nanos(static_cast(duration_ns.count())); - return timestamp; -} - -response_metadata response_metadata::from_proto(const viam::common::v1::ResponseMetadata& proto) { - response_metadata metadata; - metadata.captured_at = timestamp_to_time_pt(proto.captured_at()); - return metadata; -} - -viam::common::v1::ResponseMetadata response_metadata::to_proto(const response_metadata& metadata) { - viam::common::v1::ResponseMetadata proto; - google::protobuf::Timestamp ts = time_pt_to_timestamp(metadata.captured_at); - *proto.mutable_captured_at() = std::move(ts); - return proto; -} - -std::chrono::microseconds from_proto(const google::protobuf::Duration& proto) { - namespace sc = std::chrono; - const sc::seconds seconds_part{proto.seconds()}; - const sc::nanoseconds nanos_part{proto.nanos()}; - - const sc::microseconds from_seconds = sc::duration_cast(seconds_part); - sc::microseconds from_nanos = sc::duration_cast(nanos_part); - - if ((nanos_part.count() < 0) && (from_nanos > nanos_part)) { - from_nanos -= sc::microseconds(1); - } else if ((nanos_part.count() > 0) && (from_nanos < nanos_part)) { - from_nanos += sc::microseconds(1); - } - return from_seconds + from_nanos; -} - -google::protobuf::Duration to_proto(const std::chrono::microseconds& duration) { - namespace sc = std::chrono; - - const sc::seconds seconds = sc::duration_cast(duration); - const sc::nanoseconds nanos = duration - seconds; - - google::protobuf::Duration proto; - proto.set_nanos(static_cast(nanos.count())); - proto.set_seconds(seconds.count()); - return proto; -} - void set_logger_severity_from_args(int argc, char** argv) { if (argc >= 3 && strcmp(argv[2], "--log-level=debug") == 0) { boost::log::core::get()->set_filter(boost::log::trivial::severity >= @@ -184,5 +123,26 @@ bool from_dm_from_extra(const ProtoStruct& extra) { return false; } +std::pair long_name_to_remote_and_short(const std::string& long_name) { + std::vector name_parts; + // boost::split causes a clang-tidy false positive, see + // https://bugs.llvm.org/show_bug.cgi?id=41141 + // + // NOLINTNEXTLINE + boost::split(name_parts, long_name, boost::is_any_of(":")); + auto name = name_parts.back(); + name_parts.pop_back(); + auto remote_name = name_parts.empty() + ? "" + : std::accumulate(std::next(name_parts.begin()), + name_parts.end(), + *name_parts.begin(), + [](const std::string& a, const std::string& b) { + return a + ":" + b; + }); + + return {std::move(remote_name), std::move(name)}; +} + } // namespace sdk } // namespace viam diff --git a/src/viam/sdk/common/utils.hpp b/src/viam/sdk/common/utils.hpp index 20beca5ee..7b0aa95ad 100644 --- a/src/viam/sdk/common/utils.hpp +++ b/src/viam/sdk/common/utils.hpp @@ -3,8 +3,6 @@ #include #include -#include - #include #include #include @@ -20,29 +18,13 @@ const std::string kBuiltin = "builtin"; struct response_metadata { std::chrono::time_point captured_at; - - static response_metadata from_proto(const viam::common::v1::ResponseMetadata& proto); - static viam::common::v1::ResponseMetadata to_proto(const response_metadata& metadata); }; bool operator==(const response_metadata& lhs, const response_metadata& rhs); -/// @brief convert a google::protobuf::Timestamp to -/// std::chrono::time_point -std::chrono::time_point timestamp_to_time_pt( - const google::protobuf::Timestamp& timestamp); - -/// @brief convert a std::chrono::time_point to -/// a google::protobuf::Timestamp. -google::protobuf::Timestamp time_pt_to_timestamp( - const std::chrono::time_point& time_pt); - std::vector string_to_bytes(std::string const& s); std::string bytes_to_string(std::vector const& b); -std::chrono::microseconds from_proto(const google::protobuf::Duration& proto); -google::protobuf::Duration to_proto(const std::chrono::microseconds& duration); - // the authority on a grpc::ClientContext is sometimes set to an invalid uri on mac, causing // `rust-utils` to fail to process gRPC requests. This class provides a convenience wrapper around a // grpc ClientContext that allows us to make any necessary modifications to authority or else where @@ -61,6 +43,10 @@ class ClientContext { grpc::ClientContext wrapped_context_; }; +/// @brief Given a fully qualified resource name, returns remote name (or "" if +/// no remote name exists) and short name +std::pair long_name_to_remote_and_short(const std::string& long_name); + /// @brief Returns a new `ProtoStruct` with a random key for server-side debug logging ProtoStruct debug_map(); diff --git a/src/viam/sdk/common/world_state.cpp b/src/viam/sdk/common/world_state.cpp index bd1fa5ca6..535634ba6 100644 --- a/src/viam/sdk/common/world_state.cpp +++ b/src/viam/sdk/common/world_state.cpp @@ -1,84 +1,10 @@ #include -#include - #include 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 obstacles; - for (const auto& po : proto_obstacles) { - obstacles.push_back(geometries_in_frame::from_proto(po)); - } - - const auto& proto_transforms = ws.transforms(); - std::vector 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::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; @@ -94,5 +20,12 @@ bool operator==(const WorldState& lhs, const WorldState& rhs) { return lhs.obstacles_ == rhs.obstacles_ && lhs.transforms_ == rhs.transforms_; } +const std::vector& WorldState::obstacles() const { + return obstacles_; +} +const std::vector& WorldState::transforms() const { + return transforms_; +} + } // namespace sdk } // namespace viam diff --git a/src/viam/sdk/common/world_state.hpp b/src/viam/sdk/common/world_state.hpp index 3cc582a3c..174c93324 100644 --- a/src/viam/sdk/common/world_state.hpp +++ b/src/viam/sdk/common/world_state.hpp @@ -1,6 +1,6 @@ #pragma once -#include +#include #include #include @@ -14,8 +14,6 @@ class WorldState { struct geometries_in_frame { std::vector geometries; std::string reference_frame; - common::v1::GeometriesInFrame to_proto() const; - static geometries_in_frame from_proto(const common::v1::GeometriesInFrame& proto); }; struct transform { @@ -23,13 +21,11 @@ class WorldState { pose_in_frame pose_in_observer_frame; std::shared_ptr 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); + const std::vector& obstacles() const; + const std::vector& transforms() const; WorldState() {} WorldState(std::vector obstacles, std::vector transforms) diff --git a/src/viam/sdk/components/arm.cpp b/src/viam/sdk/components/arm.cpp index 593139a59..0e4a0d638 100644 --- a/src/viam/sdk/components/arm.cpp +++ b/src/viam/sdk/components/arm.cpp @@ -11,20 +11,6 @@ API API::traits::api() { return {kRDK, kComponent, "arm"}; } -Arm::KinematicsData Arm::from_proto(const viam::common::v1::GetKinematicsResponse& proto) { - std::vector bytes(proto.kinematics_data().begin(), - proto.kinematics_data().end()); - switch (proto.format()) { - case common::v1::KinematicsFileFormat::KINEMATICS_FILE_FORMAT_SVA: - return Arm::KinematicsDataSVA(std::move(bytes)); - case common::v1::KinematicsFileFormat::KINEMATICS_FILE_FORMAT_URDF: - return Arm::KinematicsDataURDF(std::move(bytes)); - case common::v1::KinematicsFileFormat::KINEMATICS_FILE_FORMAT_UNSPECIFIED: // fallthrough - default: - return Arm::KinematicsDataUnspecified{}; - } -} - Arm::Arm(std::string name) : Component(std::move(name)) {} } // namespace sdk diff --git a/src/viam/sdk/components/arm.hpp b/src/viam/sdk/components/arm.hpp index e0ff7bba9..79b60bb26 100644 --- a/src/viam/sdk/components/arm.hpp +++ b/src/viam/sdk/components/arm.hpp @@ -8,8 +8,6 @@ #include #include -#include - #include #include #include @@ -67,8 +65,6 @@ class Arm : public Component, public Stoppable { boost::optional max_acc_degs_per_sec2; }; - static KinematicsData from_proto(const viam::common::v1::GetKinematicsResponse& proto); - /// @brief Get the current position of the end of the arm. /// @return The `pose` representing the end position of the arm. inline pose get_end_position() { diff --git a/src/viam/sdk/components/base.cpp b/src/viam/sdk/components/base.cpp index 6a4e56582..10e665d5c 100644 --- a/src/viam/sdk/components/base.cpp +++ b/src/viam/sdk/components/base.cpp @@ -1,13 +1,5 @@ #include -#include - -#include -#include - -#include -#include - namespace viam { namespace sdk { @@ -19,12 +11,6 @@ API API::traits::api() { return {kRDK, kComponent, "base"}; } -Base::properties Base::properties::from_proto( - const component::base::v1::GetPropertiesResponse& proto) { - return { - proto.width_meters(), proto.turning_radius_meters(), proto.wheel_circumference_meters()}; -} - std::ostream& operator<<(std::ostream& os, const Base::properties& v) { os << "{ turning_radius_meters: " << v.turning_radius_meters << ", wheel_circumference_meters: " << v.wheel_circumference_meters diff --git a/src/viam/sdk/components/base.hpp b/src/viam/sdk/components/base.hpp index 687f9e484..6bfc896a2 100644 --- a/src/viam/sdk/components/base.hpp +++ b/src/viam/sdk/components/base.hpp @@ -5,8 +5,6 @@ #include -#include - #include #include #include @@ -32,8 +30,6 @@ class Base : public Component, public Stoppable { double width_meters; double turning_radius_meters; double wheel_circumference_meters; - - static properties from_proto(const component::base::v1::GetPropertiesResponse& proto); }; friend std::ostream& operator<<(std::ostream& os, const properties& v); friend bool operator==(const properties& lhs, const properties& rhs); diff --git a/src/viam/sdk/components/board.cpp b/src/viam/sdk/components/board.cpp index 4b80d386b..fd6e2baf2 100644 --- a/src/viam/sdk/components/board.cpp +++ b/src/viam/sdk/components/board.cpp @@ -2,10 +2,6 @@ #include -#include -#include - -#include #include #include @@ -20,59 +16,6 @@ API API::traits::api() { return {kRDK, kComponent, "board"}; } -Board::status Board::from_proto(const viam::component::board::v1::Status& proto) { - Board::status status; - for (const auto& analog : proto.analogs()) { - status.analog_reader_values.emplace(analog.first, analog.second); - } - for (const auto& digital : proto.digital_interrupts()) { - status.digital_interrupt_values.emplace(digital.first, digital.second); - } - return status; -} - -Board::power_mode Board::from_proto(viam::component::board::v1::PowerMode proto) { - switch (proto) { - case viam::component::board::v1::POWER_MODE_NORMAL: { - return Board::power_mode::normal; - } - case viam::component::board::v1::POWER_MODE_OFFLINE_DEEP: { - return Board::power_mode::offline_deep; - } - case viam::component::board::v1::POWER_MODE_UNSPECIFIED: - default: { - throw Exception(ErrorCondition::k_not_supported, - "Invalid proto board power_mode to decode"); - } - } -} - -viam::component::board::v1::Status Board::to_proto(const status& status) { - viam::component::board::v1::Status proto; - for (const auto& analog : status.analog_reader_values) { - proto.mutable_analogs()->insert({analog.first, analog.second}); - } - - for (const auto& digital : status.digital_interrupt_values) { - proto.mutable_digital_interrupts()->insert({digital.first, digital.second}); - } - return proto; -} - -viam::component::board::v1::PowerMode Board::to_proto(Board::power_mode power_mode) { - switch (power_mode) { - case Board::power_mode::normal: { - return viam::component::board::v1::POWER_MODE_NORMAL; - } - case Board::power_mode::offline_deep: { - return viam::component::board::v1::POWER_MODE_OFFLINE_DEEP; - } - default: { - throw Exception(ErrorCondition::k_not_supported, "Invalid board power_mode to encode"); - } - } -} - Board::Board(std::string name) : Component(std::move(name)){}; bool operator==(const Board::status& lhs, const Board::status& rhs) { diff --git a/src/viam/sdk/components/board.hpp b/src/viam/sdk/components/board.hpp index b61a8208a..8cb8078e9 100644 --- a/src/viam/sdk/components/board.hpp +++ b/src/viam/sdk/components/board.hpp @@ -7,8 +7,6 @@ #include #include -#include - #include #include #include @@ -76,18 +74,6 @@ class Board : public Component { API api() const override; - /// @brief Creates a `status` struct from its proto representation. - static status from_proto(const viam::component::board::v1::Status& proto); - - /// @brief Creates a `power_mode` enum from its proto representation. - static power_mode from_proto(viam::component::board::v1::PowerMode proto); - - /// @brief Converts a `status` struct to its proto representation. - static viam::component::board::v1::Status to_proto(const status& status); - - /// @brief Converts a `power_mode` enum to its proto representation. - static viam::component::board::v1::PowerMode to_proto(power_mode power_mode); - /// @brief Gets the high/low state of the given pin on a board. /// @param pin board pin name /// @return high/low state of the given pin. High = on, low = off diff --git a/src/viam/sdk/components/camera.cpp b/src/viam/sdk/components/camera.cpp index e97ae00b3..669e0a17c 100644 --- a/src/viam/sdk/components/camera.cpp +++ b/src/viam/sdk/components/camera.cpp @@ -3,10 +3,6 @@ #include #include -#include -#include -#include - #include #include #include @@ -134,127 +130,6 @@ std::string Camera::normalize_mime_type(const std::string& str) { return mime_type; } -std::string Camera::format_to_MIME_string(viam::component::camera::v1::Format format) { - switch (format) { - case viam::component::camera::v1::FORMAT_RAW_RGBA: - return "image/vnd.viam.rgba"; - case viam::component::camera::v1::FORMAT_RAW_DEPTH: - return "image/vnd.viam.dep"; - case viam::component::camera::v1::FORMAT_JPEG: - return "image/jpeg"; - case viam::component::camera::v1::FORMAT_PNG: - return "image/png"; - default: - return ""; - } -} - -::viam::component::camera::v1::Format Camera::MIME_string_to_format( - const std::string& mime_string) { - if (mime_string == "image/vnd.viam.rgba") { - return viam::component::camera::v1::FORMAT_RAW_RGBA; - } - if (mime_string == "image/vnd.viam.dep") { - return viam::component::camera::v1::FORMAT_RAW_DEPTH; - } - if (mime_string == "image/jpeg") { - return viam::component::camera::v1::FORMAT_JPEG; - } - if (mime_string == "image/png") { - return viam::component::camera::v1::FORMAT_PNG; - } - return viam::component::camera::v1::FORMAT_UNSPECIFIED; -} - -Camera::raw_image Camera::from_proto(const viam::component::camera::v1::GetImageResponse& proto) { - Camera::raw_image raw_image; - std::string img_string = proto.image(); - const std::vector bytes(img_string.begin(), img_string.end()); - raw_image.bytes = bytes; - raw_image.mime_type = proto.mime_type(); - raw_image.source_name = ""; - return raw_image; -} - -Camera::image_collection Camera::from_proto( - const viam::component::camera::v1::GetImagesResponse& proto) { - Camera::image_collection image_collection; - std::vector images; - for (const auto& img : proto.images()) { - Camera::raw_image raw_image; - std::string img_string = img.image(); - const std::vector bytes(img_string.begin(), img_string.end()); - raw_image.bytes = bytes; - raw_image.mime_type = format_to_MIME_string(img.format()); - raw_image.source_name = img.source_name(); - images.push_back(raw_image); - } - image_collection.images = std::move(images); - image_collection.metadata = response_metadata::from_proto(proto.response_metadata()); - return image_collection; -} - -Camera::point_cloud Camera::from_proto( - const viam::component::camera::v1::GetPointCloudResponse& proto) { - Camera::point_cloud point_cloud; - std::string pc_string = proto.point_cloud(); - const std::vector bytes(pc_string.begin(), pc_string.end()); - point_cloud.pc = bytes; - point_cloud.mime_type = proto.mime_type(); - return point_cloud; -} - -Camera::intrinsic_parameters Camera::from_proto( - const viam::component::camera::v1::IntrinsicParameters& proto) { - Camera::intrinsic_parameters params; - // NOLINTNEXTLINE(bugprone-narrowing-conversions) - params.width_px = proto.width_px(); - // NOLINTNEXTLINE(bugprone-narrowing-conversions) - params.height_px = proto.height_px(); - params.focal_x_px = proto.focal_x_px(); - params.focal_y_px = proto.focal_y_px(); - params.center_x_px = proto.center_x_px(); - params.center_y_px = proto.center_y_px(); - return params; -} - -Camera::distortion_parameters Camera::from_proto( - const viam::component::camera::v1::DistortionParameters& proto) { - Camera::distortion_parameters params; - params.model = proto.model(); - params.parameters = {proto.parameters().begin(), proto.parameters().end()}; - return params; -} - -Camera::properties Camera::from_proto( - const viam::component::camera::v1::GetPropertiesResponse& proto) { - return {proto.supports_pcd(), - from_proto(proto.intrinsic_parameters()), - from_proto(proto.distortion_parameters()), - {proto.mime_types().begin(), proto.mime_types().end()}, - (proto.frame_rate())}; -} - -viam::component::camera::v1::IntrinsicParameters Camera::to_proto( - const Camera::intrinsic_parameters& params) { - viam::component::camera::v1::IntrinsicParameters proto; - proto.set_width_px(params.width_px); - proto.set_height_px(params.height_px); - proto.set_focal_x_px(params.focal_x_px); - proto.set_focal_y_px(params.focal_y_px); - proto.set_center_x_px(params.center_x_px); - proto.set_center_y_px(params.center_y_px); - return proto; -} - -viam::component::camera::v1::DistortionParameters Camera::to_proto( - const Camera::distortion_parameters& params) { - viam::component::camera::v1::DistortionParameters proto; - *proto.mutable_model() = params.model; - *proto.mutable_parameters() = {params.parameters.begin(), params.parameters.end()}; - return proto; -} - Camera::Camera(std::string name) : Component(std::move(name)){}; bool operator==(const Camera::point_cloud& lhs, const Camera::point_cloud& rhs) { diff --git a/src/viam/sdk/components/camera.hpp b/src/viam/sdk/components/camera.hpp index e47ff929a..203025c3f 100644 --- a/src/viam/sdk/components/camera.hpp +++ b/src/viam/sdk/components/camera.hpp @@ -11,9 +11,6 @@ #include #include -#include -#include - #include #include #include @@ -137,39 +134,6 @@ class Camera : public Component { /// @brief remove any extra suffix's from the mime type string. static std::string normalize_mime_type(const std::string& str); - /// @brief convert a protobuf format enum with a MIME type string. - static std::string format_to_MIME_string(viam::component::camera::v1::Format format); - - /// @brief convert a MIME type string with a protobuf format enum. - static viam::component::camera::v1::Format MIME_string_to_format( - const std::string& mime_string); - - /// @brief Creates a `raw_image` struct from its proto representation. - static raw_image from_proto(const viam::component::camera::v1::GetImageResponse& proto); - - /// @brief Creates a `image_collection` struct from its proto representation. - static image_collection from_proto(const viam::component::camera::v1::GetImagesResponse& proto); - - /// @brief Creates a `point_cloud` struct from its proto representation. - static point_cloud from_proto(const viam::component::camera::v1::GetPointCloudResponse& proto); - - /// @brief creates an `intrinsic_parameters` struct from its proto representation. - static intrinsic_parameters from_proto( - const viam::component::camera::v1::IntrinsicParameters& proto); - - /// @brief creats a `distortion_parameters` struct from its proto representation. - static distortion_parameters from_proto( - const viam::component::camera::v1::DistortionParameters& proto); - - /// @brief creates a `properties` struct from its proto representation. - static properties from_proto(const viam::component::camera::v1::GetPropertiesResponse& proto); - - /// @brief converts a `distortion_parameters` struct to its proto representation. - static viam::component::camera::v1::DistortionParameters to_proto(const distortion_parameters&); - - /// @brief converts an `intrinsic_parameters` struct to its proto representation. - static viam::component::camera::v1::IntrinsicParameters to_proto(const intrinsic_parameters&); - /// @brief Send/receive arbitrary commands to the resource. /// @param Command the command to execute. /// @return The result of the executed command. @@ -179,7 +143,7 @@ class Camera : public Component { /// @param mime_type the desired mime_type of the image (does not guarantee output type). /// @return The frame as a `raw_image`. inline raw_image get_image(std::string mime_type) { - return get_image(mime_type, {}); + return get_image(std::move(mime_type), {}); } /// @brief Get the next image from the camera as a raw image. @@ -197,7 +161,7 @@ class Camera : public Component { /// @param mime_type the desired mime_type of the point_cloud (does not guarantee output type). /// @return The requested `point_cloud`. inline point_cloud get_point_cloud(std::string mime_type) { - return get_point_cloud(mime_type, {}); + return get_point_cloud(std::move(mime_type), {}); } /// @brief Get the next `point_cloud` from the camera. diff --git a/src/viam/sdk/components/component.cpp b/src/viam/sdk/components/component.cpp index c44bd91e9..f4326ea17 100644 --- a/src/viam/sdk/components/component.cpp +++ b/src/viam/sdk/components/component.cpp @@ -1,27 +1,16 @@ #include -#include - -#include - -#include - #include -#include namespace viam { namespace sdk { -using viam::common::v1::ResourceName; - Component::Component() : Resource("component"){}; Component::Component(std::string name) : Resource(std::move(name)){}; -ResourceName Component::get_resource_name(std::string name) const { - auto r = this->Resource::get_resource_name(name); - *r.mutable_type() = kComponent; - return r; +Name Component::get_resource_name() const { + return Resource::get_resource_name(kComponent); } } // namespace sdk diff --git a/src/viam/sdk/components/component.hpp b/src/viam/sdk/components/component.hpp index 83830d2e4..c96628a4c 100644 --- a/src/viam/sdk/components/component.hpp +++ b/src/viam/sdk/components/component.hpp @@ -2,10 +2,6 @@ #include -#include - -#include - #include namespace viam { @@ -14,7 +10,7 @@ namespace sdk { class Component : public Resource { public: Component(); - viam::common::v1::ResourceName get_resource_name(std::string name) const override; + Name get_resource_name() const override; protected: explicit Component(std::string name); diff --git a/src/viam/sdk/components/encoder.cpp b/src/viam/sdk/components/encoder.cpp index 6c3fbf7ba..6963249ca 100644 --- a/src/viam/sdk/components/encoder.cpp +++ b/src/viam/sdk/components/encoder.cpp @@ -1,10 +1,5 @@ #include -#include - -#include -#include - #include #include #include @@ -20,77 +15,6 @@ API API::traits::api() { return {kRDK, kComponent, "encoder"}; } -Encoder::position_type Encoder::from_proto(viam::component::encoder::v1::PositionType proto) { - switch (proto) { - case viam::component::encoder::v1::POSITION_TYPE_UNSPECIFIED: { - return Encoder::position_type::unspecified; - } - case viam::component::encoder::v1::POSITION_TYPE_ANGLE_DEGREES: { - return Encoder::position_type::angle_degrees; - } - case viam::component::encoder::v1::POSITION_TYPE_TICKS_COUNT: { - return Encoder::position_type::ticks_count; - } - default: { - throw Exception(ErrorCondition::k_not_supported, - "Invalid proto encoder type to decode"); - } - } -} - -Encoder::position Encoder::from_proto( - const viam::component::encoder::v1::GetPositionResponse& proto) { - Encoder::position position; - position.value = proto.value(); - - position.type = from_proto(proto.position_type()); - return position; -} - -Encoder::properties Encoder::from_proto( - const viam::component::encoder::v1::GetPropertiesResponse& proto) { - Encoder::properties properties; - properties.ticks_count_supported = proto.ticks_count_supported(); - - properties.angle_degrees_supported = proto.angle_degrees_supported(); - return properties; -} - -viam::component::encoder::v1::PositionType Encoder::to_proto(position_type position_type) { - switch (position_type) { - case Encoder::position_type::unspecified: { - return viam::component::encoder::v1::PositionType::POSITION_TYPE_UNSPECIFIED; - } - case Encoder::position_type::angle_degrees: { - return viam::component::encoder::v1::POSITION_TYPE_ANGLE_DEGREES; - } - case Encoder::position_type::ticks_count: { - return viam::component::encoder::v1::POSITION_TYPE_TICKS_COUNT; - } - default: { - throw Exception(ErrorCondition::k_not_supported, - "Invalid proto encoder type to encode"); - } - } -} - -viam::component::encoder::v1::GetPositionResponse Encoder::to_proto(const position& position) { - viam::component::encoder::v1::GetPositionResponse proto; - proto.set_value(position.value); - - proto.set_position_type(to_proto(position.type)); - return proto; -} - -viam::component::encoder::v1::GetPropertiesResponse Encoder::to_proto( - const properties& properties) { - viam::component::encoder::v1::GetPropertiesResponse proto; - proto.set_ticks_count_supported(properties.ticks_count_supported); - - proto.set_angle_degrees_supported(properties.angle_degrees_supported); - return proto; -} - Encoder::Encoder(std::string name) : Component(std::move(name)){}; bool operator==(const Encoder::position& lhs, const Encoder::position& rhs) { diff --git a/src/viam/sdk/components/encoder.hpp b/src/viam/sdk/components/encoder.hpp index 760827081..19292e4dd 100644 --- a/src/viam/sdk/components/encoder.hpp +++ b/src/viam/sdk/components/encoder.hpp @@ -5,8 +5,6 @@ #include -#include - #include #include #include @@ -46,25 +44,6 @@ class Encoder : public Component { bool angle_degrees_supported; }; - /// @brief Creates a `position_type` struct from its proto representation. - static position_type from_proto(viam::component::encoder::v1::PositionType proto); - - /// @brief Creates a `position` struct from its proto representation. - static position from_proto(const viam::component::encoder::v1::GetPositionResponse& proto); - - /// @brief Creates a `properties` struct from its proto representation. - static properties from_proto(const viam::component::encoder::v1::GetPropertiesResponse& proto); - - /// @brief Converts a `position_type` struct to its proto representation. - static viam::component::encoder::v1::PositionType to_proto(position_type position_type); - - /// @brief Converts a `position` struct to its proto representation. - static viam::component::encoder::v1::GetPositionResponse to_proto(const position& position); - - /// @brief Converts a `properties` struct to its proto representation. - static viam::component::encoder::v1::GetPropertiesResponse to_proto( - const properties& properties); - /// @brief Returns position of the encoder which can either be ticks since last zeroing for an /// incremental encoder or degrees for an absolute encoder. /// @param position_type The type of position you are requesting. If the driver does not diff --git a/src/viam/sdk/components/gantry.hpp b/src/viam/sdk/components/gantry.hpp index e955da5c4..b01522897 100644 --- a/src/viam/sdk/components/gantry.hpp +++ b/src/viam/sdk/components/gantry.hpp @@ -3,9 +3,6 @@ /// @brief Defines a `Gantry` component #pragma once -#include -#include - #include namespace viam { diff --git a/src/viam/sdk/components/generic.cpp b/src/viam/sdk/components/generic.cpp index 2c6f825f4..b2a3cd10e 100644 --- a/src/viam/sdk/components/generic.cpp +++ b/src/viam/sdk/components/generic.cpp @@ -1,14 +1,5 @@ #include -#include - -#include - -#include - -#include -#include - namespace viam { namespace sdk { diff --git a/src/viam/sdk/components/generic.hpp b/src/viam/sdk/components/generic.hpp index af9d0ef6f..6e853ac2b 100644 --- a/src/viam/sdk/components/generic.hpp +++ b/src/viam/sdk/components/generic.hpp @@ -4,8 +4,6 @@ /// commands. #pragma once -#include - #include #include #include diff --git a/src/viam/sdk/components/gripper.hpp b/src/viam/sdk/components/gripper.hpp index 40fee9882..c74095b2c 100644 --- a/src/viam/sdk/components/gripper.hpp +++ b/src/viam/sdk/components/gripper.hpp @@ -5,9 +5,6 @@ #include -#include -#include - #include namespace viam { diff --git a/src/viam/sdk/components/motor.cpp b/src/viam/sdk/components/motor.cpp index 906992717..e0e223de7 100644 --- a/src/viam/sdk/components/motor.cpp +++ b/src/viam/sdk/components/motor.cpp @@ -2,18 +2,12 @@ #include -#include -#include - #include #include namespace viam { namespace sdk { -Motor::position Motor::from_proto(const viam::component::motor::v1::GetPositionResponse& proto) { - return proto.position(); -} API Motor::api() const { return API::get(); } @@ -22,41 +16,6 @@ API API::traits::api() { return {kRDK, kComponent, "motor"}; } -Motor::power_status Motor::from_proto(const viam::component::motor::v1::IsPoweredResponse& proto) { - Motor::power_status power_status; - power_status.is_on = proto.is_on(); - - power_status.power_pct = proto.power_pct(); - return power_status; -} - -Motor::properties Motor::from_proto( - const viam::component::motor::v1::GetPropertiesResponse& proto) { - Motor::properties properties; - properties.position_reporting = proto.position_reporting(); - return properties; -} - -viam::component::motor::v1::GetPositionResponse Motor::to_proto(const position& position) { - viam::component::motor::v1::GetPositionResponse proto; - proto.set_position(position); - return proto; -} - -viam::component::motor::v1::IsPoweredResponse Motor::to_proto(const power_status& power_status) { - viam::component::motor::v1::IsPoweredResponse proto; - proto.set_is_on(power_status.is_on); - - proto.set_power_pct(power_status.power_pct); - return proto; -} - -viam::component::motor::v1::GetPropertiesResponse Motor::to_proto(const properties& properties) { - viam::component::motor::v1::GetPropertiesResponse proto; - proto.set_position_reporting(properties.position_reporting); - return proto; -} - Motor::Motor(std::string name) : Component(std::move(name)){}; bool operator==(const Motor::power_status& lhs, const Motor::power_status& rhs) { diff --git a/src/viam/sdk/components/motor.hpp b/src/viam/sdk/components/motor.hpp index 6741d2426..be37e03d5 100644 --- a/src/viam/sdk/components/motor.hpp +++ b/src/viam/sdk/components/motor.hpp @@ -5,8 +5,6 @@ #include -#include - #include #include #include @@ -45,24 +43,6 @@ class Motor : public Component, public Stoppable { bool position_reporting; }; - /// @brief Creates a `position` struct from its proto representation. - static position from_proto(const viam::component::motor::v1::GetPositionResponse& proto); - - /// @brief Creates a `power_status` struct from its proto representation. - static power_status from_proto(const viam::component::motor::v1::IsPoweredResponse& proto); - - /// @brief Creates a `properties` struct from its proto representation. - static properties from_proto(const viam::component::motor::v1::GetPropertiesResponse& proto); - - /// @brief Converts a `position` struct to its proto representation. - static viam::component::motor::v1::GetPositionResponse to_proto(const position& position); - - /// @brief Converts a `power_status` struct to its proto representation. - static viam::component::motor::v1::IsPoweredResponse to_proto(const power_status& power_status); - - /// @brief Converts a `properties` struct to its proto representation. - static viam::component::motor::v1::GetPropertiesResponse to_proto(const properties& properties); - /// @brief Sets the percentage of the motor's total power that should be employed. /// @param power_pct Percentage of motor's power, between -1 and 1, where negative values /// indicate a backwards direction and positive values, a forward direction. diff --git a/src/viam/sdk/components/movement_sensor.cpp b/src/viam/sdk/components/movement_sensor.cpp index 27a235c26..f17705567 100644 --- a/src/viam/sdk/components/movement_sensor.cpp +++ b/src/viam/sdk/components/movement_sensor.cpp @@ -1,10 +1,5 @@ #include -#include - -#include -#include - #include #include @@ -19,78 +14,6 @@ API API::traits::api() { return {kRDK, kComponent, "movement_sensor"}; } -MovementSensor::compassheading MovementSensor::from_proto( - const viam::component::movementsensor::v1::GetCompassHeadingResponse& proto) { - MovementSensor::compassheading compassheading; - compassheading.value = proto.value(); - return compassheading; -} - -MovementSensor::position MovementSensor::from_proto( - const viam::component::movementsensor::v1::GetPositionResponse& proto) { - MovementSensor::position position; - position.coordinate = viam::sdk::geo_point::from_proto(proto.coordinate()); - position.altitude_m = proto.altitude_m(); - return position; -} - -MovementSensor::orientation MovementSensor::from_proto(const viam::common::v1::Orientation& proto) { - MovementSensor::orientation orientation; - orientation.o_x = proto.o_x(); - orientation.o_y = proto.o_y(); - orientation.o_z = proto.o_z(); - orientation.theta = proto.theta(); - return orientation; -} - -MovementSensor::properties MovementSensor::from_proto( - const viam::component::movementsensor::v1::GetPropertiesResponse& proto) { - MovementSensor::properties properties; - properties.linear_velocity_supported = proto.linear_velocity_supported(); - properties.angular_velocity_supported = proto.angular_velocity_supported(); - properties.orientation_supported = proto.orientation_supported(); - properties.position_supported = proto.position_supported(); - properties.compass_heading_supported = proto.compass_heading_supported(); - properties.linear_acceleration_supported = proto.linear_acceleration_supported(); - return properties; -} - -viam::component::movementsensor::v1::GetCompassHeadingResponse MovementSensor::to_proto( - const compassheading& compassheading) { - viam::component::movementsensor::v1::GetCompassHeadingResponse proto; - proto.set_value(compassheading.value); - return proto; -} - -viam::common::v1::Orientation MovementSensor::to_proto(const orientation& orientation) { - viam::common::v1::Orientation proto; - proto.set_o_x(orientation.o_x); - proto.set_o_y(orientation.o_y); - proto.set_o_z(orientation.o_z); - proto.set_theta(orientation.theta); - return proto; -} - -viam::component::movementsensor::v1::GetPositionResponse MovementSensor::to_proto( - const position& position) { - component::movementsensor::v1::GetPositionResponse proto; - proto.set_altitude_m(position.altitude_m); - *proto.mutable_coordinate() = position.coordinate.to_proto(); - return proto; -} - -viam::component::movementsensor::v1::GetPropertiesResponse MovementSensor::to_proto( - const properties& properties) { - viam::component::movementsensor::v1::GetPropertiesResponse proto; - proto.set_linear_velocity_supported(properties.linear_velocity_supported); - proto.set_angular_velocity_supported(properties.angular_velocity_supported); - proto.set_orientation_supported(properties.orientation_supported); - proto.set_position_supported(properties.position_supported); - proto.set_compass_heading_supported(properties.compass_heading_supported); - proto.set_linear_acceleration_supported(properties.linear_acceleration_supported); - return proto; -} - bool operator==(const MovementSensor::compassheading& lhs, const MovementSensor::compassheading& rhs) { return (lhs.value == rhs.value); diff --git a/src/viam/sdk/components/movement_sensor.hpp b/src/viam/sdk/components/movement_sensor.hpp index 5e6ede7cb..b0aa95920 100644 --- a/src/viam/sdk/components/movement_sensor.hpp +++ b/src/viam/sdk/components/movement_sensor.hpp @@ -5,8 +5,6 @@ #include -#include - #include #include #include @@ -55,36 +53,6 @@ class MovementSensor : public Component { API api() const override; - /// @brief Creates a `compassheading` struct from its proto representation. - static compassheading from_proto( - const viam::component::movementsensor::v1::GetCompassHeadingResponse& proto); - - /// @brief Creates a `position` struct from its proto representation. - static position from_proto( - const viam::component::movementsensor::v1::GetPositionResponse& proto); - - /// @brief Creates an `orientation` struct from its proto representation. - static orientation from_proto(const viam::common::v1::Orientation& proto); - - /// @brief Creates a `properties` struct from its proto representation. - static properties from_proto( - const viam::component::movementsensor::v1::GetPropertiesResponse& proto); - - /// @brief Converts a `compassheading` struct to its proto representation. - static viam::component::movementsensor::v1::GetCompassHeadingResponse to_proto( - const compassheading& compassheading); - - /// @brief Converts a `position` struct to its proto representation. - static viam::component::movementsensor::v1::GetPositionResponse to_proto( - const position& position); - - /// @brief Converts an `orientation` struct to its proto representation. - static viam::common::v1::Orientation to_proto(const orientation& orientation); - - /// @brief Converts a `properties` struct to its proto representation. - static viam::component::movementsensor::v1::GetPropertiesResponse to_proto( - const properties& properties); - inline Vector3 get_linear_velocity() { return get_linear_velocity({}); } diff --git a/src/viam/sdk/components/pose_tracker.hpp b/src/viam/sdk/components/pose_tracker.hpp index b0d8980d0..da3025a58 100644 --- a/src/viam/sdk/components/pose_tracker.hpp +++ b/src/viam/sdk/components/pose_tracker.hpp @@ -7,9 +7,6 @@ #include #include -#include -#include - #include #include #include diff --git a/src/viam/sdk/components/power_sensor.cpp b/src/viam/sdk/components/power_sensor.cpp index e2c3fb7ff..ceb8ae228 100644 --- a/src/viam/sdk/components/power_sensor.cpp +++ b/src/viam/sdk/components/power_sensor.cpp @@ -1,13 +1,8 @@ #include -#include -#include - #include #include -using namespace viam::component::powersensor::v1; - namespace viam { namespace sdk { @@ -19,34 +14,6 @@ API API::traits::api() { return {kRDK, kComponent, "power_sensor"}; } -PowerSensor::voltage PowerSensor::from_proto(const GetVoltageResponse& proto) { - PowerSensor::voltage v; - v.volts = proto.volts(); - v.is_ac = proto.is_ac(); - return v; -} - -PowerSensor::current PowerSensor::from_proto(const GetCurrentResponse& proto) { - PowerSensor::current c; - c.amperes = proto.amperes(); - c.is_ac = proto.is_ac(); - return c; -} - -GetVoltageResponse PowerSensor::to_proto(const voltage& v) { - GetVoltageResponse proto; - proto.set_volts(v.volts); - proto.set_is_ac(v.is_ac); - return proto; -} - -GetCurrentResponse PowerSensor::to_proto(const current& c) { - GetCurrentResponse proto; - proto.set_amperes(c.amperes); - proto.set_is_ac(c.is_ac); - return proto; -} - bool operator==(const PowerSensor::voltage& lhs, const PowerSensor::voltage& rhs) { return (lhs.volts == rhs.volts && lhs.is_ac == rhs.is_ac); } diff --git a/src/viam/sdk/components/power_sensor.hpp b/src/viam/sdk/components/power_sensor.hpp index e50010db9..e896502c0 100644 --- a/src/viam/sdk/components/power_sensor.hpp +++ b/src/viam/sdk/components/power_sensor.hpp @@ -5,14 +5,10 @@ #include -#include - #include #include #include -using namespace viam::component::powersensor::v1; - namespace viam { namespace sdk { @@ -43,18 +39,6 @@ class PowerSensor : public Component { API api() const override; - /// @brief Creates a `voltage` struct from its proto representation. - static voltage from_proto(const GetVoltageResponse& proto); - - /// @brief Creates a `current` struct from its proto representation. - static current from_proto(const GetCurrentResponse& proto); - - /// @brief Converts a `voltage` struct to its proto representation. - static GetVoltageResponse to_proto(const voltage& v); - - /// @brief Converts a `current` struct to its proto representation. - static GetCurrentResponse to_proto(const current& c); - /// @brief Returns the voltage reading of this sensor. /// @return The voltage reading of this sensor. inline voltage get_voltage() { diff --git a/src/viam/sdk/components/private/arm_client.cpp b/src/viam/sdk/components/private/arm_client.cpp index a3899036d..6506e3cda 100644 --- a/src/viam/sdk/components/private/arm_client.cpp +++ b/src/viam/sdk/components/private/arm_client.cpp @@ -4,6 +4,7 @@ #include #include +#include namespace viam { namespace sdk { @@ -17,12 +18,12 @@ ArmClient::ArmClient(std::string name, std::shared_ptr channel) pose ArmClient::get_end_position(const ProtoStruct& extra) { return make_client_helper(this, *stub_, &StubType::GetEndPosition) .with(extra) - .invoke([&](auto& response) { return pose::from_proto(response.pose()); }); + .invoke([&](auto& response) { return from_proto(response.pose()); }); } void ArmClient::move_to_position(const pose& pose, const ProtoStruct& extra) { return make_client_helper(this, *stub_, &StubType::MoveToPosition) - .with(extra, [&](auto& request) { *request.mutable_to() = pose.to_proto(); }) + .with(extra, [&](auto& request) { *request.mutable_to() = to_proto(pose); }) .invoke(); } @@ -90,13 +91,26 @@ ProtoStruct ArmClient::do_command(const ProtoStruct& command) { Arm::KinematicsData ArmClient::get_kinematics(const ProtoStruct& extra) { return make_client_helper(this, *stub_, &StubType::GetKinematics) .with(extra) - .invoke([](auto& response) { return Arm::from_proto(response); }); + .invoke([](auto& response) -> Arm::KinematicsData { + std::vector bytes(response.kinematics_data().begin(), + response.kinematics_data().end()); + switch (response.format()) { + case common::v1::KinematicsFileFormat::KINEMATICS_FILE_FORMAT_SVA: + return Arm::KinematicsDataSVA(std::move(bytes)); + case common::v1::KinematicsFileFormat::KINEMATICS_FILE_FORMAT_URDF: + return Arm::KinematicsDataURDF(std::move(bytes)); + case common::v1::KinematicsFileFormat:: + KINEMATICS_FILE_FORMAT_UNSPECIFIED: // fallthrough + default: + return Arm::KinematicsDataUnspecified{}; + } + }); } std::vector ArmClient::get_geometries(const ProtoStruct& extra) { return make_client_helper(this, *stub_, &StubType::GetGeometries) .with(extra) - .invoke([](auto& response) { return GeometryConfig::from_proto(response); }); + .invoke([](auto& response) { return from_proto(response); }); } } // namespace impl diff --git a/src/viam/sdk/components/private/arm_server.cpp b/src/viam/sdk/components/private/arm_server.cpp index fad2c0399..4ca661b3c 100644 --- a/src/viam/sdk/components/private/arm_server.cpp +++ b/src/viam/sdk/components/private/arm_server.cpp @@ -1,5 +1,6 @@ #include +#include #include namespace viam { @@ -16,7 +17,7 @@ ::grpc::Status ArmServer::GetEndPosition( return make_service_helper( "ArmServer::GetEndPosition", this, request)([&](auto& helper, auto& arm) { const pose p = arm->get_end_position(helper.getExtra()); - *response->mutable_pose() = p.to_proto(); + *response->mutable_pose() = to_proto(p); }); } @@ -26,7 +27,7 @@ ::grpc::Status ArmServer::MoveToPosition( ::viam::component::arm::v1::MoveToPositionResponse*) noexcept { return make_service_helper( "ArmServer::MoveToPosition", this, request)([&](auto& helper, auto& arm) { - arm->move_to_position(pose::from_proto(request->to()), helper.getExtra()); + arm->move_to_position(from_proto(request->to()), helper.getExtra()); }); } @@ -143,7 +144,7 @@ ::grpc::Status ArmServer::GetGeometries( "ArmServer::GetGeometries", this, request)([&](auto& helper, auto& arm) { const std::vector geometries = arm->get_geometries(helper.getExtra()); for (const auto& geometry : geometries) { - *response->mutable_geometries()->Add() = geometry.to_proto(); + *response->mutable_geometries()->Add() = to_proto(geometry); } }); } diff --git a/src/viam/sdk/components/private/base_client.cpp b/src/viam/sdk/components/private/base_client.cpp index 05ab39bcc..3168d4dba 100644 --- a/src/viam/sdk/components/private/base_client.cpp +++ b/src/viam/sdk/components/private/base_client.cpp @@ -11,6 +11,7 @@ #include #include +#include #include #include #include @@ -53,8 +54,8 @@ void BaseClient::set_power(const Vector3& linear, return make_client_helper(this, *stub_, &StubType::SetPower) .with(extra, [&](auto& request) { - *request.mutable_linear() = linear.to_proto(); - *request.mutable_angular() = angular.to_proto(); + *request.mutable_linear() = to_proto(linear); + *request.mutable_angular() = to_proto(angular); }) .invoke(); } @@ -65,8 +66,8 @@ void BaseClient::set_velocity(const Vector3& linear, return make_client_helper(this, *stub_, &StubType::SetVelocity) .with(extra, [&](auto& request) { - *request.mutable_linear() = linear.to_proto(); - *request.mutable_angular() = angular.to_proto(); + *request.mutable_linear() = to_proto(linear); + *request.mutable_angular() = to_proto(angular); }) .invoke(); } @@ -84,13 +85,17 @@ bool BaseClient::is_moving() { std::vector BaseClient::get_geometries(const ProtoStruct& extra) { return make_client_helper(this, *stub_, &StubType::GetGeometries) .with(extra) - .invoke([](auto& response) { return GeometryConfig::from_proto(response); }); + .invoke([](auto& response) { return from_proto(response); }); } Base::properties BaseClient::get_properties(const ProtoStruct& extra) { return make_client_helper(this, *stub_, &StubType::GetProperties) .with(extra) - .invoke([](auto& response) { return properties::from_proto(response); }); + .invoke([](auto& response) { + return properties{response.width_meters(), + response.turning_radius_meters(), + response.wheel_circumference_meters()}; + }); } ProtoStruct BaseClient::do_command(const ProtoStruct& command) { diff --git a/src/viam/sdk/components/private/base_server.cpp b/src/viam/sdk/components/private/base_server.cpp index a76a2024f..1da33fe0d 100644 --- a/src/viam/sdk/components/private/base_server.cpp +++ b/src/viam/sdk/components/private/base_server.cpp @@ -3,6 +3,7 @@ #include #include +#include #include #include #include @@ -40,8 +41,8 @@ ::grpc::Status BaseServer::SetPower(::grpc::ServerContext*, ::viam::component::base::v1::SetPowerResponse*) noexcept { return make_service_helper( "BaseServer::SetPower", this, request)([&](auto& helper, auto& base) { - auto linear = Vector3::from_proto(request->linear()); - auto angular = Vector3::from_proto(request->angular()); + auto linear = from_proto(request->linear()); + auto angular = from_proto(request->angular()); base->set_power(linear, angular, helper.getExtra()); }); } @@ -52,8 +53,8 @@ ::grpc::Status BaseServer::SetVelocity( ::viam::component::base::v1::SetVelocityResponse*) noexcept { return make_service_helper( "BaseServer::SetVelocity", this, request)([&](auto& helper, auto& base) { - auto linear = Vector3::from_proto(request->linear()); - auto angular = Vector3::from_proto(request->angular()); + auto linear = from_proto(request->linear()); + auto angular = from_proto(request->angular()); base->set_velocity(linear, angular, helper.getExtra()); }); } @@ -83,7 +84,7 @@ ::grpc::Status BaseServer::GetGeometries( "BaseServer::GetGeometries", this, request)([&](auto& helper, auto& base) { const std::vector geometries = base->get_geometries(helper.getExtra()); for (const auto& geometry : geometries) { - *response->mutable_geometries()->Add() = geometry.to_proto(); + *response->mutable_geometries()->Add() = to_proto(geometry); } }); } diff --git a/src/viam/sdk/components/private/board_client.cpp b/src/viam/sdk/components/private/board_client.cpp index 3f41357df..7337a5a13 100644 --- a/src/viam/sdk/components/private/board_client.cpp +++ b/src/viam/sdk/components/private/board_client.cpp @@ -11,6 +11,7 @@ #include #include +#include #include #include #include @@ -20,6 +21,32 @@ namespace viam { namespace sdk { namespace impl { +viam::component::board::v1::Status to_proto(const Board::status& status) { + viam::component::board::v1::Status proto; + for (const auto& analog : status.analog_reader_values) { + proto.mutable_analogs()->insert({analog.first, analog.second}); + } + + for (const auto& digital : status.digital_interrupt_values) { + proto.mutable_digital_interrupts()->insert({digital.first, digital.second}); + } + return proto; +} + +viam::component::board::v1::PowerMode to_proto(Board::power_mode power_mode) { + switch (power_mode) { + case Board::power_mode::normal: { + return viam::component::board::v1::POWER_MODE_NORMAL; + } + case Board::power_mode::offline_deep: { + return viam::component::board::v1::POWER_MODE_OFFLINE_DEEP; + } + default: { + throw Exception(ErrorCondition::k_not_supported, "Invalid board power_mode to encode"); + } + } +} + BoardClient::BoardClient(std::string name, std::shared_ptr channel) : Board(std::move(name)), stub_(viam::component::board::v1::BoardService::NewStub(channel)), @@ -156,7 +183,7 @@ void BoardClient::set_power_mode(power_mode power_mode, [&](auto& request) { request.set_power_mode(to_proto(power_mode)); if (duration.has_value()) { - *request.mutable_duration() = ::viam::sdk::to_proto(duration.get()); + *request.mutable_duration() = to_proto(duration.get()); } }) .invoke(); @@ -165,7 +192,7 @@ void BoardClient::set_power_mode(power_mode power_mode, std::vector BoardClient::get_geometries(const ProtoStruct& extra) { return make_client_helper(this, *stub_, &StubType::GetGeometries) .with(extra) - .invoke([](auto& response) { return GeometryConfig::from_proto(response); }); + .invoke([](auto& response) { return from_proto(response); }); }; } // namespace impl diff --git a/src/viam/sdk/components/private/board_server.cpp b/src/viam/sdk/components/private/board_server.cpp index ba4780ac2..228921f0c 100644 --- a/src/viam/sdk/components/private/board_server.cpp +++ b/src/viam/sdk/components/private/board_server.cpp @@ -1,5 +1,7 @@ #include +#include +#include #include #include #include @@ -11,6 +13,33 @@ namespace viam { namespace sdk { namespace impl { +Board::status from_proto(const viam::component::board::v1::Status& proto) { + Board::status status; + for (const auto& analog : proto.analogs()) { + status.analog_reader_values.emplace(analog.first, analog.second); + } + for (const auto& digital : proto.digital_interrupts()) { + status.digital_interrupt_values.emplace(digital.first, digital.second); + } + return status; +} + +Board::power_mode from_proto(viam::component::board::v1::PowerMode proto) { + switch (proto) { + case viam::component::board::v1::POWER_MODE_NORMAL: { + return Board::power_mode::normal; + } + case viam::component::board::v1::POWER_MODE_OFFLINE_DEEP: { + return Board::power_mode::offline_deep; + } + case viam::component::board::v1::POWER_MODE_UNSPECIFIED: + default: { + throw Exception(ErrorCondition::k_not_supported, + "Invalid proto board power_mode to decode"); + } + } +} + BoardServer::BoardServer(std::shared_ptr manager) : ResourceServer(std::move(manager)){}; @@ -198,11 +227,10 @@ ::grpc::Status BoardServer::SetPowerMode( return make_service_helper( "BoardServer::SetPowerMode", this, request)([&](auto& helper, auto& board) { if (request->has_duration()) { - auto duration = ::viam::sdk::from_proto(request->duration()); - board->set_power_mode( - Board::from_proto(request->power_mode()), helper.getExtra(), duration); + auto duration = from_proto(request->duration()); + board->set_power_mode(from_proto(request->power_mode()), helper.getExtra(), duration); } else { - board->set_power_mode(Board::from_proto(request->power_mode()), helper.getExtra()); + board->set_power_mode(from_proto(request->power_mode()), helper.getExtra()); } }); } @@ -215,7 +243,7 @@ ::grpc::Status BoardServer::GetGeometries( "BoardServer::GetGeometries", this, request)([&](auto& helper, auto& board) { const std::vector geometries = board->get_geometries(helper.getExtra()); for (const auto& geometry : geometries) { - *response->mutable_geometries()->Add() = geometry.to_proto(); + *response->mutable_geometries()->Add() = to_proto(geometry); } }); } diff --git a/src/viam/sdk/components/private/camera_client.cpp b/src/viam/sdk/components/private/camera_client.cpp index e0d9cf18c..3076e739b 100644 --- a/src/viam/sdk/components/private/camera_client.cpp +++ b/src/viam/sdk/components/private/camera_client.cpp @@ -9,6 +9,7 @@ #include #include +#include #include #include #include @@ -18,6 +19,87 @@ namespace viam { namespace sdk { namespace impl { +std::string format_to_MIME_string(viam::component::camera::v1::Format format) { + switch (format) { + case viam::component::camera::v1::FORMAT_RAW_RGBA: + return "image/vnd.viam.rgba"; + case viam::component::camera::v1::FORMAT_RAW_DEPTH: + return "image/vnd.viam.dep"; + case viam::component::camera::v1::FORMAT_JPEG: + return "image/jpeg"; + case viam::component::camera::v1::FORMAT_PNG: + return "image/png"; + default: + return ""; + } +} + +Camera::raw_image from_proto(const viam::component::camera::v1::GetImageResponse& proto) { + Camera::raw_image raw_image; + std::string img_string = proto.image(); + const std::vector bytes(img_string.begin(), img_string.end()); + raw_image.bytes = bytes; + raw_image.mime_type = proto.mime_type(); + raw_image.source_name = ""; + return raw_image; +} + +Camera::image_collection from_proto(const viam::component::camera::v1::GetImagesResponse& proto) { + Camera::image_collection image_collection; + std::vector images; + for (const auto& img : proto.images()) { + Camera::raw_image raw_image; + std::string img_string = img.image(); + const std::vector bytes(img_string.begin(), img_string.end()); + raw_image.bytes = bytes; + raw_image.mime_type = format_to_MIME_string(img.format()); + raw_image.source_name = img.source_name(); + images.push_back(raw_image); + } + image_collection.images = std::move(images); + image_collection.metadata = from_proto(proto.response_metadata()); + return image_collection; +} + +Camera::point_cloud from_proto(const viam::component::camera::v1::GetPointCloudResponse& proto) { + Camera::point_cloud point_cloud; + std::string pc_string = proto.point_cloud(); + const std::vector bytes(pc_string.begin(), pc_string.end()); + point_cloud.pc = bytes; + point_cloud.mime_type = proto.mime_type(); + return point_cloud; +} + +Camera::intrinsic_parameters from_proto( + const viam::component::camera::v1::IntrinsicParameters& proto) { + Camera::intrinsic_parameters params; + // NOLINTNEXTLINE(bugprone-narrowing-conversions) + params.width_px = proto.width_px(); + // NOLINTNEXTLINE(bugprone-narrowing-conversions) + params.height_px = proto.height_px(); + params.focal_x_px = proto.focal_x_px(); + params.focal_y_px = proto.focal_y_px(); + params.center_x_px = proto.center_x_px(); + params.center_y_px = proto.center_y_px(); + return params; +} + +Camera::distortion_parameters from_proto( + const viam::component::camera::v1::DistortionParameters& proto) { + Camera::distortion_parameters params; + params.model = proto.model(); + params.parameters = {proto.parameters().begin(), proto.parameters().end()}; + return params; +} + +Camera::properties from_proto(const viam::component::camera::v1::GetPropertiesResponse& proto) { + return {proto.supports_pcd(), + from_proto(proto.intrinsic_parameters()), + from_proto(proto.distortion_parameters()), + {proto.mime_types().begin(), proto.mime_types().end()}, + (proto.frame_rate())}; +} + CameraClient::CameraClient(std::string name, std::shared_ptr channel) : Camera(std::move(name)), stub_(viam::component::camera::v1::CameraService::NewStub(channel)), @@ -53,7 +135,7 @@ Camera::point_cloud CameraClient::get_point_cloud(std::string mime_type, const P std::vector CameraClient::get_geometries(const ProtoStruct& extra) { return make_client_helper(this, *stub_, &StubType::GetGeometries) .with(extra) - .invoke([](auto& response) { return GeometryConfig::from_proto(response); }); + .invoke([](auto& response) { return from_proto(response); }); }; Camera::properties CameraClient::get_properties() { diff --git a/src/viam/sdk/components/private/camera_server.cpp b/src/viam/sdk/components/private/camera_server.cpp index 372252ecc..e88f46fca 100644 --- a/src/viam/sdk/components/private/camera_server.cpp +++ b/src/viam/sdk/components/private/camera_server.cpp @@ -3,6 +3,7 @@ #include #include +#include #include #include #include @@ -14,6 +15,26 @@ namespace viam { namespace sdk { namespace impl { +viam::component::camera::v1::IntrinsicParameters to_proto( + const Camera::intrinsic_parameters& params) { + viam::component::camera::v1::IntrinsicParameters proto; + proto.set_width_px(params.width_px); + proto.set_height_px(params.height_px); + proto.set_focal_x_px(params.focal_x_px); + proto.set_focal_y_px(params.focal_y_px); + proto.set_center_x_px(params.center_x_px); + proto.set_center_y_px(params.center_y_px); + return proto; +} + +viam::component::camera::v1::DistortionParameters to_proto( + const Camera::distortion_parameters& params) { + viam::component::camera::v1::DistortionParameters proto; + *proto.mutable_model() = params.model; + *proto.mutable_parameters() = {params.parameters.begin(), params.parameters.end()}; + return proto; +} + CameraServer::CameraServer(std::shared_ptr manager) : ResourceServer(std::move(manager)){}; @@ -42,6 +63,22 @@ ::grpc::Status CameraServer::GetImage( }); } +::viam::component::camera::v1::Format MIME_string_to_format(const std::string& mime_string) { + if (mime_string == "image/vnd.viam.rgba") { + return viam::component::camera::v1::FORMAT_RAW_RGBA; + } + if (mime_string == "image/vnd.viam.dep") { + return viam::component::camera::v1::FORMAT_RAW_DEPTH; + } + if (mime_string == "image/jpeg") { + return viam::component::camera::v1::FORMAT_JPEG; + } + if (mime_string == "image/png") { + return viam::component::camera::v1::FORMAT_PNG; + } + return viam::component::camera::v1::FORMAT_UNSPECIFIED; +} + ::grpc::Status CameraServer::GetImages( ::grpc::ServerContext*, const ::viam::component::camera::v1::GetImagesRequest* request, @@ -54,11 +91,11 @@ ::grpc::Status CameraServer::GetImages( const std::string img_string = bytes_to_string(img.bytes); proto_image.set_source_name(img.source_name); proto_image.set_format( - Camera::MIME_string_to_format(Camera::normalize_mime_type(img.mime_type))); + MIME_string_to_format(Camera::normalize_mime_type(img.mime_type))); proto_image.set_image(img_string); *response->mutable_images()->Add() = std::move(proto_image); } - *response->mutable_response_metadata() = response_metadata::to_proto(image_coll.metadata); + *response->mutable_response_metadata() = to_proto(image_coll.metadata); }); } @@ -96,7 +133,7 @@ ::grpc::Status CameraServer::GetGeometries( "CameraServer::GetGeometries", this, request)([&](auto& helper, auto& camera) { const std::vector geometries = camera->get_geometries(helper.getExtra()); for (const auto& geometry : geometries) { - *response->mutable_geometries()->Add() = geometry.to_proto(); + *response->mutable_geometries()->Add() = to_proto(geometry); } }); } @@ -109,10 +146,8 @@ ::grpc::Status CameraServer::GetProperties( "CameraServer::GetProperties", this, request)([&](auto&, auto& camera) { const Camera::properties properties = camera->get_properties(); - *response->mutable_distortion_parameters() = - Camera::to_proto(properties.distortion_parameters); - *response->mutable_intrinsic_parameters() = - Camera::to_proto(properties.intrinsic_parameters); + *response->mutable_distortion_parameters() = to_proto(properties.distortion_parameters); + *response->mutable_intrinsic_parameters() = to_proto(properties.intrinsic_parameters); response->set_supports_pcd(properties.supports_pcd); response->set_frame_rate(properties.frame_rate); }); diff --git a/src/viam/sdk/components/private/encoder.cpp b/src/viam/sdk/components/private/encoder.cpp new file mode 100644 index 000000000..6d1637a0a --- /dev/null +++ b/src/viam/sdk/components/private/encoder.cpp @@ -0,0 +1,47 @@ +#include + +#include + +namespace viam { +namespace sdk { +namespace impl { + +Encoder::position_type from_proto(viam::component::encoder::v1::PositionType proto) { + switch (proto) { + case viam::component::encoder::v1::POSITION_TYPE_UNSPECIFIED: { + return Encoder::position_type::unspecified; + } + case viam::component::encoder::v1::POSITION_TYPE_ANGLE_DEGREES: { + return Encoder::position_type::angle_degrees; + } + case viam::component::encoder::v1::POSITION_TYPE_TICKS_COUNT: { + return Encoder::position_type::ticks_count; + } + default: { + throw Exception(ErrorCondition::k_not_supported, + "Invalid proto encoder type to decode"); + } + } +} + +viam::component::encoder::v1::PositionType to_proto(Encoder::position_type position_type) { + switch (position_type) { + case Encoder::position_type::unspecified: { + return viam::component::encoder::v1::PositionType::POSITION_TYPE_UNSPECIFIED; + } + case Encoder::position_type::angle_degrees: { + return viam::component::encoder::v1::POSITION_TYPE_ANGLE_DEGREES; + } + case Encoder::position_type::ticks_count: { + return viam::component::encoder::v1::POSITION_TYPE_TICKS_COUNT; + } + default: { + throw Exception(ErrorCondition::k_not_supported, + "Invalid proto encoder type to encode"); + } + } +} + +} // namespace impl +} // namespace sdk +} // namespace viam diff --git a/src/viam/sdk/components/private/encoder.hpp b/src/viam/sdk/components/private/encoder.hpp new file mode 100644 index 000000000..5b4376179 --- /dev/null +++ b/src/viam/sdk/components/private/encoder.hpp @@ -0,0 +1,17 @@ +#pragma once + +#include +#include + +#include + +namespace viam { +namespace sdk { +namespace impl { + +Encoder::position_type from_proto(viam::component::encoder::v1::PositionType proto); +viam::component::encoder::v1::PositionType to_proto(Encoder::position_type position_type); + +} // namespace impl +} // namespace sdk +} // namespace viam diff --git a/src/viam/sdk/components/private/encoder_client.cpp b/src/viam/sdk/components/private/encoder_client.cpp index 13da808ba..a844a603e 100644 --- a/src/viam/sdk/components/private/encoder_client.cpp +++ b/src/viam/sdk/components/private/encoder_client.cpp @@ -1,8 +1,6 @@ #include -#include #include -#include #include #include @@ -10,8 +8,10 @@ #include #include +#include #include #include +#include #include #include @@ -19,6 +19,30 @@ namespace viam { namespace sdk { namespace impl { +viam::component::encoder::v1::GetPositionResponse to_proto(const Encoder::position& position) { + viam::component::encoder::v1::GetPositionResponse proto; + proto.set_value(position.value); + + proto.set_position_type(to_proto(position.type)); + return proto; +} + +Encoder::position from_proto(const viam::component::encoder::v1::GetPositionResponse& proto) { + Encoder::position position; + position.value = proto.value(); + + position.type = from_proto(proto.position_type()); + return position; +} + +Encoder::properties from_proto(const viam::component::encoder::v1::GetPropertiesResponse& proto) { + Encoder::properties properties; + properties.ticks_count_supported = proto.ticks_count_supported(); + + properties.angle_degrees_supported = proto.angle_degrees_supported(); + return properties; +} + EncoderClient::EncoderClient(std::string name, std::shared_ptr channel) : Encoder(std::move(name)), stub_(viam::component::encoder::v1::EncoderService::NewStub(channel)), @@ -44,7 +68,7 @@ Encoder::properties EncoderClient::get_properties(const ProtoStruct& extra) { std::vector EncoderClient::get_geometries(const ProtoStruct& extra) { return make_client_helper(this, *stub_, &StubType::GetGeometries) .with(extra) - .invoke([](auto& response) { return GeometryConfig::from_proto(response); }); + .invoke([](auto& response) { return from_proto(response); }); }; ProtoStruct EncoderClient::do_command(const ProtoStruct& command) { diff --git a/src/viam/sdk/components/private/encoder_client.hpp b/src/viam/sdk/components/private/encoder_client.hpp index a4c96a78c..73935401b 100644 --- a/src/viam/sdk/components/private/encoder_client.hpp +++ b/src/viam/sdk/components/private/encoder_client.hpp @@ -8,9 +8,7 @@ #include #include -#include #include -#include namespace viam { namespace sdk { diff --git a/src/viam/sdk/components/private/encoder_server.cpp b/src/viam/sdk/components/private/encoder_server.cpp index 2bd48a73a..911df89d7 100644 --- a/src/viam/sdk/components/private/encoder_server.cpp +++ b/src/viam/sdk/components/private/encoder_server.cpp @@ -1,8 +1,11 @@ #include +#include +#include #include #include #include +#include #include #include @@ -20,9 +23,9 @@ ::grpc::Status EncoderServer::GetPosition( return make_service_helper( "EncoderServer::GetPosition", this, request)([&](auto& helper, auto& encoder) { const Encoder::position result = - encoder->get_position(helper.getExtra(), Encoder::from_proto(request->position_type())); + encoder->get_position(helper.getExtra(), from_proto(request->position_type())); response->set_value(result.value); - response->set_position_type(Encoder::to_proto(result.type)); + response->set_position_type(to_proto(result.type)); }); } @@ -54,7 +57,7 @@ ::grpc::Status EncoderServer::GetGeometries( "EncoderServer::GetGeometries", this, request)([&](auto& helper, auto& encoder) { const std::vector geometries = encoder->get_geometries(helper.getExtra()); for (const auto& geometry : geometries) { - *response->mutable_geometries()->Add() = geometry.to_proto(); + *response->mutable_geometries()->Add() = to_proto(geometry); } }); } diff --git a/src/viam/sdk/components/private/gantry_client.cpp b/src/viam/sdk/components/private/gantry_client.cpp index 18ac97b62..4f6be2659 100644 --- a/src/viam/sdk/components/private/gantry_client.cpp +++ b/src/viam/sdk/components/private/gantry_client.cpp @@ -4,6 +4,7 @@ #include #include +#include namespace viam { namespace sdk { @@ -72,7 +73,7 @@ ProtoStruct GantryClient::do_command(const ProtoStruct& command) { std::vector GantryClient::get_geometries(const ProtoStruct& extra) { return make_client_helper(this, *stub_, &StubType::GetGeometries) .with(extra) - .invoke([](auto& response) { return GeometryConfig::from_proto(response); }); + .invoke([](auto& response) { return from_proto(response); }); } } // namespace impl diff --git a/src/viam/sdk/components/private/gantry_server.cpp b/src/viam/sdk/components/private/gantry_server.cpp index 5819cd524..24c5ba5b1 100644 --- a/src/viam/sdk/components/private/gantry_server.cpp +++ b/src/viam/sdk/components/private/gantry_server.cpp @@ -1,5 +1,6 @@ #include +#include #include namespace viam { @@ -88,7 +89,7 @@ ::grpc::Status GantryServer::GetGeometries( "GantryServer::GetGeometries", this, request)([&](auto& helper, auto& gantry) { const std::vector geometries = gantry->get_geometries(helper.getExtra()); for (const auto& geometry : geometries) { - *response->mutable_geometries()->Add() = geometry.to_proto(); + *response->mutable_geometries()->Add() = to_proto(geometry); } }); } diff --git a/src/viam/sdk/components/private/generic_client.cpp b/src/viam/sdk/components/private/generic_client.cpp index 21087109a..21534fee6 100644 --- a/src/viam/sdk/components/private/generic_client.cpp +++ b/src/viam/sdk/components/private/generic_client.cpp @@ -6,6 +6,7 @@ #include #include +#include #include #include #include @@ -30,7 +31,7 @@ ProtoStruct GenericComponentClient::do_command(const ProtoStruct& command) { std::vector GenericComponentClient::get_geometries(const ProtoStruct& extra) { return make_client_helper(this, *stub_, &StubType::GetGeometries) .with(extra) - .invoke([](auto& response) { return GeometryConfig::from_proto(response); }); + .invoke([](auto& response) { return from_proto(response); }); } } // namespace impl diff --git a/src/viam/sdk/components/private/generic_server.cpp b/src/viam/sdk/components/private/generic_server.cpp index 0a151a1c8..3eae52a08 100644 --- a/src/viam/sdk/components/private/generic_server.cpp +++ b/src/viam/sdk/components/private/generic_server.cpp @@ -1,5 +1,6 @@ #include +#include #include #include #include @@ -29,7 +30,7 @@ ::grpc::Status GenericComponentServer::GetGeometries( "GenericComponentServer::GetGeometries", this, request)([&](auto& helper, auto& generic) { const std::vector geometries = generic->get_geometries(helper.getExtra()); for (const auto& geometry : geometries) { - *response->mutable_geometries()->Add() = geometry.to_proto(); + *response->mutable_geometries()->Add() = to_proto(geometry); } }); } diff --git a/src/viam/sdk/components/private/gripper_client.cpp b/src/viam/sdk/components/private/gripper_client.cpp index 7da3cea58..da2c30e67 100644 --- a/src/viam/sdk/components/private/gripper_client.cpp +++ b/src/viam/sdk/components/private/gripper_client.cpp @@ -4,6 +4,7 @@ #include #include +#include #include #include @@ -45,7 +46,7 @@ ProtoStruct GripperClient::do_command(const ProtoStruct& command) { std::vector GripperClient::get_geometries(const ProtoStruct& extra) { return make_client_helper(this, *stub_, &StubType::GetGeometries) .with(extra) - .invoke([](auto& response) { return GeometryConfig::from_proto(response); }); + .invoke([](auto& response) { return from_proto(response); }); } } // namespace impl diff --git a/src/viam/sdk/components/private/gripper_server.cpp b/src/viam/sdk/components/private/gripper_server.cpp index 33d3e07b3..e28eb67f0 100644 --- a/src/viam/sdk/components/private/gripper_server.cpp +++ b/src/viam/sdk/components/private/gripper_server.cpp @@ -1,5 +1,6 @@ #include +#include #include namespace viam { @@ -59,7 +60,7 @@ ::grpc::Status GripperServer::GetGeometries( "GripperServer::GetGeometries", this, request)([&](auto& helper, auto& gripper) { const std::vector geometries = gripper->get_geometries(helper.getExtra()); for (const auto& geometry : geometries) { - *response->mutable_geometries()->Add() = geometry.to_proto(); + *response->mutable_geometries()->Add() = to_proto(geometry); } }); } diff --git a/src/viam/sdk/components/private/motor_client.cpp b/src/viam/sdk/components/private/motor_client.cpp index b12f46d96..378a7198e 100644 --- a/src/viam/sdk/components/private/motor_client.cpp +++ b/src/viam/sdk/components/private/motor_client.cpp @@ -8,6 +8,7 @@ #include #include +#include #include #include #include @@ -16,6 +17,24 @@ namespace viam { namespace sdk { namespace impl { +Motor::position from_proto(const viam::component::motor::v1::GetPositionResponse& proto) { + return proto.position(); +} + +Motor::power_status from_proto(const viam::component::motor::v1::IsPoweredResponse& proto) { + Motor::power_status power_status; + power_status.is_on = proto.is_on(); + + power_status.power_pct = proto.power_pct(); + return power_status; +} + +Motor::properties from_proto(const viam::component::motor::v1::GetPropertiesResponse& proto) { + Motor::properties properties; + properties.position_reporting = proto.position_reporting(); + return properties; +} + MotorClient::MotorClient(std::string name, std::shared_ptr channel) : Motor(std::move(name)), stub_(viam::component::motor::v1::MotorService::NewStub(channel)), @@ -84,7 +103,7 @@ Motor::power_status MotorClient::get_power_status(const ProtoStruct& extra) { std::vector MotorClient::get_geometries(const ProtoStruct& extra) { return make_client_helper(this, *stub_, &StubType::GetGeometries) .with(extra) - .invoke([](auto& response) { return GeometryConfig::from_proto(response); }); + .invoke([](auto& response) { return from_proto(response); }); } bool MotorClient::is_moving() { diff --git a/src/viam/sdk/components/private/motor_server.cpp b/src/viam/sdk/components/private/motor_server.cpp index 70b7c4559..64eddb66b 100644 --- a/src/viam/sdk/components/private/motor_server.cpp +++ b/src/viam/sdk/components/private/motor_server.cpp @@ -1,5 +1,6 @@ #include +#include #include #include #include @@ -118,7 +119,7 @@ ::grpc::Status MotorServer::GetGeometries( "MotorServer::GetGeometries", this, request)([&](auto& helper, auto& motor) { const std::vector geometries = motor->get_geometries(helper.getExtra()); for (const auto& geometry : geometries) { - *response->mutable_geometries()->Add() = geometry.to_proto(); + *response->mutable_geometries()->Add() = to_proto(geometry); } }); } diff --git a/src/viam/sdk/components/private/movement_sensor_client.cpp b/src/viam/sdk/components/private/movement_sensor_client.cpp index e9be9228f..9fef53d7e 100644 --- a/src/viam/sdk/components/private/movement_sensor_client.cpp +++ b/src/viam/sdk/components/private/movement_sensor_client.cpp @@ -6,11 +6,9 @@ #include #include -#include -#include - #include #include +#include #include #include #include @@ -20,6 +18,42 @@ namespace viam { namespace sdk { namespace impl { +MovementSensor::compassheading from_proto( + const viam::component::movementsensor::v1::GetCompassHeadingResponse& proto) { + MovementSensor::compassheading compassheading; + compassheading.value = proto.value(); + return compassheading; +} + +MovementSensor::position from_proto( + const viam::component::movementsensor::v1::GetPositionResponse& proto) { + MovementSensor::position position; + position.coordinate = from_proto(proto.coordinate()); + position.altitude_m = proto.altitude_m(); + return position; +} + +MovementSensor::orientation from_proto(const viam::common::v1::Orientation& proto) { + MovementSensor::orientation orientation; + orientation.o_x = proto.o_x(); + orientation.o_y = proto.o_y(); + orientation.o_z = proto.o_z(); + orientation.theta = proto.theta(); + return orientation; +} + +MovementSensor::properties from_proto( + const viam::component::movementsensor::v1::GetPropertiesResponse& proto) { + MovementSensor::properties properties; + properties.linear_velocity_supported = proto.linear_velocity_supported(); + properties.angular_velocity_supported = proto.angular_velocity_supported(); + properties.orientation_supported = proto.orientation_supported(); + properties.position_supported = proto.position_supported(); + properties.compass_heading_supported = proto.compass_heading_supported(); + properties.linear_acceleration_supported = proto.linear_acceleration_supported(); + return properties; +} + MovementSensorClient::MovementSensorClient(std::string name, std::shared_ptr channel) : MovementSensor(std::move(name)), stub_(viam::component::movementsensor::v1::MovementSensorService::NewStub(channel)), @@ -30,13 +64,13 @@ using namespace viam::component::movementsensor::v1; Vector3 MovementSensorClient::get_linear_velocity(const ProtoStruct& extra) { return make_client_helper(this, *stub_, &StubType::GetLinearVelocity) .with(extra) - .invoke([](auto& response) { return Vector3::from_proto(response.linear_velocity()); }); + .invoke([](auto& response) { return from_proto(response.linear_velocity()); }); } Vector3 MovementSensorClient::get_angular_velocity(const ProtoStruct& extra) { return make_client_helper(this, *stub_, &StubType::GetAngularVelocity) .with(extra) - .invoke([](auto& response) { return Vector3::from_proto(response.angular_velocity()); }); + .invoke([](auto& response) { return from_proto(response.angular_velocity()); }); } MovementSensor::compassheading MovementSensorClient::get_compass_heading(const ProtoStruct& extra) { @@ -79,7 +113,7 @@ std::unordered_map MovementSensorClient::get_accuracy( Vector3 MovementSensorClient::get_linear_acceleration(const ProtoStruct& extra) { return make_client_helper(this, *stub_, &StubType::GetLinearAcceleration) .with(extra) - .invoke([](auto& response) { return Vector3::from_proto(response.linear_acceleration()); }); + .invoke([](auto& response) { return from_proto(response.linear_acceleration()); }); } ProtoStruct MovementSensorClient::do_command(const ProtoStruct& command) { @@ -91,7 +125,7 @@ ProtoStruct MovementSensorClient::do_command(const ProtoStruct& command) { std::vector MovementSensorClient::get_geometries(const ProtoStruct& extra) { return make_client_helper(this, *stub_, &StubType::GetGeometries) .with(extra) - .invoke([](auto& response) { return GeometryConfig::from_proto(response); }); + .invoke([](auto& response) { return from_proto(response); }); } } // namespace impl diff --git a/src/viam/sdk/components/private/movement_sensor_server.cpp b/src/viam/sdk/components/private/movement_sensor_server.cpp index eb44c409a..b3f464de8 100644 --- a/src/viam/sdk/components/private/movement_sensor_server.cpp +++ b/src/viam/sdk/components/private/movement_sensor_server.cpp @@ -1,6 +1,7 @@ #include #include +#include #include #include #include @@ -13,6 +14,15 @@ namespace viam { namespace sdk { namespace impl { +viam::common::v1::Orientation to_proto(const MovementSensor::orientation& orientation) { + viam::common::v1::Orientation proto; + proto.set_o_x(orientation.o_x); + proto.set_o_y(orientation.o_y); + proto.set_o_z(orientation.o_z); + proto.set_theta(orientation.theta); + return proto; +} + MovementSensorServer::MovementSensorServer(std::shared_ptr manager) : ResourceServer(std::move(manager)){}; @@ -24,7 +34,7 @@ ::grpc::Status MovementSensorServer::GetLinearVelocity( this, request)([&](auto& helper, auto& movementsensor) { const Vector3 result = movementsensor->get_linear_velocity(helper.getExtra()); - *response->mutable_linear_velocity() = result.to_proto(); + *response->mutable_linear_velocity() = to_proto(result); }); } @@ -36,7 +46,7 @@ ::grpc::Status MovementSensorServer::GetAngularVelocity( this, request)([&](auto& helper, auto& movementsensor) { const Vector3 result = movementsensor->get_angular_velocity(helper.getExtra()); - *response->mutable_angular_velocity() = result.to_proto(); + *response->mutable_angular_velocity() = to_proto(result); }); } @@ -61,7 +71,7 @@ ::grpc::Status MovementSensorServer::GetOrientation(::grpc::ServerContext*, request)([&](auto& helper, auto& movementsensor) { const MovementSensor::orientation result = movementsensor->get_orientation(helper.getExtra()); - *response->mutable_orientation() = MovementSensor::to_proto(result); + *response->mutable_orientation() = to_proto(result); }); } @@ -71,7 +81,7 @@ ::grpc::Status MovementSensorServer::GetPosition(::grpc::ServerContext*, return make_service_helper("MovementSensorServer::GetPosition", this, request)( [&](auto& helper, auto& movementsensor) { const MovementSensor::position result = movementsensor->get_position(helper.getExtra()); - *response->mutable_coordinate() = result.coordinate.to_proto(); + *response->mutable_coordinate() = to_proto(result.coordinate); response->set_altitude_m(result.altitude_m); }); } @@ -112,7 +122,7 @@ ::grpc::Status MovementSensorServer::GetLinearAcceleration( this, request)([&](auto& helper, auto& movementsensor) { const Vector3 result = movementsensor->get_linear_acceleration(helper.getExtra()); - *response->mutable_linear_acceleration() = result.to_proto(); + *response->mutable_linear_acceleration() = to_proto(result); }); } @@ -136,7 +146,7 @@ ::grpc::Status MovementSensorServer::GetGeometries( request)([&](auto& helper, auto& movementsensor) { const auto geometries = movementsensor->get_geometries(helper.getExtra()); for (const auto& geometry : geometries) { - *response->mutable_geometries()->Add() = geometry.to_proto(); + *response->mutable_geometries()->Add() = to_proto(geometry); } }); } diff --git a/src/viam/sdk/components/private/pose_tracker_client.cpp b/src/viam/sdk/components/private/pose_tracker_client.cpp index 6b2794540..4a9da6b41 100644 --- a/src/viam/sdk/components/private/pose_tracker_client.cpp +++ b/src/viam/sdk/components/private/pose_tracker_client.cpp @@ -5,6 +5,7 @@ #include #include +#include namespace viam { namespace sdk { @@ -25,7 +26,7 @@ PoseTracker::pose_map PoseTrackerClient::get_poses(const std::vector PoseTrackerClient::get_geometries(const ProtoStruct& extra) { return make_client_helper(this, *stub_, &StubType::GetGeometries) .with(extra) - .invoke([](auto& response) { return GeometryConfig::from_proto(response); }); + .invoke([](auto& response) { return from_proto(response); }); } ProtoStruct PoseTrackerClient::do_command(const ProtoStruct& command) { diff --git a/src/viam/sdk/components/private/pose_tracker_server.cpp b/src/viam/sdk/components/private/pose_tracker_server.cpp index ea26daaa3..3202d1129 100644 --- a/src/viam/sdk/components/private/pose_tracker_server.cpp +++ b/src/viam/sdk/components/private/pose_tracker_server.cpp @@ -2,6 +2,7 @@ #include +#include #include #include #include @@ -25,7 +26,7 @@ ::grpc::Status PoseTrackerServer::GetPoses( {request->body_names().begin(), request->body_names().end()}, helper.getExtra()); for (const auto& pair : result) { - response->mutable_body_poses()->insert({pair.first, pair.second.to_proto()}); + response->mutable_body_poses()->insert({pair.first, to_proto(pair.second)}); } }); } @@ -50,7 +51,7 @@ ::grpc::Status PoseTrackerServer::GetGeometries( const std::vector geometries = pose_tracker->get_geometries(helper.getExtra()); for (const auto& geometry : geometries) { - *response->mutable_geometries()->Add() = geometry.to_proto(); + *response->mutable_geometries()->Add() = to_proto(geometry); } }); } diff --git a/src/viam/sdk/components/private/power_sensor_client.cpp b/src/viam/sdk/components/private/power_sensor_client.cpp index be337537e..63f2cc6b8 100644 --- a/src/viam/sdk/components/private/power_sensor_client.cpp +++ b/src/viam/sdk/components/private/power_sensor_client.cpp @@ -21,6 +21,20 @@ namespace viam { namespace sdk { namespace impl { +PowerSensor::voltage from_proto(const GetVoltageResponse& proto) { + PowerSensor::voltage v; + v.volts = proto.volts(); + v.is_ac = proto.is_ac(); + return v; +} + +PowerSensor::current from_proto(const GetCurrentResponse& proto) { + PowerSensor::current c; + c.amperes = proto.amperes(); + c.is_ac = proto.is_ac(); + return c; +} + PowerSensorClient::PowerSensorClient(std::string name, std::shared_ptr channel) : PowerSensor(std::move(name)), stub_(PowerSensorService::NewStub(channel)), diff --git a/src/viam/sdk/components/private/power_sensor_server.cpp b/src/viam/sdk/components/private/power_sensor_server.cpp index b15589623..69461775f 100644 --- a/src/viam/sdk/components/private/power_sensor_server.cpp +++ b/src/viam/sdk/components/private/power_sensor_server.cpp @@ -12,6 +12,20 @@ namespace viam { namespace sdk { namespace impl { +GetVoltageResponse to_proto(const PowerSensor::voltage& v) { + GetVoltageResponse proto; + proto.set_volts(v.volts); + proto.set_is_ac(v.is_ac); + return proto; +} + +GetCurrentResponse to_proto(const PowerSensor::current& c) { + GetCurrentResponse proto; + proto.set_amperes(c.amperes); + proto.set_is_ac(c.is_ac); + return proto; +} + PowerSensorServer::PowerSensorServer(std::shared_ptr manager) : ResourceServer(std::move(manager)){}; @@ -21,7 +35,7 @@ ::grpc::Status PowerSensorServer::GetVoltage(::grpc::ServerContext*, return make_service_helper( "PowerSensorServer::GetVoltage", this, request)([&](auto& helper, auto& powersensor) { const PowerSensor::voltage result = powersensor->get_voltage(helper.getExtra()); - *response = PowerSensor::to_proto(result); + *response = to_proto(result); }); } @@ -31,7 +45,7 @@ ::grpc::Status PowerSensorServer::GetCurrent(::grpc::ServerContext*, return make_service_helper( "PowerSensorServer::GetCurrent", this, request)([&](auto& helper, auto& powersensor) { const PowerSensor::current result = powersensor->get_current(helper.getExtra()); - *response = PowerSensor::to_proto(result); + *response = to_proto(result); }); } diff --git a/src/viam/sdk/components/private/sensor_client.cpp b/src/viam/sdk/components/private/sensor_client.cpp index 3780e256a..5cb204639 100644 --- a/src/viam/sdk/components/private/sensor_client.cpp +++ b/src/viam/sdk/components/private/sensor_client.cpp @@ -1,8 +1,6 @@ #include -#include #include -#include #include #include @@ -10,6 +8,7 @@ #include #include +#include #include #include #include @@ -46,7 +45,7 @@ ProtoStruct SensorClient::do_command(const ProtoStruct& command) { std::vector SensorClient::get_geometries(const ProtoStruct& extra) { return make_client_helper(this, *stub_, &StubType::GetGeometries) .with(extra) - .invoke([](auto& response) { return GeometryConfig::from_proto(response); }); + .invoke([](auto& response) { return from_proto(response); }); } } // namespace impl diff --git a/src/viam/sdk/components/private/sensor_server.cpp b/src/viam/sdk/components/private/sensor_server.cpp index f763ab0b7..940d2e76e 100644 --- a/src/viam/sdk/components/private/sensor_server.cpp +++ b/src/viam/sdk/components/private/sensor_server.cpp @@ -1,5 +1,6 @@ #include +#include #include #include #include @@ -44,7 +45,7 @@ ::grpc::Status SensorServer::GetGeometries(::grpc::ServerContext*, "SensorServer::GetGeometries", this, request)([&](auto& helper, auto& sensor) { const auto geometries = sensor->get_geometries(helper.getExtra()); for (const auto& geometry : geometries) { - *response->mutable_geometries()->Add() = geometry.to_proto(); + *response->mutable_geometries()->Add() = to_proto(geometry); } }); } diff --git a/src/viam/sdk/components/private/servo_client.cpp b/src/viam/sdk/components/private/servo_client.cpp index 46ed8a070..305b0bede 100644 --- a/src/viam/sdk/components/private/servo_client.cpp +++ b/src/viam/sdk/components/private/servo_client.cpp @@ -10,6 +10,7 @@ #include #include +#include #include #include #include @@ -18,6 +19,10 @@ namespace viam { namespace sdk { namespace impl { +Servo::position from_proto(const viam::component::servo::v1::GetPositionResponse& proto) { + return proto.position_deg(); +} + ServoClient::ServoClient(std::string name, std::shared_ptr channel) : Servo(std::move(name)), stub_(viam::component::servo::v1::ServoService::NewStub(channel)), @@ -48,7 +53,7 @@ bool ServoClient::is_moving() { std::vector ServoClient::get_geometries(const ProtoStruct& extra) { return make_client_helper(this, *stub_, &StubType::GetGeometries) .with(extra) - .invoke([](auto& response) { return GeometryConfig::from_proto(response); }); + .invoke([](auto& response) { return from_proto(response); }); } ProtoStruct ServoClient::do_command(const ProtoStruct& command) { diff --git a/src/viam/sdk/components/private/servo_client.hpp b/src/viam/sdk/components/private/servo_client.hpp index 20e123612..e1a68dd7d 100644 --- a/src/viam/sdk/components/private/servo_client.hpp +++ b/src/viam/sdk/components/private/servo_client.hpp @@ -7,7 +7,6 @@ #include -#include #include #include #include diff --git a/src/viam/sdk/components/private/servo_server.cpp b/src/viam/sdk/components/private/servo_server.cpp index 7b3b937e1..7bdc185bc 100644 --- a/src/viam/sdk/components/private/servo_server.cpp +++ b/src/viam/sdk/components/private/servo_server.cpp @@ -1,5 +1,6 @@ #include +#include #include #include #include @@ -57,7 +58,7 @@ ::grpc::Status ServoServer::GetGeometries( "ServoServer::GetGeometries", this, request)([&](auto& helper, auto& servo) { const std::vector geometries = servo->get_geometries(helper.getExtra()); for (const auto& geometry : geometries) { - *response->mutable_geometries()->Add() = geometry.to_proto(); + *response->mutable_geometries()->Add() = to_proto(geometry); } }); } diff --git a/src/viam/sdk/components/sensor.cpp b/src/viam/sdk/components/sensor.cpp index d25b5bed3..c91d6015b 100644 --- a/src/viam/sdk/components/sensor.cpp +++ b/src/viam/sdk/components/sensor.cpp @@ -1,10 +1,5 @@ #include -#include -#include - -#include - namespace viam { namespace sdk { diff --git a/src/viam/sdk/components/sensor.hpp b/src/viam/sdk/components/sensor.hpp index 04f86b9a0..96954b7ff 100644 --- a/src/viam/sdk/components/sensor.hpp +++ b/src/viam/sdk/components/sensor.hpp @@ -5,8 +5,6 @@ #include -#include - #include #include #include diff --git a/src/viam/sdk/components/servo.cpp b/src/viam/sdk/components/servo.cpp index eeda4fb24..7d9033c52 100644 --- a/src/viam/sdk/components/servo.cpp +++ b/src/viam/sdk/components/servo.cpp @@ -1,11 +1,5 @@ #include -#include -#include - -#include -#include - namespace viam { namespace sdk { @@ -17,9 +11,5 @@ API API::traits::api() { return {kRDK, kComponent, "servo"}; } -Servo::position Servo::from_proto(const viam::component::servo::v1::GetPositionResponse& proto) { - return proto.position_deg(); -} - } // namespace sdk } // namespace viam diff --git a/src/viam/sdk/components/servo.hpp b/src/viam/sdk/components/servo.hpp index b3601f3c6..91ee33271 100644 --- a/src/viam/sdk/components/servo.hpp +++ b/src/viam/sdk/components/servo.hpp @@ -3,8 +3,6 @@ /// @brief Defines a `Servo` component. #pragma once -#include - #include #include #include @@ -28,9 +26,6 @@ class Servo : public Component, public Stoppable { API api() const override; - /// @brief Creates a `position` struct from its proto representation. - static position from_proto(const viam::component::servo::v1::GetPositionResponse& proto); - /// @brief Move the servo to the provided angle /// @param angle_deg The desired angle of the servo in degrees. inline void move(uint32_t angle_deg) { diff --git a/src/viam/sdk/config/resource.cpp b/src/viam/sdk/config/resource.cpp index 758a52637..8e42bae0d 100644 --- a/src/viam/sdk/config/resource.cpp +++ b/src/viam/sdk/config/resource.cpp @@ -2,13 +2,9 @@ #include #include -#include #include -#include -#include - #include #include #include @@ -89,53 +85,32 @@ void ResourceConfig::fix_api() { } } -ResourceConfig ResourceConfig::from_proto(const viam::app::v1::ComponentConfig& proto_cfg) { - ResourceConfig resource(proto_cfg.type()); - resource.name_ = proto_cfg.name(); - resource.namespace__ = proto_cfg.namespace_(); - resource.type_ = proto_cfg.type(); - resource.attributes_ = struct_to_map(proto_cfg.attributes()); - const std::string& api = proto_cfg.api(); - if (api.find(':') != std::string::npos) { - resource.api_ = API::from_string(api); - } - resource.model_ = Model::from_str(proto_cfg.model()); - - resource.fix_api(); - - if (proto_cfg.has_frame()) { - resource.frame_ = LinkConfig::from_proto(proto_cfg.frame()); - } - - return resource; +const std::vector& ResourceConfig::depends_on() const { + return depends_on_; }; -viam::app::v1::ComponentConfig ResourceConfig::to_proto() const { - viam::app::v1::ComponentConfig proto_cfg; - const google::protobuf::Struct s = map_to_struct(attributes_); - const google::protobuf::RepeatedPtrField - service_configs; - - for (const auto& svc_cfg : service_config_) { - viam::app::v1::ResourceLevelServiceConfig cfg; - *cfg.mutable_type() = svc_cfg.type; - *cfg.mutable_attributes() = map_to_struct(svc_cfg.attributes); - *proto_cfg.mutable_service_configs()->Add() = cfg; - } +const std::vector& ResourceConfig::service_config() const { + return service_config_; +}; - *proto_cfg.mutable_name() = name_; - *proto_cfg.mutable_namespace_() = namespace__; - *proto_cfg.mutable_type() = type_; - *proto_cfg.mutable_api() = api_.to_string(); - const std::string mm = model_.to_string(); - *proto_cfg.mutable_model() = mm; - *proto_cfg.mutable_attributes() = map_to_struct(attributes_); - for (const auto& dep : depends_on_) { - *proto_cfg.mutable_depends_on()->Add() = dep; +ResourceConfig::ResourceConfig(std::string type, + std::string name, + std::string namespace_, + ProtoStruct attributes, + std::string api, + Model model, + LinkConfig frame) + : api_({kRDK, type, ""}), + frame_(std::move(frame)), + model_(std::move(model)), + name_(std::move(name)), + namespace__(std::move(namespace_)), + type_(std::move(type)), + attributes_(std::move(attributes)) { + if (api.find(':') != std::string::npos) { + api_ = API::from_string(std::move(api)); } - *proto_cfg.mutable_frame() = frame_.to_proto(); - - return proto_cfg; + fix_api(); } ResourceConfig::ResourceConfig(std::string type) : api_({kRDK, type, ""}), type_(std::move(type)){}; diff --git a/src/viam/sdk/config/resource.hpp b/src/viam/sdk/config/resource.hpp index a89ff0f9e..f84ee1258 100644 --- a/src/viam/sdk/config/resource.hpp +++ b/src/viam/sdk/config/resource.hpp @@ -1,10 +1,6 @@ #pragma once #include -#include - -#include -#include #include #include @@ -22,9 +18,14 @@ class ResourceLevelServiceConfig { class ResourceConfig { public: - static ResourceConfig from_proto(const viam::app::v1::ComponentConfig& proto_cfg); - viam::app::v1::ComponentConfig to_proto() const; ResourceConfig(std::string type); + ResourceConfig(std::string type, + std::string name, + std::string namespace_, + ProtoStruct attributes, + std::string api, + Model model, + LinkConfig frame); Name resource_name(); const API& api() const; const LinkConfig& frame() const; @@ -33,6 +34,8 @@ class ResourceConfig { const std::string& namespace_() const; const std::string& type() const; const ProtoStruct& attributes() const; + const std::vector& depends_on() const; + const std::vector& service_config() const; private: API api_; diff --git a/src/viam/sdk/module/handler_map.cpp b/src/viam/sdk/module/handler_map.cpp index d6e5fe0ed..dd7df0ef2 100644 --- a/src/viam/sdk/module/handler_map.cpp +++ b/src/viam/sdk/module/handler_map.cpp @@ -1,70 +1,14 @@ #include -#include - -#include -#include - -#include -#include -#include - -#include - namespace viam { namespace sdk { -viam::module::v1::HandlerMap HandlerMap_::to_proto() const { - viam::module::v1::HandlerMap proto; - for (const auto& h : this->handles_) { - viam::module::v1::HandlerDefinition hd; - for (const auto& model : h.second) { - const std::string m = model.to_string(); - *hd.mutable_models()->Add() = m; - } - viam::robot::v1::ResourceRPCSubtype rpc_subtype; - const Name name(h.first.api(), "", ""); - const viam::common::v1::ResourceName resource_name = name.to_proto(); - *rpc_subtype.mutable_subtype() = resource_name; - *rpc_subtype.mutable_proto_service() = h.first.proto_service_name(); - *hd.mutable_subtype() = rpc_subtype; - *proto.add_handlers() = hd; - } - - return proto; -}; +const std::unordered_map>& HandlerMap_::handles() const { + return handles_; +} HandlerMap_::HandlerMap_(){}; -// NOLINTNEXTLINE(readability-const-return-type) -const HandlerMap_ HandlerMap_::from_proto(const viam::module::v1::HandlerMap& proto) { - HandlerMap_ hm; - - const google::protobuf::RepeatedPtrField& handlers = - proto.handlers(); - - for (const auto& handler : handlers) { - std::vector models; - const viam::common::v1::ResourceName name = handler.subtype().subtype(); - const API api(name.namespace_(), name.type(), name.subtype()); - const google::protobuf::DescriptorPool* pool = - google::protobuf::DescriptorPool::generated_pool(); - const google::protobuf::ServiceDescriptor* sd = pool->FindServiceByName(name.type()); - const RPCSubtype handle(api, *sd); - for (const auto& mod : handler.models()) { - try { - const Model model = Model::from_str(mod); - models.push_back(model); - } catch (std::string error) { // NOLINT - BOOST_LOG_TRIVIAL(error) << "Error processing model " + mod; - } - } - hm.handles_.emplace(handle, models); - } - - return hm; -} - void HandlerMap_::add_model(Model model, const RPCSubtype& subtype) { handles_[subtype].push_back(std::move(model)); } diff --git a/src/viam/sdk/module/handler_map.hpp b/src/viam/sdk/module/handler_map.hpp index ad83db867..4d448268f 100644 --- a/src/viam/sdk/module/handler_map.hpp +++ b/src/viam/sdk/module/handler_map.hpp @@ -1,8 +1,8 @@ #pragma once -#include +#include -#include +#include namespace viam { namespace sdk { @@ -12,9 +12,8 @@ class HandlerMap_ { HandlerMap_(); void add_model(Model model, const RPCSubtype& subtype); - viam::module::v1::HandlerMap to_proto() const; - static const HandlerMap_ from_proto(const viam::module::v1::HandlerMap& proto); friend std::ostream& operator<<(std::ostream& os, const HandlerMap_& hm); + const std::unordered_map>& handles() const; private: std::unordered_map> handles_; diff --git a/src/viam/sdk/module/service.cpp b/src/viam/sdk/module/service.cpp index b1bad21ac..528ac8500 100644 --- a/src/viam/sdk/module/service.cpp +++ b/src/viam/sdk/module/service.cpp @@ -25,6 +25,7 @@ #include #include +#include #include #include #include @@ -73,7 +74,7 @@ ::grpc::Status ModuleService::AddResource(::grpc::ServerContext*, const ::viam::module::v1::AddResourceRequest* request, ::viam::module::v1::AddResourceResponse*) { const viam::app::v1::ComponentConfig& proto = request->config(); - const ResourceConfig cfg = ResourceConfig::from_proto(proto); + const ResourceConfig cfg = impl::from_proto(proto); const std::lock_guard lock(lock_); std::shared_ptr res; @@ -101,7 +102,7 @@ ::grpc::Status ModuleService::ReconfigureResource( const ::viam::module::v1::ReconfigureResourceRequest* request, ::viam::module::v1::ReconfigureResourceResponse*) { const viam::app::v1::ComponentConfig& proto = request->config(); - ResourceConfig cfg = ResourceConfig::from_proto(proto); + ResourceConfig cfg = impl::from_proto(proto); const Dependencies deps = get_dependencies_(request->dependencies(), cfg.name()); @@ -150,7 +151,7 @@ ::grpc::Status ModuleService::ValidateConfig( const ::viam::module::v1::ValidateConfigRequest* request, ::viam::module::v1::ValidateConfigResponse* response) { const viam::app::v1::ComponentConfig& proto = request->config(); - ResourceConfig cfg = ResourceConfig::from_proto(proto); + ResourceConfig cfg = impl::from_proto(proto); const std::shared_ptr reg = Registry::lookup_model(cfg.api(), cfg.model()); @@ -202,7 +203,7 @@ ::grpc::Status ModuleService::Ready(::grpc::ServerContext*, const ::viam::module::v1::ReadyRequest* request, ::viam::module::v1::ReadyResponse* response) { const std::lock_guard lock(lock_); - const viam::module::v1::HandlerMap hm = this->module_->handles().to_proto(); + const viam::module::v1::HandlerMap hm = impl::to_proto(this->module_->handles()); *response->mutable_handlermap() = hm; parent_addr_ = request->parent_address(); response->set_ready(module_->ready()); diff --git a/src/viam/sdk/referenceframe/frame.cpp b/src/viam/sdk/referenceframe/frame.cpp index 299942384..b07157a59 100644 --- a/src/viam/sdk/referenceframe/frame.cpp +++ b/src/viam/sdk/referenceframe/frame.cpp @@ -2,9 +2,6 @@ #include -#include -#include - #include #include #include @@ -12,34 +9,16 @@ namespace viam { namespace sdk { -viam::app::v1::Frame LinkConfig::to_proto() const { - viam::app::v1::Frame frame; - - *frame.mutable_parent() = parent_; - *frame.mutable_geometry() = geometry_.to_proto(); - *frame.mutable_orientation() = orientation_.to_proto(); - *frame.mutable_translation() = translation_.to_proto(); - return frame; -}; - -LinkConfig LinkConfig::from_proto(const viam::app::v1::Frame& proto) { - LinkConfig lc; - - lc.parent_ = proto.parent(); - lc.translation_.x = proto.translation().x(); - lc.translation_.y = proto.translation().y(); - lc.translation_.z = proto.translation().z(); - - if (proto.has_orientation()) { - lc.orientation_ = OrientationConfig::from_proto(proto.orientation()); - } - - if (proto.has_geometry()) { - lc.geometry_ = GeometryConfig::from_proto(proto.geometry()); - } +LinkConfig::LinkConfig(translation translation, + OrientationConfig ocfg, + GeometryConfig gcfg, + std::string parent) + : translation_(std::move(translation)), + orientation_(std::move(ocfg)), + geometry_(std::move(gcfg)), + parent_(std::move(parent)) {} - return lc; -}; +LinkConfig::LinkConfig() = default; translation LinkConfig::get_translation() const { return translation_; diff --git a/src/viam/sdk/referenceframe/frame.hpp b/src/viam/sdk/referenceframe/frame.hpp index 4d5f593e5..a11b27a55 100644 --- a/src/viam/sdk/referenceframe/frame.hpp +++ b/src/viam/sdk/referenceframe/frame.hpp @@ -2,8 +2,6 @@ #include -#include - #include #include @@ -12,15 +10,17 @@ namespace sdk { class LinkConfig { public: - viam::app::v1::Frame to_proto() const; - static LinkConfig from_proto(const viam::app::v1::Frame& proto); + LinkConfig(); + LinkConfig(translation translation, + OrientationConfig ocfg, + GeometryConfig gcfg, + std::string parent); translation get_translation() const; OrientationConfig get_orientation_config() const; GeometryConfig get_geometry_config() const; std::string get_parent() const; private: - std::string id_; translation translation_; OrientationConfig orientation_; GeometryConfig geometry_; diff --git a/src/viam/sdk/registry/registry.cpp b/src/viam/sdk/registry/registry.cpp index e0676af4d..540eb8907 100644 --- a/src/viam/sdk/registry/registry.cpp +++ b/src/viam/sdk/registry/registry.cpp @@ -1,6 +1,5 @@ #include -#include #include #include #include @@ -55,8 +54,6 @@ namespace viam { namespace sdk { -using viam::robot::v1::Status; - ResourceServerRegistration::~ResourceServerRegistration() = default; ResourceClientRegistration::~ResourceClientRegistration() = default; @@ -159,14 +156,6 @@ Registry::registered_models() { return resources_; } -// NOLINTNEXTLINE(readability-convert-member-functions-to-static) -Status ModelRegistration::create_status(const std::shared_ptr& resource) const { - Status status; - *status.mutable_name() = resource->get_resource_name(resource->name()); - *status.mutable_status() = google::protobuf::Struct(); - return status; -} - const google::protobuf::ServiceDescriptor* ResourceServerRegistration::service_descriptor() const { return service_descriptor_; } diff --git a/src/viam/sdk/registry/registry.hpp b/src/viam/sdk/registry/registry.hpp index 436600e8a..5fd8b4c63 100644 --- a/src/viam/sdk/registry/registry.hpp +++ b/src/viam/sdk/registry/registry.hpp @@ -5,8 +5,6 @@ #include -#include -#include #include #include #include @@ -95,9 +93,6 @@ class ModelRegistration { /// @throws Can throw exceptions, which will be returned to the parent via gRPC. std::function(ResourceConfig)> validate; - /// @brief Creates a `Status` object for a given resource. - viam::robot::v1::Status create_status(const std::shared_ptr& resource) const; - private: // default_validator is the default validator for all models if no validator // is provided in construction. No dependencies are returned. diff --git a/src/viam/sdk/resource/resource.cpp b/src/viam/sdk/resource/resource.cpp index 04b304679..04a8328fc 100644 --- a/src/viam/sdk/resource/resource.cpp +++ b/src/viam/sdk/resource/resource.cpp @@ -1,7 +1,5 @@ #include -#include - #include #include #include @@ -10,8 +8,6 @@ namespace viam { namespace sdk { -using common::v1::ResourceName; - Resource::~Resource() = default; Resource::Resource(std::string name) : name_(std::move(name)) {} @@ -19,14 +15,17 @@ std::string Resource::name() const { return name_; } -ResourceName Resource::get_resource_name(std::string name) const { - ResourceName r; - *r.mutable_namespace_() = kRDK; - *r.mutable_type() = kResource; - *r.mutable_subtype() = this->api().resource_subtype(); - *r.mutable_name() = std::move(name); +Name Resource::get_resource_name(const std::string& type) const { + auto api_ = api(); + api_.set_resource_type(type); + + auto name_parts = long_name_to_remote_and_short(name_); + + return {api_, name_parts.first, name_parts.second}; +} - return r; +Name Resource::get_resource_name() const { + return get_resource_name(kResource); } } // namespace sdk diff --git a/src/viam/sdk/resource/resource.hpp b/src/viam/sdk/resource/resource.hpp index 0d1d55eb7..11c950e76 100644 --- a/src/viam/sdk/resource/resource.hpp +++ b/src/viam/sdk/resource/resource.hpp @@ -22,14 +22,17 @@ class Resource { /// @brief Returns the `API` associated with a particular resource. virtual API api() const = 0; - /// @brief Returns a `ResourceName` for a particular resource name. - virtual viam::common::v1::ResourceName get_resource_name(std::string name) const; + /// @brief Returns the `Name` for a particular resource. + virtual Name get_resource_name() const; /// @brief Return the resource's name. virtual std::string name() const; private: std::string name_; + + protected: + Name get_resource_name(const std::string& type) const; }; template <> diff --git a/src/viam/sdk/resource/resource_api.cpp b/src/viam/sdk/resource/resource_api.cpp index 4924b1bbe..efa8769ed 100644 --- a/src/viam/sdk/resource/resource_api.cpp +++ b/src/viam/sdk/resource/resource_api.cpp @@ -1,16 +1,11 @@ #include -#include #include -#include -#include #include #include #include -#include - #include #include #include @@ -112,37 +107,6 @@ std::string Name::short_name() const { return name_; } -viam::common::v1::ResourceName Name::to_proto() const { - viam::common::v1::ResourceName rn; - *rn.mutable_namespace_() = this->api().type_namespace(); - if (this->remote_name().empty()) { - *rn.mutable_name() = this->name(); - } else { - *rn.mutable_name() = this->remote_name() + ":" + this->name(); - } - *rn.mutable_type() = this->api().resource_type(); - *rn.mutable_subtype() = this->api().resource_subtype(); - return rn; -} - -Name Name::from_proto(const viam::common::v1::ResourceName& proto) { - const API api(proto.namespace_(), proto.type(), proto.subtype()); - std::vector name_parts; - boost::split(name_parts, proto.name(), boost::is_any_of(":")); - auto name = name_parts.back(); - name_parts.pop_back(); - auto remote_name = name_parts.empty() - ? "" - : std::accumulate(std::next(name_parts.begin()), - name_parts.end(), - *name_parts.begin(), - [](const std::string& a, const std::string& b) { - return a + ":" + b; - }); - - return Name({proto.namespace_(), proto.type(), proto.subtype()}, remote_name, name); -}; - Name Name::from_string(std::string name) { if (!std::regex_match(name, NAME_REGEX)) { throw Exception("Received invalid Name string: " + name); diff --git a/src/viam/sdk/resource/resource_api.hpp b/src/viam/sdk/resource/resource_api.hpp index 5677a8fd4..142f40f25 100644 --- a/src/viam/sdk/resource/resource_api.hpp +++ b/src/viam/sdk/resource/resource_api.hpp @@ -4,8 +4,6 @@ #include -#include - namespace viam { namespace sdk { @@ -64,8 +62,6 @@ class Name { public: std::string short_name() const; std::string to_string() const; - viam::common::v1::ResourceName to_proto() const; - static Name from_proto(const viam::common::v1::ResourceName& proto); static Name from_string(std::string name); Name(API api, std::string remote_name, std::string name); Name(){}; diff --git a/src/viam/sdk/resource/resource_manager.cpp b/src/viam/sdk/resource/resource_manager.cpp index 8f2940c5f..9e7e50e34 100644 --- a/src/viam/sdk/resource/resource_manager.cpp +++ b/src/viam/sdk/resource/resource_manager.cpp @@ -12,8 +12,6 @@ #include #include -#include - #include #include #include diff --git a/src/viam/sdk/resource/resource_manager.hpp b/src/viam/sdk/resource/resource_manager.hpp index 3bc894f49..1d81239a1 100644 --- a/src/viam/sdk/resource/resource_manager.hpp +++ b/src/viam/sdk/resource/resource_manager.hpp @@ -11,8 +11,6 @@ #include #include -#include - #include #include diff --git a/src/viam/sdk/robot/client.cpp b/src/viam/sdk/robot/client.cpp index 15cac8e17..5cd574db4 100644 --- a/src/viam/sdk/robot/client.cpp +++ b/src/viam/sdk/robot/client.cpp @@ -19,6 +19,7 @@ #include #include +#include #include #include #include @@ -72,7 +73,7 @@ RobotClient::discovery from_proto(const Discovery& proto) { RobotClient::frame_system_config from_proto(const FrameSystemConfig& proto) { RobotClient::frame_system_config fsconfig; - fsconfig.frame = WorldState::transform::from_proto(proto.frame()); + fsconfig.frame = impl::from_proto(proto.frame()); if (proto.has_kinematics()) { fsconfig.kinematics = struct_to_map(proto.kinematics()); } @@ -82,13 +83,13 @@ RobotClient::frame_system_config from_proto(const FrameSystemConfig& proto) { RobotClient::status from_proto(const Status& proto) { RobotClient::status status; if (proto.has_name()) { - status.name = Name::from_proto(proto.name()); + status.name = ::viam::sdk::impl::from_proto(proto.name()); } if (proto.has_status()) { status.status_map = struct_to_map(proto.status()); } if (proto.has_last_reconfigured()) { - status.last_reconfigured = timestamp_to_time_pt(proto.last_reconfigured()); + status.last_reconfigured = impl::timestamp_to_time_pt(proto.last_reconfigured()); } return status; } @@ -104,7 +105,7 @@ RobotClient::operation from_proto(const Operation& proto) { op.arguments = struct_to_map(proto.arguments()); } if (proto.has_started()) { - op.started = timestamp_to_time_pt(proto.started()); + op.started = impl::timestamp_to_time_pt(proto.started()); } return op; } @@ -178,7 +179,7 @@ std::vector RobotClient::get_status(std::vector& comp viam::robot::v1::GetStatusResponse resp; ClientContext ctx; for (const Name& name : components) { - *req.mutable_resource_names()->Add() = name.to_proto(); + *req.mutable_resource_names()->Add() = ::viam::sdk::impl::to_proto(name); } const grpc::Status response = impl_->stub_->GetStatus(ctx, req, &resp); @@ -255,7 +256,7 @@ void RobotClient::refresh() { std::unordered_map> new_resources; std::vector current_resources; for (const auto& name : resp.resources()) { - current_resources.push_back(Name::from_proto(name)); + current_resources.push_back(::viam::sdk::impl::from_proto(name)); if (name.subtype() == "remote") { continue; } @@ -370,7 +371,7 @@ std::vector RobotClient::get_frame_system_conf RepeatedPtrField* req_transforms = req.mutable_supplemental_transforms(); for (const WorldState::transform& transform : additional_transforms) { - *req_transforms->Add() = transform.to_proto(); + *req_transforms->Add() = ::viam::sdk::impl::to_proto(transform); } const grpc::Status response = impl_->stub_->FrameSystemConfig(ctx, req, &resp); @@ -398,12 +399,12 @@ pose_in_frame RobotClient::transform_pose( viam::robot::v1::TransformPoseResponse resp; ClientContext ctx; - *req.mutable_source() = query.to_proto(); + *req.mutable_source() = ::viam::sdk::impl::to_proto(query); *req.mutable_destination() = std::move(destination); RepeatedPtrField* req_transforms = req.mutable_supplemental_transforms(); for (const WorldState::transform& transform : additional_transforms) { - *req_transforms->Add() = transform.to_proto(); + *req_transforms->Add() = ::viam::sdk::impl::to_proto(transform); } const grpc::Status response = impl_->stub_->TransformPose(ctx, req, &resp); @@ -411,7 +412,7 @@ pose_in_frame RobotClient::transform_pose( BOOST_LOG_TRIVIAL(error) << "Error getting PoseInFrame: " << response.error_message(); } - return pose_in_frame::from_proto(resp.pose()); + return ::viam::sdk::impl::from_proto(resp.pose()); } std::vector RobotClient::discover_components( @@ -463,7 +464,7 @@ void RobotClient::stop_all(const std::unordered_map& extra) { const ProtoStruct& params = xtra.second; const google::protobuf::Struct s = map_to_struct(params); viam::robot::v1::StopExtraParameters stop; - *stop.mutable_name() = name.to_proto(); + *stop.mutable_name() = ::viam::sdk::impl::to_proto(name); *stop.mutable_params() = s; *ep->Add() = stop; } diff --git a/src/viam/sdk/robot/service.cpp b/src/viam/sdk/robot/service.cpp index 26004a939..4b9351ee2 100644 --- a/src/viam/sdk/robot/service.cpp +++ b/src/viam/sdk/robot/service.cpp @@ -14,6 +14,7 @@ #include #include +#include #include #include #include @@ -31,6 +32,13 @@ using viam::common::v1::ResourceName; using viam::robot::v1::Status; namespace { +Status create_status(const std::shared_ptr& resource) { + Status status; + *status.mutable_name() = impl::to_proto(resource->get_resource_name()); + *status.mutable_status() = google::protobuf::Struct(); + return status; +} + std::vector registered_models_for_resource(const std::shared_ptr& resource) { std::string resource_type; std::string resource_subtype; @@ -67,7 +75,7 @@ std::vector RobotService_::generate_metadata_() { std::vector metadata; for (const auto& key_and_val : resource_manager()->resources()) { for (const Name& name : registered_models_for_resource(key_and_val.second)) { - metadata.push_back(name.to_proto()); + metadata.push_back(impl::to_proto(name)); } } return metadata; @@ -82,7 +90,7 @@ std::vector RobotService_::generate_status_( const std::shared_ptr registration = kv.second; if (registration->api().resource_subtype() == resource->api().resource_subtype()) { bool resource_present = false; - const ResourceName name = resource->get_resource_name(resource->name()); + const ResourceName name = impl::to_proto(resource->get_resource_name()); for (const auto& resource_name : resource_names) { if (name.SerializeAsString() == resource_name.SerializeAsString()) { resource_present = true; @@ -91,7 +99,7 @@ std::vector RobotService_::generate_status_( } if (resource_present) { - const Status status = registration->create_status(resource); + const Status status = create_status(resource); statuses.push_back(status); } } @@ -195,7 +203,7 @@ ::grpc::Status RobotService_::StopAll(::grpc::ServerContext*, for (const auto& r : resource_manager()->resources()) { const std::shared_ptr resource = r.second; - const ResourceName rn = resource->get_resource_name(resource->name()); + const ResourceName rn = impl::to_proto(resource->get_resource_name()); const std::string rn_ = rn.SerializeAsString(); if (extra.find(rn_) != extra.end()) { try { diff --git a/src/viam/sdk/robot/service.hpp b/src/viam/sdk/robot/service.hpp index 3b5cf4b1f..abb896c3d 100644 --- a/src/viam/sdk/robot/service.hpp +++ b/src/viam/sdk/robot/service.hpp @@ -3,11 +3,6 @@ /// @brief gRPC service implementation for a `robot`. #pragma once -#include -#include -#include - -#include #include #include diff --git a/src/viam/sdk/rpc/dial.cpp b/src/viam/sdk/rpc/dial.cpp index 2f4cbaa8d..40ca5d01f 100644 --- a/src/viam/sdk/rpc/dial.cpp +++ b/src/viam/sdk/rpc/dial.cpp @@ -1,6 +1,5 @@ #include -#include #include #include @@ -8,8 +7,6 @@ #include #include -#include -#include #include #include @@ -111,8 +108,6 @@ std::shared_ptr ViamChannel::dial(const char* uri, address += socket_path; const std::shared_ptr channel = impl::create_viam_channel(address, grpc::InsecureChannelCredentials()); - const std::unique_ptr st = - viam::robot::v1::RobotService::NewStub(channel); return std::make_shared(channel, socket_path, ptr); }; diff --git a/src/viam/sdk/services/generic.cpp b/src/viam/sdk/services/generic.cpp index 7379c2f2b..525f6eb62 100644 --- a/src/viam/sdk/services/generic.cpp +++ b/src/viam/sdk/services/generic.cpp @@ -1,11 +1,7 @@ #include -#include - #include -#include - #include #include diff --git a/src/viam/sdk/services/generic.hpp b/src/viam/sdk/services/generic.hpp index b004b2cde..2682b8bf3 100644 --- a/src/viam/sdk/services/generic.hpp +++ b/src/viam/sdk/services/generic.hpp @@ -4,8 +4,6 @@ /// executing arbitrary commands. #pragma once -#include - #include #include #include diff --git a/src/viam/sdk/services/mlmodel.hpp b/src/viam/sdk/services/mlmodel.hpp index 3f9058965..cc790889c 100644 --- a/src/viam/sdk/services/mlmodel.hpp +++ b/src/viam/sdk/services/mlmodel.hpp @@ -20,8 +20,6 @@ #include #include -#include - #include #include diff --git a/src/viam/sdk/services/motion.cpp b/src/viam/sdk/services/motion.cpp index 58e8cb92c..286d8a64d 100644 --- a/src/viam/sdk/services/motion.cpp +++ b/src/viam/sdk/services/motion.cpp @@ -2,10 +2,6 @@ #include -#include -#include -#include - #include #include @@ -14,73 +10,6 @@ namespace sdk { Motion::Motion(std::string name) : Service(std::move(name)){}; -service::motion::v1::Constraints Motion::constraints::to_proto() const { - service::motion::v1::Constraints proto; - for (const auto& lc : linear_constraints) { - service::motion::v1::LinearConstraint proto_lc; - proto_lc.set_line_tolerance_mm(lc.line_tolerance_mm); - proto_lc.set_orientation_tolerance_degs(lc.orientation_tolerance_degs); - *proto.mutable_linear_constraint()->Add() = std::move(proto_lc); - } - - for (const auto& oc : orientation_constraints) { - service::motion::v1::OrientationConstraint proto_oc; - proto_oc.set_orientation_tolerance_degs(oc.orientation_tolerance_degs); - *proto.mutable_orientation_constraint()->Add() = std::move(proto_oc); - } - - for (const auto& cs : collision_specifications) { - service::motion::v1::CollisionSpecification proto_cs; - for (const auto& allow : cs.allows) { - service::motion::v1::CollisionSpecification::AllowedFrameCollisions proto_allow; - *proto_allow.mutable_frame1() = allow.frame1; - *proto_allow.mutable_frame2() = allow.frame2; - *proto_cs.mutable_allows()->Add() = std::move(proto_allow); - } - *proto.mutable_collision_specification()->Add() = std::move(proto_cs); - } - - return proto; -} - -Motion::constraints Motion::constraints::from_proto(const service::motion::v1::Constraints& proto) { - std::vector lcs; - for (const auto& proto_lc : proto.linear_constraint()) { - Motion::linear_constraint lc; - lc.orientation_tolerance_degs = proto_lc.orientation_tolerance_degs(); - lc.line_tolerance_mm = proto_lc.line_tolerance_mm(); - lcs.push_back(lc); - } - - std::vector ocs; - for (const auto& proto_oc : proto.orientation_constraint()) { - Motion::orientation_constraint oc; - oc.orientation_tolerance_degs = proto_oc.orientation_tolerance_degs(); - ocs.push_back(oc); - } - - std::vector css; - for (const auto& proto_cs : proto.collision_specification()) { - std::vector allows; - for (const auto& proto_allow : proto_cs.allows()) { - Motion::collision_specification::allowed_frame_collisions allow; - allow.frame1 = proto_allow.frame1(); - allow.frame2 = proto_allow.frame2(); - allows.push_back(allow); - } - Motion::collision_specification cs; - cs.allows = allows; - css.push_back(cs); - } - - Motion::constraints constraints; - constraints.linear_constraints = lcs; - constraints.orientation_constraints = ocs; - constraints.collision_specifications = css; - - return constraints; -} - API Motion::api() const { return API::get(); } @@ -89,21 +18,6 @@ API API::traits::api() { return {kRDK, kService, "motion"}; } -service::motion::v1::ObstacleDetector obstacle_detector::to_proto() const { - service::motion::v1::ObstacleDetector proto; - *proto.mutable_vision_service() = vision_service.to_proto(); - *proto.mutable_camera() = camera.to_proto(); - return proto; -} - -obstacle_detector obstacle_detector::from_proto( - const service::motion::v1::ObstacleDetector& proto) { - obstacle_detector oc; - oc.vision_service = Name::from_proto(proto.vision_service()); - oc.camera = Name::from_proto(proto.camera()); - return oc; -} - bool operator==(const obstacle_detector& lhs, const obstacle_detector& rhs) { return lhs.vision_service == rhs.vision_service && lhs.camera == rhs.camera; } @@ -140,67 +54,6 @@ std::ostream& operator<<(std::ostream& os, const obstacle_detector& v) { return os; } -service::motion::v1::MotionConfiguration motion_configuration::to_proto() const { - service::motion::v1::MotionConfiguration proto; - - for (const obstacle_detector& od : obstacle_detectors) { - *proto.mutable_obstacle_detectors()->Add() = od.to_proto(); - } - - if (position_polling_frequency_hz && !isnan(*position_polling_frequency_hz)) { - proto.set_position_polling_frequency_hz(*position_polling_frequency_hz); - } - - if (obstacle_polling_frequency_hz && !isnan(*obstacle_polling_frequency_hz)) { - proto.set_obstacle_polling_frequency_hz(*obstacle_polling_frequency_hz); - } - - if (plan_deviation_m && !isnan(*plan_deviation_m)) { - proto.set_plan_deviation_m(*plan_deviation_m); - } - - if (linear_m_per_sec && !isnan(*linear_m_per_sec)) { - proto.set_linear_m_per_sec(*linear_m_per_sec); - } - - if (angular_degs_per_sec && !isnan(*angular_degs_per_sec)) { - proto.set_angular_degs_per_sec(*angular_degs_per_sec); - } - - return proto; -} - -motion_configuration motion_configuration::from_proto( - const service::motion::v1::MotionConfiguration& proto) { - motion_configuration mc; - - for (const service::motion::v1::ObstacleDetector& od : proto.obstacle_detectors()) { - mc.obstacle_detectors.push_back(obstacle_detector::from_proto(od)); - } - - if (proto.has_position_polling_frequency_hz()) { - *mc.position_polling_frequency_hz = proto.position_polling_frequency_hz(); - } - - if (proto.has_obstacle_polling_frequency_hz()) { - *mc.obstacle_polling_frequency_hz = proto.obstacle_polling_frequency_hz(); - } - - if (proto.has_plan_deviation_m()) { - *mc.plan_deviation_m = proto.plan_deviation_m(); - } - - if (proto.has_linear_m_per_sec()) { - *mc.linear_m_per_sec = proto.linear_m_per_sec(); - } - - if (proto.has_angular_degs_per_sec()) { - *mc.angular_degs_per_sec = proto.angular_degs_per_sec(); - } - - return mc; -} - bool operator==(const motion_configuration& lhs, const motion_configuration& rhs) { return lhs.angular_degs_per_sec == rhs.angular_degs_per_sec && lhs.obstacle_detectors == rhs.obstacle_detectors && @@ -239,175 +92,5 @@ std::ostream& operator<<(std::ostream& os, const motion_configuration& v) { return os; } -Motion::plan_state Motion::from_proto(const service::motion::v1::PlanState& proto) { - switch (proto) { - case service::motion::v1::PLAN_STATE_FAILED: { - return Motion::plan_state::k_failed; - } - case service::motion::v1::PLAN_STATE_SUCCEEDED: { - return Motion::plan_state::k_succeeded; - } - case service::motion::v1::PLAN_STATE_IN_PROGRESS: { - return Motion::plan_state::k_in_progress; - } - case service::motion::v1::PLAN_STATE_STOPPED: { - return Motion::plan_state::k_stopped; - } - default: { - throw Exception(ErrorCondition::k_not_supported, "Invalid proto PlanState to encode"); - } - } -} - -service::motion::v1::PlanState Motion::to_proto(const Motion::plan_state& state) { - switch (state) { - case Motion::plan_state::k_failed: { - return service::motion::v1::PLAN_STATE_FAILED; - } - case Motion::plan_state::k_succeeded: { - return service::motion::v1::PLAN_STATE_SUCCEEDED; - } - case Motion::plan_state::k_in_progress: { - return service::motion::v1::PLAN_STATE_IN_PROGRESS; - } - case Motion::plan_state::k_stopped: { - return service::motion::v1::PLAN_STATE_STOPPED; - } - default: { - throw Exception(ErrorCondition::k_not_supported, - "Invalid plan_state to encode to proto"); - } - } -} - -Motion::plan_status Motion::plan_status::from_proto(const service::motion::v1::PlanStatus& proto) { - plan_status mps; - mps.state = Motion::from_proto(proto.state()); - if (proto.has_reason()) { - mps.reason = proto.reason(); - } - mps.timestamp = timestamp_to_time_pt(proto.timestamp()); - - return mps; -} - -std::vector Motion::plan_status::from_proto( - const google::protobuf::RepeatedPtrField& proto) { - std::vector pss; - for (const auto& ps : proto) { - pss.push_back(Motion::plan_status::from_proto(ps)); - } - - return pss; -} -service::motion::v1::PlanStatus Motion::plan_status::to_proto() const { - service::motion::v1::PlanStatus proto; - *proto.mutable_timestamp() = time_pt_to_timestamp(timestamp); - if (reason) { - *proto.mutable_reason() = *reason; - } - proto.set_state(Motion::to_proto(state)); - - return proto; -} - -Motion::steps Motion::steps::from_proto( - const google::protobuf::RepeatedPtrField& proto) { - std::vector steps; - for (const auto& ps : proto) { - step step; - for (const auto& component : ps.step()) { - step.emplace(component.first, pose::from_proto(component.second.pose())); - } - steps.push_back(std::move(step)); - } - - return {steps}; -} - -service::motion::v1::PlanStep Motion::steps::to_proto(const Motion::steps::step& step) { - service::motion::v1::PlanStep proto; - for (const auto& kv : step) { - service::motion::v1::ComponentState cs; - *cs.mutable_pose() = kv.second.to_proto(); - proto.mutable_step()->insert({kv.first, cs}); - } - - return proto; -} - -Motion::plan Motion::plan::from_proto(const service::motion::v1::Plan& proto) { - Motion::plan plan; - plan.id = proto.id(); - plan.execution_id = proto.execution_id(); - plan.component_name = Name::from_proto(proto.component_name()); - plan.steps = Motion::steps::from_proto(proto.steps()); - return plan; -} - -service::motion::v1::Plan Motion::plan::to_proto() const { - service::motion::v1::Plan proto; - *proto.mutable_id() = id; - *proto.mutable_component_name() = component_name.to_proto(); - *proto.mutable_execution_id() = execution_id; - for (const auto& step : steps.steps) { - *proto.mutable_steps()->Add() = Motion::steps::to_proto(step); - } - - return proto; -} - -Motion::plan_with_status Motion::plan_with_status::from_proto( - const service::motion::v1::PlanWithStatus& proto) { - Motion::plan_with_status pws; - pws.plan = Motion::plan::from_proto(proto.plan()); - pws.status = Motion::plan_status::from_proto(proto.status()); - pws.status_history = Motion::plan_status::from_proto(proto.status_history()); - - return pws; -} - -std::vector Motion::plan_with_status::from_proto( - const google::protobuf::RepeatedPtrField& proto) { - std::vector plans; - for (const auto& plan : proto) { - plans.push_back(Motion::plan_with_status::from_proto(plan)); - } - return plans; -} - -service::motion::v1::PlanWithStatus Motion::plan_with_status::to_proto() const { - service::motion::v1::PlanWithStatus proto; - *proto.mutable_plan() = plan.to_proto(); - *proto.mutable_status() = status.to_proto(); - for (const auto& sh : status_history) { - *proto.mutable_status_history()->Add() = sh.to_proto(); - } - - return proto; -} - -Motion::plan_status_with_id Motion::plan_status_with_id::from_proto( - const service::motion::v1::PlanStatusWithID& proto) { - Motion::plan_status_with_id pswi; - pswi.execution_id = proto.execution_id(); - pswi.component_name = Name::from_proto(proto.component_name()); - pswi.plan_id = proto.plan_id(); - pswi.status = Motion::plan_status::from_proto(proto.status()); - - return pswi; -} - -service::motion::v1::PlanStatusWithID Motion::plan_status_with_id::to_proto() const { - service::motion::v1::PlanStatusWithID proto; - - *proto.mutable_execution_id() = execution_id; - *proto.mutable_component_name() = component_name.to_proto(); - *proto.mutable_plan_id() = plan_id; - *proto.mutable_status() = status.to_proto(); - - return proto; -} - } // namespace sdk } // namespace viam diff --git a/src/viam/sdk/services/motion.hpp b/src/viam/sdk/services/motion.hpp index 9db70a98b..f7005ae64 100644 --- a/src/viam/sdk/services/motion.hpp +++ b/src/viam/sdk/services/motion.hpp @@ -5,8 +5,6 @@ #include -#include - #include #include #include @@ -28,8 +26,6 @@ struct obstacle_detector { /// @brief The name of the camera component to be used for obstacle detection. Name camera; - service::motion::v1::ObstacleDetector to_proto() const; - static obstacle_detector from_proto(const service::motion::v1::ObstacleDetector& proto); friend bool operator==(const obstacle_detector& lhs, const obstacle_detector& rhs); friend std::ostream& operator<<(std::ostream& os, const obstacle_detector& v); }; @@ -56,8 +52,6 @@ struct motion_configuration { /// @brief Optional angular velocity to target when turning boost::optional angular_degs_per_sec; - service::motion::v1::MotionConfiguration to_proto() const; - static motion_configuration from_proto(const service::motion::v1::MotionConfiguration& proto); friend bool operator==(const motion_configuration& lhs, const motion_configuration& rhs); friend std::ostream& operator<<(std::ostream& os, const motion_configuration& v); }; @@ -83,9 +77,6 @@ class Motion : public Service { k_failed, }; - static plan_state from_proto(const service::motion::v1::PlanState& proto); - static service::motion::v1::PlanState to_proto(const plan_state& state); - /// @struct plan_status /// @brief Describes the state of a given plan at a point in time. /// @ingroup Motion @@ -100,10 +91,6 @@ class Motion : public Service { /// re-plan reason if re-planning was necessary. boost::optional reason; - static plan_status from_proto(const service::motion::v1::PlanStatus& proto); - static std::vector from_proto( - const google::protobuf::RepeatedPtrField& proto); - service::motion::v1::PlanStatus to_proto() const; friend bool operator==(const plan_status& lhs, const plan_status& rhs); }; @@ -123,8 +110,6 @@ class Motion : public Service { /// @brief The plan status. plan_status status; - static plan_status_with_id from_proto(const service::motion::v1::PlanStatusWithID& proto); - service::motion::v1::PlanStatusWithID to_proto() const; friend bool operator==(const plan_status_with_id& lhs, const plan_status_with_id& rhs); }; @@ -139,10 +124,6 @@ class Motion : public Service { /// @brief The ordered list of steps. std::vector steps; - static struct steps from_proto( - const google::protobuf::RepeatedPtrField& proto); - - static service::motion::v1::PlanStep to_proto(const step& step); friend bool operator==(const struct steps& lhs, const struct steps& rhs); }; @@ -163,8 +144,6 @@ class Motion : public Service { /// @brief An ordered list of plan steps. struct steps steps; - static plan from_proto(const service::motion::v1::Plan& proto); - service::motion::v1::Plan to_proto() const; friend bool operator==(const plan& lhs, const plan& rhs); }; @@ -182,10 +161,6 @@ class Motion : public Service { /// @brief The prior status changes that have happened during plan execution. std::vector status_history; - static plan_with_status from_proto(const service::motion::v1::PlanWithStatus& proto); - static std::vector from_proto( - const google::protobuf::RepeatedPtrField& proto); - service::motion::v1::PlanWithStatus to_proto() const; friend bool operator==(const plan_with_status& lhs, const plan_with_status& rhs); }; @@ -220,9 +195,6 @@ class Motion : public Service { std::vector linear_constraints; std::vector orientation_constraints; std::vector collision_specifications; - - static constraints from_proto(const service::motion::v1::Constraints& proto); - service::motion::v1::Constraints to_proto() const; }; API api() const override; diff --git a/src/viam/sdk/services/private/mlmodel.hpp b/src/viam/sdk/services/private/mlmodel.hpp index 66ee6f3a9..3f829fda9 100644 --- a/src/viam/sdk/services/private/mlmodel.hpp +++ b/src/viam/sdk/services/private/mlmodel.hpp @@ -16,6 +16,8 @@ #include +#include + #include namespace viam { diff --git a/src/viam/sdk/services/private/motion_client.cpp b/src/viam/sdk/services/private/motion_client.cpp index db082de7f..3cd95337d 100644 --- a/src/viam/sdk/services/private/motion_client.cpp +++ b/src/viam/sdk/services/private/motion_client.cpp @@ -8,6 +8,7 @@ #include #include +#include #include #include #include @@ -16,6 +17,165 @@ namespace viam { namespace sdk { namespace impl { +service::motion::v1::ObstacleDetector to_proto(const obstacle_detector& od) { + service::motion::v1::ObstacleDetector proto; + *proto.mutable_vision_service() = to_proto(od.vision_service); + *proto.mutable_camera() = to_proto(od.camera); + return proto; +} + +service::motion::v1::MotionConfiguration to_proto(const motion_configuration& mc) { + service::motion::v1::MotionConfiguration proto; + + for (const obstacle_detector& od : mc.obstacle_detectors) { + *proto.mutable_obstacle_detectors()->Add() = to_proto(od); + } + + if (mc.position_polling_frequency_hz && !isnan(*mc.position_polling_frequency_hz)) { + proto.set_position_polling_frequency_hz(*mc.position_polling_frequency_hz); + } + + if (mc.obstacle_polling_frequency_hz && !isnan(*mc.obstacle_polling_frequency_hz)) { + proto.set_obstacle_polling_frequency_hz(*mc.obstacle_polling_frequency_hz); + } + + if (mc.plan_deviation_m && !isnan(*mc.plan_deviation_m)) { + proto.set_plan_deviation_m(*mc.plan_deviation_m); + } + + if (mc.linear_m_per_sec && !isnan(*mc.linear_m_per_sec)) { + proto.set_linear_m_per_sec(*mc.linear_m_per_sec); + } + + if (mc.angular_degs_per_sec && !isnan(*mc.angular_degs_per_sec)) { + proto.set_angular_degs_per_sec(*mc.angular_degs_per_sec); + } + + return proto; +} + +service::motion::v1::Constraints to_proto(const Motion::constraints& cs) { + service::motion::v1::Constraints proto; + for (const auto& lc : cs.linear_constraints) { + service::motion::v1::LinearConstraint proto_lc; + proto_lc.set_line_tolerance_mm(lc.line_tolerance_mm); + proto_lc.set_orientation_tolerance_degs(lc.orientation_tolerance_degs); + *proto.mutable_linear_constraint()->Add() = std::move(proto_lc); + } + + for (const auto& oc : cs.orientation_constraints) { + service::motion::v1::OrientationConstraint proto_oc; + proto_oc.set_orientation_tolerance_degs(oc.orientation_tolerance_degs); + *proto.mutable_orientation_constraint()->Add() = std::move(proto_oc); + } + + for (const auto& spec : cs.collision_specifications) { + service::motion::v1::CollisionSpecification proto_cs; + for (const auto& allow : spec.allows) { + service::motion::v1::CollisionSpecification::AllowedFrameCollisions proto_allow; + *proto_allow.mutable_frame1() = allow.frame1; + *proto_allow.mutable_frame2() = allow.frame2; + *proto_cs.mutable_allows()->Add() = std::move(proto_allow); + } + *proto.mutable_collision_specification()->Add() = std::move(proto_cs); + } + + return proto; +} + +Motion::plan_state from_proto(const service::motion::v1::PlanState& proto) { + switch (proto) { + case service::motion::v1::PLAN_STATE_FAILED: { + return Motion::plan_state::k_failed; + } + case service::motion::v1::PLAN_STATE_SUCCEEDED: { + return Motion::plan_state::k_succeeded; + } + case service::motion::v1::PLAN_STATE_IN_PROGRESS: { + return Motion::plan_state::k_in_progress; + } + case service::motion::v1::PLAN_STATE_STOPPED: { + return Motion::plan_state::k_stopped; + } + default: { + throw Exception(ErrorCondition::k_not_supported, "Invalid proto PlanState to encode"); + } + } +} + +Motion::plan_status from_proto(const service::motion::v1::PlanStatus& proto) { + Motion::plan_status mps; + mps.state = from_proto(proto.state()); + if (proto.has_reason()) { + mps.reason = proto.reason(); + } + mps.timestamp = timestamp_to_time_pt(proto.timestamp()); + + return mps; +} + +std::vector from_proto( + const google::protobuf::RepeatedPtrField& proto) { + std::vector pss; + for (const auto& ps : proto) { + pss.push_back(from_proto(ps)); + } + + return pss; +} + +Motion::plan_status_with_id from_proto(const service::motion::v1::PlanStatusWithID& proto) { + Motion::plan_status_with_id pswi; + pswi.execution_id = proto.execution_id(); + pswi.component_name = from_proto(proto.component_name()); + pswi.plan_id = proto.plan_id(); + pswi.status = from_proto(proto.status()); + + return pswi; +} + +Motion::steps steps_from_proto( + const google::protobuf::RepeatedPtrField& proto) { + using step = Motion::steps::step; + std::vector steps; + for (const auto& ps : proto) { + step step; + for (const auto& component : ps.step()) { + step.emplace(component.first, from_proto(component.second.pose())); + } + steps.push_back(std::move(step)); + } + + return {steps}; +} + +Motion::plan plan_from_proto(const service::motion::v1::Plan& proto) { + Motion::plan plan; + plan.id = proto.id(); + plan.execution_id = proto.execution_id(); + plan.component_name = from_proto(proto.component_name()); + plan.steps = steps_from_proto(proto.steps()); + return plan; +} + +Motion::plan_with_status from_proto(const service::motion::v1::PlanWithStatus& proto) { + Motion::plan_with_status pws; + pws.plan = plan_from_proto(proto.plan()); + pws.status = from_proto(proto.status()); + pws.status_history = from_proto(proto.status_history()); + + return pws; +} + +std::vector from_proto( + const google::protobuf::RepeatedPtrField& proto) { + std::vector plans; + for (const auto& plan : proto) { + plans.push_back(from_proto(plan)); + } + return plans; +} + MotionClient::MotionClient(std::string name, std::shared_ptr channel) : Motion(std::move(name)), stub_(service::motion::v1::MotionService::NewStub(channel)), @@ -29,13 +189,13 @@ bool MotionClient::move(const pose_in_frame& destination, return make_client_helper(this, *stub_, &StubType::Move) .with(extra, [&](auto& request) { - *request.mutable_component_name() = component_name.to_proto(); - *request.mutable_destination() = destination.to_proto(); + *request.mutable_component_name() = to_proto(component_name); + *request.mutable_destination() = to_proto(destination); if (constraints) { - *request.mutable_constraints() = constraints->to_proto(); + *request.mutable_constraints() = to_proto(*constraints); } if (world_state) { - *request.mutable_world_state() = world_state->to_proto(); + *request.mutable_world_state() = to_proto(*world_state); } }) .invoke([](auto& response) { return response.success(); }); @@ -51,16 +211,16 @@ std::string MotionClient::move_on_map( return make_client_helper(this, *stub_, &StubType::MoveOnMap) .with(extra, [&](auto& request) { - *request.mutable_destination() = destination.to_proto(); - *request.mutable_component_name() = component_name.to_proto(); - *request.mutable_slam_service_name() = slam_name.to_proto(); + *request.mutable_destination() = to_proto(destination); + *request.mutable_component_name() = to_proto(component_name); + *request.mutable_slam_service_name() = to_proto(slam_name); for (const auto& obstacle : obstacles) { - *request.mutable_obstacles()->Add() = obstacle.to_proto(); + *request.mutable_obstacles()->Add() = to_proto(obstacle); } if (motion_configuration) { - *request.mutable_motion_configuration() = motion_configuration->to_proto(); + *request.mutable_motion_configuration() = to_proto(*motion_configuration); } }) .invoke([](auto& response) { return response.execution_id(); }); @@ -78,24 +238,24 @@ std::string MotionClient::move_on_globe( return make_client_helper(this, *stub_, &StubType::MoveOnGlobe) .with(extra, [&](auto& request) { - *request.mutable_destination() = destination.to_proto(); - *request.mutable_component_name() = component_name.to_proto(); - *request.mutable_movement_sensor_name() = movement_sensor_name.to_proto(); + *request.mutable_destination() = to_proto(destination); + *request.mutable_component_name() = to_proto(component_name); + *request.mutable_movement_sensor_name() = to_proto(movement_sensor_name); if (heading && !isnan(*heading)) { request.set_heading(*heading); } for (const auto& obstacle : obstacles) { - *request.mutable_obstacles()->Add() = obstacle.to_proto(); + *request.mutable_obstacles()->Add() = to_proto(obstacle); } if (motion_configuration) { - *request.mutable_motion_configuration() = motion_configuration->to_proto(); + *request.mutable_motion_configuration() = to_proto(*motion_configuration); } for (const auto& bounding_region : bounding_regions) { - *request.mutable_bounding_regions()->Add() = bounding_region.to_proto(); + *request.mutable_bounding_regions()->Add() = to_proto(bounding_region); } }) .invoke([](auto& response) { return response.execution_id(); }); @@ -109,18 +269,18 @@ pose_in_frame MotionClient::get_pose( return make_client_helper(this, *stub_, &StubType::GetPose) .with(extra, [&](auto& request) { - *request.mutable_component_name() = component_name.to_proto(); + *request.mutable_component_name() = to_proto(component_name); *request.mutable_destination_frame() = destination_frame; for (const auto& transform : supplemental_transforms) { - *request.mutable_supplemental_transforms()->Add() = transform.to_proto(); + *request.mutable_supplemental_transforms()->Add() = to_proto(transform); } }) - .invoke([](auto& response) { return pose_in_frame::from_proto(response.pose()); }); + .invoke([](auto& response) { return from_proto(response.pose()); }); } void MotionClient::stop_plan(const Name& name, const ProtoStruct& extra) { return make_client_helper(this, *stub_, &StubType::StopPlan) - .with(extra, [&](auto& request) { *request.mutable_component_name() = name.to_proto(); }) + .with(extra, [&](auto& request) { *request.mutable_component_name() = to_proto(name); }) .invoke(); } @@ -132,7 +292,7 @@ std::pair> Motio return make_client_helper(this, *stub_, &StubType::GetPlan) .with(extra, [&](auto& request) { - *request.mutable_component_name() = component_name.to_proto(); + *request.mutable_component_name() = to_proto(component_name); request.set_last_plan_only(last_plan_only); if (execution_id) { *request.mutable_execution_id() = *execution_id; @@ -140,8 +300,8 @@ std::pair> Motio }) .invoke([](auto& response) { return std::pair>( - Motion::plan_with_status::from_proto(response.current_plan_with_status()), - Motion::plan_with_status::from_proto(response.replan_history())); + from_proto(response.current_plan_with_status()), + from_proto(response.replan_history())); }); } @@ -174,7 +334,7 @@ std::vector MotionClient::list_plan_statuses_( .invoke([](auto& response) { std::vector statuses; for (const auto& proto : response.plan_statuses_with_ids()) { - statuses.push_back(Motion::plan_status_with_id::from_proto(proto)); + statuses.push_back(from_proto(proto)); } return statuses; diff --git a/src/viam/sdk/services/private/motion_server.cpp b/src/viam/sdk/services/private/motion_server.cpp index 87e7bd41b..14d3e3656 100644 --- a/src/viam/sdk/services/private/motion_server.cpp +++ b/src/viam/sdk/services/private/motion_server.cpp @@ -2,7 +2,9 @@ #include +#include #include +#include #include #include #include @@ -14,9 +16,161 @@ namespace viam { namespace sdk { namespace impl { +service::motion::v1::PlanState to_proto(const Motion::plan_state& state) { + switch (state) { + case Motion::plan_state::k_failed: { + return service::motion::v1::PLAN_STATE_FAILED; + } + case Motion::plan_state::k_succeeded: { + return service::motion::v1::PLAN_STATE_SUCCEEDED; + } + case Motion::plan_state::k_in_progress: { + return service::motion::v1::PLAN_STATE_IN_PROGRESS; + } + case Motion::plan_state::k_stopped: { + return service::motion::v1::PLAN_STATE_STOPPED; + } + default: { + throw Exception(ErrorCondition::k_not_supported, + "Invalid plan_state to encode to proto"); + } + } +} + +service::motion::v1::PlanStatus to_proto(const Motion::plan_status& ps) { + service::motion::v1::PlanStatus proto; + *proto.mutable_timestamp() = time_pt_to_timestamp(ps.timestamp); + if (ps.reason) { + *proto.mutable_reason() = *ps.reason; + } + proto.set_state(to_proto(ps.state)); + + return proto; +} + +service::motion::v1::PlanStep to_proto(const Motion::steps::step& step) { + service::motion::v1::PlanStep proto; + for (const auto& kv : step) { + service::motion::v1::ComponentState cs; + *cs.mutable_pose() = to_proto(kv.second); + proto.mutable_step()->insert({kv.first, cs}); + } + + return proto; +} + +service::motion::v1::Plan to_proto(const Motion::plan& plan) { + service::motion::v1::Plan proto; + *proto.mutable_id() = plan.id; + *proto.mutable_component_name() = to_proto(plan.component_name); + *proto.mutable_execution_id() = plan.execution_id; + for (const auto& step : plan.steps.steps) { + *proto.mutable_steps()->Add() = to_proto(step); + } + + return proto; +} + +service::motion::v1::PlanWithStatus to_proto(const Motion::plan_with_status& pws) { + service::motion::v1::PlanWithStatus proto; + *proto.mutable_plan() = to_proto(pws.plan); + *proto.mutable_status() = to_proto(pws.status); + for (const auto& sh : pws.status_history) { + *proto.mutable_status_history()->Add() = to_proto(sh); + } + + return proto; +} + +service::motion::v1::PlanStatusWithID to_proto(const Motion::plan_status_with_id& pswi) { + service::motion::v1::PlanStatusWithID proto; + + *proto.mutable_execution_id() = pswi.execution_id; + *proto.mutable_component_name() = to_proto(pswi.component_name); + *proto.mutable_plan_id() = pswi.plan_id; + *proto.mutable_status() = to_proto(pswi.status); + + return proto; +} + +obstacle_detector from_proto(const service::motion::v1::ObstacleDetector& proto) { + obstacle_detector oc; + oc.vision_service = from_proto(proto.vision_service()); + oc.camera = from_proto(proto.camera()); + return oc; +} + +motion_configuration from_proto(const service::motion::v1::MotionConfiguration& proto) { + motion_configuration mc; + + for (const service::motion::v1::ObstacleDetector& od : proto.obstacle_detectors()) { + mc.obstacle_detectors.push_back(from_proto(od)); + } + + if (proto.has_position_polling_frequency_hz()) { + *mc.position_polling_frequency_hz = proto.position_polling_frequency_hz(); + } + + if (proto.has_obstacle_polling_frequency_hz()) { + *mc.obstacle_polling_frequency_hz = proto.obstacle_polling_frequency_hz(); + } + + if (proto.has_plan_deviation_m()) { + *mc.plan_deviation_m = proto.plan_deviation_m(); + } + + if (proto.has_linear_m_per_sec()) { + *mc.linear_m_per_sec = proto.linear_m_per_sec(); + } + + if (proto.has_angular_degs_per_sec()) { + *mc.angular_degs_per_sec = proto.angular_degs_per_sec(); + } + + return mc; +} + MotionServer::MotionServer(std::shared_ptr manager) : ResourceServer(std::move(manager)){}; +Motion::constraints from_proto(const service::motion::v1::Constraints& proto) { + std::vector lcs; + for (const auto& proto_lc : proto.linear_constraint()) { + Motion::linear_constraint lc; + lc.orientation_tolerance_degs = proto_lc.orientation_tolerance_degs(); + lc.line_tolerance_mm = proto_lc.line_tolerance_mm(); + lcs.push_back(lc); + } + + std::vector ocs; + for (const auto& proto_oc : proto.orientation_constraint()) { + Motion::orientation_constraint oc; + oc.orientation_tolerance_degs = proto_oc.orientation_tolerance_degs(); + ocs.push_back(oc); + } + + std::vector css; + for (const auto& proto_cs : proto.collision_specification()) { + std::vector allows; + for (const auto& proto_allow : proto_cs.allows()) { + Motion::collision_specification::allowed_frame_collisions allow; + allow.frame1 = proto_allow.frame1(); + allow.frame2 = proto_allow.frame2(); + allows.push_back(allow); + } + Motion::collision_specification cs; + cs.allows = allows; + css.push_back(cs); + } + + Motion::constraints constraints; + constraints.linear_constraints = lcs; + constraints.orientation_constraints = ocs; + constraints.collision_specifications = css; + + return constraints; +} + ::grpc::Status MotionServer::Move(::grpc::ServerContext*, const ::viam::service::motion::v1::MoveRequest* request, ::viam::service::motion::v1::MoveResponse* response) noexcept { @@ -24,17 +178,16 @@ ::grpc::Status MotionServer::Move(::grpc::ServerContext*, "MotionServer::Move", this, request)([&](auto& helper, auto& motion) { std::shared_ptr ws; if (request->has_world_state()) { - ws = std::make_shared(WorldState::from_proto(request->world_state())); + ws = std::make_shared(from_proto(request->world_state())); } std::shared_ptr constraints; if (request->has_constraints()) { - constraints = std::make_shared( - Motion::constraints::from_proto(request->constraints())); + constraints = std::make_shared(from_proto(request->constraints())); } - const bool success = motion->move(pose_in_frame::from_proto(request->destination()), - Name::from_proto(request->component_name()), + const bool success = motion->move(from_proto(request->destination()), + from_proto(request->component_name()), std::move(ws), std::move(constraints), helper.getExtra()); @@ -48,19 +201,19 @@ ::grpc::Status MotionServer::MoveOnMap( ::viam::service::motion::v1::MoveOnMapResponse* response) noexcept { return make_service_helper( "MotionServer::MoveOnMap", this, request)([&](auto& helper, auto& motion) { - const auto destination = pose::from_proto(request->destination()); - const auto component_name = Name::from_proto(request->component_name()); - const auto slam_name = Name::from_proto(request->slam_service_name()); + const auto destination = from_proto(request->destination()); + const auto component_name = from_proto(request->component_name()); + const auto slam_name = from_proto(request->slam_service_name()); std::shared_ptr mc; if (request->has_motion_configuration()) { - mc = std::make_shared( - motion_configuration::from_proto(request->motion_configuration())); + mc = + std::make_shared(from_proto(request->motion_configuration())); } std::vector obstacles; for (const auto& obstacle : request->obstacles()) { - obstacles.push_back(GeometryConfig::from_proto(obstacle)); + obstacles.push_back(from_proto(obstacle)); } const std::string execution_id = motion->move_on_map( @@ -76,14 +229,14 @@ ::grpc::Status MotionServer::MoveOnGlobe( ::viam::service::motion::v1::MoveOnGlobeResponse* response) noexcept { return make_service_helper( "MotionServer::MoveOnGlobe", this, request)([&](auto& helper, auto& motion) { - const auto destination = geo_point::from_proto(request->destination()); - const auto component_name = Name::from_proto(request->component_name()); - const auto movement_sensor_name = Name::from_proto(request->movement_sensor_name()); + const auto destination = from_proto(request->destination()); + const auto component_name = from_proto(request->component_name()); + const auto movement_sensor_name = from_proto(request->movement_sensor_name()); std::vector obstacles; std::vector bounding_regions; for (const auto& obstacle : request->obstacles()) { - obstacles.push_back(geo_geometry::from_proto(obstacle)); + obstacles.push_back(from_proto(obstacle)); } boost::optional heading; @@ -93,12 +246,12 @@ ::grpc::Status MotionServer::MoveOnGlobe( std::shared_ptr mc; if (request->has_motion_configuration()) { - mc = std::make_shared( - motion_configuration::from_proto(request->motion_configuration())); + mc = + std::make_shared(from_proto(request->motion_configuration())); } for (const auto& bounding_region : request->bounding_regions()) { - bounding_regions.push_back(geo_geometry::from_proto(bounding_region)); + bounding_regions.push_back(from_proto(bounding_region)); } const std::string execution_id = motion->move_on_globe(destination, @@ -120,15 +273,15 @@ ::grpc::Status MotionServer::GetPose( ::viam::service::motion::v1::GetPoseResponse* response) noexcept { return make_service_helper( "MotionServer::GetPose", this, request)([&](auto& helper, auto& motion) { - const auto& component_name = Name::from_proto(request->component_name()); + const auto& component_name = from_proto(request->component_name()); const std::string& destination_frame = request->destination_frame(); std::vector supplemental_transforms; for (const auto& proto_transform : request->supplemental_transforms()) { - supplemental_transforms.push_back(WorldState::transform::from_proto(proto_transform)); + supplemental_transforms.push_back(from_proto(proto_transform)); } const pose_in_frame pose = motion->get_pose( component_name, destination_frame, supplemental_transforms, helper.getExtra()); - *response->mutable_pose() = pose.to_proto(); + *response->mutable_pose() = to_proto(pose); }); }; @@ -138,7 +291,7 @@ ::grpc::Status MotionServer::GetPlan( ::viam::service::motion::v1::GetPlanResponse* response) noexcept { return make_service_helper( "MotionServer::GetPlan", this, request)([&](auto& helper, auto& motion) { - const auto& component_name = Name::from_proto(request->component_name()); + const auto& component_name = from_proto(request->component_name()); Motion::plan_with_status plan; std::vector replan_history; const bool last_plan_only(request->last_plan_only()); @@ -160,9 +313,9 @@ ::grpc::Status MotionServer::GetPlan( replan_history = res.second; } - *response->mutable_current_plan_with_status() = plan.to_proto(); + *response->mutable_current_plan_with_status() = to_proto(plan); for (const auto& p : replan_history) { - *response->mutable_replan_history()->Add() = p.to_proto(); + *response->mutable_replan_history()->Add() = to_proto(p); } }); } @@ -181,7 +334,7 @@ ::grpc::Status MotionServer::ListPlanStatuses( } for (const auto& status : statuses) { - *response->mutable_plan_statuses_with_ids()->Add() = status.to_proto(); + *response->mutable_plan_statuses_with_ids()->Add() = to_proto(status); } }); } @@ -191,7 +344,7 @@ ::grpc::Status MotionServer::StopPlan(::grpc::ServerContext*, ::viam::service::motion::v1::StopPlanResponse*) noexcept { return make_service_helper( "MotionServer::StopPlan", this, request)([&](auto& helper, auto& motion) { - const auto& component_name = Name::from_proto(request->component_name()); + const auto& component_name = from_proto(request->component_name()); motion->stop_plan(component_name, helper.getExtra()); }); diff --git a/src/viam/sdk/services/service.cpp b/src/viam/sdk/services/service.cpp index 739ab0d8d..c20b0d474 100644 --- a/src/viam/sdk/services/service.cpp +++ b/src/viam/sdk/services/service.cpp @@ -2,18 +2,14 @@ #include -#include - #include #include namespace viam { namespace sdk { -common::v1::ResourceName Service::get_resource_name(std::string name) const { - auto r = this->Resource::get_resource_name(name); - *r.mutable_type() = kService; - return r; +Name Service::get_resource_name() const { + return Resource::get_resource_name(kService); } Service::Service() : Resource("service"){}; diff --git a/src/viam/sdk/services/service.hpp b/src/viam/sdk/services/service.hpp index 817304867..e74fb2195 100644 --- a/src/viam/sdk/services/service.hpp +++ b/src/viam/sdk/services/service.hpp @@ -2,8 +2,6 @@ #include -#include - #include namespace viam { @@ -11,7 +9,7 @@ namespace sdk { class Service : public Resource { public: - viam::common::v1::ResourceName get_resource_name(std::string name) const override; + Name get_resource_name() const override; Service(); protected: diff --git a/src/viam/sdk/spatialmath/geometry.cpp b/src/viam/sdk/spatialmath/geometry.cpp index 4fc999910..251e85b04 100644 --- a/src/viam/sdk/spatialmath/geometry.cpp +++ b/src/viam/sdk/spatialmath/geometry.cpp @@ -1,174 +1,17 @@ #include #include -#include #include -#include - #include #include namespace viam { namespace sdk { -viam::common::v1::Sphere GeometryConfig::sphere_proto() const { - try { - viam::common::v1::Sphere sphere; - const auto sphere_specifics = boost::get(geometry_specifics_); - sphere.set_radius_mm(sphere_specifics.radius); - return sphere; - } catch (...) { - throw Exception( - "Couldn't convert geometry config to sphere proto; sphere specifics not found"); - } -} - -viam::common::v1::RectangularPrism GeometryConfig::box_proto() const { - try { - const auto box_specifics = boost::get(geometry_specifics_); - viam::common::v1::RectangularPrism box; - viam::common::v1::Vector3 vec3; - vec3.set_x(box_specifics.x); - vec3.set_y(box_specifics.y); - vec3.set_z(box_specifics.z); - *box.mutable_dims_mm() = vec3; - return box; - } catch (...) { - throw Exception("Couldn't convert geometry config to box proto; box specifics not found"); - } -} - -viam::common::v1::Capsule GeometryConfig::capsule_proto() const { - try { - const auto capsule_specifics = boost::get(geometry_specifics_); - viam::common::v1::Capsule capsule; - capsule.set_radius_mm(capsule_specifics.radius); - capsule.set_length_mm(capsule_specifics.length); - return capsule; - } catch (...) { - throw Exception( - "Couldn't convert geometry config to capsule proto; capsule specifics not found"); - } -} - -viam::common::v1::Pose GeometryConfig::pose_proto() const { - return pose_.to_proto(); -} - -pose pose::from_proto(const viam::common::v1::Pose& proto) { - struct pose pose; - pose.coordinates.x = proto.x(); - pose.coordinates.y = proto.y(); - pose.coordinates.z = proto.z(); - pose.orientation.o_x = proto.o_x(); - pose.orientation.o_y = proto.o_y(); - pose.orientation.o_z = proto.o_z(); - pose.theta = proto.theta(); - - return pose; -} - -viam::common::v1::Pose pose::to_proto() const { - viam::common::v1::Pose proto; - proto.set_x(coordinates.x); - proto.set_y(coordinates.y); - proto.set_z(coordinates.z); - proto.set_o_x(orientation.o_x); - proto.set_o_y(orientation.o_y); - proto.set_o_z(orientation.o_z); - proto.set_theta(theta); - - return proto; -} - GeometryConfig::GeometryConfig() : geometry_type_(GeometryType::box) {} -GeometryConfig GeometryConfig::from_proto(const viam::common::v1::Geometry& proto) { - GeometryConfig cfg; - const auto& pose = proto.center(); - cfg.pose_ = pose::from_proto(pose); - cfg.label_ = proto.label(); - - switch (proto.geometry_type_case()) { - case viam::common::v1::Geometry::GeometryTypeCase::kBox: { - cfg.geometry_type_ = GeometryType::box; - struct box box; - box.x = proto.box().dims_mm().x(); - box.y = proto.box().dims_mm().y(); - box.z = proto.box().dims_mm().z(); - cfg.set_geometry_specifics(box); - return cfg; - } - case viam::common::v1::Geometry::GeometryTypeCase::kSphere: { - auto r = proto.sphere().radius_mm(); - if (r == 0) { - cfg.geometry_type_ = GeometryType::point; - } else { - cfg.geometry_type_ = GeometryType::sphere; - } - struct sphere sphere({r}); - cfg.set_geometry_specifics(sphere); - return cfg; - } - case viam::common::v1::Geometry::GeometryTypeCase::kCapsule: { - cfg.geometry_type_ = GeometryType::capsule; - struct capsule capsule; - capsule.radius = proto.capsule().radius_mm(); - capsule.length = proto.capsule().length_mm(); - cfg.set_geometry_specifics(capsule); - return cfg; - } - case viam::common::v1::Geometry::GeometryTypeCase::GEOMETRY_TYPE_NOT_SET: - default: { - throw Exception(ErrorCondition::k_not_supported, "Geometry type is not supported"); - } - } -} -std::vector GeometryConfig::from_proto( - const viam::common::v1::GetGeometriesResponse& proto) { - std::vector response; - for (const auto& geometry : proto.geometries()) { - response.push_back(from_proto(geometry)); - } - return response; -} - -viam::common::v1::Geometry GeometryConfig::to_proto() const { - viam::common::v1::Geometry geometry_; - *geometry_.mutable_label() = label_; - *geometry_.mutable_center() = pose_proto(); - switch (geometry_type_) { - case GeometryType::box: { - *geometry_.mutable_box() = box_proto(); - return geometry_; - } - case GeometryType::sphere: { - *geometry_.mutable_sphere() = sphere_proto(); - return geometry_; - } - case point: { - viam::common::v1::Sphere sphere; - sphere.set_radius_mm(0); - *geometry_.mutable_sphere() = sphere; - return geometry_; - } - case capsule: { - *geometry_.mutable_capsule() = capsule_proto(); - return geometry_; - } - case unknown: - default: { - if (pose_.coordinates.x == 0 && pose_.coordinates.y == 0 && pose_.coordinates.z == 0) { - *geometry_.mutable_box() = box_proto(); - } else { - *geometry_.mutable_sphere() = sphere_proto(); - } - return geometry_; - } - } -} void GeometryConfig::set_coordinates(coordinates coordinates) { pose_.coordinates = std::move(coordinates); } @@ -276,44 +119,5 @@ bool operator==(const geo_geometry& lhs, const geo_geometry& rhs) { return lhs.location == rhs.location && lhs.geometries == rhs.geometries; } -common::v1::GeoPoint geo_point::to_proto() const { - common::v1::GeoPoint proto; - proto.set_latitude(latitude); - proto.set_longitude(longitude); - - return proto; -} - -geo_point geo_point::from_proto(const common::v1::GeoPoint& proto) { - struct geo_point geo_point; - geo_point.latitude = proto.latitude(); - geo_point.longitude = proto.longitude(); - - return geo_point; -} - -common::v1::GeoGeometry geo_geometry::to_proto() const { - common::v1::GeoGeometry proto; - *proto.mutable_location() = location.to_proto(); - - for (const auto& geometry : geometries) { - *proto.mutable_geometries()->Add() = geometry.to_proto(); - } - - return proto; -} - -geo_geometry geo_geometry::from_proto(const common::v1::GeoGeometry& proto) { - struct geo_geometry geo_geometry; - - geo_geometry.location = geo_point::from_proto(proto.location()); - for (const auto& proto_geometry : proto.geometries()) { - auto geometry = GeometryConfig::from_proto(proto_geometry); - geo_geometry.geometries.push_back(std::move(geometry)); - } - - return geo_geometry; -} - } // namespace sdk } // namespace viam diff --git a/src/viam/sdk/spatialmath/geometry.hpp b/src/viam/sdk/spatialmath/geometry.hpp index 0d0dff78b..d992c3bd6 100644 --- a/src/viam/sdk/spatialmath/geometry.hpp +++ b/src/viam/sdk/spatialmath/geometry.hpp @@ -1,10 +1,6 @@ #pragma once -#include #include -#include - -#include #include #include @@ -41,14 +37,6 @@ typedef boost::variant class GeometryConfig { public: - viam::common::v1::Geometry to_proto() const; - viam::common::v1::RectangularPrism box_proto() const; - viam::common::v1::Sphere sphere_proto() const; - viam::common::v1::Capsule capsule_proto() const; - viam::common::v1::Pose pose_proto() const; - static GeometryConfig from_proto(const viam::common::v1::Geometry& proto); - static std::vector from_proto( - const viam::common::v1::GetGeometriesResponse& proto); void set_coordinates(coordinates coordinates); void set_pose(pose pose); void set_pose_orientation(pose_orientation orientation); @@ -80,8 +68,6 @@ class GeometryConfig { struct geo_point { double longitude, latitude; - common::v1::GeoPoint to_proto() const; - static geo_point from_proto(const common::v1::GeoPoint& proto); friend bool operator==(const geo_point& lhs, const geo_point& rhs); friend std::ostream& operator<<(std::ostream& os, const geo_point& v); }; @@ -90,8 +76,6 @@ struct geo_geometry { geo_point location; std::vector geometries; - common::v1::GeoGeometry to_proto() const; - static geo_geometry from_proto(const common::v1::GeoGeometry& proto); friend bool operator==(const geo_geometry& lhs, const geo_geometry& rhs); }; diff --git a/src/viam/sdk/spatialmath/orientation.cpp b/src/viam/sdk/spatialmath/orientation.cpp index 2de5a6802..3f0b15743 100644 --- a/src/viam/sdk/spatialmath/orientation.cpp +++ b/src/viam/sdk/spatialmath/orientation.cpp @@ -1,21 +1,16 @@ #include -#include #include #include #include -#include - #include #include namespace viam { namespace sdk { -namespace proto = viam::app::v1; - OrientationConfig::OrientationConfig() { type_ = Quaternion; quaternion quat; @@ -26,137 +21,6 @@ OrientationConfig::OrientationConfig() { orientation_ = quat; } -OrientationConfig OrientationConfig::from_proto(const proto::Orientation& proto) { - OrientationConfig cfg; - switch (proto.type_case()) { - case proto::Orientation::TypeCase::kAxisAngles: { - cfg.type_ = AxisAngles; - axis_angles aa; - aa.x = proto.axis_angles().x(); - aa.y = proto.axis_angles().y(); - aa.z = proto.axis_angles().z(); - aa.theta = proto.axis_angles().theta(); - cfg.orientation_ = aa; - break; - } - case proto::Orientation::TypeCase::kEulerAngles: { - cfg.type_ = EulerAngles; - euler_angles ea; - ea.yaw = proto.euler_angles().yaw(); - ea.pitch = proto.euler_angles().pitch(); - ea.roll = proto.euler_angles().roll(); - cfg.orientation_ = ea; - break; - } - case proto::Orientation::TypeCase::kQuaternion: { - cfg.type_ = Quaternion; - quaternion quat; - quat.w = proto.quaternion().w(); - quat.x = proto.quaternion().x(); - quat.y = proto.quaternion().y(); - quat.z = proto.quaternion().z(); - cfg.orientation_ = quat; - break; - } - case proto::Orientation::TypeCase::kVectorDegrees: { - cfg.type_ = OrientationVectorDegrees; - orientation_vector_degrees ovd; - ovd.x = proto.vector_degrees().x(); - ovd.y = proto.vector_degrees().y(); - ovd.z = proto.vector_degrees().z(); - ovd.theta = proto.vector_degrees().theta(); - cfg.orientation_ = ovd; - break; - } - case proto::Orientation::TypeCase::kVectorRadians: { - cfg.type_ = OrientationVector; - orientation_vector ov; - ov.x = proto.vector_radians().x(); - ov.y = proto.vector_radians().y(); - ov.z = proto.vector_radians().z(); - ov.theta = proto.vector_radians().theta(); - cfg.orientation_ = ov; - break; - } - case proto::Orientation::TypeCase::kNoOrientation: { - // if type is NoOrientation, we put a sentinel - // orientation that indicates no rotation - cfg.type_ = Quaternion; - quaternion quat; - quat.w = 1; - quat.x = 0; - quat.y = 0; - quat.z = 0; - cfg.orientation_ = quat; - break; - } - case proto::Orientation::TypeCase::TYPE_NOT_SET: - default: { - throw Exception(ErrorCondition::k_not_supported, "orientation type not known"); - } - } - return cfg; -} - -proto::Orientation OrientationConfig::to_proto() const { - proto::Orientation orientation; - switch (type_) { - case AxisAngles: { - proto::Orientation_AxisAngles aa; - const axis_angles a = boost::get(orientation_); - aa.set_x(a.x); - aa.set_y(a.y); - aa.set_z(a.z); - aa.set_theta(a.theta); - *orientation.mutable_axis_angles() = std::move(aa); - return orientation; - }; - case OrientationVector: { - proto::Orientation_OrientationVectorRadians ovec; - const orientation_vector ov = boost::get(orientation_); - ovec.set_x(ov.x); - ovec.set_y(ov.y); - ovec.set_z(ov.z); - ovec.set_theta(ov.theta); - *orientation.mutable_vector_radians() = std::move(ovec); - return orientation; - }; - case OrientationVectorDegrees: { - proto::Orientation_OrientationVectorDegrees ovec; - const orientation_vector_degrees ovd = - boost::get(orientation_); - ovec.set_x(ovd.x); - ovec.set_y(ovd.y); - ovec.set_z(ovd.z); - ovec.set_theta(ovd.theta); - *orientation.mutable_vector_degrees() = std::move(ovec); - return orientation; - }; - case EulerAngles: { - proto::Orientation_EulerAngles euler; - const euler_angles ea = boost::get(orientation_); - euler.set_pitch(ea.pitch); - euler.set_roll(ea.roll); - euler.set_yaw(ea.yaw); - *orientation.mutable_euler_angles() = std::move(euler); - return orientation; - }; - case Quaternion: { - proto::Orientation_Quaternion quat; - const quaternion q = boost::get(orientation_); - quat.set_w(q.w); - quat.set_x(q.x); - quat.set_y(q.y); - quat.set_z(q.z); - *orientation.mutable_quaternion() = std::move(quat); - return orientation; - }; - default: { - throw Exception(ErrorCondition::k_not_supported, "orientation type not known"); - } - } -} - OrientationType OrientationConfig::get_type() const { return type_; }; diff --git a/src/viam/sdk/spatialmath/orientation.hpp b/src/viam/sdk/spatialmath/orientation.hpp index 6d77980e3..8b6d15b6e 100644 --- a/src/viam/sdk/spatialmath/orientation.hpp +++ b/src/viam/sdk/spatialmath/orientation.hpp @@ -1,12 +1,9 @@ #pragma once -#include #include #include -#include - #include namespace viam { @@ -18,8 +15,6 @@ typedef boost:: class OrientationConfig { public: - viam::app::v1::Orientation to_proto() const; - static OrientationConfig from_proto(const viam::app::v1::Orientation& proto); OrientationConfig(OrientationType type_, std::vector value, orientation orientation) diff --git a/src/viam/sdk/spatialmath/orientation_types.cpp b/src/viam/sdk/spatialmath/orientation_types.cpp index 454154ef3..f20084fe9 100644 --- a/src/viam/sdk/spatialmath/orientation_types.cpp +++ b/src/viam/sdk/spatialmath/orientation_types.cpp @@ -1,17 +1,5 @@ #include -#include - namespace viam { -namespace sdk { - -viam::app::v1::Translation translation::to_proto() const { - viam::app::v1::Translation t; - t.set_x(x); - t.set_y(y); - t.set_z(z); - return t; -} - -} // namespace sdk +namespace sdk {} // namespace sdk } // namespace viam diff --git a/src/viam/sdk/spatialmath/orientation_types.hpp b/src/viam/sdk/spatialmath/orientation_types.hpp index bcd077f6e..7c77cd9e4 100644 --- a/src/viam/sdk/spatialmath/orientation_types.hpp +++ b/src/viam/sdk/spatialmath/orientation_types.hpp @@ -1,7 +1,5 @@ #pragma once -#include - namespace viam { namespace sdk { @@ -35,7 +33,7 @@ struct quaternion { struct translation { double x, y, z; - viam::app::v1::Translation to_proto() const; }; + } // namespace sdk } // namespace viam diff --git a/src/viam/sdk/tests/mocks/mock_robot.cpp b/src/viam/sdk/tests/mocks/mock_robot.cpp index 11eae813b..cd53b520a 100644 --- a/src/viam/sdk/tests/mocks/mock_robot.cpp +++ b/src/viam/sdk/tests/mocks/mock_robot.cpp @@ -7,6 +7,7 @@ #include #include +#include #include #include @@ -35,7 +36,7 @@ pose default_pose(int offset) { } Pose default_proto_pose(int offset = 0) { - return default_pose(offset).to_proto(); + return ::viam::sdk::impl::to_proto(default_pose(offset)); } std::vector mock_operations_response() { diff --git a/src/viam/sdk/tests/test_common.cpp b/src/viam/sdk/tests/test_common.cpp index 0d27d468c..91b500c7e 100644 --- a/src/viam/sdk/tests/test_common.cpp +++ b/src/viam/sdk/tests/test_common.cpp @@ -2,8 +2,7 @@ #include -#include - +#include #include #include #include @@ -24,9 +23,9 @@ BOOST_AUTO_TEST_CASE(test_zero) { Duration input; input.set_nanos(0); input.set_seconds(0); - auto converted = from_proto(input); + auto converted = ::viam::sdk::impl::from_proto(input); BOOST_CHECK_EQUAL(converted.count(), 0); - auto reconverted = to_proto(converted); + auto reconverted = ::viam::sdk::impl::to_proto(converted); BOOST_CHECK_EQUAL(reconverted.nanos(), 0); BOOST_CHECK_EQUAL(reconverted.seconds(), 0); } @@ -35,9 +34,9 @@ BOOST_AUTO_TEST_CASE(test_rounding_negative) { Duration input; input.set_nanos(-100); input.set_seconds(0); - auto converted = from_proto(input); + auto converted = ::viam::sdk::impl::from_proto(input); BOOST_CHECK_EQUAL(converted.count(), -1); - auto reconverted = to_proto(converted); + auto reconverted = ::viam::sdk::impl::to_proto(converted); BOOST_CHECK_EQUAL(reconverted.nanos(), -1000); BOOST_CHECK_EQUAL(reconverted.seconds(), 0); } @@ -46,9 +45,9 @@ BOOST_AUTO_TEST_CASE(test_rounding_positive) { Duration input; input.set_nanos(999); input.set_seconds(0); - auto converted = from_proto(input); + auto converted = ::viam::sdk::impl::from_proto(input); BOOST_CHECK_EQUAL(converted.count(), 1); - auto reconverted = to_proto(converted); + auto reconverted = ::viam::sdk::impl::to_proto(converted); BOOST_CHECK_EQUAL(reconverted.nanos(), 1000); BOOST_CHECK_EQUAL(reconverted.seconds(), 0); } @@ -58,9 +57,9 @@ BOOST_AUTO_TEST_CASE(test_mixed_sign_rounding) { // Should round to -1 μs input.set_nanos(-500); input.set_seconds(1); - auto converted = from_proto(input); + auto converted = ::viam::sdk::impl::from_proto(input); BOOST_CHECK_EQUAL(converted.count(), 1e6 - 1); - auto reconverted = to_proto(converted); + auto reconverted = ::viam::sdk::impl::to_proto(converted); BOOST_CHECK_EQUAL(reconverted.nanos(), 1e9 - 1000); BOOST_CHECK_EQUAL(reconverted.seconds(), 0); } @@ -70,9 +69,9 @@ BOOST_AUTO_TEST_CASE(test_medium_positive) { // Should round to 2 μs input.set_nanos(1500); input.set_seconds(1000); - auto converted = from_proto(input); + auto converted = ::viam::sdk::impl::from_proto(input); BOOST_CHECK_EQUAL(converted.count(), 1000 * 1e6 + 2); - auto reconverted = to_proto(converted); + auto reconverted = ::viam::sdk::impl::to_proto(converted); BOOST_CHECK_EQUAL(reconverted.nanos(), 2000); BOOST_CHECK_EQUAL(reconverted.seconds(), 1000); } @@ -85,9 +84,9 @@ BOOST_AUTO_TEST_CASE(test_large_positive) { // compliant with the proto spec int64_t max_seconds = 10e3 * 365 * 24 * 60 * 60; input.set_seconds(max_seconds); - auto converted = from_proto(input); + auto converted = ::viam::sdk::impl::from_proto(input); BOOST_CHECK_EQUAL(converted.count(), 1e6 * max_seconds + 2); - auto reconverted = to_proto(converted); + auto reconverted = ::viam::sdk::impl::to_proto(converted); BOOST_CHECK_EQUAL(reconverted.nanos(), 2000); BOOST_CHECK_EQUAL(reconverted.seconds(), max_seconds); } diff --git a/src/viam/sdk/tests/test_resource.cpp b/src/viam/sdk/tests/test_resource.cpp index 39cdedb5e..2e8ec2453 100644 --- a/src/viam/sdk/tests/test_resource.cpp +++ b/src/viam/sdk/tests/test_resource.cpp @@ -4,6 +4,7 @@ #include #include #include +#include #include #include #include @@ -50,7 +51,7 @@ BOOST_AUTO_TEST_CASE(test_name) { BOOST_CHECK_EQUAL(name1.name(), "name"); BOOST_CHECK_EQUAL(name1.short_name(), "remote:name"); BOOST_CHECK_EQUAL(name1.to_string(), "ns:service:st/remote:name"); - BOOST_CHECK_EQUAL(Name::from_proto(name1.to_proto()), name1); + BOOST_CHECK_EQUAL(impl::from_proto(impl::to_proto(name1)), name1); Name name2(API::from_string("ns:service:st"), "remote1:remote2", "name"); BOOST_CHECK_EQUAL(name2.api().to_string(), "ns:service:st"); @@ -58,7 +59,7 @@ BOOST_AUTO_TEST_CASE(test_name) { BOOST_CHECK_EQUAL(name2.name(), "name"); BOOST_CHECK_EQUAL(name2.short_name(), "remote1:remote2:name"); BOOST_CHECK_EQUAL(name2.to_string(), "ns:service:st/remote1:remote2:name"); - BOOST_CHECK_EQUAL(Name::from_proto(name2.to_proto()), name2); + BOOST_CHECK_EQUAL(impl::from_proto(impl::to_proto(name2)), name2); Name name3 = Name::from_string("ns:component:st/name"); BOOST_CHECK_EQUAL(name3.api().to_string(), "ns:component:st"); @@ -66,7 +67,7 @@ BOOST_AUTO_TEST_CASE(test_name) { BOOST_CHECK_EQUAL(name3.name(), "name"); BOOST_CHECK_EQUAL(name3.short_name(), "name"); BOOST_CHECK_EQUAL(name3.to_string(), "ns:component:st/name"); - BOOST_CHECK_EQUAL(Name::from_proto(name3.to_proto()), name3); + BOOST_CHECK_EQUAL(impl::from_proto(impl::to_proto(name3)), name3); BOOST_CHECK_THROW(Name::from_string("ns:service:#st/remote:name"), Exception); } @@ -132,34 +133,34 @@ BOOST_AUTO_TEST_CASE(test_linkconfig) { *frame.mutable_orientation() = o; *frame.mutable_translation() = t; - LinkConfig lc = LinkConfig::from_proto(frame); + LinkConfig lc = impl::from_proto(frame); BOOST_CHECK_EQUAL(lc.get_parent(), "parent"); BOOST_CHECK_EQUAL(lc.get_translation().x, t.x()); BOOST_CHECK_EQUAL(lc.get_translation().y, t.y()); BOOST_CHECK_EQUAL(lc.get_translation().z, t.z()); GeometryConfig gcfg = lc.get_geometry_config(); BOOST_CHECK_EQUAL(gcfg.get_label(), "label"); - BOOST_CHECK_EQUAL(gcfg.get_pose(), pose::from_proto(pose)); + BOOST_CHECK_EQUAL(gcfg.get_pose(), impl::from_proto(pose)); BOOST_CHECK_EQUAL(gcfg.get_geometry_type(), GeometryType::box); - const auto gs = gcfg.box_proto(); + const auto gs = impl::box_proto(gcfg); BOOST_CHECK_EQUAL(gs.dims_mm().x(), box.dims_mm().x()); BOOST_CHECK_EQUAL(gs.dims_mm().y(), box.dims_mm().y()); BOOST_CHECK_EQUAL(gs.dims_mm().z(), box.dims_mm().z()); - viam::app::v1::Frame proto_lc = lc.to_proto(); + viam::app::v1::Frame proto_lc = impl::to_proto(lc); BOOST_CHECK_EQUAL(proto_lc.parent(), "parent"); BOOST_CHECK_EQUAL(proto_lc.translation().x(), t.x()); BOOST_CHECK_EQUAL(proto_lc.translation().y(), t.y()); BOOST_CHECK_EQUAL(proto_lc.translation().z(), t.z()); BOOST_CHECK(proto_lc.has_orientation()); - viam::app::v1::Orientation proto_ocfg = proto_lc.orientation(); + const viam::app::v1::Orientation& proto_ocfg = proto_lc.orientation(); BOOST_CHECK_EQUAL(proto_ocfg.type_case(), viam::app::v1::Orientation::TypeCase::kAxisAngles); BOOST_CHECK_EQUAL(proto_ocfg.axis_angles().x(), o.axis_angles().x()); BOOST_CHECK_EQUAL(proto_ocfg.axis_angles().y(), o.axis_angles().y()); BOOST_CHECK_EQUAL(proto_ocfg.axis_angles().z(), o.axis_angles().z()); BOOST_CHECK_EQUAL(proto_ocfg.axis_angles().theta(), o.axis_angles().theta()); BOOST_CHECK(proto_lc.has_geometry()); - viam::common::v1::Geometry proto_gcfg = proto_lc.geometry(); + const viam::common::v1::Geometry& proto_gcfg = proto_lc.geometry(); BOOST_CHECK_EQUAL(proto_gcfg.label(), "label"); BOOST_CHECK_EQUAL(proto_gcfg.center().x(), pose.x()); BOOST_CHECK_EQUAL(proto_gcfg.center().y(), pose.y()); @@ -230,7 +231,7 @@ BOOST_AUTO_TEST_CASE(test_resource) { *frame.mutable_translation() = t; *proto_cfg.mutable_frame() = frame; - ResourceConfig resource2 = ResourceConfig::from_proto(proto_cfg); + ResourceConfig resource2 = impl::from_proto(proto_cfg); BOOST_CHECK_EQUAL(resource2.name(), "name"); BOOST_CHECK_EQUAL(resource2.namespace_(), "ns"); BOOST_CHECK_EQUAL(resource2.type(), "type"); @@ -247,7 +248,7 @@ BOOST_AUTO_TEST_CASE(test_resource) { BOOST_CHECK_EQUAL(value.number_value(), 1); *proto_cfg.mutable_api() = "ns:component:test"; - BOOST_CHECK_THROW(ResourceConfig::from_proto(proto_cfg), Exception); + BOOST_CHECK_THROW(impl::from_proto(proto_cfg), Exception); } } // namespace sdktests