-
Notifications
You must be signed in to change notification settings - Fork 165
/
Copy pathgripper_command_tests.cpp
178 lines (146 loc) · 5.5 KB
/
gripper_command_tests.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// Copyright (c) 2023 Franka Robotics GmbH
// Use of this source code is governed by the Apache-2.0 license, see LICENSE
#include <gmock/gmock.h>
#include <franka/exception.h>
#include <franka/gripper.h>
#include "helpers.h"
#include "mock_server.h"
using franka::CommandException;
using franka::Gripper;
using franka::GripperState;
using franka::IncompatibleVersionException;
using franka::Network;
using namespace research_interface::gripper;
template <typename T>
class GripperCommand : public ::testing::Test {
public:
using TCommand = T;
bool executeCommand(Gripper& gripper);
typename T::Request getExpected();
typename T::Status getSuccess();
bool compare(const typename T::Request& request_one, const typename T::Request& request_two);
typename T::Response createResponse(const typename T::Request& request,
const typename T::Status status);
};
template <typename T>
typename T::Status GripperCommand<T>::getSuccess() {
return T::Status::kSuccess;
}
template <typename T>
bool GripperCommand<T>::compare(const typename T::Request&, const typename T::Request&) {
return true;
}
template <>
bool GripperCommand<Grasp>::compare(const Grasp::Request& request_one,
const Grasp::Request& request_two) {
return request_one.width == request_two.width &&
request_one.epsilon.inner == request_two.epsilon.inner &&
request_one.epsilon.outer == request_two.epsilon.outer &&
request_one.speed == request_two.speed && request_one.force == request_two.force;
}
template <>
bool GripperCommand<Move>::compare(const Move::Request& request_one,
const Move::Request& request_two) {
return request_one.width == request_two.width && request_one.speed == request_two.speed;
}
template <typename T>
typename T::Request GripperCommand<T>::getExpected() {
return typename T::Request();
}
template <>
Move::Request GripperCommand<Move>::getExpected() {
double width = 0.05;
double speed = 0.1;
return Move::Request(width, speed);
}
template <>
Grasp::Request GripperCommand<Grasp>::getExpected() {
double width = 0.05;
double epsilon_inner = 0.004;
double epsilon_outer = 0.005;
double speed = 0.1;
double force = 400.0;
return Grasp::Request(width, {epsilon_inner, epsilon_outer}, speed, force);
}
template <>
bool GripperCommand<Move>::executeCommand(Gripper& gripper) {
double width = 0.05;
double speed = 0.1;
return gripper.move(width, speed);
}
template <>
bool GripperCommand<Grasp>::executeCommand(Gripper& gripper) {
double width = 0.05;
double epsilon_inner = 0.004;
double epsilon_outer = 0.005;
double speed = 0.1;
double force = 400.0;
return gripper.grasp(width, speed, force, epsilon_inner, epsilon_outer);
}
template <>
bool GripperCommand<Stop>::executeCommand(Gripper& gripper) {
return gripper.stop();
}
template <>
bool GripperCommand<Homing>::executeCommand(Gripper& gripper) {
return gripper.homing();
}
template <typename T>
typename T::Response GripperCommand<T>::createResponse(const typename T::Request&,
const typename T::Status status) {
return typename T::Response(status);
}
using CommandTypes = ::testing::Types<Homing, Move, Grasp, Stop>;
TYPED_TEST_CASE(GripperCommand, CommandTypes);
TYPED_TEST(GripperCommand, CanSendAndReceiveSuccess) {
GripperMockServer server;
Gripper gripper("127.0.0.1");
server
.waitForCommand<typename TestFixture::TCommand>(
[this](const typename TestFixture::TCommand::Request& request) ->
typename TestFixture::TCommand::Response {
EXPECT_TRUE(this->compare(request, this->getExpected()));
return this->createResponse(request, this->getSuccess());
})
.spinOnce();
EXPECT_NO_THROW(TestFixture::executeCommand(gripper));
}
TYPED_TEST(GripperCommand, CanSendAndReceiveFail) {
GripperMockServer server;
Gripper gripper("127.0.0.1");
server
.waitForCommand<typename TestFixture::TCommand>(
[this](const typename TestFixture::TCommand::Request& request) ->
typename TestFixture::TCommand::Response {
EXPECT_TRUE(this->compare(request, this->getExpected()));
return this->createResponse(request, TestFixture::TCommand::Status::kFail);
})
.spinOnce();
EXPECT_THROW(TestFixture::executeCommand(gripper), CommandException);
}
TYPED_TEST(GripperCommand, CanSendAndReceiveUnsucessful) {
GripperMockServer server;
Gripper gripper("127.0.0.1");
server
.waitForCommand<typename TestFixture::TCommand>(
[this](const typename TestFixture::TCommand::Request& request) ->
typename TestFixture::TCommand::Response {
EXPECT_TRUE(this->compare(request, this->getExpected()));
return this->createResponse(request, TestFixture::TCommand::Status::kUnsuccessful);
})
.spinOnce();
EXPECT_FALSE(TestFixture::executeCommand(gripper));
}
TYPED_TEST(GripperCommand, CanSendAndReceiveAborted) {
GripperMockServer server;
Gripper gripper("127.0.0.1");
server
.waitForCommand<typename TestFixture::TCommand>(
[this](const typename TestFixture::TCommand::Request& request) ->
typename TestFixture::TCommand::Response {
EXPECT_TRUE(this->compare(request, this->getExpected()));
return this->createResponse(request, TestFixture::TCommand::Status::kAborted);
})
.spinOnce();
EXPECT_THROW(TestFixture::executeCommand(gripper), CommandException);
}