File size: 2,928 Bytes
0c3dd1d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
"""Reference path specifications (NEW CODE).

Reference arrays are N x 4: x, y, yaw, v_ref -- the released waypoint format
read by ControlNode.__init__ with np.loadtxt(delimiter=',').
"""
import hashlib
import os

import numpy as np

from .released import WAYPOINT_DIR, sha256_file


def wrap(a):
    return np.arctan2(np.sin(a), np.cos(a))


class Reference:
    def __init__(self, spec, array, ref_path_parameter, override_array, source_files):
        self.spec = spec
        self.array = np.asarray(array, dtype=float)
        self.ref_path_parameter = ref_path_parameter
        self.override_array = override_array
        self.source_files = source_files  # {path: sha256}
        seg = np.linalg.norm(np.diff(self.array[:, :2], axis=0), axis=1)
        self.arc = np.concatenate(([0.0], np.cumsum(seg)))
        self.length = float(self.arc[-1])
        self.sha256 = hashlib.sha256(np.ascontiguousarray(self.array).tobytes()).hexdigest()


def _load_released(name):
    path = os.path.join(WAYPOINT_DIR, name + '.csv')
    with open(path) as f:
        arr = np.loadtxt(f, delimiter=',')  # identical call to path_follower.py:103-104
    return path, arr


def build_reference(spec):
    kind = spec['type']
    if kind == 'released_csv':
        path, arr = _load_released(spec['name'])
        return Reference(spec, arr, spec['name'], None, {path: sha256_file(path)})
    if kind == 'csv_transformed':
        path, arr = _load_released(spec['name'])
        rot = float(spec.get('rotate', 0.0))
        c, s = np.cos(rot), np.sin(rot)
        out = arr.copy()
        out[:, 0] = c * arr[:, 0] - s * arr[:, 1] + float(spec.get('dx', 0.0))
        out[:, 1] = s * arr[:, 0] + c * arr[:, 1] + float(spec.get('dy', 0.0))
        if rot != 0.0:
            out[:, 2] = wrap(arr[:, 2] + rot)
        return Reference(spec, out, spec['name'], out, {path: sha256_file(path)})
    if kind == 'straight':
        x0, y0 = float(spec.get('x0', 0.0)), float(spec.get('y0', 0.0))
        h = float(spec.get('heading', 0.0))
        spacing = float(spec.get('spacing', 0.1))
        s0, s1 = float(spec.get('s_start', -10.0)), float(spec.get('s_end', 60.0))
        n = int(round((s1 - s0) / spacing)) + 1
        s = s0 + spacing * np.arange(n)
        arr = np.column_stack((x0 + s * np.cos(h), y0 + s * np.sin(h),
                               np.full(n, float(wrap(h))), np.full(n, float(spec.get('v_ref', 1.0)))))
        # ref_path parameter only makes the released __init__ open an existing file;
        # node.ref_traj is then replaced by this array.
        return Reference(spec, arr, 'straight_line', arr, {})
    if kind == 'array':
        arr = np.asarray(spec['points'], dtype=float)
        if arr.ndim != 2 or arr.shape[1] != 4:
            raise ValueError('array reference must be N x 4')
        return Reference(spec, arr, 'straight_line', arr, {})
    raise ValueError(f'unknown reference type {kind}')