Skip to content
Snippets Groups Projects
Commit c6693614 authored by Armin Co's avatar Armin Co
Browse files

Added Collistion sensor

parent 95695810
No related branches found
No related tags found
No related merge requests found
...@@ -3,6 +3,8 @@ import glob ...@@ -3,6 +3,8 @@ import glob
import os import os
import random import random
import sys import sys
import math
import weakref
import numpy as np import numpy as np
import pygame import pygame
...@@ -64,13 +66,34 @@ class Camera: ...@@ -64,13 +66,34 @@ class Camera:
self.display.blit(self.surface, (0, 0)) self.display.blit(self.surface, (0, 0))
pygame.display.flip() pygame.display.flip()
class CollisionSensor:
sensor = None
def __init__(self, world, parent):
bp = world.get_blueprint_library().find('sensor.other.collision')
self.sensor = world.spawn_actor(bp, carla.Transform(), attach_to=parent)
weak_self = weakref.ref(self)
self.sensor.listen(lambda event: CollisionSensor._on_collision(weak_self, event))
def _on_collision(weak_self, event):
self = weak_self()
if not self:
return
print(event.other_actor)
impulse = event.normal_impulse
intensity = math.sqrt(impulse.x**2 + impulse.y**2 + impulse.z**2)
print(intensity)
class World: class World:
""" Wrapper for the carla environment, incl. player/vehicle """ """ Wrapper for the carla environment, incl. player/vehicle """
player = None player = None
collision_sensor = None
world = None world = None
blueprint_library = None blueprint_library = None
spawn_points = None spawn_points = None
actors = []
def __init__(self, world): def __init__(self, world):
self.world = world self.world = world
...@@ -82,28 +105,46 @@ class World: ...@@ -82,28 +105,46 @@ class World:
""" Remove and create new player/vehicle. """ """ Remove and create new player/vehicle. """
self.destroy() self.destroy()
self.spawn_player() self.spawn_player()
self.spawn_actors()
def spawn_player(self): def spawn_player(self):
""" Add a vehicle to the world. """ """ Add a vehicle to the world. """
while self.player is None: while self.player is None:
blueprint = random.choice(self.blueprint_library.filter('model3')) blueprint = random.choice(self.blueprint_library.filter('model3'))
position = random.choice(self.spawn_points) position = carla.Transform(carla.Location(x=-13, y=57.5, z=0.2), carla.Rotation(pitch=0.0, yaw=-180, roll=0.0))
self.player = self.world.try_spawn_actor(blueprint, position) self.player = self.world.try_spawn_actor(blueprint, position)
start_location = self.player.get_location() start_location = self.player.get_location()
print(str(start_location)) print(str(start_location))
self.collision_sensor = CollisionSensor(self.world, self.player)
# start_location.x = 288.0 # start_location.x = 288.0
# start_location.y = 55.0 # start_location.y = 55.0
# self.player.set_location(start_location) # self.player.set_location(start_location)
def spawn_actors(self):
blueprint = random.choice(self.blueprint_library.filter('etron'))
transforms = []
transforms.append(carla.Transform(carla.Location(x=-19.2, y=54.5, z=0.2), carla.Rotation(pitch=0.0, yaw=-180, roll=0.0)))
transforms.append(carla.Transform(carla.Location(x=-14.3, y=54.7, z=0.2), carla.Rotation(pitch=0.0, yaw=-180, roll=0.0)))
transforms.append(carla.Transform(carla.Location(x=-41.3, y=53.7, z=0.2), carla.Rotation(pitch=0.0, yaw=-180, roll=0.0)))
for transform in transforms:
actor = self.world.try_spawn_actor(blueprint, transform)
if actor is not None:
self.actors.append(actor)
def destroy(self): def destroy(self):
""" Remove vehicle from the world. """ """ Remove vehicle from the world. """
if self.player is not None: if self.player is not None:
self.player.destroy() self.player.destroy()
if self.actors is not None:
for actor in self.actors:
actor.destroy()
def step(self, action): def step(self, action):
""" Apply controls to vehicle. """ """ Apply controls to vehicle. """
self.player.apply_control(action) self.player.apply_control(action)
# print(str(self.player.get_location())) # print(str(self.player.get_transform()))
# print(str(self.player.get_velocity())) # print(str(self.player.get_velocity()))
......
...@@ -2,12 +2,12 @@ ...@@ -2,12 +2,12 @@
import pygame import pygame
DIRECTION=0 JOYSTICK_DIRECTION=0
THROTTLE=1 JOYSTICK_THROTTLE=1
BRAKES=2 JOYSTICK_BRAKES=2
INPUTS=3 JOYSTICK_REVERSE=3
OFFSET = 2.0 INPUT_OFFSET = 2.0
class ManualSteeringWheel: class ManualSteeringWheel:
""" Steering wheel """ """ Steering wheel """
...@@ -18,6 +18,7 @@ class ManualSteeringWheel: ...@@ -18,6 +18,7 @@ class ManualSteeringWheel:
brakes = 0.0 brakes = 0.0
reverse = False reverse = False
def __init__(self): def __init__(self):
self.joystick = pygame.joystick.Joystick(0) self.joystick = pygame.joystick.Joystick(0)
self.joystick.init() self.joystick.init()
...@@ -27,23 +28,22 @@ class ManualSteeringWheel: ...@@ -27,23 +28,22 @@ class ManualSteeringWheel:
""" Update and return direction. """ """ Update and return direction. """
if update: if update:
pygame.event.get() pygame.event.get()
self.direction = self.joystick.get_axis(DIRECTION) self.direction = self.joystick.get_axis(JOYSTICK_DIRECTION)
return self.direction return self.direction
def get_throttle(self, update=True): def get_throttle(self, update=True):
""" Update and return throttle.""" """ Update and return throttle."""
if update: if update:
pygame.event.get() pygame.event.get()
self.throttle = (OFFSET - (self.joystick.get_axis(THROTTLE) + 1.0)) / OFFSET self.throttle = round((INPUT_OFFSET - (self.joystick.get_axis(JOYSTICK_THROTTLE) + 1.0)) / INPUT_OFFSET, 3)
return self.throttle return self.throttle
def get_brakes(self, update=True): def get_brakes(self, update=True):
""" Update and return brakes. """ """ Update and return brakes. """
if update: if update:
pygame.event.get() pygame.event.get()
self.brakes = (OFFSET - (self.joystick.get_axis(BRAKES) + 1.0)) / OFFSET self.brakes = (INPUT_OFFSET - (self.joystick.get_axis(JOYSTICK_BRAKES) + 1.0)) / INPUT_OFFSET
if self.brakes < 0.01: self.brakes = round(self.brakes, 3)
self.brakes = 0.0
return self.brakes return self.brakes
def update_controls(self): def update_controls(self):
...@@ -53,6 +53,7 @@ class ManualSteeringWheel: ...@@ -53,6 +53,7 @@ class ManualSteeringWheel:
self.get_brakes(update=False) self.get_brakes(update=False)
def get_action(self): def get_action(self):
return [self.throttle, self.brakes, self.direction, self.reverse] return [self.throttle, self.brakes, self.direction, self.reverse]
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment