Skip to content

Commit 6735e78

Browse files
committed
updated main, penv, robot to work with tree refactor
1 parent d835b27 commit 6735e78

File tree

3 files changed

+48
-44
lines changed

3 files changed

+48
-44
lines changed

main.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
def main():
1717
pbutils = PyBUtils(renders=True)
18-
robot_start_orientation = Rotation.from_euler('xyz', [0, 0,180], degrees=True).as_quat()
18+
robot_start_orientation = Rotation.from_euler("xyz", [0, 0, 180], degrees=True).as_quat()
1919
robot = Robot(pbclient=pbutils.pbclient, position=[0, 1, 0], orientation=robot_start_orientation)
2020

2121
penv = PruningEnv(
@@ -42,7 +42,7 @@ def main():
4242
save_tree_urdf=False,
4343
# randomize_pose=True
4444
)
45-
penv.activate_tree(tree_id_str=tree_name)
45+
penv.activate_tree_by_id_str(tree_id_str=tree_name)
4646

4747
# # Run the sim a little just to get the environment properly loaded.
4848
for i in range(100):

pybullet_tree_sim/pruning_environment.py

Lines changed: 31 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ def __init__(
6969
# make_trees: bool = False,
7070
name: str = "PruningEnv",
7171
# num_trees: int | None = None,
72-
renders: bool = False,
72+
# renders: bool = False,
7373
verbose: bool = True,
7474
) -> None:
7575
"""Initialize the Pruning Environment
@@ -83,7 +83,7 @@ def __init__(
8383

8484
# Pybullet GUI variables
8585
self.render_mode = "rgb_array"
86-
self.renders = renders
86+
# self.renders = renders
8787
# self.eval = evaluate
8888

8989
# Gym variables
@@ -143,61 +143,54 @@ def load_tree( # TODO: Clean up Tree init vs create_tree, probably not needed.
143143
randomize_pose=randomize_pose,
144144
)
145145

146-
tree_id_str = f"{tree_namespace}{tree_type}_tree{tree_id}"
147-
# urdf_path = os.path.join(URDF_PATH, "trees", tree_type, "generated", f"{tree.id_str}.urdf")
148-
149146
# Add tree to dict of trees
150147
self.trees[tree.id_str] = tree
151-
return tree_id_str
148+
return tree.id_str
149+
150+
def get_tree_from_id_str(self, tree_id_str: str) -> Tree:
151+
try:
152+
tree = self.trees[tree_id_str]
153+
except KeyError as e:
154+
raise TreeException(f"{e}: Tree with ID {tree_id_str} not found")
155+
return tree
152156

153157
def activate_tree(
154158
self,
155-
tree: Tree | None = None,
156-
tree_id_str: str | None = None,
159+
tree: Tree,
157160
include_support_posts: bool = True,
158161
) -> None:
159162
"""Activate a tree by object or by tree_id_str. Can include support posts. Must provide either a Tree or tree_id_str.
160163
@param tree (Tree/None): Tree object to be activated into the pruning environment.
161164
@param tree_id_str (str/None): String including the identification characteristics of the tree.
162165
@return None
163166
"""
164-
165-
if tree is None and tree_id_str is None:
166-
raise TreeException("Parameters 'tree' and 'tree_id_str' cannot both be None")
167-
168-
if tree is None and tree_id_str is not None:
169-
try:
170-
tree = self.trees[tree_id_str]
171-
except KeyError as e:
172-
raise TreeException(f"{e}: Tree with ID {tree_id_str} not found")
173-
174-
if tree is not None:
167+
if tree:
175168
if self.verbose:
176169
log.info("Activating tree")
177-
tree.pyb_tree_id = self.pbutils.pbclient.loadURDF(tree.urdf_path, useFixedBase=True)
178-
log.info(f"Tree {tree.id_str} activated with PyBID {tree.pyb_tree_id}")
170+
tree.pyb_id = self.pbutils.pbclient.loadURDF(tree.urdf_path, useFixedBase=True)
171+
log.info(f"Tree {tree.id_str} activated with PyBID {tree.pyb_id}")
179172

180173
if include_support_posts:
181174
self.activate_support_posts(associated_tree=tree)
182175
return
183176

184-
def deactivate_tree(self, tree: Tree | None = None, tree_id_str: str | None = None) -> None:
185-
"""Deactivate a tree by object or by tree_id_str"""
186-
if tree is None and tree_id_str is None:
187-
raise TreeException("Parameters 'tree' and 'tree_id_str' cannot both be None")
188-
189-
if tree is None and tree_id_str is not None:
190-
try:
191-
tree = self.trees[tree_id_str]
192-
except KeyError as e:
193-
raise TreeException(f"{e}: Tree with ID {tree_id_str} not found")
194-
195-
if tree is not None:
196-
try:
197-
self.pbutils.pbclient.removeBody(tree.pyb_tree_id)
198-
log.info(f"Tree {tree.id_str} with PyBID {tree.pyb_tree_id} deactivated")
199-
except Exception as e:
200-
log.error(f"Error deactivating tree: {e}")
177+
def activate_tree_by_id_str(self, tree_id_str: str, include_support_posts: bool = True) -> None:
178+
tree = self.get_tree_from_id_str(tree_id_str=tree_id_str)
179+
self.activate_tree(tree=tree, include_support_posts=include_support_posts)
180+
return
181+
182+
def deactivate_tree(self, tree: Tree) -> None:
183+
"""Deactivate a tree object"""
184+
try:
185+
self.pbutils.pbclient.removeBody(tree.pyb_id)
186+
log.info(f"Tree {tree.id_str} with PyBID {tree.pyb_id} deactivated")
187+
except Exception as e:
188+
log.error(f"Error deactivating tree: {e}")
189+
return
190+
191+
def deactivate_tree_by_id_str(self, tree_id_str: str) -> None:
192+
tree = self.get_tree_from_id_str(tree_id_str=tree_id_str)
193+
self.deactivate_tree(tree=tree)
201194
return
202195

203196
def activate_support_posts(

pybullet_tree_sim/robot.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def __init__(
4444
self.position = position
4545
self.orientation = orientation
4646
self.randomize_pose = randomize_pose # TODO: This isn't set up anymore... fix
47-
self.init_joint_angles = (
47+
self.init_joint_angles = ( # TODO: dynamically assign from defaults (i.e. if linear-slider is loaded in)
4848
(
4949
-np.pi / 2 + np.pi / 4,
5050
-np.pi * 2 / 3,
@@ -172,6 +172,7 @@ def _setup_robot(self):
172172
def _get_joints(self) -> dict:
173173
"""Return a dict of joint information for the robot"""
174174
joints = {}
175+
175176
for i in range(self.num_joints):
176177
info = self.pbclient.getJointInfo(self.robot, i)
177178
joint_name = info[1].decode("utf-8")
@@ -187,16 +188,25 @@ def _get_joints(self) -> dict:
187188
}
188189
}
189190
)
191+
log.warn(joints)
190192
return joints
191193

192-
def _assign_control_joints(self, joints: dict) -> list:
194+
def _assign_control_joints(self, joints: dict) -> tuple[list]:
193195
"""Get list of controllable joints from the joint dict by joint type"""
194196
control_joints = []
195197
control_joint_idxs = []
198+
self.control_joint_lower_limits = []
199+
self.control_joint_upper_limits = []
200+
self.control_joint_ranges = []
196201
for joint, joint_info in joints.items():
197202
if joint_info["type"] == 0: # TODO: Check if this works for prismatic joints or just revolute
198203
control_joints.append(joint)
199204
control_joint_idxs.append(joint_info["id"])
205+
206+
# self.joint_upper_limits,
207+
# self.joint_lower_limits,
208+
# self.joint_ranges, # ,
209+
200210
return control_joints, control_joint_idxs
201211

202212
def _get_links(self) -> dict:
@@ -323,6 +333,7 @@ def reset_robot(self, joint_angles: tuple = None) -> None:
323333
else joint_angles
324334
)
325335
self.set_joint_angles_no_collision(self.init_joint_angles)
336+
# self.pbclient.resetJointState() # TODO Fill out
326337
return
327338

328339
def remove_robot(self):
@@ -565,7 +576,7 @@ def check_collisions(self, collision_objects) -> Tuple[bool, dict]:
565576
collisons_self = self.pbclient.getContactPoints(bodyA=self.robot, bodyB=self.robot)
566577
collisions_unacceptable = collisons_self
567578
for i in range(len(collisions_unacceptable)):
568-
if collisions_unacceptable[i][-6] < -0.001:
579+
if collisions_unacceptable[i][-6] < -0.00:
569580
collision_info["collisions_unacceptable"] = True
570581
break
571582
if self.verbose > 1:
@@ -578,7 +589,7 @@ def check_collisions(self, collision_objects) -> Tuple[bool, dict]:
578589

579590
def check_success_collision(self, body_b) -> bool:
580591
"""Check if there are any collisions between the robot and the environment
581-
Returns: Boolw
592+
Returns: Bool
582593
"""
583594
collisions_success = self.pbclient.getContactPoints(
584595
bodyA=self.robot, bodyB=body_b, linkIndexA=self.success_link_index

0 commit comments

Comments
 (0)