|
| 1 | +import ctypes |
| 2 | +import numpy as np |
| 3 | +from ctypes import c_double, c_int |
| 4 | + |
| 5 | +cdll = ctypes.CDLL("build/libFrenetOptimalTrajectory.so") |
| 6 | +_get_fot_frenet_space = cdll.get_fot_frenet_space |
| 7 | +_get_fot_frenet_space.argtypes = ( |
| 8 | + c_double, c_double, c_double, c_double, c_double, # s0, c_speed, c_d, c_d_d, c_d_dd |
| 9 | + ctypes.POINTER(c_double), ctypes.POINTER(c_double), # wx, wy |
| 10 | + ctypes.POINTER(c_double), ctypes.POINTER(c_double), # ox, oy |
| 11 | + c_int, c_int, c_double, # path length, num obstacles, target speed |
| 12 | + ctypes.POINTER(c_double), ctypes.POINTER(c_double), # result x, result y |
| 13 | + ctypes.POINTER(c_double), ctypes.POINTER(c_double) # speeds, misc |
| 14 | +) |
| 15 | +_get_fot_frenet_space.restype = ctypes.c_int |
| 16 | +_compute_initial_conditions = cdll.compute_initial_conditions |
| 17 | +_compute_initial_conditions.restype = None |
| 18 | +_compute_initial_conditions.argtypes = ( |
| 19 | + c_double, c_double, c_double, c_double, c_double, c_double, |
| 20 | + ctypes.POINTER(c_double), ctypes.POINTER(c_double), |
| 21 | + c_int, ctypes.POINTER(c_double) |
| 22 | +) |
| 23 | + |
| 24 | + |
| 25 | +def get_fot_frenet_space(s0, c_speed, c_d, c_d_d, c_d_dd, wx, wy, obs, |
| 26 | + target_speed): |
| 27 | + """ Return the frenet optimal trajectory given initial conditions in |
| 28 | + cartesian space. |
| 29 | +
|
| 30 | + Args: |
| 31 | + s0 (float): initial longitudinal position |
| 32 | + c_speed (float): initial forward speed |
| 33 | + c_d (float): initial lateral position |
| 34 | + c_d_d (float): initial lateral velocity |
| 35 | + c_d_dd (float): initial lateral acceleration |
| 36 | + wx (list(float)): list of global x waypoints |
| 37 | + wy (list(float)): list of global y waypoints |
| 38 | + obs (list((float, float))): list of obstacle locations |
| 39 | + target_speed (float): target speed |
| 40 | +
|
| 41 | + Returns: |
| 42 | + result_x (list(float)): x positions of fot, if it exists |
| 43 | + result_y (list(float)): y positions of fot, if it exists |
| 44 | + speeds (list(float)): velocities of fot, if it exists |
| 45 | + params (dict): next frenet coordinates, if they exist |
| 46 | + success (bool): whether a fot was found or not |
| 47 | + """ |
| 48 | + result_x = np.zeros(100) |
| 49 | + result_y = np.zeros(100) |
| 50 | + speeds = np.zeros(100) |
| 51 | + misc = np.zeros(5) |
| 52 | + if obs.shape[0] == 0: |
| 53 | + obs = np.empty((0, 2)) |
| 54 | + success = _get_fot_frenet_space( |
| 55 | + c_double(s0), c_double(c_speed), c_double(c_d), |
| 56 | + c_double(c_d_d), c_double(c_d_dd), |
| 57 | + wx.ctypes.data_as(ctypes.POINTER(c_double)), |
| 58 | + wy.ctypes.data_as(ctypes.POINTER(c_double)), |
| 59 | + obs[:, 0].ctypes.data_as(ctypes.POINTER(c_double)), |
| 60 | + obs[:, 1].ctypes.data_as(ctypes.POINTER(c_double)), |
| 61 | + c_int(len(wx)), c_int(len(obs)), c_double(target_speed), |
| 62 | + result_x.ctypes.data_as(ctypes.POINTER(c_double)), |
| 63 | + result_y.ctypes.data_as(ctypes.POINTER(c_double)), |
| 64 | + speeds.ctypes.data_as(ctypes.POINTER(c_double)), |
| 65 | + misc.ctypes.data_as(ctypes.POINTER(c_double)) |
| 66 | + ) |
| 67 | + params = { |
| 68 | + "s0": misc[0], |
| 69 | + "c_speed": misc[1], |
| 70 | + "c_d": misc[2], |
| 71 | + "c_d_d": misc[3], |
| 72 | + "c_d_dd": misc[4] |
| 73 | + } |
| 74 | + ind = -1 |
| 75 | + if success: |
| 76 | + ind = np.where(np.isnan(result_x))[0][0] |
| 77 | + |
| 78 | + return result_x[:ind], result_y[:ind], speeds[:ind], params, success |
| 79 | + |
| 80 | + |
| 81 | +def compute_initial_conditions(s0, x, y, vx, vy, forward_speed, wx, wy): |
| 82 | + """ Return the frenet optimal trajectory given initial conditions in |
| 83 | + cartesian space. |
| 84 | +
|
| 85 | + Args: |
| 86 | + s0 (float): previous longitudinal position |
| 87 | + x (float): initial x position |
| 88 | + y (float): initial y position |
| 89 | + vx (float): initial x velocity |
| 90 | + vy (float): initial y velocity |
| 91 | + forward_speed (float): initial speed |
| 92 | + wx (list(float)): list of global x waypoints |
| 93 | + wy (list(float)): list of global y waypoints |
| 94 | +
|
| 95 | + Returns: |
| 96 | + s_c (float): current longitudinal position |
| 97 | + c_speed (float): current speed |
| 98 | + c_d (float): current lateral offset |
| 99 | + c_d_d (float): current lateral velocity |
| 100 | + c_d_dd (float): current lateral acceleration |
| 101 | + """ |
| 102 | + misc = np.zeros(5) |
| 103 | + _compute_initial_conditions( |
| 104 | + c_double(s0), c_double(x), c_double(y), c_double(vx), c_double(vy), |
| 105 | + c_double(forward_speed), wx.ctypes.data_as(ctypes.POINTER(c_double)), |
| 106 | + wy.ctypes.data_as(ctypes.POINTER(c_double)), c_int(len(wx)), |
| 107 | + misc.ctypes.data_as(ctypes.POINTER(c_double)) |
| 108 | + ) |
| 109 | + |
| 110 | + return misc[0], misc[1], misc[2], misc[3], misc[4] |
0 commit comments