41import matplotlib.pyplot
as plt
45plt.rc(
'text', usetex=
True)
46plt.rc(
'text.latex', preamble=
r'\usepackage{amsmath}')
49from visp.core
import ExponentialMap
50from visp.core
import HomogeneousMatrix
51from visp.core
import Math
52from visp.core
import Point
53from visp.core
import RotationMatrix
54from visp.core
import ThetaUVector
55from visp.core
import TranslationVector
57from visp.visual_features
import FeatureBuilder
58from visp.visual_features
import FeaturePoint
60from visp.vs
import Servo
63 def __init__(self, e, norm_e, v, x, xd, c_T_w, plot_log_scale):
72 def stack(self, e, norm_e, v, x, xd, c_T_w):
80 plt.figure(figsize=(10,10))
82 plot_e = plt.subplot(2, 2, 1)
83 plot_v = plt.subplot(2, 2, 2)
84 plot_ne = plt.subplot(2, 2, 3)
85 plot_x = plt.subplot(2, 2, 4)
87 plot_e.set_title(
'error')
88 plot_v.set_title(
'camera velocity')
89 plot_x.set_title(
'point trajectory in the image plane')
92 plot_ne.set_title(
'log(norm error)')
95 plot_ne.set_title(
'norm error')
104 plot_e.legend([
'$x_1$',
'$y_1$',
'$x_2$',
'$y_2$',
'$x_3$',
'$y_3$',
'$x_4$',
'$y_4$',])
106 for i
in range(self.
vector_v.shape[1]):
108 plot_v.legend([
'$v_x$',
'$v_y$',
'$v_z$',
'$\omega_x$',
'$\omega_y$',
'$\omega_z$'])
110 for i
in range(self.
vector_x.shape[1]):
111 pts = np.asarray([[p.get_x(), p.get_y()]
for p
in self.
vector_x[:, i]])
112 plot_x.plot(pts[:, 0], pts[:, 1])
113 plot_x.legend([
'$x_1$',
'$x_2$',
'$x_3$',
'$x_4$'])
115 for i
in range(self.
vector_x.shape[1] ):
119 output_folder = os.path.dirname(fig_filename)
120 if not os.path.exists(output_folder):
121 os.makedirs(output_folder)
122 print(
"Create output folder: ", output_folder)
124 print(f
"Figure is saved in {fig_filename}")
125 plt.savefig(fig_filename)
128 plot_traj = plt.figure().add_subplot(projection=
'3d')
133 for i
in range(len(min_s)):
134 if (max_s[i] - min_s[i]) < 1.:
137 plot_traj.axis(xmin=min_s[0], xmax=max_s[0])
138 plot_traj.axis(ymin=min_s[1], ymax=max_s[1])
141 plot_traj.set_title(
'Camera trajectory w_t_c in world space')
143 filename = os.path.splitext(fig_filename)[0] +
"-traj-w_t_c.png"
144 print(f
"Figure is saved in {filename}")
145 plt.savefig(filename)
149if __name__ ==
'__main__':
150 parser = argparse.ArgumentParser(description=
'The script corresponding to TP 4, IBVS on 4 points.')
151 parser.add_argument(
'--initial-position', type=int, default=2, dest=
'initial_position', help=
'Initial position selection (value 1, 2, or 3)')
152 parser.add_argument(
'--interaction', type=str, default=
"current", dest=
'interaction_matrix_type', help=
'Interaction matrix type (value \"current\" or \"desired\")')
153 parser.add_argument(
'--plot-log-scale', action=
'store_true', help=
'Plot norm of the error using a logarithmic scale')
154 parser.add_argument(
'--no-plot', action=
'store_true', help=
'Disable plots')
156 args, unknown_args = parser.parse_known_args()
158 print(
"The following args are not recognized and will not be used: %s" % unknown_args)
161 print(f
"Use initial position {args.initial_position}")
164 c_T_w = HomogeneousMatrix()
166 if args.initial_position == 1:
168 thetau = ThetaUVector(0.0, 0.0, 0.0)
169 c_R_w = RotationMatrix(thetau)
170 c_t_w = TranslationVector(0.0, 0.0, 1.3)
173 elif args.initial_position == 2:
175 thetau = ThetaUVector(Math.rad(10), Math.rad(20), Math.rad(30))
176 c_R_w = RotationMatrix(thetau)
177 c_t_w = TranslationVector(-0.2, -0.1, 1.3)
180 elif args.initial_position == 3:
182 thetau = ThetaUVector(0.0, 0.0, Math.rad(90))
183 c_R_w = RotationMatrix(thetau)
184 c_t_w = TranslationVector(0.0, 0.0, 1.0)
188 raise ValueError(f
"Wrong initial position value. Values are 1, 2 or 3")
191 cd_T_w = HomogeneousMatrix()
192 thetau = ThetaUVector(0, 0, 0)
193 cd_R_w = RotationMatrix(thetau)
194 cd_t_w = TranslationVector(0.0, 0.0, 1.0)
195 cd_T_w.insert(cd_R_w)
196 cd_T_w.insert(cd_t_w)
200 wX.append(Point(-0.1, 0.1, 0.0))
201 wX.append(Point( 0.1, 0.1, 0.0))
202 wX.append(Point( 0.1, -0.1, 0.0))
203 wX.append(Point(-0.1, -0.1, 0.0))
206 x = [FeaturePoint(), FeaturePoint(), FeaturePoint(),FeaturePoint()]
207 xd = [FeaturePoint(), FeaturePoint(), FeaturePoint(), FeaturePoint()]
211 task.setServo(Servo.EYEINHAND_CAMERA)
215 for i
in range(len(wX)):
218 FeatureBuilder.create(x[i], wX[i])
220 print(f
"Visual features at initial position for point {i}:")
221 print(f
" wX[{i}]: {wX[i].get_oX()} {wX[i].get_oY()} {wX[i].get_oZ()}")
222 print(f
" cX[{i}]: {wX[i].get_X()} {wX[i].get_Y()} {wX[i].get_Z()}")
223 print(f
" x[{i}]: {x[i].get_x()} {x[i].get_y()} {x[i].get_Z()}")
227 FeatureBuilder.create(xd[i], wX[i])
229 print(f
"Visual features at desired position for point {i}:")
230 print(f
"cdX[{i}]: {wX[i].get_X()} {wX[i].get_Y()} {wX[i].get_Z()}")
231 print(f
"xd[{i}]: {xd[i].get_x()} {xd[i].get_y()} {xd[i].get_Z()}")
234 task.addFeature(x[i], xd[i])
239 while (iter == 0
or norm_e > 0.0001):
240 print(f
"---- Visual servoing iteration {iter} ----")
248 if args.interaction_matrix_type ==
"current":
250 task.setInteractionMatrixType(Servo.CURRENT, Servo.PSEUDO_INVERSE)
252 for i
in range(len(wX)):
255 FeatureBuilder.create(x[i], wX[i])
256 elif args.interaction_matrix_type ==
"desired":
258 task.setInteractionMatrixType(Servo.DESIRED, Servo.PSEUDO_INVERSE)
260 for i
in range(len(wX)):
263 FeatureBuilder.create(x[i], wX[i])
266 FeatureBuilder.create(xd[i], wX[i])
268 raise ValueError(f
"Wrong interaction matrix type. Values are \"current\" or \"desired\"")
271 v = task.computeControlLaw()
273 norm_e = e.frobeniusNorm()
274 Lx = task.getInteractionMatrix()
281 p.buildFrom(f.get_x(), f.get_y(), f.get_Z())
285 plot =
PlotIbvs(e, norm_e, v, xplot, xd, c_T_w, args.plot_log_scale)
287 plot.stack(e, norm_e, v, xplot, xd, c_T_w)
290 c_T_c_delta_t = ExponentialMap.direct(v, 0.040)
293 c_T_w = c_T_c_delta_t.inverse() * c_T_w
296 print(f
"norm e: \n{norm_e}")
299 print(f
"c_T_w: \n{c_T_w}")
304 print(f
"\nConvergence achieved in {iter} iterations")
308 plot.display(
"results/fig-ibvs-four-points-initial-position-" +
str(args.initial_position) +
".png")
310 print(
"Kill the figure to quit...")
display(self, fig_filename)
__init__(self, e, norm_e, v, x, xd, c_T_w, plot_log_scale)
stack(self, e, norm_e, v, x, xd, c_T_w)