2525namespace rmf_fleet_adapter {
2626namespace reservation {
2727
28- // / This class implements the protocol for negotiating a spot with the "reservation"
29- // / node. The reservation node maintains a list of spots which are free.
28+ // / This class implements the protocol for negotiating a spot with the
29+ // / "reservation" node. The reservation node maintains a list of spots which
30+ // / are free.
3031class ReservationNodeNegotiator :
3132 public std::enable_shared_from_this<ReservationNodeNegotiator>
3233{
3334public:
35+ // ============================================================================
36+ // / Creates a ReservationNegotiator to handle the reservation system protocol
37+ // / \param[in] context the RobotContext with all of the current robot's
38+ // / context.
39+ // / \param[in] goals a list of potential spots.
40+ // / \param[in] same_map Is the goal on the same map
41+ // / \param[in] selected_final_destination_cb A callback which is triggered
42+ // / when the final destination is selected.
43+ // / \param[in] always_recalculate_nearest_goal Always find the nearest goal
3444 static std::shared_ptr<ReservationNodeNegotiator> make (
3545 std::shared_ptr<agv::RobotContext> context,
3646 const std::vector<rmf_traffic::agv::Plan::Goal> goals,
3747 const bool same_map,
3848 const std::function<void (const rmf_traffic::agv::Plan::Goal&)>
3949 selected_final_destination_cb,
40- const std::function<void(const rmf_traffic::agv::Plan::Goal&)> selected_waitpoint_cb)
50+ const std::function<void(const rmf_traffic::agv::Plan::Goal&)>
51+ selected_waitpoint_cb,
52+ bool always_recalculate_nearest_goal = false)
4153 {
4254 auto negotiator = std::shared_ptr<ReservationNodeNegotiator>(
4355 new ReservationNodeNegotiator (
44- context, goals, selected_final_destination_cb, selected_waitpoint_cb));
56+ context,
57+ goals,
58+ selected_final_destination_cb,
59+ selected_waitpoint_cb));
4560
4661 negotiator->_reservation_ticket =
47- context->node ()->location_ticket_obs ().observe_on (rxcpp::identity_same_worker (
48- context->worker ()))
62+ context->node ()->location_ticket_obs ().observe_on (
63+ rxcpp::identity_same_worker ( context->worker ()))
4964 .subscribe ([ptr = negotiator->weak_from_this ()](
5065 const std::shared_ptr<rmf_reservation_msgs::msg::Ticket>
5166 & msg)
@@ -169,22 +184,24 @@ class ReservationNodeNegotiator :
169184 }
170185 });
171186
172-
173- for (std::size_t i = 0 ; i < negotiator->_goals .size (); ++i)
187+ if (!always_recalculate_nearest_goal)
174188 {
175- if ( events::wp_name (*context. get (), negotiator-> _goals [i]) == context-> _get_reserved_location () )
189+ for (std:: size_t i = 0 ; i < negotiator-> _goals . size (); ++i )
176190 {
177- RCLCPP_INFO (context->node ()->get_logger (),
178- " %s: Already at goal no need to engage reservation system\n " ,
179- context->requester_id ().c_str ());
180- context->worker ().schedule ([
181- cb = negotiator->_selected_final_destination_cb ,
182- wp = negotiator->_goals [i]
183- ](const auto &)
184- {
185- cb (wp);
186- });
187- return negotiator;
191+ if (events::wp_name (*context.get (), negotiator->_goals [i]) == context->_get_reserved_location ())
192+ {
193+ RCLCPP_INFO (context->node ()->get_logger (),
194+ " %s: Already have a goal no need to engage reservation system\n " ,
195+ context->requester_id ().c_str ());
196+ context->worker ().schedule ([
197+ cb = negotiator->_selected_final_destination_cb ,
198+ wp = negotiator->_goals [i]
199+ ](const auto &)
200+ {
201+ cb (wp);
202+ });
203+ return negotiator;
204+ }
188205 }
189206 }
190207 RCLCPP_INFO (negotiator->_context ->node ()->get_logger (),
@@ -205,13 +222,14 @@ class ReservationNodeNegotiator :
205222 }
206223
207224private:
208-
225+ // ============================================================================
209226 ReservationNodeNegotiator (
210227 std::shared_ptr<agv::RobotContext> context,
211228 const std::vector<rmf_traffic::agv::Plan::Goal> goals,
212229 const std::function<void (const rmf_traffic::agv::Plan::Goal&)>
213- selected_final_destination_cb,
214- const std::function<void (const rmf_traffic::agv::Plan::Goal&)> selected_waitpoint_cb)
230+ selected_final_destination_cb,
231+ const std::function<void (const rmf_traffic::agv::Plan::Goal&)>
232+ selected_waitpoint_cb)
215233 {
216234 _context = context;
217235 _goals = goals;
@@ -220,6 +238,7 @@ class ReservationNodeNegotiator :
220238 _reservation_id = _context->last_reservation_request_id ();
221239 }
222240
241+ // ============================================================================
223242 enum class ReservationState
224243 {
225244 Pending=0 ,
@@ -228,7 +247,7 @@ class ReservationNodeNegotiator :
228247 ReceivedResponseProceedImmediate=3
229248 };
230249
231-
250+ // ============================================================================
232251 void make_request (bool only_same_map)
233252 {
234253 auto current_location = _context->location ();
0 commit comments