From c6ab72dd6228ba3d12546fd7893f22df74194bb9 Mon Sep 17 00:00:00 2001 From: Ethan Date: Tue, 19 Nov 2024 11:02:20 -0500 Subject: [PATCH] RSDK-6634 - remove resource proto conversions from public headers (#328) --- .../examples/modules/complex/proto/buf.lock | 4 +- src/viam/sdk/CMakeLists.txt | 1 + src/viam/sdk/components/arm.cpp | 14 - src/viam/sdk/components/arm.hpp | 4 - src/viam/sdk/components/base.cpp | 14 - src/viam/sdk/components/base.hpp | 4 - src/viam/sdk/components/board.cpp | 57 ---- src/viam/sdk/components/board.hpp | 14 - src/viam/sdk/components/camera.cpp | 125 ------- src/viam/sdk/components/camera.hpp | 40 +-- src/viam/sdk/components/encoder.cpp | 76 ----- src/viam/sdk/components/encoder.hpp | 21 -- src/viam/sdk/components/gantry.hpp | 3 - src/viam/sdk/components/generic.cpp | 9 - src/viam/sdk/components/generic.hpp | 2 - src/viam/sdk/components/gripper.hpp | 3 - src/viam/sdk/components/motor.cpp | 41 --- src/viam/sdk/components/motor.hpp | 20 -- src/viam/sdk/components/movement_sensor.cpp | 77 ----- src/viam/sdk/components/movement_sensor.hpp | 32 -- src/viam/sdk/components/pose_tracker.hpp | 3 - src/viam/sdk/components/power_sensor.cpp | 33 -- src/viam/sdk/components/power_sensor.hpp | 16 - .../sdk/components/private/arm_client.cpp | 15 +- .../sdk/components/private/base_client.cpp | 8 +- .../sdk/components/private/base_server.cpp | 2 +- .../sdk/components/private/board_client.cpp | 26 ++ .../sdk/components/private/board_server.cpp | 35 +- .../sdk/components/private/camera_client.cpp | 83 ++++- .../sdk/components/private/camera_server.cpp | 46 ++- src/viam/sdk/components/private/encoder.cpp | 47 +++ src/viam/sdk/components/private/encoder.hpp | 17 + .../sdk/components/private/encoder_client.cpp | 29 +- .../sdk/components/private/encoder_client.hpp | 2 - .../sdk/components/private/encoder_server.cpp | 8 +- .../sdk/components/private/generic_client.cpp | 2 +- .../sdk/components/private/generic_server.cpp | 2 +- .../sdk/components/private/motor_client.cpp | 20 +- .../sdk/components/private/motor_server.cpp | 2 +- .../private/movement_sensor_client.cpp | 41 ++- .../private/movement_sensor_server.cpp | 13 +- .../private/power_sensor_client.cpp | 14 + .../private/power_sensor_server.cpp | 18 +- .../sdk/components/private/sensor_client.cpp | 4 +- .../sdk/components/private/sensor_server.cpp | 2 +- .../sdk/components/private/servo_client.cpp | 6 +- .../sdk/components/private/servo_client.hpp | 1 - .../sdk/components/private/servo_server.cpp | 2 +- src/viam/sdk/components/sensor.cpp | 5 - src/viam/sdk/components/sensor.hpp | 2 - src/viam/sdk/components/servo.cpp | 10 - src/viam/sdk/components/servo.hpp | 5 - src/viam/sdk/services/generic.cpp | 4 - src/viam/sdk/services/generic.hpp | 2 - src/viam/sdk/services/mlmodel.hpp | 2 - src/viam/sdk/services/motion.cpp | 317 ------------------ src/viam/sdk/services/motion.hpp | 28 -- src/viam/sdk/services/navigation.cpp | 3 +- .../sdk/services/private/generic_client.cpp | 2 +- src/viam/sdk/services/private/mlmodel.hpp | 2 + .../sdk/services/private/motion_client.cpp | 173 +++++++++- .../sdk/services/private/motion_server.cpp | 172 +++++++++- 62 files changed, 740 insertions(+), 1045 deletions(-) create mode 100644 src/viam/sdk/components/private/encoder.cpp create mode 100644 src/viam/sdk/components/private/encoder.hpp diff --git a/src/viam/examples/modules/complex/proto/buf.lock b/src/viam/examples/modules/complex/proto/buf.lock index 603482e23..a9e9a6e88 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: c0913f24652a4cfc95f77d97443a5005 + digest: shake256:0ef3248c6235d420fe61f373154adcde6b94e3297f82472b1d8d8c3747240b61b4a10405e2a6f8ac1c98816ac6e690ea7871024aa5ae0e035cd540214667ceed diff --git a/src/viam/sdk/CMakeLists.txt b/src/viam/sdk/CMakeLists.txt index fd38c196e..626254823 100644 --- a/src/viam/sdk/CMakeLists.txt +++ b/src/viam/sdk/CMakeLists.txt @@ -76,6 +76,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/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/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..a78700168 100644 --- a/src/viam/sdk/components/private/arm_client.cpp +++ b/src/viam/sdk/components/private/arm_client.cpp @@ -90,7 +90,20 @@ 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) { diff --git a/src/viam/sdk/components/private/base_client.cpp b/src/viam/sdk/components/private/base_client.cpp index 05ab39bcc..8a31e288e 100644 --- a/src/viam/sdk/components/private/base_client.cpp +++ b/src/viam/sdk/components/private/base_client.cpp @@ -25,7 +25,7 @@ namespace impl { BaseClient::BaseClient(std::string name, std::shared_ptr channel) : Base(std::move(name)), stub_(viam::component::base::v1::BaseService::NewStub(channel)), - channel_(std::move(channel)){}; + channel_(std::move(channel)) {} void BaseClient::move_straight(int64_t distance_mm, double mm_per_sec, const ProtoStruct& extra) { return make_client_helper(this, *stub_, &StubType::MoveStraight) @@ -90,7 +90,11 @@ std::vector BaseClient::get_geometries(const ProtoStruct& extra) 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..238542721 100644 --- a/src/viam/sdk/components/private/base_server.cpp +++ b/src/viam/sdk/components/private/base_server.cpp @@ -14,7 +14,7 @@ namespace sdk { namespace impl { BaseServer::BaseServer(std::shared_ptr manager) - : ResourceServer(std::move(manager)){}; + : ResourceServer(std::move(manager)) {} ::grpc::Status BaseServer::MoveStraight( ::grpc::ServerContext*, diff --git a/src/viam/sdk/components/private/board_client.cpp b/src/viam/sdk/components/private/board_client.cpp index 3f41357df..76ad230c1 100644 --- a/src/viam/sdk/components/private/board_client.cpp +++ b/src/viam/sdk/components/private/board_client.cpp @@ -20,6 +20,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)), diff --git a/src/viam/sdk/components/private/board_server.cpp b/src/viam/sdk/components/private/board_server.cpp index ba4780ac2..6f09e2011 100644 --- a/src/viam/sdk/components/private/board_server.cpp +++ b/src/viam/sdk/components/private/board_server.cpp @@ -1,5 +1,6 @@ #include +#include #include #include #include @@ -11,8 +12,35 @@ 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)){}; + : ResourceServer(std::move(manager)) {} ::grpc::Status BoardServer::SetGPIO(::grpc::ServerContext*, const ::viam::component::board::v1::SetGPIORequest* request, @@ -199,10 +227,9 @@ ::grpc::Status BoardServer::SetPowerMode( "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); + 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()); } }); } diff --git a/src/viam/sdk/components/private/camera_client.cpp b/src/viam/sdk/components/private/camera_client.cpp index e0d9cf18c..5b164e31f 100644 --- a/src/viam/sdk/components/private/camera_client.cpp +++ b/src/viam/sdk/components/private/camera_client.cpp @@ -18,10 +18,91 @@ 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 = response_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)), - channel_(std::move(channel)){}; + channel_(std::move(channel)) {} ProtoStruct CameraClient::do_command(const ProtoStruct& command) { return make_client_helper(this, *stub_, &StubType::DoCommand) diff --git a/src/viam/sdk/components/private/camera_server.cpp b/src/viam/sdk/components/private/camera_server.cpp index 372252ecc..ea1e89154 100644 --- a/src/viam/sdk/components/private/camera_server.cpp +++ b/src/viam/sdk/components/private/camera_server.cpp @@ -14,8 +14,28 @@ 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)){}; + : ResourceServer(std::move(manager)) {} ::grpc::Status CameraServer::DoCommand(::grpc::ServerContext*, const ::viam::common::v1::DoCommandRequest* request, @@ -42,6 +62,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,7 +90,7 @@ ::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); } @@ -109,10 +145,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..4468f25c6 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 @@ -12,6 +10,7 @@ #include #include #include +#include #include #include @@ -19,10 +18,34 @@ 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)), - channel_(std::move(channel)){}; + channel_(std::move(channel)) {} Encoder::position EncoderClient::get_position(const ProtoStruct& extra, position_type position_type) { 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..dfb6c0aa9 100644 --- a/src/viam/sdk/components/private/encoder_server.cpp +++ b/src/viam/sdk/components/private/encoder_server.cpp @@ -1,8 +1,10 @@ #include +#include #include #include #include +#include #include #include @@ -11,7 +13,7 @@ namespace sdk { namespace impl { EncoderServer::EncoderServer(std::shared_ptr manager) - : ResourceServer(std::move(manager)){}; + : ResourceServer(std::move(manager)) {} ::grpc::Status EncoderServer::GetPosition( ::grpc::ServerContext*, @@ -20,9 +22,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)); }); } diff --git a/src/viam/sdk/components/private/generic_client.cpp b/src/viam/sdk/components/private/generic_client.cpp index 21087109a..007dd9e51 100644 --- a/src/viam/sdk/components/private/generic_client.cpp +++ b/src/viam/sdk/components/private/generic_client.cpp @@ -19,7 +19,7 @@ GenericComponentClient::GenericComponentClient(std::string name, std::shared_ptr channel) : GenericComponent(std::move(name)), stub_(viam::component::generic::v1::GenericService::NewStub(channel)), - channel_(std::move(channel)){}; + channel_(std::move(channel)) {} ProtoStruct GenericComponentClient::do_command(const ProtoStruct& command) { return make_client_helper(this, *stub_, &StubType::DoCommand) diff --git a/src/viam/sdk/components/private/generic_server.cpp b/src/viam/sdk/components/private/generic_server.cpp index 0a151a1c8..34a63a0ba 100644 --- a/src/viam/sdk/components/private/generic_server.cpp +++ b/src/viam/sdk/components/private/generic_server.cpp @@ -9,7 +9,7 @@ namespace sdk { namespace impl { GenericComponentServer::GenericComponentServer(std::shared_ptr manager) - : ResourceServer(std::move(manager)){}; + : ResourceServer(std::move(manager)) {} ::grpc::Status GenericComponentServer::DoCommand( ::grpc::ServerContext*, diff --git a/src/viam/sdk/components/private/motor_client.cpp b/src/viam/sdk/components/private/motor_client.cpp index b12f46d96..a734502cc 100644 --- a/src/viam/sdk/components/private/motor_client.cpp +++ b/src/viam/sdk/components/private/motor_client.cpp @@ -16,10 +16,28 @@ 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)), - channel_(std::move(channel)){}; + channel_(std::move(channel)) {} void MotorClient::set_power(double power_pct, const ProtoStruct& extra) { return make_client_helper(this, *stub_, &StubType::SetPower) diff --git a/src/viam/sdk/components/private/motor_server.cpp b/src/viam/sdk/components/private/motor_server.cpp index 70b7c4559..75cb6cc7c 100644 --- a/src/viam/sdk/components/private/motor_server.cpp +++ b/src/viam/sdk/components/private/motor_server.cpp @@ -12,7 +12,7 @@ namespace sdk { namespace impl { MotorServer::MotorServer(std::shared_ptr manager) - : ResourceServer(std::move(manager)){}; + : ResourceServer(std::move(manager)) {} ::grpc::Status MotorServer::SetPower(::grpc::ServerContext*, const ::viam::component::motor::v1::SetPowerRequest* request, diff --git a/src/viam/sdk/components/private/movement_sensor_client.cpp b/src/viam/sdk/components/private/movement_sensor_client.cpp index e9be9228f..7646cbe85 100644 --- a/src/viam/sdk/components/private/movement_sensor_client.cpp +++ b/src/viam/sdk/components/private/movement_sensor_client.cpp @@ -6,9 +6,6 @@ #include #include -#include -#include - #include #include #include @@ -20,10 +17,46 @@ 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 = geo_point::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)), - channel_(std::move(channel)){}; + channel_(std::move(channel)) {} using namespace viam::component::movementsensor::v1; diff --git a/src/viam/sdk/components/private/movement_sensor_server.cpp b/src/viam/sdk/components/private/movement_sensor_server.cpp index eb44c409a..6cfb32dc0 100644 --- a/src/viam/sdk/components/private/movement_sensor_server.cpp +++ b/src/viam/sdk/components/private/movement_sensor_server.cpp @@ -13,8 +13,17 @@ 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)){}; + : ResourceServer(std::move(manager)) {} ::grpc::Status MovementSensorServer::GetLinearVelocity( ::grpc::ServerContext*, @@ -61,7 +70,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); }); } 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..04e0b3e1a 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 @@ -21,7 +19,7 @@ namespace impl { SensorClient::SensorClient(std::string name, std::shared_ptr channel) : Sensor(std::move(name)), stub_(viam::component::sensor::v1::SensorService::NewStub(channel)), - channel_(std::move(channel)){}; + channel_(std::move(channel)) {} using namespace viam::common::v1; diff --git a/src/viam/sdk/components/private/sensor_server.cpp b/src/viam/sdk/components/private/sensor_server.cpp index f763ab0b7..c7a623f63 100644 --- a/src/viam/sdk/components/private/sensor_server.cpp +++ b/src/viam/sdk/components/private/sensor_server.cpp @@ -13,7 +13,7 @@ namespace sdk { namespace impl { SensorServer::SensorServer(std::shared_ptr manager) - : ResourceServer(std::move(manager)){}; + : ResourceServer(std::move(manager)) {} ::grpc::Status SensorServer::GetReadings(::grpc::ServerContext*, const GetReadingsRequest* request, diff --git a/src/viam/sdk/components/private/servo_client.cpp b/src/viam/sdk/components/private/servo_client.cpp index 46ed8a070..1bd483687 100644 --- a/src/viam/sdk/components/private/servo_client.cpp +++ b/src/viam/sdk/components/private/servo_client.cpp @@ -18,10 +18,14 @@ 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)), - channel_(std::move(channel)){}; + channel_(std::move(channel)) {} void ServoClient::move(uint32_t angle_deg, const ProtoStruct& extra) { return make_client_helper(this, *stub_, &StubType::Move) 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..de594db63 100644 --- a/src/viam/sdk/components/private/servo_server.cpp +++ b/src/viam/sdk/components/private/servo_server.cpp @@ -11,7 +11,7 @@ namespace sdk { namespace impl { ServoServer::ServoServer(std::shared_ptr manager) - : ResourceServer(std::move(manager)){}; + : ResourceServer(std::move(manager)) {} ::grpc::Status ServoServer::Move(::grpc::ServerContext*, const ::viam::component::servo::v1::MoveRequest* request, 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/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/navigation.cpp b/src/viam/sdk/services/navigation.cpp index b5d60e069..016927564 100644 --- a/src/viam/sdk/services/navigation.cpp +++ b/src/viam/sdk/services/navigation.cpp @@ -1,13 +1,12 @@ #include -#include #include #include namespace viam { namespace sdk { -Navigation::Navigation(std::string name) : Service(std::move(name)){}; +Navigation::Navigation(std::string name) : Service(std::move(name)) {} API Navigation::api() const { return API::get(); diff --git a/src/viam/sdk/services/private/generic_client.cpp b/src/viam/sdk/services/private/generic_client.cpp index 44e56b9d5..de953d291 100644 --- a/src/viam/sdk/services/private/generic_client.cpp +++ b/src/viam/sdk/services/private/generic_client.cpp @@ -18,7 +18,7 @@ namespace impl { GenericServiceClient::GenericServiceClient(std::string name, std::shared_ptr channel) : GenericService(std::move(name)), stub_(viam::service::generic::v1::GenericService::NewStub(channel)), - channel_(std::move(channel)){}; + channel_(std::move(channel)) {} ProtoStruct GenericServiceClient::do_command(const ProtoStruct& command) { return make_client_helper(this, *stub_, &StubType::DoCommand) 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..d2c6c12c3 100644 --- a/src/viam/sdk/services/private/motion_client.cpp +++ b/src/viam/sdk/services/private/motion_client.cpp @@ -16,10 +16,169 @@ 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() = od.vision_service.to_proto(); + *proto.mutable_camera() = od.camera.to_proto(); + 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 = 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, pose::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 = 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)), - channel_(std::move(channel)){}; + channel_(std::move(channel)) {} bool MotionClient::move(const pose_in_frame& destination, const Name& component_name, @@ -32,7 +191,7 @@ bool MotionClient::move(const pose_in_frame& destination, *request.mutable_component_name() = component_name.to_proto(); *request.mutable_destination() = destination.to_proto(); 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(); @@ -60,7 +219,7 @@ std::string MotionClient::move_on_map( } 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(); }); @@ -91,7 +250,7 @@ std::string MotionClient::move_on_globe( } 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) { @@ -140,8 +299,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 +333,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..997bc1a67 100644 --- a/src/viam/sdk/services/private/motion_server.cpp +++ b/src/viam/sdk/services/private/motion_server.cpp @@ -2,6 +2,7 @@ #include +#include #include #include #include @@ -14,8 +15,160 @@ 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() = kv.second.to_proto(); + 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() = plan.component_name.to_proto(); + *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() = pswi.component_name.to_proto(); + *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 = Name::from_proto(proto.vision_service()); + oc.camera = Name::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)){}; + : 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, @@ -29,8 +182,7 @@ ::grpc::Status MotionServer::Move(::grpc::ServerContext*, 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()), @@ -54,8 +206,8 @@ ::grpc::Status MotionServer::MoveOnMap( 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; @@ -93,8 +245,8 @@ ::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()) { @@ -160,9 +312,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 +333,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); } }); }