trajectory_point.py
Go to the documentation of this file.
1 # Copyright (c) 2016-2019 The UUV Simulator Authors.
2 # All rights reserved.
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 import rospy
16 import numpy as np
17 from uuv_control_msgs.msg import TrajectoryPoint as TrajectoryPointMsg
18 import geometry_msgs.msg as geometry_msgs
19 from tf_quaternion.transformations import quaternion_from_euler, euler_from_quaternion, quaternion_matrix
20 
21 
22 class TrajectoryPoint(object):
23  """Trajectory point data structure.
24 
25  > *Input arguments*
26 
27  * `t` (*type:* `float`, *value:* `0`): Timestamp
28  * `pos` (*type:* list of `float` or `numpy.array`, *default:* `[0, 0, 0]`):
29  3D position vector in meters
30  * `quat` (*type:* list of `float` or `numpy.array`, *default:* `[0, 0, 0, 1]`):
31  Quaternion in the form of `(x, y, z, w)`.
32  * `lin_vel` (*type:* list of `float` or `numpy.array`, *default:* `[0, 0, 0]`):
33  3D linear velocity vector in m/s
34  * `ang_vel` (*type:* list of `float` or `numpy.array`, *default:* `[0, 0, 0]`):
35  3D angular velocity vector as rad/s
36  * `lin_acc` (*type:* list of `float` or `numpy.array`, *default:* `[0, 0, 0]`):
37  3D linear acceleration vector as m/s$^2$
38  * `ang_acc` (*type:* list of `float` or `numpy.array`, *default:* `[0, 0, 0]`):
39  3D angular acceleration vector as rad/s$^2$
40  """
41  def __init__(self, t=0.0, pos=[0, 0, 0], quat=[0, 0, 0, 1],
42  lin_vel=[0, 0, 0], ang_vel=[0, 0, 0], lin_acc=[0, 0, 0],
43  ang_acc=[0, 0, 0]):
44  self._pos = np.array(pos)
45  self._rot = np.array(quat)
46  self._vel = np.hstack((lin_vel, ang_vel))
47  self._acc = np.hstack((lin_acc, ang_acc))
48  self._t = t
49 
50  def __str__(self):
51  msg = 'Time [s] = {}\n'.format(self._t)
52  msg += 'Position [m] = ({}, {}, {})\n'.format(self._pos[0], self._pos[1], self._pos[2])
53  eu = [a * 180 / np.pi for a in self.rot]
54  msg += 'Rotation [degrees] = ({}, {}, {})\n'.format(eu[0], eu[1], eu[2])
55  msg += 'Lin. velocity [m/s] = ({}, {}, {})\n'.format(self._vel[0], self._vel[1], self._vel[2])
56  msg += 'Ang. velocity [m/s] = ({}, {}, {})\n'.format(self._vel[3], self._vel[4], self._vel[5])
57  return msg
58 
59  def __eq__(self, pnt):
60  return self._t == pnt._t and np.array_equal(self._pos, pnt._pos) and \
61  np.array_equal(self._rot, pnt._rot) and \
62  np.array_equal(self._vel, pnt._vel) and \
63  np.array_equal(self._acc, pnt._acc)
64 
65  @property
66  def p(self):
67  """`numpy.array`: Position vector"""
68  return self._pos
69 
70  @property
71  def q(self):
72  """`numpy.array`: Quaternion vector as `(x, y, z, w)`"""
73  return self._rot
74 
75  @property
76  def v(self):
77  """`numpy.array`: Linear velocity vector"""
78  return self._vel[0:3]
79 
80  @property
81  def w(self):
82  """`numpy.array`: Angular velocity vector"""
83  return self._vel[3::]
84 
85  @property
86  def a(self):
87  """`numpy.array`: Linear acceleration vector"""
88  return self._acc[0:3]
89 
90  @property
91  def alpha(self):
92  """`numpy.array`: Angular acceleartion vector"""
93  return self._acc[3::]
94 
95  @property
96  def x(self):
97  """`float`: X coordinate of position vector"""
98  return self._pos[0]
99 
100  @x.setter
101  def x(self, x):
102  self._pos[0] = x
103 
104  @property
105  def y(self):
106  """`float`: Y coordinate of position vector"""
107  return self._pos[1]
108 
109  @y.setter
110  def y(self, y):
111  self._pos[1] = y
112 
113  @property
114  def z(self):
115  """`float`: Z coordinate of position vector"""
116  return self._pos[2]
117 
118  @z.setter
119  def z(self, z):
120  self._pos[2] = z
121 
122  @property
123  def t(self):
124  """`float`: Time stamp"""
125  return self._t
126 
127  @t.setter
128  def t(self, new_t):
129  self._t = new_t
130 
131  @property
132  def pos(self):
133  """`numpy.array`: Position vector"""
134  return self._pos
135 
136  @pos.setter
137  def pos(self, new_pos):
138  self._pos = np.array(new_pos)
139 
140  @property
141  def rot(self):
142  """`numpy.array`: `roll`, `pitch` and `yaw` angles"""
143  rpy = euler_from_quaternion(self._rot)
144  return np.array([rpy[0], rpy[1], rpy[2]])
145 
146  @rot.setter
147  def rot(self, new_rot):
148  self._rot = quaternion_from_euler(*new_rot)
149 
150  @property
151  def rot_matrix(self):
152  """`numpy.array`: Rotation matrix"""
153  return quaternion_matrix(self._rot)[0:3, 0:3]
154 
155  @property
156  def rotq(self):
157  """`numpy.array`: Quaternion vector as `(x, y, z, w)`"""
158  return self._rot
159 
160  @rotq.setter
161  def rotq(self, quat):
162  self._rot = quat
163 
164  @property
165  def vel(self):
166  """`numpy.array`: Linear velocity vector"""
167  return self._vel
168 
169  @vel.setter
170  def vel(self, new_vel):
171  self._vel = np.array(new_vel)
172 
173  @property
174  def acc(self):
175  """`numpy.array`: Linear acceleration vector"""
176  return self._acc
177 
178  @acc.setter
179  def acc(self, new_acc):
180  self._acc = np.array(new_acc)
181 
182  def to_message(self):
183  """Convert current data to a trajectory point message.
184 
185  > *Returns*
186 
187  Trajectory point message as `uuv_control_msgs/TrajectoryPoint`
188  """
189  p_msg = TrajectoryPointMsg()
190  # FIXME Sometimes the time t stored is NaN
191  p_msg.header.stamp = rospy.Time(self.t)
192  p_msg.pose.position = geometry_msgs.Vector3(*self.p)
193  p_msg.pose.orientation = geometry_msgs.Quaternion(*self.q)
194  p_msg.velocity.linear = geometry_msgs.Vector3(*self.v)
195  p_msg.velocity.angular = geometry_msgs.Vector3(*self.w)
196  p_msg.acceleration.linear = geometry_msgs.Vector3(*self.a)
197  p_msg.acceleration.angular = geometry_msgs.Vector3(*self.alpha)
198  return p_msg
199 
200  def from_message(self, msg):
201  """Parse a trajectory point message of type `uuv_control_msgs/TrajectoryPoint`
202  into the `uuv_trajectory_generator/TrajectoryPoint`.
203 
204  > *Input arguments*
205 
206  * `msg` (*type:* `uuv_control_msgs/TrajectoryPoint`): Input trajectory message
207  """
208  t = msg.header.stamp.to_sec()
209  p = msg.pose.position
210  q = msg.pose.orientation
211  v = msg.velocity.linear
212  w = msg.velocity.angular
213  a = msg.acceleration.linear
214  al = msg.acceleration.angular
215 
216  self._t = t
217  self._pos = np.array([p.x, p.y, p.z])
218  self._rot = np.array([q.x, q.y, q.z, q.w])
219  self._vel = np.array([v.x, v.y, v.z, w.x, w.y, w.z])
220  self._acc = np.array([a.x, a.y, a.z, al.x, al.y, al.z])
221  return True
222 
223  def from_dict(self, data):
224  """Initialize the trajectory point attributes from a `dict`.
225 
226  > *Input arguments*
227 
228  * `data` (*type:* `dict`): Trajectory point as a `dict`
229  """
230  self._t = data['time']
231  self._pos = np.array(data['pos'])
232  if self._rot.size == 3:
233  self._rot = quaternion_from_euler(data['rot'])
234  else:
235  self._rot = np.array(data['rot'])
236  self._vel = np.array(data['vel'])
237  self._acc = np.array(data['acc'])
238 
239  def to_dict(self):
240  """Convert trajectory point to `dict`.
241 
242  > *Returns*
243 
244  Trajectory points data as a `dict`
245  """
246  data = dict(time=self._t,
247  pos=self._pos,
248  rot=self._rot,
249  vel=self._vel,
250  acc=self._acc)
251  return data
def __init__(self, t=0.0, pos=[0, quat=[0, lin_vel=[0, ang_vel=[0, lin_acc=[0, ang_acc=[0)


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