demo_wp_trajectory_generator.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # Copyright (c) 2016-2019 The UUV Simulator Authors.
3 # All rights reserved.
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 from __future__ import print_function
17 import roslib
18 import numpy as np
19 import matplotlib.pyplot as plt
20 import uuv_trajectory_generator
21 import time
22 from geometry_msgs.msg import Point
23 from mpl_toolkits.mplot3d import Axes3D
24 
25 roslib.load_manifest('uuv_trajectory_control')
26 
27 """
28 Demo file to demonstrate the waypoint interpolation method with generation of
29 velocity and acceleration profile using a constant rate.
30 """
31 
32 def run_generator(waypoint_set, interp_method):
33  # Initialize the trajectory generator
34  gen = uuv_trajectory_generator.WPTrajectoryGenerator(full_dof=False)
35  gen.set_interp_method(interp_method)
36  gen.init_waypoints(waypoint_set)
37 
38  dt = 0.05
39  idx = 0
40  pnts = list()
41  avg_time = 0.0
42 
43  gen.set_start_time(0)
44  for ti in np.arange(-2, gen.get_max_time(), dt):
45  tic = time.clock()
46  pnts.append(gen.interpolate(ti))
47  toc = time.clock()
48  avg_time += toc - tic
49  idx += 1
50  avg_time /= idx
51  print('Average processing time [s] =', avg_time)
52  fig = plt.figure()
53  # Trajectory and heading 3D plot
54  ax = fig.add_subplot(111, projection='3d')
55 
56  # Plot the generated path
57  ax.plot([p.x for p in pnts], [p.y for p in pnts], [p.z for p in pnts], 'b')
58 
59  # Plot original waypoints
60  ax.plot(waypoint_set.x, waypoint_set.y, waypoint_set.z, 'r.')
61 
62  # Plot the raw path along the waypoints
63  ax.plot(waypoint_set.x, waypoint_set.y, waypoint_set.z, 'g--')
64 
65  for i in range(1, len(pnts), 100):
66  p0 = pnts[i - 1]
67  p1 = p0.pos + np.dot(p0.rot_matrix, [2, 0, 0])
68  ax.plot([p0.pos[0], p1[0]], [p0.pos[1], p1[1]], [p0.pos[2], p1[2]], 'c', linewidth=2)
69  ax.grid(True)
70  ax.set_title(interp_method)
71 
72  # Position and orientation plot
73  fig = plt.figure()
74  ax = fig.add_subplot(211)
75  for i in range(3):
76  ax.plot([p.t for p in pnts], [p.pos[i] for p in pnts], label='%d' % i)
77  ax.legend()
78  ax.grid(True)
79  ax.set_title('Position - ' + interp_method)
80  ax.set_xlim(pnts[0].t, pnts[-1].t)
81 
82  ax = fig.add_subplot(212)
83  for i in range(3):
84  ax.plot([p.t for p in pnts], [p.rot[i] * 180 / np.pi for p in pnts], label='%d' % i)
85  ax.legend()
86  ax.grid(True)
87  ax.set_title('Rotation - ' + interp_method)
88  ax.set_xlim(pnts[0].t, pnts[-1].t)
89 
90  fig = plt.figure()
91  ax = fig.add_subplot(211)
92  for i in range(3):
93  ax.plot([p.t for p in pnts], [p.vel[i] for p in pnts], label='%d' % i)
94  ax.legend()
95  ax.grid(True)
96  ax.set_title('Linear velocity - ' + interp_method)
97  ax.set_xlim(pnts[0].t, pnts[-1].t)
98 
99  ax = fig.add_subplot(212)
100  for i in range(3):
101  ax.plot([p.t for p in pnts], [p.vel[i+3] for p in pnts], label='%d' % i)
102  ax.legend()
103  ax.grid(True)
104  ax.set_title('Angular velocity - ' + interp_method)
105  ax.set_xlim(pnts[0].t, pnts[-1].t)
106 
107  fig = plt.figure()
108  ax = fig.add_subplot(211)
109  for i in range(3):
110  ax.plot([p.t for p in pnts], [p.acc[i] for p in pnts], label='%d' % i)
111  ax.legend()
112  ax.grid(True)
113  ax.set_title('Linear accelerations - ' + interp_method)
114  ax.set_xlim(pnts[0].t, pnts[-1].t)
115 
116  ax = fig.add_subplot(212)
117  for i in range(3):
118  ax.plot([p.t for p in pnts], [p.acc[i+3] for p in pnts], label='%d' % i)
119  ax.legend()
120  ax.grid(True)
121  ax.set_title('Angular accelerations - ' + interp_method)
122  ax.set_xlim(pnts[0].t, pnts[-1].t)
123 
124 if __name__ == '__main__':
125  # For a helical trajectory
126  wp_set = uuv_trajectory_generator.WaypointSet()
127  # Add some waypoints at the beginning
128  wp_set.add_waypoint(uuv_trajectory_generator.Waypoint(-10, -12, -36, 0.5),
129  add_to_beginning=True)
130  wp_set.add_waypoint(uuv_trajectory_generator.Waypoint(-13, -15, -44, 0.5),
131  add_to_beginning=True)
132  wp_set.add_waypoint(uuv_trajectory_generator.Waypoint(-20, -24, -48, 0.5),
133  add_to_beginning=True)
134  wp_set.add_waypoint(uuv_trajectory_generator.Waypoint(-10, 10, -5, 0.5))
135  wp_set.add_waypoint(uuv_trajectory_generator.Waypoint(-20, 20, -5, 0.5))
136  wp_set.add_waypoint(uuv_trajectory_generator.Waypoint(-30, 60, -50, 0.5))
137  wp_set.add_waypoint(uuv_trajectory_generator.Waypoint(-40, 70, -55, 0.5))
138  wp_set.add_waypoint(uuv_trajectory_generator.Waypoint(-40, 80, -30, 0.5))
139 
140  run_generator(wp_set, 'cubic_interpolator')
141  run_generator(wp_set, 'lipb_interpolator')
142 
143  plt.show()
144 
def run_generator(waypoint_set, interp_method)


uuv_trajectory_control
Author(s):
autogenerated on Thu Jun 18 2020 03:28:42