-
Notifications
You must be signed in to change notification settings - Fork 2
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
Implement locking service #15
Open
gunarp
wants to merge
6
commits into
main
Choose a base branch
from
ros_service_impl
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
cf77dab
first pass at implementing lock server
gunarp 3a4aa78
clean up folder organization
gunarp b68b0cf
debug simple test case and provide sample code
gunarp 86678a0
correct exit function
gunarp 4fe97c1
add newlines to most python files
gunarp 4fa2276
correct arrow originator
gunarp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
@startuml Locking Service Sequence Diagram | ||
title Locking Service Sequence Diagram | ||
participant Auto_Script as auto | ||
participant UI as ui | ||
participant Lock_Service as ls | ||
|
||
ls -> ui : Sends enable message over topic | ||
|
||
... | ||
|
||
auto -> ls : Sends a lock request | ||
ls -> ui : Send disable message over topic | ||
ls --> auto : Send lock confirmation | ||
|
||
... | ||
|
||
Second_Auto_Script -> ls : Sends lock request | ||
return Send failure message | ||
Second_Auto_Script -> Second_Auto_Script : Periodically ping\nto try and get lock | ||
|
||
... | ||
|
||
auto -> ls : Send lock release message | ||
return : Acknowledge lock release (no need to wait) | ||
auto -> ui : Send enable message over topic | ||
|
||
@enduml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#!/usr/bin/env python3 | ||
|
||
from locks.lock_service_client import LockClient | ||
import time | ||
import rospy | ||
|
||
if __name__ == '__main__': | ||
rospy.init_node('test_lock_client') | ||
rospy.on_shutdown(lambda: rospy.loginfo("shutting down test client")) | ||
|
||
# Test case 1 : manage lock using the with statement | ||
with LockClient('test_client') as lc: | ||
time.sleep(0.25) | ||
rospy.loginfo("lock acquired automatically") | ||
rospy.loginfo("lock released automatically") | ||
|
||
|
||
# Test case 2 : manage lock manually | ||
lc = LockClient('test_client') | ||
lc.lock() | ||
rospy.loginfo("lock acquired manually") | ||
lc.unlock() | ||
rospy.loginfo("lock released manually") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#!/usr/bin/env python3 | ||
|
||
from locks.lock_service_server import LockServer | ||
|
||
if __name__ == '__main__': | ||
ls = LockServer() | ||
ls.init_server() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
#!/usr/bin/env python3 | ||
|
||
from robot_module.srv import acquire_lock, release_lock | ||
from std_msgs.msg import String | ||
import rospy | ||
import time | ||
|
||
class LockClient: | ||
def __init__(self, name): | ||
self.name = name | ||
|
||
|
||
def __enter__(self): | ||
acquire_attempts = 0 | ||
lock_acquired = self.lock() | ||
|
||
# use exponential backoff to try again without flooding network | ||
while not lock_acquired: | ||
rospy.loginfo("Couldn't acquire the lock, trying again") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider changing to a debug message instead, this will pop up whenever a node is waiting on the lock every once in a while. |
||
time.sleep(1 * (2**acquire_attempts)) | ||
acquire_attempts += 1 | ||
|
||
lock_acquired = self.lock() | ||
|
||
|
||
def __exit__(self, exc_type, exc_value, traceback): | ||
self.unlock() | ||
|
||
|
||
def lock(self) -> bool: | ||
rospy.wait_for_service('acquire_service') | ||
try: | ||
acquire_lock_fn = rospy.ServiceProxy('acquire_service', acquire_lock) | ||
lock_server_response = acquire_lock_fn(self.name) | ||
return lock_server_response.result | ||
except rospy.ServiceException as e: | ||
rospy.logerr("couldn't access acquire_service service") | ||
return False | ||
|
||
|
||
def unlock(self): | ||
rospy.wait_for_service('release_service') | ||
try: | ||
release_lock_fn = rospy.ServiceProxy('release_service', release_lock) | ||
release_lock_fn(self.name) | ||
except rospy.ServiceException as e: | ||
rospy.logerr("couldn't access release_service service... something's broken, destroy everything and try again") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
#!/usr/bin/env python3 | ||
|
||
from threading import Lock | ||
from robot_module.srv import acquire_lock, release_lock | ||
from std_msgs.msg import String, Bool | ||
import rospy | ||
|
||
# Consider tossing this into a command line parameter for on-the-fly retargeting | ||
ui_enablement_topic = '/ui/enablement' | ||
|
||
class LockServer: | ||
def __init__(self): | ||
self.lock_holder = None | ||
self.ui_publisher = None | ||
self.mutex = Lock() | ||
|
||
|
||
def handle_acquire_request(self, req): | ||
with self.mutex: | ||
req_name = req.requester | ||
|
||
# If no autonomous script is holding on, preempt our UI | ||
if self.lock_holder is None: | ||
self.lock_holder = req_name | ||
self.ui_publisher.publish(False) | ||
return True | ||
|
||
# Be forgiving and allow a single caller to acquire a lock more than once | ||
return self.lock_holder == req_name | ||
|
||
|
||
def handle_release_request(self, req): | ||
with self.mutex: | ||
req_name = req.requester | ||
|
||
# If the client holding the lock is releasing, give control back to UI | ||
if self.lock_holder is not None and req_name == self.lock_holder: | ||
self.lock_holder = None | ||
self.ui_publisher.publish(True) | ||
return True | ||
|
||
return False | ||
|
||
|
||
def init_server(self): | ||
rospy.init_node('lock_server') | ||
rospy.on_shutdown(lambda : rospy.loginfo("Shutting down Lock Server")) | ||
|
||
rospy.loginfo("Starting Lock Server") | ||
self.ui_publisher = rospy.Publisher(ui_enablement_topic, Bool, queue_size=1) | ||
|
||
# services can't take in extra parameters like a subscriber can, | ||
# so we have to use this funny workaround | ||
# https://answers.ros.org/question/247540/pass-parameters-to-a-service-handler-in-rospy/ | ||
acquire_service = rospy.Service('acquire_service', acquire_lock, lambda msg: self.handle_acquire_request(msg)) | ||
release_service = rospy.Service('release_service', release_lock, lambda msg: self.handle_release_request(msg)) | ||
|
||
rospy.spin() |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
#!/usr/bin/env python3 | ||
|
||
import rospy | ||
from .subscriber import ServerSub | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
string requester | ||
--- | ||
bool result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
string requester | ||
--- | ||
bool result |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PUML is a diagram generator we use at Microsoft, it's free and is a nice way to create pictures from text.
You can generate the diagram by copying this into the bottom of the plant uml website or downloading a local viewer (there's a vs code extension called PlantUml by Jebbs that has some instructions on that)
Also here's a link to the image