2021-04-15 10:44:08 +02:00
|
|
|
import numpy as np
|
2021-08-06 15:23:50 +02:00
|
|
|
from sensor_msgs.msg import CameraInfo
|
2021-05-26 14:46:12 +02:00
|
|
|
from pathlib import Path
|
2021-04-15 10:44:08 +02:00
|
|
|
import rospy
|
2021-07-22 11:05:30 +02:00
|
|
|
|
2021-09-12 11:29:58 +02:00
|
|
|
from .timer import Timer
|
2021-10-08 13:53:07 +02:00
|
|
|
from .rviz import Visualizer
|
2021-09-12 00:21:58 +02:00
|
|
|
from robot_helpers.model import KDLModel
|
2021-07-22 11:05:30 +02:00
|
|
|
from robot_helpers.ros import tf
|
|
|
|
from robot_helpers.ros.conversions import *
|
2021-08-09 15:19:17 +02:00
|
|
|
from vgn.detection import *
|
2021-07-06 14:00:04 +02:00
|
|
|
from vgn.perception import UniformTSDFVolume
|
2021-04-15 10:44:08 +02:00
|
|
|
|
|
|
|
|
2021-07-22 11:05:30 +02:00
|
|
|
class Policy:
|
2021-09-12 00:21:58 +02:00
|
|
|
def __init__(self):
|
2021-08-03 18:11:30 +02:00
|
|
|
self.load_parameters()
|
2021-09-12 00:21:58 +02:00
|
|
|
self.init_robot_model()
|
2021-08-03 18:11:30 +02:00
|
|
|
self.init_visualizer()
|
|
|
|
|
|
|
|
def load_parameters(self):
|
2021-09-03 22:39:17 +02:00
|
|
|
self.base_frame = rospy.get_param("~base_frame_id")
|
2021-09-12 16:23:10 +02:00
|
|
|
self.T_grasp_ee = Transform.from_list(rospy.get_param("~ee_grasp_offset")).inv()
|
2021-09-12 00:21:58 +02:00
|
|
|
self.cam_frame = rospy.get_param("~camera/frame_id")
|
2021-08-03 18:11:30 +02:00
|
|
|
self.task_frame = "task"
|
2021-09-11 20:49:55 +02:00
|
|
|
info_topic = rospy.get_param("~camera/info_topic")
|
2021-08-06 15:23:50 +02:00
|
|
|
msg = rospy.wait_for_message(info_topic, CameraInfo, rospy.Duration(2.0))
|
|
|
|
self.intrinsic = from_camera_info_msg(msg)
|
2021-10-11 14:31:31 +02:00
|
|
|
self.qual_thresh = rospy.get_param("vgn/qual_threshold")
|
2021-08-03 18:11:30 +02:00
|
|
|
|
2021-09-12 00:21:58 +02:00
|
|
|
def init_robot_model(self):
|
|
|
|
self.model = KDLModel.from_parameter_server(self.base_frame, self.cam_frame)
|
2021-09-12 16:23:10 +02:00
|
|
|
self.ee_model = KDLModel.from_parameter_server(self.base_frame, "panda_link8")
|
2021-09-12 00:21:58 +02:00
|
|
|
|
2021-08-03 18:11:30 +02:00
|
|
|
def init_visualizer(self):
|
2021-08-25 14:57:43 +02:00
|
|
|
self.vis = Visualizer()
|
2021-07-07 15:08:32 +02:00
|
|
|
|
2021-09-12 00:21:58 +02:00
|
|
|
def is_feasible(self, view, q_init=None):
|
2021-10-08 14:48:26 +02:00
|
|
|
if q_init is None:
|
|
|
|
q_init = [0.0, -0.79, 0.0, -2.356, 0.0, 1.57, 0.79]
|
2021-09-12 00:21:58 +02:00
|
|
|
return self.model.ik(q_init, view) is not None
|
|
|
|
|
2021-09-11 20:49:55 +02:00
|
|
|
def activate(self, bbox, view_sphere):
|
2021-08-25 18:29:10 +02:00
|
|
|
self.vis.clear()
|
2021-09-11 14:52:27 +02:00
|
|
|
|
|
|
|
self.bbox = bbox
|
2021-09-11 20:49:55 +02:00
|
|
|
self.view_sphere = view_sphere
|
2021-09-11 14:52:27 +02:00
|
|
|
self.vis.bbox(self.base_frame, self.bbox)
|
|
|
|
|
2021-09-10 23:29:15 +02:00
|
|
|
self.calibrate_task_frame()
|
2021-09-11 14:52:27 +02:00
|
|
|
|
2021-08-25 18:29:10 +02:00
|
|
|
self.tsdf = UniformTSDFVolume(0.3, 40)
|
|
|
|
self.vgn = VGN(Path(rospy.get_param("vgn/model")))
|
2021-09-11 14:52:27 +02:00
|
|
|
|
2021-08-25 21:32:47 +02:00
|
|
|
self.views = []
|
2021-08-03 18:11:30 +02:00
|
|
|
self.best_grasp = None
|
2021-09-11 20:49:55 +02:00
|
|
|
self.x_d = None
|
2021-08-25 18:29:10 +02:00
|
|
|
self.done = False
|
2021-09-12 11:29:58 +02:00
|
|
|
self.info = {}
|
2021-08-03 18:11:30 +02:00
|
|
|
|
2021-08-25 18:29:10 +02:00
|
|
|
def calibrate_task_frame(self):
|
2021-09-11 14:52:27 +02:00
|
|
|
self.T_base_task = Transform.translation(self.bbox.center - np.full(3, 0.15))
|
2021-08-25 18:29:10 +02:00
|
|
|
self.T_task_base = self.T_base_task.inv()
|
|
|
|
tf.broadcast(self.T_base_task, self.base_frame, self.task_frame)
|
2021-09-12 17:55:42 +02:00
|
|
|
rospy.sleep(1.0) # Wait for tf tree to be updated
|
2021-10-08 17:19:36 +02:00
|
|
|
self.vis.workspace(self.task_frame, 0.3)
|
2021-06-08 10:20:34 +02:00
|
|
|
|
2021-09-12 14:40:17 +02:00
|
|
|
def update(self, img, x, q):
|
2021-09-11 14:52:27 +02:00
|
|
|
raise NotImplementedError
|
2021-09-03 22:39:17 +02:00
|
|
|
|
2021-10-08 13:53:07 +02:00
|
|
|
def sort_grasps(self, grasps, qualities, q):
|
|
|
|
"""
|
|
|
|
1. Transform grasp configurations into base_frame
|
|
|
|
2. Check whether the finger tips lie within the bounding box
|
|
|
|
3. Remove grasps for which no IK solution was found
|
|
|
|
4. Sort grasps according to score_fn
|
|
|
|
"""
|
|
|
|
filtered_grasps, scores = [], []
|
|
|
|
for grasp, quality in zip(grasps, qualities):
|
2021-08-10 18:52:03 +02:00
|
|
|
pose = self.T_base_task * grasp.pose
|
2021-09-10 23:29:15 +02:00
|
|
|
R, t = pose.rotation, pose.translation
|
2021-09-12 17:11:42 +02:00
|
|
|
tip = pose.rotation.apply([0, 0, 0.05]) + pose.translation
|
2021-07-22 11:05:30 +02:00
|
|
|
if self.bbox.is_inside(tip):
|
2021-08-10 18:52:03 +02:00
|
|
|
grasp.pose = pose
|
2021-09-12 16:23:10 +02:00
|
|
|
q_grasp = self.ee_model.ik(q, pose * self.T_grasp_ee)
|
|
|
|
if q_grasp is not None:
|
2021-10-08 13:53:07 +02:00
|
|
|
filtered_grasps.append(grasp)
|
|
|
|
scores.append(self.score_fn(grasp, quality, q, q_grasp))
|
|
|
|
filtered_grasps, scores = np.asarray(filtered_grasps), np.asarray(scores)
|
|
|
|
i = np.argsort(-scores)
|
|
|
|
return filtered_grasps[i], qualities[i], scores[i]
|
2021-08-25 18:29:10 +02:00
|
|
|
|
2021-10-08 13:53:07 +02:00
|
|
|
def score_fn(self, grasp, quality, q, q_grasp):
|
2021-10-11 14:31:31 +02:00
|
|
|
return grasp.pose.translation[2]
|
2021-08-25 18:29:10 +02:00
|
|
|
|
|
|
|
|
|
|
|
class SingleViewPolicy(Policy):
|
2021-09-12 14:40:17 +02:00
|
|
|
def update(self, img, x, q):
|
2021-09-11 20:49:55 +02:00
|
|
|
linear, _ = compute_error(self.x_d, x)
|
2021-09-04 15:50:29 +02:00
|
|
|
if np.linalg.norm(linear) < 0.02:
|
2021-09-03 22:39:17 +02:00
|
|
|
self.views.append(x)
|
|
|
|
self.tsdf.integrate(img, self.intrinsic, x.inv() * self.T_base_task)
|
2021-08-25 18:29:10 +02:00
|
|
|
tsdf_grid, voxel_size = self.tsdf.get_grid(), self.tsdf.voxel_size
|
|
|
|
self.vis.scene_cloud(self.task_frame, self.tsdf.get_scene_cloud())
|
|
|
|
self.vis.map_cloud(self.task_frame, self.tsdf.get_map_cloud())
|
|
|
|
|
|
|
|
out = self.vgn.predict(tsdf_grid)
|
2021-09-10 23:29:15 +02:00
|
|
|
self.vis.quality(self.task_frame, voxel_size, out.qual, 0.5)
|
2021-08-25 18:29:10 +02:00
|
|
|
|
2021-10-11 14:31:31 +02:00
|
|
|
grasps, qualities = select_local_maxima(voxel_size, out, self.qual_thresh)
|
2021-10-11 14:51:42 +02:00
|
|
|
grasps, qualities, _ = self.sort_grasps(grasps, qualities, q)
|
2021-09-12 16:23:10 +02:00
|
|
|
|
|
|
|
if len(grasps) > 0:
|
|
|
|
self.best_grasp = grasps[0]
|
2021-10-11 14:51:42 +02:00
|
|
|
# self.vis.grasps(self.base_frame, grasps, qualities)
|
|
|
|
self.vis.grasp(self.base_frame, self.best_grasp, qualities[0])
|
2021-08-25 18:29:10 +02:00
|
|
|
|
|
|
|
self.done = True
|
2021-06-08 10:20:34 +02:00
|
|
|
|
2021-04-15 10:44:08 +02:00
|
|
|
|
2021-08-25 21:32:47 +02:00
|
|
|
class MultiViewPolicy(Policy):
|
2021-10-08 15:41:50 +02:00
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
|
|
|
self.T = rospy.get_param("policy/window_size")
|
|
|
|
|
2021-09-11 22:31:48 +02:00
|
|
|
def activate(self, bbox, view_sphere):
|
|
|
|
super().activate(bbox, view_sphere)
|
|
|
|
self.qual_hist = np.zeros((self.T,) + (40,) * 3, np.float32)
|
|
|
|
|
2021-09-12 16:23:10 +02:00
|
|
|
def integrate(self, img, x, q):
|
2021-09-03 22:39:17 +02:00
|
|
|
self.views.append(x)
|
2021-09-12 11:29:58 +02:00
|
|
|
self.vis.path(self.base_frame, self.views)
|
2021-08-25 21:32:47 +02:00
|
|
|
|
2021-09-12 11:29:58 +02:00
|
|
|
with Timer("tsdf_integration"):
|
|
|
|
self.tsdf.integrate(img, self.intrinsic, x.inv() * self.T_base_task)
|
2021-08-25 21:32:47 +02:00
|
|
|
self.vis.scene_cloud(self.task_frame, self.tsdf.get_scene_cloud())
|
|
|
|
self.vis.map_cloud(self.task_frame, self.tsdf.get_map_cloud())
|
|
|
|
|
2021-09-12 11:29:58 +02:00
|
|
|
with Timer("grasp_prediction"):
|
|
|
|
tsdf_grid, voxel_size = self.tsdf.get_grid(), self.tsdf.voxel_size
|
|
|
|
out = self.vgn.predict(tsdf_grid)
|
2021-09-13 18:29:41 +02:00
|
|
|
self.vis.quality(self.task_frame, self.tsdf.voxel_size, out.qual, 0.9)
|
2021-08-25 21:32:47 +02:00
|
|
|
|
2021-09-11 22:31:48 +02:00
|
|
|
t = (len(self.views) - 1) % self.T
|
|
|
|
self.qual_hist[t, ...] = out.qual
|
|
|
|
|
2021-09-12 11:29:58 +02:00
|
|
|
with Timer("grasp_selection"):
|
2021-10-11 14:31:31 +02:00
|
|
|
grasps, qualities = select_local_maxima(voxel_size, out, self.qual_thresh)
|
2021-10-08 13:53:07 +02:00
|
|
|
grasps, qualities, _ = self.sort_grasps(grasps, qualities, q)
|
2021-09-12 18:19:10 +02:00
|
|
|
|
2021-08-25 21:32:47 +02:00
|
|
|
if len(grasps) > 0:
|
|
|
|
self.best_grasp = grasps[0]
|
2021-10-08 13:53:07 +02:00
|
|
|
# self.vis.grasps(self.base_frame, grasps, qualities)
|
|
|
|
self.vis.grasp(self.base_frame, self.best_grasp, qualities[0])
|
2021-09-12 16:48:31 +02:00
|
|
|
else:
|
|
|
|
self.best_grasp = None
|
2021-10-08 13:53:07 +02:00
|
|
|
self.vis.clear_grasp()
|
2021-08-25 21:32:47 +02:00
|
|
|
|
|
|
|
|
2021-09-11 14:52:27 +02:00
|
|
|
def compute_error(x_d, x):
|
|
|
|
linear = x_d.translation - x.translation
|
|
|
|
angular = (x_d.rotation * x.rotation.inv()).as_rotvec()
|
|
|
|
return linear, angular
|
|
|
|
|
|
|
|
|
2021-07-07 17:46:11 +02:00
|
|
|
registry = {}
|
2021-06-08 10:20:34 +02:00
|
|
|
|
2021-07-07 17:46:11 +02:00
|
|
|
|
|
|
|
def register(id, cls):
|
|
|
|
global registry
|
|
|
|
registry[id] = cls
|
|
|
|
|
|
|
|
|
2021-08-03 18:11:30 +02:00
|
|
|
def make(id, *args, **kwargs):
|
2021-07-07 17:46:11 +02:00
|
|
|
if id in registry:
|
2021-08-03 18:11:30 +02:00
|
|
|
return registry[id](*args, **kwargs)
|
2021-07-07 17:46:11 +02:00
|
|
|
else:
|
|
|
|
raise ValueError("{} policy does not exist.".format(id))
|