Skip to content

Commit

Permalink
Merge pull request #11 from jerryz123/docs
Browse files Browse the repository at this point in the history
Filled in docs for state, env, and objects
  • Loading branch information
jerryz123 committed Nov 15, 2017
2 parents e430547 + 1bcbd88 commit 0b323ac
Show file tree
Hide file tree
Showing 16 changed files with 336 additions and 18 deletions.
3 changes: 2 additions & 1 deletion docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
'sphinx.ext.mathjax',
'sphinx.ext.viewcode',
'sphinx.ext.githubpages',
'sphinx.ext.autodoc']
'sphinx.ext.autodoc',
'sphinx.ext.napoleon']

# Add any paths that contain templates here, relative to this directory.
templates_path = ['_templates']
Expand Down
5 changes: 5 additions & 0 deletions docs/source/environment.rst
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
Environments
============

The UrbanDrivingEnvironment Class
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
.. autoclass:: gym_urbandriving.UrbanDrivingEnv
:members: _step, _reset, _render

Ray Integration
^^^^^^^^^^^^^^^
34 changes: 34 additions & 0 deletions docs/source/examples.rst
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,41 @@ in the scene. For every other object, it queries its internal list of agents as

Designing a State
^^^^^^^^^^^^^^^^^
Designing a state is very simple in UDS. To design a custom state, simply inherit from ``PositionState`` and implement your own array of ``static_objects`` and define your own ``randomize()`` function.

::

class CustomState(PositionState):
static_objects = [Terrain(175, 175, 350, 350),
Terrain(825, 175, 350, 350),
Terrain(175, 825, 350, 350),
Terrain(825, 825, 350, 350),
Lane(200, 450, 400, 100, angle=-180),
Lane(200, 550, 400, 100),
Lane(800, 450, 400, 100, angle=-180),
Lane(800, 550, 400, 100),
Lane(450, 200, 400, 100, angle=-90),
Lane(550, 200, 400, 100, angle=90),
Lane(450, 800, 400, 100, angle=-90),
Lane(550, 800, 400, 100, angle=90),
Street(500, 500, 200, 200),
Sidewalk(200, 375, 400, 50),
Sidewalk(200, 625, 400, 50),
Sidewalk(800, 375, 400, 50),
Sidewalk(800, 625, 400, 50),
Sidewalk(375, 175, 50, 350),
Sidewalk(625, 175, 50, 350),
Sidewalk(375, 825, 50, 350),
Sidewalk(625, 825, 50, 350),]

You should place the dynamic objects in the ``randomize()`` function


::
def randomize(self):
self.dynamic_objects = []
self.dynamic_objects.append(Car(500, 200))

Imitation Learning
^^^^^^^^^^^^^^^^^^
2 changes: 2 additions & 0 deletions docs/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@ This is Berkeley AUTOLAB's Urban Driving Simulator.
environment.rst
state.rst
objects.rst


17 changes: 16 additions & 1 deletion docs/source/objects.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,33 @@ Objects

Cars
^^^^
.. autoclass:: gym_urbandriving.assets.Car
:members: step, can_collide


Pedestrians
^^^^^^^^^^^

.. autoclass:: gym_urbandriving.assets.Pedestrian
:members: step, can_collide

Terrain
^^^^^^^
.. autoclass:: gym_urbandriving.assets.Terrain

Lanes
^^^^^
.. autoclass:: gym_urbandriving.assets.Lane
:members: generate_car


Sidewalks
^^^^^^^^^
.. autoclass:: gym_urbandriving.assets.Sidewalk
:members: generate_man



Streets
^^^^^^^
.. autoclass:: gym_urbandriving.assets.Street

10 changes: 10 additions & 0 deletions docs/source/state.rst
Original file line number Diff line number Diff line change
@@ -1,2 +1,12 @@
States
======

The Abstract PositionState
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
.. autoclass:: gym_urbandriving.state.PositionState
:members: get_collisions, collides_any, min_dist_to_coll

The Simple Intersection
^^^^^^^^^^^^^^^^^^^^^^^
.. autoclass:: gym_urbandriving.state.SimpleIntersectionState
:members: randomize
24 changes: 21 additions & 3 deletions gym_urbandriving/assets/car.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ def __init__(self, x, y, xdim=80, ydim=40, angle=0.0, vel=0.0,
self.l_f = self.l_r = self.ydim / 2.0

def step(self, action):
"""
Updates this object given this action input
Parameters
----------
action :
The action to take
"""
if self.dynamics_model == "kinematic":
self.kinematic_model_step(action)
else:
Expand All @@ -53,7 +61,7 @@ def step(self, action):
def point_model_step(self, action, info_dict=None):
"""
Updates the car for one timestep based on point model
Parameters
----------
action: 1x2 array,
Expand All @@ -80,7 +88,7 @@ def point_model_step(self, action, info_dict=None):
self.y += dy
self.vel += acc
self.vel = max(min(self.vel, self.max_vel), -self.max_vel)

def kinematic_model_step(self, action):
"""
Updates the car for one timestep.
Expand Down Expand Up @@ -118,7 +126,7 @@ def integrator(self, state, t, acc, delta_f):
equation variables for dynamics.
SciPy ODE integrator calls this function.
Args:
:
state: 1x6 array, contains x, y, dx_body, dy_body, rad_angle, rad_dangle
of car.
t: float, timestep.
Expand Down Expand Up @@ -152,6 +160,16 @@ def get_state(self):
def can_collide(self, other):
"""
Specifies whether this object can collide with another object
Parameters
----------
other :
Object to test collision against
Returns
-------
bool
True if this object can collide with other
"""
from gym_urbandriving.assets.lane import Lane
if type(other) in {Terrain, Sidewalk, Car, Pedestrian}:
Expand Down
33 changes: 32 additions & 1 deletion gym_urbandriving/assets/lane.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,42 @@


class Lane(Street):
"""
Represents a lane of road. Lanes have directionality, so cars should drive in the
right direction.
Parameters
----------
x : float
Upper left x coordinate of the lane block
y : float
Upper left y coordinate of the lane block
xdim : float
Width of the lane block
ydim : float
Height of the lane block
angle ; float
In degrees, the rotation of the lane block.
"""
def __init__(self, x, y, xdim, ydim, angle=0.0):
Rectangle.__init__(self, x, y, xdim, ydim, angle=angle, sprite="lane.png", static=True);

def generate_car(self, car_type="kinematic", randomize=True):
"""
Generates a car on the lane, facing the right direction.
Parameters
----------
car_type : "kinematic" or "point"
Specifies dynamics model for the car
randomize : bool
Specifies if the car's position is randomized
Returns
-------
Car
Generated Car object
"""
if randomize:
car = Car(0, 0, angle=self.angle+np.random.uniform(-10, 10),
dynamics_model=car_type)
Expand Down Expand Up @@ -36,4 +68,3 @@ def generate_car(self, car_type="kinematic", randomize=True):
x, y = x+self.x, y+self.y
car.x, car.y = x, y
return car

32 changes: 28 additions & 4 deletions gym_urbandriving/assets/pedestrian.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,26 @@
from gym_urbandriving.assets.terrain import Terrain

class Pedestrian(Circle):
"""
Represents a pedestrian as a circle
Parameters
----------
x : float
Center x coordinate
y : float
Center y coordinate
radius : float
Size of the pedestrian
angle : float
Initial orientation, in degrees
vel : float
Initial velocity
max_vel : float
Maximum velocity
mass : float
Mass of pedestrian
"""
def __init__(self, x, y, radius=17, angle=0.0, vel=0.0, acc=0.0, max_vel=2.0, mass=100.0):
Circle.__init__(self, x, y, radius, angle, sprite="person.png")
self.vel = vel
Expand All @@ -13,10 +33,14 @@ def __init__(self, x, y, radius=17, angle=0.0, vel=0.0, acc=0.0, max_vel=2.0, ma

def step(self, action, info_dict=None):
"""
Updates the car for one timestep.
Args:
action: 1x2 array, steering / acceleration action.
info_dict: dict, contains information about the environment.
Updates the pedestrian for one timestep.
Parameters
----------
action : 1x2 array
Steering / acceleration action.
info_dict : dict
Contains information about the environment.
"""
self.shapely_obj = None
if action is None:
Expand Down
24 changes: 23 additions & 1 deletion gym_urbandriving/assets/sidewalk.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,32 @@
from gym_urbandriving.assets.pedestrian import Pedestrian

class Sidewalk(Rectangle):
"""
Represents a block of sidewalk. Passable for pedestrians, not for cars
Parameters
----------
x : float
Upper left x coordinate of the sidewalk block
y : float
Upper left y coordinate of the sidewalk block
xdim : float
Width of the sidewalk block
ydim : float
Height of the sidewalk block
"""
def __init__(self, x, y, xdim, ydim):
Rectangle.__init__(self, x, y, xdim, ydim, sprite="gray.png", static=True);
Rectangle.__init__(self, x, y, xdim, ydim, sprite="gray.png", static=True)

def generate_man(self, man_type=Pedestrian):
"""
Generates a man on the sidewalk
Returns
-------
Pedestrian
Generated Pedestrian object
"""
man = man_type(0, 0, angle=self.angle)
angle = np.radians(-self.angle)
rotation_mat = np.array([[np.cos(angle), -np.sin(angle)],
Expand Down
20 changes: 18 additions & 2 deletions gym_urbandriving/assets/street.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,22 @@
from gym_urbandriving.assets.primitives.rectangle import Rectangle

class Street(Rectangle):
def __init__(self, x, y, xdim, ydim, angle=0.0, sprite="black.png"):
Rectangle.__init__(self, x, y, xdim, ydim, angle=0.0, sprite=sprite, static=True);
"""
Represents a block of street. Passable for cars and pedestrians.
Does not have directionality associated with it, so use this for
the middle of an intersection
Parameters
----------
x : float
Upper left x coordinate of the street block
y : float
Upper left y coordinate of the street block
xdim : float
Width of the street block
ydim : float
Height of the street block
"""
def __init__(self, x, y, xdim, ydim):
Rectangle.__init__(self, x, y, xdim, ydim, angle=0.0, sprite="black.png", static=True);

14 changes: 14 additions & 0 deletions gym_urbandriving/assets/terrain.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,20 @@
from gym_urbandriving.assets.primitives.rectangle import Rectangle

class Terrain(Rectangle):
"""
Represents a square of impassable terrain
Parameters
----------
x : float
Upper left x coordinate of the terrain block
y : float
Upper left y coordinate of the terrain block
xdim : float
Width of the terrain block
ydim : float
Height of the terrain block
"""
def __init__(self, x, y, xdim, ydim):
sprite = ["block1.png", "block2.png", "block3.png", "block4.png"][np.random.randint(0, 3)]
Rectangle.__init__(self, x, y, xdim, ydim, sprite="grass.jpg", static=True);
Expand Down

0 comments on commit 0b323ac

Please sign in to comment.