nbv_sim/active_grasp/baselines.py

65 lines
2.0 KiB
Python
Raw Normal View History

2021-07-07 17:46:11 +02:00
import numpy as np
2021-08-25 21:32:47 +02:00
import rospy
import scipy.interpolate
2021-07-07 17:46:11 +02:00
2021-08-25 21:32:47 +02:00
from .policy import SingleViewPolicy, MultiViewPolicy
2021-08-25 18:29:10 +02:00
from vgn.utils import look_at
2021-07-07 17:46:11 +02:00
2021-08-25 18:29:10 +02:00
class InitialView(SingleViewPolicy):
2021-09-03 22:39:17 +02:00
def update(self, img, pose):
self.x_d = pose
super().update(img, pose)
2021-07-07 17:46:11 +02:00
2021-08-25 21:32:47 +02:00
class TopView(SingleViewPolicy):
2021-07-07 17:46:11 +02:00
def activate(self, bbox):
super().activate(bbox)
2021-09-03 22:39:17 +02:00
eye = np.r_[self.center[:2], self.center[2] + self.min_z_dist]
2021-07-07 17:46:11 +02:00
up = np.r_[1.0, 0.0, 0.0]
2021-09-03 22:39:17 +02:00
self.x_d = look_at(eye, self.center, up)
2021-07-07 17:46:11 +02:00
2021-07-08 09:36:51 +02:00
2021-08-25 21:32:47 +02:00
class TopTrajectory(MultiViewPolicy):
2021-07-08 09:36:51 +02:00
def activate(self, bbox):
super().activate(bbox)
2021-09-03 22:39:17 +02:00
eye = np.r_[self.center[:2], self.center[2] + self.min_z_dist]
2021-07-08 09:36:51 +02:00
up = np.r_[1.0, 0.0, 0.0]
2021-09-03 22:39:17 +02:00
self.x_d = look_at(eye, self.center, up)
2021-08-25 21:32:47 +02:00
2021-09-03 22:39:17 +02:00
def update(self, img, x):
self.integrate(img, x)
linear, angular = self.compute_error(self.x_d, x)
2021-09-04 15:50:29 +02:00
if np.linalg.norm(linear) < 0.02:
2021-08-25 21:32:47 +02:00
self.done = True
2021-09-04 15:50:29 +02:00
return np.zeros(6)
2021-08-25 21:32:47 +02:00
else:
2021-09-03 22:39:17 +02:00
return self.compute_velocity_cmd(linear, angular)
2021-08-25 21:32:47 +02:00
class CircularTrajectory(MultiViewPolicy):
def __init__(self, rate):
super().__init__(rate)
self.r = 0.1
2021-09-03 22:39:17 +02:00
self.h = self.min_z_dist
self.duration = 2.0 * np.pi * self.r / self.linear_vel + 2.0
self.m = scipy.interpolate.interp1d([0.0, self.duration], [np.pi, 3.0 * np.pi])
2021-08-25 21:32:47 +02:00
def activate(self, bbox):
super().activate(bbox)
self.tic = rospy.Time.now()
2021-09-03 22:39:17 +02:00
def update(self, img, x):
self.integrate(img, x)
2021-08-25 21:32:47 +02:00
elapsed_time = (rospy.Time.now() - self.tic).to_sec()
if elapsed_time > self.duration:
self.done = True
2021-09-04 15:50:29 +02:00
return np.zeros(6)
2021-08-25 21:32:47 +02:00
else:
t = self.m(elapsed_time)
eye = self.center + np.r_[self.r * np.cos(t), self.r * np.sin(t), self.h]
up = np.r_[1.0, 0.0, 0.0]
2021-09-03 22:39:17 +02:00
x_d = look_at(eye, self.center, up)
linear, angular = self.compute_error(x_d, x)
return self.compute_velocity_cmd(linear, angular)