Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
L
LearningEnvironment
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Armin Co
LearningEnvironment
Commits
b1945b0e
Commit
b1945b0e
authored
Feb 3, 2021
by
Armin Co
Browse files
Options
Downloads
Patches
Plain Diff
Carla
parent
e15e11e3
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
carla_environment.py
+39
-43
39 additions, 43 deletions
carla_environment.py
steering_wheel.py
+43
-21
43 additions, 21 deletions
steering_wheel.py
with
82 additions
and
64 deletions
carla_environment.py
+
39
−
43
View file @
b1945b0e
...
...
@@ -7,6 +7,7 @@ import numpy as np
import
pygame
import
math
import
atexit
from
steering_wheel
import
Controller
# find carla module
try
:
...
...
@@ -19,34 +20,46 @@ except IndexError:
import
carla
class
Camera
:
"""
Add camera sensor to the carla world
"""
sensor
=
None
display
=
None
surface
=
None
width
=
None
height
=
None
surface
=
None
def
__init__
(
self
,
width
=
640
,
height
=
480
):
def
__init__
(
self
,
world
,
width
=
640
,
height
=
480
):
self
.
width
=
width
self
.
height
=
height
def
spawn
(
self
,
camera_actor
):
self
.
sensor
=
camera_actor
self
.
display
=
pygame
.
display
.
set_mode
(
(
width
,
height
),
pygame
.
HWSURFACE
|
pygame
.
DOUBLEBUF
)
self
.
display
.
fill
((
0
,
0
,
0
))
camera_blueprint
=
world
.
world
.
get_blueprint_library
().
find
(
'
sensor.camera.rgb
'
)
camera_transform
=
carla
.
Transform
(
carla
.
Location
(
x
=
1.5
,
z
=
2.4
))
camera_blueprint
.
set_attribute
(
'
image_size_x
'
,
f
'
{
self
.
width
}
'
)
camera_blueprint
.
set_attribute
(
'
image_size_y
'
,
f
'
{
self
.
height
}
'
)
camera_blueprint
.
set_attribute
(
'
fov
'
,
'
110
'
)
self
.
sensor
=
world
.
world
.
spawn_actor
(
camera_blueprint
,
camera_transform
,
attach_to
=
world
.
player
)
self
.
sensor
.
listen
(
lambda
data
:
self
.
process_img
(
data
))
def
process_img
(
self
,
data
):
array
=
np
.
frombuffer
(
data
.
raw_data
,
dtype
=
np
.
dtype
(
"
uint8
"
))
array
=
np
.
reshape
(
array
,
(
self
.
width
,
self
.
height
,
4
))
array
=
np
.
reshape
(
array
,
(
self
.
height
,
self
.
width
,
4
))
array
=
array
[:,
:,
:
3
]
array
=
array
[:,
:,
::
-
1
]
self
.
surface
=
pygame
.
surfarray
.
make_surface
(
array
.
swapaxes
(
0
,
1
))
def
on_update
(
self
):
if
self
.
surface
is
not
None
:
self
.
display
.
blit
(
self
.
surface
,
(
0
,
0
))
pygame
.
display
.
flip
()
class
World
:
"""
Wrapper for the carla environment, incl. player/vehicle
"""
player
=
None
world
=
None
camera
=
None
surface
=
None
blueprint_library
=
None
spawn_points
=
None
...
...
@@ -54,7 +67,6 @@ class World:
self
.
world
=
world
self
.
blueprint_library
=
self
.
world
.
get_blueprint_library
()
self
.
spawn_points
=
self
.
world
.
get_map
().
get_spawn_points
()
self
.
camera
=
Camera
()
self
.
reset
()
def
reset
(
self
):
...
...
@@ -66,58 +78,42 @@ class World:
blueprint
=
random
.
choice
(
self
.
blueprint_library
.
filter
(
'
vehicle
'
))
position
=
random
.
choice
(
self
.
spawn_points
)
self
.
player
=
self
.
world
.
try_spawn_actor
(
blueprint
,
position
)
self
.
player
.
set_autopilot
(
True
)
self
.
spawn_camera
()
def
spawn_camera
(
self
):
camera_blueprint
=
self
.
blueprint_library
.
find
(
'
sensor.camera.rgb
'
)
camera_transform
=
carla
.
Transform
(
carla
.
Location
(
x
=
1.5
,
z
=
2.4
))
camera_blueprint
.
set_attribute
(
'
image_size_x
'
,
f
'
{
self
.
camera
.
width
}
'
)
camera_blueprint
.
set_attribute
(
'
image_size_y
'
,
f
'
{
self
.
camera
.
height
}
'
)
camera_blueprint
.
set_attribute
(
'
fov
'
,
'
110
'
)
self
.
camera
.
spawn
(
self
.
world
.
spawn_actor
(
camera_blueprint
,
camera_transform
,
attach_to
=
self
.
player
))
def
destroy
(
self
):
if
self
.
player
is
not
None
:
self
.
player
.
destroy
()
def
__del__
(
self
):
self
.
player
.
destroy
()
def
step
(
self
,
action
):
self
.
player
.
apply_control
(
action
)
class
CarlaEnvironment
:
world
=
None
client
=
None
display
=
None
def
__init__
(
self
,
host
=
"
127.0.0.1
"
,
port
=
2000
,
width
=
1280
,
height
=
720
):
pygame
.
init
()
self
.
client
=
carla
.
Client
(
host
,
port
)
self
.
client
.
set_timeout
(
2.0
)
self
.
world
=
World
(
self
.
client
.
get_world
())
self
.
display
=
pygame
.
display
.
set_mode
(
(
width
,
height
),
pygame
.
HWSURFACE
|
pygame
.
DOUBLEBUF
)
self
.
display
.
fill
((
0
,
0
,
0
))
def
on_update
(
self
):
if
self
.
world
.
camera
.
surface
is
not
None
:
self
.
display
.
blit
(
self
.
world
.
camera
.
surface
,
(
0
,
0
))
pygame
.
display
.
flip
()
def
__del__
(
self
):
pygame
.
quit
()
def
step
(
self
,
action
):
control
=
carla
.
VehicleControl
(
throttle
=
action
[
0
],
steer
=
action
[
2
],
brake
=
action
[
1
])
self
.
world
.
step
(
control
)
if
__name__
==
"
__main__
"
:
pygame
.
init
()
env
=
CarlaEnvironment
()
cam
=
Camera
(
env
.
world
)
ctrl
=
Controller
()
running
=
True
while
running
:
env
.
on_update
()
for
event
in
pygame
.
event
.
get
():
if
event
.
type
==
pygame
.
KEYUP
:
if
event
.
key
==
pygame
.
K_q
:
running
=
False
while
ctrl
.
is_running
():
ctrl
.
on_update
()
env
.
step
(
ctrl
.
get_action
())
cam
.
on_update
()
env
.
world
.
destroy
()
env
=
None
pygame
.
quit
()
This diff is collapsed.
Click to expand it.
steering_wheel.py
+
43
−
21
View file @
b1945b0e
...
...
@@ -3,22 +3,22 @@
import
pygame
DIRECTION
=
0
SPEED
=
1
BR
E
AKS
=
2
THROTTLE
=
1
BRAK
E
S
=
2
INPUTS
=
3
OFFSET
=
2.0
class
ManualSteeringWheel
:
"""
Steering wheel
"""
axis_count
=
0
axis_count
=
0
.0
joystick
=
None
direction
=
0
speed
=
0
breaks
=
0
direction
=
0.0
throttle
=
0.0
brakes
=
0.0
reverse
=
False
def
__init__
(
self
):
pygame
.
init
()
self
.
joystick
=
pygame
.
joystick
.
Joystick
(
0
)
self
.
joystick
.
init
()
self
.
axis_count
=
self
.
joystick
.
get_numaxes
()
...
...
@@ -30,28 +30,50 @@ class ManualSteeringWheel:
self
.
direction
=
self
.
joystick
.
get_axis
(
DIRECTION
)
return
self
.
direction
def
get_
speed
(
self
,
update
=
True
):
"""
Update and return
speed
.
"""
def
get_
throttle
(
self
,
update
=
True
):
"""
Update and return
throttle
.
"""
if
update
:
pygame
.
event
.
get
()
self
.
speed
=
(
OFFSET
-
(
self
.
joystick
.
get_axis
(
SPEED
)
+
1.0
))
/
OFFSET
return
self
.
speed
self
.
throttle
=
(
OFFSET
-
(
self
.
joystick
.
get_axis
(
THROTTLE
)
+
1.0
))
/
OFFSET
return
self
.
throttle
def
get_br
e
aks
(
self
,
update
=
True
):
"""
Update and return br
e
aks.
"""
def
get_brak
e
s
(
self
,
update
=
True
):
"""
Update and return brak
e
s.
"""
if
update
:
pygame
.
event
.
get
()
self
.
breaks
=
(
OFFSET
-
(
self
.
joystick
.
get_axis
(
BREAKS
)
+
1.0
))
/
OFFSET
return
self
.
breaks
self
.
brakes
=
(
OFFSET
-
(
self
.
joystick
.
get_axis
(
BRAKES
)
+
1.0
))
/
OFFSET
if
self
.
brakes
<
0.01
:
self
.
brakes
=
0.0
return
self
.
brakes
def
on_
update
(
self
):
def
update
_controls
(
self
):
"""
Poll for pygame events.
"""
pygame
.
event
.
get
()
self
.
get_direction
(
update
=
False
)
self
.
get_speed
(
update
=
False
)
self
.
get_breaks
(
update
=
False
)
self
.
get_throttle
(
update
=
False
)
self
.
get_brakes
(
update
=
False
)
def
get_action
(
self
):
return
[
self
.
throttle
,
self
.
brakes
,
self
.
direction
,
self
.
reverse
]
class
Controller
(
ManualSteeringWheel
):
running
=
True
def
is_running
(
self
):
return
self
.
running
def
on_update
(
self
):
for
event
in
pygame
.
event
.
get
():
if
event
.
type
==
pygame
.
KEYUP
:
if
event
.
key
==
pygame
.
K_r
:
self
.
reverse
=
not
self
.
reverse
elif
event
.
key
==
pygame
.
K_q
:
self
.
running
=
False
self
.
update_controls
()
if
__name__
==
'
__main__
'
:
pygame
.
init
()
clock
=
pygame
.
time
.
Clock
()
sw
=
ManualSteeringWheel
()
...
...
@@ -59,6 +81,6 @@ if __name__ == '__main__':
# Receive all occured events
print
(
""
)
print
(
"
Direction:
"
+
str
(
sw
.
get_direction
()))
print
(
"
Speed:
"
+
str
(
sw
.
get_
speed
()))
print
(
"
Br
e
aks:
"
+
str
(
sw
.
get_br
e
aks
()))
print
(
"
Speed:
"
+
str
(
sw
.
get_
throttle
()))
print
(
"
Brak
e
s:
"
+
str
(
sw
.
get_brak
e
s
()))
clock
.
tick
(
2
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment