This repository was archived by the owner on Jul 19, 2024. It is now read-only.
forked from frankaemika/libfranka
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvacuum_gripper_command_tests.cpp
168 lines (136 loc) · 5.68 KB
/
vacuum_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
// Copyright (c) 2019 Franka Emika 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/vacuum_gripper.h>
#include <chrono>
#include "helpers.h"
#include "mock_server.h"
using franka::CommandException;
using franka::IncompatibleVersionException;
using franka::Network;
using franka::VacuumGripper;
using franka::VacuumGripperState;
using namespace research_interface::vacuum_gripper;
template <typename T>
class VacuumGripperCommand : public ::testing::Test {
public:
using TCommand = T;
bool executeCommand(VacuumGripper& vacuum_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 VacuumGripperCommand<T>::getSuccess() {
return T::Status::kSuccess;
}
template <typename T>
bool VacuumGripperCommand<T>::compare(const typename T::Request&, const typename T::Request&) {
return true;
}
template <>
bool VacuumGripperCommand<Vacuum>::compare(const Vacuum::Request& request_one,
const Vacuum::Request& request_two) {
return request_one.vacuum == request_two.vacuum && request_one.profile == request_two.profile &&
request_one.timeout == request_two.timeout;
}
template <>
bool VacuumGripperCommand<DropOff>::compare(const DropOff::Request& request_one,
const DropOff::Request& request_two) {
return request_one.timeout == request_two.timeout;
}
template <typename T>
typename T::Request VacuumGripperCommand<T>::getExpected() {
return typename T::Request();
}
template <>
Vacuum::Request VacuumGripperCommand<Vacuum>::getExpected() {
uint8_t vacuum = 100;
Profile profile = Profile::kP0;
std::chrono::milliseconds timeout = std::chrono::milliseconds(1000);
return Vacuum::Request(vacuum, profile, timeout);
}
template <>
DropOff::Request VacuumGripperCommand<DropOff>::getExpected() {
std::chrono::milliseconds timeout = std::chrono::milliseconds(1000);
return DropOff::Request(timeout);
}
template <>
bool VacuumGripperCommand<Vacuum>::executeCommand(VacuumGripper& vacuum_gripper) {
uint8_t vacuum = 100;
franka::VacuumGripper::ProductionSetupProfile profile =
franka::VacuumGripper::ProductionSetupProfile::kP0;
std::chrono::milliseconds timeout = std::chrono::milliseconds(1000);
return vacuum_gripper.vacuum(vacuum, timeout, profile);
}
template <>
bool VacuumGripperCommand<DropOff>::executeCommand(VacuumGripper& vacuum_gripper) {
std::chrono::milliseconds timeout = std::chrono::milliseconds(1000);
return vacuum_gripper.dropOff(timeout);
}
template <>
bool VacuumGripperCommand<Stop>::executeCommand(VacuumGripper& vacuum_gripper) {
return vacuum_gripper.stop();
}
template <typename T>
typename T::Response VacuumGripperCommand<T>::createResponse(const typename T::Request&,
const typename T::Status status) {
return typename T::Response(status);
}
using CommandTypes = ::testing::Types<Vacuum, DropOff, Stop>;
TYPED_TEST_CASE(VacuumGripperCommand, CommandTypes);
TYPED_TEST(VacuumGripperCommand, CanSendAndReceiveSuccess) {
VacuumGripperMockServer server;
VacuumGripper vacuum_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(vacuum_gripper));
}
TYPED_TEST(VacuumGripperCommand, CanSendAndReceiveFail) {
VacuumGripperMockServer server;
VacuumGripper vacuum_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(vacuum_gripper), CommandException);
}
TYPED_TEST(VacuumGripperCommand, CanSendAndReceiveUnsucessful) {
VacuumGripperMockServer server;
franka::VacuumGripper vacuum_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(vacuum_gripper));
}
TYPED_TEST(VacuumGripperCommand, CanSendAndReceiveAborted) {
VacuumGripperMockServer server;
VacuumGripper vacuum_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(vacuum_gripper), CommandException);
}