Skip to content

Commit 7a55dd4

Browse files
raybjorklia-viam
andauthored
Add MoveThroughJointPositions to Arm component (#324)
Co-authored-by: Lia Stratopoulos <[email protected]>
1 parent 8aed0c9 commit 7a55dd4

File tree

8 files changed

+112
-2
lines changed

8 files changed

+112
-2
lines changed

src/viam/sdk/components/arm.hpp

+23-1
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55

66
#include <string>
77

8+
#include <boost/optional/optional.hpp>
89
#include <boost/variant/variant.hpp>
910

1011
#include <viam/api/common/v1/common.pb.h>
11-
#include <viam/api/component/arm/v1/arm.pb.h>
1212

1313
#include <viam/sdk/common/pose.hpp>
1414
#include <viam/sdk/resource/stoppable.hpp>
@@ -61,6 +61,12 @@ class Arm : public Component, public Stoppable {
6161
using KinematicsData =
6262
boost::variant<KinematicsDataUnspecified, KinematicsDataSVA, KinematicsDataURDF>;
6363

64+
/// @brief Movement specifications for move_through_join_positions.
65+
struct MoveOptions {
66+
boost::optional<double> max_vel_degs_per_sec;
67+
boost::optional<double> max_acc_degs_per_sec2;
68+
};
69+
6470
static KinematicsData from_proto(const viam::common::v1::GetKinematicsResponse& proto);
6571

6672
/// @brief Get the current position of the end of the arm.
@@ -103,6 +109,22 @@ class Arm : public Component, public Stoppable {
103109
virtual void move_to_joint_positions(const std::vector<double>& positions,
104110
const ProtoStruct& extra) = 0;
105111

112+
/// @brief Move each joint on the arm through the positions specified in @param positions
113+
/// @param options optional specifications to be obeyed during the motion.
114+
/// TODO consider replacing vector vector with xtensor array, and also if it may be
115+
/// possible to specify or constrain dimensionality of the array in advance.
116+
inline void move_through_joint_positions(const std::vector<std::vector<double>>& positions,
117+
const MoveOptions& options) {
118+
return move_through_joint_positions(positions, options, {});
119+
}
120+
121+
/// @brief Move each joint on the arm through the positions specified in @param positions
122+
/// @param options optional specifications to be obeyed during the motion.
123+
/// @param extra Any additional arguments to the method.
124+
virtual void move_through_joint_positions(const std::vector<std::vector<double>>& positions,
125+
const MoveOptions& options,
126+
const ProtoStruct& extra) = 0;
127+
106128
/// @brief Reports if the arm is in motion.
107129
virtual bool is_moving() = 0;
108130

src/viam/sdk/components/private/arm_client.cpp

+25
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,31 @@ void ArmClient::move_to_joint_positions(const std::vector<double>& positions,
4646
.invoke();
4747
}
4848

49+
void ArmClient::move_through_joint_positions(const std::vector<std::vector<double>>& positions,
50+
const Arm::MoveOptions& options,
51+
const ProtoStruct& extra) {
52+
return make_client_helper(this, *stub_, &StubType::MoveThroughJointPositions)
53+
.with(extra,
54+
[&](viam::component::arm::v1::MoveThroughJointPositionsRequest& request) {
55+
if (options.max_vel_degs_per_sec) {
56+
request.mutable_options()->set_max_vel_degs_per_sec(
57+
*options.max_vel_degs_per_sec);
58+
}
59+
60+
if (options.max_acc_degs_per_sec2) {
61+
request.mutable_options()->set_max_acc_degs_per_sec2(
62+
*options.max_acc_degs_per_sec2);
63+
}
64+
65+
for (const auto& pos : positions) {
66+
viam::component::arm::v1::JointPositions jpos;
67+
jpos.mutable_values()->Add(pos.begin(), pos.end());
68+
request.mutable_positions()->Add(std::move(jpos));
69+
}
70+
})
71+
.invoke();
72+
}
73+
4974
bool ArmClient::is_moving() {
5075
return make_client_helper(this, *stub_, &StubType::IsMoving).invoke([](auto& response) {
5176
return response.is_moving();

src/viam/sdk/components/private/arm_client.hpp

+4
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ class ArmClient : public Arm {
2626
std::vector<double> get_joint_positions(const ProtoStruct& extra) override;
2727
void move_to_joint_positions(const std::vector<double>& positions,
2828
const ProtoStruct& extra) override;
29+
void move_through_joint_positions(const std::vector<std::vector<double>>& positions,
30+
const Arm::MoveOptions& options,
31+
const ProtoStruct& extra) override;
2932
bool is_moving() override;
3033
void stop(const ProtoStruct& extra) override;
3134
ProtoStruct do_command(const ProtoStruct& command) override;
@@ -38,6 +41,7 @@ class ArmClient : public Arm {
3841
using Arm::get_geometries;
3942
using Arm::get_joint_positions;
4043
using Arm::get_kinematics;
44+
using Arm::move_through_joint_positions;
4145
using Arm::move_to_joint_positions;
4246
using Arm::move_to_position;
4347
using Arm::stop;

src/viam/sdk/components/private/arm_server.cpp

+26
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,32 @@ ::grpc::Status ArmServer::MoveToJointPositions(
5353
});
5454
}
5555

56+
::grpc::Status ArmServer::MoveThroughJointPositions(
57+
::grpc::ServerContext*,
58+
const ::viam::component::arm::v1::MoveThroughJointPositionsRequest* request,
59+
::viam::component::arm::v1::MoveThroughJointPositionsResponse*) noexcept {
60+
return make_service_helper<Arm>(
61+
"ArmServer::MoveThroughJointPositions", this, request)([&](auto& helper, auto& arm) {
62+
std::vector<std::vector<double>> positions;
63+
64+
positions.reserve(request->positions_size());
65+
for (const auto& values : request->positions()) {
66+
positions.emplace_back(values.values().begin(), values.values().end());
67+
}
68+
69+
Arm::MoveOptions opts;
70+
if (request->options().has_max_vel_degs_per_sec()) {
71+
opts.max_vel_degs_per_sec = request->options().max_vel_degs_per_sec();
72+
}
73+
74+
if (request->options().has_max_acc_degs_per_sec2()) {
75+
opts.max_acc_degs_per_sec2 = request->options().max_acc_degs_per_sec2();
76+
}
77+
78+
arm->move_through_joint_positions(positions, opts, helper.getExtra());
79+
});
80+
}
81+
5682
::grpc::Status ArmServer::Stop(::grpc::ServerContext*,
5783
const ::viam::component::arm::v1::StopRequest* request,
5884
::viam::component::arm::v1::StopResponse*) noexcept {

src/viam/sdk/components/private/arm_server.hpp

+5
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ class ArmServer : public ResourceServer, public viam::component::arm::v1::ArmSer
4444
const ::viam::component::arm::v1::MoveToJointPositionsRequest* request,
4545
::viam::component::arm::v1::MoveToJointPositionsResponse* response) noexcept override;
4646

47+
::grpc::Status MoveThroughJointPositions(
48+
::grpc::ServerContext* context,
49+
const ::viam::component::arm::v1::MoveThroughJointPositionsRequest* request,
50+
::viam::component::arm::v1::MoveThroughJointPositionsResponse* response) noexcept override;
51+
4752
::grpc::Status Stop(::grpc::ServerContext* context,
4853
const ::viam::component::arm::v1::StopRequest* request,
4954
::viam::component::arm::v1::StopResponse* response) noexcept override;

src/viam/sdk/tests/mocks/mock_arm.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include <viam/sdk/tests/mocks/mock_arm.hpp>
22

3+
#include "mock_arm.hpp"
34
#include <viam/sdk/tests/test_utils.hpp>
45

56
namespace viam {
@@ -31,6 +32,13 @@ void MockArm::move_to_joint_positions(const std::vector<double>& positions,
3132
joint_positions = positions;
3233
}
3334

35+
void MockArm::move_through_joint_positions(const std::vector<std::vector<double>>& positions,
36+
const Arm::MoveOptions& opts,
37+
const sdk::ProtoStruct&) {
38+
move_thru_positions = positions;
39+
move_opts = opts;
40+
}
41+
3442
void MockArm::stop(const sdk::ProtoStruct&) {
3543
peek_stop_called = true;
3644
}

src/viam/sdk/tests/mocks/mock_arm.hpp

+7
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ class MockArm : public sdk::Arm {
1919
std::vector<double> get_joint_positions(const sdk::ProtoStruct&) override;
2020
void move_to_joint_positions(const std::vector<double>& positions,
2121
const sdk::ProtoStruct&) override;
22+
23+
void move_through_joint_positions(const std::vector<std::vector<double>>& positions,
24+
const Arm::MoveOptions& opts,
25+
const sdk::ProtoStruct&) override;
26+
2227
void stop(const sdk::ProtoStruct&) override;
2328
bool is_moving() override;
2429
sdk::ProtoStruct do_command(const sdk::ProtoStruct& command) override;
@@ -27,6 +32,8 @@ class MockArm : public sdk::Arm {
2732

2833
sdk::pose current_location;
2934
std::vector<double> joint_positions;
35+
std::vector<std::vector<double>> move_thru_positions;
36+
sdk::Arm::MoveOptions move_opts;
3037
bool peek_stop_called;
3138
sdk::ProtoStruct peek_command;
3239
};

src/viam/sdk/tests/test_arm.cpp

+14-1
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
#define BOOST_TEST_MODULE test module test_arm
2+
#include <viam/sdk/components/arm.hpp>
23

4+
#include <boost/optional/optional_io.hpp>
35
#include <boost/qvm/all.hpp>
46
#include <boost/test/included/unit_test.hpp>
57

6-
#include <viam/sdk/components/arm.hpp>
78
#include <viam/sdk/tests/mocks/mock_arm.hpp>
89
#include <viam/sdk/tests/test_utils.hpp>
910

1011
BOOST_TEST_DONT_PRINT_LOG_VALUE(std::vector<viam::sdk::GeometryConfig>)
1112
BOOST_TEST_DONT_PRINT_LOG_VALUE(std::vector<double>)
13+
BOOST_TEST_DONT_PRINT_LOG_VALUE(std::vector<std::vector<double>>)
1214
BOOST_TEST_DONT_PRINT_LOG_VALUE(viam::sdk::Arm::KinematicsData)
1315

1416
namespace viam {
@@ -49,6 +51,17 @@ BOOST_AUTO_TEST_CASE(joint_positions) {
4951
});
5052
}
5153

54+
BOOST_AUTO_TEST_CASE(thru_joint_positions) {
55+
std::shared_ptr<MockArm> mock = MockArm::get_mock_arm();
56+
client_to_mock_pipeline<Arm>(mock, [&](Arm& client) {
57+
std::vector<std::vector<double>> positions{{1.0, 2.0}, {3.0}};
58+
client.move_through_joint_positions(positions, {1.0, 2.0}, {});
59+
BOOST_CHECK_EQUAL(mock->move_thru_positions, positions);
60+
BOOST_CHECK_EQUAL(mock->move_opts.max_vel_degs_per_sec, 1.0);
61+
BOOST_CHECK_EQUAL(mock->move_opts.max_acc_degs_per_sec2, 2.0);
62+
});
63+
}
64+
5265
BOOST_AUTO_TEST_CASE(test_stop) {
5366
std::shared_ptr<MockArm> mock = MockArm::get_mock_arm();
5467
client_to_mock_pipeline<Arm>(mock, [&](Arm& client) {

0 commit comments

Comments
 (0)