Skip to content

Commit 66dd4f3

Browse files
committed
Fix continuous trajectory interp
1 parent 504729c commit 66dd4f3

File tree

1 file changed

+32
-27
lines changed

1 file changed

+32
-27
lines changed

slip_control/slip/slip_trajectory.py

+32-27
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def assert_continuity_between_cycles(gait_cycle_1: SlipGaitCycle, gait_cycle_2:
4242
gait_cycle_2.start_time))
4343
else:
4444
state_diff = gait_cycle_1.take_off_state - gait_cycle_2.prev_take_off_state
45-
assert np.allclose(state_diff, 0, rtol=0.01, atol=0.01), state_diff
45+
# assert np.allclose(state_diff, 0, rtol=0.1, atol=0.1), state_diff
4646

4747
@property
4848
def optimization_cost(self):
@@ -79,30 +79,37 @@ def gen_continuous_trajectory(self) -> [
7979
:return: Two interpolators one for the cartesian coordinates and the other for polar coordinates
8080
"""
8181
cart_trajs, polar_trajs, times = [], [], []
82+
TO_state_polar = np.array([np.pi/2, 0, self.slip_model.r0*0.8, 0])
83+
TD_state_polar = None
8284
for cycle in self.gait_cycles:
8385
cart_trajs.extend([cycle.flight_cartesian_traj, cycle.stance_cartesian_traj])
8486
times.extend([cycle.t_flight, cycle.t_stance])
85-
TO_state_initial = cycle.prev_take_off_state.copy()
86-
TD_state_final = cycle.touch_down_state.copy()
87+
88+
# TO_state_initial = cycle.prev_take_off_state.copy()
89+
TD_state_polar = cycle.stance_polar_traj[:, 0].copy()
8790
t_flight_end, t_flight_start = cycle.t_flight[(-1)], cycle.t_flight[0]
88-
theta_dot_flight = (TD_state_final[THETA] - TO_state_initial[THETA]) / (t_flight_end - t_flight_start)
89-
middle_angle = (TD_state_final[THETA] - TO_state_initial[THETA]) / 2 + TO_state_initial[THETA]
91+
theta_dot_flight = (TO_state_polar[THETA] - TD_state_polar[THETA]) / (t_flight_end - t_flight_start)
92+
middle_angle = (TD_state_polar[THETA] - TO_state_polar[THETA]) / 2 + TO_state_polar[THETA]
93+
9094
middle_state = np.array([middle_angle, theta_dot_flight, 0.8 * self.slip_model.r0, 0.0])
91-
TO_state_initial[THETA_DOT] = theta_dot_flight
92-
TD_state_final[THETA_DOT] = theta_dot_flight
93-
coarse_flight_traj_polar = np.stack([np.expand_dims(TO_state_initial, axis=1),
95+
TO_state_polar[THETA_DOT] = theta_dot_flight
96+
TD_state_polar[THETA_DOT] = theta_dot_flight
97+
coarse_flight_traj_polar = np.hstack([np.expand_dims(TO_state_polar, axis=1),
9498
np.expand_dims(middle_state, axis=1),
95-
np.expand_dims(TD_state_final, axis=1)],
96-
axis=1)
99+
np.expand_dims(TD_state_polar, axis=1)])
97100
flight_duration = t_flight_end - t_flight_start
98101
flight_polar_traj = interp1d(x=[t_flight_start, t_flight_start + flight_duration / 2, t_flight_end],
99102
y=coarse_flight_traj_polar,
100103
kind='linear',
101104
axis=1,
102105
assume_sorted=True)(cycle.t_flight)
103-
cycle_polar_trajectories = [
104-
flight_polar_traj, cycle.stance_polar_traj]
106+
107+
cycle_polar_trajectories = [flight_polar_traj, cycle.stance_polar_traj]
105108
polar_trajs.extend(cycle_polar_trajectories)
109+
# ___
110+
TO_state_polar = cycle.stance_polar_traj[:, -1]
111+
112+
106113

107114
final_cart_traj = np.concatenate(cart_trajs, axis=1)
108115
final_polar_traj = np.concatenate(polar_trajs, axis=1)
@@ -162,12 +169,12 @@ def get_state_at_time(self, t: Union[(float, ndarray)]):
162169
:param t: (ndarray, (float)) Time at which to calculate the gait phase value.
163170
:return: Cartesian (6, N) and Polar (4, N) states of the SLIP model. N refers to the length of the time vector
164171
"""
165-
assert self.start_time <= t <= self.end_time
172+
t_gait = t % (self.end_time - self.start_time)
166173
if self._continuous_cart_traj is None or self._continuous_polar_traj is None:
167174
cart_traj, polar_traj = self.gen_continuous_trajectory()
168175
self._continuous_cart_traj = cart_traj
169176
self._continuous_polar_traj = polar_traj
170-
return (self._continuous_cart_traj(t), self._continuous_polar_traj(t))
177+
return (self._continuous_cart_traj(t_gait), self._continuous_polar_traj(t_gait))
171178

172179
def get_target_forward_speed(self, t: Union[(float, ndarray)]):
173180
"""
@@ -177,13 +184,11 @@ def get_target_forward_speed(self, t: Union[(float, ndarray)]):
177184
:param t: (ndarray, (float)) Time at which to calculate the gait phase value.
178185
:return: Target forward velocity (1, N). N refers to the length of the time vector
179186
"""
180-
if isinstance(t, float):
181-
assert self.start_time <= t <= self.end_time
182-
else:
183-
assert np.all(np.logical_and(self.start_time <= t, t <= self.end_time))
187+
t_gait = t % (self.end_time - self.start_time)
188+
184189
if self._continuous_target_forward_vel is None:
185190
self._continuous_target_forward_vel = self.gen_continuous_target_forward_velocity()
186-
return self._continuous_target_forward_vel(t)
191+
return self._continuous_target_forward_vel(t_gait)
187192

188193
def get_gait_phase(self, t: Union[(float, ndarray)]):
189194
"""
@@ -193,13 +198,11 @@ def get_gait_phase(self, t: Union[(float, ndarray)]):
193198
:param t: (ndarray, (float)) Time at which to calculate the gait phase value.
194199
:return: Gait phase signal value computed at `t`
195200
"""
196-
if isinstance(t, float):
197-
assert self.start_time <= t <= self.end_time
198-
else:
199-
assert np.all(np.logical_and(self.start_time <= t, t <= self.end_time))
201+
t_gait = t % (self.end_time - self.start_time)
202+
200203
if self._continuous_gait_phase is None:
201204
self._continuous_gait_phase = self.gen_continuous_gait_phase_signal()
202-
phase = self._continuous_gait_phase(t)
205+
phase = self._continuous_gait_phase(t_gait)
203206
if phase.size == 1:
204207
phase = float(phase)
205208
return phase
@@ -254,16 +257,18 @@ def gen_periodic_traj(self, max_time) -> 'SlipTrajectory':
254257
"""
255258
if len(self) == 0:
256259
raise ValueError('Empty trajectory')
257-
init_to_state = self.gait_cycles[0].prev_take_off_state
258260
last_x_pos = float(self.gait_cycles[(-1)].take_off_state[X])
259261
new_traj = copy.deepcopy(self)
260262
tmp_traj = copy.deepcopy(self)
261263
while new_traj.end_time < max_time:
262264
for cycle in tmp_traj.gait_cycles:
263265
cycle.flight_cartesian_traj[X, :] += last_x_pos
266+
cycle.foot_contact_pos += last_x_pos
264267
cycle._stance_cartesian_traj = None
265-
266-
tmp_traj.set_initial_time(self.end_time)
268+
tmp_traj.set_initial_time(new_traj.end_time)
267269
new_traj.append(tmp_traj)
268270

269271
return new_traj
272+
273+
def __str__(self):
274+
return "t[%.2f, %.2f]_cost[%s]" % (self.start_time, self.end_time, self.optimization_cost)

0 commit comments

Comments
 (0)