Skip to content

Commit

Permalink
linting
Browse files Browse the repository at this point in the history
  • Loading branch information
luigiberducci committed Dec 26, 2023
1 parent 2bb4ef4 commit 9211767
Show file tree
Hide file tree
Showing 13 changed files with 110 additions and 68 deletions.
3 changes: 2 additions & 1 deletion gym/f110_gym/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import gymnasium as gym

gym.register(
id="f110-v0", entry_point="f110_gym.envs:F110Env",
id="f110-v0",
entry_point="f110_gym.envs:F110Env",
)
2 changes: 1 addition & 1 deletion gym/f110_gym/envs/base_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ def step(self, control_inputs):
for i, agent in enumerate(self.agents):
# update agent's information on other agents
opp_poses = np.concatenate(
(self.agent_poses[0:i, :], self.agent_poses[i + 1:, :]), axis=0
(self.agent_poses[0:i, :], self.agent_poses[i + 1 :, :]), axis=0
)
agent.update_opp_poses(opp_poses)

Expand Down
6 changes: 3 additions & 3 deletions gym/f110_gym/envs/cubic_spline.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def calc_position(self, x):
i = self.__search_index(x)
dx = x - self.x[i]
position = (
self.a[i] + self.b[i] * dx + self.c[i] * dx ** 2.0 + self.d[i] * dx ** 3.0
self.a[i] + self.b[i] * dx + self.c[i] * dx**2.0 + self.d[i] * dx**3.0
)

return position
Expand All @@ -86,7 +86,7 @@ def calc_first_derivative(self, x):

i = self.__search_index(x)
dx = x - self.x[i]
dy = self.b[i] + 2.0 * self.c[i] * dx + 3.0 * self.d[i] * dx ** 2.0
dy = self.b[i] + 2.0 * self.c[i] * dx + 3.0 * self.d[i] * dx**2.0
return dy

def calc_second_derivative(self, x):
Expand Down Expand Up @@ -205,7 +205,7 @@ def calc_curvature(self, s):
ddx = self.sx.calc_second_derivative(s)
dy = self.sy.calc_first_derivative(s)
ddy = self.sy.calc_second_derivative(s)
k = (ddy * dx - ddx * dy) / ((dx ** 2 + dy ** 2) ** (3 / 2))
k = (ddy * dx - ddx * dy) / ((dx**2 + dy**2) ** (3 / 2))
return k

def calc_yaw(self, s):
Expand Down
98 changes: 60 additions & 38 deletions gym/f110_gym/envs/dynamic_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ def upper_accel_limit(vel, a_max, v_switch):
pos_limit = a_max

return pos_limit



@njit(cache=True)
Expand Down Expand Up @@ -129,6 +128,7 @@ def accl_constraints(vel, a_long_d, v_switch, a_max, v_min, v_max):

return a_long


@njit(cache=True)
def steering_constraint(
steering_angle, steering_velocity, s_min, s_max, sv_min, sv_max
Expand Down Expand Up @@ -183,7 +183,7 @@ def vehicle_dynamics_ks(
v_max,
):
"""
Single Track Kinematic Vehicle Dynamics.
Single Track Kinematic Vehicle Dynamics.
Follows https://gitlab.lrz.de/tum-cps/commonroad-vehicle-models/-/blob/master/vehicleModels_commonRoad.pdf, section 5
Args:
Expand Down Expand Up @@ -242,11 +242,11 @@ def vehicle_dynamics_ks(
# system dynamics
f = np.array(
[
V * np.cos(PSI), #X_DOT
V * np.sin(PSI), #Y_DOT
STEER_VEL, #DELTA_DOT
ACCL, #V_DOT
(V / lwb) * np.tan(DELTA), #PSI_DOT
V * np.cos(PSI), # X_DOT
V * np.sin(PSI), # Y_DOT
STEER_VEL, # DELTA_DOT
ACCL, # V_DOT
(V / lwb) * np.tan(DELTA), # PSI_DOT
]
)
return f
Expand Down Expand Up @@ -318,7 +318,7 @@ def vehicle_dynamics_st(
PSI_DOT = x[5]
BETA = x[6]
# We have to wrap the slip angle to [-pi, pi]
#BETA = np.arctan2(np.sin(BETA), np.cos(BETA))
# BETA = np.arctan2(np.sin(BETA), np.cos(BETA))

# gravity constant m/s^2
g = 9.81
Expand All @@ -338,40 +338,62 @@ def vehicle_dynamics_st(
if V < 0.5:
# wheelbase
lwb = lf + lr
BETA_HAT = np.arctan(np.tan(DELTA) * lr /lwb)
BETA_DOT = (1/(1+ (np.tan(DELTA)*(lr/lwb))**2))*(lr/(lwb*np.cos(DELTA)**2))*STEER_VEL
f = np.array([
V * np.cos(PSI + BETA_HAT), # X_DOT
V * np.sin(PSI + BETA_HAT), # Y_DOT
STEER_VEL, # DELTA_DOT
ACCL, # V_DOT
V*np.cos(BETA_HAT)*np.tan(DELTA)/lwb, # PSI_DOT
(1/lwb)*(
ACCL*np.cos(BETA)*np.tan(DELTA) - \
V*np.sin(BETA)*np.tan(DELTA)*BETA_DOT + \
((V*np.cos(BETA)*STEER_VEL)/(np.cos(DELTA)**2))
), # PSI_DOT_DOT
BETA_DOT # BETA_DOT
])
BETA_HAT = np.arctan(np.tan(DELTA) * lr / lwb)
BETA_DOT = (
(1 / (1 + (np.tan(DELTA) * (lr / lwb)) ** 2))
* (lr / (lwb * np.cos(DELTA) ** 2))
* STEER_VEL
)
f = np.array(
[
V * np.cos(PSI + BETA_HAT), # X_DOT
V * np.sin(PSI + BETA_HAT), # Y_DOT
STEER_VEL, # DELTA_DOT
ACCL, # V_DOT
V * np.cos(BETA_HAT) * np.tan(DELTA) / lwb, # PSI_DOT
(1 / lwb)
* (
ACCL * np.cos(BETA) * np.tan(DELTA)
- V * np.sin(BETA) * np.tan(DELTA) * BETA_DOT
+ ((V * np.cos(BETA) * STEER_VEL) / (np.cos(DELTA) ** 2))
), # PSI_DOT_DOT
BETA_DOT, # BETA_DOT
]
)
else:
# system dynamics
f = np.array(
[
V * np.cos(PSI + BETA), # X_DOT
V * np.sin(PSI + BETA), # Y_DOT
STEER_VEL, # DELTA_DOT
ACCL, # V_DOT
PSI_DOT, # PSI_DOT
((mu*m)/(I*(lf+lr)))*(
lf*C_Sf*(g*lr - ACCL*h)*DELTA + \
(lr*C_Sr*(g*lf + ACCL*h) - lf*C_Sf*(g*lr - ACCL*h))*BETA - \
(lf*lf*C_Sf*(g*lr - ACCL*h) + lr*lr*C_Sr*(g*lf + ACCL*h))*(PSI_DOT/V)
), # PSI_DOT_DOT
(mu/(V*(lr+lf)))*(
C_Sf*(g*lr - ACCL*h)*DELTA - \
(C_Sr*(g*lf + ACCL*h) + C_Sf*(g*lr - ACCL*h))*BETA + \
(C_Sr*(g*lf + ACCL*h)*lr - C_Sf*(g*lr - ACCL*h)*lf)*(PSI_DOT/V)
) - PSI_DOT, # BETA_DOT
V * np.cos(PSI + BETA), # X_DOT
V * np.sin(PSI + BETA), # Y_DOT
STEER_VEL, # DELTA_DOT
ACCL, # V_DOT
PSI_DOT, # PSI_DOT
((mu * m) / (I * (lf + lr)))
* (
lf * C_Sf * (g * lr - ACCL * h) * DELTA
+ (
lr * C_Sr * (g * lf + ACCL * h)
- lf * C_Sf * (g * lr - ACCL * h)
)
* BETA
- (
lf * lf * C_Sf * (g * lr - ACCL * h)
+ lr * lr * C_Sr * (g * lf + ACCL * h)
)
* (PSI_DOT / V)
), # PSI_DOT_DOT
(mu / (V * (lr + lf)))
* (
C_Sf * (g * lr - ACCL * h) * DELTA
- (C_Sr * (g * lf + ACCL * h) + C_Sf * (g * lr - ACCL * h)) * BETA
+ (
C_Sr * (g * lf + ACCL * h) * lr
- C_Sf * (g * lr - ACCL * h) * lf
)
* (PSI_DOT / V)
)
- PSI_DOT, # BETA_DOT
]
)

Expand Down
12 changes: 7 additions & 5 deletions gym/f110_gym/envs/f110_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,13 +169,15 @@ def __init__(self, config: dict = None, render_mode=None, **kwargs):
# match render_fps to integration timestep
self.metadata["render_fps"] = int(1.0 / self.timestep)
if self.render_mode == "human_fast":
self.metadata["render_fps"] *= 10 # boost fps by 10x
self.metadata["render_fps"] *= 10 # boost fps by 10x
self.renderer, self.render_spec = make_renderer(
params=self.params, track=self.track, agent_ids=self.agent_ids,
render_mode=render_mode, render_fps=self.metadata["render_fps"]
params=self.params,
track=self.track,
agent_ids=self.agent_ids,
render_mode=render_mode,
render_fps=self.metadata["render_fps"],
)


@classmethod
def default_config(cls) -> dict:
"""
Expand Down Expand Up @@ -264,7 +266,7 @@ def _check_done(self):
temp_y[idx2] = -right_t - temp_y[idx2]
temp_y[np.invert(np.logical_or(idx1, idx2))] = 0

dist2 = delta_pt[0, :] ** 2 + temp_y ** 2
dist2 = delta_pt[0, :] ** 2 + temp_y**2
closes = dist2 <= 0.1
for i in range(self.num_agents):
if closes[i] and not self.near_starts[i]:
Expand Down
6 changes: 4 additions & 2 deletions gym/f110_gym/envs/f110_env_backup.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@

# others
import numpy as np

# protobuf import
import sim_requests_pb2
import yaml

# zmq imports
import zmq
from PIL import Image
Expand Down Expand Up @@ -261,7 +263,7 @@ def _check_done(self):
temp_y[idx2] = -right_t - temp_y[idx2]
temp_y[np.invert(np.logical_or(idx1, idx2))] = 0

dist2 = delta_pt[0, :] ** 2 + temp_y ** 2
dist2 = delta_pt[0, :] ** 2 + temp_y**2
closes = dist2 <= 0.1
for i in range(self.num_agents):
if closes[i] and not self.near_starts[i]:
Expand Down Expand Up @@ -289,7 +291,7 @@ def _check_done(self):
temp_y = -right_t - delta_pt[1]
else:
temp_y = 0
dist2 = delta_pt[0] ** 2 + temp_y ** 2
dist2 = delta_pt[0] ** 2 + temp_y**2
close = dist2 <= 0.1
# close = dist_to_start <= self.start_thresh
if close and not self.near_start:
Expand Down
6 changes: 5 additions & 1 deletion gym/f110_gym/test/benchmark_renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,11 @@ def _make_env(config={}, render_mode=None) -> F110Env:
}
config = deep_update(base_config, config)

env = gym.make("f110_gym:f110-v0", config=config, render_mode=render_mode,)
env = gym.make(
"f110_gym:f110-v0",
config=config,
render_mode=render_mode,
)

return env

Expand Down
6 changes: 5 additions & 1 deletion gym/f110_gym/test/test_renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ def _make_env(config={}, render_mode=None) -> F110Env:
}
config = deep_update(base_config, config)

env = gym.make("f110_gym:f110-v0", config=config, render_mode=render_mode,)
env = gym.make(
"f110_gym:f110-v0",
config=config,
render_mode=render_mode,
)

return env

Expand Down
18 changes: 9 additions & 9 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@
"opencv-python",
],
extras_require={
'dev': [
'pytest',
'flake8',
'black',
'ipykernel',
'isort',
'autoflake',
'matplotlib'
"dev": [
"pytest",
"flake8",
"black",
"ipykernel",
"isort",
"autoflake",
"matplotlib",
]
}
},
)
1 change: 0 additions & 1 deletion tests/test_collision_checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,3 @@ def test_fps(self):
fps = 1000 / elapsed
print("gjk fps:", fps)
# self.assertGreater(fps, 500) This is a platform dependent test, not ideal.

9 changes: 7 additions & 2 deletions tests/test_dynamics.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,12 @@
import unittest

import numpy as np
from f110_gym.envs.dynamic_models import vehicle_dynamics_ks, vehicle_dynamics_st, func_KS, func_ST
from f110_gym.envs.dynamic_models import (
vehicle_dynamics_ks,
vehicle_dynamics_st,
func_KS,
func_ST,
)


class DynamicsTest(unittest.TestCase):
Expand Down Expand Up @@ -560,4 +565,4 @@ def test_zeroinit_rollleft_singletrack(self):


if __name__ == "__main__":
unittest.main()
unittest.main()
6 changes: 4 additions & 2 deletions tests/test_f110_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

class TestEnvInterface(unittest.TestCase):
def _make_env(self, config={}):

conf = {
"map": "Example",
"num_agents": 1,
Expand All @@ -18,7 +17,10 @@ def _make_env(self, config={}):
}
conf = deep_update(conf, config)

env = gym.make("f110_gym:f110-v0", config=conf,)
env = gym.make(
"f110_gym:f110-v0",
config=conf,
)
return env

def test_gymnasium_api(self):
Expand Down
5 changes: 3 additions & 2 deletions tests/test_observation.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
class TestObservationInterface(unittest.TestCase):
@staticmethod
def _make_env(config={}) -> F110Env:

conf = {
"map": "Example",
"num_agents": 1,
Expand Down Expand Up @@ -228,5 +227,7 @@ def test_gymnasium_api(self):
for obs_type_id in obs_type_ids:
env = self._make_env(config={"observation_config": {"type": obs_type_id}})
check_env(
env.unwrapped, f"Observation {obs_type_id} breaks the gymnasium API", skip_render_check=True
env.unwrapped,
f"Observation {obs_type_id} breaks the gymnasium API",
skip_render_check=True,
)

0 comments on commit 9211767

Please sign in to comment.