timor.utilities.rtb_trajectory ============================== .. py:module:: timor.utilities.rtb_trajectory Attributes ---------- .. autoapisummary:: timor.utilities.rtb_trajectory.ArrayLike timor.utilities.rtb_trajectory._scalartypes Classes ------- .. autoapisummary:: timor.utilities.rtb_trajectory.RtbTrajectory Functions --------- .. autoapisummary:: timor.utilities.rtb_trajectory.getvector timor.utilities.rtb_trajectory.jtraj timor.utilities.rtb_trajectory.mstraj Module Contents --------------- .. py:data:: ArrayLike .. py:class:: RtbTrajectory(name, t, s, sd=None, sdd=None, istime=False) A container class for trajectory data. Reduced version of the original, without plotting functionalities. Construct a new trajectory instance :param name: name of the function that created the trajectory :type name: str :param t: independent variable, eg. time or step :type t: ndarray(m) :param s: position :type s: ndarray(m) or ndarray(m,n) :param sd: velocity :type sd: ndarray(m) or ndarray(m,n) :param sdd: acceleration :type sdd: ndarray(m) or ndarray(m,n) :param istime: ``t`` is time, otherwise step number :type istime: bool :param tblend: blend duration (``trapezoidal`` only) :type istime: float The object has attributes: - ``t`` the independent variable - ``s`` the position - ``sd`` the velocity - ``sdd`` the acceleration If ``t`` is time, ie. ``istime`` is True, then the units of ``sd`` and ``sdd`` are :math:`s^{-1}` and :math:`s^{-2}` respectively, otherwise with respect to ``t``. .. note:: Data is stored with timesteps as rows and axes as columns. .. py:method:: __len__() Length of trajectory :return: number of steps in the trajectory :rtype: int .. py:method:: __repr__() String representation of the trajectory .. py:method:: __str__() Print representation of the trajectory .. py:attribute:: istime :value: False .. py:attribute:: name .. py:property:: naxes Number of axes in the trajectory :return: number of axes or dimensions :rtype: int .. py:property:: q Position trajectory :return: trajectory with one row per timestep, one column per axis :rtype: ndarray(n,m) .. note:: This is a synonym for ``.s``, for compatibility with other applications. .. py:property:: qd Velocity trajectory :return: trajectory velocity with one row per timestep, one column per axis :rtype: ndarray(n,m) .. note:: This is a synonym for ``.sd``, for compatibility with other applications. .. py:property:: qdd Acceleration trajectory :return: trajectory acceleration with one row per timestep, one column per axis :rtype: ndarray(n,m) .. note:: This is a synonym for ``.sdd``, for compatibility with other applications. .. py:attribute:: s .. py:attribute:: sd :value: None .. py:attribute:: sdd :value: None .. py:attribute:: t .. py:data:: _scalartypes .. py:function:: getvector(v, dim=None, out='array', dtype=np.float64) Return a vector value :param v: passed vector :param dim: required dimension, or None if any length is ok :type dim: int or None :param out: output format, default is 'array' :type out: str :param dtype: datatype for numPy array return (default np.float64) :type dtype: numPy type :return: vector value in specified format :raises TypeError: value is not a list or NumPy array :raises ValueError: incorrect number of elements - ``getvector(vec)`` is ``vec`` converted to the output format ``out`` where ``vec`` is any of: - a Python native int or float, a 1-vector - Python native list or tuple - numPy real 1D array, ie. shape=(N,) - numPy real 2D array with a singleton dimension, ie. shape=(1,N) or (N,1) - ``getvector(vec, N)`` as above but must be an ``N``-element vector. The returned vector will be in the format specified by ``out``: ========== =============================================== format return type ========== =============================================== 'sequence' Python list, or tuple if a tuple was passed in 'list' Python list 'array' 1D numPy array, shape=(N,) [default] 'row' row vector, a 2D numPy array, shape=(1,N) 'col' column vector, 2D numPy array, shape=(N,1) ========== =============================================== .. runblock:: pycon >>> from spatialmath.base import getvector >>> import numpy as np >>> getvector([1,2]) # list >>> getvector([1,2], out='row') # list >>> getvector([1,2], out='col') # list >>> getvector((1,2)) # tuple >>> getvector(np.r_[1,2,3], out='sequence') # numpy array >>> getvector(1) # scalar >>> getvector([1]) >>> getvector([[1]]) .. note:: - For 'array', 'row' or 'col' output the NumPy dtype defaults to the ``dtype`` of ``v`` if it is a NumPy array, otherwise it is set to the value specified by the ``dtype`` keyword which defaults to ``np.float64``. - If ``v`` is symbolic the ``dtype`` is retained as ``'O'`` :seealso: :func:`isvector` .. py:function:: jtraj(q0, qf, t, qd0=None, qd1=None) Compute a joint-space trajectory :param q0: initial joint coordinate :type q0: array_like(n) :param qf: final joint coordinate :type qf: array_like(n) :param t: time vector or number of steps :type t: array_like or int :param qd0: initial velocity, defaults to zero :type qd0: array_like(n), optional :param qd1: final velocity, defaults to zero :type qd1: array_like(n), optional :return: trajectory :rtype: Trajectory instance - ``tg = jtraj(q0, qf, N)`` is a joint space trajectory where the joint coordinates vary from ``q0`` (M) to ``qf`` (M). A quintic (5th order) polynomial is used with default zero boundary conditions for velocity and acceleration. Time is assumed to vary from 0 to 1 in ``N`` steps. - ``tg = jtraj(q0, qf, t)`` as above but ``t`` is a uniformly-spaced time vector The return value is an object that contains position, velocity and acceleration data. Notes: - The time vector, if given, scales the velocity and acceleration outputs assuming that the time vector starts at zero and increases linearly. :seealso: :func:`ctraj`, :func:`qplot`, :func:`~SerialLink.jtraj` .. py:function:: mstraj(viapoints, dt, tacc, qdmax=None, tsegment=None, q0=None, qd0=None, qdf=None, verbose=False) Multi-segment multi-axis trajectory :param viapoints: A set of viapoints, one per row :type viapoints: ndarray(m,n) :param dt: time step :type dt: float (seconds) :param tacc: acceleration time (seconds) :type tacc: float :param qdmax: maximum speed, defaults to None :type qdmax: array_like(n) or float, optional :param tsegment: maximum time of each motion segment (seconds), defaults to None :type tsegment: array_like, optional :param q0: initial coordinates, defaults to first row of viapoints :type q0: array_like(n), optional :param qd0: inital velocity, defaults to zero :type qd0: array_like(n), optional :param qdf: final velocity, defaults to zero :type qdf: array_like(n), optional :param verbose: print debug information, defaults to False :type verbose: bool, optional :return: trajectory :rtype: Trajectory instance Computes a trajectory for N axes moving smoothly through a set of viapoints. The motion comprises M segments: - The initial coordinates are the first row of ``viapoints`` or ``q0`` if provided. - The final coordinates are the last row of ``viapoints`` - Each segment is linear motion and polynomial blends connect the viapoints. - All joints arrive at each via point at the same time, ie. the motion is coordinated across axes The time of the segments can be specified in two different ways: #. In terms of segment time where ``tsegment`` is an array of segment times which is the number of via points minus one:: ``traj = mstraj(viapoints, dt, tacc, tsegment=TS)`` #. Governed by the speed of the slowest axis for the segment. The axis speed is a scalar (all axes have the same speed) or an N-vector of speed per axis:: traj = mstraj(viapoints, dt, tacc, qdmax=SPEED) The return value is a namedtuple (named ``mstraj``) with elements: - ``t`` the time coordinate as a numpy ndarray, shape=(K,) - ``q`` the axis values as a numpy ndarray, shape=(K,N) - ``arrive`` a list of arrival times for each segment - ``info`` a list of named tuples, one per segment that describe the slowest axis, segment time, and time stamp - ``via`` the passed set of via points The trajectory proper is (``traj.t``, ``traj.q``). The trajectory is a matrix has one row per time step, and one column per axis. Notes: - Only one of ``qdmag`` or ``tsegment`` can be specified - If ``tacc`` is greater than zero then the path smoothly accelerates between segments using a polynomial blend. This means that the the via point is not actually reached. - The path length K is a function of the number of via points and the time or velocity limits that apply. - Can be used to create joint space trajectories where each axis is a joint coordinate. - Can be used to create Cartesian trajectories where the "axes" correspond to translation and orientation in RPY or Euler angle form. - If ``qdmax`` is a scalar then all axes are assumed to have the same maximum speed. - ``tg`` has extra attributes ``arrive``, ``info`` and ``via`` :seealso: :func:`trapezoidal`, :func:`ctraj`, :func:`mtraj`