Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ActionServer #270

Merged
merged 18 commits into from
Feb 25, 2019
Merged

Add ActionServer #270

merged 18 commits into from
Feb 25, 2019

Conversation

jacobperron
Copy link
Member

Resolves #257

Similar to ActionClient, ActionServer is implemented as a Waitable and the user interacts with goals via ServerGoalHandle objects.

This is a work in progress. Still need to implement the "get result" logic, add documentation, and some more tests. I separated some changes to _rclpy.c into #269 to avoid adding more to this large change. Since this PR is based on that branch, #269 should be resolved first.

@jacobperron jacobperron added the in progress Actively being worked on (Kanban column) label Feb 6, 2019
@jacobperron jacobperron self-assigned this Feb 6, 2019
@jacobperron
Copy link
Member Author

Rooting out any cross-platform issues:

  • Linux Build Status
  • Linux-aarch64 Build Status
  • macOS Build Status
  • Windows Build Status

@jacobperron
Copy link
Member Author

Since this is a large PR, I think it can start being reviewed while I try to resolve two outstanding issues:

  1. Feedback messages aren't making it to action clients (but the mock action client used in the tests appears to work).
  2. Tests involving a MultiThreadedExecutor timeout (Tests with MultiThreadedExecutor time out on some CI instances #268)

@jacobperron jacobperron added in review Waiting for review (Kanban column) and removed in progress Actively being worked on (Kanban column) labels Feb 7, 2019
@jacobperron
Copy link
Member Author

  1. Feedback messages aren't making it to action clients (but the mock action client used in the tests appears to work).

This is because I forgot to set the goal ID when publishing feedback messages.

@jacobperron
Copy link
Member Author

@sloretz Please take a look at c2026b5. It is an attempt to fix a scenario where 'done callbacks' for futures were not being called, specifically when result responses are ready for Action Servers. I'm not sure if this is the right thing to do.

@sloretz
Copy link
Contributor

sloretz commented Feb 11, 2019

@sloretz Please take a look at c2026b5. It is an attempt to fix a scenario where 'done callbacks' for futures were not being called,

@jacobperron Looking at it now. Mind moving it to a separate PR with a test? (Edit: oops, it already has a test)

@jacobperron
Copy link
Member Author

Looking at it now. Mind moving it to a separate PR with a test?

Done #272

rclpy/test/action/test_server.py Outdated Show resolved Hide resolved

goal_handle._update_state(GoalEvent.EXECUTE)
# Call user execute callback
execute_result = await await_or_execute(self._execute_callback, goal_handle)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like it is calling the execute_callback even if the user said ACCEPT_AND_DEFER.

goal_handle._update_state(GoalEvent.EXECUTE)
# Call user execute callback
execute_result = await await_or_execute(self._execute_callback, goal_handle)
# If user did not trigger a terminal state, assume success
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If an execute callback returns without reaching a terminal state I would assume aborted.


self._node.get_logger().debug('New goal accepted: {0}'.format(goal_uuid.uuid))

goal_handle._update_state(GoalEvent.EXECUTE)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like it is setting the goal handle to EXECUTE even if the user chose ACCEPT_AND_DEFER. In rclcpp instead of an execute_callback there is handle_accepted. If the user says ACCEPT_AND_DEFER then it arrives at that callback in state ACCEPTED.

If an execute_callback is provided then it should be separate from handle_accepted. Maybe add the execute_callback as a task to the executor if handle_accepted returns and the goal handle is not at a terminal state?

Copy link
Member Author

@jacobperron jacobperron Feb 12, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I... deferred implementing the defer logic. I meant to make a note of it.

Originally I was thinking that we could provide an API via the GoalHandle to execute a deferred goal. E.g. def execute(callback=None) where the callback will optionally override a previous established callback. This function will schedule the callback (if there is one) with the executor.

I like the idea of the execute_callback as a way to let the action server handle scheduling execution with the executor. I think this puts less burden on the user; not having to manage threads. Do you think this is something rclcpp_action is missing?

Reflecting on the rclcpp action server, I think it makes sense to take a handle_accepted callback in rclpy's action server as well. The handle_accepted makes a nice venue for providing the user with a reference to the goal handle.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the idea of the execute_callback as a way to let the action server handle scheduling execution with the executor. I think this puts less burden on the user; not having to manage threads. Do you think this is something rclcpp_action is missing?

I agree an execute_callback is convenient. I've assumed in rclcpp_action an execute callback would be provided by a class like ROS 1's SimpleActionServer. It should only be invoked after a goal transitions to EXECUTING though. Right now I don't see a way to make a python action server queue goals instead of executing them all in parallel.

Looking back, I'm not sure ACCEPT_AND_EXECUTE and ACCEPT_AND_DEFER are necessary. The same thing could be accomplished by whether or not handle_goal changed the state to EXECUTING .

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking back, I'm not sure ACCEPT_AND_EXECUTE and ACCEPT_AND_DEFER are necessary.

Agreed.

I've made an update in 9a15695 that adds a handle_accepted_callback in which the user can optionally call goal_handle.execute(), or hold onto the goal handle to execute later (ie. defer).

Copy link
Member Author

@jacobperron jacobperron Feb 14, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right now I don't see a way to make a python action server queue goals instead of executing them all in parallel

With the updated examples, I believe an action server that queues goals could be implemented in Python by adapting the "single goal" example. Instead of aborting newly received goals, it could queue them instead.

If the user doesn't mind ignoring cancel requests, then a SingleThreadedExecutor could be used to create a queuing behavior.

@jacobperron
Copy link
Member Author

Rebased on #275, which is required for creating tasks associated with user callbacks with the executor.
The Waitable.add_future mechanism is not as reliable since it needs something in the wait set to be ready for rcl_wait to wake up.

I'm considering replacing the add_future API introduced in #262 with direct calls to Executor.create_task().

Copy link
Contributor

@sloretz sloretz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All minor comments, I didn't test manually but the code LGTM

rclpy/rclpy/action/server.py Outdated Show resolved Hide resolved
rclpy/rclpy/action/server.py Outdated Show resolved Hide resolved
rclpy/rclpy/action/server.py Outdated Show resolved Hide resolved
rclpy/rclpy/action/server.py Outdated Show resolved Hide resolved
rclpy/rclpy/action/server.py Outdated Show resolved Hide resolved
rclpy/rclpy/action/server.py Outdated Show resolved Hide resolved
rclpy/src/rclpy/_rclpy_action.c Show resolved Hide resolved
@jacobperron
Copy link
Member Author

@sloretz Thank you for the reviews 🙇‍♂️

In addition to addressing your latest comments, I've resolved an issue for the scenario where a goal is deferred, canceled, and then executed in 093d52d.

I've also pushed an example of a goal being deferred in ros2/examples#222.

# It's possible that there has been a request to cancel the goal prior to executing.
# In this case we want to avoid the illegal state transition to EXECUTING
# but still call the users execute callback to let them handle canceling the goal.
if not self.is_cancel_requested:
Copy link
Contributor

@sloretz sloretz Feb 14, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does is_cancel_requested mean the cancellation was accepted? If this only means it was requested then what happens if: a goal to be requested to be canceled, the execute callback is called but the state transition to EXECUTE is skipped, then the cancellation request rejected, and finally the execute callback tries to set a terminal state? It looks like it might try to transition from ACCEPTED -> SUCCEEDED/ABORTED which isn't an allowed transition.

Copy link
Member Author

@jacobperron jacobperron Feb 14, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does is_cancel_requested mean the cancellation was accepted?

Yes. The cancellation request was accepted and the goal is in the CANCELING state.

def is_cancel_requested(self):
return GoalStatus.STATUS_CANCELING == self.status

@jacobperron
Copy link
Member Author

  • Linux Build Status
  • Linux-aarch64 Build Status
  • macOS Build Status
  • Windows Build Status

* Add Action server functions to extension module
    * Separated service related macros into separate request and response calls
* Add server goal handle functions to extension module
* Update Action extension module to use conversion functions
* Add partial implementation of Python ActionServer
    * Handles goal and cancel requests, responds, and calls user-defined functions for executing goals.

Signed-off-by: Jacob Perron <jacob@openrobotics.org>
And some linting.

Signed-off-by: Jacob Perron <jacob@openrobotics.org>
Pass an optional 'result_timeout' argument to the constructor specififying how long after a goal is completed to keep a reference to the result.

Signed-off-by: Jacob Perron <jacob@openrobotics.org>
Signed-off-by: Jacob Perron <jacob@openrobotics.org>
Signed-off-by: Jacob Perron <jacob@openrobotics.org>
Signed-off-by: Jacob Perron <jacob@openrobotics.org>
Signed-off-by: Jacob Perron <jacob@openrobotics.org>
Signed-off-by: Jacob Perron <jacob@openrobotics.org>
The goal status array is published whenever a goal state is updated.
Published feedback messages should have the goal ID field populated, otherwise the message will be ignored at the rcl layer.

Signed-off-by: Jacob Perron <jacob@openrobotics.org>
Signed-off-by: Jacob Perron <jacob@openrobotics.org>
Upon accepting a goal, the provided `handle_accepted_callback` is called with a reference to the goal handle.
The user can then decide to execute the goal or defer.
If `handle_accepted_callback` is not provided, then the default behavior is to execute the goal handle immediately.

Signed-off-by: Jacob Perron <jacob@openrobotics.org>
Signed-off-by: Jacob Perron <jacob@openrobotics.org>
Signed-off-by: Jacob Perron <jacob@openrobotics.org>
* Goal should first transition to CANCELING before CANCELED
* Handle case where goal skips the EXECUTING state if deferred (accepted, but not executed) and then canceled

Signed-off-by: Jacob Perron <jacob@openrobotics.org>
Signed-off-by: Jacob Perron <jacob@openrobotics.org>
@jacobperron
Copy link
Member Author

Rebased on master.

@jacobperron jacobperron mentioned this pull request Feb 15, 2019
10 tasks
@jacobperron
Copy link
Member Author

  • Linux Build Status
  • Linux-aarch64 Build Status
  • macOS Build Status
  • Windows Build Status

Signed-off-by: Jacob Perron <jacob@openrobotics.org>
This resolves test errors that were present in Windows builds.

Signed-off-by: Jacob Perron <jacob@openrobotics.org>
@jacobperron
Copy link
Member Author

Hopefully, fixed the Windows build 🤞

  • Linux Build Status
  • Linux-aarch64 Build Status
  • macOS Build Status
  • Windows Build Status

@jacobperron
Copy link
Member Author

@sloretz Test failures are unrelated.
This is ready for another round of review :)

Signed-off-by: Jacob Perron <jacob@openrobotics.org>
@jacobperron jacobperron merged commit be4bc88 into master Feb 25, 2019
@jacobperron jacobperron deleted the rclpy_action_server branch February 25, 2019 17:42
@jacobperron jacobperron removed the in review Waiting for review (Kanban column) label Feb 25, 2019
jacobperron added a commit that referenced this pull request Mar 7, 2019
* Add Action server functions to extension module
    * Separated service related macros into separate request and response calls
* Add server goal handle functions to extension module
* Update Action extension module to use conversion functions
* Add implementation of Python ActionServer
    * Handles goal and cancel requests, responds, and calls user-defined functions for executing goals.
    * Handle result requests
    * Handle expired goals
    * Publish goal status array and feedback
* Add `handle_accepted_callback` to ActionServer

Upon accepting a goal, the provided `handle_accepted_callback` is called with a reference to the goal handle.
The user can then decide to execute the goal or defer.
If `handle_accepted_callback` is not provided, then the default behavior is to execute the goal handle immediately.

Signed-off-by: Jacob Perron <jacob@openrobotics.org>
nuclearsandwich pushed a commit that referenced this pull request Mar 10, 2019
* Add Action Client (#262)

* Add rclpy_action module

With functions for creating and destroying action clients.

* Implement action client

* Move common conversion function and typedefs to shared header file (impl/common.h)

* Add tests using mock action server

* Add action module for aggregating action related submodules

* Extend Waitable API so executors are aware of Futures

* Move check_for_type_support() to its own module

Signed-off-by: Jacob Perron <jacob@openrobotics.org>

* Fix Executor not executing tasks if there are no ready entities in the wait set (#272)

If a task is created, then trigger the Executors guard condition. This will wake any blocking call to `rcl_wait()`.
In this scenario, no work is yielded, so we also have to move the check for 'in-progress' tasks into the wait loop.

Added a unit test to reproduce the issue.

This resolves an issue in some cases where the result response from the action server is never processed, therefore never reaching the action client.

Signed-off-by: Jacob Perron <jacob@openrobotics.org>

* Fix Node's reference to executor (#275)

Previously, if a `Node` was added to an `Executor` using the executors `add_node` method, then nodes `executor` property would return `None`.

Signed-off-by: Jacob Perron <jacob@openrobotics.org>

* Abstract type conversions into functions (#269)

* Abstract type conversions into functions

This helps with readability and maintainability.
Also eliminated use of assertions during conversions, defering to exceptions.

* Move common C functions to a shared library 'rclpy_common'

Signed-off-by: Jacob Perron <jacob@openrobotics.org>

* Add ActionServer (#270)

* Add Action server functions to extension module
    * Separated service related macros into separate request and response calls
* Add server goal handle functions to extension module
* Update Action extension module to use conversion functions
* Add implementation of Python ActionServer
    * Handles goal and cancel requests, responds, and calls user-defined functions for executing goals.
    * Handle result requests
    * Handle expired goals
    * Publish goal status array and feedback
* Add `handle_accepted_callback` to ActionServer

Upon accepting a goal, the provided `handle_accepted_callback` is called with a reference to the goal handle.
The user can then decide to execute the goal or defer.
If `handle_accepted_callback` is not provided, then the default behavior is to execute the goal handle immediately.

Signed-off-by: Jacob Perron <jacob@openrobotics.org>

* Enable test using MultiThreadedExecutor (#280)

Signed-off-by: Jacob Perron <jacob@openrobotics.org>

* Guard against failed take when taking action messages (#281)

Some middlewares (e.g. Connext and OpenSplice) send invalid messages to indicate an instance has been disposed which results in a 'failed take'.

Signed-off-by: Jacob Perron <jacob@openrobotics.org>
@dirk-thomas dirk-thomas mentioned this pull request Mar 21, 2019
20 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Action implementation
2 participants