Skip to content

Commit

Permalink
RSDK-6634 - remove resource proto conversions from public headers (#328)
Browse files Browse the repository at this point in the history
  • Loading branch information
stuqdog authored Nov 19, 2024
1 parent 7c8487c commit c6ab72d
Show file tree
Hide file tree
Showing 62 changed files with 740 additions and 1,045 deletions.
4 changes: 2 additions & 2 deletions src/viam/examples/modules/complex/proto/buf.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ deps:
- remote: buf.build
owner: googleapis
repository: googleapis
commit: e7f8d366f5264595bcc4cd4139af9973
digest: shake256:e5e5f1c12f82e028ea696faa43b4f9dc6258a6d1226282962a8c8b282e10946281d815884f574bd279ebd9cd7588629beb3db17b892af6c33b56f92f8f67f509
commit: c0913f24652a4cfc95f77d97443a5005
digest: shake256:0ef3248c6235d420fe61f373154adcde6b94e3297f82472b1d8d8c3747240b61b4a10405e2a6f8ac1c98816ac6e690ea7871024aa5ae0e035cd540214667ceed
1 change: 1 addition & 0 deletions src/viam/sdk/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 0 additions & 14 deletions src/viam/sdk/components/arm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,6 @@ API API::traits<Arm>::api() {
return {kRDK, kComponent, "arm"};
}

Arm::KinematicsData Arm::from_proto(const viam::common::v1::GetKinematicsResponse& proto) {
std::vector<unsigned char> 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
Expand Down
4 changes: 0 additions & 4 deletions src/viam/sdk/components/arm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
#include <boost/optional/optional.hpp>
#include <boost/variant/variant.hpp>

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

#include <viam/sdk/common/pose.hpp>
#include <viam/sdk/resource/stoppable.hpp>
#include <viam/sdk/spatialmath/geometry.hpp>
Expand Down Expand Up @@ -67,8 +65,6 @@ class Arm : public Component, public Stoppable {
boost::optional<double> 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() {
Expand Down
14 changes: 0 additions & 14 deletions src/viam/sdk/components/base.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
#include <viam/sdk/components/base.hpp>

#include <google/protobuf/descriptor.h>

#include <viam/api/component/base/v1/base.grpc.pb.h>
#include <viam/api/component/base/v1/base.pb.h>

#include <viam/sdk/common/utils.hpp>
#include <viam/sdk/resource/resource.hpp>

namespace viam {
namespace sdk {

Expand All @@ -19,12 +11,6 @@ API API::traits<Base>::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
Expand Down
4 changes: 0 additions & 4 deletions src/viam/sdk/components/base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@

#include <string>

#include <viam/api/component/base/v1/base.pb.h>

#include <viam/sdk/common/linear_algebra.hpp>
#include <viam/sdk/common/proto_value.hpp>
#include <viam/sdk/common/utils.hpp>
Expand All @@ -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);
Expand Down
57 changes: 0 additions & 57 deletions src/viam/sdk/components/board.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@

#include <google/protobuf/descriptor.h>

#include <viam/api/component/board/v1/board.grpc.pb.h>
#include <viam/api/component/board/v1/board.pb.h>

#include <viam/sdk/common/exception.hpp>
#include <viam/sdk/common/utils.hpp>
#include <viam/sdk/resource/resource.hpp>

Expand All @@ -20,59 +16,6 @@ API API::traits<Board>::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) {
Expand Down
14 changes: 0 additions & 14 deletions src/viam/sdk/components/board.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
#include <string>
#include <unordered_map>

#include <viam/api/component/board/v1/board.pb.h>

#include <viam/sdk/common/proto_value.hpp>
#include <viam/sdk/common/utils.hpp>
#include <viam/sdk/config/resource.hpp>
Expand Down Expand Up @@ -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
Expand Down
125 changes: 0 additions & 125 deletions src/viam/sdk/components/camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@
#include <google/protobuf/descriptor.h>
#include <google/protobuf/util/time_util.h>

#include <viam/api/common/v1/common.pb.h>
#include <viam/api/component/camera/v1/camera.grpc.pb.h>
#include <viam/api/component/camera/v1/camera.pb.h>

#include <viam/sdk/common/exception.hpp>
#include <viam/sdk/common/utils.hpp>
#include <viam/sdk/resource/resource.hpp>
Expand Down Expand Up @@ -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<unsigned char> 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<Camera::raw_image> images;
for (const auto& img : proto.images()) {
Camera::raw_image raw_image;
std::string img_string = img.image();
const std::vector<unsigned char> 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<unsigned char> 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) {
Expand Down
Loading

0 comments on commit c6ab72d

Please sign in to comment.