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

Named joint states helper #71

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

henrygerardmoore
Copy link

Resolves #65

Before merging this, I want to tackle #56 (since, as mentioned, named states should be able to pertain to a subset of joints)

Just making this PR now to get it out there and see if there is feedback about the preliminary state of this.

I will also be adding some unit tests before marking this PR ready for review.

self.collision_data = self.collision_model.createData()
self.options = options
self.configuration_dict = {}

Copy link
Author

Choose a reason for hiding this comment

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

I should also override __delitem__, I think.

Copy link
Owner

Choose a reason for hiding this comment

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

Definitely, removing named configurations is well within reason

Copy link
Owner

@sea-bass sea-bass left a comment

Choose a reason for hiding this comment

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

Thanks for sharing this draft, and for breathing some life into this project. Left a few comments, and looking forward to the other stuff you're working on as well!


# Initialize
named_joints_configuration = NamedJointConfigurations(model, collision_model)
named_joints_configuration["home"] = q
Copy link
Owner

Choose a reason for hiding this comment

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

it would be nice to save the "home" pose in the actual src/pyroboplan/models/two_dof.py, so you could retrieve an initial set of joint configurations and add to them.

while True:
# get user input
user_input = input(
"Press 'Enter' to show another random state, enter a new name to save the current state as a named joint configuration, enter a used name to visualize that saved state, type 'h' or 'help' to see the existing saved joint configurations, or ctrl-c to quit.\n"
Copy link
Owner

@sea-bass sea-bass Sep 14, 2024

Choose a reason for hiding this comment

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

highly recommend splitting up this into multiple lines -- it was a bit hard to read.

Comment on lines +44 to +53
if user_input:
# if the input isn't empty
user_input = user_input.lower()
if user_input == "h" or user_input == "help":
print("Stored states:")
print(named_joints_configuration)
if user_input in named_joints_configuration:
named_joints_configuration.visualize_state(visualizer, user_input)
else:
named_joints_configuration[user_input] = q
Copy link
Owner

Choose a reason for hiding this comment

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

You probably also want to add confirmation that a new named state was added / the robot is going to an existing state.

Comment on lines +48 to +49
print("Stored states:")
print(named_joints_configuration)
Copy link
Owner

Choose a reason for hiding this comment

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

Might be good to also show their numeric values. Perhaps this can be some kind of print() or show() method of the NamedJointConfigurations class itself?

bool
True if the configuration q is a possible configuration of the given model
"""
return len(q) == model.nq and check_within_limits(model, q)
Copy link
Owner

Choose a reason for hiding this comment

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

nit: put parentheses around these conditions

@@ -0,0 +1,98 @@
from pyroboplan.core.utils import (
Copy link
Owner

Choose a reason for hiding this comment

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

not sure if visualization is the right module for this, it's probably good under src/pyroboplan/core as a new file there.

Also the name set_get_joints.py maybe should be replaced with named_joint_configurations.py or something like that?

Comment on lines +60 to +61
if not check_valid_pose(self.model, state):
if not check_within_limits(self.model, state):
Copy link
Owner

Choose a reason for hiding this comment

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

I think you may want to reconsider this function... since check_valid_pose already calls check_within_limits internally, that seems redundant. Is it worth maybe just removing that function and doing two separate (non-nested) checks for length and then limits?

Comment on lines +68 to +69
if not self.options.allow_collisions:
if check_collisions_at_state(self.model, self.collision_model, state):
Copy link
Owner

Choose a reason for hiding this comment

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

you can just and these 2 instead of more nesting -- but this is exactly the "check and early return" pattern you should use for the previous checks too

Comment on lines +75 to +98
def __getitem__(self, key):
"""
Get the configuration stored at key
"""
# just return the dict at the key. Errors from accessing the dict incorrectly (invalid key type, no entry) should be sufficiently explanatory
return self.configuration_dict[key]

def __contains__(self, key):
"""
See whether this object contains a configuration at key
"""
return key in self.configuration_dict

def __str__(self):
"""
Print the states in this object
"""
return str([k for k in self.configuration_dict])

def visualize_state(self, visualizer, name):
"""
Visualize the joint state stored in key `name`
"""
visualizer.display(self[name])
Copy link
Owner

Choose a reason for hiding this comment

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

fill in these docstrings

Comment on lines +88 to +92
def __str__(self):
"""
Print the states in this object
"""
return str([k for k in self.configuration_dict])
Copy link
Owner

Choose a reason for hiding this comment

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

How does __str__() differ from __repr__()? Genuine question, just that I've always done the latter when I want my class to be printable.

Copy link
Author

Choose a reason for hiding this comment

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

I also didn't know this, even __str__ was new to me 😄
But I looked it up and it seems that (according to this and a couple other places) that __repr__ is supposed to be the be-all end-all representation for a programmer or more advanced user, while the __str__ is supposed to be readable. So maybe I should have the __str__ method give just the joint states that are there by name and __repr__ should also include the actual states like you were suggesting?

@henrygerardmoore
Copy link
Author

@sea-bass thanks for the feedback! Lots of good suggestions, and I'll apply these when I come back to this after I get to #56. I have some WIP on that now and I'm gonna do some more work this weekend, so hopefully next weekend I'll have a PR for that one

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.

Support setting / getting named joint states
2 participants