forked from osrf/nexus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmock_transporter.cpp
366 lines (321 loc) · 10.4 KB
/
mock_transporter.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/*
* Copyright (C) 2022 Johnson & Johnson
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#include <nexus_transporter/Transporter.hpp>
#include <mutex>
#include <thread>
#include <unordered_set>
namespace nexus_transporter {
struct Location
{
double pose;
std::optional<std::string> name;
Location(
double pose_,
std::optional<std::string> name_)
: pose(std::move(pose_)),
name(std::move(name_))
{}
};
struct MockTransporter3000
{
std::string name;
Location current_location;
std::optional<Itinerary> itinerary; // If idle and not at `unloading_station` this should be nullopt.
MockTransporter3000(
const std::string& name_,
Location current_location_,
std::optional<Itinerary> itinerary_)
: name(name_),
current_location(current_location_),
itinerary(itinerary_)
{}
};
// MockTransporter that can only process once request at a time.
class MockTransporter : public Transporter
{
public:
bool configure(const rclcpp_lifecycle::LifecycleNode::WeakPtr& node) final
{
auto n = node.lock();
if (!n)
{
return false;
}
RCLCPP_INFO(
n->get_logger(),
"Configuring MockTransporter...");
// Set the x-increment (m)
_increment = n->declare_parameter("x_increment", 2.0);
RCLCPP_INFO(
n->get_logger(),
"MockTransporter x_increment set to [%.2f].",
_increment
);
std::vector<std::string> destinations;
destinations = n->declare_parameter(
"destinations",
std::vector<std::string>(
{"loading", "workcell_1", "workcell_2", "unloading"}));
if (!destinations.empty())
{
double x_pose = 0.0;
for (const auto& dest : destinations)
{
_destinations.insert({dest, x_pose});
x_pose += _increment;
}
}
std::stringstream ss;
for (const auto& d : _destinations)
ss << d.first << ", ";
RCLCPP_INFO(
n->get_logger(),
"MockTransporter configured with the following destinations: %s",
ss.str().c_str()
);
// Set the initial location of all added transporters as first destination in the vector
_initial_location = destinations.front();
// Set the unloading station
_unloading_station = n->declare_parameter("unloading_station", "unloading");
RCLCPP_INFO(
n->get_logger(),
"MockTransporter unloading station set to [%s].",
_unloading_station.c_str()
);
// Set the speed of transporter (m/s)
_speed = n->declare_parameter("speed", 1.0);
RCLCPP_INFO(
n->get_logger(),
"MockTransporter speed set to [%.2f].",
_speed
);
_node = node;
_ready = true;
RCLCPP_INFO(
n->get_logger(),
"Finished configuring MockTransporter!"
);
return true;
}
bool ready() const final
{
return _ready;
}
std::optional<Itinerary> get_itinerary(
const std::string& id,
const std::string& destination)
{
if (_destinations.find(destination) == _destinations.end())
return std::nullopt;
auto n = _node.lock();
const rclcpp::Time now = n ? n->get_clock()->now() : rclcpp::Clock().now();
// Assign transporter
const std::string transporter_name = "mock_transporter_" + id;
auto it = _transporters.insert({transporter_name, nullptr});
if (it.second)
{
// New insertion
it.first->second = std::make_shared<MockTransporter3000>(
transporter_name,
Location(0.0, _initial_location),
std::nullopt);
}
const auto& transporter_location = it.first->second->current_location;
const auto& dest_pose = _destinations.find(destination)->second;
double travel_duration;
if (transporter_location.name.has_value() &&
destination == transporter_location.name)
{
travel_duration = 0.0;
}
else
{
travel_duration = abs(dest_pose - transporter_location.pose)/_speed;
}
return Itinerary{
id,
destination,
transporter_name,
now + rclcpp::Duration::from_seconds(travel_duration),
now + rclcpp::Duration::from_seconds(60.0)
};
}
void transport_to_destination(
const Itinerary& itinerary,
Transporter::TransportFeedback feedback_cb,
Transporter::TransportCompleted completed_cb) final
{
const auto& current_transporter = itinerary.transporter_name();
const auto& current_location =
_transporters.at(current_transporter)->current_location;
auto n = _node.lock();
RCLCPP_INFO(
n->get_logger(),
"MockTransporter %s starting at pose %.2f meters",
current_transporter.c_str(), current_location.pose);
{
std::lock_guard<std::mutex> lock(_mutex);
if (!_transporters.empty() &&
(_transporters.find(current_transporter) != _transporters.end()) &&
(_transporters.at(current_transporter)->itinerary.has_value()))
{
completed_cb(false);
return;
}
}
if (current_location.name.has_value() &&
itinerary.destination() == current_location.name)
{
completed_cb(true);
return;
}
if (_thread.joinable())
_thread.join();
RCLCPP_INFO(n->get_logger(),
"Received request for transporter [%s] to %s",
current_transporter.c_str(), itinerary.destination().c_str());
// TODO(YV): Capture a data_ptr to avoid reference captures
_thread = std::thread(
[this, feedback_cb = feedback_cb, completed_cb = completed_cb](
const Itinerary& itinerary)
{
const auto& current_transporter = itinerary.transporter_name();
{
std::lock_guard<std::mutex> lock(_mutex);
_transporters.at(current_transporter)->itinerary = itinerary;
}
const auto& dest_pose =
_destinations.find(itinerary.destination())->second;
const auto& current_pose = _transporters.at(
current_transporter)->current_location.pose;
const auto& dist = abs(dest_pose - current_pose);
const auto now = rclcpp::Clock().now();
// TODO(YV): Make duration a param
const auto finish_time =
now + rclcpp::Duration::from_seconds(dist/_speed);
Transporter::TransporterState state;
state.transporter = current_transporter;
state.model = "MockTransporter3000";
state.task_id = itinerary.id();
state.location.header.frame_id = "world";
state.location.header.stamp = now;
state.location.pose.position.x = current_pose;
state.state = state.STATE_IDLE;
feedback_cb(state);
state.state = state.STATE_MOVING;
_stop = false;
double dist_traveled = 0.0;
while (rclcpp::Clock().now() < finish_time && !_stop)
{
std::this_thread::sleep_for(std::chrono::seconds(1));
double x_increment;
if (dist - dist_traveled < _speed &&
(finish_time - rclcpp::Clock().now()).seconds() <= 0.0)
{
auto gap = dist - dist_traveled;
x_increment = dest_pose < current_pose ?
-1*gap : gap;
}
else
x_increment = dest_pose < current_pose ?
-1*_speed : _speed;
state.location.pose.position.x += x_increment;
dist_traveled += abs(x_increment);
_transporters.at(
current_transporter)->current_location.pose = state.location.pose.position.x;
feedback_cb(state);
}
completed_cb(true);
std::lock_guard<std::mutex> lock(_mutex);
// Check if the transporter is at the unloading station
if (itinerary.destination() == _unloading_station)
{
// Work order completed, ok to remove transporter from list
_transporters.erase(
current_transporter);
}
else
{
// Update the transporter location
_transporters.at(
current_transporter)->current_location.name = std::nullopt;
for (const auto& it : _destinations)
{
if (it.second ==
_transporters.at(current_transporter)->current_location.pose)
{
_transporters.at(
current_transporter)->current_location.name = it.first;
break;
}
}
_transporters.at(current_transporter)->itinerary = std::nullopt;
}
}, itinerary);
}
bool cancel(const Itinerary& itinerary) final
{
std::unique_lock lock(_mutex);
const auto& transporter = itinerary.transporter_name();
if (_transporters.find(transporter) == _transporters.end())
{
return true;
}
if (_transporters.at(transporter)->itinerary.has_value())
{
_stop = true;
// need to unlock first to avoid deadlock
lock.unlock();
if (_thread.joinable())
_thread.join();
return true;
}
return false;
}
~MockTransporter()
{
_stop = true;
if (_thread.joinable())
_thread.join();
}
private:
/// A weak_ptr to a lifecycle node
rclcpp_lifecycle::LifecycleNode::WeakPtr _node;
/// A boolean that is set true when the system is initialized and without
/// errors
bool _ready = false;
/// The MockTransporter system offers multiple transporters capable of transporting items
std::unordered_map<std::string,
std::shared_ptr<MockTransporter3000>> _transporters;
/// First stop in the list of destinations where the transporter starts
std::string _initial_location;
/// A set of destinations that _transporter is capable to visiting
std::unordered_map<std::string, double> _destinations;
/// The final location of each transporter request
std::string _unloading_station;
/// The speed of the transporter in m/s
double _speed;
/// Increment along X axis for transporter destinations
double _increment;
std::thread _thread;
std::mutex _mutex;
std::atomic_bool _stop = false;
};
} // namespace nexus_transporter
#include <pluginlib/class_list_macros.hpp>
PLUGINLIB_EXPORT_CLASS(
nexus_transporter::MockTransporter, nexus_transporter::Transporter)